WebAssembly.validate()
方法用于验证包含 WebAssembly 二进制码的一个 typed array 是否合法,返回 true
如果这些字节能构成一个合法的 wasm 模块,否则返回 false
。
WebAssembly.validate(bufferSource);
bufferSource
一个布尔值,用来表示给定的 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");
});
Specification | Status | Comment |
---|---|---|
WebAssembly JavaScript Interface validate() |
Working Draft | Initial draft definition. |
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
validate | Chrome Full support 57 | Edge Full support 16 | Firefox
Full support
52
| IE No support No | Opera Full support 44 | Safari Full support 11 | WebView Android Full support 57 | Chrome Android Full support 57 | Edge Mobile
Full support
Yes
| Firefox Android
Full support
52
| Opera Android ? | Safari iOS Full support 11 | Samsung Internet Android Full support 7.0 | nodejs Full support 8.0.0 |