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

117 lines
7.9 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.Instance</code></strong> 对象是有状态的, 是<a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Module">WebAssembly.Module</a> 的一个可执行实例.  <code>实例</code>包含所有的 <a href="https://developer.mozilla.org/en-US/docs/WebAssembly/Exported_functions">WebAssembly 导出函数</a> ,允许从JavaScript 调用 WebAssembly 代码.</p>
<p><code>WebAssembly.Instance()</code> 构造函数以同步方式实例化一个<a href="Reference/Global_Objects/WebAssembly/Module" title="所有 Module 实例继承自 Module() 构造函数的原型对象 —— 修改它会影响所有 Module 实例。"><code>WebAssembly.Module</code></a> 对象. 然而, 通常获取实例的方法是通过异步函数<a href="Reference/Global_Objects/WebAssembly/instantiate" title="WebAssembly.instantiate() 是编译和实例化 WebAssembly 代码的主要方法.  这个方法有两个重载方式:"><code>WebAssembly.instantiate()</code></a> .</p>
<h2 id="构造函数语法">构造函数语法</h2>
<div class="warning">
<p><strong>重要</strong>: 因为大型模块的实例化比较昂贵, 开发人员应该只在同步实例化是必须时,才使用<code>Instance()</code>; 绝大多数情况应该使用异步方法<a href="Reference/Global_Objects/WebAssembly/instantiate" title="WebAssembly.instantiate() 是编译和实例化 WebAssembly 代码的主要方法.  这个方法有两个重载方式:"><code>WebAssembly.instantiate()</code></a> .</p>
</div>
<pre><code class="language-javascript">var myInstance = new WebAssembly.Instance(<em>module</em>, <em>importObject</em>);</code></pre>
<h3 id="参数">参数</h3>
<dl>
<dt><em>module</em></dt>
<dd>要被实例化的 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Module" title="The Module() constructor of the WebAssembly global object creates a new Module object instance."><code>WebAssembly.Module</code></a> 对象.</dd>
<dt><em>importObject</em> <span class="inlineIndicator optional optionalInline">可选</span></dt>
<dd>一个包含值的对象,导入到新创建的 <code>实例</code>, 比如函数或 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory" title="The Memory() constructor of the WebAssembly global object creates a new Memory object instance, which represents a WebAssembly memory. These can be thought of as resizeable array buffers, wrappers around WebAssembly memories."><code>WebAssembly.Memory</code></a> 对象. There must be one matching property for each declared import of <code>module</code> 否则抛出 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/LinkError">WebAssembly.LinkError</a> 异常.</dd>
</dl>
<h2 id="Instance_实例化"><code>Instance</code> 实例化</h2>
<p>所有的 <code>Instance</code> 实例继承自<code>Instance()</code> <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance/prototype">属性对象</a>— 修改它会影响所有的<code>Instance</code> 实例.</p>
<h3 id="Instance_属性">Instance 属性</h3>
<p></p><dl>
<dt><code>Instance.prototype.constructor</code></dt>
<dd>Returns the function that created this object's instance. By default this is the <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance" title="A WebAssembly.Instance object is a stateful, executable instance of a WebAssembly.Module.  Instance objects contain all the Exported WebAssembly functions that allow calling into WebAssembly code from JavaScript."><code>WebAssembly.Instance()</code></a> constructor.</dd>
<dt><code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance/exports">Instance.prototype.exports</a></code> <span class="inlineIndicator readOnly readOnlyInline" title="This value may not be changed.">Read only </span></dt>
<dd>Returns an object containing as its members all the functions exported from the WebAssembly module instance, to allow them to be accessed and used by JavaScript.</dd>
</dl><p></p>
<h3 id="Instance_方法">Instance 方法</h3>
<p></p><p>None.</p><p></p>
<h2 id="规格">规格</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">规格</th>
<th scope="col">状态</th>
<th scope="col">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><a class="external" href="https://webassembly.github.io/spec/js-api/#webassemblyinstance-objects" hreflang="en" lang="en" rel="noopener">WebAssembly JavaScript Interface<br/><small lang="zh-CN">Instance</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">浏览器兼容性</h2>
<div><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
<strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div></div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Edge</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari (WebKit)</th>
</tr>
<tr>
<td>Basic support</td>
<td>57</td>
<td>15<sup>[2]</sup></td>
<td><a href="/en-US/Firefox/Releases/52" title="Released on 2017-03-07.">52</a> (52)<sup>[1]</sup></td>
<td><span style="color: #f00;">未实现</span></td>
<td>44</td>
<td>11</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome for Android</th>
<th>Android Webview</th>
<th>Edge Mobile</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td>57</td>
<td>57</td>
<td><span style="color: #f00;">未实现</span></td>
<td>52.0 (52)<sup>[1]</sup></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td>11</td>
</tr>
</tbody>
</table>
</div>
<p>[1] WebAssembly is enabled in Firefox 52+, although disabled in the <a class="external" href="https://www.mozilla.org/en-US/firefox/organizations/" rel="noopener">Firefox 52 Extended Support Release</a> (ESR.)</p>
<p>[2] Currently supported behind the “Experimental JavaScript Features” flag. See <a class="external" href="https://blogs.windows.com/msedgedev/2017/04/20/improved-javascript-performance-webassembly-shared-memory/" rel="noopener">this blog post</a> for more details.</p>
<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>