WebAssembly.compileStreaming()
WebAssembly.compileStreaming()
方法用来从一个流式源中直接编译一个WebAssembly.instantiateStreaming()
方法。
语法
Promise<WebAssembly.Module> WebAssembly.compileStreaming(source);
参数
- source
- 一个
Response
对象或一个会履行(fulfill)它的 promise,用来表示你想编译的.wasm 模块的流式源。
返回值
一个会被解决(resolve)为编译后的WebAssembly.Module
对象的Promise
。
异常
- 如果
bufferSource
不是一个 typed array,将会抛出TypeError
异常。 - 如果编译失败,promise 会通过拒绝(reject)来返回一个
WebAssembly.CompileError
。
例子
下面的例子(在 GitHub 上查看我们的 compile-streaming.html 示例或者直接在线预览)直接从流式源传输一个.wasm 模块然后将其编译为一个WindowOrWorkerGlobalScope.fetch()
的调用结果来调用该方法。
var importObject = { imports: { imported_func: arg => console.log(arg) } }; WebAssembly.compileStreaming(fetch('simple.wasm')) .then(module => WebAssembly.instantiate(module, importObject)) .then(instance => instance.exports.exported_func());
得到的 module 实例接下来通过WebAssembly.instantiate()
方法被实例化了,然后调用模块导出的函数。