emscripten-core / emsdk

Emscripten SDK
http://emscripten.org
Other
3.02k stars 688 forks source link

Embind post-init setup in worker #1245

Closed guthriec closed 1 year ago

guthriec commented 1 year ago

Hello!

I have a project in which a web app calls into WASM (C++) to kick off a (potentially long-running) task on a pthread. Rather than have the web app poll the WASM module to see when the task is finished, I would like the C++ to be able to call a method on a Javascript handler object, from within this pthread.

The trouble is, the relevant EMSCRIPTEN_BINDING runs an EM_JS block exposing a method on the WASM module to register types, and this type registration is central to getting the handler object to work. Currently, I register the types on the main thread after the Module finishes loading, in the initial promise completion. However, when Embind is initialized in the worker, this completion doesn't get called, since the Module is already initialized.

The .worker.js file has a section like:

      if (!initializedJS) {
        // Embind must initialize itself on all threads, as it generates support JS.
        // We only do this once per worker since they get reused
        Module['__embind_initialize_bindings']();
        initializedJS = true;
      }

What I (think) I would like is some callback after embind_initialize_bindings has been called so I can do some setup on the WASM module.

I'm pretty new to this, so let me know if this is confusing or you need more clarification, or if there's something big I'm missing. Thanks!

guthriec commented 1 year ago

Actually, let me see if I can figure out how to do what I'm trying from within the EM_JS block. I'll reopen if I end up having trouble with that.