Skip to content

Fix embind exports with MODULARIZE=instance and AUTO_INIT#27414

Open
guybedford wants to merge 1 commit into
emscripten-core:mainfrom
guybedford:fix-auto-init-embind
Open

Fix embind exports with MODULARIZE=instance and AUTO_INIT#27414
guybedford wants to merge 1 commit into
emscripten-core:mainfrom
guybedford:fix-auto-init-embind

Conversation

@guybedford

@guybedford guybedford commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

This fixes embind ES module exports being undefined when using -sMODULARIZE=instance -sAUTO_INIT -sEMBIND_AOT, resolves #27411.

With AUTO_INIT the module self-initializes via top-level await init() in the postamble, but the embind AOT export snippet was appended after the postamble. Its addOnPostCtor(assignEmbindExports) therefore registered only after onPostCtors had already fired during init, so the export assignments never ran.

Following review feedback, rather than supporting late registration, the snippet is now emitted via a <<< EMBIND_AOT_EXPORTS >>> placeholder at the start of the MODULARIZE=instance postamble (same mechanism as EMBIND_AOT_INVOKERS), so registration always precedes initialization. addOnPostCtor additionally asserts under ASSERTIONS that it is not called after ctors have run, rather than silently never firing.

Adds other.test_modularize_instance_auto_init_embind reusing modularize_instance_embind.cpp, verified to fail before the fix and pass after; existing modularize=instance, ESM integration, dylink and codesize tests still pass (also manually verified WASM_ESM_INTEGRATION + AUTO_INIT + EMBIND_AOT under node 26).

Made with AI assistance under my review

@guybedford
guybedford force-pushed the fix-auto-init-embind branch from 2e2d59b to a9024eb Compare July 24, 2026 05:36
@guybedford guybedford changed the title Fix embind exports with MODULARIZE=instance and AUTO_INIT Support addOnPostCtor after runtime initialization Jul 24, 2026
@sbc100

sbc100 commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

I would hope that we don't need to do this (instead maybe we could make it an assertion if this function is called too late), and we can instread find a way to do the registration before startup. Otherwise we might be tempted to support other event handler beeing registered too late too.

@brendandahl WDYT?

@guybedford
guybedford force-pushed the fix-auto-init-embind branch from 18d6d56 to cefe794 Compare July 24, 2026 05:57
@guybedford

Copy link
Copy Markdown
Collaborator Author

I've updated the approach so postCtor events can only be registered before startup.

@guybedford
guybedford force-pushed the fix-auto-init-embind branch from cefe794 to 00c19f3 Compare July 24, 2026 05:58
With AUTO_INIT the embind AOT export snippet was appended after the
top-level `await init()`, so its addOnPostCtor callback registered
after onPostCtors had already fired, leaving the ES module exports
undefined.

Emit the snippet via a placeholder ahead of the init logic in the
postamble instead, and assert against late addOnPostCtor registration.

Fixes emscripten-core#27411
@guybedford
guybedford force-pushed the fix-auto-init-embind branch from 00c19f3 to 1beb18d Compare July 24, 2026 06:00
@guybedford guybedford changed the title Support addOnPostCtor after runtime initialization Fix embind exports with MODULARIZE=instance and AUTO_INIT Jul 24, 2026
@sbc100
sbc100 requested a review from brendandahl July 24, 2026 06:05
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.

AUTO_INIT doesn't initialize embind exports

2 participants