From 1e5ee9bf7364594507acb1a2000bedb86a3f5d50 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Sat, 12 Jan 2019 15:27:35 +0100 Subject: [PATCH 1/2] Implemented try/catch and throw --- src/LuaTransformer.ts | 48 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/src/LuaTransformer.ts b/src/LuaTransformer.ts index b6131715e..c2b93c1c7 100644 --- a/src/LuaTransformer.ts +++ b/src/LuaTransformer.ts @@ -1350,12 +1350,50 @@ export class LuaTransformer { return tstl.createBreakStatement(undefined, breakStatement); } - public transformTryStatement(arg0: ts.TryStatement): StatementVisitResult { - throw new Error("Method not implemented."); + public transformTryStatement(statement: ts.TryStatement): StatementVisitResult { + const pCall = tstl.createIdentifier("pcall"); + const tryBlock = this.transformBlock(statement.tryBlock); + const tryResult = tstl.createIdentifier("__TS_try"); + + const returnVariables = statement.catchClause && statement.catchClause.variableDeclaration + ? [tryResult, this.transformIdentifier(statement.catchClause.variableDeclaration.name as ts.Identifier)] + : [tryResult]; + + const catchAssignment = this.createLocalOrGlobalDeclaration( + returnVariables, + tstl.createCallExpression(pCall, [tstl.createFunctionExpression(tryBlock)]) + ); + + const result: tstl.Statement[] = [catchAssignment]; + + if (statement.catchClause) { + const notTryResult = tstl.createUnaryExpression(tryResult, tstl.SyntaxKind.NotOperator); + result.push(tstl.createIfStatement(notTryResult, this.transformBlock(statement.catchClause.block))); + } + + if (statement.finallyBlock) { + result.push(...this.transformBlock(statement.finallyBlock).statements); + } + + return tstl.createDoStatement( + result, + undefined, + statement + ); } - public transformThrowStatement(arg0: ts.ThrowStatement): StatementVisitResult { - throw new Error("Method not implemented."); + public transformThrowStatement(statement: ts.ThrowStatement): StatementVisitResult { + const type = this.checker.getTypeAtLocation(statement.expression); + if (tsHelper.isStringType(type)) { + const error = tstl.createIdentifier("error"); + return tstl.createExpressionStatement( + tstl.createCallExpression(error, [this.transformExpression(statement.expression)]), + undefined, + statement + ); + } else { + throw TSTLErrors.InvalidThrowExpression(statement.expression); + } } public transformContinueStatement(statement: ts.ContinueStatement): StatementVisitResult { @@ -2889,7 +2927,7 @@ export class LuaTransformer { } private createLocalOrGlobalDeclaration( - lhs: tstl.Identifier, + lhs: tstl.Identifier | tstl.Identifier[], rhs: tstl.Expression, parent?: tstl.Node, tsOriginal?: ts.Node From e9060cf07dd6d8d03d13260a441f32fc90fcd8f4 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Sun, 13 Jan 2019 14:01:04 +0100 Subject: [PATCH 2/2] PR adjustments --- src/LuaTransformer.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/LuaTransformer.ts b/src/LuaTransformer.ts index c2b93c1c7..fa7c278e3 100644 --- a/src/LuaTransformer.ts +++ b/src/LuaTransformer.ts @@ -1353,13 +1353,13 @@ export class LuaTransformer { public transformTryStatement(statement: ts.TryStatement): StatementVisitResult { const pCall = tstl.createIdentifier("pcall"); const tryBlock = this.transformBlock(statement.tryBlock); - const tryResult = tstl.createIdentifier("__TS_try"); + const tryResult = tstl.createIdentifier("____TS_try"); const returnVariables = statement.catchClause && statement.catchClause.variableDeclaration ? [tryResult, this.transformIdentifier(statement.catchClause.variableDeclaration.name as ts.Identifier)] : [tryResult]; - const catchAssignment = this.createLocalOrGlobalDeclaration( + const catchAssignment = tstl.createVariableDeclarationStatement( returnVariables, tstl.createCallExpression(pCall, [tstl.createFunctionExpression(tryBlock)]) );