Skip to content

Commit a1abb9e

Browse files
committed
feat: build another copy of lualib for 5.0
1 parent cc427a2 commit a1abb9e

File tree

6 files changed

+57
-19
lines changed

6 files changed

+57
-19
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
"dist/**/*.lua",
2020
"dist/**/*.ts",
2121
"dist/lualib/*.json",
22+
"dist/lualib-lua50/*.json",
2223
"language-extensions/**/*.ts"
2324
],
2425
"main": "dist/index.js",
2526
"types": "dist/index.d.ts",
2627
"scripts": {
2728
"build": "tsc && npm run build-lualib",
28-
"build-lualib": "node dist/tstl.js -p src/lualib/tsconfig.json",
29+
"build-lualib": "node dist/tstl.js -p src/lualib/tsconfig.json && node dist/tstl.js -p src/lualib-lua50/tsconfig.json",
2930
"pretest": "npm run lint && npm run check:language-extensions && npm run build-lualib",
3031
"test": "jest",
3132
"lint": "npm run lint:eslint && npm run lint:prettier",

src/LuaLib.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as path from "path";
22
import { EmitHost } from "./transpilation";
33
import * as lua from "./LuaAST";
4+
import { LuaTarget } from "./CompilerOptions";
45

56
export enum LuaLibFeature {
67
ArrayConcat = "ArrayConcat",
@@ -113,9 +114,10 @@ export type LuaLibModulesInfo = Record<LuaLibFeature, LuaLibFeatureInfo>;
113114

114115
export const luaLibModulesInfoFileName = "lualib_module_info.json";
115116
let 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

138141
export 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

203215
let 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;

src/LuaPrinter.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as path from "path";
22
import { Mapping, SourceMapGenerator, SourceNode } from "source-map";
33
import * as ts from "typescript";
4-
import { CompilerOptions, isBundleEnabled, LuaLibImportKind } from "./CompilerOptions";
4+
import { CompilerOptions, isBundleEnabled, LuaLibImportKind, LuaTarget } from "./CompilerOptions";
55
import * as lua from "./LuaAST";
66
import { loadInlineLualibFeatures, LuaLibFeature, loadImportedLualibFeatures } from "./LuaLib";
77
import { isValidLuaIdentifier, shouldAllowUnicode } from "./transformation/utils/safe-names";
@@ -233,14 +233,15 @@ export class LuaPrinter {
233233
sourceChunks.push(tstlHeader);
234234
}
235235

236+
const luaTarget = this.options.luaTarget ?? LuaTarget.Lua54;
236237
const luaLibImport = this.options.luaLibImport ?? LuaLibImportKind.Require;
237238
if (luaLibImport === LuaLibImportKind.Require && file.luaLibFeatures.size > 0) {
238239
// Import lualib features
239-
sourceChunks = this.printStatementArray(loadImportedLualibFeatures(file.luaLibFeatures, this.emitHost));
240+
sourceChunks = this.printStatementArray(loadImportedLualibFeatures(file.luaLibFeatures, luaTarget, this.emitHost));
240241
} else if (luaLibImport === LuaLibImportKind.Inline && file.luaLibFeatures.size > 0) {
241242
// Inline lualib features
242243
sourceChunks.push("-- Lua Library inline imports\n");
243-
sourceChunks.push(loadInlineLualibFeatures(file.luaLibFeatures, this.emitHost));
244+
sourceChunks.push(loadInlineLualibFeatures(file.luaLibFeatures, luaTarget, this.emitHost));
244245
sourceChunks.push("-- End of Lua Library inline imports\n");
245246
}
246247

src/lualib-build/plugin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class LuaLibPlugin implements tstl.Plugin {
4747

4848
// Figure out the order required in the bundle by recursively resolving all dependency features
4949
const allFeatures = Object.values(LuaLibFeature) as LuaLibFeature[];
50-
const orderedFeatures = resolveRecursiveLualibFeatures(allFeatures, emitHost, luaLibModuleInfo);
50+
const luaTarget = options.luaTarget ?? tstl.LuaTarget.Lua54;
51+
const orderedFeatures = resolveRecursiveLualibFeatures(allFeatures, luaTarget, emitHost, luaLibModuleInfo);
5152

5253
// Concatenate lualib files into bundle with exports table and add lualib_bundle.lua to results
5354
let lualibBundle = orderedFeatures.map(f => exportedLualibFeatures.get(LuaLibFeature[f])).join("\n");

src/lualib-lua50/tsconfig.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "../../dist/lualib-lua50",
4+
"target": "esnext",
5+
"lib": ["esnext"],
6+
// Counterintuitively, we do not build with the 5.0 types as they are incompatible with
7+
// branches written for the universal target. For now, the 5.4 types work for both targets.
8+
"types": ["lua-types/5.4"],
9+
"skipLibCheck": true,
10+
11+
"noUnusedLocals": true,
12+
"noUnusedParameters": true
13+
},
14+
"tstl": {
15+
"luaLibImport": "none",
16+
"noHeader": true,
17+
"luaTarget": "5.0",
18+
"luaPlugins": [{ "name": "../../dist/lualib-build/plugin.js" }]
19+
},
20+
"include": ["../lualib", "../../language-extensions/index.d.ts"]
21+
}

src/transpilation/transpiler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from "path";
22
import * as ts from "typescript";
3-
import { CompilerOptions, isBundleEnabled } from "../CompilerOptions";
3+
import { CompilerOptions, isBundleEnabled, LuaTarget } from "../CompilerOptions";
44
import { getLuaLibBundle } from "../LuaLib";
55
import { normalizeSlashes, trimExtension } from "../utils";
66
import { getBundleResult } from "./bundle";
@@ -126,7 +126,8 @@ export class Transpiler {
126126

127127
// Add lualib bundle to source dir 'virtually', will be moved to correct output dir in emitPlan
128128
const fileName = normalizeSlashes(path.resolve(getSourceDir(program), "lualib_bundle.lua"));
129-
resolutionResult.resolvedFiles.unshift({ fileName, code: getLuaLibBundle(this.emitHost) });
129+
const luaTarget = options.luaTarget ?? LuaTarget.Lua54;
130+
resolutionResult.resolvedFiles.unshift({ fileName, code: getLuaLibBundle(luaTarget, this.emitHost) });
130131
}
131132

132133
let emitPlan: EmitFile[];

0 commit comments

Comments
 (0)