Skip to content

Commit 1c9eca6

Browse files
andreiraduPerryvw
authored andcommitted
-added support for standard array functions (#234)
* -added support for array.shift, array.unshift, array.sort and array.reverse * -updated tests * -properly fixed transpileLuaLibFunction() when called with no params
1 parent 8e9dfe8 commit 1c9eca6

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

src/Transpiler.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export enum LuaLibFeature {
2626
ArrayIndexOf = "ArrayIndexOf",
2727
ArrayMap = "ArrayMap",
2828
ArrayPush = "ArrayPush",
29+
ArrayReverse = "ArrayReverse",
30+
ArrayShift = "ArrayShift",
31+
ArrayUnshift = "ArrayUnshift",
32+
ArraySort = "ArraySort",
2933
ArraySlice = "ArraySlice",
3034
ArraySome = "ArraySome",
3135
ArraySplice = "ArraySplice",
@@ -305,6 +309,9 @@ export abstract class LuaTranspiler {
305309

306310
public transpileLuaLibFunction(func: LuaLibFeature, ...params: string[]): string {
307311
this.importLuaLibFeature(func);
312+
params = params.filter(element => {
313+
return element.toString() !== "";
314+
});
308315
return `__TS__${func}(${params.join(", ")})`;
309316
}
310317

@@ -1239,6 +1246,14 @@ export abstract class LuaTranspiler {
12391246
return this.transpileLuaLibFunction(LuaLibFeature.ArrayConcat, caller, params);
12401247
case "push":
12411248
return this.transpileLuaLibFunction(LuaLibFeature.ArrayPush, caller, params);
1249+
case "reverse":
1250+
return this.transpileLuaLibFunction(LuaLibFeature.ArrayReverse, caller);
1251+
case "shift":
1252+
return this.transpileLuaLibFunction(LuaLibFeature.ArrayShift, caller);
1253+
case "unshift":
1254+
return this.transpileLuaLibFunction(LuaLibFeature.ArrayUnshift, caller, params);
1255+
case "sort":
1256+
return this.transpileLuaLibFunction(LuaLibFeature.ArraySort, caller);
12421257
case "pop":
12431258
return `table.remove(${caller})`;
12441259
case "forEach":

src/lualib/ArrayReverse.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function __TS__ArrayReverse(arr: any[]): any[] {
2+
let i = 0;
3+
let j = arr.length - 1;
4+
while (i < j) {
5+
const temp = arr[j];
6+
arr[j] = arr[i];
7+
arr[i] = temp;
8+
i = i + 1;
9+
j = j - 1;
10+
}
11+
return arr;
12+
}

src/lualib/ArrayShift.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare namespace table {
2+
function remove<T>(arr: T[], idx: number): T;
3+
}
4+
function __TS__ArrayShift<T>(arr: T[]): T {
5+
return table.remove(arr, 1);
6+
}

src/lualib/ArraySort.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare namespace table {
2+
function sort<T>(arr: T[], compareFn?: (a: T, b: T) => number): void;
3+
}
4+
function __TS__ArraySort<T>(arr: T[], compareFn?: (a: T, b: T) => number): T[] {
5+
table.sort(arr, compareFn);
6+
return arr;
7+
}

src/lualib/ArrayUnshift.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
declare namespace table {
2+
function insert<T>(arr: T[], idx: number, val: T): void;
3+
}
4+
function __TS__ArrayUnshift<T>(arr: T[], ...items: T[]): number {
5+
for (let i = items.length - 1; i >= 0; --i) {
6+
table.insert(arr, 1, items[i]);
7+
}
8+
return arr.length;
9+
}

test/unit/lualib/lualib.spec.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,98 @@ export class LuaLibArrayTests {
320320
Expect(result).toBe(expected[1]);
321321
}
322322
}
323+
@TestCase("[1, 2, 3]", [3, 2, 1])
324+
@TestCase("[1, 2, 3, null]", [3, 2, 1])
325+
@TestCase("[1, 2, 3, 4]", [4, 3, 2, 1])
326+
@TestCase("[1]", [1])
327+
@TestCase("[]", [])
328+
@Test("array.reverse")
329+
public arrayReverse(array: string, expected): void {
330+
{
331+
// Transpile
332+
const lua = util.transpileString(
333+
`let testArray = ${array};
334+
let val = testArray.reverse();
335+
return JSONStringify(testArray)`);
336+
337+
// Execute
338+
const result = util.executeLua(lua);
339+
// Assert
340+
Expect(result).toBe(JSON.stringify(expected));
341+
}
342+
}
343+
@TestCase("[1, 2, 3]", [2, 3], 1)
344+
@TestCase("[1]", [], 1)
345+
@TestCase("[]", [], null)
346+
@Test("array.shift")
347+
public arrayShift(array: string, expectedArray: number[], expectedValue: number): void {
348+
{
349+
// test array mutation
350+
{
351+
// Transpile
352+
const lua = util.transpileString(
353+
`let testArray = ${array};
354+
let val = testArray.shift();
355+
return JSONStringify(testArray)`);
356+
357+
// Execute
358+
const result = util.executeLua(lua);
359+
// Assert
360+
Expect(result).toBe(JSON.stringify(expectedArray));
361+
}
362+
// test return value
363+
{
364+
// Transpile
365+
const lua = util.transpileString(
366+
`let testArray = ${array};
367+
let val = testArray.shift();
368+
return val`);
369+
370+
// Execute
371+
const result = util.executeLua(lua);
372+
// Assert
373+
Expect(result).toBe(expectedValue);
374+
}
375+
}
376+
}
377+
@TestCase("[3, 4, 5]", [1, 2], [1, 2, 3, 4, 5])
378+
@TestCase("[]", [], [])
379+
@TestCase("[1]", [], [1])
380+
@TestCase("[]", [1], [1])
381+
@Test("array.unshift")
382+
public arrayUnshift(array: string, toUnshift, expected): void {
383+
{
384+
// Transpile
385+
const lua = util.transpileString(
386+
`let testArray = ${array};
387+
testArray.unshift(${toUnshift});
388+
return JSONStringify(testArray)`);
389+
// Execute
390+
const result = util.executeLua(lua);
391+
392+
// Assert
393+
Expect(result).toBe(JSON.stringify(expected));
394+
}
395+
}
396+
@TestCase("[4, 5, 3, 2, 1]", [1, 2, 3, 4, 5])
397+
@TestCase("[1]", [1])
398+
@TestCase("[1, null]", [1])
399+
@TestCase("[]", [])
400+
@Test("array.sort")
401+
public arraySort(array: string, expected): void {
402+
{
403+
// Transpile
404+
const lua = util.transpileString(
405+
`let testArray = ${array};
406+
testArray.sort();
407+
return JSONStringify(testArray)`);
323408

409+
// Execute
410+
const result = util.executeLua(lua);
411+
// Assert
412+
Expect(result).toBe(JSON.stringify(expected));
413+
}
414+
}
324415
@TestCase("true", "4", "5", 4)
325416
@TestCase("false", "4", "5", 5)
326417
@TestCase("3", "4", "5", 4)

0 commit comments

Comments
 (0)