WebAssembly.exports()
関数は指定した Module
のエクスポート宣言の定義の配列を返します。
構文
var exports = WebAssembly.Module.exports(module);
パラメータ
- module
WebAssembly.Module
オブジェクト。
戻り値
指定したモジュールのエクスポートされた関数を表現するオブジェクトの配列。
例外
もしモジュールが WebAssembly.Module
オブジェクトインスタンス出ない場合、TypeError
がスローされます。
例
次の例では (Github のデモ index-compile.html と、動作例 もご確認ください) WebAssembly.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)
);
worker (wasm_worker.js
を参照) 内で、モジュールで使用するためにインポートオブジェクトを定義して、そのあとにメインスレッドからモジュールを受け取るためのイベントハンドラをセットアップします。モジュールを受け取ったとき、WebAssembly.Instantiate()
メソッドを使用してインスタンスを生成し、その内部でエクスポートされた関数を実行します。そのあとに 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]);
};
exports[0]
のアウトプットはこのようになります:
{ name: "exported_func", kind: "function" }
仕様
仕様 | 策定状況 | コメント |
---|---|---|
WebAssembly JavaScript Interface exports() の定義 |
草案 | Initial draft definition. |
ブラウザ実装状況
BCD tables only load in the browser
関連情報
- WebAssembly overview page
- WebAssembly のコンセプト
- WebAssembly JavaScript API を使用する