我正在使用Windows 10 Pro
我有这个main.cpp
测试文件:
#include <emscripten.h>
EMSCRIPTEN_KEEPALIVE
const char* testFunc(const char* parameter) {
return parameter;
}
然后,我使用以下命令进行编译:
em++ .\main.cpp -s WASM=1 -s EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]' -o data_converter.js
我将输出文件复制到了我的网站目录:
$website = "...";
Copy-Item .\data_converter.js ($website + "src\data\wasm\data_converter.js");
Copy-Item .\data_converter.wasm ($website + "src\data\wasm\data_converter.wasm");
然后,在wasm-test.js
中:
async function fetchWasm(wasm) {
const { data } = await axios(wasm, { responseType: "arraybuffer" });
return data;
}
async loadWasm(wasmPath) {
const bits = await fetchWasm(wasmPath);
const worker = new Worker("wasm-worker.js");
worker.postMessage({ bits });
return new Promise((resolve) =>
worker.addEventListener("message", resolve)
);
}
async init() {
console.log(await loadWasm("data/wasm/data_converter.wasm"))
}
init();
然后,wasm-worker.js
:
addEventListener("message", ({ data: { bits } }) => {
WebAssembly.compile(bits).then((module) => {
const instance = new WebAssembly.Instance(module);
self.postMessage(instance);
});
});
最终,我得到了错误:
Uncaught (in promise) TypeError: WebAssembly.Instance(): Imports argument must be present and must be an object