Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Technical review: Document Wasm exception handling#44025

Merged
chrisdavidmills merged 29 commits into
mdn:mainfrom
chrisdavidmills:wasm-exception-handling
May 22, 2026
Merged

Technical review: Document Wasm exception handling#44025
chrisdavidmills merged 29 commits into
mdn:mainfrom
chrisdavidmills:wasm-exception-handling

Conversation

@chrisdavidmills
Copy link
Copy Markdown
Contributor

@chrisdavidmills chrisdavidmills commented May 6, 2026

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:

  • tag definition
  • exnref type
  • throw instruction
  • throw_ref instruction
  • try_table instruction
  • catch instruction
  • catch_all instruction
  • catch_ref instruction
  • catch_ref_all instruction

I am also going to check whether the following, already existing, ref docs need any updates:

  • WebAssembly.Tag and members
  • WebAssembly.Exception and members

I have also added a page for WebAssembly.JSTag

Motivation

Additional details

Related issues and pull requests

@chrisdavidmills chrisdavidmills requested a review from a team as a code owner May 6, 2026 15:13
@chrisdavidmills chrisdavidmills requested review from hamishwillee and removed request for a team May 6, 2026 15:13
@chrisdavidmills chrisdavidmills marked this pull request as draft May 6, 2026 15:13
@github-actions github-actions Bot added Content:wasm WebAssembly docs size/m [PR only] 51-500 LoC changed labels May 6, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 6, 2026

Preview URLs (30 pages)

Flaws (1)

Note! 29 documents with no flaws that don't need to be listed. 🎉

Found an unexpected or unresolvable flaw? Please report it here.

URL: /en-US/docs/WebAssembly/Reference/JavaScript_interface/Tag
Title: WebAssembly.Tag
Flaw count: 1

  • broken_links:
    • Link /en-US/docs/WebAssembly/Reference/Types/exnref doesn't resolve

(comment last updated: 2026-05-22 18:51:07)

@github-actions github-actions Bot added size/l [PR only] 501-1000 LoC changed and removed size/m [PR only] 51-500 LoC changed labels May 13, 2026
@github-actions github-actions Bot added size/xl [PR only] >1000 LoC changed and removed size/l [PR only] 501-1000 LoC changed labels May 14, 2026
@chrisdavidmills chrisdavidmills marked this pull request as ready for review May 14, 2026 18:47
@chrisdavidmills chrisdavidmills requested a review from a team as a code owner May 14, 2026 18:47
@chrisdavidmills chrisdavidmills requested review from pepelsbey and removed request for a team May 14, 2026 18:47
@chrisdavidmills chrisdavidmills changed the title Document Wasm exception handling Technical review: Document Wasm exception handling May 14, 2026
Copy link
Copy Markdown
Contributor

@eqrion eqrion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 externref value in exception is caught, not a WebAssembly.Exception object.

[1] https://www.w3.org/TR/wasm-js-api-2/#dom-webassembly-jstag

Comment thread files/en-us/webassembly/reference/exception_handling/throw/index.md Outdated
Comment thread files/en-us/webassembly/reference/exception_handling/try_table/index.md Outdated
Comment thread files/en-us/webassembly/reference/exception_handling/try_table/index.md Outdated
Comment thread files/en-us/webassembly/reference/exception_handling/try_table/index.md Outdated
Comment thread files/en-us/webassembly/reference/exception_handling/catch_all/index.md Outdated
Comment thread files/en-us/webassembly/reference/javascript_interface/tag/tag/index.md Outdated
Comment thread files/en-us/webassembly/reference/definitions/tag/index.md
Comment on lines +76 to +80
- : 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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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*
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread files/en-us/webassembly/reference/exception_handling/try_table/index.md Outdated
@chrisdavidmills chrisdavidmills requested a review from eqrion May 16, 2026 18:56
@chrisdavidmills
Copy link
Copy Markdown
Contributor Author

@caugner @LeoMcA the WebAssembly.JSTag page I've added in this PR is not showing up in the generated sidebar, at least, not in the local environment. What can I do about this? Should I file a bug on the fred repo?

@caugner
Copy link
Copy Markdown
Collaborator

caugner commented May 16, 2026

@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.

@chrisdavidmills
Copy link
Copy Markdown
Contributor Author

@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
Copy link
Copy Markdown
Contributor Author

I'll file a rari issue about it.

mdn/rari#703

@eqrion
Copy link
Copy Markdown
Contributor

eqrion commented May 18, 2026

I've also added one-line definitions of the basic numeric types to the Types page, so that they are represented. I don't think they need their own pages, but let me know if you think differently.

@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.

@chrisdavidmills
Copy link
Copy Markdown
Contributor Author

I've also added one-line definitions of the basic numeric types to the Types page, so that they are represented. I don't think they need their own pages, but let me know if you think differently.

@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.

@chrisdavidmills chrisdavidmills requested a review from a team as a code owner May 19, 2026 20:59
@chrisdavidmills chrisdavidmills requested review from caugner and removed request for a team and eqrion May 19, 2026 20:59
@github-actions github-actions Bot added the system [PR only] Infrastructure and configuration for the project label May 19, 2026
Copy link
Copy Markdown
Collaborator

@caugner caugner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Front matter config change LGTM.

@github-actions github-actions Bot added the merge conflicts 🚧 [PR only] label May 21, 2026
@github-actions
Copy link
Copy Markdown
Contributor

This pull request has merge conflicts that must be resolved before it can be merged.

@chrisdavidmills
Copy link
Copy Markdown
Contributor Author

@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.

@caugner
Copy link
Copy Markdown
Collaborator

caugner commented May 21, 2026

@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.

It looks like #44181 removed the WebAssembly/Reference/Types page with a redirect to WebAssembly/Reference/Value_types, but this PR here updated that page.

To resolve the conflict, you'll want to remove that page in your branch as well, and potentially replicate your changes to WebAssembly/Reference/Value_types.

No matter what I do, it keeps telling me my local branch is up to date.

That sounds like your main is not up to date with upstream (although your fork's main is up to date). So if I were you I would switch to your local main, and run git pull, then switch back to this branch (shortcut: git checkout -), then try git merge main again.

Copy link
Copy Markdown
Contributor

@eqrion eqrion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the note. I've also updated all the value type page titles from "Wasm type" to "Wasm value type".

@github-actions github-actions Bot removed the merge conflicts 🚧 [PR only] label May 22, 2026
@chrisdavidmills
Copy link
Copy Markdown
Contributor Author

@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.

It looks like #44181 removed the WebAssembly/Reference/Types page with a redirect to WebAssembly/Reference/Value_types, but this PR here updated that page.

To resolve the conflict, you'll want to remove that page in your branch as well, and potentially replicate your changes to WebAssembly/Reference/Value_types.

No matter what I do, it keeps telling me my local branch is up to date.

That sounds like your main is not up to date with upstream (although your fork's main is up to date). So if I were you I would switch to your local main, and run git pull, then switch back to this branch (shortcut: git checkout -), then try git merge main again.

Cool, thanks @caugner! I think I got it sorted ;-)

@chrisdavidmills chrisdavidmills merged commit 48b0dc4 into mdn:main May 22, 2026
11 checks passed
@chrisdavidmills chrisdavidmills deleted the wasm-exception-handling branch May 22, 2026 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:wasm WebAssembly docs size/xl [PR only] >1000 LoC changed system [PR only] Infrastructure and configuration for the project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants