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

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

Conversation

@zacharywhitley

Copy link
Copy Markdown
Contributor

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`.
@alexcrichton

Copy link
Copy Markdown
Member

Thanks for the PR, and looks like a rustfmt is needed to get CI passing. Also please make sure to review our AI tool usage policy, for a change like this a small new function doesn't need such a large PR description

@alexcrichton
alexcrichton requested review from alexcrichton and removed request for dicej September 14, 2026 14:18
@zacharywhitley

Copy link
Copy Markdown
Contributor Author

Sorry about that. Damn things are a little too enthusiastic to post a PR before I can review it. I really wish they'd add something that would queue so it couldn't send them out before review. "Let me just go ahead and submit that PR.....beep bop-boop!" nooooo!!! :)

@cfallin

cfallin commented Sep 14, 2026

Copy link
Copy Markdown
Member

Sorry about that. Damn things are a little too enthusiastic to post a PR before I can review it. I really wish they'd add something that would queue so it couldn't send them out before review. "Let me just go ahead and submit that PR.....beep bop-boop!" nooooo!!! :)

FYI, just to make the point more explicit: automated posting of code that has not been reviewed by a human is even more against our policies than the lengthy LLM-generated description is. (In this case it's a trivial function, but the process is important here.)

I say this because your wording seems to imply somehow that this is not in your control ("I wish they'd add..."). I'd recommend either removing your bot's GitHub token or setting a hard blocker on creating PRs to Bytecode Alliance repos in whatever permissions system you have until you can work out why this slipped through. If we otherwise have an account that is posting AI-authored content, unreviewed by humans, with a human admitting they don't have full control of it, then this would have to go to the TSC for further action. Thanks!

@zacharywhitley

Copy link
Copy Markdown
Contributor Author

I appreciate that. I was just highlighting that it's far too easy to have happen and making a joke out of it

@zacharywhitley

Copy link
Copy Markdown
Contributor Author

On that note I think WebAssembly should be used more often for AI agent sanboxing. It could possibly have prevented the recent OpenAI/HF incident.

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.

3 participants