11import * as path from "path" ;
22import { EmitHost } from "./transpilation" ;
33import * as lua from "./LuaAST" ;
4+ import { LuaTarget } from "./CompilerOptions" ;
45
56export enum LuaLibFeature {
67 ArrayConcat = "ArrayConcat" ,
@@ -113,9 +114,10 @@ export type LuaLibModulesInfo = Record<LuaLibFeature, LuaLibFeatureInfo>;
113114
114115export const luaLibModulesInfoFileName = "lualib_module_info.json" ;
115116let luaLibModulesInfo : LuaLibModulesInfo | undefined ;
116- export function getLuaLibModulesInfo ( emitHost : EmitHost ) : LuaLibModulesInfo {
117+ export function getLuaLibModulesInfo ( luaTarget : LuaTarget , emitHost : EmitHost ) : LuaLibModulesInfo {
117118 if ( luaLibModulesInfo === undefined ) {
118- const lualibPath = path . resolve ( __dirname , `../dist/lualib/${ luaLibModulesInfoFileName } ` ) ;
119+ const lualibDir = `../dist/${ luaTarget === LuaTarget . Lua50 ? "lualib-lua50" : "lualib" } ` ;
120+ const lualibPath = path . resolve ( __dirname , `${ lualibDir } /${ luaLibModulesInfoFileName } ` ) ;
119121 const result = emitHost . readFile ( lualibPath ) ;
120122 if ( result !== undefined ) {
121123 luaLibModulesInfo = JSON . parse ( result ) as LuaLibModulesInfo ;
@@ -126,8 +128,9 @@ export function getLuaLibModulesInfo(emitHost: EmitHost): LuaLibModulesInfo {
126128 return luaLibModulesInfo ;
127129}
128130
129- export function readLuaLibFeature ( feature : LuaLibFeature , emitHost : EmitHost ) : string {
130- const featurePath = path . resolve ( __dirname , `../dist/lualib/${ feature } .lua` ) ;
131+ export function readLuaLibFeature ( feature : LuaLibFeature , luaTarget : LuaTarget , emitHost : EmitHost ) : string {
132+ const lualibDir = `../dist/${ luaTarget === LuaTarget . Lua50 ? "lualib-lua50" : "lualib" } ` ;
133+ const featurePath = path . resolve ( __dirname , `${ lualibDir } /${ feature } .lua` ) ;
131134 const luaLibFeature = emitHost . readFile ( featurePath ) ;
132135 if ( luaLibFeature === undefined ) {
133136 throw new Error ( `Could not load lualib feature from '${ featurePath } '` ) ;
@@ -137,8 +140,9 @@ export function readLuaLibFeature(feature: LuaLibFeature, emitHost: EmitHost): s
137140
138141export function resolveRecursiveLualibFeatures (
139142 features : Iterable < LuaLibFeature > ,
143+ luaTarget : LuaTarget ,
140144 emitHost : EmitHost ,
141- luaLibModulesInfo : LuaLibModulesInfo = getLuaLibModulesInfo ( emitHost )
145+ luaLibModulesInfo : LuaLibModulesInfo = getLuaLibModulesInfo ( luaTarget , emitHost )
142146) : LuaLibFeature [ ] {
143147 const loadedFeatures = new Set < LuaLibFeature > ( ) ;
144148 const result : LuaLibFeature [ ] = [ ] ;
@@ -162,19 +166,27 @@ export function resolveRecursiveLualibFeatures(
162166 return result ;
163167}
164168
165- export function loadInlineLualibFeatures ( features : Iterable < LuaLibFeature > , emitHost : EmitHost ) : string {
169+ export function loadInlineLualibFeatures (
170+ features : Iterable < LuaLibFeature > ,
171+ luaTarget : LuaTarget ,
172+ emitHost : EmitHost
173+ ) : string {
166174 let result = "" ;
167175
168- for ( const feature of resolveRecursiveLualibFeatures ( features , emitHost ) ) {
169- const luaLibFeature = readLuaLibFeature ( feature , emitHost ) ;
176+ for ( const feature of resolveRecursiveLualibFeatures ( features , luaTarget , emitHost ) ) {
177+ const luaLibFeature = readLuaLibFeature ( feature , luaTarget , emitHost ) ;
170178 result += luaLibFeature + "\n" ;
171179 }
172180
173181 return result ;
174182}
175183
176- export function loadImportedLualibFeatures ( features : Iterable < LuaLibFeature > , emitHost : EmitHost ) : lua . Statement [ ] {
177- const luaLibModuleInfo = getLuaLibModulesInfo ( emitHost ) ;
184+ export function loadImportedLualibFeatures (
185+ features : Iterable < LuaLibFeature > ,
186+ luaTarget : LuaTarget ,
187+ emitHost : EmitHost
188+ ) : lua . Statement [ ] {
189+ const luaLibModuleInfo = getLuaLibModulesInfo ( luaTarget , emitHost ) ;
178190
179191 const imports = Array . from ( features ) . flatMap ( feature => luaLibModuleInfo [ feature ] . exports ) ;
180192
@@ -201,9 +213,10 @@ export function loadImportedLualibFeatures(features: Iterable<LuaLibFeature>, em
201213}
202214
203215let luaLibBundleContent : string ;
204- export function getLuaLibBundle ( emitHost : EmitHost ) : string {
216+ export function getLuaLibBundle ( luaTarget : LuaTarget , emitHost : EmitHost ) : string {
205217 if ( luaLibBundleContent === undefined ) {
206- const lualibPath = path . resolve ( __dirname , "../dist/lualib/lualib_bundle.lua" ) ;
218+ const lualibDir = `../dist/${ luaTarget === LuaTarget . Lua50 ? "lualib-lua50" : "lualib" } ` ;
219+ const lualibPath = path . resolve ( __dirname , `${ lualibDir } /lualib_bundle.lua` ) ;
207220 const result = emitHost . readFile ( lualibPath ) ;
208221 if ( result !== undefined ) {
209222 luaLibBundleContent = result ;
0 commit comments