mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2026-02-28 01:58:28 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -9,8 +9,8 @@
|
||||
</ul>
|
||||
<h2 id="语法">语法</h2>
|
||||
<h3 id="主重载方式_—_使用wasm二进制代码">主重载方式 — 使用wasm二进制代码</h3>
|
||||
<pre class="syntaxbox">Promise<ResultObject> WebAssembly.instantiate(bufferSource, importObject);
|
||||
</pre>
|
||||
<pre><code class="language-javascript">Promise<ResultObject> WebAssembly.instantiate(bufferSource, importObject);
|
||||
</code></pre>
|
||||
<h4 id="参数">参数</h4>
|
||||
<dl>
|
||||
<dt><em>bufferSource</em></dt>
|
||||
@@ -30,8 +30,8 @@
|
||||
<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 class="syntaxbox">Promise<WebAssembly.Instance> WebAssembly.instantiate(module, importObject);
|
||||
</pre>
|
||||
<pre><code class="language-javascript">Promise<WebAssembly.Instance> WebAssembly.instantiate(module, importObject);
|
||||
</code></pre>
|
||||
<h4 id="参数_2">参数</h4>
|
||||
<dl>
|
||||
<dt><em>module</em></dt>
|
||||
@@ -49,7 +49,7 @@
|
||||
<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 class="brush: js">var importObject = {
|
||||
<pre><code class="language-javascript">var importObject = {
|
||||
imports: {
|
||||
imported_func: function(arg) {
|
||||
console.log(arg);
|
||||
@@ -63,13 +63,13 @@ fetch('simple.wasm').then(response =>
|
||||
WebAssembly.instantiate(bytes, importObject)
|
||||
).then(result =>
|
||||
result.instance.exports.exported_func()
|
||||
);</pre>
|
||||
);</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 class="brush: js">var worker = new Worker("wasm_worker.js");
|
||||
<pre><code class="language-javascript">var worker = new Worker("wasm_worker.js");
|
||||
|
||||
fetch('simple.wasm').then(response =>
|
||||
response.arrayBuffer()
|
||||
@@ -77,9 +77,9 @@ fetch('simple.wasm').then(response =>
|
||||
WebAssembly.compile(bytes)
|
||||
).then(mod =>
|
||||
worker.postMessage(mod)
|
||||
);</pre>
|
||||
);</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 class="brush: js">var importObject = {
|
||||
<pre><code class="language-javascript">var importObject = {
|
||||
imports: {
|
||||
imported_func: function(arg) {
|
||||
console.log(arg);
|
||||
@@ -94,7 +94,7 @@ onmessage = function(e) {
|
||||
WebAssembly.instantiate(mod, importObject).then(function(instance) {
|
||||
instance.exports.exported_func();
|
||||
});
|
||||
};</pre>
|
||||
};</code></pre>
|
||||
<h2 id="Specifications">Specifications</h2>
|
||||
<table class="standard-table">
|
||||
<thead>
|
||||
|
||||
Reference in New Issue
Block a user