Skip to content

fix sync try/catch re-throw and return handling#1726

Open
pilaoda wants to merge 4 commits into
TypeScriptToLua:masterfrom
pilaoda:fix/sync-try-rethrow
Open

fix sync try/catch re-throw and return handling#1726
pilaoda wants to merge 4 commits into
TypeScriptToLua:masterfrom
pilaoda:fix/sync-try-rethrow

Conversation

@pilaoda
Copy link
Copy Markdown
Contributor

@pilaoda pilaoda commented May 28, 2026

Summary

The previous sync try/catch implementation had a fundamental issue: the catch block was not wrapped in pcall, so if catch itself threw an error, it would bypass the finally block and propagate as an unhandled Lua error instead of being re-thrown after finally completes — diverging from JavaScript semantics.

Fixing this required rewriting the sync transformTryStatement to:

  • Always capture 3 return values (____trySuccess, ____hasReturnOrError, ____returnValue) from pcall
  • Wrap the catch block in its own pcall so catch errors are captured
  • Use if ____trySuccess / elseif ____catchSuccess / else error() post-finally decision structure

This unified architecture also simplifies adding break/continue support inside try/catch (see follow-up PR).

…ly handle returns and re-throws

(test):
- enable previously skipped re-throw tests
- add tests for complex try-catch-finally return and throw scenarios
Copilot AI review requested due to automatic review settings May 28, 2026 13:33
pilaoda and others added 3 commits May 28, 2026 21:48
Extract duplicated return-if-has-return-or-error into a module-level
helper. Expand the elseBranch ternary into an explicit if/else with
comments for each case (non-empty catch, no catch, empty catch).

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant