百科狗-知识改变命运!
--

WebAssembly.Exception - JavaScript WebAssembly 对象

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

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.

WebAssembly.Exception - JavaScript WebAssembly 对象

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".

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

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