WebAssembly.instantiate()
function allows you to compile and instantiate WebAssembly code. This function has two overloads:
ArrayBuffer
, and performs both compilation and instantiation in one step. The returned
Promise
resolves to both a compiled
WebAssembly.Module
and its first
WebAssembly.Instance
.
WebAssembly.Module
and returns a
Promise
that resolves to an
Instance
of that
Module
. This overload is useful if the
Module
has already been compiled.
Important
: This method is not the most efficient way of fetching and instantiating wasm modules. If at all possible, you should use the newer
WebAssembly.instantiateStreaming()
method instead, which fetches, compiles, and instantiates a module all in one step, directly from the raw bytecode, so doesn't require conversion to an
ArrayBuffer
.
Promise<ResultObject> WebAssembly.instantiate(bufferSource, importObject);
ArrayBuffer
containing the binary code of the .wasm module you want to compile.
Instance
, such as functions or
WebAssembly.Memory
objects. There must be one matching property for each declared import of the compiled module or else a
WebAssembly.LinkError
is thrown.
A
Promise
解析为
ResultObject
which contains two fields:
module
: A
WebAssembly.Module
object representing the compiled WebAssembly module. This
Module
can be instantiated again, shared via
postMessage()
or
cached in IndexedDB
.
instance
: A
WebAssembly.Instance
object that contains all the
Exported WebAssembly functions
.
TypeError
is thrown.
WebAssembly.CompileError
,
WebAssembly.LinkError
,或
WebAssembly.RuntimeError
, depending on the cause of the failure.
Promise<WebAssembly.Instance> WebAssembly.instantiate(module, importObject);
WebAssembly.Module
object to be instantiated.
Instance
, such as functions or
WebAssembly.Memory
objects. There must be one matching property for each declared import of
module
or else a
WebAssembly.LinkError
is thrown.
A
Promise
that resolves to an
WebAssembly.Instance
对象。
TypeError
is thrown.
WebAssembly.CompileError
,
WebAssembly.LinkError
,或
WebAssembly.RuntimeError
, depending on the cause of the failure.
注意
: You'll probably want to use
WebAssembly.instantiateStreaming()
in most cases, as it is more efficient than
instantiate()
.
After fetching some WebAssembly bytecode using fetch, we compile and instantiate the module using the
WebAssembly.instantiate()
function, importing a JavaScript function into the WebAssembly Module in the process. We then call an
Exported WebAssembly function
that is exported by the
Instance
.
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()
);
注意 : You can also find this example at index.html on GitHub ( view it live also ).
The following example (see our
index-compile.html
demo on GitHub, and
view it live
also) compiles the loaded simple.wasm byte code using the
WebAssembly.compileStreaming()
method and then sends it to a
worker
使用
postMessage()
.
var worker = new Worker("wasm_worker.js");
WebAssembly.compileStreaming(fetch('simple.wasm'))
.then(mod =>
worker.postMessage(mod)
);
In the worker (see
wasm_worker.js
) we define an import object for the module to use, then set up an event handler to receive the module from the main thread. When the module is received, we create an instance from it using the
WebAssembly.instantiate()
method and invoke an exported function from inside it.
var importObject = {
imports: {
imported_func: function(arg) {
console.log(arg);
}
}
};
onmessage = function(e) {
console.log('module received from main thread');
var mod = e.data;
WebAssembly.instantiate(mod, importObject).then(function(instance) {
instance.exports.exported_func();
});
};
| 规范 |
|---|
|
WebAssembly JavaScript 接口
The definition of 'instantiate()' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
instantiate
|
Chrome 57 | Edge 16 |
Firefox
52
|
IE No | Opera 44 | Safari 11 | WebView Android 57 | Chrome Android 57 |
Firefox Android
52
|
Opera Android 43 | Safari iOS 11 | Samsung Internet Android 7.0 | nodejs 8.0.0 |
完整支持
不支持
见实现注意事项。
WebAssembly
WebAssembly.compile()
WebAssembly.compileStreaming()
WebAssembly.instantiate()
WebAssembly.instantiateStreaming()
WebAssembly.validate()
WebAssembly.Module
WebAssembly.Global
WebAssembly.Instance
WebAssembly.Memory
WebAssembly.Table
WebAssembly.CompileError
WebAssembly.LinkError
WebAssembly.RuntimeError
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()