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/ja/web/javascript/reference/global_objects/webassembly/compile | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/ja/web/javascript/reference/global_objects/webassembly/compile')
-rw-r--r-- | files/ja/web/javascript/reference/global_objects/webassembly/compile/index.html | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/files/ja/web/javascript/reference/global_objects/webassembly/compile/index.html b/files/ja/web/javascript/reference/global_objects/webassembly/compile/index.html new file mode 100644 index 0000000000..1cdf19f657 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/webassembly/compile/index.html @@ -0,0 +1,97 @@ +--- +title: WebAssembly.compile() +slug: Web/JavaScript/Reference/Global_Objects/WebAssembly/compile +translation_of: Web/JavaScript/Reference/Global_Objects/WebAssembly/compile +--- +<div>{{JSRef}} {{SeeCompatTable}}</div> + +<p><strong><code>WebAssembly.compile()</code></strong> 関数は WebAssembly バイナリコードから {{jsxref("WebAssembly.Module")}} にコンパイルします。この関数はモジュールをインスタンス化する前にコンパイルする必要がある時に便利です。(そうでなければ、 {{jsxref("WebAssembly.instantiate()")}} 関数の使用が推奨されます。</p> + +<h2 id="構文">構文</h2> + +<pre class="syntaxbox">Promise<WebAssembly.Module> WebAssembly.compile(bufferSource);</pre> + +<h3 id="パラメータ">パラメータ</h3> + +<dl> + <dt><em>bufferSource</em></dt> + <dd>コンパイルする <a href="/ja/docs/Web/JavaScript/Typed_arrays">型付き配列</a> か <a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer">ArrayBuffer</a> を含む .wasm モジュールのバイナリコード。</dd> +</dl> + +<h3 id="戻り値">戻り値</h3> + +<p>解決時にコンパイルされたモジュールを表す {{jsxref("WebAssembly.Module")}} オブジェクト渡す <code>Promise 。</code></p> + +<h3 id="例外">例外</h3> + +<ul> + <li><font face="Consolas, Liberation Mono, Courier, monospace">バイナリソースが</font> <a href="/ja/docs/Web/JavaScript/Typed_arrays">型付き配列</a> でない場合、 {{jsxref("TypeError")}} がスローされます。</li> + <li>コンパイルが失敗したとき、プロミスは {{jsxref("WebAssembly.CompileError")}} で棄却されます。</li> +</ul> + +<h2 id="例">例</h2> + +<p>以下の例では (Github上のデモ <a href="https://github.com/mdn/webassembly-examples/blob/master/js-api-examples/index-compile.html">index-compile.html</a> と <a href="https://mdn.github.io/webassembly-examples/js-api-examples/index-compile.html">動作例</a> をご確認ください) <code>compile()</code> 関数を使ってロードした simple.wasm のバイトコードをコンパイルして、その後 <a href="/ja/docs/Web/API/Worker/postMessage">postMessage()</a> を使って <a href="https://developer.mozilla.org/ja/docs/Web/API/Web_Workers_API">worker</a> に送信しています。</p> + +<pre class="brush: js">var worker = new Worker("wasm_worker.js"); + +fetch('simple.wasm').then(response => + response.arrayBuffer() +).then(bytes => + WebAssembly.compile(bytes) +).then(mod => + worker.postMessage(mod) +);</pre> + +<p>ワーカー内で (<code><a href="https://github.com/mdn/webassembly-examples/blob/master/js-api-examples/wasm_worker.js">wasm_worker.js</a> を参照</code>) モジュールで使用するためのインポートオブジェクトを定義して、メインスレッドからモジュールを受け取るためのイベントハンドラをセットアップします。モジュールを受け取ったとき、 {{jsxref("WebAssembly.Instantiate()")}} メソッドを使用してモジュールからインスタンスを生成します。内部からエクスポートされた関数を実行して、その後に {{jsxref("WebAssembly.Module/exports", "WebAssembly.Module.exports")}} プロパティを使用してモジュール上で利用可能なエクスポートに関する情報を確認する方法を示します。</p> + +<pre class="brush: js">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(); + }); + + var exports = WebAssembly.Module.exports(mod); + console.log(exports[0]); +};</pre> + +<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>{{SpecName('WebAssembly JS', '#webassemblycompile', 'compile()')}}</td> + <td>{{Spec2('WebAssembly JS')}}</td> + <td>初回ドラフト定義。</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザ実装状況</h2> + +<div>{{Compat("javascript.builtins.WebAssembly.compile")}}</div> + +<h2 id="関連情報">関連情報</h2> + +<ul> + <li><a href="/ja/docs/WebAssembly">WebAssembly</a> overview page</li> + <li><a href="https://developer.mozilla.org/ja/docs/WebAssembly/Concepts">WebAssembly のコンセプト</a></li> + <li><a href="https://developer.mozilla.org/ja/docs/WebAssembly/Using_the_JavaScript_API">WebAssembly JavaScript API を使用する</a></li> +</ul> |