百科狗-知识改变命运!

WebAssembly.Exception - JavaScript WebAssembly 对象

乐乐2年前 (2023-11-21)阅读数 26#技术干货
文章标签异常

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 newWebAssembly.Exceptionobject.


Instance methods

Exception.prototype.is()

Tests whether the exception matches a particular tag.

WebAssembly.Exception - JavaScript WebAssembly 对象

Exception.prototype.getArg()

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


Instance properties

Exception.prototype.stackNon-standard

Returns the stack trace for the exception, orundefined.


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$tagnameinternally and that has a singlei32parameter. The tag expects the tag to be passed using moduleextmodand tagexttag.
  • The$throwExceptionfunction throws an exception using thethrowinstruction, taking the$tagnameand the parameter argument.
  • The module exports the functionrun()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 callsWebAssembly.instantiateStreamingto import the example.wasm file, passing in an "import object"(importObject)that includes a newWebAssembly.TagnamedtagToImport. The import object defines an object with properties that match theimportstatement in the WebAssembly code.

Once the file is instantiated, the code calls the exported WebAssemblyrun()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 thecatchblock. We can see it is of typeWebAssembly.Exception, but if we didn't have the right tag we couldn't do much else.

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

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)
{if $zbp->Config('yd1125')->foot}