forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.ts
More file actions
147 lines (123 loc) · 4.66 KB
/
Copy pathutil.ts
File metadata and controls
147 lines (123 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import * as path from "path";
import * as ts from "typescript";
import { Expect } from "alsatian";
import { transpileString as compilerTranspileString, createStringCompilerProgram } from "../../src/Compiler";
import { CompilerOptions, LuaTarget, LuaLibImportKind } from "../../src/CompilerOptions";
import {lauxlib, lua, lualib, to_jsstring, to_luastring } from "fengari";
import * as fs from "fs";
import { LuaTransformer } from "../../src/LuaTransformer";
export function transpileString(
str: string,
options?: CompilerOptions,
ignoreDiagnostics = true,
filePath = "file.ts"): string {
if (options) {
if (options.noHeader === undefined) {
options.noHeader = true;
}
return compilerTranspileString(str, options, ignoreDiagnostics, filePath);
} else {
return compilerTranspileString(
str,
{
luaLibImport: LuaLibImportKind.Require,
luaTarget: LuaTarget.Lua53,
target: ts.ScriptTarget.ES2015,
noHeader: true,
},
ignoreDiagnostics,
filePath
);
}
}
export function executeLua(luaStr: string, withLib = true): any {
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 expectCodeEqual(code1: string, code2: string): void {
// Trim leading/trailing whitespace
let c1 = code1.trim();
let c2 = code2.trim();
// Unify indentation
c1 = c1.replace(/\s+/g, " ");
c2 = c2.replace(/\s+/g, " ");
Expect(c1).toBe(c2);
}
// Get a mock transformer to use for testing
export function makeTestTransformer(target: LuaTarget = LuaTarget.Lua53): LuaTransformer {
const options = {luaTarget: target};
return new LuaTransformer(ts.createProgram([], options), options);
}
export function transpileAndExecute(
tsStr: string,
compilerOptions?: CompilerOptions,
luaHeader?: string,
tsHeader?: string,
ignoreDiagnosticsOverride = process.argv[2] === "--ignoreDiagnostics"
): any
{
const wrappedTsString = `declare function JSONStringify(p: any): string;
${tsHeader ? tsHeader : ""}
function __runTest(): any {${tsStr}}`;
const lua = `${luaHeader ? luaHeader : ""}
${transpileString(wrappedTsString, compilerOptions, ignoreDiagnosticsOverride)}
return __runTest();`;
return executeLua(lua);
}
export function transpileExecuteAndReturnExport(
tsStr: string,
returnExport: string,
compilerOptions?: CompilerOptions,
luaHeader?: string
): any
{
const wrappedTsString = `declare function JSONStringify(p: any): string;
${tsStr}`;
const lua = `return (function()
${luaHeader ? luaHeader : ""}
${transpileString(wrappedTsString, compilerOptions, false)}
end)().${returnExport}`;
return executeLua(lua);
}
export function parseTypeScript(typescript: string, target: LuaTarget = LuaTarget.Lua53)
: [ts.SourceFile, ts.TypeChecker] {
const program = createStringCompilerProgram(typescript, { luaTarget: target });
return [program.getSourceFile("file.ts"), program.getTypeChecker()];
}
export function findFirstChild(node: ts.Node, predicate: (node: ts.Node) => boolean): ts.Node | undefined {
for (const child of node.getChildren()) {
if (predicate(child)) {
return child;
}
const childChild = findFirstChild(child, predicate);
if (childChild !== undefined) {
return childChild;
}
}
return undefined;
}
const jsonlib = fs.readFileSync("test/src/json.lua") + "\n";
export const minimalTestLib = jsonlib;