Skip to content

[WASM_WORKERS] Inline .ww.js file into the main output .js file - #24163

Merged
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:wasm_worker_single_file
Apr 25, 2025
Merged

sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:wasm_worker_single_file

Conversation

@sbc100

@sbc100 sbc100 commented Apr 21, 2025 •

Copy link
Copy Markdown
Collaborator

Similar to what we did for pthreads in #21701.

This is win for both code size and complexity. Importantly it avoid the issue how to package and deploy the extra .ww.js file.

This also completely removes the need for the -sWASM_WORKERS=2 mode.

@sbc100
sbc100 marked this pull request as draft April 21, 2025 22:48
@sbc100 sbc100 changed the title Inline ww.js file into the main output file [WIP] Inline ww.js file into the main output file Apr 21, 2025
@sbc100
sbc100 force-pushed the wasm_worker_single_file branch from 02fe7c8 to c823cd9 Compare April 22, 2025 00:08
@RReverser

Copy link
Copy Markdown
Collaborator

Very happy to see this happening, thanks for working on it!

I think conceptually .aw.js are also very similar, I gave a stab to trying to inline them before, but the amount of changes was getting out of hand (now I see why from this diff, it does require quite a bit of work). Do you think they could be also covered in or after this PR?

@sbc100

sbc100 commented Apr 22, 2025

Copy link
Copy Markdown
Collaborator Author

Very happy to see this happening, thanks for working on it!

I think conceptually .aw.js are also very similar, I gave a stab to trying to inline them before, but the amount of changes was getting out of hand (now I see why from this diff, it does require quite a bit of work). Do you think they could be also covered in or after this PR?

I think may be possible, but would likely be a followup.

@sbc100
sbc100 force-pushed the wasm_worker_single_file branch from c823cd9 to 65b40c8 Compare April 23, 2025 21:37
@sbc100 sbc100 changed the title [WIP] Inline ww.js file into the main output file Inline ww.js file into the main output file Apr 23, 2025
@sbc100
sbc100 requested review from RReverser, dschuff and kripken April 23, 2025 21:49
@sbc100 sbc100 changed the title Inline ww.js file into the main output file [WASM_WORKERS] Inline ww.js file into the main output file Apr 23, 2025
@sbc100
sbc100 marked this pull request as ready for review April 23, 2025 21:51
@sbc100
sbc100 force-pushed the wasm_worker_single_file branch 3 times, most recently from 7ba347e to b74a829 Compare April 23, 2025 21:55
Comment thread src/audio_worklet.js Resolved
Comment thread src/lib/libcore.js Resolved
Comment thread src/lib/libwasm_worker.js Resolved
Comment thread src/wasm_worker.js Resolved
@sbc100
sbc100 force-pushed the wasm_worker_single_file branch from b74a829 to 081ac97 Compare April 23, 2025 22:41
Comment thread src/wasm_worker.js Resolved
@sbc100
sbc100 force-pushed the wasm_worker_single_file branch 3 times, most recently from 61dd4db to 6d8ef34 Compare April 24, 2025 00:27

@RReverser RReverser left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is further cleanup that could be made here (eg replacing onmessage = with addEventListener / removeEventListener to merge more polyfills together) but those can be left for future PRs.

LGTM % CI fixes.

@juj

juj commented Apr 24, 2025 •

Copy link
Copy Markdown
Collaborator

What is the origin that the Worker gets loaded from after this change? Is it in the same origin? Also, does this retain csp-eval safety?

@RReverser

Copy link
Copy Markdown
Collaborator

@juj The worker is the main JS file itself, so the origin and CSP properties should remain the same, since you needed to load main JS before this PR as well (without it nothing works).

@sbc100
sbc100 force-pushed the wasm_worker_single_file branch 3 times, most recently from 8773c80 to 14a2674 Compare April 24, 2025 22:11
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 25, 2025
This is similar to what we already did for pthreads (emscripten-core#24163) and normal
wasm workers (emscripten-core#24163).  By integrating this file we can actually share
more startup code with normal wasm workers.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 25, 2025
This is similar to what we already did for pthreads (emscripten-core#24163) and normal
wasm workers (emscripten-core#24163).  By integrating this file we can actually share
more startup code with normal wasm workers.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 25, 2025
This is similar to what we already did for pthreads (emscripten-core#24163) and normal
wasm workers (emscripten-core#24163).  By integrating this file we can actually share
more startup code with normal wasm workers.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 25, 2025
This is similar to what we already did for pthreads (emscripten-core#24163) and normal
wasm workers (emscripten-core#24163).  By integrating this file we can actually share
more startup code with normal wasm workers.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 25, 2025
This is similar to what we already did for pthreads (emscripten-core#24163) and normal
wasm workers (emscripten-core#24163).  By integrating this file we can actually share
more startup code with normal wasm workers.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 25, 2025
This is similar to what we already did for pthreads (emscripten-core#24163) and normal
wasm workers (emscripten-core#24163).  By integrating this file we can actually share
more startup code with normal wasm workers.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 25, 2025
This is similar to what we already did for pthreads (emscripten-core#24163) and normal
wasm workers (emscripten-core#24163).  By integrating this file we can actually share
more startup code with normal wasm workers.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 28, 2025
This is similar to what we already did for pthreads (emscripten-core#24163) and normal
wasm workers (emscripten-core#24163).  By integrating this file we can actually share
more startup code with normal wasm workers.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 28, 2025
This is similar to what we already did for pthreads (emscripten-core#24163) and normal
wasm workers (emscripten-core#24163).  By integrating this file we can actually share
more startup code with normal wasm workers.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 28, 2025
This is similar to what we already did for pthreads (emscripten-core#24163) and normal
wasm workers (emscripten-core#24163).  By integrating this file we can actually share
more startup code with normal wasm workers.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 28, 2025
This is similar to what we already did for pthreads (emscripten-core#24163) and normal
wasm workers (emscripten-core#24163).  By integrating this file we can actually share
more startup code with normal wasm workers.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 28, 2025
This is similar to what we already did for pthreads (emscripten-core#24163) and normal
wasm workers (emscripten-core#24163).  By integrating this file we can actually share
more startup code with normal wasm workers.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 28, 2025
This is similar to what we already did for pthreads (emscripten-core#24163) and normal
wasm workers (emscripten-core#24163).  By integrating this file we can actually share
more startup code with normal wasm workers.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 28, 2025
This is similar to what we already did for pthreads (emscripten-core#24163) and normal
wasm workers (emscripten-core#24163).  By integrating this file we can actually share
more startup code with normal wasm workers.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 28, 2025
This is similar to what we already did for pthreads (emscripten-core#24163) and normal
wasm workers (emscripten-core#24163).  By integrating this file we can actually share
more startup code with normal wasm workers.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 28, 2025
This is similar to what we already did for pthreads (emscripten-core#24163) and normal
wasm workers (emscripten-core#24163).  By integrating this file we can actually share
more startup code with normal wasm workers.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 29, 2025
This is similar to what we already did for pthreads (emscripten-core#24163) and normal
wasm workers (emscripten-core#24163).  By integrating this file we can actually share
more startup code with normal wasm workers.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 29, 2025
This is similar to what we already did for pthreads (emscripten-core#24163) and normal
wasm workers (emscripten-core#24163).  By integrating this file we can actually share
more startup code with normal wasm workers.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 29, 2025
This is similar to what we already did for pthreads (emscripten-core#24163) and normal
wasm workers (emscripten-core#24163).  By integrating this file we can actually share
more startup code with normal wasm workers.
sbc100 added a commit that referenced this pull request Apr 29, 2025
…24190)

This is similar to what we already did for pthreads (#24163) and normal
wasm workers (#24163). By integrating this file we can also share
more startup code with normal wasm workers.
sbc100 added a commit to sbc100/emscripten that referenced this pull request May 1, 2025
Now that the wasm worker code is always in the same file as the main
JS code there is no need to quote or minify these names.  See emscripten-core#24163.
sbc100 added a commit to sbc100/emscripten that referenced this pull request May 1, 2025
Now that the wasm worker code is always in the same file as the main
JS code there is no need to quote or minify these names.  See emscripten-core#24163.
sbc100 added a commit to sbc100/emscripten that referenced this pull request May 1, 2025
Now that the wasm worker code is always in the same file as the main
JS code there is no need to quote or minify these names.  See emscripten-core#24163.
sbc100 added a commit to sbc100/emscripten that referenced this pull request May 1, 2025
Now that the wasm worker code is always in the same file as the main
JS code there is no need to quote or minify these names.  See emscripten-core#24163.
sbc100 added a commit to sbc100/emscripten that referenced this pull request May 5, 2025
Now that the wasm worker code is always in the same file as the main
JS code there is no need to quote or minify these names.  See emscripten-core#24163.
sbc100 added a commit that referenced this pull request May 5, 2025
Now that the wasm worker code is always in the same file as the main JS
code there is no need to quote or minify these names. See #24163.
juj added a commit to juj/emscripten that referenced this pull request Dec 2, 2026
In `-sMINIMAL_RUNTIME -sENVIRONMENT=web` builds the `Module` object is
expected to be provided by the shell HTML.  Wasm Workers and pthreads load
this same script in a Worker scope, where no such global exists:

- With `--closure=1`, `Module` was read with a bare `Module` reference, and
  the Worker failed at startup with `ReferenceError: Module is not defined`.
  For Wasm Workers this regressed in emscripten-core#24163, which removed the separate
  `.ww.js` bootstrap file that used to define `Module` in the Worker before
  loading the main script.
- In pthreads builds without closure, `Module` was left undefined, and the
  Worker failed with
  `TypeError: Cannot read properties of undefined (reading 'onExit')`.

Read the object as `self.Module` in closure builds with Wasm Workers or
pthreads, which yields undefined in the Worker instead of throwing, and
create the `Module` object at startup in pthreads builds.  Wasm Workers
already create it when the Worker starts.

Add a `--closure=1` variant of `browser.test_wasm_worker_hello`, and
`-sENVIRONMENT=web` variants of `browser.test_minimal_runtime_hello_thread`
with and without closure, all of which fail without this change.
juj added a commit to juj/emscripten that referenced this pull request Dec 2, 2026
In `-sMINIMAL_RUNTIME -sENVIRONMENT=web` builds the `Module` object is
expected to be provided by the shell HTML.  Wasm Workers and pthreads load
this same script in a Worker scope, where no such global exists:

- With `--closure=1`, `Module` was read with a bare `Module` reference, and
  the Worker failed at startup with `ReferenceError: Module is not defined`.
  For Wasm Workers this regressed in emscripten-core#24163, which removed the separate
  `.ww.js` bootstrap file that used to define `Module` in the Worker before
  loading the main script.
- In pthreads builds without closure, `Module` was left undefined, and the
  Worker failed with
  `TypeError: Cannot read properties of undefined (reading 'onExit')`.

Read the object as `self.Module` in closure builds with Wasm Workers or
pthreads, which yields undefined in the Worker instead of throwing, and
create the `Module` object at startup in pthreads builds.  Wasm Workers
already create it when the Worker starts.

Add a `--closure=1` variant of `browser.test_wasm_worker_hello`, and
`-sENVIRONMENT=web` variants of `browser.test_minimal_runtime_hello_thread`
with and without closure, all of which fail without this change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants