From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/webassembly/compile/index.html | 97 ++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 files/ja/web/javascript/reference/global_objects/webassembly/compile/index.html (limited to 'files/ja/web/javascript/reference/global_objects/webassembly/compile') 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 +--- +
{{JSRef}} {{SeeCompatTable}}
+ +

WebAssembly.compile() 関数は WebAssembly バイナリコードから {{jsxref("WebAssembly.Module")}} にコンパイルします。この関数はモジュールをインスタンス化する前にコンパイルする必要がある時に便利です。(そうでなければ、 {{jsxref("WebAssembly.instantiate()")}} 関数の使用が推奨されます。

+ +

構文

+ +
Promise<WebAssembly.Module> WebAssembly.compile(bufferSource);
+ +

パラメータ

+ +
+
bufferSource
+
コンパイルする 型付き配列 か ArrayBuffer を含む .wasm モジュールのバイナリコード。
+
+ +

戻り値

+ +

解決時にコンパイルされたモジュールを表す {{jsxref("WebAssembly.Module")}} オブジェクト渡す Promise 。

+ +

例外

+ + + +

+ +

以下の例では (Github上のデモ index-compile.html と 動作例 をご確認ください) compile() 関数を使ってロードした simple.wasm のバイトコードをコンパイルして、その後 postMessage() を使って worker に送信しています。

+ +
var worker = new Worker("wasm_worker.js");
+
+fetch('simple.wasm').then(response =>
+  response.arrayBuffer()
+).then(bytes =>
+  WebAssembly.compile(bytes)
+).then(mod =>
+  worker.postMessage(mod)
+);
+ +

ワーカー内で (wasm_worker.js を参照) モジュールで使用するためのインポートオブジェクトを定義して、メインスレッドからモジュールを受け取るためのイベントハンドラをセットアップします。モジュールを受け取ったとき、 {{jsxref("WebAssembly.Instantiate()")}} メソッドを使用してモジュールからインスタンスを生成します。内部からエクスポートされた関数を実行して、その後に {{jsxref("WebAssembly.Module/exports", "WebAssembly.Module.exports")}} プロパティを使用してモジュール上で利用可能なエクスポートに関する情報を確認する方法を示します。

+ +
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]);
+};
+ +

仕様

+ + + + + + + + + + + + + + + + +
仕様策定状況コメント
{{SpecName('WebAssembly JS', '#webassemblycompile', 'compile()')}}{{Spec2('WebAssembly JS')}}初回ドラフト定義。
+ +

ブラウザ実装状況

+ +
{{Compat("javascript.builtins.WebAssembly.compile")}}
+ +

関連情報

+ + -- cgit v1.2.3-54-g00ecf