簡體   English   中英

Web worker中的Emscripten WASM:“模塊不是對象或函數”

[英]Emscripten WASM in web worker: “module is not an object or function”

我正在嘗試按照教程在Webworker中運行Emscripten構建的Web程序集。

實例化模塊時,我得到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);
    });

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'global.Math: Math memory import 0 is smaller than initial 4096, got 256 ,依此類推,直到我感覺自己像是被重擊的痣。

我懷疑自己做錯了。

我想我知道了。 如果我從頭開始,並在第一個文件和worker.js中保留var g_worker = new Worker('worker.js')

self.importScripts('my_module.js');

有用。 似乎我只是讀錯了教程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM