11import * as path from "path" ;
22import { EmitHost } from "./transpilation" ;
3+ import * as lua from "./LuaAST" ;
34
45export enum LuaLibFeature {
56 ArrayConcat = "ArrayConcat" ,
@@ -99,81 +100,51 @@ export enum LuaLibFeature {
99100 Unpack = "Unpack" ,
100101}
101102
102- /* eslint-disable @typescript-eslint/naming-convention */
103- const luaLibDependencies : Partial < Record < LuaLibFeature , LuaLibFeature [ ] > > = {
104- ArrayConcat : [ LuaLibFeature . ArrayIsArray ] ,
105- ArrayFlat : [ LuaLibFeature . ArrayConcat , LuaLibFeature . ArrayIsArray ] ,
106- ArrayFlatMap : [ LuaLibFeature . ArrayConcat , LuaLibFeature . ArrayIsArray ] ,
107- Await : [ LuaLibFeature . InstanceOf , LuaLibFeature . New , LuaLibFeature . Promise ] ,
108- Decorate : [ LuaLibFeature . ObjectGetOwnPropertyDescriptor , LuaLibFeature . SetDescriptor , LuaLibFeature . ObjectAssign ] ,
109- DelegatedYield : [ LuaLibFeature . StringAccess ] ,
110- Delete : [ LuaLibFeature . ObjectGetOwnPropertyDescriptors , LuaLibFeature . Error , LuaLibFeature . New ] ,
111- Error : [ LuaLibFeature . Class , LuaLibFeature . ClassExtends , LuaLibFeature . New ] ,
112- FunctionBind : [ LuaLibFeature . Unpack ] ,
113- Generator : [ LuaLibFeature . Symbol ] ,
114- InstanceOf : [ LuaLibFeature . Symbol ] ,
115- Iterator : [ LuaLibFeature . Symbol ] ,
116- NumberToString : [ LuaLibFeature . StringAccess ] ,
117- ObjectDefineProperty : [ LuaLibFeature . CloneDescriptor , LuaLibFeature . SetDescriptor ] ,
118- ObjectFromEntries : [ LuaLibFeature . Iterator , LuaLibFeature . Symbol ] ,
119- Promise : [
120- LuaLibFeature . ArrayPush ,
121- LuaLibFeature . Class ,
122- LuaLibFeature . FunctionBind ,
123- LuaLibFeature . InstanceOf ,
124- LuaLibFeature . New ,
125- ] ,
126- PromiseAll : [ LuaLibFeature . InstanceOf , LuaLibFeature . New , LuaLibFeature . Promise , LuaLibFeature . Iterator ] ,
127- PromiseAllSettled : [ LuaLibFeature . InstanceOf , LuaLibFeature . New , LuaLibFeature . Promise , LuaLibFeature . Iterator ] ,
128- PromiseAny : [
129- LuaLibFeature . ArrayPush ,
130- LuaLibFeature . InstanceOf ,
131- LuaLibFeature . New ,
132- LuaLibFeature . Promise ,
133- LuaLibFeature . Iterator ,
134- ] ,
135- PromiseRace : [
136- LuaLibFeature . ArrayPush ,
137- LuaLibFeature . InstanceOf ,
138- LuaLibFeature . New ,
139- LuaLibFeature . Promise ,
140- LuaLibFeature . Iterator ,
141- ] ,
142- ParseFloat : [ LuaLibFeature . StringAccess ] ,
143- ParseInt : [ LuaLibFeature . StringSubstr , LuaLibFeature . StringSubstring ] ,
144- SetDescriptor : [ LuaLibFeature . CloneDescriptor ] ,
145- Spread : [ LuaLibFeature . Iterator , LuaLibFeature . StringAccess , LuaLibFeature . Unpack ] ,
146- StringSplit : [ LuaLibFeature . StringSubstring , LuaLibFeature . StringAccess ] ,
147- SymbolRegistry : [ LuaLibFeature . Symbol ] ,
148-
149- Map : [ LuaLibFeature . InstanceOf , LuaLibFeature . Iterator , LuaLibFeature . Symbol , LuaLibFeature . Class ] ,
150- Set : [ LuaLibFeature . InstanceOf , LuaLibFeature . Iterator , LuaLibFeature . Symbol , LuaLibFeature . Class ] ,
151- WeakMap : [ LuaLibFeature . InstanceOf , LuaLibFeature . Iterator , LuaLibFeature . Symbol , LuaLibFeature . Class ] ,
152- WeakSet : [ LuaLibFeature . InstanceOf , LuaLibFeature . Iterator , LuaLibFeature . Symbol , LuaLibFeature . Class ] ,
153- } ;
154- /* eslint-enable @typescript-eslint/naming-convention */
155-
156- export function loadLuaLibFeatures ( features : Iterable < LuaLibFeature > , emitHost : EmitHost ) : string {
103+ export interface LuaLibFeatureInfo {
104+ dependencies ?: LuaLibFeature [ ] ;
105+ exports : string [ ] ;
106+ }
107+ export type LuaLibModulesInfo = Record < LuaLibFeature , LuaLibFeatureInfo > ;
108+
109+ let luaLibModuleInfo : LuaLibModulesInfo | undefined ;
110+ export function getLuaLibModuleInfo ( emitHost : EmitHost ) : LuaLibModulesInfo {
111+ if ( luaLibModuleInfo === undefined ) {
112+ const lualibPath = path . resolve ( __dirname , "../dist/lualib/lualib_dependencies.json" ) ;
113+ const result = emitHost . readFile ( lualibPath ) ;
114+ if ( result !== undefined ) {
115+ luaLibModuleInfo = JSON . parse ( result ) as LuaLibModulesInfo ;
116+ } else {
117+ throw new Error ( `Could not load lualib dependencies from '${ lualibPath } '` ) ;
118+ }
119+ }
120+ return luaLibModuleInfo ;
121+ }
122+
123+ export function readLuaLibFeature ( feature : LuaLibFeature , emitHost : EmitHost ) : string {
124+ const featurePath = path . resolve ( __dirname , `../dist/lualib/${ feature } .lua` ) ;
125+ const luaLibFeature = emitHost . readFile ( featurePath ) ;
126+ if ( luaLibFeature === undefined ) {
127+ throw new Error ( `Could not load lualib feature from '${ featurePath } '` ) ;
128+ }
129+ return luaLibFeature ;
130+ }
131+
132+ export function loadInlineLualibFeatures ( features : Iterable < LuaLibFeature > , emitHost : EmitHost ) : string {
157133 let result = "" ;
158134
159135 const loadedFeatures = new Set < LuaLibFeature > ( ) ;
160136
137+ const luaLibDependencyMap = getLuaLibModuleInfo ( emitHost ) ;
161138 function load ( feature : LuaLibFeature ) : void {
162139 if ( loadedFeatures . has ( feature ) ) return ;
163140 loadedFeatures . add ( feature ) ;
164141
165- const dependencies = luaLibDependencies [ feature ] ;
142+ const dependencies = luaLibDependencyMap [ feature ] ?. dependencies ;
166143 if ( dependencies ) {
167144 dependencies . forEach ( load ) ;
168145 }
169-
170- const featurePath = path . resolve ( __dirname , `../dist/lualib/${ feature } .lua` ) ;
171- const luaLibFeature = emitHost . readFile ( featurePath ) ;
172- if ( luaLibFeature !== undefined ) {
173- result += luaLibFeature + "\n" ;
174- } else {
175- throw new Error ( `Could not load lualib feature from '${ featurePath } '` ) ;
176- }
146+ const luaLibFeature = readLuaLibFeature ( feature , emitHost ) ;
147+ result += luaLibFeature + "\n" ;
177148 }
178149
179150 for ( const feature of features ) {
@@ -183,6 +154,40 @@ export function loadLuaLibFeatures(features: Iterable<LuaLibFeature>, emitHost:
183154 return result ;
184155}
185156
157+ export function loadImportedLualibFeatures (
158+ features : Iterable < LuaLibFeature > ,
159+ emitHost : EmitHost ,
160+ alwaysRequire = false
161+ ) : lua . Statement [ ] {
162+ const luaLibModuleInfo = getLuaLibModuleInfo ( emitHost ) ;
163+
164+ const imports = Array . from ( features ) . flatMap ( feature => luaLibModuleInfo [ feature ] . exports ) ;
165+
166+ const requireCall = lua . createCallExpression ( lua . createIdentifier ( "require" ) , [
167+ lua . createStringLiteral ( "lualib_bundle" ) ,
168+ ] ) ;
169+ if ( imports . length === 0 ) {
170+ if ( alwaysRequire ) {
171+ return [ lua . createExpressionStatement ( requireCall ) ] ;
172+ }
173+ return [ ] ;
174+ }
175+
176+ const luaLibId = lua . createIdentifier ( "____lualib" ) ;
177+ const importStatement = lua . createVariableDeclarationStatement ( luaLibId , requireCall ) ;
178+ const statements : lua . Statement [ ] = [ importStatement ] ;
179+ // local <export> = ____luaLib.<export>
180+ for ( const item of imports ) {
181+ statements . push (
182+ lua . createVariableDeclarationStatement (
183+ lua . createIdentifier ( item ) ,
184+ lua . createTableIndexExpression ( luaLibId , lua . createStringLiteral ( item ) )
185+ )
186+ ) ;
187+ }
188+ return statements ;
189+ }
190+
186191let luaLibBundleContent : string ;
187192export function getLuaLibBundle ( emitHost : EmitHost ) : string {
188193 if ( luaLibBundleContent === undefined ) {
0 commit comments