Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion dist/lualib/typescript.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ function TS_indexOf(list, object )
return -1
end

function TS_replace(source, searchVal, newVal)
local result = string.gsub(source, searchVal, newVal)
return result
end

function TS_split(str, separator)
local out = {}

Expand Down Expand Up @@ -116,7 +121,7 @@ function TS_split(str, separator)
end

function TS_push(list, ...)
for _, v in pairs({...}) do
for _, v in ipairs({...}) do
list[#list + 1] = v
end
end
Expand Down
46 changes: 40 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"alsatian": "^2.2.1",
"codecov": "^3.0.1",
"deep-equal": "^1.0.1",
"lua.vm.js": "0.0.1",
"fengari": "^0.1.2",
"nyc": "^11.7.1",
"tslint": "^5.9.1"
}
Expand Down
4 changes: 2 additions & 2 deletions src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ export class LuaTranspiler {
const caller = this.transpileExpression(expression.expression);
switch (expression.name.escapedText) {
case "replace":
return `string.gsub(${caller},${params})`;
return `TS_replace(${caller},${params})`;
case "indexOf":
if (node.arguments.length === 1) {
return `(string.find(${caller},${params},1,true) or 0)-1`;
Expand Down Expand Up @@ -1203,7 +1203,7 @@ export class LuaTranspiler {
) {
return `local ${vars}=${value}\n`;
} else {
return `local ${vars}=unpack(${value})\n`;
return `local ${vars}=table.unpack(${value})\n`;
}
} else {
throw new TranspileError(
Expand Down
44 changes: 34 additions & 10 deletions test/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import { Expect } from "alsatian";
import { LuaTranspiler, TranspileError, LuaTarget } from "../../src/Transpiler";
import { CompilerOptions } from "../../src/CommandLineParser";

const LuaVM = require("lua.vm.js");
import {lauxlib, lua, lualib, to_luastring, to_jsstring } from "fengari";

const fs = require("fs");

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

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

const options: ts.CompilerOptions = { dontRequireLuaLib: true };
const lua = LuaTranspiler.transpileSourceFile(program.getSourceFile(path), checker, options);
return lua.trim();
const result = LuaTranspiler.transpileSourceFile(program.getSourceFile(path), checker, options);
return result.trim();
}

export function executeLua(lua: string, withLib = true): any {
export function executeLua(luaStr: string, withLib = true): any {
if (withLib) {
lua = minimalTestLib + lua
luaStr = minimalTestLib + luaStr;
}

const L = lauxlib.luaL_newstate();
lualib.luaL_openlibs(L);
const status = lauxlib.luaL_dostring(L, to_luastring(luaStr));

if (status === lua.LUA_OK) {
// Read the return value from stack depending on its type.
if (lua.lua_isboolean(L, -1)) {
return lua.lua_toboolean(L, -1);
} else if (lua.lua_isnil(L, -1)) {
return null;
} else if (lua.lua_isnumber(L, -1)) {
return lua.lua_tonumber(L, -1);
} else if (lua.lua_isstring(L, -1)) {
return lua.lua_tojsstring(L, -1);
} else {
throw new Error("Unsupported lua return type: " + to_jsstring(lua.lua_typename(L, lua.lua_type(L, -1))));
}
} else {
// If the lua VM did not terminate with status code LUA_OK an error occurred.
// Throw a JS error with the message, retrieved by reading a string from the stack.

// Filter control characters out of string which are in there because ????
throw new Error("LUA ERROR: " + to_jsstring(lua.lua_tostring(L, -1).filter(c => c >= 20)));
}
const luavm = new LuaVM.Lua.State();
return luavm.execute(lua)[0];
}

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

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

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

export const minimalTestLib = lualib + jsonlib;
export const minimalTestLib = tslualib + jsonlib;
2 changes: 1 addition & 1 deletion test/translation/lua/assignmentDestructing.lua
Original file line number Diff line number Diff line change
@@ -1 +1 @@
local a,b=unpack(myFunc())
local a,b=table.unpack(myFunc())
2 changes: 1 addition & 1 deletion test/unit/assignments.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class AssignmentTests {
public nullAssignment(declaration: string) {
const lua = util.transpileString(declaration + " return myvar;");
const result = util.executeLua(lua);
Expect(result).toBe(undefined);
Expect(result).toBe(null);
}

@TestCase(["a", "b"], ["e", "f"])
Expand Down
36 changes: 18 additions & 18 deletions test/unit/conditionals.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Expect, Test, TestCase, FocusTest } from "alsatian";
import * as util from "../src/util"
import { Expect, Test, TestCase } from "alsatian";
import * as util from "../src/util";

export class LuaConditionalsTests {

Expand All @@ -8,7 +8,7 @@ export class LuaConditionalsTests {
@Test("if")
public if(inp: number, expected: number) {
// Transpile
let lua = util.transpileString(
const lua = util.transpileString(
`let input = ${inp}
if (input === 0) {
return 0;
Expand All @@ -17,7 +17,7 @@ export class LuaConditionalsTests {
);

// Execute
let result = util.executeLua(lua);
const result = util.executeLua(lua);

// Assert
Expect(result).toBe(expected);
Expand All @@ -28,7 +28,7 @@ export class LuaConditionalsTests {
@Test("ifelse")
public ifelse(inp: number, expected: number) {
// Transpile
let lua = util.transpileString(
const lua = util.transpileString(
`let input = ${inp}
if (input === 0) {
return 0;
Expand All @@ -38,7 +38,7 @@ export class LuaConditionalsTests {
);

// Execute
let result = util.executeLua(lua);
const result = util.executeLua(lua);

// Assert
Expect(result).toBe(expected);
Expand All @@ -51,7 +51,7 @@ export class LuaConditionalsTests {
@Test("ifelseif")
public ifelseif(inp: number, expected: number) {
// Transpile
let lua = util.transpileString(
const lua = util.transpileString(
`let input = ${inp}
if (input === 0) {
return 0;
Expand All @@ -64,7 +64,7 @@ export class LuaConditionalsTests {
);

// Execute
let result = util.executeLua(lua);
const result = util.executeLua(lua);

// Assert
Expect(result).toBe(expected);
Expand All @@ -77,7 +77,7 @@ export class LuaConditionalsTests {
@Test("ifelseifelse")
public ifelseifelse(inp: number, expected: number) {
// Transpile
let lua = util.transpileString(
const lua = util.transpileString(
`let input = ${inp}
if (input === 0) {
return 0;
Expand All @@ -91,7 +91,7 @@ export class LuaConditionalsTests {
);

// Execute
let result = util.executeLua(lua);
const result = util.executeLua(lua);

// Assert
Expect(result).toBe(expected);
Expand All @@ -104,7 +104,7 @@ export class LuaConditionalsTests {
@Test("switch")
public switch(inp: number, expected: number) {
// Transpile
let lua = util.transpileString(
const lua = util.transpileString(
`let result = -1;

switch (${inp}) {
Expand All @@ -122,7 +122,7 @@ export class LuaConditionalsTests {
);

// Execute
let result = util.executeLua(lua);
const result = util.executeLua(lua);

// Assert
Expect(result).toBe(expected);
Expand All @@ -135,7 +135,7 @@ export class LuaConditionalsTests {
@Test("switchdefault")
public switchdefault(inp: number, expected: number) {
// Transpile
let lua = util.transpileString(
const lua = util.transpileString(
`let result = -1;

switch (${inp}) {
Expand All @@ -156,7 +156,7 @@ export class LuaConditionalsTests {
);

// Execute
let result = util.executeLua(lua);
const result = util.executeLua(lua);

// Assert
Expect(result).toBe(expected);
Expand All @@ -172,7 +172,7 @@ export class LuaConditionalsTests {
@Test("switchfallthrough")
public switchfallthrough(inp: number, expected: number) {
/// Transpile
let lua = util.transpileString(
const lua = util.transpileString(
`let result = -1;

switch (${inp}) {
Expand Down Expand Up @@ -202,7 +202,7 @@ export class LuaConditionalsTests {
);

// Execute
let result = util.executeLua(lua);
const result = util.executeLua(lua);

// Assert
Expect(result).toBe(expected);
Expand All @@ -215,7 +215,7 @@ export class LuaConditionalsTests {
@Test("nestedSwitch")
public nestedSwitch(inp: number, expected: number) {
// Transpile
let lua = util.transpileString(
const lua = util.transpileString(
`let result = -1;

switch (${inp}) {
Expand Down Expand Up @@ -246,7 +246,7 @@ export class LuaConditionalsTests {
);

// Execute
let result = util.executeLua(lua);
const result = util.executeLua(lua);

// Assert
Expect(result).toBe(expected);
Expand Down
8 changes: 4 additions & 4 deletions test/unit/curry.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Expect, Test, TestCase } from "alsatian";
import * as util from "../src/util"
import * as util from "../src/util";

export class LuaCurryTests {

@Test("currying")
public currying() {
// Transpile
let lua = util.transpileString(
const lua = util.transpileString(
`(x: number) => (y: number) => x + y;`
);
// Assert
Expand All @@ -18,13 +18,13 @@ export class LuaCurryTests {
@TestCase(5, 4)
public curryingAdd(x: number, y: number) {
// Transpile
let lua = util.transpileString(
const lua = util.transpileString(
`let add = (x: number) => (y: number) => x + y;
return add(${x})(${y})`
);

// Execute
let result = util.executeLua(lua);
const result = util.executeLua(lua);

// Assert
Expect(result).toBe(x + y);
Expand Down
Loading