Struct wasmtime_environ::component::ResourcesBuilder

source ·
pub struct ResourcesBuilder { /* private fields */ }
Expand description

Builder state used to translate wasmparser’s ResourceId types to Wasmtime’s TypeResourceTableIndex type.

This is contained in a ComponentTypesBuilder but is modified quite a bit manually via the inline phase of component instantiation.

This type crucially implements the Clone trait which is used to “snapshot” the current state of resource translation. The purpose of Clone here is to record translation information just before a subcomponent is instantiated to restore it after the subcomponent’s instantiation has completed. This is done to handle instantiations of the same component multiple times correctly.

Wasmparser produces one set of type information for a component, and not a unique set of type information about its internals for each instantiation. Each instance which results from instantiation gets a new type, but when we’re translating the instantiation of a component Wasmtime will re-run all initializers. This means that if naively implemented the ResourceId mapping from the first instantiation will be reused by the second instantiation. The snapshotting behavior and restoration guarantees that whenever a subcomponent is visited and instantiated it’s guaranteed that there’s no registered information for its ResourceId definitions within this builder.

Note that ResourceId references are guaranteed to be “local” in the sense that if a resource is defined within a component then the ID it’s assigned internally within a component is different than the ID when it’s instantiated (since all instantiations produce new types). This means that when throwing away state built-up from within a component that’s also reasonable because the information should never be used after a component is left anyway.

Implementations§

source§

impl ResourcesBuilder

source

pub fn convert( &mut self, id: ResourceId, types: &mut ComponentTypes, ) -> TypeResourceTableIndex

Converts the id provided into a TypeResourceTableIndex.

If id has previously been seen or converted, the prior value is returned. Otherwise the resource_id_to_resource_index table must have been previously populated and additionally current_instance must have been previously set. Using these a new TypeResourceTable value is allocated which produces a fresh TypeResourceTableIndex within the types provided.

Due to wasmparser’s uniqueness of resource IDs combined with the snapshotting and restoration behavior of ResourcesBuilder itself this should have the net effect of the first time a resource is seen within any component it’s assigned a new table, which is exactly what we want.

source

pub fn register_component_entity_type<'a>( &mut self, types: &'a TypesRef<'_>, ty: ComponentEntityType, path: &mut Vec<&'a str>, register: &mut dyn FnMut(&[&'a str]) -> ResourceIndex, )

Walks over the ty provided, as defined within types, and registers all the defined resources found with the register function provided.

This is used to register ResourceIndex entries within the resource_id_to_resource_index table of this type for situations such as when a resource is imported into a component. During the inlining phase of compilation the actual underlying type of the resource is known due to tracking dataflow and this registers that relationship.

The path provided is temporary storage to pass to the register function eventually.

The register callback is invoked with path with a list of names which correspond to exports of instances to reach the “leaf” where a resource type is expected.

source

pub fn register_resource(&mut self, id: ResourceId, ty: ResourceIndex)

Declares that the wasmparser id, which must point to a resource, is defined by the ty provided.

This is used when a local resource is defined within a component for example.

source

pub fn set_current_instance(&mut self, instance: RuntimeComponentInstanceIndex)

Updates the current_instance field to assign instance fields of future TypeResourceTableIndex values produced via convert.

Trait Implementations§

source§

impl Clone for ResourcesBuilder

source§

fn clone(&self) -> ResourcesBuilder

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Default for ResourcesBuilder

source§

fn default() -> ResourcesBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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

§

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.