--- title: WebAssembly slug: Web/JavaScript/Reference/Global_Objects/WebAssembly tags: - API - JavaScript - Namespace - Object - Reference - WebAssembly translation_of: Web/JavaScript/Reference/Global_Objects/WebAssembly ---
{{JSRef}}{{SeeCompatTable}}

WebAssembly JavaScript オブジェクトは全ての WebAssembly に関連する機能の名前空間として振る舞います。

他のグローバルオブジェクトとは異なり、WebAssembly はコンストラクタではありません (関数オブジェクトではない) 。数学定数、関数の名前空間である {{jsxref("Math")}} や 、国際化コンストラクタと他の言語に依存する関数のための {{jsxref("Intl")}} と同等のものです。

概要

WebAssembly オブジェクトの主な用途は次のとおりです:

メソッド

{{jsxref("WebAssembly.instantiate()")}}
WebAssembly コードをコンパイル、インスタンス化するための主要な API で、 Module と、その最初の Instance を返します。
{{jsxref("WebAssembly.instantiateStreaming()")}}
ソースのストリームから直接 WebAssembly モジュールをコンパイル、インスタンス化し、 Module と、その最初の Instance を返します。
{{jsxref("WebAssembly.compile()")}}
{{jsxref("WebAssembly.Module")}} を用いて WebAssembly バイナリコードからコンパイルします。インスタンス化は別ステップとして分離されます。
{{jsxref("WebAssembly.compileStreaming()")}}
ソースのストリームから直接 {{jsxref("WebAssembly.Module")}} にコンパイルします。インスタンス化は別ステップとして分離されます。
{{jsxref("WebAssembly.validate()")}}
WebAssembly バイナリコードの型付き配列を検証し、バイト列が有効な WebAssembly コードか (true) 否か (false) を返します。

コンストラクタ

{{jsxref("WebAssembly.Module()")}}
新しい WebAssembly Module オブジェクトを生成します。
{{jsxref("WebAssembly.Instance()")}}
新しい WebAssembly Instance オブジェクトを生成します。
{{jsxref("WebAssembly.Memory()")}}
新しい WebAssembly Memory オブジェクトを生成します。
{{jsxref("WebAssembly.Table()")}}
新しい WebAssembly Table オブジェクトを生成します。
{{jsxref("WebAssembly.CompileError()")}}
新しい WebAssembly CompileError オブジェクトを生成します。
{{jsxref("WebAssembly.LinkError()")}}
新しい WebAssembly LinkError オブジェクトを生成します。
{{jsxref("WebAssembly.RuntimeError()")}}
新しい WebAssembly RuntimeError オブジェクトを生成します。

fetch を使用して WebAssembly バイトコードをフェッチした後、{{jsxref("WebAssembly.instantiate()")}} 関数を使用してモジュールをコンパイル、インスタンス化します。その過程で、WebAssembly モジュールに JavaScript の関数をインポートします。このプロミスは解決時に Module と Instance を含むオブジェクト (result) を渡します。次に、Instance からエクスポートされている エクスポートされた WebAssembly 関数 を呼び出します。

var importObject = {
  imports: {
    imported_func: function(arg) {
      console.log(arg);
    }
  }
};

fetch('simple.wasm').then(response =>
  response.arrayBuffer()
).then(bytes =>
  WebAssembly.instantiate(bytes, importObject)
).then(result =>
  result.instance.exports.exported_func()
);

: GitHub上の例 (動作例) のindex.htmlでは、我々で定義した fetchAndInstantiate() ライブラリ関数を使用しています。

仕様

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

ブラウザ実装状況

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

関連情報