WebAssembly.instantiateStreaming()
function compiles and instantiates a WebAssembly module directly from a streamed underlying source. This is the most efficient, optimized way to load wasm code.
Promise<ResultObject> WebAssembly.instantiateStreaming(source, importObject);
Response
object or a promise that will fulfill with one, representing the underlying source of a .wasm module you want to stream, compile, and instantiate.
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 or shared via
postMessage()
.
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.
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.
注意
: For this to work,
.wasm
files should be returned with an
application/wasm
MIME type by the server.
| 规范 |
|---|
|
WebAssembly features for web embedding
The definition of 'instantiateStreaming()' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
完整支持
不支持
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()