Skip to content

Emscripten: use __export/__force symbol attributes for public exports - #5253

Merged
guybedford merged 1 commit into
mainfrom
emscripten-export-attrs
Aug 6, 2026
Merged

guybedford merged 1 commit into
mainfrom
emscripten-export-attrs

Conversation

@guybedford

@guybedford guybedford commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

This updates emscripten JS-library generation to declare public exports through emscripten symbol attributes rather than mutating EXPORTED_FUNCTIONS at library load time, matching the symbol-attribute support added in emscripten-core/emscripten#27436.

Each public hoisted export now carries metadata on its own library symbol:

addToLibrary({
  $Counter: class Counter { ... },
  $Counter__deps: ['$initBindgen', '$CounterFinalization'],
  $Counter__export: true,
  $Counter__force: true,
});
  • Public functions, classes, enums, and namespace roots receive both __export and __force; private exports receive neither.
  • For namespaced APIs only the public namespace root carries the attributes; its private leaves stay reachable through the root's __deps.
  • $initBindgen is kept via __force: true instead of extraLibraryFuncs; its __deps continue to carry the global helper symbols.
  • All generated EXPORTED_FUNCTIONS.add(...) statements and the extraLibraryFuncs.push(...) are removed.
  • Existing __deps, __postset, finalizer, enum, import-shim, and Module[name] behavior is preserved.

Test coverage: the CLI expectations assert public symbols carry both attributes and private symbols neither, namespace roots vs leaves behave as above, and the generated library contains no EXPORTED_FUNCTIONS/extraLibraryFuncs references. The emscripten test harness now discovers exports from the __export attributes (with decorator filtering recognizing __export/__force) and preserves the factory-mode Module.<name> assertions.

Requires an emscripten with __export/__force symbol-attribute support. Once released, emscripten #27208 can bump its crate and CLI pins from 0.2.126.

Replaces the load-time EXPORTED_FUNCTIONS.add() self-registration and
extraLibraryFuncs force-keep with __export: true / __force: true
attributes on each public symbol's addToLibrary entry. $initBindgen is
kept via __force: true; private symbols and namespace leaves carry
neither attribute and remain reachable through __deps.
@guybedford
guybedford merged commit 297640c into main Aug 6, 2026
63 checks passed
@guybedford
guybedford deleted the emscripten-export-attrs branch August 6, 2026 17:26
guybedford added a commit to guybedford/emscripten that referenced this pull request Aug 7, 2026
wasm-bindgen (wasm-bindgen/wasm-bindgen#5210, updated for the __export/
__force symbol attributes from emscripten-core#27436 in wasm-bindgen/wasm-bindgen#5253)
hoists the clean exported API (functions, classes, enums) into top-level
library symbols that self-register as exports via its `--js-library`.
The user-facing API now comes from wasm-bindgen's library in every flow,
so emscripten no longer needs to guess or own the export set.

This collapses the previous staticlib-only handling into a single
-sWASM_BINDGEN path that works whether cargo/rustc drives the link (bin
crate, emcc as the linker, rustc supplies -sEXPORTED_FUNCTIONS) or emcc
drives it (staticlib, exports discovered locally):

- The exports the wasm-bindgen expansion reaches by name - the supplied
  EXPORTED_FUNCTIONS (method shims, the __wbindgen_* runtime, the marker,
  main) plus anything its expansion adds - are internal glue, not a user
  API. They are captured and kept off every export layer: the ESM wrapper
  (user_requested_exports), the factory Module attachment
  (EXPORTED_FUNCTIONS, via should_export), and the keepalive pass in
  finalize_wasm. `main` still runs automatically on init; `_main` isn't
  surfaced.
- A genuine EMSCRIPTEN_KEEPALIVE C/C++ export is not in that internal set
  and remains surfaced, so a hand-written native export composes with
  wasm-bindgen's API in the same module. Human-supplied EXPORTED_FUNCTIONS
  are not preserved through wasm-bindgen linkage yet (the rustc-supplied
  set is indistinguishable from glue); that can be revisited later.
- Strip the placeholder symbols wasm-bindgen consumes
  (__wbindgen_describe*, __externref_*, ...) so they aren't reported as
  undefined exports.
- Only run nm-based export discovery for explicit -sWASM_BINDGEN when no
  driver supplied EXPORTED_FUNCTIONS; the rustc-driven link already lists
  them exactly.
- Wire imported JS: feed library_bindgen.extern-pre.js as extern-pre-js
  and copy the snippets/ dir next to the output so relative imports
  resolve.
- The WASM_ESM_INTEGRATION wrapper re-exports the JS library symbols that
  were exported (MODULARIZE=instance), and provides wasmExports via a
  namespace import of the wasm so by-name export access works.

Add -sWASM_BINDGEN=auto: run wasm-bindgen only when the linked wasm
carries wasm-bindgen's __wasm_bindgen_emscripten_marker custom section,
which is how cargo/rustc opts in when driving emcc as the linker
(addressing the request to replace implicit marker detection with an
explicit flag); otherwise it is a no-op and wasm-bindgen need not be
installed.

Both output modes then expose only the clean API (e.g. a `Greeter`
class). Add an end-to-end test parameterized over the ESM and factory
output modes (built via cargo with -sWASM_BINDGEN=auto), a no-marker
test asserting auto is a no-op for an ordinary build, extend the
staticlib integration test to assert an EMSCRIPTEN_KEEPALIVE export
survives alongside the wasm-bindgen API, and install a pinned
wasm-bindgen-cli alongside rust in CI so the flow is always exercised.

The wasm-bindgen library and CLI are pinned to the same wasm-bindgen
main rev (the attribute support is not yet in a release); they must
match exactly, and can move to a version pin once released.
guybedford added a commit to guybedford/emscripten that referenced this pull request Aug 7, 2026
wasm-bindgen (wasm-bindgen/wasm-bindgen#5210, updated for the __export/
__force symbol attributes from emscripten-core#27436 in wasm-bindgen/wasm-bindgen#5253)
hoists the clean exported API (functions, classes, enums) into top-level
library symbols that self-register as exports via its `--js-library`.
The user-facing API now comes from wasm-bindgen's library in every flow,
so emscripten no longer needs to guess or own the export set.

This collapses the previous staticlib-only handling into a single
-sWASM_BINDGEN path that works whether cargo/rustc drives the link (bin
crate, emcc as the linker, rustc supplies -sEXPORTED_FUNCTIONS) or emcc
drives it (staticlib, exports discovered locally):

- The exports the wasm-bindgen expansion reaches by name - the supplied
  EXPORTED_FUNCTIONS (method shims, the __wbindgen_* runtime, the marker,
  main) plus anything its expansion adds - are internal glue, not a user
  API. They are captured and kept off every export layer: the ESM wrapper
  (user_requested_exports), the factory Module attachment
  (EXPORTED_FUNCTIONS, via should_export), and the keepalive pass in
  finalize_wasm. `main` still runs automatically on init; `_main` isn't
  surfaced.
- A genuine EMSCRIPTEN_KEEPALIVE C/C++ export is not in that internal set
  and remains surfaced, so a hand-written native export composes with
  wasm-bindgen's API in the same module. Human-supplied EXPORTED_FUNCTIONS
  are not preserved through wasm-bindgen linkage yet (the rustc-supplied
  set is indistinguishable from glue); that can be revisited later.
- Strip the placeholder symbols wasm-bindgen consumes
  (__wbindgen_describe*, __externref_*, ...) so they aren't reported as
  undefined exports.
- Only run nm-based export discovery for explicit -sWASM_BINDGEN when no
  driver supplied EXPORTED_FUNCTIONS; the rustc-driven link already lists
  them exactly.
- Wire imported JS: feed library_bindgen.extern-pre.js as extern-pre-js
  and copy the snippets/ dir next to the output so relative imports
  resolve.
- The WASM_ESM_INTEGRATION wrapper re-exports the JS library symbols that
  were exported (MODULARIZE=instance), and provides wasmExports via a
  namespace import of the wasm so by-name export access works.

Add -sWASM_BINDGEN=auto: run wasm-bindgen only when the linked wasm
carries wasm-bindgen's __wasm_bindgen_emscripten_marker custom section,
which is how cargo/rustc opts in when driving emcc as the linker
(addressing the request to replace implicit marker detection with an
explicit flag); otherwise it is a no-op and wasm-bindgen need not be
installed.

Both output modes then expose only the clean API (e.g. a `Greeter`
class). Add an end-to-end test parameterized over the ESM and factory
output modes (built via cargo with -sWASM_BINDGEN=auto), a no-marker
test asserting auto is a no-op for an ordinary build, extend the
staticlib integration test to assert an EMSCRIPTEN_KEEPALIVE export
survives alongside the wasm-bindgen API, and install a pinned
wasm-bindgen-cli alongside rust in CI so the flow is always exercised.

The wasm-bindgen library and CLI are pinned to the same wasm-bindgen
main rev (the attribute support is not yet in a release); they must
match exactly, and can move to a version pin once released.
guybedford added a commit to guybedford/emscripten that referenced this pull request Aug 7, 2026
wasm-bindgen (wasm-bindgen/wasm-bindgen#5210, updated for the __export/
__force symbol attributes from emscripten-core#27436 in wasm-bindgen/wasm-bindgen#5253)
hoists the clean exported API (functions, classes, enums) into top-level
library symbols that self-register as exports via its `--js-library`.
The user-facing API now comes from wasm-bindgen's library in every flow,
so emscripten no longer needs to guess or own the export set.

This collapses the previous staticlib-only handling into a single
-sWASM_BINDGEN path that works whether cargo/rustc drives the link (bin
crate, emcc as the linker, rustc supplies -sEXPORTED_FUNCTIONS) or emcc
drives it (staticlib, exports discovered locally):

- The exports the wasm-bindgen expansion reaches by name - the supplied
  EXPORTED_FUNCTIONS (method shims, the __wbindgen_* runtime, the marker,
  main) plus anything its expansion adds - are internal glue, not a user
  API. They are captured and kept off every export layer: the ESM wrapper
  (user_requested_exports), the factory Module attachment
  (EXPORTED_FUNCTIONS, via should_export), and the keepalive pass in
  finalize_wasm. `main` still runs automatically on init; `_main` isn't
  surfaced.
- A genuine EMSCRIPTEN_KEEPALIVE C/C++ export is not in that internal set
  and remains surfaced, so a hand-written native export composes with
  wasm-bindgen's API in the same module. Human-supplied EXPORTED_FUNCTIONS
  are not preserved through wasm-bindgen linkage yet (the rustc-supplied
  set is indistinguishable from glue); that can be revisited later.
- Strip the placeholder symbols wasm-bindgen consumes
  (__wbindgen_describe*, __externref_*, ...) so they aren't reported as
  undefined exports.
- Only run nm-based export discovery for explicit -sWASM_BINDGEN when no
  driver supplied EXPORTED_FUNCTIONS; the rustc-driven link already lists
  them exactly.
- Wire imported JS: feed library_bindgen.extern-pre.js as extern-pre-js
  and copy the snippets/ dir next to the output so relative imports
  resolve.
- The WASM_ESM_INTEGRATION wrapper re-exports the JS library symbols that
  were exported (MODULARIZE=instance), and provides wasmExports via a
  namespace import of the wasm so by-name export access works.

Add -sWASM_BINDGEN=auto: run wasm-bindgen only when the linked wasm
carries wasm-bindgen's __wasm_bindgen_emscripten_marker custom section,
which is how cargo/rustc opts in when driving emcc as the linker
(addressing the request to replace implicit marker detection with an
explicit flag); otherwise it is a no-op and wasm-bindgen need not be
installed.

Both output modes then expose only the clean API (e.g. a `Greeter`
class). Add an end-to-end test parameterized over the ESM and factory
output modes (built via cargo with -sWASM_BINDGEN=auto), a no-marker
test asserting auto is a no-op for an ordinary build, extend the
staticlib integration test to assert an EMSCRIPTEN_KEEPALIVE export
survives alongside the wasm-bindgen API, and install a pinned
wasm-bindgen-cli alongside rust in CI so the flow is always exercised.

The wasm-bindgen library and CLI are pinned to the same wasm-bindgen
main rev (the attribute support is not yet in a release); they must
match exactly, and can move to a version pin once released.
guybedford added a commit to guybedford/emscripten that referenced this pull request Aug 8, 2026
wasm-bindgen (wasm-bindgen/wasm-bindgen#5210, updated for the __export/
__force symbol attributes from emscripten-core#27436 in wasm-bindgen/wasm-bindgen#5253)
hoists the clean exported API (functions, classes, enums) into top-level
library symbols that self-register as exports via its `--js-library`.
The user-facing API now comes from wasm-bindgen's library in every flow,
so emscripten no longer needs to guess or own the export set.

This collapses the previous staticlib-only handling into a single
-sWASM_BINDGEN path that works whether cargo/rustc drives the link (bin
crate, emcc as the linker, rustc supplies -sEXPORTED_FUNCTIONS) or emcc
drives it (staticlib, exports discovered locally):

- The exports the wasm-bindgen expansion reaches by name - the supplied
  EXPORTED_FUNCTIONS (method shims, the __wbindgen_* runtime, the marker,
  main) plus anything its expansion adds - are internal glue, not a user
  API. They are captured and kept off every export layer: the ESM wrapper
  (user_requested_exports), the factory Module attachment
  (EXPORTED_FUNCTIONS, via should_export), and the keepalive pass in
  finalize_wasm. `main` still runs automatically on init; `_main` isn't
  surfaced.
- A genuine EMSCRIPTEN_KEEPALIVE C/C++ export is not in that internal set
  and remains surfaced, so a hand-written native export composes with
  wasm-bindgen's API in the same module. Human-supplied EXPORTED_FUNCTIONS
  are not preserved through wasm-bindgen linkage yet (the rustc-supplied
  set is indistinguishable from glue); that can be revisited later.
- Strip the placeholder symbols wasm-bindgen consumes
  (__wbindgen_describe*, __externref_*, ...) so they aren't reported as
  undefined exports.
- Only run nm-based export discovery for explicit -sWASM_BINDGEN when no
  driver supplied EXPORTED_FUNCTIONS; the rustc-driven link already lists
  them exactly.
- Wire imported JS: feed library_bindgen.extern-pre.js as extern-pre-js
  and copy the snippets/ dir next to the output so relative imports
  resolve.
- The WASM_ESM_INTEGRATION wrapper re-exports the JS library symbols that
  were exported (MODULARIZE=instance), and provides wasmExports via a
  namespace import of the wasm so by-name export access works.

Add -sWASM_BINDGEN=auto: run wasm-bindgen only when the linked wasm
carries wasm-bindgen's __wasm_bindgen_emscripten_marker custom section,
which is how cargo/rustc opts in when driving emcc as the linker
(addressing the request to replace implicit marker detection with an
explicit flag); otherwise it is a no-op and wasm-bindgen need not be
installed.

Both output modes then expose only the clean API (e.g. a `Greeter`
class). Add an end-to-end test parameterized over the ESM and factory
output modes (built via cargo with -sWASM_BINDGEN=auto), a no-marker
test asserting auto is a no-op for an ordinary build, extend the
staticlib integration test to assert an EMSCRIPTEN_KEEPALIVE export
survives alongside the wasm-bindgen API, and install a pinned
wasm-bindgen-cli alongside rust in CI so the flow is always exercised.

The wasm-bindgen library and CLI are pinned to the same wasm-bindgen
main rev (the attribute support is not yet in a release); they must
match exactly, and can move to a version pin once released.
guybedford added a commit to guybedford/emscripten that referenced this pull request Aug 8, 2026
wasm-bindgen (wasm-bindgen/wasm-bindgen#5210, updated for the __export/
__force symbol attributes from emscripten-core#27436 in wasm-bindgen/wasm-bindgen#5253)
hoists the clean exported API (functions, classes, enums) into top-level
library symbols that self-register as exports via its `--js-library`.
The user-facing API now comes from wasm-bindgen's library in every flow,
so emscripten no longer needs to guess or own the export set.

This collapses the previous staticlib-only handling into a single
-sWASM_BINDGEN path that works whether cargo/rustc drives the link (bin
crate, emcc as the linker, rustc supplies -sEXPORTED_FUNCTIONS) or emcc
drives it (staticlib, exports discovered locally):

- The exports the wasm-bindgen expansion reaches by name - the supplied
  EXPORTED_FUNCTIONS (method shims, the __wbindgen_* runtime, the marker,
  main) plus anything its expansion adds - are internal glue, not a user
  API. They are captured and kept off every export layer: the ESM wrapper
  (user_requested_exports), the factory Module attachment
  (EXPORTED_FUNCTIONS, via should_export), and the keepalive pass in
  finalize_wasm. `main` still runs automatically on init; `_main` isn't
  surfaced.
- A genuine EMSCRIPTEN_KEEPALIVE C/C++ export is not in that internal set
  and remains surfaced, so a hand-written native export composes with
  wasm-bindgen's API in the same module. Human-supplied EXPORTED_FUNCTIONS
  are not preserved through wasm-bindgen linkage yet (the rustc-supplied
  set is indistinguishable from glue); that can be revisited later.
- Strip the placeholder symbols wasm-bindgen consumes
  (__wbindgen_describe*, __externref_*, ...) so they aren't reported as
  undefined exports.
- Only run nm-based export discovery for explicit -sWASM_BINDGEN when no
  driver supplied EXPORTED_FUNCTIONS; the rustc-driven link already lists
  them exactly.
- Wire imported JS: feed library_bindgen.extern-pre.js as extern-pre-js
  and copy the snippets/ dir next to the output so relative imports
  resolve.
- The WASM_ESM_INTEGRATION wrapper re-exports the JS library symbols that
  were exported (MODULARIZE=instance), and provides wasmExports via a
  namespace import of the wasm so by-name export access works.

Add -sWASM_BINDGEN=auto: run wasm-bindgen only when the linked wasm
carries wasm-bindgen's __wasm_bindgen_emscripten_marker custom section,
which is how cargo/rustc opts in when driving emcc as the linker
(addressing the request to replace implicit marker detection with an
explicit flag); otherwise it is a no-op and wasm-bindgen need not be
installed.

Both output modes then expose only the clean API (e.g. a `Greeter`
class). Add an end-to-end test parameterized over the ESM and factory
output modes (built via cargo with -sWASM_BINDGEN=auto), a no-marker
test asserting auto is a no-op for an ordinary build, extend the
staticlib integration test to assert an EMSCRIPTEN_KEEPALIVE export
survives alongside the wasm-bindgen API, and install a pinned
wasm-bindgen-cli alongside rust in CI so the flow is always exercised.

The wasm-bindgen library and CLI are pinned to the same wasm-bindgen
main rev (the attribute support is not yet in a release); they must
match exactly, and can move to a version pin once released.
guybedford added a commit to guybedford/emscripten that referenced this pull request Aug 8, 2026
wasm-bindgen (wasm-bindgen/wasm-bindgen#5210, updated for the __export/
__force symbol attributes from emscripten-core#27436 in wasm-bindgen/wasm-bindgen#5253)
hoists the clean exported API (functions, classes, enums) into top-level
library symbols that self-register as exports via its `--js-library`.
The user-facing API now comes from wasm-bindgen's library in every flow,
so emscripten no longer needs to guess or own the export set.

This collapses the previous staticlib-only handling into a single
-sWASM_BINDGEN path that works whether cargo/rustc drives the link (bin
crate, emcc as the linker, rustc supplies -sEXPORTED_FUNCTIONS) or emcc
drives it (staticlib, exports discovered locally):

- The exports the wasm-bindgen expansion reaches by name - the supplied
  EXPORTED_FUNCTIONS (method shims, the __wbindgen_* runtime, the marker,
  main) plus anything its expansion adds - are internal glue, not a user
  API. They are captured and kept off every export layer: the ESM wrapper
  (user_requested_exports), the factory Module attachment
  (EXPORTED_FUNCTIONS, via should_export), and the keepalive pass in
  finalize_wasm. `main` still runs automatically on init; `_main` isn't
  surfaced.
- A genuine EMSCRIPTEN_KEEPALIVE C/C++ export is not in that internal set
  and remains surfaced, so a hand-written native export composes with
  wasm-bindgen's API in the same module. Human-supplied EXPORTED_FUNCTIONS
  are not preserved through wasm-bindgen linkage yet (the rustc-supplied
  set is indistinguishable from glue); that can be revisited later.
- Strip the placeholder symbols wasm-bindgen consumes
  (__wbindgen_describe*, __externref_*, ...) so they aren't reported as
  undefined exports.
- Only run nm-based export discovery for explicit -sWASM_BINDGEN when no
  driver supplied EXPORTED_FUNCTIONS; the rustc-driven link already lists
  them exactly.
- Wire imported JS: feed library_bindgen.extern-pre.js as extern-pre-js
  and copy the snippets/ dir next to the output so relative imports
  resolve.
- The WASM_ESM_INTEGRATION wrapper re-exports the JS library symbols that
  were exported (MODULARIZE=instance), and provides wasmExports via a
  namespace import of the wasm so by-name export access works.

Add -sWASM_BINDGEN=auto: run wasm-bindgen only when the linked wasm
carries wasm-bindgen's __wasm_bindgen_emscripten_marker custom section,
which is how cargo/rustc opts in when driving emcc as the linker
(addressing the request to replace implicit marker detection with an
explicit flag); otherwise it is a no-op and wasm-bindgen need not be
installed.

Both output modes then expose only the clean API (e.g. a `Greeter`
class). Add an end-to-end test parameterized over the ESM and factory
output modes (built via cargo with -sWASM_BINDGEN=auto), a no-marker
test asserting auto is a no-op for an ordinary build, extend the
staticlib integration test to assert an EMSCRIPTEN_KEEPALIVE export
survives alongside the wasm-bindgen API, and install a pinned
wasm-bindgen-cli alongside rust in CI so the flow is always exercised.

The wasm-bindgen library and CLI are pinned to the same wasm-bindgen
main rev (the attribute support is not yet in a release); they must
match exactly, and can move to a version pin once released.
guybedford added a commit to guybedford/emscripten that referenced this pull request Aug 10, 2026
wasm-bindgen (wasm-bindgen/wasm-bindgen#5210, updated for the __export/
__force symbol attributes from emscripten-core#27436 in wasm-bindgen/wasm-bindgen#5253)
hoists the clean exported API (functions, classes, enums) into top-level
library symbols that self-register as exports via its `--js-library`.
The user-facing API now comes from wasm-bindgen's library in every flow,
so emscripten no longer needs to guess or own the export set.

This collapses the previous staticlib-only handling into a single
-sWASM_BINDGEN path that works whether cargo/rustc drives the link (bin
crate, emcc as the linker, rustc supplies -sEXPORTED_FUNCTIONS) or emcc
drives it (staticlib, exports discovered locally):

- The exports the wasm-bindgen expansion reaches by name - the supplied
  EXPORTED_FUNCTIONS (method shims, the __wbindgen_* runtime, the marker,
  main) plus anything its expansion adds - are internal glue, not a user
  API. They are captured and kept off every export layer: the ESM wrapper
  (user_requested_exports), the factory Module attachment
  (EXPORTED_FUNCTIONS, via should_export), and the keepalive pass in
  finalize_wasm. `main` still runs automatically on init; `_main` isn't
  surfaced.
- A genuine EMSCRIPTEN_KEEPALIVE C/C++ export is not in that internal set
  and remains surfaced, so a hand-written native export composes with
  wasm-bindgen's API in the same module. Human-supplied EXPORTED_FUNCTIONS
  are not preserved through wasm-bindgen linkage yet (the rustc-supplied
  set is indistinguishable from glue); that can be revisited later.
- Strip the placeholder symbols wasm-bindgen consumes
  (__wbindgen_describe*, __externref_*, ...) so they aren't reported as
  undefined exports.
- Only run nm-based export discovery for explicit -sWASM_BINDGEN when no
  driver supplied EXPORTED_FUNCTIONS; the rustc-driven link already lists
  them exactly.
- Wire imported JS: feed library_bindgen.extern-pre.js as extern-pre-js
  and copy the snippets/ dir next to the output so relative imports
  resolve.
- The WASM_ESM_INTEGRATION wrapper re-exports the JS library symbols that
  were exported (MODULARIZE=instance), and provides wasmExports via a
  namespace import of the wasm so by-name export access works.

Add -sWASM_BINDGEN=auto: run wasm-bindgen only when the linked wasm
carries wasm-bindgen's __wasm_bindgen_emscripten_marker custom section,
which is how cargo/rustc opts in when driving emcc as the linker
(addressing the request to replace implicit marker detection with an
explicit flag); otherwise it is a no-op and wasm-bindgen need not be
installed.

Both output modes then expose only the clean API (e.g. a `Greeter`
class). Add an end-to-end test parameterized over the ESM and factory
output modes (built via cargo with -sWASM_BINDGEN=auto), a no-marker
test asserting auto is a no-op for an ordinary build, extend the
staticlib integration test to assert an EMSCRIPTEN_KEEPALIVE export
survives alongside the wasm-bindgen API, and install a pinned
wasm-bindgen-cli alongside rust in CI so the flow is always exercised.

The wasm-bindgen library and CLI are pinned to the same wasm-bindgen
main rev (the attribute support is not yet in a release); they must
match exactly, and can move to a version pin once released.
guybedford added a commit to guybedford/emscripten that referenced this pull request Aug 10, 2026
wasm-bindgen (wasm-bindgen/wasm-bindgen#5210, updated for the __export/
__force symbol attributes from emscripten-core#27436 in wasm-bindgen/wasm-bindgen#5253)
hoists the clean exported API (functions, classes, enums) into top-level
library symbols that self-register as exports via its `--js-library`.
The user-facing API now comes from wasm-bindgen's library in every flow,
so emscripten no longer needs to guess or own the export set.

This collapses the previous staticlib-only handling into a single
-sWASM_BINDGEN path that works whether cargo/rustc drives the link (bin
crate, emcc as the linker, rustc supplies -sEXPORTED_FUNCTIONS) or emcc
drives it (staticlib, exports discovered locally):

- The exports the wasm-bindgen expansion reaches by name - the supplied
  EXPORTED_FUNCTIONS (method shims, the __wbindgen_* runtime, the marker,
  main) plus anything its expansion adds - are internal glue, not a user
  API. They are captured and kept off every export layer: the ESM wrapper
  (user_requested_exports), the factory Module attachment
  (EXPORTED_FUNCTIONS, via should_export), and the keepalive pass in
  finalize_wasm. `main` still runs automatically on init; `_main` isn't
  surfaced.
- A genuine EMSCRIPTEN_KEEPALIVE C/C++ export is not in that internal set
  and remains surfaced, so a hand-written native export composes with
  wasm-bindgen's API in the same module. Human-supplied EXPORTED_FUNCTIONS
  are not preserved through wasm-bindgen linkage yet (the rustc-supplied
  set is indistinguishable from glue); that can be revisited later.
- Strip the placeholder symbols wasm-bindgen consumes
  (__wbindgen_describe*, __externref_*, ...) so they aren't reported as
  undefined exports.
- Only run nm-based export discovery for explicit -sWASM_BINDGEN when no
  driver supplied EXPORTED_FUNCTIONS; the rustc-driven link already lists
  them exactly.
- Wire imported JS: feed library_bindgen.extern-pre.js as extern-pre-js
  and copy the snippets/ dir next to the output so relative imports
  resolve.
- The WASM_ESM_INTEGRATION wrapper re-exports the JS library symbols that
  were exported (MODULARIZE=instance), and provides wasmExports via a
  namespace import of the wasm so by-name export access works.

Add -sWASM_BINDGEN=auto: run wasm-bindgen only when the linked wasm
carries wasm-bindgen's __wasm_bindgen_emscripten_marker custom section,
which is how cargo/rustc opts in when driving emcc as the linker
(addressing the request to replace implicit marker detection with an
explicit flag); otherwise it is a no-op and wasm-bindgen need not be
installed.

Both output modes then expose only the clean API (e.g. a `Greeter`
class). Add an end-to-end test parameterized over the ESM and factory
output modes (built via cargo with -sWASM_BINDGEN=auto), a no-marker
test asserting auto is a no-op for an ordinary build, extend the
staticlib integration test to assert an EMSCRIPTEN_KEEPALIVE export
survives alongside the wasm-bindgen API, and install a pinned
wasm-bindgen-cli alongside rust in CI so the flow is always exercised.

The wasm-bindgen library and CLI are pinned to the same wasm-bindgen
main rev (the attribute support is not yet in a release); they must
match exactly, and can move to a version pin once released.
guybedford added a commit to guybedford/emscripten that referenced this pull request Aug 29, 2026
wasm-bindgen (wasm-bindgen/wasm-bindgen#5210, updated for the __export/
__force symbol attributes from emscripten-core#27436 in wasm-bindgen/wasm-bindgen#5253)
hoists the clean exported API (functions, classes, enums) into top-level
library symbols that self-register as exports via its `--js-library`.
The user-facing API now comes from wasm-bindgen's library in every flow,
so emscripten no longer needs to guess or own the export set.

This collapses the previous staticlib-only handling into a single
-sWASM_BINDGEN path that works whether cargo/rustc drives the link (bin
crate, emcc as the linker, rustc supplies -sEXPORTED_FUNCTIONS) or emcc
drives it (staticlib, exports discovered locally):

- The exports the wasm-bindgen expansion reaches by name - the supplied
  EXPORTED_FUNCTIONS (method shims, the __wbindgen_* runtime, the marker,
  main) plus anything its expansion adds - are internal glue, not a user
  API. They are captured and kept off every export layer: the ESM wrapper
  (user_requested_exports), the factory Module attachment
  (EXPORTED_FUNCTIONS, via should_export), and the keepalive pass in
  finalize_wasm. `main` still runs automatically on init; `_main` isn't
  surfaced.
- A genuine EMSCRIPTEN_KEEPALIVE C/C++ export is not in that internal set
  and remains surfaced, so a hand-written native export composes with
  wasm-bindgen's API in the same module. Human-supplied EXPORTED_FUNCTIONS
  are not preserved through wasm-bindgen linkage yet (the rustc-supplied
  set is indistinguishable from glue); that can be revisited later.
- Strip the placeholder symbols wasm-bindgen consumes
  (__wbindgen_describe*, __externref_*, ...) so they aren't reported as
  undefined exports.
- Only run nm-based export discovery for explicit -sWASM_BINDGEN when no
  driver supplied EXPORTED_FUNCTIONS; the rustc-driven link already lists
  them exactly.
- Wire imported JS: feed library_bindgen.extern-pre.js as extern-pre-js
  and copy the snippets/ dir next to the output so relative imports
  resolve.
- The WASM_ESM_INTEGRATION wrapper re-exports the JS library symbols that
  were exported (MODULARIZE=instance), and provides wasmExports via a
  namespace import of the wasm so by-name export access works.

Add -sWASM_BINDGEN=auto: run wasm-bindgen only when the linked wasm
carries wasm-bindgen's __wasm_bindgen_emscripten_marker custom section,
which is how cargo/rustc opts in when driving emcc as the linker
(addressing the request to replace implicit marker detection with an
explicit flag); otherwise it is a no-op and wasm-bindgen need not be
installed.

Both output modes then expose only the clean API (e.g. a `Greeter`
class). Add an end-to-end test parameterized over the ESM and factory
output modes (built via cargo with -sWASM_BINDGEN=auto), a no-marker
test asserting auto is a no-op for an ordinary build, extend the
staticlib integration test to assert an EMSCRIPTEN_KEEPALIVE export
survives alongside the wasm-bindgen API, and install a pinned
wasm-bindgen-cli alongside rust in CI so the flow is always exercised.

The wasm-bindgen library and CLI are pinned to the same wasm-bindgen
main rev (the attribute support is not yet in a release); they must
match exactly, and can move to a version pin once released.
guybedford added a commit to guybedford/emscripten that referenced this pull request Sep 3, 2026
wasm-bindgen (wasm-bindgen/wasm-bindgen#5210, updated for the __export/
__force symbol attributes from emscripten-core#27436 in wasm-bindgen/wasm-bindgen#5253)
hoists the clean exported API (functions, classes, enums) into top-level
library symbols that self-register as exports via its `--js-library`.
The user-facing API now comes from wasm-bindgen's library in every flow,
so emscripten no longer needs to guess or own the export set.

This collapses the previous staticlib-only handling into a single
-sWASM_BINDGEN path that works whether cargo/rustc drives the link (bin
crate, emcc as the linker, rustc supplies -sEXPORTED_FUNCTIONS) or emcc
drives it (staticlib, exports discovered locally):

- The exports the wasm-bindgen expansion reaches by name - the supplied
  EXPORTED_FUNCTIONS (method shims, the __wbindgen_* runtime, the marker,
  main) plus anything its expansion adds - are internal glue, not a user
  API. They are captured and kept off every export layer: the ESM wrapper
  (user_requested_exports), the factory Module attachment
  (EXPORTED_FUNCTIONS, via should_export), and the keepalive pass in
  finalize_wasm. `main` still runs automatically on init; `_main` isn't
  surfaced.
- A genuine EMSCRIPTEN_KEEPALIVE C/C++ export is not in that internal set
  and remains surfaced, so a hand-written native export composes with
  wasm-bindgen's API in the same module. Human-supplied EXPORTED_FUNCTIONS
  are not preserved through wasm-bindgen linkage yet (the rustc-supplied
  set is indistinguishable from glue); that can be revisited later.
- Strip the placeholder symbols wasm-bindgen consumes
  (__wbindgen_describe*, __externref_*, ...) so they aren't reported as
  undefined exports.
- Only run nm-based export discovery for explicit -sWASM_BINDGEN when no
  driver supplied EXPORTED_FUNCTIONS; the rustc-driven link already lists
  them exactly.
- Wire imported JS: feed library_bindgen.extern-pre.js as extern-pre-js
  and copy the snippets/ dir next to the output so relative imports
  resolve.
- The WASM_ESM_INTEGRATION wrapper re-exports the JS library symbols that
  were exported (MODULARIZE=instance), and provides wasmExports via a
  namespace import of the wasm so by-name export access works.

Add -sWASM_BINDGEN=auto: run wasm-bindgen only when the linked wasm
carries wasm-bindgen's __wasm_bindgen_emscripten_marker custom section,
which is how cargo/rustc opts in when driving emcc as the linker
(addressing the request to replace implicit marker detection with an
explicit flag); otherwise it is a no-op and wasm-bindgen need not be
installed.

Both output modes then expose only the clean API (e.g. a `Greeter`
class). Add an end-to-end test parameterized over the ESM and factory
output modes (built via cargo with -sWASM_BINDGEN=auto), a no-marker
test asserting auto is a no-op for an ordinary build, extend the
staticlib integration test to assert an EMSCRIPTEN_KEEPALIVE export
survives alongside the wasm-bindgen API, and install a pinned
wasm-bindgen-cli alongside rust in CI so the flow is always exercised.

The wasm-bindgen library and CLI are pinned to the same wasm-bindgen
main rev (the attribute support is not yet in a release); they must
match exactly, and can move to a version pin once released.
guybedford added a commit to guybedford/emscripten that referenced this pull request Sep 4, 2026
wasm-bindgen (wasm-bindgen/wasm-bindgen#5210, updated for the __export/
__force symbol attributes from emscripten-core#27436 in wasm-bindgen/wasm-bindgen#5253)
hoists the clean exported API (functions, classes, enums) into top-level
library symbols that self-register as exports via its `--js-library`.
The user-facing API now comes from wasm-bindgen's library in every flow,
so emscripten no longer needs to guess or own the export set.

This collapses the previous staticlib-only handling into a single
-sWASM_BINDGEN path that works whether cargo/rustc drives the link (bin
crate, emcc as the linker, rustc supplies -sEXPORTED_FUNCTIONS) or emcc
drives it (staticlib, exports discovered locally):

- The exports the wasm-bindgen expansion reaches by name - the supplied
  EXPORTED_FUNCTIONS (method shims, the __wbindgen_* runtime, the marker,
  main) plus anything its expansion adds - are internal glue, not a user
  API. They are captured and kept off every export layer: the ESM wrapper
  (user_requested_exports), the factory Module attachment
  (EXPORTED_FUNCTIONS, via should_export), and the keepalive pass in
  finalize_wasm. `main` still runs automatically on init; `_main` isn't
  surfaced.
- A genuine EMSCRIPTEN_KEEPALIVE C/C++ export is not in that internal set
  and remains surfaced, so a hand-written native export composes with
  wasm-bindgen's API in the same module. Human-supplied EXPORTED_FUNCTIONS
  are not preserved through wasm-bindgen linkage yet (the rustc-supplied
  set is indistinguishable from glue); that can be revisited later.
- Strip the placeholder symbols wasm-bindgen consumes
  (__wbindgen_describe*, __externref_*, ...) so they aren't reported as
  undefined exports.
- Only run nm-based export discovery for explicit -sWASM_BINDGEN when no
  driver supplied EXPORTED_FUNCTIONS; the rustc-driven link already lists
  them exactly.
- Wire imported JS: feed library_bindgen.extern-pre.js as extern-pre-js
  and copy the snippets/ dir next to the output so relative imports
  resolve.
- The WASM_ESM_INTEGRATION wrapper re-exports the JS library symbols that
  were exported (MODULARIZE=instance), and provides wasmExports via a
  namespace import of the wasm so by-name export access works.

Add -sWASM_BINDGEN=auto: run wasm-bindgen only when the linked wasm
carries wasm-bindgen's __wasm_bindgen_emscripten_marker custom section,
which is how cargo/rustc opts in when driving emcc as the linker
(addressing the request to replace implicit marker detection with an
explicit flag); otherwise it is a no-op and wasm-bindgen need not be
installed.

Both output modes then expose only the clean API (e.g. a `Greeter`
class). Add an end-to-end test parameterized over the ESM and factory
output modes (built via cargo with -sWASM_BINDGEN=auto), a no-marker
test asserting auto is a no-op for an ordinary build, extend the
staticlib integration test to assert an EMSCRIPTEN_KEEPALIVE export
survives alongside the wasm-bindgen API, and install a pinned
wasm-bindgen-cli alongside rust in CI so the flow is always exercised.

The wasm-bindgen library and CLI are pinned to the same wasm-bindgen
main rev (the attribute support is not yet in a release); they must
match exactly, and can move to a version pin once released.
sbc100 pushed a commit to emscripten-core/emscripten that referenced this pull request Sep 5, 2026
…ction (#27208)

This unifies wasm-bindgen output generation under a single
`-sWASM_BINDGEN` path, building on the wasm-bindgen side change in
wasm-bindgen/wasm-bindgen#5210. It supersedes #27179.

Previously `-sWASM_BINDGEN` was effectively a staticlib-only flow where
emcc had to discover and own the export set itself. With
wasm-bindgen/wasm-bindgen#5210, wasm-bindgen hoists its clean exported
API (functions, classes, enums) into top-level library symbols that
self-register as public exports via the `__export`/`__force` symbol
attributes (#27436, wasm-bindgen/wasm-bindgen#5253) in its
`--js-library`. The user-facing API now comes from wasm-bindgen's own
library in every flow, so emscripten no longer needs to guess or own
exports — it just surfaces what wasm-bindgen registered. That lets one
`-sWASM_BINDGEN` path serve both whether cargo/rustc drives the link
(bin crate, emcc as the linker, rustc supplies `-sEXPORTED_FUNCTIONS`)
or emcc drives it (staticlib, exports discovered locally).

The model for a public export is the union across both systems:
wasm-bindgen's self-registered API, plus emscripten's own
`EMSCRIPTEN_KEEPALIVE` exports — minus wasm-bindgen's internal expansion
glue, which must not spill into the public surface.

What's implemented:

* `-sWASM_BINDGEN` is a no-op unless the linker inputs carry
wasm-bindgen's `__wasm_bindgen_emscripten_marker` custom section
(emitted by the wasm-bindgen crate). The check runs on the inputs before
the link (`llvm-objdump --section-headers`, so archive members are
covered), so there is a single link and no speculative exports. When the
marker is absent wasm-bindgen is never invoked and need not be
installed, so cargo/rustc can pass the flag unconditionally via
`-Clink-arg` without affecting ordinary builds.
* The exports the wasm-bindgen expansion reaches by name — the supplied
`EXPORTED_FUNCTIONS` (method shims, the `__wbindgen_*` runtime, the
marker, `main`) plus anything its expansion adds — are captured as
internal glue and kept off every export layer: the ESM wrapper
(`user_requested_exports`), the factory `Module` attachment
(`EXPORTED_FUNCTIONS`, via `should_export`), and the keepalive pass in
`finalize_wasm`. `main` still runs automatically on init; `_main` isn't
surfaced.
* A genuine `EMSCRIPTEN_KEEPALIVE` C/C++ export is **not** in that
internal set and remains surfaced, so a hand-written native export
composes with wasm-bindgen's API in the same module.
* Placeholder symbols wasm-bindgen consumes (`__wbindgen_describe*`,
`__externref_*`, ...) are stripped so they aren't reported as undefined
exports.
* nm-based export discovery only runs when no driver supplied
`EXPORTED_FUNCTIONS`; the rustc-driven link already lists them exactly.
* Imported JS is wired through: `library_bindgen.extern-pre.js` is fed
as extern-pre-js and the `snippets/` dir is copied next to the output so
relative imports resolve.
* Under `WASM_ESM_INTEGRATION`, `wasmExports` is provided via a
namespace import of the wasm so wasm-bindgen's by-name export access
works. This is gated behind `WASM_BINDGEN` so plain ESM integration
keeps per-symbol named imports and their tree-shakability. (The wrapper
re-export of self-registered JS library symbols itself landed in
#27436.)

Both output modes then expose only the clean API (e.g. a `Greeter`
class).

Testing:

* End-to-end test parameterized over the ESM (`-sWASM_ESM_INTEGRATION`)
and factory (`-sMODULARIZE -sEXPORT_ES6`) output modes, built via `cargo
build` with `-sWASM_BINDGEN` passed as a link arg, asserting the clean
`Greeter` API works, `main` runs on init, and none of the raw wasm
exports leak.
* A no-marker test asserting `-sWASM_BINDGEN` is a no-op for an ordinary
`hello_world.c` build (doesn't require wasm-bindgen installed).
* The staticlib integration test is extended to assert an
`EMSCRIPTEN_KEEPALIVE` native export survives alongside the wasm-bindgen
API (fails under a blanket export wipe).
* CI installs a pinned `wasm-bindgen-cli` alongside rust so the flow is
always exercised.

Not in scope (follow-up): preserving human-supplied `EXPORTED_FUNCTIONS`
through wasm-bindgen linkage (the rustc-supplied set is currently
indistinguishable from glue, so explicit exports aren't surfaced).
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