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 October 2017.

* 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

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
validate() static method
compileOptions parameter

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support

参见