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
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
validateChrome Full support 57Edge Full support 16Firefox Full support 52
Notes
Full support 52
Notes
Notes Disabled in the Firefox 52 Extended Support Release (ESR).
IE No support NoOpera Full support 44Safari Full support 11WebView Android Full support 57Chrome Android Full support 57Edge Mobile Full support Yes
Disabled
Full support Yes
Disabled
Disabled This feature is behind the Experimental JavaScript Features preference.
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 11Samsung Internet Android Full support 7.0nodejs Full support 8.0.0

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

See also