WebAssembly.validate()

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

语法

WebAssembly.validate(bufferSource);

参数

bufferSource
一个包含 WebAssembly 二进制码的  typed array 或  ArrayBuffer

返回值

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

异常

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

例子

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

fetch('simple.wasm').then(response =>
  response.arrayBuffer()
).then(function(bytes) {
  var valid = WebAssembly.validate(bytes);
  console.log("The given bytes are "
    + (valid ? "" : "not ") + "a valid wasm module");
});

Specifications

Specification Status Comment
WebAssembly JavaScript Interface
validate()
Working Draft Initial draft definition.

Browser compatibility

Update compatibility data on GitHub
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
validate Chrome Full support 57 Edge Full support 16 Firefox Full support 52
Notes
Full support 52
Notes
Notes Disabled in the Firefox 52 Extended Support Release (ESR).
IE No support No Opera Full support 44 Safari Full support 11 WebView Android Full support 57 Chrome Android Full support 57 Firefox Android Full support 52
Notes
Full support 52
Notes
Notes Disabled in the Firefox 52 Extended Support Release (ESR).
Opera Android ? Safari iOS Full support 11 Samsung Internet Android Full support 7.0 nodejs Full support 8.0.0

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.

See also