Investigate custom try-catch support #662
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR aims at investigating ways to at least support basic try-catch without finally. Idea is that whenever an error is
throw
n, it will be remembered in a helper global. Now, if there is a reachable catch block in the same function, the scope will be cleaned and control flow branches there, or if there's not, the function will be terminated forcefully, possibly with a return value of whatever is zero.Each callsite then evaluates the flow of the called function to determine whether the call possibly throws, and if so inserts a check whether an error is present right after the call. This continues until a reachable catch block is found in a calling function that handles the error, moving the error into the catch variable and forgetting about it, or, if it's never caught, leading to an
uncaught error
abort at top-level.The implementation is somewhat fragile currently because the compiler already has to handle a shit ton of cases everywhere, misses a way to catch externally and leaks memory for reasons I still have to investigate, but I thought I share what I have so far.