简体   繁体   English

如何在Web Worker中使用emscripted .js库

[英]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或源代码。

What I know: 我知道的:

  • emscripten has bindings to work with workers, but only when there is an emscripted process on the main thread to use these bindings (see emscripten.h ) emscripten具有与worker一起工作的绑定,但只有当主线程上有一个emscripted进程才能使用这些绑定时(参见emscripten.h
  • In order to compile a task (within a library that runs on the main thread) to a worker, emcc needs special options passed at compile time for that task (see settings.js ) 为了将任务(在主线程上运行的库中) 编译到worker, emcc需要在编译时为该任务传递的特殊选项(请参阅settings.js

What I surmise: 我猜测的是:

  • There may be flags that I need to pass to let emcc know not to call functions on nonexistent objects, like window 我可能需要传递一些标志,让emcc知道不要在不存在的对象上调用函数,比如window
  • Having looked through the implementation of /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 ,我不相信它是支持的工人

What I am asking: 我要问的是:

  • How can I use my library, a single js file, in a worker? 如何在工作中使用我的库,单个js文件?
    • My library loads other files from a 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. 注意:顶部的代码是简化的 - 它似乎不是语法错误。

Update: 更新:

  • The stacktrace ( Error.stack ) only tells me that the error occured in Module.callMain . stacktrace( 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.

相关问题 如何在Web Worker中使用其他库? - How Can I use Other Libraries in a Web Worker? 我可以在网络工作者中使用模块吗? - Can I use modules in a web worker? 如何将库导入 React.js 中的 web 工作人员? - How to import a library into a web worker in React.js? 如果我在ffmpeg.js上使用Web Worker,如何获取正确的结果文件数据? - How to get the correct result file data if I use Web Worker on ffmpeg.js? 如何通过JavascriptCore注入和使用此javascript库(web3) - How can I inject and use this javascript library (web3) with JavascriptCore 如果Service Worker要求html与servive_worker.js位于同一文件夹中,我如何使用EJS(需要Views文件夹)? - If Service Worker requires html to be in same folder as servive_worker.js, how can I use EJS (which requires Views folder)? 在Web Worker中调整和压缩图像。 我可以用帆布吗? - Resize & Compress Image in Web Worker. Can I use a canvas? 如何在反应中使用第三个 js 库? - How can I use third js library in react? 没有任何安装,我可以使用 Service Worker 作为更持久的 Web Worker 吗? - Without any installs, can I just use a Service Worker as more persistent Web Worker? 如何在 web worker 中使用 import - How to use import in the web worker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM