Skip to content
Open
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
48 changes: 26 additions & 22 deletions src/transformation/visitors/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,39 +39,43 @@ const transformAsyncTry: FunctionVisitor<ts.TryStatement> = (statement, context)
// local ____try = __TS__AsyncAwaiter(<catch block>)
const result: lua.Statement[] = [awaiterDefinition];

if (statement.finallyBlock) {
const awaiterFinally = lua.createTableIndexExpression(awaiterIdentifier, lua.createStringLiteral("finally"));
const finallyFunction = lua.createFunctionExpression(
lua.createBlock(context.transformStatements(statement.finallyBlock.statements))
);
const finallyCall = lua.createCallExpression(
awaiterFinally,
[awaiterIdentifier, finallyFunction],
statement.finallyBlock
);
// ____try.finally(<finally function>)
result.push(lua.createExpressionStatement(finallyCall));
}

if (statement.catchClause) {
// ____try.catch(<catch function>)
// ____try = ____try.catch(<catch function>)
const [catchFunction] = transformCatchClause(context, statement.catchClause);
if (catchFunction.params) {
catchFunction.params.unshift(lua.createAnonymousIdentifier());
}

const catchBodyStatements = catchFunction.body ? catchFunction.body.statements : [];
const asyncWrappedCatch = wrapInAsyncAwaiter(context, [...catchBodyStatements], false);
catchFunction.body = lua.createBlock([lua.createReturnStatement([asyncWrappedCatch])]);

const awaiterCatch = lua.createTableIndexExpression(awaiterIdentifier, lua.createStringLiteral("catch"));
const catchCall = lua.createCallExpression(awaiterCatch, [awaiterIdentifier, catchFunction]);
result.push(lua.createAssignmentStatement(lua.cloneIdentifier(awaiterIdentifier), catchCall));
}

// await ____try.catch(<catch function>)
const promiseAwait = transformLuaLibFunction(context, LuaLibFeature.Await, statement, catchCall);
result.push(lua.createExpressionStatement(promiseAwait, statement));
} else {
// await ____try
const promiseAwait = transformLuaLibFunction(context, LuaLibFeature.Await, statement, awaiterIdentifier);
result.push(lua.createExpressionStatement(promiseAwait, statement));
if (statement.finallyBlock) {
// ____try = ____try.finally(<finally function>)
const finallyStatements = context.transformStatements(statement.finallyBlock.statements);
const asyncWrappedFinally = wrapInAsyncAwaiter(context, finallyStatements, false);
const finallyFunction = lua.createFunctionExpression(
lua.createBlock([lua.createReturnStatement([asyncWrappedFinally])])
);

const awaiterFinally = lua.createTableIndexExpression(awaiterIdentifier, lua.createStringLiteral("finally"));
const finallyCall = lua.createCallExpression(
awaiterFinally,
[awaiterIdentifier, finallyFunction],
statement.finallyBlock
);
result.push(lua.createAssignmentStatement(lua.cloneIdentifier(awaiterIdentifier), finallyCall));
}

// __TS__Await(____try)
const promiseAwait = transformLuaLibFunction(context, LuaLibFeature.Await, statement, awaiterIdentifier);
result.push(lua.createExpressionStatement(promiseAwait, statement));

return result;
};

Expand Down
88 changes: 88 additions & 0 deletions test/unit/builtins/async-await.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -815,4 +815,92 @@ describe("try/catch in async function", () => {
},
});
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1659
test("await inside catch handler resolves correctly (#1659)", () => {
util.testFunction`
let reject: (reason: string) => void = () => {};

async function failing() {
return new Promise((_, rej) => { reject = rej; });
}

async function run() {
try {
await failing();
} catch (e) {
log("catch");
const a = await Promise.resolve(true);
log("a", a);
}
}

run();
reject("error");

return allLogs;
`
.setTsHeader(promiseTestLib)
.expectToEqual(["catch", "a", true]);
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1659
test("await inside finally handler resolves correctly (#1659)", () => {
util.testFunction`
let reject: (reason: string) => void = () => {};

async function failing() {
return new Promise((_, rej) => { reject = rej; });
}

async function run() {
try {
await failing();
} finally {
log("finally");
const a = await Promise.resolve(true);
log("a", a);
}
}

run().catch(() => {});
reject("error");

return allLogs;
`
.setTsHeader(promiseTestLib)
.expectToEqual(["finally", "a", true]);
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1659
test("await inside both catch and finally handlers (#1659)", () => {
util.testFunction`
let reject: (reason: string) => void = () => {};

async function failing() {
return new Promise((_, rej) => { reject = rej; });
}

async function run() {
try {
await failing();
} catch (e) {
log("catch");
const a = await Promise.resolve("caught");
log("a", a);
} finally {
log("finally");
const b = await Promise.resolve("done");
log("b", b);
}
}

run();
reject("error");

return allLogs;
`
.setTsHeader(promiseTestLib)
.expectToEqual(["catch", "a", "caught", "finally", "b", "done"]);
});
});
Loading