Accessing a thread's module instance in an -sMODULARIZE -pthread setting?

30 views
Skip to first unread message

Stephan Bergmann

unread,
Jul 17, 2025, 10:37:32 AMJul 17
to emscripte...@googlegroups.com
When I use -sMODULARIZE and -pthread, code gets generated into the *.js
file so that the pthread workers do

> isPthread && Module();

(where the Module() invocation returns a promise that resolves to a
module instance). But that module instance is effectively "lost", or is
there some way for me to obtain it? (Other than using sed on the
generated *.js to make that line instead read

> isPthread && createOnlineModule().then(module => ...);

and capture the instance.)

Sam Clegg

unread,
Jul 22, 2025, 2:49:11 PMJul 22
to emscripte...@googlegroups.com
Yes, the "module" object in this case is internal to the thread only.    No code other than emscripten-generted code ever runs in a pthread worker so hopefully there is no external code that might need it.

All internal code such as EM_ASM and js-library functions all run in the module scope so have access to all the module instance internals already.  Anything you would access have `MyModule.foo` can be accessed simply via `foo` in internal code (this goes for single threaded code too).

cheers,
sam

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/emscripten-discuss/c7ce1e8e-2f14-4739-a1f2-5598133ad270%40gmail.com.

Stephan Bergmann

unread,
Jul 23, 2025, 5:43:26 AMJul 23
to emscripte...@googlegroups.com
On 7/22/25 20:48, 'Sam Clegg' via emscripten-discuss wrote:
> Yes, the "module" object in this case is internal to the thread only.
> No code other than emscripten-generted code ever runs in a pthread
> worker so hopefully there is no external code that might need it.

unless code gets loaded via WorkerGlobalScope.importScripts (as is e.g.
done at
<https://git.libreoffice.org/core/+/5dbc995f73da45ad99b95bc3119c75451b2c415d/desktop/source/app/appinit.cxx#78>)

> All internal code such as EM_ASM and js-library functions all run in the
> module scope so have access to all the module instance internals
> already.  Anything you would access have `MyModule.foo` can be accessed
> simply via `foo` in internal code (this goes for single threaded code too).
>
> cheers,
> sam
>
> On Thu, Jul 17, 2025 at 7:37 AM Stephan Bergmann <sber...@gmail.com
> <mailto:sber...@gmail.com>> wrote:
>
> When I use -sMODULARIZE and -pthread, code gets generated into the *.js
> file so that the pthread workers do
>
> > isPthread && Module();
>
> (where the Module() invocation returns a promise that resolves to a
> module instance).  But that module instance is effectively "lost",
> or is
> there some way for me to obtain it?  (Other than using sed on the
> generated *.js to make that line instead read
>
> > isPthread && createOnlineModule().then(module => ...);
>
> and capture the instance.)
>
> --
> You received this message because you are subscribed to the Google
> Groups "emscripten-discuss" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to emscripten-disc...@googlegroups.com
> <mailto:emscripten-discuss%2Bunsu...@googlegroups.com>.
> To view this discussion visit https://groups.google.com/d/msgid/
> emscripten-discuss/c7ce1e8e-2f14-4739-a1f2-5598133ad270%40gmail.com
> <https://groups.google.com/d/msgid/emscripten-discuss/
> c7ce1e8e-2f14-4739-a1f2-5598133ad270%40gmail.com>.

Sam Clegg

unread,
Jul 23, 2025, 7:14:58 PMJul 23
to emscripte...@googlegroups.com
On Wed, Jul 23, 2025 at 2:43 AM Stephan Bergmann <sber...@gmail.com> wrote:
On 7/22/25 20:48, 'Sam Clegg' via emscripten-discuss wrote:
> Yes, the "module" object in this case is internal to the thread only.   
> No code other than emscripten-generted code ever runs in a pthread
> worker so hopefully there is no external code that might need it.

unless code gets loaded via WorkerGlobalScope.importScripts (as is e.g.
done at
<https://git.libreoffice.org/core/+/5dbc995f73da45ad99b95bc3119c75451b2c415d/desktop/source/app/appinit.cxx#78>)

I think you could try something like this your JS code right before the call to `importScripts`:  `globaThis.Module ||= Module`.

Then the imported code would have access to the `Module` object. 

cheers,
sam
 
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/emscripten-discuss/1f9f3b36-be1c-474a-b266-acce3f2d483e%40gmail.com.

Stephan Bergmann

unread,
Jul 24, 2025, 5:14:34 AMJul 24
to emscripte...@googlegroups.com
On 7/24/25 01:14, 'Sam Clegg' via emscripten-discuss wrote:
> I think you could try something like this your JS code right before the
> call to `importScripts`:  `globaThis.Module ||= Module`.

oh yes, sure, thanks! :)
Reply all
Reply to author
Forward
0 new messages