网络工作者中的 Emscripten WASM:"module is not an object or function"
Emscripten WASM in web worker: "module is not an object or function"
我正在尝试跟随 tutorials 到 运行 emscripten-built web assembly in a webworker。
当我实例化我的模块时,我得到 WebAssembly Instantiation: Import #9 module="global" error: module is not an object or function
。
这是我生成一个 worker 并将编译后的模块发送给它的代码:
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 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
如果我将 global: {},
添加到 config
字典中,它会抱怨 Import #11 module="global.Math" error: module is not an object or function
,如果我随后添加 'global.Math: Math
,我会得到 memory import 0 is smaller than initial 4096, got 256
,依此类推直到我觉得我是被打的内奸。
我怀疑我做的完全错了。
我想我明白了。如果我从头开始并将 var g_worker = new Worker('worker.js')
保留在我的第一个文件中,并在 worker.js:
self.importScripts('my_module.js');
有效。好像看错教程了
我正在尝试跟随 tutorials 到 运行 emscripten-built web assembly in a webworker。
当我实例化我的模块时,我得到 WebAssembly Instantiation: Import #9 module="global" error: module is not an object or function
。
这是我生成一个 worker 并将编译后的模块发送给它的代码:
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 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
如果我将 global: {},
添加到 config
字典中,它会抱怨 Import #11 module="global.Math" error: module is not an object or function
,如果我随后添加 'global.Math: Math
,我会得到 memory import 0 is smaller than initial 4096, got 256
,依此类推直到我觉得我是被打的内奸。
我怀疑我做的完全错了。
我想我明白了。如果我从头开始并将 var g_worker = new Worker('worker.js')
保留在我的第一个文件中,并在 worker.js:
self.importScripts('my_module.js');
有效。好像看错教程了