Skip to content

wasm-encoder: add ComponentBuilder::instantiate_exports - #2655

Open
zacharywhitley wants to merge 1 commit into
bytecodealliance:mainfrom
zacharywhitley:feat/component-instantiate-exports
Open

wasm-encoder: add ComponentBuilder::instantiate_exports#2655
zacharywhitley wants to merge 1 commit into
bytecodealliance:mainfrom
zacharywhitley:feat/component-instantiate-exports

Conversation

@zacharywhitley

Copy link
Copy Markdown

The core-level analogue core_instantiate_exports already exists on
ComponentBuilder and mirrors the Instance::FromExports variant of
the core instance section. The component-level side had no equivalent:
ComponentInstanceSection::export_items is public but only reachable
through the manual section-append path, not the builder facade.

This adds instantiate_exports next to instantiate so callers
rebuilding a component structurally from a parsed one can round-trip
ComponentInstance::FromExports the same way core-level
Instance::FromExports already round-trips.

Signature

Mirrors core_instantiate_exports — same (&'a str, K, u32) triples,
just with ComponentExportKind in the kind slot instead of
ExportKind:

pub fn instantiate_exports<'a, E>(
    &mut self,
    debug_name: Option<&str>,
    exports: E,
) -> u32
where
    E: IntoIterator<Item = (&'a str, ComponentExportKind, u32)>,
    E::IntoIter: ExactSizeIterator,
{
    self.component_instances().export_items(exports);
    self.instances.add(debug_name)
}

Motivation

A component optimizer that reads a component with wasmparser and
re-emits it via ComponentBuilder needs to round-trip every
ComponentInstance variant. Instantiate was already covered by
ComponentBuilder::instantiate; FromExports was the missing case.
The public alternative was dropping down to
ComponentInstanceSection::export_items and manually appending the
section, bypassing the builder's index-space bookkeeping — which
defeats the point of using the builder.

Alternatives

  • Expose the builder's internal component_instances() accessor:
    wider surface area than needed and doesn't match the pattern
    established by core_instantiate_exports.
  • Require callers to use the raw section API: works but requires
    duplicating the builder's index-space accounting.

The chosen shape sticks to the precedent already set for the core
side.

@zacharywhitley
zacharywhitley requested a review from a team as a code owner September 12, 2026 12:16
@zacharywhitley
zacharywhitley requested review from dicej and removed request for a team September 12, 2026 12:16
The core-level analogue `core_instantiate_exports` already exists on
`ComponentBuilder` and mirrors the `Instance::FromExports` variant of
the core instance section. The component-level side had no equivalent:
`ComponentInstanceSection::export_items` is public but only reachable
through the manual section-append path, not the builder facade.

Add `instantiate_exports` next to `instantiate` so callers rebuilding
a component structurally from a parsed one can round-trip
`ComponentInstance::FromExports` the same way core-level
`Instance::FromExports` already round-trips.

The signature follows the section-level `ComponentInstanceSection::
export_items` — `N: Into<ComponentExternName<'a>>` in the name slot,
preserving the rich name form (`implements` / `version_suffix` /
`external_id`) an item can carry. `&'a str` satisfies the bound via
the existing `From<&'a str> for ComponentExternName<'a>`, so callers
that only need a plain name write the same call as with
`core_instantiate_exports`.
@zacharywhitley
zacharywhitley force-pushed the feat/component-instantiate-exports branch from f66f04b to 5c4e5a8 Compare September 12, 2026 12:29
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.

1 participant