2019-04-21 11:50:48 +08:00

188 lines
21 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div><div class="blockIndicator experimental indicator-warning">
<p><span title="这是一个实验性的 API请尽量不要在生产环境中使用它。"><i class="icon-beaker"> </i></span> <strong>这是一个实验中的功能</strong><br/>此功能某些浏览器尚在开发中,请参考<a href="#Browser_compatibility">浏览器兼容性表格</a>以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。</p>
</div></div>
<p><strong><code>WebAssembly.instantiate()</code></strong> 是编译和实例化 WebAssembly 代码的主要方法.  这个方法有两个重载方式:</p>
<ul>
<li>第一种重载使用WebAssembly二进制代码的 <a href="/en-US/docs/Web/JavaScript/Typed_arrays">typed array</a><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer">ArrayBuffer</a> 形式进行编译和实例化.  返回的 <code>Promise</code> 会携带已编译的 <a href="Reference/Global_Objects/WebAssembly/Module" title="所有 Module 实例继承自 Module() 构造函数的原型对象 —— 修改它会影响所有 Module 实例。"><code>WebAssembly.Module</code></a> 和它的第一个实例化对象 <a href="Reference/Global_Objects/WebAssembly/Instance" title="一个 WebAssembly.Instance 对象是有状态的, 是WebAssembly.Module 的一个可执行实例.  实例包含所有的 WebAssembly 导出函数 ,允许从JavaScript 调用 WebAssembly 代码."><code>WebAssembly.Instance</code></a>.</li>
<li>第二种重载使用已编译的 <a href="Reference/Global_Objects/WebAssembly/Module" title="所有 Module 实例继承自 Module() 构造函数的原型对象 —— 修改它会影响所有 Module 实例。"><code>WebAssembly.Module</code></a> , 返回的 <code>Promise</code> 携带一个 <code>Module</code>的实例化对象 <code>Instance</code>. 如果这个 <code>Module</code> 已经被编译了或者是从缓存中获取的( <a href="/en-US/docs/WebAssembly/Caching_modules">retrieved from cache</a>), 那么这种重载方式是非常有用的.</li>
</ul>
<h2 id="语法">语法</h2>
<h3 id="主重载方式_—_使用wasm二进制代码">主重载方式 — 使用wasm二进制代码</h3>
<pre><code class="language-javascript">Promise&lt;ResultObject&gt; WebAssembly.instantiate(bufferSource, importObject);
</code></pre>
<h4 id="参数">参数</h4>
<dl>
<dt><em>bufferSource</em></dt>
<dd>一个包含你想编译的wasm模块二进制代码的 <a href="/en-US/docs/Web/JavaScript/Typed_arrays">typed array</a>(类型数组) or <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer">ArrayBuffer</a>(数组缓冲区)</dd>
<dt><em>importObject</em> <span class="inlineIndicator optional optionalInline">可选</span></dt>
<dd>一个将被导入到新创建实例中的对象,它包含的值有函数、<a href="Reference/Global_Objects/WebAssembly/Memory" title="WebAssembly.Memory() 构造函数创建一个新的 Memory 对象。它包含一个可调整大小的 ArrayBuffer 其内存储的是 WebAssembly 实例 所访问内存的原始字节码。"><code>WebAssembly.Memory</code></a> 对象等等。编译的模块中,对于每一个导入的值都要有一个与其匹配的属性与之相对应,否则将会抛出 <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/LinkError">WebAssembly.LinkError</a></dd>
</dl>
<h4 id="返回值">返回值</h4>
<p>解析为包含两个字段的 <code>ResultObject</code> 的一个 <code>Promise</code>:</p>
<ul>
<li><code>module</code>: 一个被编译好的 <a href="Reference/Global_Objects/WebAssembly/Module" title="所有 Module 实例继承自 Module() 构造函数的原型对象 —— 修改它会影响所有 Module 实例。"><code>WebAssembly.Module</code></a> 对象. 这个模块可以被再次实例化,通过 <a href="/en-US/docs/Web/API/Worker/postMessage">postMessage()</a> 被分享,或者缓存到 <a href="/en-US/docs/WebAssembly/Caching_modules">IndexedDB</a></li>
<li><code>instance</code>: 一个包含所有 <a href="/en-US/docs/WebAssembly/Exported_functions">Exported WebAssembly functions</a><a href="Reference/Global_Objects/WebAssembly/Instance" title="一个 WebAssembly.Instance 对象是有状态的, 是WebAssembly.Module 的一个可执行实例.  实例包含所有的 WebAssembly 导出函数 ,允许从JavaScript 调用 WebAssembly 代码."><code>WebAssembly.Instance</code></a>对象。</li>
</ul>
<h4 id="异常">异常</h4>
<ul>
<li>如果参数的类型或结构不正确,将会抛出异常 <a href="Reference/Global_Objects/TypeError" title="TypeError类型错误 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a> .</li>
<li>如果操作失败promise 将会被 reject 掉, 根据失败的原因不同会抛出3种异常<a class="new" href="Reference/Global_Objects/WebAssembly/CompileError" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!"><code>WebAssembly.CompileError</code></a><a href="Reference/Global_Objects/WebAssembly/LinkError" title="此页面仍未被本地化, 期待您的翻译!"><code>WebAssembly.LinkError</code></a>, 或<a class="new" href="Reference/Global_Objects/WebAssembly/RuntimeError" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!"><code>WebAssembly.RuntimeError</code></a></li>
</ul>
<h3 id="第二种重载_—_使用模块对象">第二种重载 — 使用模块对象</h3>
<pre><code class="language-javascript">Promise&lt;WebAssembly.Instance&gt; WebAssembly.instantiate(module, importObject);
</code></pre>
<h4 id="参数_2">参数</h4>
<dl>
<dt><em>module</em></dt>
<dd>将被实例化的 <a href="Reference/Global_Objects/WebAssembly/Module" title="所有 Module 实例继承自 Module() 构造函数的原型对象 —— 修改它会影响所有 Module 实例。"><code>WebAssembly.Module</code></a> 对象。</dd>
<dt><em>importObject</em> <span class="inlineIndicator optional optionalInline">可选</span></dt>
<dd>一个将被导入到新创建实例中的对象,它包含的值有函数、<a href="Reference/Global_Objects/WebAssembly/Memory" title="WebAssembly.Memory() 构造函数创建一个新的 Memory 对象。它包含一个可调整大小的 ArrayBuffer 其内存储的是 WebAssembly 实例 所访问内存的原始字节码。"><code>WebAssembly.Memory</code></a> 对象等等。编译的模块中,对于每一个导入的值都要有一个与其匹配的属性与之相对应,否则将会抛出 <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/LinkError">WebAssembly.LinkError</a></dd>
</dl>
<h4 id="返回值_2">返回值</h4>
<p>一个解析为 <a href="Reference/Global_Objects/WebAssembly/Instance" title="一个 WebAssembly.Instance 对象是有状态的, 是WebAssembly.Module 的一个可执行实例.  实例包含所有的 WebAssembly 导出函数 ,允许从JavaScript 调用 WebAssembly 代码."><code>WebAssembly.Instance</code></a><code>Promise</code> 对象。</p>
<h4 id="异常_2">异常</h4>
<ul>
<li>如果参数的类型或结构不正确,将抛出异常 <a href="Reference/Global_Objects/TypeError" title="TypeError类型错误 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a></li>
<li>如果操作失败promise 将会被 reject 掉, 根据失败的原因不同会抛出3种异常<a class="new" href="Reference/Global_Objects/WebAssembly/CompileError" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!"><code>WebAssembly.CompileError</code></a><a href="Reference/Global_Objects/WebAssembly/LinkError" title="此页面仍未被本地化, 期待您的翻译!"><code>WebAssembly.LinkError</code></a>, 或<a class="new" href="Reference/Global_Objects/WebAssembly/RuntimeError" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!"><code>WebAssembly.RuntimeError</code></a></li>
</ul>
<h2 id="例子">例子</h2>
<h3 id="第一种重载例子">第一种重载例子</h3>
<p>使用 fetch 获取一些 WebAssembly 二进制代码后,我们使用 <a href="Reference/Global_Objects/WebAssembly/instantiate" title="WebAssembly.instantiate() 是编译和实例化 WebAssembly 代码的主要方法.  这个方法有两个重载方式:"><code>WebAssembly.instantiate()</code></a>  方法编译并实例化模块,在此过程中,导入了一个 Javascript 方法在 WebAssembly 模块中, 接下来我们使用<code>Instance</code> 导出的<a href="/en-US/docs/WebAssembly/Exported_functions">Exported WebAssembly </a>方法。</p>
<pre><code class="language-javascript">var importObject = {
imports: {
imported_func: function(arg) {
console.log(arg);
}
}
};
fetch('simple.wasm').then(response =&gt;
  response.arrayBuffer()
).then(bytes =&gt;
  WebAssembly.instantiate(bytes, importObject)
).then(result =&gt;
  result.instance.exports.exported_func()
);</code></pre>
<div class="note">
<p><strong></strong>: 查看GitHub(<a class="external" href="https://mdn.github.io/webassembly-examples/js-api-examples/" rel="noopener">在线实例</a>)的 <a class="external" href="https://github.com/mdn/webassembly-examples/blob/master/js-api-examples/index.html" rel="noopener">index.html</a> 中一个相似的例子,使用了我们的<code><a class="external" href="https://github.com/mdn/webassembly-examples/blob/master/wasm-utils.js#L1" rel="noopener">fetchAndInstantiate()</a></code>库函数</p>
</div>
<h3 id="第二种重载例子">第二种重载例子</h3>
<p>下面的例子查看我们GitHub的 <a class="external" href="https://github.com/mdn/webassembly-examples/blob/master/js-api-examples/index-compile.html" rel="noopener">index-compile.html</a> 例子,可在线演示)使用 <code>compile()</code> 方法编译了 simple.wasm 字节码,然后通过 <a href="/en-US/docs/Web/API/Worker/postMessage">postMessage()</a> 发送给一个线程 <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API">worker</a></p>
<pre><code class="language-javascript">var worker = new Worker("wasm_worker.js");
fetch('simple.wasm').then(response =&gt;
  response.arrayBuffer()
).then(bytes =&gt;
  WebAssembly.compile(bytes)
).then(mod =&gt;
  worker.postMessage(mod)
);</code></pre>
<p>在线程中 (查看 <code><a class="external" href="https://github.com/mdn/webassembly-examples/blob/master/js-api-examples/wasm_worker.js" rel="noopener">wasm_worker.js</a></code>) 我们定义了一个导入对象供模块使用,然后设置了一个事件处理函数来接收主线程发来的模块。当模块被接收到后,我们使用<a href="Reference/Global_Objects/WebAssembly/instantiate" title="WebAssembly.instantiate() 是编译和实例化 WebAssembly 代码的主要方法.  这个方法有两个重载方式:"><code>WebAssembly.instantiate()</code></a> 方法创建一个实例并且调用它从内部导出的函数。</p>
<pre><code class="language-javascript">var importObject = {
imports: {
imported_func: function(arg) {
console.log(arg);
}
}
};
onmessage = function(e) {
console.log('module received from main thread');
var mod = e.data;
WebAssembly.instantiate(mod, importObject).then(function(instance) {
instance.exports.exported_func();
});
};</code></pre>
<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><a class="external" href="https://webassembly.github.io/spec/js-api/#webassemblyinstantiate" hreflang="en" lang="en" rel="noopener">WebAssembly JavaScript Interface<br/><small lang="zh-CN">instantiate()</small></a></td>
<td><span class="spec-WD">Working Draft</span></td>
<td>Initial draft definition.</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>
<div>
<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>
<p></p><div class="bc-data"><a class="bc-github-link external" href="https://github.com/mdn/browser-compat-data" rel="noopener">Update compatibility data on GitHub</a><table class="bc-table bc-table-js"><thead><tr class="bc-platforms"><td></td><th class="bc-platform-desktop" colspan="6"><span>Desktop</span></th><th class="bc-platform-mobile" colspan="7"><span>Mobile</span></th><th class="bc-platform-server" colspan="1"><span>Server</span></th></tr><tr class="bc-browsers"><td></td><th class="bc-browser-chrome"><span class="bc-head-txt-label bc-head-icon-chrome">Chrome</span></th><th class="bc-browser-edge"><span class="bc-head-txt-label bc-head-icon-edge">Edge</span></th><th class="bc-browser-firefox"><span class="bc-head-txt-label bc-head-icon-firefox">Firefox</span></th><th class="bc-browser-ie"><span class="bc-head-txt-label bc-head-icon-ie">Internet Explorer</span></th><th class="bc-browser-opera"><span class="bc-head-txt-label bc-head-icon-opera">Opera</span></th><th class="bc-browser-safari"><span class="bc-head-txt-label bc-head-icon-safari">Safari</span></th><th class="bc-browser-webview_android"><span class="bc-head-txt-label bc-head-icon-webview_android">Android webview</span></th><th class="bc-browser-chrome_android"><span class="bc-head-txt-label bc-head-icon-chrome_android">Chrome for Android</span></th><th class="bc-browser-edge_mobile"><span class="bc-head-txt-label bc-head-icon-edge_mobile">Edge Mobile</span></th><th class="bc-browser-firefox_android"><span class="bc-head-txt-label bc-head-icon-firefox_android">Firefox for Android</span></th><th class="bc-browser-opera_android"><span class="bc-head-txt-label bc-head-icon-opera_android">Opera for Android</span></th><th class="bc-browser-safari_ios"><span class="bc-head-txt-label bc-head-icon-safari_ios">Safari on iOS</span></th><th class="bc-browser-samsunginternet_android"><span class="bc-head-txt-label bc-head-icon-samsunginternet_android">Samsung Internet</span></th><th class="bc-browser-nodejs"><span class="bc-head-txt-label bc-head-icon-nodejs">Node.js</span></th></tr></thead><tbody><tr><th scope="row"><code>instantiate</code></th><td class="bc-supports-yes bc-browser-chrome"><span class="bc-browser-name">Chrome</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
57</td><td class="bc-supports-yes bc-browser-edge"><span class="bc-browser-name">Edge</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
16</td><td class="bc-supports-yes bc-browser-firefox bc-has-history"><span class="bc-browser-name">Firefox</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
52<div class="bc-icons"><abbr class="only-icon" title="See implementation notes"><span>Notes</span><i class="ic-footnote"></i></abbr> </div><section class="bc-history" id="sect1"><dl><dt class="bc-supports-yes bc-supports"><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
52<div class="bc-icons"><abbr class="only-icon" title="See implementation notes"><span>Notes</span><i class="ic-footnote"></i></abbr> </div></dt><dd><abbr class="only-icon" title="See implementation notes"><span>Notes</span><i class="ic-footnote"></i></abbr> Disabled in the Firefox 52 Extended Support Release (ESR).</dd></dl></section></td><td class="bc-supports-no bc-browser-ie"><span class="bc-browser-name">IE</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
No</td><td class="bc-supports-yes bc-browser-opera"><span class="bc-browser-name">Opera</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
44</td><td class="bc-supports-yes bc-browser-safari"><span class="bc-browser-name">Safari</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
11</td><td class="bc-supports-yes bc-browser-webview_android"><span class="bc-browser-name">WebView Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
57</td><td class="bc-supports-yes bc-browser-chrome_android"><span class="bc-browser-name">Chrome Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
57</td><td class="bc-supports-yes bc-browser-edge_mobile bc-has-history"><span class="bc-browser-name">Edge Mobile</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
Yes<div class="bc-icons"><abbr class="only-icon" title="User must explicitly enable this feature."><span>Disabled</span><i class="ic-disabled"></i></abbr> </div><section class="bc-history" id="sect2"><dl><dt class="bc-supports-yes bc-supports"><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
Yes<div class="bc-icons"><abbr class="only-icon" title="User must explicitly enable this feature."><span>Disabled</span><i class="ic-disabled"></i></abbr> </div></dt><dd><abbr class="only-icon" title="User must explicitly enable this feature."><span>Disabled</span><i class="ic-disabled"></i></abbr> This feature is behind the <code>Experimental JavaScript Features</code> preference.</dd></dl></section></td><td class="bc-supports-yes bc-browser-firefox_android bc-has-history"><span class="bc-browser-name">Firefox Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
52<div class="bc-icons"><abbr class="only-icon" title="See implementation notes"><span>Notes</span><i class="ic-footnote"></i></abbr> </div><section class="bc-history" id="sect3"><dl><dt class="bc-supports-yes bc-supports"><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
52<div class="bc-icons"><abbr class="only-icon" title="See implementation notes"><span>Notes</span><i class="ic-footnote"></i></abbr> </div></dt><dd><abbr class="only-icon" title="See implementation notes"><span>Notes</span><i class="ic-footnote"></i></abbr> Disabled in the Firefox 52 Extended Support Release (ESR).</dd></dl></section></td><td class="bc-supports-unknown bc-browser-opera_android"><span class="bc-browser-name">Opera Android</span><abbr title="Compatibility unknown; please update this.">
?
</abbr></td><td class="bc-supports-yes bc-browser-safari_ios"><span class="bc-browser-name">Safari iOS</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
11</td><td class="bc-supports-yes bc-browser-samsunginternet_android"><span class="bc-browser-name">Samsung Internet Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
7.0</td><td class="bc-supports-yes bc-browser-nodejs"><span class="bc-browser-name">nodejs</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
8.0.0</td></tr></tbody></table><section class="bc-legend" id="sect4"><h3 class="offscreen" id="Legend">Legend</h3><dl><dt><span class="bc-supports-yes bc-supports">
<abbr class="bc-level bc-level-yes only-icon" title="Full support">
<span>Full support</span>
 
</abbr></span></dt><dd>Full support</dd><dt><span class="bc-supports-no bc-supports">
<abbr class="bc-level bc-level-no only-icon" title="No support">
<span>No support</span>
 
</abbr></span></dt><dd>No support</dd><dt><span class="bc-supports-unknown bc-supports">
<abbr class="bc-level bc-level-unknown only-icon" title="Compatibility unknown">
<span>Compatibility unknown</span>
 
</abbr></span></dt><dd>Compatibility unknown</dd><dt><abbr class="only-icon" title="See implementation notes."><span>See implementation notes.</span><i class="ic-footnote"></i></abbr></dt><dd>See implementation notes.</dd><dt><abbr class="only-icon" title="User must explicitly enable this feature."><span>User must explicitly enable this feature.</span><i class="ic-disabled"></i></abbr></dt><dd>User must explicitly enable this feature.</dd></dl></section></div><p></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>
</article>