WebAssembly.Module.exports()
WebAssembly.Module.exports() 靜態方法返回一個數組,其中包含給定 Module 的所有已宣告匯出項的描述。
語法
js
WebAssembly.Module.exports(module)
引數
模組-
一個
WebAssembly.Module物件。
返回值
一個包含代表給定模組匯出函式的物件的陣列。
異常
如果 module 不是 WebAssembly.Module 物件例項,則會丟擲 TypeError。
示例
使用匯出項
以下示例(請參閱 GitHub 上的 index-compile.html 演示,以及 線上檢視)使用 WebAssembly.compileStreaming() 方法編譯載入的 simple.wasm 位元組碼,然後使用 postMessage() 將其傳送到 worker。
js
const worker = new Worker("wasm_worker.js");
WebAssembly.compileStreaming(fetch("simple.wasm")).then((mod) =>
worker.postMessage(mod),
);
在 worker 中(請參閱 wasm_worker.js),我們為模組定義了一個匯入物件,然後設定一個事件處理程式來接收主執行緒的模組。收到模組後,我們使用 WebAssembly.Instantiate() 方法從中建立一個例項,呼叫其中的一個匯出函式,然後展示如何使用 WebAssembly.Module.exports 返回有關模組可用匯出項的資訊。
js
const importObject = {
my_namespace: {
imported_func(arg) {
console.log(arg);
},
},
};
onmessage = (e) => {
console.log("module received from main thread");
const mod = e.data;
WebAssembly.instantiate(mod, importObject).then((instance) => {
instance.exports.exported_func();
});
const exports = WebAssembly.Module.exports(mod);
console.log(exports[0]);
};
exports[0] 的輸出如下所示
json
{ "name": "exported_func", "kind": "function" }
規範
| 規範 |
|---|
| WebAssembly JavaScript 介面 # dom-module-exports |
瀏覽器相容性
載入中…