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
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out
https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| 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 | 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 |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- See implementation notes.
- See implementation notes.