From 6881e529ddbfad3dd1fed29572c4e16a005e3d0d Mon Sep 17 00:00:00 2001 From: Zachary Whitley Date: Sat, 12 Sep 2026 08:11:32 -0400 Subject: [PATCH] feat(wasm-encoder): add `ComponentBuilder::instantiate_exports` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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>` 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`. --- crates/wasm-encoder/src/component/builder.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/wasm-encoder/src/component/builder.rs b/crates/wasm-encoder/src/component/builder.rs index e7cf7baf8a..71603f8bd8 100644 --- a/crates/wasm-encoder/src/component/builder.rs +++ b/crates/wasm-encoder/src/component/builder.rs @@ -482,6 +482,19 @@ impl ComponentBuilder { base } + /// Creates a new component instance from the `exports` provided. + /// + /// Returns the index of the component instance created. + pub fn instantiate_exports<'a, E, N>(&mut self, debug_name: Option<&str>, exports: E) -> u32 + where + E: IntoIterator, + E::IntoIter: ExactSizeIterator, + N: Into>, + { + self.component_instances().export_items(exports); + self.instances.add(debug_name) + } + /// Declares a new `resource.drop` intrinsic. pub fn resource_drop(&mut self, ty: u32) -> u32 { self.canonical_functions().resource_drop(ty);