Skip to content

Commit 3595bd4

Browse files
authored
Implemented try/catch and throw (#334)
* Implemented try/catch and throw * PR adjustments
1 parent 761cd14 commit 3595bd4

1 file changed

Lines changed: 43 additions & 5 deletions

File tree

src/LuaTransformer.ts

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,12 +1350,50 @@ export class LuaTransformer {
13501350
return tstl.createBreakStatement(undefined, breakStatement);
13511351
}
13521352

1353-
public transformTryStatement(arg0: ts.TryStatement): StatementVisitResult {
1354-
throw new Error("Method not implemented.");
1353+
public transformTryStatement(statement: ts.TryStatement): StatementVisitResult {
1354+
const pCall = tstl.createIdentifier("pcall");
1355+
const tryBlock = this.transformBlock(statement.tryBlock);
1356+
const tryResult = tstl.createIdentifier("____TS_try");
1357+
1358+
const returnVariables = statement.catchClause && statement.catchClause.variableDeclaration
1359+
? [tryResult, this.transformIdentifier(statement.catchClause.variableDeclaration.name as ts.Identifier)]
1360+
: [tryResult];
1361+
1362+
const catchAssignment = tstl.createVariableDeclarationStatement(
1363+
returnVariables,
1364+
tstl.createCallExpression(pCall, [tstl.createFunctionExpression(tryBlock)])
1365+
);
1366+
1367+
const result: tstl.Statement[] = [catchAssignment];
1368+
1369+
if (statement.catchClause) {
1370+
const notTryResult = tstl.createUnaryExpression(tryResult, tstl.SyntaxKind.NotOperator);
1371+
result.push(tstl.createIfStatement(notTryResult, this.transformBlock(statement.catchClause.block)));
1372+
}
1373+
1374+
if (statement.finallyBlock) {
1375+
result.push(...this.transformBlock(statement.finallyBlock).statements);
1376+
}
1377+
1378+
return tstl.createDoStatement(
1379+
result,
1380+
undefined,
1381+
statement
1382+
);
13551383
}
13561384

1357-
public transformThrowStatement(arg0: ts.ThrowStatement): StatementVisitResult {
1358-
throw new Error("Method not implemented.");
1385+
public transformThrowStatement(statement: ts.ThrowStatement): StatementVisitResult {
1386+
const type = this.checker.getTypeAtLocation(statement.expression);
1387+
if (tsHelper.isStringType(type)) {
1388+
const error = tstl.createIdentifier("error");
1389+
return tstl.createExpressionStatement(
1390+
tstl.createCallExpression(error, [this.transformExpression(statement.expression)]),
1391+
undefined,
1392+
statement
1393+
);
1394+
} else {
1395+
throw TSTLErrors.InvalidThrowExpression(statement.expression);
1396+
}
13591397
}
13601398

13611399
public transformContinueStatement(statement: ts.ContinueStatement): StatementVisitResult {
@@ -2889,7 +2927,7 @@ export class LuaTransformer {
28892927
}
28902928

28912929
private createLocalOrGlobalDeclaration(
2892-
lhs: tstl.Identifier,
2930+
lhs: tstl.Identifier | tstl.Identifier[],
28932931
rhs: tstl.Expression,
28942932
parent?: tstl.Node,
28952933
tsOriginal?: ts.Node

0 commit comments

Comments
 (0)