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
40 changes: 30 additions & 10 deletions src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,24 +661,44 @@ export abstract class LuaTranspiler {
}

public transpileTry(node: ts.TryStatement): string {
let tryFunc = "function()\n";
let result = this.indent + "do\n";
this.pushIndent();

result += this.indent;
if (node.catchClause) {
result += "local __TS_try";
if (node.catchClause.variableDeclaration) {
const variableName = this.transpileIdentifier(
node.catchClause.variableDeclaration.name as ts.Identifier);
result += ", " + variableName;
}
result += " = ";
}

result += "pcall(function()\n";
this.pushIndent();
tryFunc += this.transpileBlock(node.tryBlock);
result += this.transpileBlock(node.tryBlock);
this.popIndent();
tryFunc += "end";
let catchFunc = "function(e)\nend";
if (node.catchClause && node.catchClause.variableDeclaration) {
const variableName = this.transpileIdentifier(node.catchClause.variableDeclaration.name as ts.Identifier);
catchFunc = this.indent + `function(${variableName})\n`;
result += this.indent + "end);\n";

if (node.catchClause) {
result += this.indent + `if not __TS_try then\n`;
this.pushIndent();
catchFunc += this.transpileBlock(node.catchClause.block);
result += this.transpileBlock(node.catchClause.block);
this.popIndent();
catchFunc += "end";
result += this.indent + "end\n";
}
let result = this.indent + `xpcall(${tryFunc},\n${catchFunc})\n`;

if (node.finallyBlock) {
result += this.indent + "do\n";
this.pushIndent();
result += this.transpileBlock(node.finallyBlock);
this.popIndent();
result += this.indent + "end\n";
}

this.popIndent();
result += this.indent + "end\n";
return result;
}

Expand Down
14 changes: 8 additions & 6 deletions test/translation/lua/tryCatch.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
xpcall(function()
local a = 42;
end,
function(er)
local b = "fail";
end)
do
local __TS_try, er = pcall(function()
local a = 42;
end);
if not __TS_try then
local b = "fail";
end
end
18 changes: 11 additions & 7 deletions test/translation/lua/tryCatchFinally.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
xpcall(function()
local a = 42;
end,
function(er)
local b = "fail";
end)
local c = "finally";
do
local __TS_try, er = pcall(function()
local a = 42;
end);
if not __TS_try then
local b = "fail";
end
do
local c = "finally";
end
end
14 changes: 8 additions & 6 deletions test/translation/lua/tryFinally.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
xpcall(function()
local a = 42;
end,
function(e)
end)
local b = "finally";
do
pcall(function()
local a = 42;
end);
do
local b = "finally";
end
end
51 changes: 51 additions & 0 deletions test/unit/error.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,55 @@ export class LuaErrorTests {
);
}).toThrowError(TranspileError, "Invalid throw expression, only strings can be thrown.");
}

@TestCase(0, "A")
@TestCase(1, "B")
@TestCase(2, "C")
@Test("re-throw")
public reThrow(i: number, expected: any): void {
const source =
`const i = ${i};
function foo() {
try {
try {
if (i === 0) { throw "z"; }
} catch (e) {
throw "a";
} finally {
if (i === 1) { throw "b"; }
}
} catch (e) {
throw (e as string).toUpperCase();
} finally {
throw "C";
}
}
let result = "x";
try {
foo();
} catch (e) {
result = (e as string)[(e as string).length - 1];
}
return result;`;
const result = util.transpileAndExecute(source);
Expect(result).toBe(expected);
}

@Test("re-throw (no catch var)")
public reThrowWithoutCatchVar(): void {
const source =
`let result = "x";
try {
try {
throw "y";
} catch {
throw "z";
}
} catch (e) {
result = (e as string)[(e as string).length - 1];
}
return result;`;
const result = util.transpileAndExecute(source);
Expect(result).toBe("z");
}
}