[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-CN/docs/WebAssembly/Reference/JavaScript_interface/Exception [Back]  [Original]

WebAssembly.Exception - WebAssembly | MDN

MDN Web Docs

View in English Always switch to English

WebAssembly.Exception

*

20225

*

WebAssembly.Exception WebAssembly JavaScript JavaScript WebAssembly

WebAssembly.Tag options Exception

JavaScript WebAssembly

WebAssembly WebAssembly Javascript options.traceStack=true stack

WebAssembly.Exception()

WebAssembly.Exception

Exception.prototype.is()

Exception.prototype.getArg()

Exception.prototype.stack

undefined

JavaScript

WebAssembly example.wasm

  • $tagname i32 tag extmod exttag
  • $throwException throw $tagname
  • 42 run()
wat
(module
  ;;  $tagname
  (import "extmod" "exttag" (tag $tagname (param i32)))

  ;; $throwException  i32  $tagname 
  (func $throwException (param $errorValueArg i32)
    local.get $errorValueArg
    throw $tagname
  )

  ;;  $throwException run
  (func (export "run")
    i32.const 42
    call $throwException
  )
)

WebAssembly.instantiateStreaming example.wasm importObject tagToImport WebAssembly.Tag WebAssembly import

WebAssembly run

js
const tagToImport = new WebAssembly.Tag({ parameters: ["i32"] });

//  WebAssembly import 
const importObject = {
  extmod: {
    exttag: tagToImport,
  },
};

WebAssembly.instantiateStreaming(fetch("example.wasm"), importObject)
  .then((obj) => {
    console.log(obj.instance.exports.run());
  })
  .catch((e) => {
    console.error(e);
    // 
    //  getArg() 
    if (e.is(tagToImport)) {
      console.log(`getArg 0 : ${e.getArg(tagToImport, 0)}`);
    }
  });

/* 
example.js:40 WebAssembly.Exception: wasm exception
example.js:41 getArg 0 : 42
*/

JavaScript catch WebAssembly.Exception

Exception.prototype.is() Exception.prototype.getArg() 42

WebAssembly JavaScript Interface: Exception Handling
# runtime-exceptions


Web Proxy Viewer  |  New URL  |  Original Page