WebAssembly.instantiateStreaming()

WebAssembly.instantiateStreaming() 方法直接从流式底层源编译和实例化WebAssembly模块。这是加载wasm代码一种非常有效的优化方式。

Syntax

Promise<ResultObject> WebAssembly.instantiateStreaming(source, importObject);

Parameters

source
一个 Response对象 或 一个可以履行(fulfill)一个(Response)的  Promise,表示你想要传输、编译和实例化的 .wasm 模块基础源。
importObject 可选
包含一些想要导入到新创建 Instance中值的对象,例如方法 或  WebAssembly.Memory 对象。每个已编译模块的声明导入必须有一个匹配属性,否则抛出  WebAssembly.LinkError 异常。

Return value

一个 Promise ,通过resolve返回一个包含两个属性的 ResultObject :

Exceptions

Examples

下面的示例 (在GitHub上查看 instantiate-streaming.html 示例, 并且也可 view it live ) 直接从基础源传输一个 .wasm 模块,然后进行编译和实例化, Promise 履行后返回一个 ResultObject. 因为 instantiateStreaming() 方法允许履行后返回Response对象的Promise,你可以直接传递一个 WindowOrWorkerGlobalScope.fetch() 请求,它会在履行后将response传递给方法.

var importObject = { imports: { imported_func: arg => console.log(arg) } };

WebAssembly.instantiateStreaming(fetch('simple.wasm'), importObject)
.then(obj => obj.instance.exports.exported_func());

然后访问ResultObject的实例成员,并调用包含的公开函数。

Specifications

Specification Status Comment
WebAssembly features for web embedding
instantiateStreaming()
Draft Initial draft definition.

Browser compatibility

Update compatibility data on GitHub
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
instantiateStreaming Chrome Full support 61 Edge Full support 16 Firefox Full support 58 IE No support No Opera Full support 47 Safari No support No WebView Android Full support 61 Chrome Android Full support 61 Firefox Android Full support 58 Opera Android ? Safari iOS No support No Samsung Internet Android Full support 8.0 nodejs No support No

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

See also