Skip to content
WebAssembly

Debugging

Inspect modules and handle traps.

By EZ4Code Team
debuginspecttrap

Code

// Inspect a module's structure before instantiation
const module = await WebAssembly.compile(bytes);

console.log("Imports:");
for (const imp of WebAssembly.Module.imports(module)) {
  console.log(" ", imp.module, imp.name, imp.kind);
}

console.log("Exports:");
for (const exp of WebAssembly.Module.exports(module)) {
  console.log(" ", exp.name, exp.kind);
}

// Catch runtime traps with try/catch
try {
  instance.exports.riskyFn();
} catch (err) {
  console.error("WASM trap:", err.message);
}

Explanation

WebAssembly.Module.imports and exports list the names and kinds a module needs and provides, which is the first step to wiring imports correctly. Runtime traps such as divide-by-zero or out-of-bounds memory access throw JS exceptions that can be caught with try/catch. Compiling with debug names preserves function names for stack traces.

More WebAssembly Snippets