Skip to content

Commit d7a4764

Browse files
committed
Merge branch 'master' into polymorphism-fixes
2 parents 66642ac + 638a0d1 commit d7a4764

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
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: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -383,19 +383,17 @@ export class LuaTranspiler {
383383
let jumpTableName = "____switch" + this.genVarCounter;
384384
this.genVarCounter++;
385385

386-
result += this.indent + `local ${jumpTableName} = {\n`;
387-
388-
this.pushIndent();
386+
result += this.indent + `local ${jumpTableName} = {}\n`;
389387

390388
// If statement to go to right entry label
391389
clauses.forEach((clause, index) => {
392390
if (ts.isCaseClause(clause)) {
393391
result += this.indent + `-- case:\n`;
394-
result += this.indent + `[${this.transpileExpression(clause.expression, true)}] = function(self)\n`;
392+
result += this.indent + `${jumpTableName}[${this.transpileExpression(clause.expression, true)}] = function()\n`;
395393
}
396394
if (ts.isDefaultClause(clause)) {
397395
result += this.indent + `-- default:\n`;
398-
result += this.indent + `["____default${this.genVarCounter}"] = function(self)\n`;
396+
result += this.indent + `${jumpTableName}["____default${this.genVarCounter}"] = function()\n`;
399397
}
400398
this.pushIndent();
401399

@@ -418,9 +416,9 @@ export class LuaTranspiler {
418416

419417
if (i !== index && nextClause) {
420418
if (ts.isCaseClause(nextClause)) {
421-
result += this.indent + `self[${this.transpileExpression(nextClause.expression, true)}]()\n`;
419+
result += this.indent + `${jumpTableName}[${this.transpileExpression(nextClause.expression, true)}]()\n`;
422420
} else {
423-
result += this.indent + `self["____default${this.genVarCounter}"]()\n`;
421+
result += this.indent + `${jumpTableName}["____default${this.genVarCounter}"]()\n`;
424422
}
425423
}
426424
} else {
@@ -429,12 +427,10 @@ export class LuaTranspiler {
429427

430428
this.popIndent();
431429

432-
result += this.indent + `end,\n`;
430+
result += this.indent + `end\n`;
433431
});
434-
this.popIndent();
435-
result += this.indent + "}\n";
436-
result += this.indent + `if ${jumpTableName}[${expression}] then ${jumpTableName}[${expression}](${jumpTableName})\n`;
437-
result += this.indent + `elseif ${jumpTableName}["____default${this.genVarCounter}"] then ${jumpTableName}["____default${this.genVarCounter}"](${jumpTableName}) end\n`;
432+
result += this.indent + `if ${jumpTableName}[${expression}] then ${jumpTableName}[${expression}]()\n`;
433+
result += this.indent + `elseif ${jumpTableName}["____default${this.genVarCounter}"] then ${jumpTableName}["____default${this.genVarCounter}"]() end\n`;
438434
result += this.indent + "--------Switch statement end--------\n";
439435

440436
//Increment counter for next switch statement
@@ -874,10 +870,7 @@ export class LuaTranspiler {
874870
const caller = this.transpileExpression(expression.expression);
875871
switch (expression.name.escapedText) {
876872
case "push":
877-
if (node.arguments.length > 1) {
878-
throw new TranspileError("Unsupported array function: " + expression.name.escapedText + " with more than one argument", node);
879-
}
880-
return `table.insert(${caller}, ${params})`;
873+
return `TS_push(${caller}, ${params})`;
881874
case "forEach":
882875
return `TS_forEach(${caller}, ${params})`;
883876
case "indexOf":
@@ -1049,7 +1042,7 @@ export class LuaTranspiler {
10491042
const vars = node.name.elements.map(element => (<ts.Identifier>(<ts.BindingElement>element).name).escapedText).join(",");
10501043

10511044
// Don't unpack TupleReturn decorated functions
1052-
if (ts.isCallExpression(node.initializer)
1045+
if (ts.isCallExpression(node.initializer)
10531046
&& tsEx.isTupleReturnFunction(this.checker.getTypeAtLocation(node.initializer.expression), this.checker)) {
10541047
return `local ${vars}=${value}`;
10551048
} 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)