I'm trying to follow tutorials to run emscripten-built web assembly in a webworker.
When I instantiate my module I get WebAssembly Instantiation: Import #9 module="global" error: module is not an object or function
.
This is my code to spawn a worker and send it the compiled module:
var g_worker = new Worker('worker.js');
WebAssembly.compileStreaming(fetch('my_module.wasm'))
.then(module => {
g_worker.postMessage(module);
});
worker.js:
self.onmessage = function (evt) {
var module = evt.data;
var config = {
env: {
memoryBase: 0,
tableBase: 0,
memory: new WebAssembly.Memory({initial: 256}),
table: new WebAssembly.Table({initial: 0, element: 'anyfunc'})
},
imports: {
imported_func: function(arg) {
console.log(arg);
}
}
};
WebAssembly.instantiate(module, config);
}
I build my module with these flags:
-I include \
-I third_party/eigen-git-mirror \
-s EXPORTED_FUNCTIONS="['_my_function', '_malloc', '_free']" \
-s EXPORT_NAME="'MyModule'" \
-s NO_FILESYSTEM=1 \
-s MODULARIZE=1 \
-s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]' \
-s BUILD_AS_WORKER=1 \
--memory-init-file 0 \
-s WASM=1 \
-s NO_EXIT_RUNTIME=1 \
-s ALLOW_MEMORY_GROWTH=1 \
-s TOTAL_MEMORY=256MB \
--closure 1 \
-g3
If I add global: {},
to the config
dictionary it complains Import #11 module="global.Math" error: module is not an object or function
, if I then add 'global.Math: Math
, I get memory import 0 is smaller than initial 4096, got 256
, and so on until I feel like I'm the mole being whacked.
I suspect I'm doing it completely wrong.
I think I figured it out. If I start from scratch and keep var g_worker = new Worker('worker.js')
in my first file, and in worker.js:
self.importScripts('my_module.js');
it works. Seems I just read the wrong tutorial.