X Tutup
Skip to content

Commit c97bf17

Browse files
tomblindPerryvw
authored andcommitted
New functions (#268)
* initial re-work of function transpiling, including NoContext decorator * added lib function for bind() * decorated lib functions with NoContext * added apply and call lib functions * checking type aliases for custom decorators to fix tslint issues with new lib functions * injecting JSONStringify decl directly into transpileString for tests
1 parent 39c0c51 commit c97bf17

File tree

5 files changed

+12
-38
lines changed

5 files changed

+12
-38
lines changed

test/src/util.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ import * as ts from "typescript";
33

44
import { Expect } from "alsatian";
55

6-
import { transpileString } from "../../src/Compiler";
6+
import { transpileString as _transpileString } from "../../src/Compiler";
7+
import { CompilerOptions } from "../../src/CompilerOptions";
78
import { LuaTarget, LuaTranspiler } from "../../src/Transpiler";
89
import { createTranspiler } from "../../src/TranspilerFactory";
910

1011
import {lauxlib, lua, lualib, to_jsstring, to_luastring } from "fengari";
1112

1213
import * as fs from "fs";
1314

14-
export { transpileString };
15+
export function transpileString(str: string, options?: CompilerOptions): string {
16+
return _transpileString("/** !NoContext */ declare function JSONStringify(t: any): string;\n" + str, options);
17+
}
1518

1619
export function executeLua(luaStr: string, withLib = true): any {
1720
if (withLib) {

test/unit/loops.spec.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export class LuaLoopTests {
1818
arrTest[i] = arrTest[i] + 1;
1919
i++;
2020
}
21-
/** !NoContext */ declare function JSONStringify(t: any): string;
2221
return JSONStringify(arrTest);`
2322
);
2423

@@ -53,7 +52,6 @@ export class LuaLoopTests {
5352
5453
i++;
5554
}
56-
/** !NoContext */ declare function JSONStringify(t: any): string;
5755
return JSONStringify(arrTest);`
5856
);
5957

@@ -88,7 +86,6 @@ export class LuaLoopTests {
8886
8987
i++;
9088
} while (i < arrTest.length)
91-
/** !NoContext */ declare function JSONStringify(t: any): string;
9289
return JSONStringify(arrTest);`
9390
);
9491

@@ -108,7 +105,6 @@ export class LuaLoopTests {
108105
for (let i = 0; i < arrTest.length; ++i) {
109106
arrTest[i] = arrTest[i] + 1;
110107
}
111-
/** !NoContext */ declare function JSONStringify(t: any): string;
112108
return JSONStringify(arrTest);`
113109
);
114110

@@ -137,7 +133,6 @@ export class LuaLoopTests {
137133
arrTest[i] = j;
138134
}
139135
}
140-
/** !NoContext */ declare function JSONStringify(t: any): string;
141136
return JSONStringify(arrTest);
142137
`
143138
);
@@ -158,7 +153,6 @@ export class LuaLoopTests {
158153
for (let i = 0; arrTest.length > i; i++) {
159154
arrTest[i] = arrTest[i] + 1;
160155
}
161-
/** !NoContext */ declare function JSONStringify(t: any): string;
162156
return JSONStringify(arrTest);`
163157
);
164158

@@ -179,7 +173,6 @@ export class LuaLoopTests {
179173
break;
180174
arrTest[i] = arrTest[i] + 1;
181175
}
182-
/** !NoContext */ declare function JSONStringify(t: any): string;
183176
return JSONStringify(arrTest);`
184177
);
185178

@@ -206,7 +199,6 @@ export class LuaLoopTests {
206199
for (${header}) {
207200
arrTest[i] = arrTest[i] + 1;
208201
}
209-
/** !NoContext */ declare function JSONStringify(t: any): string;
210202
return JSONStringify(arrTest);`
211203
);
212204

@@ -226,7 +218,6 @@ export class LuaLoopTests {
226218
for (let key in objTest) {
227219
objTest[key] = objTest[key] + 1;
228220
}
229-
/** !NoContext */ declare function JSONStringify(t: any): string;
230221
return JSONStringify(objTest);`
231222
);
232223

@@ -264,7 +255,6 @@ export class LuaLoopTests {
264255
265256
obj[i] = 0;
266257
}
267-
/** !NoContext */ declare function JSONStringify(t: any): string;
268258
return JSONStringify(obj);
269259
`
270260
);
@@ -286,7 +276,6 @@ export class LuaLoopTests {
286276
for (let value of objTest) {
287277
arrResultTest.push(value + 1)
288278
}
289-
/** !NoContext */ declare function JSONStringify(t: any): string;
290279
return JSONStringify(arrResultTest);`
291280
);
292281

@@ -318,7 +307,6 @@ export class LuaLoopTests {
318307
}
319308
a++;
320309
}
321-
/** !NoContext */ declare function JSONStringify(t: any): string;
322310
return JSONStringify(testArr);`
323311
);
324312

test/unit/lualib/lualib.spec.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export class LuaLibArrayTests {
1212
arrTest.forEach((elem, index) => {
1313
arrTest[index] = arrTest[index] + 1;
1414
})
15-
/** !NoContext */ declare function JSONStringify(t: any): string;
1615
return JSONStringify(arrTest);`
1716
);
1817

@@ -32,8 +31,7 @@ export class LuaLibArrayTests {
3231
@Test("array.map")
3332
public map<T>(inp: T[], func: string) {
3433
// Transpile
35-
const lua = util.transpileString(`/** !NoContext */ declare function JSONStringify(t: any): string;
36-
return JSONStringify([${inp.toString()}].map(${func}))`);
34+
const lua = util.transpileString(`return JSONStringify([${inp.toString()}].map(${func}))`);
3735

3836
// Execute
3937
const result = util.executeLua(lua);
@@ -52,8 +50,7 @@ export class LuaLibArrayTests {
5250
@Test("array.filter")
5351
public filter<T>(inp: T[], func: string) {
5452
// Transpile
55-
const lua = util.transpileString(`/** !NoContext */ declare function JSONStringify(t: any): string;
56-
return JSONStringify([${inp.toString()}].filter(${func}))`);
53+
const lua = util.transpileString(`return JSONStringify([${inp.toString()}].filter(${func}))`);
5754

5855
// Execute
5956
const result = util.executeLua(lua);
@@ -69,8 +66,7 @@ export class LuaLibArrayTests {
6966
@Test("array.every")
7067
public every<T>(inp: T[], func: string) {
7168
// Transpile
72-
const lua = util.transpileString(`/** !NoContext */ declare function JSONStringify(t: any): string;
73-
return JSONStringify([${inp.toString()}].every(${func})))`);
69+
const lua = util.transpileString(`return JSONStringify([${inp.toString()}].every(${func})))`);
7470

7571
// Execute
7672
const result = util.executeLua(lua);
@@ -86,8 +82,7 @@ export class LuaLibArrayTests {
8682
@Test("array.some")
8783
public some<T>(inp: T[], func: string) {
8884
// Transpile
89-
const lua = util.transpileString(`/** !NoContext */ declare function JSONStringify(t: any): string;
90-
return JSONStringify([${inp.toString()}].some(${func})))`);
85+
const lua = util.transpileString(`return JSONStringify([${inp.toString()}].some(${func})))`);
9186

9287
// Execute
9388
const result = util.executeLua(lua);
@@ -106,8 +101,7 @@ export class LuaLibArrayTests {
106101
@Test("array.slice")
107102
public slice<T>(inp: T[], start: number, end?: number) {
108103
// Transpile
109-
const lua = util.transpileString(`/** !NoContext */ declare function JSONStringify(t: any): string;
110-
return JSONStringify([${inp.toString()}].slice(${start}, ${end}))`);
104+
const lua = util.transpileString(`return JSONStringify([${inp.toString()}].slice(${start}, ${end}))`);
111105

112106
// Execute
113107
const result = util.executeLua(lua);
@@ -129,7 +123,6 @@ export class LuaLibArrayTests {
129123
const lua = util.transpileString(
130124
`let spliceTestTable = [${inp.toString()}];
131125
spliceTestTable.splice(${start}, ${deleteCount}, ${newElements});
132-
/** !NoContext */ declare function JSONStringify(t: any): string;
133126
return JSONStringify(spliceTestTable);`
134127
);
135128

@@ -156,14 +149,12 @@ export class LuaLibArrayTests {
156149
lua = util.transpileString(
157150
`let spliceTestTable = [${inp.toString()}];
158151
spliceTestTable.splice(${start}, ${deleteCount}, ${newElements});
159-
/** !NoContext */ declare function JSONStringify(t: any): string;
160152
return JSONStringify(spliceTestTable);`
161153
);
162154
} else {
163155
lua = util.transpileString(
164156
`let spliceTestTable = [${inp.toString()}];
165157
spliceTestTable.splice(${start});
166-
/** !NoContext */ declare function JSONStringify(t: any): string;
167158
return JSONStringify(spliceTestTable);`
168159
);
169160
}
@@ -197,7 +188,6 @@ export class LuaLibArrayTests {
197188
// Transpile
198189
const lua = util.transpileString(
199190
`let concatTestTable = ${JSON.stringify(arr)};
200-
/** !NoContext */ declare function JSONStringify(t: any): string;
201191
return JSONStringify(concatTestTable.concat(${argStr}));`
202192
);
203193

@@ -288,7 +278,6 @@ export class LuaLibArrayTests {
288278
const lua = util.transpileString(
289279
`let testArray = [0];
290280
testArray.push(${inp.join(", ")});
291-
/** !NoContext */ declare function JSONStringify(t: any): string;
292281
return JSONStringify(testArray);
293282
`
294283
);
@@ -343,7 +332,6 @@ export class LuaLibArrayTests {
343332
const lua = util.transpileString(
344333
`let testArray = ${array};
345334
let val = testArray.reverse();
346-
/** !NoContext */ declare function JSONStringify(t: any): string;
347335
return JSONStringify(testArray)`);
348336

349337
// Execute
@@ -364,7 +352,6 @@ export class LuaLibArrayTests {
364352
const lua = util.transpileString(
365353
`let testArray = ${array};
366354
let val = testArray.shift();
367-
/** !NoContext */ declare function JSONStringify(t: any): string;
368355
return JSONStringify(testArray)`);
369356

370357
// Execute
@@ -398,7 +385,6 @@ export class LuaLibArrayTests {
398385
const lua = util.transpileString(
399386
`let testArray = ${array};
400387
testArray.unshift(${toUnshift});
401-
/** !NoContext */ declare function JSONStringify(t: any): string;
402388
return JSONStringify(testArray)`);
403389
// Execute
404390
const result = util.executeLua(lua);
@@ -418,7 +404,6 @@ export class LuaLibArrayTests {
418404
const lua = util.transpileString(
419405
`let testArray = ${array};
420406
testArray.sort();
421-
/** !NoContext */ declare function JSONStringify(t: any): string;
422407
return JSONStringify(testArray)`);
423408

424409
// Execute

test/unit/spreadElement.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ export class SpreadElementTest {
1010
@TestCase([1, "test", 3])
1111
@Test("Spread Element Push")
1212
public spreadElementPush(inp: any[]) {
13-
const lua = util.transpileString(`/** !NoContext */ declare function JSONStringify(t: any): string;
14-
return JSONStringify([].push(...${JSON.stringify(inp)}));`);
13+
const lua = util.transpileString(`return JSONStringify([].push(...${JSON.stringify(inp)}));`);
1514
const result = util.executeLua(lua);
1615
Expect(result).toBe([].push(...inp));
1716
}

test/unit/string.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,7 @@ export class StringTests {
274274
public split(inp: string, separator: string): void {
275275
// Transpile
276276
const lua = util.transpileString(
277-
`/** !NoContext */ declare function JSONStringify(t: any): string;
278-
return JSONStringify("${inp}".split("${separator}"))`
277+
`return JSONStringify("${inp}".split("${separator}"))`
279278
);
280279

281280
// Execute

0 commit comments

Comments
 (0)
X Tutup