From 7ebd63d106103bc3c9ec1115102357e6301a5bae Mon Sep 17 00:00:00 2001 From: ark120202 Date: Tue, 26 Feb 2019 20:53:40 +0500 Subject: [PATCH 1/3] Fix array.sort compare function not working --- src/lualib/ArraySort.ts | 8 ++++++-- test/unit/lualib/lualib.spec.ts | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/lualib/ArraySort.ts b/src/lualib/ArraySort.ts index 18745695c..01229598d 100644 --- a/src/lualib/ArraySort.ts +++ b/src/lualib/ArraySort.ts @@ -1,7 +1,11 @@ declare namespace table { - function sort(arr: T[], compareFn?: (a: T, b: T) => number): void; + function sort(arr: T[], compareFn?: (a: T, b: T) => boolean): void; } function __TS__ArraySort(arr: T[], compareFn?: (a: T, b: T) => number): T[] { - table.sort(arr, compareFn); + if (compareFn !== undefined) { + table.sort(arr, (a, b) => compareFn(a, b) < 0); + } else { + table.sort(arr); + } return arr; } diff --git a/test/unit/lualib/lualib.spec.ts b/test/unit/lualib/lualib.spec.ts index be87d1084..21adcad4a 100644 --- a/test/unit/lualib/lualib.spec.ts +++ b/test/unit/lualib/lualib.spec.ts @@ -352,6 +352,22 @@ export class LuaLibTests // Assert Expect(result).toBe(JSON.stringify(expected)); } + + @TestCase("[4, 5, 3, 2, 1]", [1, 2, 3, 4, 5], "a - b") + @TestCase('["4", "5", "3", "2", "1"]', ["1", "2", "3", "4", "5"], "Number(a) - Number(b)") + @TestCase('["4", "5", "3", "2", "1"]', ["5", "4", "3", "2", "1"], "Number(b) - Number(a)") + @Test("array.sort with compare function") + public arraySortWithCompareFunction(array: string, expected: any, compare: string): void + { + const result = util.transpileAndExecute( + `let testArray = ${array}; + testArray.sort((a, b) => ${compare}); + return JSONStringify(testArray)`); + + // Assert + Expect(result).toBe(JSON.stringify(expected)); + } + @TestCase("true", "4", "5", 4) @TestCase("false", "4", "5", 5) @TestCase("3", "4", "5", 4) From 5e7ad1890ef887d43a37f5312102852050f4ab3b Mon Sep 17 00:00:00 2001 From: ark120202 Date: Tue, 26 Feb 2019 22:03:02 +0500 Subject: [PATCH 2/3] Fix compare function not being passed --- src/LuaTransformer.ts | 2 +- test/unit/lualib/lualib.spec.ts | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/LuaTransformer.ts b/src/LuaTransformer.ts index 7d7420ca6..bbc7e59a1 100644 --- a/src/LuaTransformer.ts +++ b/src/LuaTransformer.ts @@ -3715,7 +3715,7 @@ export class LuaTransformer { case "unshift": return this.transformLuaLibFunction(LuaLibFeature.ArrayUnshift, node, caller, ...params); case "sort": - return this.transformLuaLibFunction(LuaLibFeature.ArraySort, node, caller); + return this.transformLuaLibFunction(LuaLibFeature.ArraySort, node, caller, ...params); case "pop": return tstl.createCallExpression( tstl.createTableIndexExpression(tstl.createIdentifier("table"), tstl.createStringLiteral("remove")), diff --git a/test/unit/lualib/lualib.spec.ts b/test/unit/lualib/lualib.spec.ts index 21adcad4a..4d9351b60 100644 --- a/test/unit/lualib/lualib.spec.ts +++ b/test/unit/lualib/lualib.spec.ts @@ -354,15 +354,19 @@ export class LuaLibTests } @TestCase("[4, 5, 3, 2, 1]", [1, 2, 3, 4, 5], "a - b") - @TestCase('["4", "5", "3", "2", "1"]', ["1", "2", "3", "4", "5"], "Number(a) - Number(b)") - @TestCase('["4", "5", "3", "2", "1"]', ["5", "4", "3", "2", "1"], "Number(b) - Number(a)") + @TestCase('["4", "5", "3", "2", "1"]', ["1", "2", "3", "4", "5"], "tonumber(a) - tonumber(b)") + @TestCase('["4", "5", "3", "2", "1"]', ["5", "4", "3", "2", "1"], "tonumber(b) - tonumber(a)") @Test("array.sort with compare function") public arraySortWithCompareFunction(array: string, expected: any, compare: string): void { const result = util.transpileAndExecute( `let testArray = ${array}; testArray.sort((a, b) => ${compare}); - return JSONStringify(testArray)`); + return JSONStringify(testArray)`, + undefined, + undefined, + `declare function tonumber(e: any): number` + ); // Assert Expect(result).toBe(JSON.stringify(expected)); From 93eecc27e6a1d3ed59f484018d57669f2622fa16 Mon Sep 17 00:00:00 2001 From: ark120202 Date: Tue, 26 Feb 2019 22:34:42 +0500 Subject: [PATCH 3/3] Change array.sort tests to compare with js results --- test/unit/lualib/lualib.spec.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/test/unit/lualib/lualib.spec.ts b/test/unit/lualib/lualib.spec.ts index 4d9351b60..f0f0a0257 100644 --- a/test/unit/lualib/lualib.spec.ts +++ b/test/unit/lualib/lualib.spec.ts @@ -353,15 +353,18 @@ export class LuaLibTests Expect(result).toBe(JSON.stringify(expected)); } - @TestCase("[4, 5, 3, 2, 1]", [1, 2, 3, 4, 5], "a - b") - @TestCase('["4", "5", "3", "2", "1"]', ["1", "2", "3", "4", "5"], "tonumber(a) - tonumber(b)") - @TestCase('["4", "5", "3", "2", "1"]', ["5", "4", "3", "2", "1"], "tonumber(b) - tonumber(a)") @Test("array.sort with compare function") - public arraySortWithCompareFunction(array: string, expected: any, compare: string): void - { + @TestCase([1, 2, 3, 4, 5], "a - b", (a: number, b: number) => a - b) + @TestCase(["4", "5", "3", "2", "1"], "tonumber(a) - tonumber(b)", (a: string, b: string) => Number(a) - Number(b)) + @TestCase(["4", "5", "3", "2", "1"], "tonumber(b) - tonumber(a)", (a: string, b: string) => Number(b) - Number(a)) + public arraySortWithCompareFunction( + array: any[], + compareStr: string, + compareFn: (a: any, b: any) => number + ): void { const result = util.transpileAndExecute( - `let testArray = ${array}; - testArray.sort((a, b) => ${compare}); + `let testArray = ${JSON.stringify(array)}; + testArray.sort((a, b) => ${compareStr}); return JSONStringify(testArray)`, undefined, undefined, @@ -369,7 +372,7 @@ export class LuaLibTests ); // Assert - Expect(result).toBe(JSON.stringify(expected)); + Expect(result).toBe(JSON.stringify(array.sort(compareFn))); } @TestCase("true", "4", "5", 4)