Repository navigation
wasm: try/catch and finally, through wasm exception handling - #816
Merged
Merged
Conversation
wasm32/wasm64 (not Emscripten) compile with the exception-handling feature and the wasm exception model, set before the target machine is made (its MCAsmInfo takes it from there; without it every invoke was lowered to a call). WasmExceptionPass turns each Itanium landing pad into a catch-all catchpad left at once (catchswitch, catchpad [ptr null], llvm.wasm.get.exception, catchret), so the code after the pad runs as ordinary code, as it does on Linux; `resume` becomes llvm.wasm.throw. What the personality would decide is decided after the catch: a pad's selector is the first clause that takes the exception (__tslang_wasm_catches: the same type, or a base class's pointer), a pad of typed clauses only rethrows what none takes, and a matched pointer clause binds the thrown pointer. The C++ runtime's type_infos and type_info vtables get definitions, so each has an address of its own. The JS host imports the __cpp_exception tag (i32 or i64, read from the module by host.instantiate), throws a WebAssembly.Exception of it from __cxa_throw, and keeps begin/end catch and rethrow as libc++abi does. It also maps Node's ERR_FS_* codes and refuses an environment name with '='. run_tests.mjs no longer skips tests that catch; json.ts under none is listed (5 GB of strings). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
try/catch and finally now work on wasm32 and wasm64 (not Emscripten), using wasm exception handling. Before this, every throw ended the program, and the test runner skipped every test that catches.
Compiler
obj.cpp): wasm triples get theexception-handlingfeature,-wasm-enable-eh, andExceptionModel = Wasm. The exception model must be set before the target machine is created, because its MCAsmInfo reads it from there. With only the flag, the code generator still lowered every invoke to a plain call.WasmExceptionPass(new, afterLandingPadFixPass): tslang keeps emitting the Itanium form it uses on Linux. The pass turns each landing pad into a catch-all funclet that it leaves straight away:catchswitch→catchpad [ptr null]→llvm.wasm.get.exception→catchretto the old pad block.The handler code after the pad then runs as ordinary code, as it does on Linux. A catch-all pad needs no personality routine, no LSDA and no C++ runtime.
__tslang_wasm_catches(internal, generated) matches the same type, or a class pointer against a pointer to one of its bases, through__si_class_type_info.llvm.eh.typeid.forbecomes the type_info's address.__cxa_begin_catchdoes in libc++abi.resumebecomesllvm.wasm.throw.--allow-undefined, all of them sat at address 0, so every type looked the same. Class type_infos are already emitted as definitions in each module that uses them.Host (
docs/how/wasm/test)__cpp_exceptiontag.host.instantiate(bytes)creates the tag, reading from the module's memory section whether its parameter is i32 or i64.run.mjsandindex.htmlnow instantiate through it.__cxa_throwthrows aWebAssembly.Exceptionof that tag. Begin catch, end catch and rethrow keep handler counts the way libc++abi does. A JS exception (exit(), an assert) is not of this tag, so no catch sees it.ERR_FS_EISDIRfromrmSyncis now reported asEISDIR.=is now refused withEINVAL.run_tests.mjsno longer skips tests that catch.json.tsis skipped under none only: it builds a 100,000-character string one character at a time, which under-mm=noneis about 5 GB of unfreed strings.Default library
No source change, but the wasm archives must be rebuilt with this compiler (
scripts\build_wasm.bat) for exceptions to be caught inside default-library code. I rebuilt them locally for these runs. The release workflow builds them with the compiler it releases.Tests
index.htmlruns a wasm32 and a wasm64 catching test in Chromium (the app's browser pane).🤖 Generated with Claude Code