[英]How can I use an emscripted .js library in a Web Worker
I've compiled my program into a single program.js
file, and within the worker, I call: 我把我的程序编译成一个
program.js
文件,在worker中,我调用:
importScripts('program.js');
Module.callMain();
However, although callMain
is executing, within that function invocation's stack I am getting the Undefined is not a function
error. 但是,虽然
callMain
正在执行,但在该函数调用的堆栈中我得到的Undefined is not a function
错误。
My hunch is that I haven't read some essential emscripten documentation, wiki, or sourcecode. 我的预感是我没有阅读一些基本的emscripten文档,wiki或源代码。
emcc
needs special options passed at compile time for that task (see settings.js ) emcc
需要在编译时为该任务传递的特殊选项(请参阅settings.js ) window
window
/dev/tty
, which is linked to by /dev/stdin
, and the implementation of /dev/stdout
, I believe there may be output to the console
, which I don't believe is supported in a worker /dev/tty
的实现,它是由/dev/stdin
,以及/dev/stdout
的实现,我相信可能会有输出到console
,我不相信它是支持的工人 program.data
file, if that makes a difference program.data
文件加载其他文件,如果这有所不同 Thanks in advance. 提前致谢。 Please let me know if I can add any details.
如果我可以添加任何细节,请告诉我。
Note: the code at top is simplified - it doesn't seem to be an error in syntax. 注意:顶部的代码是简化的 - 它似乎不是语法错误。
Error.stack
) only tells me that the error occured in Module.callMain
. Error.stack
)只告诉我Module.callMain
发生了错误。 It does not tell me anything about the imported script that might be causing it. Emscripten output should run on a website, on a web worker, or in node, all without modifications. Emscripten输出应该在网站,Web工作者或节点中运行,所有这些都无需修改。 There might be some limitations (eg no canvas to render to in a worker), but otherwise it is just pure JS that can run in those three environments.
可能存在一些限制(例如,没有画布可以在工作器中呈现),但是否则它只是纯JS可以在这三种环境中运行。
Does the exact same JS file that emscripten generated work outside of a worker? emscripten生成的完全相同的JS文件是否在工作者之外工作?
If callMain()
fails, check that your project has a main()
function. 如果
callMain()
失败,请检查您的项目是否具有main()
函数。
Immediately after loading the js (by importScripts()
) it may not be executed yet, hence you are not ready. 加载js后立即(通过
importScripts()
)它可能还没有执行,因此你还没准备好。 You can ask Emscripten to call a callback for you when things are ready. 事情准备好后,您可以要求Emscripten为您拨打电话。 You will have functions accessible via the variable
Module
. 您可以通过变量
Module
访问功能。
var Module = {
preRun:[],
onRuntimeInitialized: function load_done_callback() {
console.info("The Module is loaded and is accessible here", Module);
console.inf("no need to call main() though", Module._main); // add "_" before your function names
},
};
importScripts('program.js');
No need to call callMain()
or Module._main()
, it will be called for you. 无需调用
callMain()
或Module._main()
,它将为您调用。 You may need to set NO_EXIT_RUNTIME=1
in compiler command line: 您可能需要在编译器命令行中设置
NO_EXIT_RUNTIME=1
:
em++ \
-s EXPORTED_FUNCTIONS="['_main', '_myfunction1' , '_my_function2' ]" \
-s NO_EXIT_RUNTIME=1 \
-s DEMANGLE_SUPPORT=1 \
program.cpp \
-o ./program.js
Note that you don't need to compile as worker in this case where you use importScripts()
. 请注意,在使用
importScripts()
情况下,您不需要编译为worker。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.