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
6 changes: 6 additions & 0 deletions dist/lualib/typescript.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ function TS_split(str, separator)
return out
end

function TS_push(list, ...)
for _, v in pairs({...}) do
list[#list + 1] = v
end
end

-- Set data structure implementation
Set = Set or {}
Set.__index = Set
Expand Down
7 changes: 2 additions & 5 deletions src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -899,10 +899,7 @@ export class LuaTranspiler {
const caller = this.transpileExpression(expression.expression);
switch (expression.name.escapedText) {
case "push":
if (node.arguments.length > 1) {
throw new TranspileError("Unsupported array function: " + expression.name.escapedText + " with more than one argument", node);
}
return `table.insert(${caller}, ${params})`;
return `TS_push(${caller}, ${params})`;
case "forEach":
return `TS_forEach(${caller}, ${params})`;
case "indexOf":
Expand Down Expand Up @@ -1074,7 +1071,7 @@ export class LuaTranspiler {
const vars = node.name.elements.map(element => (<ts.Identifier>(<ts.BindingElement>element).name).escapedText).join(",");

// Don't unpack TupleReturn decorated functions
if (ts.isCallExpression(node.initializer)
if (ts.isCallExpression(node.initializer)
&& tsEx.isTupleReturnFunction(this.checker.getTypeAtLocation(node.initializer.expression), this.checker)) {
return `local ${vars}=${value}`;
} else {
Expand Down
19 changes: 19 additions & 0 deletions test/integration/lua/lualib.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,23 @@ export class LuaLibArrayTests {
// Assert
Expect(result).toBe(expected);
}

@TestCase([1])
@TestCase([1, 2, 3])
@Test("array.push")
public arrayPush(inp: number[]) {
// Transpile
let lua = util.transpileString(
`let testArray = [0];
testArray.push(${inp.join(', ')});
return JSONStringify(testArray);
`
, util.dummyTypes.Array);

// Execute
let result = util.executeLua(lua);

// Assert
Expect(result).toBe(JSON.stringify([0].concat(inp)));
}
}