wasmtime_environ::component

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

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

Source§

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

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.