pub struct Component<'a> {Show 14 fields
pub id: ComponentId,
pub components: AppendOnlyVec<Component<'a>>,
pub modules: AppendOnlyVec<Module<'a>>,
pub component_types: ComponentTypes<'a>,
pub component_instance: AppendOnlyVec<ComponentInstance<'a>>,
pub canons: Canons,
pub alias: Aliases<'a>,
pub imports: AppendOnlyVec<ComponentImport<'a>>,
pub exports: AppendOnlyVec<ComponentExport<'a>>,
pub core_types: AppendOnlyVec<Box<CoreType<'a>>>,
pub instances: AppendOnlyVec<Instance<'a>>,
pub custom_sections: CustomSections<'a>,
pub start_section: AppendOnlyVec<ComponentStartFunction>,
pub sections: Vec<(u32, ComponentSection)>,
/* private fields */
}Expand description
Intermediate Representation of a wasm component.
Fields§
§id: ComponentId§components: AppendOnlyVec<Component<'a>>Nested Components
modules: AppendOnlyVec<Module<'a>>Modules
component_types: ComponentTypes<'a>Component Types
component_instance: AppendOnlyVec<ComponentInstance<'a>>Component Instances
canons: CanonsCanons
alias: Aliases<'a>Alias
imports: AppendOnlyVec<ComponentImport<'a>>Imports
exports: AppendOnlyVec<ComponentExport<'a>>Exports
core_types: AppendOnlyVec<Box<CoreType<'a>>>Core Types
instances: AppendOnlyVec<Instance<'a>>Core Instances
custom_sections: CustomSections<'a>Custom sections
start_section: AppendOnlyVec<ComponentStartFunction>Component Start Section
sections: Vec<(u32, ComponentSection)>Sections of the Component. Represented as (#num of occurrences of a section, type of section)
Implementations§
Source§impl<'a> Component<'a>
impl<'a> Component<'a>
Sourcepub fn concretize_import(&'a self, name: &str) -> Option<ConcreteType<'a>>
pub fn concretize_import(&'a self, name: &str) -> Option<ConcreteType<'a>>
Resolve an import by name to its fully-concrete type.
Follows all alias chains, outer references, and index lookups so that
the returned ConcreteType contains no remaining index references.
Returns None if no import with the given name exists, or if its type
is not one wirm currently concretizes (e.g. a raw module import).
Sourcepub fn concretize_export(&'a self, name: &str) -> Option<ConcreteType<'a>>
pub fn concretize_export(&'a self, name: &str) -> Option<ConcreteType<'a>>
Resolve an export by name to its fully-concrete type.
Follows all alias chains, outer references, and index lookups so that
the returned ConcreteType contains no remaining index references.
Returns None if no export with the given name exists, or if its type
is not one wirm currently concretizes.
Source§impl<'a> Component<'a>
impl<'a> Component<'a>
Sourcepub fn emit_wasm(&self, file_name: &str) -> Result<()>
pub fn emit_wasm(&self, file_name: &str) -> Result<()>
Emit the Component into a wasm binary file.
Sourcepub fn add_module(&mut self, module: Module<'a>) -> ModuleID
pub fn add_module(&mut self, module: Module<'a>) -> ModuleID
Add a Module to this Component.
Sourcepub fn add_globals(&mut self, global: Global, module_idx: ModuleID) -> GlobalID
pub fn add_globals(&mut self, global: Global, module_idx: ModuleID) -> GlobalID
Add a Global to this Component.
pub fn add_custom_section( &mut self, section: CustomSection<'a>, ) -> CustomSectionID
Sourcepub fn add_import(&mut self, import: ComponentImport<'a>) -> u32
pub fn add_import(&mut self, import: ComponentImport<'a>) -> u32
Add an Import to this Component.
Sourcepub fn add_alias_func(
&mut self,
alias: ComponentAlias<'a>,
) -> (AliasFuncId, AliasId)
pub fn add_alias_func( &mut self, alias: ComponentAlias<'a>, ) -> (AliasFuncId, AliasId)
Add an Aliased function to this Component.
Sourcepub fn add_alias_core_memory(
&mut self,
alias: ComponentAlias<'a>,
) -> (AliasMemId, AliasId)
pub fn add_alias_core_memory( &mut self, alias: ComponentAlias<'a>, ) -> (AliasMemId, AliasId)
Add an Aliased core memory to this Component.
Sourcepub fn add_canon_func(&mut self, canon: CanonicalFunction) -> CanonicalFuncId
pub fn add_canon_func(&mut self, canon: CanonicalFunction) -> CanonicalFuncId
Add a Canonical Function to this Component.
Sourcepub fn add_type_instance(
&mut self,
decls: Vec<InstanceTypeDeclaration<'a>>,
) -> (ComponentTypeInstanceId, ComponentTypeId)
pub fn add_type_instance( &mut self, decls: Vec<InstanceTypeDeclaration<'a>>, ) -> (ComponentTypeInstanceId, ComponentTypeId)
Add a Component Type that is an Instance to this component.
Sourcepub fn add_type_func(
&mut self,
ty: ComponentFuncType<'a>,
) -> (ComponentTypeFuncId, ComponentTypeId)
pub fn add_type_func( &mut self, ty: ComponentFuncType<'a>, ) -> (ComponentTypeFuncId, ComponentTypeId)
Add a Component Type that is a Function to this component.
Sourcepub fn add_core_instance(&mut self, instance: Instance<'a>) -> CoreInstanceId
pub fn add_core_instance(&mut self, instance: Instance<'a>) -> CoreInstanceId
Add a new core instance to this component.
Sourcepub fn parse(
wasm: &[u8],
enable_multi_memory: bool,
with_offsets: bool,
) -> Result<Component<'_>, Error>
pub fn parse( wasm: &[u8], enable_multi_memory: bool, with_offsets: bool, ) -> Result<Component<'_>, Error>
Parse a Component from a wasm binary.
Set enable_multi_memory to true to support parsing modules using multiple memories.
Set with_offsets to true to save opcode pc offset metadata during parsing
(can be used to determine the static pc offset inside a function body of the start of any opcode).
§Example
use wirm::Component;
let file = "path_to_file";
let buff = wat::parse_file(file).expect("couldn't convert the input wat to Wasm");
let comp = Component::parse(&buff, false, false).unwrap();Sourcepub fn encode(&self) -> Result<Vec<u8>>
pub fn encode(&self) -> Result<Vec<u8>>
Encode this component into a WebAssembly binary.
This method performs encoding in three high-level phases:
- Collect – Walks the component IR and records all items that will be encoded, along with any index references that need to be rewritten.
- Assign – Resolves all index references after instrumentation so that every reference points to its final concrete index.
- Encode – Emits the final WebAssembly binary using the resolved indices.
§Nested Components
Components may be arbitrarily nested. During traversal, the encoder maintains a stack of component IDs that tracks which component is currently being visited. A registry maps component IDs to their corresponding components, allowing the encoder to correctly resolve cross-component references.
§Scoped Definitions
Many IR nodes introduce scopes (such as types, instances, or component-local definitions). To support correct index resolution in the presence of deep nesting and instrumentation, the encoder tracks scopes explicitly:
- A scope stack is maintained during traversal.
- Each scoped IR node is registered by identity in a scope registry.
- Index lookups can recover the correct scope for any IR node in O(1) time.
This design allows instrumentation to safely insert, reorder, or modify nodes without breaking encoding invariants.
§Panics
Panics if encoding encounters an internal inconsistency, such as:
- An index reference that cannot be resolved to a registered scope
- A malformed or structurally invalid component
- Violations of encoding invariants introduced by instrumentation
These panics indicate a bug in the encoder or in transformations applied to the component prior to encoding, rather than a recoverable runtime error.
§Example
use wirm::Component;
let file = "path/to/file.wasm";
let buff = wat::parse_file(file).expect("couldn't convert the input wat to Wasm");
let mut comp = Component::parse(&buff, false, false).unwrap();
let result = comp.encode();Sourcepub fn resolve<'s>(&'s self, ref_: &IndexedRef) -> ResolvedItem<'s, 'a>
pub fn resolve<'s>(&'s self, ref_: &IndexedRef) -> ResolvedItem<'s, 'a>
Resolve a reference that is local to this component’s own index space.
The ref_ must have depth == 0 (i.e. it refers to an item defined directly
within this component, not to an outer scope). This is the typical case for
refs found in a component’s own exports, imports, types, etc.
This uses the index space that was built at parse time — no walk is required.
Because all nested components share the same index store and scope registry
(via Rc), this method works correctly on any component in the tree, not just the root.
Sourcepub fn resolve_named_import<'s>(
&'s self,
name: &str,
) -> Option<ResolvedItem<'s, 'a>>
pub fn resolve_named_import<'s>( &'s self, name: &str, ) -> Option<ResolvedItem<'s, 'a>>
Resolve the item referred to by the named import, using this component’s own index space.
Finds the first import whose name matches name, then resolves the type ref it carries
(e.g. ComponentTypeRef::Instance(idx)) into a ResolvedItem.
Returns None if no import with that name exists or if the import carries no type ref.
Sourcepub fn resolve_named_export<'s>(
&'s self,
name: &str,
) -> Option<ResolvedItem<'s, 'a>>
pub fn resolve_named_export<'s>( &'s self, name: &str, ) -> Option<ResolvedItem<'s, 'a>>
Resolve the item referred to by the named export, using this component’s own index space.
Finds the first export whose name matches name, then resolves the item ref it carries
into a ResolvedItem.
Returns None if no export with that name exists.
Sourcepub fn get_type_of_exported_lift_func(
&self,
export_id: ComponentExportId,
) -> Option<&ComponentType<'a>>
pub fn get_type_of_exported_lift_func( &self, export_id: ComponentExportId, ) -> Option<&ComponentType<'a>>
Lookup the type for an exported lift canonical function.
Sourcepub fn get_fid_by_name(
&self,
name: &str,
module_idx: ModuleID,
) -> Option<FunctionID>
pub fn get_fid_by_name( &self, name: &str, module_idx: ModuleID, ) -> Option<FunctionID>
Get Local Function ID by name