And*_*son 5 javascript web-worker emscripten webassembly
我正在尝试按照教程在网络工作人员中运行 emscripten 构建的网络程序集。
当我实例化我的模块时,我得到了WebAssembly Instantiation: Import #9 module="global" error: module is not an object or function.
这是我的代码,用于生成一个工作程序并向其发送已编译的模块:
var g_worker = new Worker('worker.js');
WebAssembly.compileStreaming(fetch('my_module.wasm'))
.then(module => {
g_worker.postMessage(module);
});
Run Code Online (Sandbox Code Playgroud)
工人.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);
}
Run Code Online (Sandbox Code Playgroud)
我用这些标志构建我的模块:
-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
Run Code Online (Sandbox Code Playgroud)
如果我添加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');
Run Code Online (Sandbox Code Playgroud)
有用。看来我刚刚读错了教程。