WebAssembly.Instance()
构造函数创建新
Instance
object which is a stateful, executable instance of a
WebAssembly.Module
.
Important
: Since instantiation for large modules can be expensive, developers should only use the
Instance()
constructor when synchronous instantiation is absolutely required; the asynchronous
WebAssembly.instantiateStreaming()
method should be used at all other times.
new WebAssembly.Instance(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.
WebAssembly.Instance()
constructor function can be called to synchronously instantiate a given
WebAssembly.Module
object, for example:
const importObject = {
imports: {
imported_func: function(arg) {
console.log(arg);
}
}
};
fetch('simple.wasm').then(response =>
response.arrayBuffer()
).then(bytes => {
let mod = new WebAssembly.Module(bytes);
let instance = new WebAssembly.Instance(mod, importObject);
instance.exports.exported_func();
})
However, the preferred way to get an
Instance
is through the asynchronous
WebAssembly.instantiateStreaming()
function, for example like this:
const importObject = {
imports: {
imported_func: function(arg) {
console.log(arg);
}
}
};
WebAssembly.instantiateStreaming(fetch('simple.wasm'), importObject)
.then(obj => obj.instance.exports.exported_func());
| 规范 |
|---|
|
WebAssembly JavaScript 接口
The definition of 'Instance' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Instance()
构造函数
|
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.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()