From 7d5e23343347b1774e67a8ea7649c06a07d55c1f Mon Sep 17 00:00:00 2001 From: = <=> Date: Fri, 12 Oct 2018 21:46:40 +0300 Subject: [PATCH 1/3] -added support for array.shift, array.unshift, array.sort and array.reverse --- src/Transpiler.ts | 18 ++++++++- src/lualib/ArrayReverse.ts | 12 ++++++ src/lualib/ArrayShift.ts | 6 +++ src/lualib/ArraySort.ts | 7 ++++ src/lualib/ArrayUnshift.ts | 9 +++++ test/unit/lualib/lualib.spec.ts | 66 +++++++++++++++++++++++++++++++++ 6 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 src/lualib/ArrayReverse.ts create mode 100644 src/lualib/ArrayShift.ts create mode 100644 src/lualib/ArraySort.ts create mode 100644 src/lualib/ArrayUnshift.ts diff --git a/src/Transpiler.ts b/src/Transpiler.ts index 94070209e..d536069d1 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -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", @@ -305,7 +309,11 @@ export abstract class LuaTranspiler { public transpileLuaLibFunction(func: LuaLibFeature, ...params: string[]): string { this.importLuaLibFeature(func); - return `__TS__${func}(${params.join(", ")})`; + if (params.length > 1) { + return `__TS__${func}(${params.join(", ")})`; + } else { + return `__TS__${func}(${params[0]})`; + } } public transpileImport(node: ts.ImportDeclaration): string { @@ -1239,6 +1247,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": diff --git a/src/lualib/ArrayReverse.ts b/src/lualib/ArrayReverse.ts new file mode 100644 index 000000000..3c4839417 --- /dev/null +++ b/src/lualib/ArrayReverse.ts @@ -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; +} diff --git a/src/lualib/ArrayShift.ts b/src/lualib/ArrayShift.ts new file mode 100644 index 000000000..a95df1a49 --- /dev/null +++ b/src/lualib/ArrayShift.ts @@ -0,0 +1,6 @@ +declare namespace table { + function remove(arr: T[], idx: number): T; +} +function __TS__ArrayShift(arr: T[]): T { + return table.remove(arr, 1); +} diff --git a/src/lualib/ArraySort.ts b/src/lualib/ArraySort.ts new file mode 100644 index 000000000..18745695c --- /dev/null +++ b/src/lualib/ArraySort.ts @@ -0,0 +1,7 @@ +declare namespace table { + function sort(arr: T[], compareFn?: (a: T, b: T) => number): void; +} +function __TS__ArraySort(arr: T[], compareFn?: (a: T, b: T) => number): T[] { + table.sort(arr, compareFn); + return arr; +} diff --git a/src/lualib/ArrayUnshift.ts b/src/lualib/ArrayUnshift.ts new file mode 100644 index 000000000..cb0031100 --- /dev/null +++ b/src/lualib/ArrayUnshift.ts @@ -0,0 +1,9 @@ +declare namespace table { + function insert(arr: T[], idx: number, val: T): void; +} +function __TS__ArrayUnshift(arr: T[], ...items: T[]): number { + for (let i = items.length - 1; i >= 0; --i) { + table.insert(arr, 1, items[i]); + } + return arr.length; +} diff --git a/test/unit/lualib/lualib.spec.ts b/test/unit/lualib/lualib.spec.ts index 328e346ee..5dab40de2 100644 --- a/test/unit/lualib/lualib.spec.ts +++ b/test/unit/lualib/lualib.spec.ts @@ -320,7 +320,73 @@ 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]) + @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]) + @Test("array.shift") + public arrayShift(array: string, expected): void { + { + // 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(expected)); + } + } + @TestCase("[3, 4, 5]", [1, 2], [1, 2, 3, 4, 5]) + @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]) + @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) From 59e4e13f8e49e8d0658a325658e219c688698bc4 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sat, 13 Oct 2018 12:17:21 +0300 Subject: [PATCH 2/3] -updated tests --- test/unit/lualib/lualib.spec.ts | 51 ++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/test/unit/lualib/lualib.spec.ts b/test/unit/lualib/lualib.spec.ts index 5dab40de2..b81f83c90 100644 --- a/test/unit/lualib/lualib.spec.ts +++ b/test/unit/lualib/lualib.spec.ts @@ -323,6 +323,8 @@ export class LuaLibArrayTests { @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 { { @@ -338,23 +340,44 @@ export class LuaLibArrayTests { Expect(result).toBe(JSON.stringify(expected)); } } - @TestCase("[1, 2, 3]", [2, 3]) + @TestCase("[1, 2, 3]", [2, 3], 1) + @TestCase("[1]", [], 1) + @TestCase("[]", [], null) @Test("array.shift") - public arrayShift(array: string, expected): void { + public arrayShift(array: string, expectedArray: number[], expectedValue: number): void { { - // 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(expected)); + // 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 { { @@ -363,7 +386,6 @@ export class LuaLibArrayTests { `let testArray = ${array}; testArray.unshift(${toUnshift}); return JSONStringify(testArray)`); - // Execute const result = util.executeLua(lua); @@ -372,6 +394,9 @@ export class LuaLibArrayTests { } } @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 { { From 2850ddf0893fa58f1b9584406de2c217a7f13a8b Mon Sep 17 00:00:00 2001 From: = <=> Date: Sat, 13 Oct 2018 12:17:39 +0300 Subject: [PATCH 3/3] -properly fixed transpileLuaLibFunction() when called with no params --- src/Transpiler.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Transpiler.ts b/src/Transpiler.ts index d536069d1..a21aa483e 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -309,11 +309,10 @@ export abstract class LuaTranspiler { public transpileLuaLibFunction(func: LuaLibFeature, ...params: string[]): string { this.importLuaLibFeature(func); - if (params.length > 1) { - return `__TS__${func}(${params.join(", ")})`; - } else { - return `__TS__${func}(${params[0]})`; - } + params = params.filter(element => { + return element.toString() !== ""; + }); + return `__TS__${func}(${params.join(", ")})`; } public transpileImport(node: ts.ImportDeclaration): string {