WebAssembly.compileStreaming()

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since October 2017.

* Some parts of this feature may have varying levels of support.

WebAssembly.compileStreaming() 方法用来从一个流式源中直接编译一个 WebAssembly.Module。当模块需要在被实例化前被编译时,这个方法会很有用。如果要从流式源实例化一个模块应采用 WebAssembly.instantiateStreaming() 方法。

语法

Promise<WebAssembly.Module> WebAssembly.compileStreaming(source);

参数

source

一个 Response 对象或一个会履行(fulfill)它的 promise,用来表示你想编译的 .wasm 模块的流式源。

返回值

一个会被解决(resolve)为编译后的 WebAssembly.Module 对象的 Promise

异常

示例

下面的例子(在 GitHub 上查看我们的 compile-streaming.html 示例或者直接在线预览)直接从流式源传输一个 .wasm 模块然后将其编译为一个 WebAssembly.Module 对象。因为 compileStreaming() 方法可以接受一个结果为 Response 对象的 promise,因此你可以直接用 fetch() 的调用结果来调用该方法。

js
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() 方法被实例化了,然后调用模块导出的函数。

规范

Specification
WebAssembly Web API
# dom-webassembly-compilestreaming

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
compileStreaming() static method
compileOptions parameter

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support

参见