WebAssembly
JavaScript 对象是所有 WebAssembly 相关功能的命名空间。
和大多数全局对象不一样,WebAssembly
不是一个构造函数(它不是一个函数对象)。它类似于 Intl
对象,Math 对象也是一个命名空间对象,用于保存数学常量和函数;Intl则是用于国际化和其他语言相关函数的命名空间对象。
描述
WebAssembly
对象主要用于:
- 使用
WebAssembly.instantiate()
函数加载 WebAssembly 代码。 - 通过
WebAssembly.Table()
构造函数创建新的内存和表实例。 - 由
WebAssembly.RuntimeError()
构造函数来提供 WebAssembly 中的错误信息。
方法
-
WebAssembly.instantiate()
- 用于编译和实例化 WebAssembly 代码的主 API,返回一个 Module 和它的第一个实例。
-
WebAssembly.instantiateStreaming()
-
直接从流式底层源编译和实例化WebAssembly模块,同时返回
Module
及其第一个实例。 -
WebAssembly.compile()
-
把 WebAssembly 二进制代码编译为一个
WebAssembly.Module
,不进行实例化。 -
WebAssembly.compileStreaming()
-
直接从流式底层源代码编译
WebAssembly.Module
,将实例化作为一个单独的步骤。 -
WebAssembly.validate()
- 校验 WebAssembly 二进制代码的类型数组是否合法,合法则返回 true ,否则返回 false 。
构造器
-
WebAssembly.Module()
- 创建一个新的WebAssembly模块对象。
-
WebAssembly.Instance()
- 创建一个新的WebAssembly实例对象。
-
WebAssembly.Memory()
- 创建一个新的WebAssembly内存对象。
-
WebAssembly.Table()
- 创建一个新的WebAssembly表格对象。
-
WebAssembly.CompileError()
- 创建一个新的WebAssembly编译错误对象。
-
WebAssembly.LinkError()
- 创建一个新的WebAssembly链接错误对象。
-
WebAssembly.RuntimeError()
- 创建一个新的WebAssembly运行时错误对象。
示例
通过fetch方法获取了WebAssembly字节码后,我们通过WebAssembly.instantiate()
方法对模块进行编译和实例化,在这过程中将一个JavaScript 函数导入WebAssembly模块。这个promise实例解析成一个对象(result),result包含编译了的Module
和Instance
对象。接下来我们调用一个Instance导出的WebAssembly方法。
var importObject = { imports: { imported_func: function(arg) { console.log(arg); } } }; fetch('simple.wasm').then(response => response.arrayBuffer() ).then(bytes => WebAssembly.instantiate(bytes, importObject) ).then(result => result.instance.exports.exported_func() );
注意:有关使用了我们 fetchAndInstantiate()
库函数的示例,请查看Github上的 index.html (或者查看在线示例) 。
规范
Specification | Status | Comment |
---|---|---|
WebAssembly JavaScript Interface WebAssembly |
Working Draft | 初始草案定义 |
浏览器兼容性
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 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
WebAssembly |
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 Full support Yes | Safari iOS Full support 11 | Samsung Internet Android Full support 7.0 | nodejs Full support 8.0.0 |
CompileError |
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 |
Global |
Chrome Full support 69 | Edge No support No | Firefox Full support 62 | IE No support No | Opera No support No | Safari No support No | WebView Android Full support 69 | Chrome Android Full support 69 | Firefox Android Full support 62 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android Full support 10.0 | nodejs No support No |
Instance |
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 |
LinkError |
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 |
Memory |
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 |
Module |
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 |
RuntimeError |
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 |
Table |
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 |
compile |
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 Full support 43 | Safari iOS Full support 11 | Samsung Internet Android Full support 7.0 | nodejs Full support 8.0.0 |
compileStreaming |
Chrome Full support 61 | Edge Full support 16 | Firefox Full support 58 | IE No support No | Opera Full support 47 | Safari No support No | WebView Android Full support 61 | Chrome Android Full support 61 | Firefox Android Full support 58 | Opera Android ? | Safari iOS No support No | Samsung Internet Android Full support 8.0 | nodejs No support No |
instantiate |
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 |
instantiateStreaming |
Chrome Full support 61 | Edge Full support 16 | Firefox Full support 58 | IE No support No | Opera Full support 47 | Safari No support No | WebView Android Full support 61 | Chrome Android Full support 61 | Firefox Android Full support 58 | Opera Android ? | Safari iOS No support No | Samsung Internet Android Full support 8.0 | nodejs No support No |
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.