← 返回首页
WebAssembly.JSTag - WebAssembly | MDN

WebAssembly.JSTag

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.

In this article

Try it

(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();

Value

A WebAssembly.Tag with a type of externref ((tag (param externref))).

Specifications

Specification
WebAssembly JavaScript Interface
# dom-webassembly-jstag

Browser compatibility

Enable JavaScript to view this browser compatibility table.

See also