WebAssembly

WebAssembly JavaScript object acts as the namespace for all WebAssembly -related functionality.

Unlike most other global objects, WebAssembly is not a constructor (it is not a function object). You can compare it to Math , which is also a namespace object for mathematical constants and functions, or to Intl which is the namespace object for internationalization constructors and other language-sensitive functions.

描述

The primary uses for the WebAssembly object are:

Constructor properties

WebAssembly.CompileError()
Indicates an error during WebAssembly decoding or validation.
WebAssembly.Global()
Represents a global variable instance, accessible from both JavaScript and importable/exportable across one or more WebAssembly.Module instances. This allows dynamic linking of multiple modules.
WebAssembly.Instance()
Is a stateful, executable instance of a WebAssembly.Module
WebAssembly.LinkError()
Indicates an error during module instantiation (besides traps from the start function).
WebAssembly.Memory()
An object whose buffer property is a resizable ArrayBuffer that holds the raw bytes of memory accessed by a WebAssembly Instance .
WebAssembly.Module()
Contains stateless WebAssembly code that has already been compiled by the browser and can be efficiently shared with Workers , and instantiated multiple times.
WebAssembly.RuntimeError()
Error type that is thrown whenever WebAssembly specifies a trap .
WebAssembly.Table()
An array-like structure representing a WebAssembly Table, which stores function references.

静态方法

WebAssembly.instantiate()
The primary API for compiling and instantiating WebAssembly code, returning both a 模块 and its first Instance .
WebAssembly.instantiateStreaming()
Compiles and instantiates a WebAssembly module directly from a streamed underlying source, returning both a 模块 and its first Instance .
WebAssembly.compile()
Compiles a WebAssembly.Module from WebAssembly binary code, leaving instantiation as a separate step.
WebAssembly.compileStreaming()
compiles a WebAssembly.Module directly from a streamed underlying source, leaving instantiation as a separate step.
WebAssembly.validate()
Validates a given typed array of WebAssembly binary code, returning whether the bytes are valid WebAssembly code ( true ) or not ( false ).

范例

Stream a .wasm module then compile and instantiate it

The following example (see our instantiate-streaming.html demo on GitHub, and view it live also) directly streams a .wasm module from an underlying source then compiles and instantiates it, the promise fulfilling with a ResultObject . Because the instantiateStreaming() function accepts a promise for a Response object, you can directly pass it a WindowOrWorkerGlobalScope.fetch() call, and it will pass the response into the function when it fulfills.

var importObject = { imports: { imported_func: arg => console.log(arg) } };
WebAssembly.instantiateStreaming(fetch('simple.wasm'), importObject)
.then(obj => obj.instance.exports.exported_func());
					

ResultObject 's instance member is then accessed, and the contained exported function invoked.

规范

规范
WebAssembly JavaScript Interface
The definition of 'WebAssembly' in that specification.

浏览器兼容性

更新 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
WebAssembly Chrome 57 Edge 16 Firefox 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
IE No Opera 44 Safari 11 WebView Android 57 Chrome Android 57 Firefox Android 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
Opera Android 43 Safari iOS 11 Samsung Internet Android 7.0 nodejs 8.0.0
CompileError Chrome 57 Edge 16 Firefox 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
IE No Opera 44 Safari 11 WebView Android 57 Chrome Android 57 Firefox Android 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
Opera Android 43 Safari iOS 11 Samsung Internet Android 7.0 nodejs 8.0.0
Global Chrome 69 Edge No Firefox 62 IE No Opera No Safari No WebView Android 69 Chrome Android 69 Firefox Android 62 Opera Android No Safari iOS No Samsung Internet Android 10.0 nodejs No
Instance Chrome 57 Edge 16 Firefox 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
IE No Opera 44 Safari 11 WebView Android 57 Chrome Android 57 Firefox Android 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
Opera Android 43 Safari iOS 11 Samsung Internet Android 7.0 nodejs 8.0.0
LinkError Chrome 57 Edge 16 Firefox 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
IE No Opera 44 Safari 11 WebView Android 57 Chrome Android 57 Firefox Android 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
Opera Android 43 Safari iOS 11 Samsung Internet Android 7.0 nodejs 8.0.0
Memory Chrome 57 Edge 16 Firefox 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
IE No Opera 44 Safari 11 WebView Android 57 Chrome Android 57 Firefox Android 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
Opera Android 43 Safari iOS 11 Samsung Internet Android 7.0 nodejs 8.0.0
模块 Chrome 57 Edge 16 Firefox 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
IE No Opera 44 Safari 11 WebView Android 57 Chrome Android 57 Firefox Android 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
Opera Android 43 Safari iOS 11 Samsung Internet Android 7.0 nodejs 8.0.0
RuntimeError Chrome 57 Edge 16 Firefox 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
IE No Opera 44 Safari 11 WebView Android 57 Chrome Android 57 Firefox Android 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
Opera Android 43 Safari iOS 11 Samsung Internet Android 7.0 nodejs 8.0.0
Table Chrome 57 Edge 16 Firefox 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
IE No Opera 44 Safari 11 WebView Android 57 Chrome Android 57 Firefox Android 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
Opera Android 43 Safari iOS 11 Samsung Internet Android 7.0 nodejs 8.0.0
compile Chrome 57 Edge 16 Firefox 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
IE No Opera 44 Safari 11 WebView Android 57 Chrome Android 57 Firefox Android 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
Opera Android 43 Safari iOS 11 Samsung Internet Android 7.0 nodejs 8.0.0
compileStreaming Chrome 61 Edge 16 Firefox 58 IE No Opera 47 Safari No WebView Android 61 Chrome Android 61 Firefox Android 58 Opera Android 45 Safari iOS No Samsung Internet Android 8.0 nodejs No
instantiate Chrome 57 Edge 16 Firefox 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
IE No Opera 44 Safari 11 WebView Android 57 Chrome Android 57 Firefox Android 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
Opera Android 43 Safari iOS 11 Samsung Internet Android 7.0 nodejs 8.0.0
instantiateStreaming Chrome 61 Edge 16 Firefox 58 IE No Opera 47 Safari No WebView Android 61 Chrome Android 61 Firefox Android 58 Opera Android 45 Safari iOS No Samsung Internet Android 8.0 nodejs No
validate Chrome 57 Edge 16 Firefox 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
IE No Opera 44 Safari 11 WebView Android 57 Chrome Android 57 Firefox Android 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
Opera Android 43 Safari iOS 11 Samsung Internet Android 7.0 nodejs 8.0.0

图例

完整支持
完整支持
不支持
不支持
见实现注意事项。

另请参阅

Metadata