1

I'm using Windows 10 Pro
I have this main.cpp test file:

#include <emscripten.h>

EMSCRIPTEN_KEEPALIVE
const char* testFunc(const char* parameter) {
    return parameter;
}

Then, I compiled using:

em++ .\main.cpp -s WASM=1 -s EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]' -o data_converter.js

I copied the output files to my website directory:

$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");

Then, in 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();

Then, wasm-worker.js:

addEventListener("message", ({ data: { bits } }) => {
  WebAssembly.compile(bits).then((module) => {
    const instance = new WebAssembly.Instance(module);
    self.postMessage(instance);
  });
});

Eventually, I'm getting the error:

Uncaught (in promise) TypeError: WebAssembly.Instance(): Imports argument must be present and must be an object
avi12
  • 2,000
  • 5
  • 24
  • 41

1 Answers1

0

The today's implementation of the WebAssembly.Instance does not explicitly require the second argument (the imports object).

When Emscripten generates the .js file, inside there shall be the code to instantiate the instance, it is not expected to do that by a hand written code.

Nikolay Handzhiyski
  • 1,360
  • 1
  • 6
  • 20