Skip to content

Emit JS library aliases after non-aliases - #28032

Merged
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:deferred-aliases
Oct 20, 2026
Merged

sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:deferred-aliases

Conversation

@sbc100

@sbc100 sbc100 commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

When a JS library alias target (e.g. foo) is visited first and
transitively depends on its own alias (e.g. foo_alias: 'foo'),
addedLibraryItems['foo'] is marked as visited before var _foo = ...
is emitted. Emitting var _foo_alias = _foo; inline during dependency
traversal causes it to appear before var _foo = ..., leaving
_foo_alias initialized to undefined for arrow functions and
non-hoisted values.

Collect JS library alias assignments in libraryAliases and emit them
after libraryItems (and before postSets) so all target symbols are
defined before any alias assignment runs.

Split out from #27995 where this issue was first spotted.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a mechanism to handle dependency cycles involving aliases in src/jsifier.mjs by deferring the emission of alias definitions until after their targets are initialized, and adds a test case in test/test_jslib.py to verify this behavior. The feedback suggests moving the newly added emittedLibraryItems and deferredAliases variables from the module scope into the runJSify function scope to avoid shared mutable state and potential correctness issues during multiple executions.

Comment thread src/jsifier.mjs Outdated
@sbc100 sbc100 changed the title Defer emitting JS library aliases when target is in a dependency cycle Emit JS library aliases after non-alias library items Oct 20, 2026
@sbc100

sbc100 commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces tracking for library aliases separately from library items in src/jsifier.mjs to ensure they are emitted in the correct order, specifically after their targets. It also updates test/test_jslib.py to add a test case verifying that aliases are correctly emitted even when a dependency cycle exists. There are no review comments to address, and I have no additional feedback to provide.

@sbc100
sbc100 force-pushed the deferred-aliases branch 2 times, most recently from c3e22dc to ecfac0e Compare October 20, 2026 19:02
@sbc100 sbc100 changed the title Emit JS library aliases after non-alias library items Emit JS library aliases after non-aliases Oct 20, 2026
@sbc100
sbc100 requested review from dschuff and kripken October 20, 2026 19:03
When a JS library alias target (e.g. `foo`) is visited first and
transitively depends on its own alias (e.g. `foo_alias: 'foo'`),
`addedLibraryItems['foo']` is marked as visited before `var _foo = ...`
is emitted. Emitting `var _foo_alias = _foo;` inline during dependency
traversal causes it to appear before `var _foo = ...`, leaving
`_foo_alias` initialized to `undefined` for arrow functions and
non-hoisted values.

Collect JS library alias assignments in `libraryAliases` and emit them
after `libraryItems` (and before `postSets`) so all target symbols are
defined before any alias assignment runs.
@sbc100
sbc100 enabled auto-merge (squash) October 20, 2026 20:48
@sbc100
sbc100 merged commit d7642b5 into emscripten-core:main Oct 20, 2026
42 checks passed
@sbc100
sbc100 deleted the deferred-aliases branch October 20, 2026 21:50
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.

2 participants