Skip to content

Commit 5f1b483

Browse files
authored
Ts to lua ast - recursive function fix and try/catch refactor (#341)
* fixed recursive local functions * refactored try/catch to not use temp when not needed and fixed transformation tests
1 parent 85fc1da commit 5f1b483

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

src/LuaTransformer.ts

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ export class LuaTransformer {
397397
// exports.className = baseName.new()
398398
const classVar = this.createLocalOrGlobalDeclaration(className, rhs, undefined, statement);
399399

400-
result.push(classVar);
400+
result.push(...classVar);
401401
} else {
402402
// {}
403403
let rhs: tstl.Expression = tstl.createTableExpression();
@@ -412,7 +412,7 @@ export class LuaTransformer {
412412
// exports.className = {}
413413
const classVar = this.createLocalOrGlobalDeclaration(className, rhs, undefined, statement);
414414

415-
result.push(classVar);
415+
result.push(...classVar);
416416
}
417417

418418
// className.__index
@@ -815,26 +815,26 @@ export class LuaTransformer {
815815

816816
const membersOnly = tsHelper.getCustomDecorators(type, this.checker).has(DecoratorKind.CompileMembersOnly);
817817

818-
const result = [];
818+
const result: tstl.Statement[] = [];
819819

820820
if (!membersOnly) {
821821
const name = this.transformIdentifier(enumDeclaration.name);
822822
const table = tstl.createTableExpression();
823-
result.push(this.createLocalOrGlobalDeclaration(name, table, undefined, enumDeclaration));
823+
result.push(...this.createLocalOrGlobalDeclaration(name, table, undefined, enumDeclaration));
824824
}
825825

826826
for (const enumMember of this.computeEnumMembers(enumDeclaration)) {
827827
const memberName = this.transformPropertyName(enumMember.name);
828828
if (membersOnly) {
829829
if (tstl.isIdentifier(memberName)) {
830-
result.push(this.createLocalOrGlobalDeclaration(
830+
result.push(...this.createLocalOrGlobalDeclaration(
831831
memberName,
832832
enumMember.value,
833833
undefined,
834834
enumDeclaration
835835
));
836836
} else {
837-
result.push(this.createLocalOrGlobalDeclaration(
837+
result.push(...this.createLocalOrGlobalDeclaration(
838838
tstl.createIdentifier(enumMember.name.getText(), undefined, enumMember.name),
839839
enumMember.value,
840840
undefined,
@@ -886,7 +886,7 @@ export class LuaTransformer {
886886
});
887887
}
888888

889-
public transformFunctionDeclaration(functionDeclaration: ts.FunctionDeclaration): tstl.Statement {
889+
public transformFunctionDeclaration(functionDeclaration: ts.FunctionDeclaration): StatementVisitResult {
890890
// Don't transform functions without body (overload declarations)
891891
if (!functionDeclaration.body) {
892892
return undefined;
@@ -1353,26 +1353,30 @@ export class LuaTransformer {
13531353
public transformTryStatement(statement: ts.TryStatement): StatementVisitResult {
13541354
const pCall = tstl.createIdentifier("pcall");
13551355
const tryBlock = this.transformBlock(statement.tryBlock);
1356-
const tryResult = tstl.createIdentifier("____TS_try");
1356+
const tryCall = tstl.createCallExpression(pCall, [tstl.createFunctionExpression(tryBlock)]);
13571357

1358-
const returnVariables = statement.catchClause && statement.catchClause.variableDeclaration
1359-
? [tryResult, this.transformIdentifier(statement.catchClause.variableDeclaration.name as ts.Identifier)]
1360-
: [tryResult];
1358+
const result: tstl.Statement[] = [];
13611359

1362-
const catchAssignment = tstl.createVariableDeclarationStatement(
1363-
returnVariables,
1364-
tstl.createCallExpression(pCall, [tstl.createFunctionExpression(tryBlock)])
1365-
);
1360+
if (statement.catchClause) {
1361+
const tryResult = tstl.createIdentifier("____TS_try");
13661362

1367-
const result: tstl.Statement[] = [catchAssignment];
1363+
const returnVariables = statement.catchClause && statement.catchClause.variableDeclaration
1364+
? [tryResult, this.transformIdentifier(statement.catchClause.variableDeclaration.name as ts.Identifier)]
1365+
: [tryResult];
1366+
1367+
const catchAssignment = tstl.createVariableDeclarationStatement(returnVariables, tryCall);
1368+
1369+
result.push(catchAssignment);
13681370

1369-
if (statement.catchClause) {
13701371
const notTryResult = tstl.createUnaryExpression(tryResult, tstl.SyntaxKind.NotOperator);
13711372
result.push(tstl.createIfStatement(notTryResult, this.transformBlock(statement.catchClause.block)));
1373+
1374+
} else {
1375+
result.push(tstl.createExpressionStatement(tryCall));
13721376
}
13731377

13741378
if (statement.finallyBlock) {
1375-
result.push(...this.transformBlock(statement.finallyBlock).statements);
1379+
result.push(tstl.createDoStatement(this.transformBlock(statement.finallyBlock).statements));
13761380
}
13771381

13781382
return tstl.createDoStatement(
@@ -2931,16 +2935,17 @@ export class LuaTransformer {
29312935
rhs: tstl.Expression,
29322936
parent?: tstl.Node,
29332937
tsOriginal?: ts.Node
2934-
): tstl.Statement
2938+
): tstl.Statement[]
29352939
{
2940+
const statements: tstl.Statement[] = [];
29362941
if (this.isModule
29372942
|| this.currentNamespace
29382943
|| (tsOriginal && tsHelper.findFirstNodeAbove(tsOriginal, ts.isFunctionLike))
29392944
) {
2940-
return tstl.createVariableDeclarationStatement(lhs, rhs, parent, tsOriginal);
2941-
} else {
2942-
return tstl.createAssignmentStatement(lhs, rhs, parent, tsOriginal);
2945+
statements.push(tstl.createVariableDeclarationStatement(lhs, undefined, parent));
29432946
}
2947+
statements.push(tstl.createAssignmentStatement(lhs, rhs, parent, tsOriginal));
2948+
return statements;
29442949
}
29452950

29462951
private validateFunctionAssignment(node: ts.Node, fromType: ts.Type, toType: ts.Type, toName?: string): void {

test/translation/lua/tryCatch.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
do
2-
local __TS_try, er = pcall(function()
2+
local ____TS_try, er = pcall(function ()
33
local a = 42;
44
end);
5-
if not __TS_try then
5+
if not ____TS_try then
66
local b = "fail";
77
end
88
end

test/translation/lua/tryCatchFinally.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
do
2-
local __TS_try, er = pcall(function()
2+
local ____TS_try, er = pcall(function ()
33
local a = 42;
44
end);
5-
if not __TS_try then
5+
if not ____TS_try then
66
local b = "fail";
77
end
88
do

test/translation/lua/tryFinally.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
do
2-
pcall(function()
2+
pcall(function ()
33
local a = 42;
44
end);
55
do

0 commit comments

Comments
 (0)