Skip to content

Commit 9675b32

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`). 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), and emit a warning when a system library `__deps` entry is redundant with an auto-detected dependency. Also log auto-detected dependencies in debug/verbose mode (`-sVERBOSE`). Remove all `autoAddDeps` calls and redundant `__deps` entries across `src/lib/*.js`. Also fix an unoptimized code size regression (~1 KB in `test_unoptimized_code_size`) where `wrapSyscallFunction` previously used a substring check (`t.includes('SYSCALLS')`) that matched a comment in `fd_write` even when `SYSCALLS_REQUIRE_FILESYSTEM=0`. 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) Fixes: #19098
1 parent 7b3808c commit 9675b32

108 files changed

Lines changed: 585 additions & 1953 deletions

File tree

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
@@ -20,6 +20,10 @@ See docs/process.md for more on how version tagging works.
2020

2121
6.0.13 (in development)
2222
-----------------------
23+
- JavaScript library dependencies on other JavaScript library symbols are now
24+
detected automatically by the compiler, so `__deps` is no longer required for
25+
direct JS-to-JS references (`__deps` is still supported for dependencies on
26+
compiled C/C++ symbols or indirect dependencies). (#27995)
2327
- Support for windows .bat/.ps1 launcher files was completely removed. These
2428
days we use a native windows launcher program. (#27359)
2529
- `MEMORY64=2` was marked as deprecated. (#27952)

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