| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/JavaScript_interface/JSTag_static | [Back] [Original] |
Get to know MDN better
The JSTag read-only static property of the WebAssembly interface is a built-in WebAssembly.Tag representing exceptions thrown in the JavaScript host it allows exceptions thrown in JavaScript to be handled from inside a Wasm module.
(module
(tag $js_tag (import "env" "js_tag") (param externref))
(import "env" "do_work" (func $do_work))
(import "env" "log" (func $log (param externref)))
(func $try_and_catch
(block $handler (result externref)
(try_table (catch $js_tag $handler)
;; Call a JS function that throws
(call $do_work)
)
(return)
)
;; The JS Error object is on the stack as an externref
;; Pass it back to JS for logging
(call $log)
)
(export "try_and_catch" (func $try_and_catch))
)
async function run() {
const { instance } = await WebAssembly.instantiateStreaming(
fetch("{%wasm-url%}"),
{
env: {
js_tag: WebAssembly.JSTag,
// This JS function throws, which Wasm will catch via JSTag
do_work() {
throw new Error("An exception was thrown in JS");
},
log(error) {
// errRef is the JS Error object passed back as an externref
console.log(error.message);
},
},
},
);
instance.exports.try_and_catch();
}
run();
A WebAssembly.Tag with a type of externref ((tag (param externref))).
| Specification |
|---|
| WebAssembly JavaScript Interface # dom-webassembly-jstag |
WebAssembly.TagThis page was last modified on Jul 8, 2026 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |