Skip to content

Emit source phase import for MODULARIZE=instance#27237

Open
guybedford wants to merge 2 commits into
emscripten-core:mainfrom
guybedford:esm-source-phase-instance
Open

Emit source phase import for MODULARIZE=instance#27237
guybedford wants to merge 2 commits into
emscripten-core:mainfrom
guybedford:esm-source-phase-instance

Conversation

@guybedford

Copy link
Copy Markdown
Collaborator

This makes -sSOURCE_PHASE_IMPORTS work together with -sMODULARIZE=instance (and EXPORT_ES6).

In MODULARIZE=instance mode the output is not wrapped by modularize(), so the top-level import source wasmModule statement that src/modularize.js normally emits was never produced. This left preamble.js referencing an undefined wasmModule, breaking source phase imports in instance mode.

Since instance mode output is itself a module, the source phase import is now emitted directly from postamble.js at module scope when both settings are enabled:

#if SOURCE_PHASE_IMPORTS && MODULARIZE == 'instance'
import source wasmModule from './{{{ WASM_BINARY_FILE }}}';
#endif

Adds a parameterized test_esm_source_phase_imports_instance (default and -O3) that builds with -sSOURCE_PHASE_IMPORTS -sMODULARIZE=instance and verifies the import is emitted and the module runs.

Comment thread test/test_other.py Outdated
Comment thread src/postamble.js Outdated
#if SOURCE_PHASE_IMPORTS && MODULARIZE == 'instance'
// In MODULARIZE=instance mode the output is not wrapped by modularize.js, so the
// source phase import (normally emitted there) is emitted here at module scope.
import source wasmModule from './{{{ WASM_BINARY_FILE }}}';

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.

Can we remove the import source wasmModule from modularize.js maybe and just have a single one here?

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.

Also should we put this in preamble.js instead? since that seems to be where it is used? (Also, var wasmModule is alreayd declared there in threaded mode.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Moved to the preamble.js next to wasmModule.

We can't remove the import source wasmModule from modularize.js since these are distinct initialization shapes - modularize.js uses a factory function, while MODULARIZE=instance skips the modularize.js entirely since it doesn't use a factory function.

In MODULARIZE=instance mode the modularize() wrapper is not applied, so the
top-level 'import source wasmModule' statement in src/modularize.js was never
emitted, leaving preamble.js referencing an undefined wasmModule. Emit the
source phase import from preamble.js (module scope in instance mode), next to
where wasmModule is declared and used, so that -sSOURCE_PHASE_IMPORTS works
alongside -sMODULARIZE=instance/EXPORT_ES6.
@guybedford guybedford force-pushed the esm-source-phase-instance branch from 103e1cd to 476868d Compare July 2, 2026 17:03
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