Skip to content

Commit ae34353

Browse files
committed
Make LuaLib.loadFeatures a regular function
1 parent f17da36 commit ae34353

3 files changed

Lines changed: 27 additions & 31 deletions

File tree

build_lualib.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from "fs";
22
import * as path from "path";
33
import * as ts from "typescript";
44
import * as tstl from "./src";
5-
import { LuaLib } from "./src/LuaLib";
5+
import { loadLuaLibFeatures } from "./src/LuaLib";
66

77
const configFileName = path.resolve(__dirname, "src/lualib/tsconfig.json");
88
const { emitResult, diagnostics } = tstl.transpileProject(configFileName);
@@ -16,8 +16,4 @@ if (fs.existsSync(bundlePath)) {
1616
fs.unlinkSync(bundlePath);
1717
}
1818

19-
const emitHost = {
20-
readFile: (path: string) => fs.readFileSync(path, "utf-8"),
21-
writeFile: fs.writeFileSync,
22-
};
23-
fs.writeFileSync(bundlePath, LuaLib.loadFeatures(Object.values(tstl.LuaLibFeature), emitHost));
19+
fs.writeFileSync(bundlePath, loadLuaLibFeatures(Object.values(tstl.LuaLibFeature), ts.sys));

src/LuaLib.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,32 +72,32 @@ const luaLibDependencies: { [lib in LuaLibFeature]?: LuaLibFeature[] } = {
7272
SymbolRegistry: [LuaLibFeature.Symbol],
7373
};
7474

75-
export class LuaLib {
76-
public static loadFeatures(features: Iterable<LuaLibFeature>, emitHost: EmitHost): string {
77-
let result = "";
75+
export function loadLuaLibFeatures(features: Iterable<LuaLibFeature>, emitHost: EmitHost): string {
76+
let result = "";
7877

79-
const loadedFeatures = new Set<LuaLibFeature>();
78+
const loadedFeatures = new Set<LuaLibFeature>();
8079

81-
function load(feature: LuaLibFeature): void {
82-
if (!loadedFeatures.has(feature)) {
83-
loadedFeatures.add(feature);
84-
const dependencies = luaLibDependencies[feature];
85-
if (dependencies) {
86-
dependencies.forEach(load);
87-
}
88-
const featureFile = path.resolve(__dirname, `../dist/lualib/${feature}.lua`);
89-
const luaLibFeature = emitHost.readFile(featureFile);
90-
if (luaLibFeature !== undefined) {
91-
result += luaLibFeature.toString() + "\n";
92-
} else {
93-
throw new Error(`Could not read lualib feature ../dist/lualib/${feature}.lua`);
94-
}
95-
}
80+
function load(feature: LuaLibFeature): void {
81+
if (loadedFeatures.has(feature)) return;
82+
loadedFeatures.add(feature);
83+
84+
const dependencies = luaLibDependencies[feature];
85+
if (dependencies) {
86+
dependencies.forEach(load);
9687
}
9788

98-
for (const feature of features) {
99-
load(feature);
89+
const featureFile = path.resolve(__dirname, `../dist/lualib/${feature}.lua`);
90+
const luaLibFeature = emitHost.readFile(featureFile);
91+
if (luaLibFeature !== undefined) {
92+
result += luaLibFeature + "\n";
93+
} else {
94+
throw new Error(`Could not read lualib feature ../dist/lualib/${feature}.lua`);
10095
}
101-
return result;
10296
}
97+
98+
for (const feature of features) {
99+
load(feature);
100+
}
101+
102+
return result;
103103
}

src/LuaPrinter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { Mapping, SourceMapGenerator, SourceNode } from "source-map";
33
import { CompilerOptions, LuaLibImportKind } from "./CompilerOptions";
44
import * as tstl from "./LuaAST";
55
import { luaKeywords } from "./LuaKeywords";
6-
import { LuaLib, LuaLibFeature } from "./LuaLib";
7-
import * as tsHelper from "./TSHelper";
6+
import { loadLuaLibFeatures, LuaLibFeature } from "./LuaLib";
87
import { EmitHost } from "./Transpile";
8+
import * as tsHelper from "./TSHelper";
99

1010
type SourceChunk = string | SourceNode;
1111

@@ -132,7 +132,7 @@ export class LuaPrinter {
132132
// Inline lualib features
133133
else if (luaLibImport === LuaLibImportKind.Inline && luaLibFeatures.size > 0) {
134134
header += "-- Lua Library inline imports\n";
135-
header += LuaLib.loadFeatures(luaLibFeatures, this.emitHost);
135+
header += loadLuaLibFeatures(luaLibFeatures, this.emitHost);
136136
}
137137
}
138138

0 commit comments

Comments
 (0)