From 5b2cf7d1fdb2a506a84336f97fa58aecab2b8374 Mon Sep 17 00:00:00 2001 From: Masahiro FUJIMOTO Date: Wed, 8 Sep 2021 23:25:10 +0900 Subject: Global_Objects/WebAssembly/compile を更新 (#2253) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Markdownに変換 - 2021/08/27 時点の英語版に同期 --- .../global_objects/webassembly/compile/index.html | 97 ---------------------- .../global_objects/webassembly/compile/index.md | 72 ++++++++++++++++ 2 files changed, 72 insertions(+), 97 deletions(-) delete mode 100644 files/ja/web/javascript/reference/global_objects/webassembly/compile/index.html create mode 100644 files/ja/web/javascript/reference/global_objects/webassembly/compile/index.md (limited to 'files') 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 deleted file mode 100644 index 1cdf19f657..0000000000 --- a/files/ja/web/javascript/reference/global_objects/webassembly/compile/index.html +++ /dev/null @@ -1,97 +0,0 @@ ---- -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")}}
- -

関連情報

- - diff --git a/files/ja/web/javascript/reference/global_objects/webassembly/compile/index.md b/files/ja/web/javascript/reference/global_objects/webassembly/compile/index.md new file mode 100644 index 0000000000..0fb86c25d4 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/webassembly/compile/index.md @@ -0,0 +1,72 @@ +--- +title: WebAssembly.compile() +slug: Web/JavaScript/Reference/Global_Objects/WebAssembly/compile +tags: + - API + - JavaScript + - Method + - Object + - Reference + - WebAssembly + - compile +browser-compat: javascript.builtins.WebAssembly.compile +translation_of: Web/JavaScript/Reference/Global_Objects/WebAssembly/compile +--- +{{JSRef}} + +**`WebAssembly.compile()`** 関数は WebAssembly バイナリーコードを {{jsxref("WebAssembly.Module")}} の形にコンパイルします。この関数は、モジュールをインスタンス化する前にコンパイルする必要がある場合に便利です (それ以外の場合は、 {{jsxref("WebAssembly.instantiate()")}} 関数を使用してください)。

+ +## 構文 + +```js +WebAssembly.compile(bufferSource) +``` + +### 引数 + +- _bufferSource_ + - : コンパイルする .wasm モジュールのバイナリーコードを含む[型付き配列](/ja/docs/Web/JavaScript/Typed_arrays)または [`ArrayBuffer`](/ja/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) です。 + +### 返値 + +コンパイルされたモジュールを表す {{jsxref("WebAssembly.Module")}} オブジェクトに解決する `Promise` です。 + +### 例外 + +- `bufferSource` が[型付き配列](/ja/docs/Web/JavaScript/Typed_arrays)ではなかった場合、 {{jsxref("TypeError")}} が発生します。 +- コンパイルに失敗した場合、プロミスは {{jsxref("WebAssembly.CompileError")}} で拒否されます。 + +## 例 + +### compile の使用 + +次の例では、読み込まれた simple.wasm バイトコードを、 `compile()` 関数を使用してコンパイルし、[ワーカー](/ja/docs/Web/API/Web_Workers_API)に [postMessage()](/ja/docs/Web/API/Worker/postMessage) を用いて送信します。 + +```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) +); +``` + +> **Note:** おそらく多くの場合は {{jsxref("WebAssembly.compileStreaming()")}} を使用したほうが `compile()` よりも効率的なのでそちらの方がいいでしょう。 + + +## 仕様書 + +{{Specifications}} + +## ブラウザーの互換性 + +{{Compat}} + +## 関連情報 + +- [WebAssembly](/ja/docs/WebAssembly) 概要ページ +- [WebAssembly の概念](/ja/docs/WebAssembly/Concepts) +- [WebAssembly JavaScript API の使用](/ja/docs/WebAssembly/Using_the_JavaScript_API) -- cgit v1.2.3-54-g00ecf