Skip to content

Commit fde8214

Browse files
authored
Merge pull request #106 from Perryvw/fengari-integration
Replaced lua.vm.js with fengari and updated tests.
2 parents 1522c6d + 885d92c commit fde8214

File tree

13 files changed

+179
-104
lines changed

13 files changed

+179
-104
lines changed

dist/lualib/typescript.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ function TS_indexOf(list, object )
8585
return -1
8686
end
8787

88+
function TS_replace(source, searchVal, newVal)
89+
local result = string.gsub(source, searchVal, newVal)
90+
return result
91+
end
92+
8893
function TS_split(str, separator)
8994
local out = {}
9095

@@ -116,7 +121,7 @@ function TS_split(str, separator)
116121
end
117122

118123
function TS_push(list, ...)
119-
for _, v in pairs({...}) do
124+
for _, v in ipairs({...}) do
120125
list[#list + 1] = v
121126
end
122127
end

package-lock.json

Lines changed: 40 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"alsatian": "^2.2.1",
3131
"codecov": "^3.0.1",
3232
"deep-equal": "^1.0.1",
33-
"lua.vm.js": "0.0.1",
33+
"fengari": "^0.1.2",
3434
"nyc": "^11.7.1",
3535
"tslint": "^5.9.1"
3636
}

src/Transpiler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ export class LuaTranspiler {
953953
const caller = this.transpileExpression(expression.expression);
954954
switch (expression.name.escapedText) {
955955
case "replace":
956-
return `string.gsub(${caller},${params})`;
956+
return `TS_replace(${caller},${params})`;
957957
case "indexOf":
958958
if (node.arguments.length === 1) {
959959
return `(string.find(${caller},${params},1,true) or 0)-1`;
@@ -1203,7 +1203,7 @@ export class LuaTranspiler {
12031203
) {
12041204
return `local ${vars}=${value}\n`;
12051205
} else {
1206-
return `local ${vars}=unpack(${value})\n`;
1206+
return `local ${vars}=table.unpack(${value})\n`;
12071207
}
12081208
} else {
12091209
throw new TranspileError(

test/src/util.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import { Expect } from "alsatian";
66
import { LuaTranspiler, TranspileError, LuaTarget } from "../../src/Transpiler";
77
import { CompilerOptions } from "../../src/CommandLineParser";
88

9-
const LuaVM = require("lua.vm.js");
9+
import {lauxlib, lua, lualib, to_luastring, to_jsstring } from "fengari";
10+
1011
const fs = require("fs");
1112

1213
const libSource = fs.readFileSync(path.join(path.dirname(require.resolve('typescript')), 'lib.d.ts')).toString();
1314

14-
export function transpileString(str: string, options: CompilerOptions = { dontRequireLuaLib: true, luaTarget: LuaTarget.LuaJIT }): string {
15+
export function transpileString(str: string, options: CompilerOptions = { dontRequireLuaLib: true, luaTarget: LuaTarget.Lua53 }): string {
1516
let compilerHost = {
1617
getSourceFile: (filename, languageVersion) => {
1718
if (filename === "file.ts") {
@@ -50,16 +51,39 @@ export function transpileFile(path: string): string {
5051
diagnostics.forEach(diagnostic => console.log(`${ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}`));
5152

5253
const options: ts.CompilerOptions = { dontRequireLuaLib: true };
53-
const lua = LuaTranspiler.transpileSourceFile(program.getSourceFile(path), checker, options);
54-
return lua.trim();
54+
const result = LuaTranspiler.transpileSourceFile(program.getSourceFile(path), checker, options);
55+
return result.trim();
5556
}
5657

57-
export function executeLua(lua: string, withLib = true): any {
58+
export function executeLua(luaStr: string, withLib = true): any {
5859
if (withLib) {
59-
lua = minimalTestLib + lua
60+
luaStr = minimalTestLib + luaStr;
61+
}
62+
63+
const L = lauxlib.luaL_newstate();
64+
lualib.luaL_openlibs(L);
65+
const status = lauxlib.luaL_dostring(L, to_luastring(luaStr));
66+
67+
if (status === lua.LUA_OK) {
68+
// Read the return value from stack depending on its type.
69+
if (lua.lua_isboolean(L, -1)) {
70+
return lua.lua_toboolean(L, -1);
71+
} else if (lua.lua_isnil(L, -1)) {
72+
return null;
73+
} else if (lua.lua_isnumber(L, -1)) {
74+
return lua.lua_tonumber(L, -1);
75+
} else if (lua.lua_isstring(L, -1)) {
76+
return lua.lua_tojsstring(L, -1);
77+
} else {
78+
throw new Error("Unsupported lua return type: " + to_jsstring(lua.lua_typename(L, lua.lua_type(L, -1))));
79+
}
80+
} else {
81+
// If the lua VM did not terminate with status code LUA_OK an error occurred.
82+
// Throw a JS error with the message, retrieved by reading a string from the stack.
83+
84+
// Filter control characters out of string which are in there because ????
85+
throw new Error("LUA ERROR: " + to_jsstring(lua.lua_tostring(L, -1).filter(c => c >= 20)));
6086
}
61-
const luavm = new LuaVM.Lua.State();
62-
return luavm.execute(lua)[0];
6387
}
6488

6589
export function expectCodeEqual(code1: string, code2: string) {
@@ -74,8 +98,8 @@ export function expectCodeEqual(code1: string, code2: string) {
7498
Expect(c1).toBe(c2);
7599
}
76100

77-
const lualib = fs.readFileSync("dist/lualib/typescript.lua") + "\n";
101+
const tslualib = fs.readFileSync("dist/lualib/typescript.lua") + "\n";
78102

79103
const jsonlib = fs.readFileSync("test/src/json.lua") + "\n";
80104

81-
export const minimalTestLib = lualib + jsonlib;
105+
export const minimalTestLib = tslualib + jsonlib;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
local a,b=unpack(myFunc())
1+
local a,b=table.unpack(myFunc())

test/unit/assignments.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class AssignmentTests {
5151
public nullAssignment(declaration: string) {
5252
const lua = util.transpileString(declaration + " return myvar;");
5353
const result = util.executeLua(lua);
54-
Expect(result).toBe(undefined);
54+
Expect(result).toBe(null);
5555
}
5656

5757
@TestCase(["a", "b"], ["e", "f"])

test/unit/conditionals.spec.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Expect, Test, TestCase, FocusTest } from "alsatian";
2-
import * as util from "../src/util"
1+
import { Expect, Test, TestCase } from "alsatian";
2+
import * as util from "../src/util";
33

44
export class LuaConditionalsTests {
55

@@ -8,7 +8,7 @@ export class LuaConditionalsTests {
88
@Test("if")
99
public if(inp: number, expected: number) {
1010
// Transpile
11-
let lua = util.transpileString(
11+
const lua = util.transpileString(
1212
`let input = ${inp}
1313
if (input === 0) {
1414
return 0;
@@ -17,7 +17,7 @@ export class LuaConditionalsTests {
1717
);
1818

1919
// Execute
20-
let result = util.executeLua(lua);
20+
const result = util.executeLua(lua);
2121

2222
// Assert
2323
Expect(result).toBe(expected);
@@ -28,7 +28,7 @@ export class LuaConditionalsTests {
2828
@Test("ifelse")
2929
public ifelse(inp: number, expected: number) {
3030
// Transpile
31-
let lua = util.transpileString(
31+
const lua = util.transpileString(
3232
`let input = ${inp}
3333
if (input === 0) {
3434
return 0;
@@ -38,7 +38,7 @@ export class LuaConditionalsTests {
3838
);
3939

4040
// Execute
41-
let result = util.executeLua(lua);
41+
const result = util.executeLua(lua);
4242

4343
// Assert
4444
Expect(result).toBe(expected);
@@ -51,7 +51,7 @@ export class LuaConditionalsTests {
5151
@Test("ifelseif")
5252
public ifelseif(inp: number, expected: number) {
5353
// Transpile
54-
let lua = util.transpileString(
54+
const lua = util.transpileString(
5555
`let input = ${inp}
5656
if (input === 0) {
5757
return 0;
@@ -64,7 +64,7 @@ export class LuaConditionalsTests {
6464
);
6565

6666
// Execute
67-
let result = util.executeLua(lua);
67+
const result = util.executeLua(lua);
6868

6969
// Assert
7070
Expect(result).toBe(expected);
@@ -77,7 +77,7 @@ export class LuaConditionalsTests {
7777
@Test("ifelseifelse")
7878
public ifelseifelse(inp: number, expected: number) {
7979
// Transpile
80-
let lua = util.transpileString(
80+
const lua = util.transpileString(
8181
`let input = ${inp}
8282
if (input === 0) {
8383
return 0;
@@ -91,7 +91,7 @@ export class LuaConditionalsTests {
9191
);
9292

9393
// Execute
94-
let result = util.executeLua(lua);
94+
const result = util.executeLua(lua);
9595

9696
// Assert
9797
Expect(result).toBe(expected);
@@ -104,7 +104,7 @@ export class LuaConditionalsTests {
104104
@Test("switch")
105105
public switch(inp: number, expected: number) {
106106
// Transpile
107-
let lua = util.transpileString(
107+
const lua = util.transpileString(
108108
`let result = -1;
109109
110110
switch (${inp}) {
@@ -122,7 +122,7 @@ export class LuaConditionalsTests {
122122
);
123123

124124
// Execute
125-
let result = util.executeLua(lua);
125+
const result = util.executeLua(lua);
126126

127127
// Assert
128128
Expect(result).toBe(expected);
@@ -135,7 +135,7 @@ export class LuaConditionalsTests {
135135
@Test("switchdefault")
136136
public switchdefault(inp: number, expected: number) {
137137
// Transpile
138-
let lua = util.transpileString(
138+
const lua = util.transpileString(
139139
`let result = -1;
140140
141141
switch (${inp}) {
@@ -156,7 +156,7 @@ export class LuaConditionalsTests {
156156
);
157157

158158
// Execute
159-
let result = util.executeLua(lua);
159+
const result = util.executeLua(lua);
160160

161161
// Assert
162162
Expect(result).toBe(expected);
@@ -172,7 +172,7 @@ export class LuaConditionalsTests {
172172
@Test("switchfallthrough")
173173
public switchfallthrough(inp: number, expected: number) {
174174
/// Transpile
175-
let lua = util.transpileString(
175+
const lua = util.transpileString(
176176
`let result = -1;
177177
178178
switch (${inp}) {
@@ -202,7 +202,7 @@ export class LuaConditionalsTests {
202202
);
203203

204204
// Execute
205-
let result = util.executeLua(lua);
205+
const result = util.executeLua(lua);
206206

207207
// Assert
208208
Expect(result).toBe(expected);
@@ -215,7 +215,7 @@ export class LuaConditionalsTests {
215215
@Test("nestedSwitch")
216216
public nestedSwitch(inp: number, expected: number) {
217217
// Transpile
218-
let lua = util.transpileString(
218+
const lua = util.transpileString(
219219
`let result = -1;
220220
221221
switch (${inp}) {
@@ -246,7 +246,7 @@ export class LuaConditionalsTests {
246246
);
247247

248248
// Execute
249-
let result = util.executeLua(lua);
249+
const result = util.executeLua(lua);
250250

251251
// Assert
252252
Expect(result).toBe(expected);

test/unit/curry.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Expect, Test, TestCase } from "alsatian";
2-
import * as util from "../src/util"
2+
import * as util from "../src/util";
33

44
export class LuaCurryTests {
55

66
@Test("currying")
77
public currying() {
88
// Transpile
9-
let lua = util.transpileString(
9+
const lua = util.transpileString(
1010
`(x: number) => (y: number) => x + y;`
1111
);
1212
// Assert
@@ -18,13 +18,13 @@ export class LuaCurryTests {
1818
@TestCase(5, 4)
1919
public curryingAdd(x: number, y: number) {
2020
// Transpile
21-
let lua = util.transpileString(
21+
const lua = util.transpileString(
2222
`let add = (x: number) => (y: number) => x + y;
2323
return add(${x})(${y})`
2424
);
2525

2626
// Execute
27-
let result = util.executeLua(lua);
27+
const result = util.executeLua(lua);
2828

2929
// Assert
3030
Expect(result).toBe(x + y);

0 commit comments

Comments
 (0)