Movatterモバイル変換


[0]ホーム

URL:


  1. WebAssembly
  2. 参考
  3. WebAssembly
  4. WebAssembly.validate()

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in EnglishAlways switch to English

WebAssembly.validate()

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2017年10月.

* Some parts of this feature may have varying levels of support.

WebAssembly.validate() 方法用于验证包含 WebAssembly 二进制码的一个类型化数组是否合法,返回true 如果这些字节能构成一个合法的 wasm 模块,否则返回false

语法

js
WebAssembly.validate(bufferSource)WebAssembly.validate(bufferSource, compileOptions)

参数

bufferSource

一个包含 WebAssembly 二进制码的typed arrayArrayBuffer

返回值

一个布尔值,用来表示给定的bufferSource 是合法 wasm 代码(true)或者不是(false)。

异常

如果给定的bufferSource 不是typed arrayArrayBuffer 类型,将会抛出TypeError 异常。

示例

下面的例子(查看 validate.html源代码,或者在线预览)通过fetch 获取了一个 .wasm 模块并将其转换为一个类型化数组。接下来用validate() 方法来验证这个模块是否合法。

js
fetch("simple.wasm")  .then((response) => response.arrayBuffer())  .then((bytes) => {    const valid = WebAssembly.validate(bytes);    console.log(      `The given bytes are ${valid ? "" : "not "}a valid Wasm module`,    );  });

规范

Specification
WebAssembly JavaScript Interface
# dom-webassembly-validate

浏览器兼容性

参见

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp