Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
e904013
Add new test builder and convert few tests
ark120202 May 28, 2019
28a8aa1
Rename fn and mod to testFunction and testModule
ark120202 May 28, 2019
e14d061
Transform some more tests
ark120202 May 28, 2019
89f2ae0
Transform some more tests
ark120202 May 30, 2019
701e285
Transform some more tests
ark120202 May 30, 2019
2a9af3f
Merge remote-tracking branch 'upstream/master' into new-testing-utils
ark120202 May 30, 2019
b3fe143
Update require tests
ark120202 May 30, 2019
e2888e1
Fix tests on node 8.5.0
ark120202 May 30, 2019
90cc57f
Change float precision in json test util
ark120202 May 30, 2019
e7e72ca
Transform some more tests
ark120202 May 30, 2019
0b3be7d
Revert "Change float precision in json test util"
ark120202 Jun 2, 2019
a17b329
Transform some more tests
ark120202 Jun 14, 2019
93d0251
Merge remote-tracking branch 'upstream/master' into new-testing-utils
ark120202 Jun 14, 2019
a4a84b5
Merge remote-tracking branch 'upstream/master' into new-testing-utils
ark120202 Jun 20, 2019
8ff931e
Move serialize to separate template functions
ark120202 Jun 20, 2019
c0ed6eb
Rename some builder methods
ark120202 Jun 20, 2019
849fd2b
numbers.spec.ts
ark120202 Jun 20, 2019
8f18432
Serialize NaN and Infinity test results
ark120202 Jun 20, 2019
6b71abb
math.spec.ts
ark120202 Jun 20, 2019
eb53e17
console.spec.ts
ark120202 Jun 20, 2019
ea45758
Merge array.spec.ts files
ark120202 Jun 20, 2019
d531fd6
Move some math tests to lualib
ark120202 Jun 20, 2019
9d8c37b
Move around some lualib tests
ark120202 Jun 20, 2019
aa2a01d
Move numbers.spec.ts to lualib tests
ark120202 Jun 21, 2019
953f58d
Move inlining tests to other luaLibImport tests
ark120202 Jun 21, 2019
0159d41
Fix node 8 tests
ark120202 Jun 21, 2019
469cb9f
Merge curry.spec.ts into functions.spec.ts
ark120202 Jun 21, 2019
641bdee
Fix object tests
ark120202 Jun 21, 2019
220a641
Merge remote-tracking branch 'upstream/master' into new-testing-utils
ark120202 Jul 10, 2019
d98048e
Refactor array tests
ark120202 Jul 12, 2019
b4ab66b
Move custom decorator tests to separate directory
ark120202 Jul 12, 2019
ec1db38
Merge tagged template literal and simple template literal tests
ark120202 Jul 12, 2019
470f16b
Update globalThis tests
ark120202 Jul 12, 2019
f6d5f3b
Merge remote-tracking branch 'upstream/master' into new-testing-utils
ark120202 Jul 12, 2019
d9ad58a
Move and remove some tests from expressions.spec.ts
ark120202 Jul 13, 2019
33e36b4
Move class-related tests to a directory
ark120202 Jul 13, 2019
66f2aa0
Move namespace tests from modules
ark120202 Jul 13, 2019
a4ec292
Move modules tests to a subdirectory
ark120202 Jul 13, 2019
0302c69
Transform tagged template literals tests
ark120202 Jul 13, 2019
aa5d5e5
Transform sourcemaps tests
ark120202 Jul 13, 2019
2b67650
Move string tests to lualib directory
ark120202 Jul 13, 2019
25385c7
Refactor spread.spec.ts
ark120202 Jul 14, 2019
54c3668
Refactor bindingpatterns.spec.ts
ark120202 Jul 14, 2019
93d8f9c
Merge and refactor destructuring tests
ark120202 Jul 14, 2019
de7583c
Merge remote-tracking branch 'upstream/master' into new-testing-utils
ark120202 Jul 14, 2019
af47a83
Fix expressions tests
ark120202 Jul 14, 2019
ef035fe
Fix declared namespace function call test
ark120202 Jul 14, 2019
404a1d7
Remove/move to other files tuples tests
ark120202 Jul 14, 2019
a605fb3
Move transformers.spec.ts to subdirectory
ark120202 Jul 14, 2019
9d82066
Move some tests to printer subdirectory
ark120202 Jul 14, 2019
b018ee9
Remove declarations.spec.ts
ark120202 Jul 14, 2019
358d7a2
Split typechecking into instanceof and typeof
ark120202 Jul 14, 2019
14b3fbe
Rename lualib tests to builtins
ark120202 Jul 14, 2019
6fd490a
Move parenthesis removal tests to printer subdirectory
ark120202 Jul 14, 2019
f6b40eb
Remove some translation tests
ark120202 Jul 14, 2019
bd9899c
Move around assignments and expressions
ark120202 Jul 14, 2019
fa614c7
Fix loading tests
ark120202 Jul 22, 2019
29ff10a
Merge remote-tracking branch 'upstream/master' into new-testing-utils
ark120202 Jul 22, 2019
302a4f4
enum.spec.ts
ark120202 Jul 24, 2019
698a26d
globalThis.spec.ts
ark120202 Jul 24, 2019
d820fd1
Rearrange some enum tests
ark120202 Jul 24, 2019
4f2a7f1
Refactor some tests
ark120202 Jul 24, 2019
9b587ed
Merge remote-tracking branch 'upstream/master' into new-testing-utils
ark120202 Jul 24, 2019
3117d6f
Fix enum tests
ark120202 Jul 25, 2019
2d15756
Merge remote-tracking branch 'upstream/master' into new-testing-utils
ark120202 Jul 29, 2019
30f7f08
Remove todos for #663
ark120202 Jul 29, 2019
0f6e75b
Move enum array index tests to array.spec.ts
ark120202 Jul 29, 2019
d6d517d
Remove some translation tests
ark120202 Jul 29, 2019
9198618
Fix nested namespace test
ark120202 Jul 29, 2019
32acca8
Fix as const cast creating diagnostics
ark120202 Jul 30, 2019
2c34c13
Address feedback
ark120202 Jul 30, 2019
7796c49
Merge remote-tracking branch 'upstream/master' into new-testing-utils
ark120202 Jul 31, 2019
5d6a5ee
Refactor new spread tests
ark120202 Jul 31, 2019
9cb9f67
Remove spreadAssignment translation test
ark120202 Jul 31, 2019
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
31 changes: 15 additions & 16 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4552,7 +4552,7 @@ export class LuaTransformer {
return tstl.createNumericLiteral(Math[name], identifier);

default:
throw TSTLErrors.UnsupportedProperty("math", name, identifier);
throw TSTLErrors.UnsupportedProperty("Math", name, identifier);
}
}

Expand Down Expand Up @@ -4622,7 +4622,7 @@ export class LuaTransformer {
}

default:
throw TSTLErrors.UnsupportedProperty("math", expressionName, expression);
throw TSTLErrors.UnsupportedProperty("Math", expressionName, expression);
}
}

Expand Down Expand Up @@ -4872,11 +4872,7 @@ export class LuaTransformer {
tstl.createStringLiteral("char")
);
default:
throw TSTLErrors.UnsupportedForTarget(
`string property ${identifierString}`,
this.luaTarget,
identifier
);
throw TSTLErrors.UnsupportedProperty("String", identifierString, identifier);
}
}

Expand All @@ -4898,7 +4894,7 @@ export class LuaTransformer {
case "values":
return this.transformLuaLibFunction(LuaLibFeature.ObjectValues, expression, ...parameters);
default:
throw TSTLErrors.UnsupportedForTarget(`object property ${methodName}`, this.luaTarget, expression);
throw TSTLErrors.UnsupportedProperty("Object", methodName, expression);
}
}

Expand Down Expand Up @@ -4969,7 +4965,7 @@ export class LuaTransformer {
);
return tstl.createCallExpression(tstl.createIdentifier("print"), [debugTracebackCall]);
default:
throw TSTLErrors.UnsupportedForTarget(`console property ${methodName}`, this.luaTarget, expression);
throw TSTLErrors.UnsupportedProperty("console", methodName, expression);
}
}

Expand All @@ -4992,7 +4988,7 @@ export class LuaTransformer {
const functionIdentifier = tstl.createIdentifier(`__TS__SymbolRegistry${upperMethodName}`);
return tstl.createCallExpression(functionIdentifier, parameters, expression);
default:
throw TSTLErrors.UnsupportedForTarget(`symbol property ${methodName}`, this.luaTarget, expression);
throw TSTLErrors.UnsupportedProperty("Symbol", methodName, expression);
}
}

Expand All @@ -5008,7 +5004,7 @@ export class LuaTransformer {
case "isFinite":
return this.transformLuaLibFunction(LuaLibFeature.NumberIsFinite, expression, ...parameters);
default:
throw TSTLErrors.UnsupportedForTarget(`number property ${methodName}`, this.luaTarget, expression);
throw TSTLErrors.UnsupportedProperty("Number", methodName, expression);
}
}

Expand Down Expand Up @@ -5186,11 +5182,14 @@ export class LuaTransformer {
}

public transformAssertionExpression(expression: ts.AssertionExpression): ExpressionVisitResult {
this.validateFunctionAssignment(
expression,
this.checker.getTypeAtLocation(expression.expression),
this.checker.getTypeAtLocation(expression.type)
);
if (!ts.isConstTypeReference(expression.type)) {
this.validateFunctionAssignment(
expression,
this.checker.getTypeAtLocation(expression.expression),
this.checker.getTypeAtLocation(expression.type)
);
}

return this.transformExpression(expression.expression);
}

Expand Down
13 changes: 9 additions & 4 deletions test/json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,15 @@ end


local function encode_number(val)
-- Check for NaN, -inf and inf
if val ~= val or val <= -math.huge or val >= math.huge then
error("unexpected number value '" .. tostring(val) .. "'")
if val ~= val then
return "NaN"
elseif val == math.huge then
return "Infinity"
elseif val == -math.huge then
return "-Infinity"
else
return string.format("%.17g", val)
end
return string.format("%.14g", val)
end


Expand All @@ -139,6 +143,7 @@ encode = function(val, stack)
end


-- TODO: Since it supports NaN and Infinity it is considered a superset of JSON, so it probably should be renamed
function JSONStringify(val)
return ( encode(val) )
end
162 changes: 162 additions & 0 deletions test/legacy-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import { lauxlib, lua, lualib, to_jsstring, to_luastring } from "fengari";
import * as fs from "fs";
import * as path from "path";
import * as ts from "typescript";
import * as tstl from "../src";
import * as tsHelper from "../src/TSHelper";

export function transpileString(
str: string | { [filename: string]: string },
options: tstl.CompilerOptions = {},
ignoreDiagnostics = true
): string {
const { diagnostics, file } = transpileStringResult(str, options);
expect(file.lua).toBeDefined();

const errors = diagnostics.filter(d => !ignoreDiagnostics || d.source === "typescript-to-lua");
expect(errors).not.toHaveDiagnostics();

return file.lua!.trim();
}

export function transpileStringsAsProject(
input: Record<string, string>,
options: tstl.CompilerOptions = {}
): tstl.TranspileResult {
const optionsWithDefaults = {
luaTarget: tstl.LuaTarget.Lua53,
noHeader: true,
skipLibCheck: true,
target: ts.ScriptTarget.ESNext,
lib: ["lib.esnext.d.ts"],
experimentalDecorators: true,
...options,
};

return tstl.transpileVirtualProject(input, optionsWithDefaults);
}

export function transpileStringResult(
input: string | Record<string, string>,
options: tstl.CompilerOptions = {}
): Required<tstl.TranspileStringResult> {
const { diagnostics, transpiledFiles } = transpileStringsAsProject(
typeof input === "string" ? { "main.ts": input } : input,
options
);

const file = transpiledFiles.find(({ fileName }) => /\bmain\.[a-z]+$/.test(fileName));
if (file === undefined) {
throw new Error('Program should have a file named "main"');
}

return { diagnostics, file };
}

const lualibContent = fs.readFileSync(path.resolve(__dirname, "../dist/lualib/lualib_bundle.lua"), "utf8");
const minimalTestLib = fs.readFileSync(path.join(__dirname, "json.lua"), "utf8") + "\n";
export function executeLua(luaStr: string, withLib = true): any {
luaStr = luaStr.replace(/require\("lualib_bundle"\)/g, lualibContent);
if (withLib) {
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 undefined;
} 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)));
}
}

export function transpileAndExecute(
tsStr: string,
compilerOptions?: tstl.CompilerOptions,
luaHeader?: string,
tsHeader?: string
): any {
const wrappedTsString = `${tsHeader ? tsHeader : ""}
declare function JSONStringify(this: void, p: any): string;
function __runTest(this: void): any {${tsStr}}`;

const lua = `${luaHeader ? luaHeader : ""}
${transpileString(wrappedTsString, compilerOptions, false)}
return __runTest();`;

return executeLua(lua);
}

export function transpileAndExecuteProjectReturningMainExport(
typeScriptFiles: Record<string, string>,
exportName: string,
options: tstl.CompilerOptions = {}
): [any, string] {
const mainFile = Object.keys(typeScriptFiles).find(typeScriptFileName => typeScriptFileName === "main.ts");
if (!mainFile) {
throw new Error("An entry point file needs to be specified. This should be called main.ts");
}

const joinedTranspiledFiles = Object.keys(typeScriptFiles)
.filter(typeScriptFileName => typeScriptFileName !== "main.ts")
.map(typeScriptFileName => {
const modulePath = tsHelper.getExportPath(typeScriptFileName, options);
const tsCode = typeScriptFiles[typeScriptFileName];
const luaCode = transpileString(tsCode, options);
return `package.preload["${modulePath}"] = function()
${luaCode}
end`;
})
.join("\n");

const luaCode = `return (function()
${joinedTranspiledFiles}
${transpileString(typeScriptFiles[mainFile])}
end)().${exportName}`;

try {
return [executeLua(luaCode), luaCode];
} catch (err) {
throw new Error(`
Encountered an error when executing the following Lua code:

${luaCode}

${err}
`);
}
}

export function transpileExecuteAndReturnExport(
tsStr: string,
returnExport: string,
compilerOptions?: tstl.CompilerOptions,
luaHeader?: string
): any {
const wrappedTsString = `declare function JSONStringify(this: void, p: any): string;
${tsStr}`;

const lua = `return (function()
${luaHeader ? luaHeader : ""}
${transpileString(wrappedTsString, compilerOptions, false)}
end)().${returnExport}`;

return executeLua(lua);
}
Loading