@@ -24,6 +24,24 @@ export enum LuaTarget {
2424 LuaJIT = "JIT" ,
2525}
2626
27+ export enum LuaLibFeature {
28+ ArrayEvery = "ArrayEvery" ,
29+ ArrayFilter = "ArrayFilter" ,
30+ ArrayForEach = "ArrayForEach" ,
31+ ArrayIndexOf = "ArrayIndexOf" ,
32+ ArrayMap = "ArrayMap" ,
33+ ArrayPush = "ArrayPush" ,
34+ ArraySlice = "ArraySlice" ,
35+ ArraySome = "ArraySome" ,
36+ ArraySplice = "ArraySplice" ,
37+ InstanceOf = "InstanceOf" ,
38+ Map = "Map" ,
39+ Set = "Set" ,
40+ StringReplace = "StringReplace" ,
41+ StringSplit = "StringSplit" ,
42+ Ternary = "Ternary" ,
43+ }
44+
2745interface ExportInfo {
2846 name : string | ts . __String ;
2947 node : ts . Node ;
@@ -46,6 +64,8 @@ export abstract class LuaTranspiler {
4664 public classStack : string [ ] ;
4765 public exportStack : ExportInfo [ ] [ ] ;
4866
67+ public luaLibFeatureSet : Set < LuaLibFeature > ;
68+
4969 constructor ( checker : ts . TypeChecker , options : CompilerOptions , sourceFile : ts . SourceFile ) {
5070 this . indent = "" ;
5171 this . checker = checker ;
@@ -59,6 +79,7 @@ export abstract class LuaTranspiler {
5979 this . loopStack = [ ] ;
6080 this . classStack = [ ] ;
6181 this . exportStack = [ ] ;
82+ this . luaLibFeatureSet = new Set < LuaLibFeature > ( ) ;
6283 }
6384
6485 public pushIndent ( ) : void {
@@ -110,6 +131,11 @@ export abstract class LuaTranspiler {
110131 return result ;
111132 }
112133
134+ public importLuaLibFeature ( feature : LuaLibFeature ) {
135+ // TODO inline imported features in output i option set
136+ this . luaLibFeatureSet . add ( feature ) ;
137+ }
138+
113139 public getAbsouluteImportPath ( relativePath : string ) {
114140 if ( relativePath . charAt ( 0 ) !== "." && this . options . baseUrl ) {
115141 return path . resolve ( this . options . baseUrl , relativePath ) ;
@@ -232,6 +258,11 @@ export abstract class LuaTranspiler {
232258 }
233259 }
234260
261+ public transpileLuaLibFunction ( func : LuaLibFeature , ...params : string [ ] ) : string {
262+ this . importLuaLibFeature ( func ) ;
263+ return `__TS__${ func } (${ params . join ( ", " ) } )` ;
264+ }
265+
235266 public transpileImport ( node : ts . ImportDeclaration ) : string {
236267 const importPath = this . transpileExpression ( node . moduleSpecifier ) ;
237268 const importPathWithoutQuotes = importPath . replace ( new RegExp ( "\"" , "g" ) , "" ) ;
@@ -779,7 +810,7 @@ export abstract class LuaTranspiler {
779810 result = `${ rhs } [${ lhs } ]~=nil` ;
780811 break ;
781812 case ts . SyntaxKind . InstanceOfKeyword :
782- result = `TS_instanceof( ${ lhs } , ${ rhs } )` ;
813+ result = this . transpileLuaLibFunction ( LuaLibFeature . InstanceOf , lhs , rhs ) ;
783814 break ;
784815 default :
785816 throw new TranspileError (
@@ -823,7 +854,8 @@ export abstract class LuaTranspiler {
823854 const val1 = this . transpileExpression ( node . whenTrue ) ;
824855 const val2 = this . transpileExpression ( node . whenFalse ) ;
825856
826- return `TS_ITE(${ condition } ,function() return ${ val1 } end,function() return ${ val2 } end)` ;
857+ return this . transpileLuaLibFunction ( LuaLibFeature . Ternary , condition ,
858+ `function() return ${ val1 } end` , `function() return ${ val2 } end)` ) ;
827859 }
828860
829861 public transpilePostfixUnaryExpression ( node : ts . PostfixUnaryExpression ) : string {
@@ -949,7 +981,7 @@ export abstract class LuaTranspiler {
949981 const caller = this . transpileExpression ( expression . expression ) ;
950982 switch ( expression . name . escapedText ) {
951983 case "replace" :
952- return `TS_replace( ${ caller } , ${ params } )` ;
984+ return this . transpileLuaLibFunction ( LuaLibFeature . StringReplace , caller , params ) ;
953985 case "indexOf" :
954986 if ( node . arguments . length === 1 ) {
955987 return `(string.find(${ caller } ,${ params } ,1,true) or 0)-1` ;
@@ -969,7 +1001,7 @@ export abstract class LuaTranspiler {
9691001 case "toUpperCase" :
9701002 return `string.upper(${ caller } )` ;
9711003 case "split" :
972- return `TS_split( ${ caller } , ${ params } )` ;
1004+ return this . transpileLuaLibFunction ( LuaLibFeature . StringSplit , caller , params ) ;
9731005 case "charAt" :
9741006 return `string.sub(${ caller } ,${ params } +1,${ params } +1)` ;
9751007 default :
@@ -1002,23 +1034,23 @@ export abstract class LuaTranspiler {
10021034 const caller = this . transpileExpression ( expression . expression ) ;
10031035 switch ( expression . name . escapedText ) {
10041036 case "push" :
1005- return `TS_push( ${ caller } , ${ params } )` ;
1037+ return this . transpileLuaLibFunction ( LuaLibFeature . ArrayPush , caller , params ) ;
10061038 case "forEach" :
1007- return `TS_forEach( ${ caller } , ${ params } )` ;
1039+ return this . transpileLuaLibFunction ( LuaLibFeature . ArrayForEach , caller , params ) ;
10081040 case "indexOf" :
1009- return `TS_indexOf( ${ caller } , ${ params } )` ;
1041+ return this . transpileLuaLibFunction ( LuaLibFeature . ArrayIndexOf , caller , params ) ;
10101042 case "map" :
1011- return `TS_map( ${ caller } , ${ params } )` ;
1043+ return this . transpileLuaLibFunction ( LuaLibFeature . ArrayMap , caller , params ) ;
10121044 case "filter" :
1013- return `TS_filter( ${ caller } , ${ params } )` ;
1045+ return this . transpileLuaLibFunction ( LuaLibFeature . ArrayFilter , caller , params ) ;
10141046 case "some" :
1015- return `TS_some( ${ caller } , ${ params } )` ;
1047+ return this . transpileLuaLibFunction ( LuaLibFeature . ArraySome , caller , params ) ;
10161048 case "every" :
1017- return `TS_every( ${ caller } , ${ params } )` ;
1049+ return this . transpileLuaLibFunction ( LuaLibFeature . ArrayEvery , caller , params ) ;
10181050 case "slice" :
1019- return `TS_slice( ${ caller } , ${ params } )` ;
1051+ return this . transpileLuaLibFunction ( LuaLibFeature . ArraySlice , caller , params ) ;
10201052 case "splice" :
1021- return `TS_splice( ${ caller } , ${ params } )` ;
1053+ return this . transpileLuaLibFunction ( LuaLibFeature . ArraySplice , caller , params ) ;
10221054 case "join" :
10231055 if ( node . arguments . length === 0 ) {
10241056 // if seperator is omitted default seperator is ","
0 commit comments