• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • WebAssembly.Exception

    WebAssembly.Exception对象表示从 WebAssembly 抛出到 JavaScript 的运行时异常,或从 JavaScript 抛出到 WebAssembly 异常处理程序。

    构造函数接受WebAssembly.Tag、值数组和options对象作为参数。标记唯一地定义了异常的type,包括其参数的顺序及其数据类型。访问抛出的异常的参数需要与创建Exception时使用的标记相同的标记。提供了方法来测试异常是否与特定标记匹配,以及通过索引获取特定值(如果异常与指定标记匹配)。

    当关联的标记被共享时,JavaScript和其他客户端代码只能访问WebAssembly异常值,反之亦然(不能只使用另一个恰好定义相同数据类型的标记)。如果没有匹配的标记,异常可以被捕获并重新抛出,但不能被检查。

    为了更快地抛出异常,从 WebAssembly 抛出的异常通常不包含堆栈跟踪。需要提供堆栈跟踪的 WebAssembly 代码必须调用 JavaScript 函数来创建异常,并在构造函数中传递options.traceStack=true参数。然后,构造函数可能会返回一个异常,并将堆栈跟踪附加到stack属性。

    注意:此功能在Web Workers中可用。


    Constructor

    WebAssembly.Exception()

    Creates a new WebAssembly.Exception object.


    Instance methods

    Exception.prototype.is()

    Tests whether the exception matches a particular tag.

    Exception.prototype.getArg()

    Returns the data fields of an exception that matches a specified tag.


    Instance properties

    Exception.prototype.stack Non-standard

    Returns the stack trace for the exception, or undefined.


    Examples

    This example shows how to define a tag and import it into a module, then use it to throw an exception that is caught in JavaScript.

    Consider the following WebAssembly code, which is assumed to be compiled to a file example.wasm.

    • The module imports a tag that is referred to as $tagname internally and that has a single i32 parameter. The tag expects the tag to be passed using module extmod and tag exttag.
    • The $throwException function throws an exception using the throw instruction, taking the $tagname and the parameter argument.
    • The module exports the function run() that throws an exception with the value "42".
    (module
      ;; import tag that will be referred to here as $tagname
      (import "extmod" "exttag" (tag $tagname (param i32)))
    
      ;; $throwException function throws i32 param as a $tagname exception
      (func $throwException (param $errorValueArg i32)
        local.get $errorValueArg
        throw $tagname
      )
    
      ;; Exported function "run" that calls $throwException
      (func (export "run")
        i32.const 42
        call $throwException
      )
    )
    

    The code below calls WebAssembly.instantiateStreaming to import the example.wasm file, passing in an "import object"(importObject)that includes a new WebAssembly.Tag named tagToImport. The import object defines an object with properties that match the import statement in the WebAssembly code.

    Once the file is instantiated, the code calls the exported WebAssembly run() method, which will immediately throw an exception.

    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
    */
    

    The exception is caught in JavaScript using the catch block. We can see it is of type WebAssembly.Exception, but if we didn't have the right tag we couldn't do much else.

    However, because we have a tag, we use Exception.prototype.is() to check that it's the right one, and because it is correct, we call Exception.prototype.getArg() to read the value of "42".

    上篇:WebAssembly.Tag