Skip to content

Commit 8f692fd

Browse files
zapp-brannigan-dotaPerryvw
authored andcommitted
Add multiple argument support to array.push() (#62)
* Add multiple argument support to array.push() * Add TS_push() to lualib * Fix TS_push() and add tests * Fix array.push test * Fix array.push test
1 parent f32d444 commit 8f692fd

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

dist/lualib/typescript.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ function TS_split(str, separator)
115115
return out
116116
end
117117

118+
function TS_push(list, ...)
119+
for _, v in pairs({...}) do
120+
list[#list + 1] = v
121+
end
122+
end
123+
118124
-- Set data structure implementation
119125
Set = Set or {}
120126
Set.__index = Set

src/Transpiler.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -899,10 +899,7 @@ export class LuaTranspiler {
899899
const caller = this.transpileExpression(expression.expression);
900900
switch (expression.name.escapedText) {
901901
case "push":
902-
if (node.arguments.length > 1) {
903-
throw new TranspileError("Unsupported array function: " + expression.name.escapedText + " with more than one argument", node);
904-
}
905-
return `table.insert(${caller}, ${params})`;
902+
return `TS_push(${caller}, ${params})`;
906903
case "forEach":
907904
return `TS_forEach(${caller}, ${params})`;
908905
case "indexOf":
@@ -1074,7 +1071,7 @@ export class LuaTranspiler {
10741071
const vars = node.name.elements.map(element => (<ts.Identifier>(<ts.BindingElement>element).name).escapedText).join(",");
10751072

10761073
// Don't unpack TupleReturn decorated functions
1077-
if (ts.isCallExpression(node.initializer)
1074+
if (ts.isCallExpression(node.initializer)
10781075
&& tsEx.isTupleReturnFunction(this.checker.getTypeAtLocation(node.initializer.expression), this.checker)) {
10791076
return `local ${vars}=${value}`;
10801077
} else {

test/integration/lua/lualib.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,23 @@ export class LuaLibArrayTests {
225225
// Assert
226226
Expect(result).toBe(expected);
227227
}
228+
229+
@TestCase([1])
230+
@TestCase([1, 2, 3])
231+
@Test("array.push")
232+
public arrayPush(inp: number[]) {
233+
// Transpile
234+
let lua = util.transpileString(
235+
`let testArray = [0];
236+
testArray.push(${inp.join(', ')});
237+
return JSONStringify(testArray);
238+
`
239+
, util.dummyTypes.Array);
240+
241+
// Execute
242+
let result = util.executeLua(lua);
243+
244+
// Assert
245+
Expect(result).toBe(JSON.stringify([0].concat(inp)));
246+
}
228247
}

0 commit comments

Comments
 (0)