aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/webassembly/index.html
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/webassembly/index.html
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-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/index.html')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/webassembly/index.html106
1 files changed, 106 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/webassembly/index.html b/files/zh-cn/web/javascript/reference/global_objects/webassembly/index.html
new file mode 100644
index 0000000000..66f14793af
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/webassembly/index.html
@@ -0,0 +1,106 @@
+---
+title: WebAssembly
+slug: Web/JavaScript/Reference/Global_Objects/WebAssembly
+tags:
+ - API
+ - JavaScript
+ - WebAssembly
+translation_of: Web/JavaScript/Reference/Global_Objects/WebAssembly
+---
+<div>{{JSRef}}</div>
+
+<p><strong><code>WebAssembly</code></strong>JavaScript 对象是所有 <a href="/en-US/docs/WebAssembly">WebAssembly</a> 相关功能的命名空间。</p>
+
+<p>和大多数全局对象不一样,<code>WebAssembly</code>不是一个构造函数(它不是一个函数对象)。它类似于 {{jsxref("Math")}} 对象或者 {{jsxref("Intl")}} 对象,Math 对象也是一个命名空间对象,用于保存数学常量和函数;Intl则是用于国际化和其他语言相关函数的命名空间对象。</p>
+
+<h2 id="描述">描述</h2>
+
+<p><code>WebAssembly</code>对象主要用于:</p>
+
+<ul>
+ <li>使用 {{jsxref("WebAssembly.instantiate()")}} 函数加载 WebAssembly 代码。</li>
+ <li>通过 {{jsxref("WebAssembly.Memory()")}}/{{jsxref("WebAssembly.Table()")}}  构造函数创建新的内存和表实例。</li>
+ <li>由 {{jsxref("WebAssembly.CompileError()")}}/{{jsxref("WebAssembly.LinkError()")}}/{{jsxref("WebAssembly.RuntimeError()")}} 构造函数来提供 WebAssembly 中的错误信息。</li>
+</ul>
+
+<h2 id="方法">方法</h2>
+
+<dl>
+ <dt>{{jsxref("WebAssembly.instantiate()")}}</dt>
+ <dd>用于编译和实例化 WebAssembly 代码的主 API,返回一个 <code>Module</code> 和它的第一个<code>Instance</code>实例。</dd>
+ <dt>{{jsxref("WebAssembly.instantiateStreaming()")}}</dt>
+ <dd>直接从流式底层源编译和实例化WebAssembly模块,同时返回<code>Module</code>及其第一个<code>Instance</code>实例。</dd>
+ <dt>{{jsxref("WebAssembly.compile()")}}</dt>
+ <dd>把 WebAssembly 二进制代码编译为一个 {{jsxref("WebAssembly.Module")}} ,不进行实例化。</dd>
+ <dt>{{jsxref("WebAssembly.compileStreaming()")}}</dt>
+ <dd>直接从流式底层源代码编译{{jsxref("WebAssembly.Module")}} ,将实例化作为一个单独的步骤。</dd>
+ <dt>{{jsxref("WebAssembly.validate()")}}</dt>
+ <dd>校验 WebAssembly 二进制代码的类型数组是否合法,合法则返回 true ,否则返回 false 。</dd>
+</dl>
+
+<h2 id="构造器">构造器</h2>
+
+<dl>
+ <dt>{{jsxref("WebAssembly.Global()")}}</dt>
+ <dd>创建一个新的WebAssembly <code>Global</code> 全局对象.</dd>
+ <dt>{{jsxref("WebAssembly.Module()")}}</dt>
+ <dd>创建一个新的WebAssembly <code>Module</code>模块对象。</dd>
+ <dt>{{jsxref("WebAssembly.Instance()")}}</dt>
+ <dd>创建一个新的WebAssembly <code>Instance</code>实例对象。</dd>
+ <dt>{{jsxref("WebAssembly.Memory()")}}</dt>
+ <dd>创建一个新的WebAssembly <code>Memory</code>内存对象。</dd>
+ <dt>{{jsxref("WebAssembly.Table()")}}</dt>
+ <dd>创建一个新的WebAssembly <code>Table</code>表格对象。</dd>
+ <dt>{{jsxref("WebAssembly.CompileError()")}}</dt>
+ <dd>创建一个新的WebAssembly <code>CompileError</code>编译错误对象。</dd>
+ <dt>{{jsxref("WebAssembly.LinkError()")}}</dt>
+ <dd>创建一个新的WebAssembly <code>LinkError</code>链接错误对象。</dd>
+ <dt>{{jsxref("WebAssembly.RuntimeError()")}}</dt>
+ <dd>创建一个新的WebAssembly <code>RuntimeError</code>运行时错误对象。</dd>
+</dl>
+
+<h2 id="示例">示例</h2>
+
+<p>下面的示例(请参见GitHub上的<a href="https://github.com/mdn/webassembly-examples/blob/master/js-api-examples/instantiate-streaming.html">Instantiate-streaming.html</a>演示,并查看<a href="https://mdn.github.io/webassembly-examples/js-api-examples/instantiate-streaming.html">在线演示</a>)直接从流式底层源传输.wasm模块,然后对其进行编译和实例化,并通过<code>ResultObject</code>实现promise。 由于<code>instantiateStreaming()</code>函数接受对 {{domxref("Response")}} 对象的promise,因此您可以直接向其传递{{domxref("WindowOrWorkerGlobalScope.fetch()")}}调用,然后它将把返回的response传递给随后的函数。</p>
+
+<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="keyword token">var</span> importObject <span class="operator token">=</span> <span class="punctuation token">{</span> imports<span class="punctuation token">:</span> <span class="punctuation token">{</span> <span class="function function-variable token">imported_func</span><span class="punctuation token">:</span> <span class="parameter token">arg</span> <span class="operator token">=&gt;</span> console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>arg<span class="punctuation token">)</span> <span class="punctuation token">}</span> <span class="punctuation token">}</span><span class="punctuation token">;</span>
+
+WebAssembly<span class="punctuation token">.</span><span class="function token">instantiateStreaming</span><span class="punctuation token">(</span><span class="function token">fetch</span><span class="punctuation token">(</span><span class="string token">'simple.wasm'</span><span class="punctuation token">)</span><span class="punctuation token">,</span> importObject<span class="punctuation token">)</span>
+<span class="punctuation token">.</span><span class="function token">then</span><span class="punctuation token">(</span><span class="parameter token">obj</span> <span class="operator token">=&gt;</span> obj<span class="punctuation token">.</span>instance<span class="punctuation token">.</span>exports<span class="punctuation token">.</span><span class="function token">exported_func</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">)</span></code></pre>
+
+<p>返回的<code>ResultObject</code>实例的成员可以被随后访问到,可以调用实例中被导出的方法。</p>
+
+<h2 id="规范">规范</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 JS', '#the-webassembly-object', 'WebAssembly')}}</td>
+ <td>{{Spec2('WebAssembly JS')}}</td>
+ <td>初始草案定义</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2>
+
+<div>
+
+
+<p>{{Compat("javascript.builtins.WebAssembly")}}</p>
+
+<h2 id="参见">参见</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>
+</div>