Skip to content

Commit 23ed469

Browse files
authored
Automate JS-to-JS library dependencies using Acorn (#27995)
Replace `addImplicitDeps` and `checkDependencies` in `src/jsifier.mjs` with `calculateDeps`, which uses `acorn.tokenizer` to automatically detect references to JS library symbols (`$foo` and `_bar`). Keep the `__deps` mechanism in place for dependencies that static JS token scanning cannot detect (such as native Wasm/C symbols, function callbacks in `__deps`, side-effect-only dependencies, and `__postset` dependencies). For system libraries we emit a warning when a `__deps` entry is redundant with an auto-detected dependency. For user JS libraries we only show this warning in `-sSTRICT` mode. Performance cost (median of 15 runs on `test/hello_world.c`): - `tools/compiler.mjs` (`INCLUDE_FULL_LIBRARY=0`): 178.8 ms -> 185.1 ms (+6.3 ms, +3.5% slower) - `tools/compiler.mjs` (`INCLUDE_FULL_LIBRARY=1`): 268.2 ms -> 343.1 ms (+74.9 ms, +27.9% slower) - `tools/compiler.mjs --symbols-only`: 181.4 ms -> 254.2 ms (+72.8 ms, +40.1% slower) - Full `emcc` link (`hello.o` -> `out.js`, cold JS cache): - `INCLUDE_FULL_LIBRARY=0`: 661.7 ms -> 740.8 ms (+79.1 ms, +12.0%) - `INCLUDE_FULL_LIBRARY=1`: 758.3 ms -> 906.0 ms (+147.7 ms, +19.5%) - Full `emcc` link (`hello.o` -> `out.js`, warm JS cache): - `INCLUDE_FULL_LIBRARY=0`: 271.8 ms (+0.0 ms, 0% slower) - `INCLUDE_FULL_LIBRARY=1`: 282.4 ms (+0.0 ms, 0% slower) Because I'm landing a few cleanup first (#27999, #28059, #28060) there are zero codesize changes acossiated with this PR. Fixes: #19098
1 parent c7391e8 commit 23ed469

84 files changed

Lines changed: 456 additions & 1859 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎ChangeLog.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ See docs/process.md for more on how version tagging works.
3232
- Restored `--whole-archive` behaviour when linking with `-sSIDE_MODULE=1`
3333
(fixing a regression from 6.0.0 where static archives were no longer included
3434
in full), while keeping native archive behaviour for `-shared`. (#28055)
35+
- JavaScript library dependencies on other JavaScript library symbols are now
36+
detected automatically by the compiler, so `__deps` is no longer required for
37+
direct JS-to-JS references (`__deps` is still supported for dependencies on
38+
compiled C/C++ symbols or indirect dependencies). (#27995)
3539

3640
6.0.13 - 10/19/26
3741
-----------------

‎site/source/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.rst‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -584,16 +584,15 @@ See the `library_*.js`_ files for other examples.
584584

585585
.. note::
586586

587-
- JavaScript libraries can declare dependencies (``__deps``), however
588-
those are only for other JavaScript libraries. See examples in
589-
`/src <https://github.com/emscripten-core/emscripten/tree/main/src>`_
590-
with the name format **library_*.js**
591-
- You can add dependencies for all your methods using
592-
``autoAddDeps(myLibrary, name)`` where myLibrary is the object with
593-
all your methods, and ``name`` is the thing they all depend upon.
594-
This is useful when all the implemented methods use a JavaScript
595-
singleton containing helper methods. See ``library_webgl.js`` for
596-
an example.
587+
- Direct dependencies between JavaScript library symbols are detected
588+
automatically by the compiler. Explicit dependency declarations
589+
(``__deps``) are only needed for compiled C/C++ functions or indirect
590+
dependencies that are not referenced directly in the symbol's body (such
591+
as ``$method_support`` above). See examples in
592+
`/src/lib <https://github.com/emscripten-core/emscripten/tree/main/src/lib>`_.
593+
- You can add an explicit dependency to all methods in a library using
594+
``autoAddDeps(myLibrary, name)``, where ``myLibrary`` is the object with
595+
all your methods and ``name`` is the symbol they all depend upon.
597596
- The keys passed into `addToLibrary` generate functions that are prefixed
598597
by ``_``. In other words ``my_func: function() {},`` becomes
599598
``function _my_func() {}``, as all C methods in emscripten have a ``_`` prefix. Keys starting with ``$`` have the ``$``

0 commit comments

Comments
 (0)