Skip to main content

Component

Struct Component 

Source
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: Canons

Canons

§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>

Source

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).

Source

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>

Source

pub fn emit_wasm(&self, file_name: &str) -> Result<()>

Emit the Component into a wasm binary file.

Source

pub fn add_module(&mut self, module: Module<'a>) -> ModuleID

Add a Module to this Component.

Source

pub fn add_globals(&mut self, global: Global, module_idx: ModuleID) -> GlobalID

Add a Global to this Component.

Source

pub fn add_custom_section( &mut self, section: CustomSection<'a>, ) -> CustomSectionID

Source

pub fn add_import(&mut self, import: ComponentImport<'a>) -> u32

Add an Import to this Component.

Source

pub fn add_alias_func( &mut self, alias: ComponentAlias<'a>, ) -> (AliasFuncId, AliasId)

Add an Aliased function to this Component.

Source

pub fn add_alias_core_memory( &mut self, alias: ComponentAlias<'a>, ) -> (AliasMemId, AliasId)

Add an Aliased core memory to this Component.

Source

pub fn add_canon_func(&mut self, canon: CanonicalFunction) -> CanonicalFuncId

Add a Canonical Function to this Component.

Source

pub fn add_type_instance( &mut self, decls: Vec<InstanceTypeDeclaration<'a>>, ) -> (ComponentTypeInstanceId, ComponentTypeId)

Add a Component Type that is an Instance to this component.

Source

pub fn add_type_func( &mut self, ty: ComponentFuncType<'a>, ) -> (ComponentTypeFuncId, ComponentTypeId)

Add a Component Type that is a Function to this component.

Source

pub fn add_core_instance(&mut self, instance: Instance<'a>) -> CoreInstanceId

Add a new core instance to this component.

Source

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();
Source

pub fn encode(&self) -> Result<Vec<u8>>

Encode this component into a WebAssembly binary.

This method performs encoding in three high-level phases:

  1. Collect – Walks the component IR and records all items that will be encoded, along with any index references that need to be rewritten.
  2. Assign – Resolves all index references after instrumentation so that every reference points to its final concrete index.
  3. 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();
Source

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.

Source

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.

Source

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.

Source

pub fn get_type_of_exported_lift_func( &self, export_id: ComponentExportId, ) -> Option<&ComponentType<'a>>

Lookup the type for an exported lift canonical function.

Source

pub fn print(&self)

Print a rudimentary textual representation of a Component

Source

pub fn get_fid_by_name( &self, name: &str, module_idx: ModuleID, ) -> Option<FunctionID>

Get Local Function ID by name

Trait Implementations§

Source§

impl<'a> Debug for Component<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl IndexSpaceOf for Component<'_>

Auto Trait Implementations§

§

impl<'a> Freeze for Component<'a>

§

impl<'a> !RefUnwindSafe for Component<'a>

§

impl<'a> !Send for Component<'a>

§

impl<'a> !Sync for Component<'a>

§

impl<'a> Unpin for Component<'a>

§

impl<'a> UnsafeUnpin for Component<'a>

§

impl<'a> !UnwindSafe for Component<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.