const myErrorTag = new WebAssembly.Tag({ parameters: ["i32"] });WebAssembly.Exception / notebook
WebAssembly.Exception
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const env = {
my_error: myErrorTag,
};
WebAssembly.instantiateStreaming(fetch("module.wasm"), { env }).then(/* ... */);WebAssembly.instantiateStreaming(fetch("module.wasm"), { env }).then(
(result) => {
try {
// Cause function to throw
result.instance.exports.throw(-1);
} catch (e) {
if (e instanceof WebAssembly.Exception && e.is(myErrorTag)) {
const errorCode = e.getArg(myErrorTag, 0); // 0 = first payload value
console.log("Error code:", errorCode); // 42
} else {
throw e; // throw other errors
}
}
},
);const tagToImport = new WebAssembly.Tag({ parameters: ["i32"] });
// Note: import object properties match the WebAssembly import statement!
const importObject = {
extmod: {
exttag: tagToImport,
},
};
WebAssembly.instantiateStreaming(fetch("example.wasm"), importObject)
.then((obj) => {
console.log(obj.instance.exports.run());
})
.catch((e) => {
console.error(e);
// Check we have the right tag for the exception
// If so, use getArg() to inspect it
if (e.is(tagToImport)) {
console.log(`getArg 0 : ${e.getArg(tagToImport, 0)}`);
}
});
/* Log output
example.js:40 WebAssembly.Exception: wasm exception
example.js:41 getArg 0 : 42
*/