diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/webassembly/compilestreaming/index.html | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/webassembly/compilestreaming/index.html')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/webassembly/compilestreaming/index.html | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/webassembly/compilestreaming/index.html b/files/zh-cn/web/javascript/reference/global_objects/webassembly/compilestreaming/index.html new file mode 100644 index 0000000000..f36e646699 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/webassembly/compilestreaming/index.html @@ -0,0 +1,77 @@ +--- +title: WebAssembly.compileStreaming() +slug: Web/JavaScript/Reference/Global_Objects/WebAssembly/compileStreaming +translation_of: Web/JavaScript/Reference/Global_Objects/WebAssembly/compileStreaming +--- +<div>{{JSRef}}</div> + +<p><strong><code>WebAssembly.compileStreaming()</code></strong> 方法用来从一个流式源中直接编译一个 {{jsxref("WebAssembly.Module")}}。当模块需要在被实例化前被编译时,这个方法会很有用。如果要从流式源实例化一个模块应采用 {{jsxref("WebAssembly.instantiateStreaming()")}} 方法。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">Promise<WebAssembly.Module> WebAssembly.compileStreaming(<em>source</em>);</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><em>source</em></dt> + <dd>一个 {{domxref("Response")}} 对象或一个会履行(fulfill)它的 promise,用来表示你想编译的 .wasm 模块的流式源。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>一个会被解决(resolve)为编译后的 {{jsxref("WebAssembly.Module")}} 对象的 <code>Promise</code>。</p> + +<h3 id="异常">异常</h3> + +<ul> + <li>如果 <code>bufferSource</code> 不是一个 <a href="/en-US/docs/Web/JavaScript/Typed_arrays">typed array</a>,将会抛出 {{jsxref("TypeError")}} 异常。</li> + <li>如果编译失败,promise 会通过拒绝(reject)来返回一个 {{jsxref("WebAssembly.CompileError")}}。</li> +</ul> + +<h2 id="例子">例子</h2> + +<p>下面的例子(在 GitHub 上查看我们的 <a href="https://github.com/mdn/webassembly-examples/blob/master/js-api-examples/compile-streaming.html">compile-streaming.html</a> 示例或者直接<a href="https://mdn.github.io/webassembly-examples/js-api-examples/compile-streaming.html">在线预览</a>)直接从流式源传输一个 .wasm 模块然后将其编译为一个 {{jsxref("WebAssembly.Module")}} 对象。因为 <code>compileStreaming()</code> 方法可以接受一个结果为 {{domxref("Response")}} 对象的 promise,因此你可以直接用 {{domxref("WindowOrWorkerGlobalScope.fetch()")}} 的调用结果来调用该方法。</p> + +<pre class="brush: 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());</pre> + +<p>得到的 module 实例接下来通过 {{jsxref("WebAssembly.instantiate()")}} 方法被实例化了,然后调用模块导出的函数。</p> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('WebAssembly Embedding', '#webassemblycompilestreaming', 'compileStreaming()')}}</td> + <td>{{Spec2('WebAssembly Embedding')}}</td> + <td>Initial draft definition.</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2> + +<div> + + +<p>{{Compat("javascript.builtins.WebAssembly.compileStreaming")}}</p> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/WebAssembly">WebAssembly</a> overview page</li> + <li><a href="/en-US/docs/WebAssembly/Concepts">WebAssembly concepts</a></li> + <li><a href="/en-US/docs/WebAssembly/Using_the_JavaScript_API">Using the WebAssembly JavaScript API</a></li> +</ul> |