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
15 changes: 15 additions & 0 deletions src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export enum LuaLibFeature {
ArrayIndexOf = "ArrayIndexOf",
ArrayMap = "ArrayMap",
ArrayPush = "ArrayPush",
ArrayReverse = "ArrayReverse",
ArrayShift = "ArrayShift",
ArrayUnshift = "ArrayUnshift",
ArraySort = "ArraySort",
ArraySlice = "ArraySlice",
ArraySome = "ArraySome",
ArraySplice = "ArraySplice",
Expand Down Expand Up @@ -305,6 +309,9 @@ export abstract class LuaTranspiler {

public transpileLuaLibFunction(func: LuaLibFeature, ...params: string[]): string {
this.importLuaLibFeature(func);
params = params.filter(element => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't understand in what situation element.tostring() would be empty string, could you give an example?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, I hit this when calling array.unshift() with no params

return element.toString() !== "";
});
return `__TS__${func}(${params.join(", ")})`;
}

Expand Down Expand Up @@ -1239,6 +1246,14 @@ export abstract class LuaTranspiler {
return this.transpileLuaLibFunction(LuaLibFeature.ArrayConcat, caller, params);
case "push":
return this.transpileLuaLibFunction(LuaLibFeature.ArrayPush, caller, params);
case "reverse":
return this.transpileLuaLibFunction(LuaLibFeature.ArrayReverse, caller);
case "shift":
return this.transpileLuaLibFunction(LuaLibFeature.ArrayShift, caller);
case "unshift":
return this.transpileLuaLibFunction(LuaLibFeature.ArrayUnshift, caller, params);
case "sort":
return this.transpileLuaLibFunction(LuaLibFeature.ArraySort, caller);
case "pop":
return `table.remove(${caller})`;
case "forEach":
Expand Down
12 changes: 12 additions & 0 deletions src/lualib/ArrayReverse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function __TS__ArrayReverse(arr: any[]): any[] {
let i = 0;
let j = arr.length - 1;
while (i < j) {
const temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i = i + 1;
j = j - 1;
}
return arr;
}
6 changes: 6 additions & 0 deletions src/lualib/ArrayShift.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare namespace table {
function remove<T>(arr: T[], idx: number): T;
}
function __TS__ArrayShift<T>(arr: T[]): T {
return table.remove(arr, 1);
}
7 changes: 7 additions & 0 deletions src/lualib/ArraySort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare namespace table {
function sort<T>(arr: T[], compareFn?: (a: T, b: T) => number): void;
}
function __TS__ArraySort<T>(arr: T[], compareFn?: (a: T, b: T) => number): T[] {
table.sort(arr, compareFn);
return arr;
}
9 changes: 9 additions & 0 deletions src/lualib/ArrayUnshift.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare namespace table {
function insert<T>(arr: T[], idx: number, val: T): void;
}
function __TS__ArrayUnshift<T>(arr: T[], ...items: T[]): number {
for (let i = items.length - 1; i >= 0; --i) {
table.insert(arr, 1, items[i]);
}
return arr.length;
}
91 changes: 91 additions & 0 deletions test/unit/lualib/lualib.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,98 @@ export class LuaLibArrayTests {
Expect(result).toBe(expected[1]);
}
}
@TestCase("[1, 2, 3]", [3, 2, 1])
@TestCase("[1, 2, 3, null]", [3, 2, 1])
@TestCase("[1, 2, 3, 4]", [4, 3, 2, 1])
@TestCase("[1]", [1])
@TestCase("[]", [])
@Test("array.reverse")
public arrayReverse(array: string, expected): void {
{
// Transpile
const lua = util.transpileString(
`let testArray = ${array};
let val = testArray.reverse();
return JSONStringify(testArray)`);

// Execute
const result = util.executeLua(lua);
// Assert
Expect(result).toBe(JSON.stringify(expected));
}
}
@TestCase("[1, 2, 3]", [2, 3], 1)
@TestCase("[1]", [], 1)
@TestCase("[]", [], null)
@Test("array.shift")
public arrayShift(array: string, expectedArray: number[], expectedValue: number): void {
{
// test array mutation
{
// Transpile
const lua = util.transpileString(
`let testArray = ${array};
let val = testArray.shift();
return JSONStringify(testArray)`);

// Execute
const result = util.executeLua(lua);
// Assert
Expect(result).toBe(JSON.stringify(expectedArray));
}
// test return value
{
// Transpile
const lua = util.transpileString(
`let testArray = ${array};
let val = testArray.shift();
return val`);

// Execute
const result = util.executeLua(lua);
// Assert
Expect(result).toBe(expectedValue);
}
}
}
@TestCase("[3, 4, 5]", [1, 2], [1, 2, 3, 4, 5])
@TestCase("[]", [], [])
@TestCase("[1]", [], [1])
@TestCase("[]", [1], [1])
@Test("array.unshift")
public arrayUnshift(array: string, toUnshift, expected): void {
{
// Transpile
const lua = util.transpileString(
`let testArray = ${array};
testArray.unshift(${toUnshift});
return JSONStringify(testArray)`);
// Execute
const result = util.executeLua(lua);

// Assert
Expect(result).toBe(JSON.stringify(expected));
}
}
@TestCase("[4, 5, 3, 2, 1]", [1, 2, 3, 4, 5])
@TestCase("[1]", [1])
@TestCase("[1, null]", [1])
@TestCase("[]", [])
@Test("array.sort")
public arraySort(array: string, expected): void {
{
// Transpile
const lua = util.transpileString(
`let testArray = ${array};
testArray.sort();
return JSONStringify(testArray)`);

// Execute
const result = util.executeLua(lua);
// Assert
Expect(result).toBe(JSON.stringify(expected));
}
}
@TestCase("true", "4", "5", 4)
@TestCase("false", "4", "5", 5)
@TestCase("3", "4", "5", 4)
Expand Down