这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
WebAssembly.instantiate()
是编译和实例化 WebAssembly 代码的主要方法. 这个方法有两个重载方式:
Promise
会携带已编译的 WebAssembly.Module
和它的第一个实例化对象 WebAssembly.Instance
.WebAssembly.Module
, 返回的 Promise
携带一个 Module
的实例化对象 Instance
. 如果这个 Module
已经被编译了或者是从缓存中获取的( retrieved from cache), 那么这种重载方式是非常有用的.Promise<ResultObject> WebAssembly.instantiate(bufferSource, importObject);
WebAssembly.Memory
对象等等。编译的模块中,对于每一个导入的值都要有一个与其匹配的属性与之相对应,否则将会抛出 WebAssembly.LinkError。解析为包含两个字段的 ResultObject
的一个 Promise
:
module
: 一个被编译好的 WebAssembly.Module
对象. 这个模块可以被再次实例化,通过 postMessage() 被分享,或者缓存到 IndexedDB。instance
: 一个包含所有 Exported WebAssembly functions的WebAssembly.Instance
对象。TypeError
.WebAssembly.CompileError
,WebAssembly.LinkError
, 或WebAssembly.RuntimeError
。Promise<WebAssembly.Instance> WebAssembly.instantiate(module, importObject);
WebAssembly.Module
对象。WebAssembly.Memory
对象等等。编译的模块中,对于每一个导入的值都要有一个与其匹配的属性与之相对应,否则将会抛出 WebAssembly.LinkError。一个解析为 WebAssembly.Instance
的Promise
对象。
TypeError
。WebAssembly.CompileError
,WebAssembly.LinkError
, 或WebAssembly.RuntimeError
。使用 fetch 获取一些 WebAssembly 二进制代码后,我们使用 WebAssembly.instantiate()
方法编译并实例化模块,在此过程中,导入了一个 Javascript 方法在 WebAssembly 模块中, 接下来我们使用Instance
导出的Exported 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()
);
注: 查看GitHub(在线实例)的 index.html 中一个相似的例子,使用了我们的fetchAndInstantiate()
库函数
下面的例子(查看我们GitHub的 index-compile.html 例子,可在线演示)使用 compile()
方法编译了 simple.wasm 字节码,然后通过 postMessage() 发送给一个线程 worker。
var worker = new Worker("wasm_worker.js");
fetch('simple.wasm').then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.compile(bytes)
).then(mod =>
worker.postMessage(mod)
);
在线程中 (查看 wasm_worker.js
) 我们定义了一个导入对象供模块使用,然后设置了一个事件处理函数来接收主线程发来的模块。当模块被接收到后,我们使用WebAssembly.instantiate()
方法创建一个实例并且调用它从内部导出的函数。
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();
});
};
Specification | Status | Comment |
---|---|---|
WebAssembly JavaScript Interface instantiate() |
Working Draft | Initial draft definition. |
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 | 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 |