Technical review: Document Wasm exception handling#44025
Conversation
eqrion
left a comment
There was a problem hiding this comment.
This is looking good. There's one missing piece (described below). And a possibly structural change around try_table.
One missing piece is the WebAssembly.JSTag global [1]. It's a special WebAssembly.Tag on the WebAssembly namespace. The type is (tag (param externref))
- Any thrown JS value that is caught by wasm is automatically wrapped in an exception with the JSTag. This allows wasm to import the JSTag and then catch JS exceptions.
- If wasm throws an exception with JSTag and JS catches it, then the wrapped
externrefvalue in exception is caught, not aWebAssembly.Exceptionobject.
[1] https://www.w3.org/TR/wasm-js-api-2/#dom-webassembly-jstag
| - : One or more `catch` clauses, each representing criteria for catching exceptions, and specifying a [`block`](/en-US/docs/WebAssembly/Reference/Control_flow/block) to branch to as a result. Each clause can be one of the following instructions: | ||
| - [`catch`](/en-US/docs/WebAssembly/Reference/Exception_handling/catch) | ||
| - [`catch_all`](/en-US/docs/WebAssembly/Reference/Exception_handling/catch_all) | ||
| - [`catch_ref`](/en-US/docs/WebAssembly/Reference/Exception_handling/catch_ref) | ||
| - [`catch_all_ref`](/en-US/docs/WebAssembly/Reference/Exception_handling/catch_all_ref) |
There was a problem hiding this comment.
Technically these are not actually instructions. They can only ever be used as part of a try_table instruction, and so they're essentially a part of the syntax of the instruction.
Is it possible to merge them into this page?
There was a problem hiding this comment.
This makes sense, although I wonder whether this might make the page a bit long and unwieldy. What do you think of the idea of making them subpages of the try_table page?
If they are not instructions, what should I refer to them as?
There was a problem hiding this comment.
Sub pages makes sense to me. You could maybe refer to them as catch clauses? I think the technical term is 'instruction immediate' because it's an inherent part of the try_table instruction (sort of like how the 32 is part of i32.const 32, or $label in br $label). But catch clause is the more common term.
There was a problem hiding this comment.
@eqrion OK, I've restructured the pages so that the catch clauses are now subpages of try_table, and are referred to as clauses everywhere, not instructions.
| ## Syntax | ||
|
|
||
| ```plain | ||
| try_table catch* instruction* |
There was a problem hiding this comment.
| try_table catch* instruction* | |
| try_table blocktype? catch* instruction* |
A try_table can have a blocktype which specifies what params and results it takes (all wasm blocks block loop if are like this)
So you could have:
;; push an i32
i32.const 42
;; pops an i32 as the param
try_table (param i32)
;; the single i32 const 42 is still on the stack
end
Or:
try_table (result i32)
;; push an i32
i32.const 42
;; the end of the block pops the results
end
;; the result i32 is now available to be used here
Or any combination.
There was a problem hiding this comment.
Cool, I missed that. I've had a go at adding the details, including a syntax addition, and a section covering it in the description. I'm guessing some more details might be needed, but let me know what you think.
I also wonder if we should provide an example in the Examples section.
…ex.md Co-authored-by: Ryan Hunt <[email protected]>
|
@chrisdavidmills The sidebar is defined in https://github.com/mdn/content/blob/main/files/sidebars/webassemblysidebar.yaml, but if there's a section that should cover this, then don't hesitate to file a Rari issue. |
@caugner yeah, I'm pretty sure that the page in question should be picked up and included in the sidebar section generated by https://github.com/mdn/content/blob/main/files/sidebars/webassemblysidebar.yaml#L47, but it doesn't. I'll file a rari issue about it. |
|
@chrisdavidmills I haven't had a chance to look at the full PR again. But on Friday I quickly whipped up some pages for the primitive value types and function types (#44181). I think it'd be a good idea to have them documented. |
Your PR is looking great; I've provided a review. We should get that one merged before we go too much further on this PR, but I'll at least get the catch clauses reorganization done now. |
caugner
left a comment
There was a problem hiding this comment.
Front matter config change LGTM.
|
This pull request has merge conflicts that must be resolved before it can be merged. |
…xception-handling
|
@caugner can I get some help with this set of merge conflicts? I'm unable to pull them to my local branch to resolve them. No matter what I do, it keeps telling me my local branch is up to date. |
It looks like #44181 removed the To resolve the conflict, you'll want to remove that page in your branch as well, and potentially replicate your changes to
That sounds like your |
eqrion
left a comment
There was a problem hiding this comment.
LGTM from a technical perspective.
| The `exnref` type represents a thrown exception in a Wasm module. This value type is returned by the [`catch_ref`](/en-US/docs/WebAssembly/Reference/Exception_handling/try_table/catch_ref) and [`catch_all_ref`](/en-US/docs/WebAssembly/Reference/Exception_handling/try_table/catch_all_ref) clauses, providing a reference to the exception that was just thrown, and allowing it to be rethrown if required using the [`throw_ref`](/en-US/docs/WebAssembly/Reference/Exception_handling/throw_ref) instruction. | ||
|
|
||
| The [`WebAssembly.Exception`](/en-US/docs/WebAssembly/Reference/JavaScript_interface/Exception) JavaScript interface represents a Wasm exception in the JavaScript host. | ||
|
|
There was a problem hiding this comment.
One extra thing worth noting is that you are not permitted to call a wasm function from JS that has 'exnref' as a parameter or result. This is the same kind of restriction as v128.
There was a problem hiding this comment.
I've added the note. I've also updated all the value type page titles from "Wasm type" to "Wasm value type".
Cool, thanks @caugner! I think I got it sorted ;-) |
Description
This PR adds documentation for "modern" WebAssembly Exception handling. I am not intending to document "legacy" Wasm exception handling alongside this, as it would make the documentation significantly more complex (some features are used in both), and I'm sure we want to encourage devs to use the modern version.
Specifically, this PR documents the following:
tagdefinitionexnreftypethrowinstructionthrow_refinstructiontry_tableinstructioncatchinstructioncatch_allinstructioncatch_refinstructioncatch_ref_allinstructionI am also going to check whether the following, already existing, ref docs need any updates:
WebAssembly.Tagand membersWebAssembly.Exceptionand membersI have also added a page for
WebAssembly.JSTagMotivation
Additional details
Related issues and pull requests