Skip to content

Commit c0c9d9f

Browse files
committed
Automate JS-to-JS library dependencies using Acorn
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`). Because `calculateDeps` uses a flat token scan rather than a full AST and scope analysis pass (both for speed and to support partial JS fragments in `--pre-js`/`--post-js`), this scanning is conservative and can include false positives if local variables or parameters shadow a library symbol name. 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). Also log auto-detected dependencies in debug/verbose mode (`-sVERBOSE`). 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) See: #19098
1 parent cc1b638 commit c0c9d9f

6 files changed

Lines changed: 236 additions & 120 deletions

File tree

‎ChangeLog.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ See docs/process.md for more on how version tagging works.
2626
of `clang`. Previously, `.dwo` files were named `<input>.dwo` and other
2727
auxiliary files were written to the temporary directory. (#28045)
2828
- libunwind and compiler-rt were updated to LLVM 23.1.3. (#28011, #28014)
29+
- JavaScript library dependencies on other JavaScript library symbols are now
30+
detected automatically by the compiler, so `__deps` is no longer required for
31+
direct JS-to-JS references (`__deps` is still supported for dependencies on
32+
compiled C/C++ symbols or indirect dependencies). (#27995)
2933
- mimalloc was updated to 3.5.3. (#28026)
3034

3135
6.0.13 - 10/19/26

‎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)