From fb7a6c3be66f678c92138bc77b7a6b0989338bda Mon Sep 17 00:00:00 2001 From: "andreiteodor.radu@gameloft.com" Date: Thu, 23 Aug 2018 13:48:35 +0300 Subject: [PATCH] -added support for array.pop --- src/Transpiler.ts | 2 ++ test/unit/lualib/lualib.spec.ts | 34 ++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Transpiler.ts b/src/Transpiler.ts index 5108276de..95dd73ace 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -1204,6 +1204,8 @@ export abstract class LuaTranspiler { switch (expressionName) { case "push": return this.transpileLuaLibFunction(LuaLibFeature.ArrayPush, caller, params); + case "pop": + return `table.remove(${caller})`; case "forEach": return this.transpileLuaLibFunction(LuaLibFeature.ArrayForEach, caller, params); case "indexOf": diff --git a/test/unit/lualib/lualib.spec.ts b/test/unit/lualib/lualib.spec.ts index ecc470fc7..30279827c 100644 --- a/test/unit/lualib/lualib.spec.ts +++ b/test/unit/lualib/lualib.spec.ts @@ -262,6 +262,38 @@ export class LuaLibArrayTests { Expect(result).toBe(JSON.stringify([0].concat(inp))); } + @TestCase("[1, 2, 3]", [3, 2]) + @TestCase("[1, 2, 3, null]", [3, 2]) + @Test("array.pop") + public arrayPop(array: string, expected) { + { + // Transpile + const lua = util.transpileString( + `let testArray = ${array}; + let val = testArray.pop(); + return val`); + + // Execute + const result = util.executeLua(lua); + + // Assert + Expect(result).toBe(expected[0]); + } + { + // Transpile + const lua = util.transpileString( + `let testArray = ${array}; + testArray.pop(); + return testArray.length`); + + // Execute + const result = util.executeLua(lua); + + // Assert + Expect(result).toBe(expected[1]); + } + } + @TestCase("true", "4", "5", 4) @TestCase("false", "4", "5", 5) @TestCase("3", "4", "5", 4) @@ -276,7 +308,7 @@ export class LuaLibArrayTests { // Assert Expect(result).toBe(expected); } - + @TestCase("true", 11) @TestCase("false", 13) @TestCase("a < 4", 13)