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
impl ResourcesBuilder
Sourcepub fn convert(
&mut self,
id: ResourceId,
types: &mut ComponentTypes,
) -> TypeResourceTableIndex
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.
Sourcepub 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,
)
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.
Sourcepub fn register_resource(&mut self, id: ResourceId, ty: ResourceIndex)
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.
Sourcepub fn set_current_instance(&mut self, instance: RuntimeComponentInstanceIndex)
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
impl Clone for ResourcesBuilder
Source§fn clone(&self) -> ResourcesBuilder
fn clone(&self) -> ResourcesBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Default for ResourcesBuilder
impl Default for ResourcesBuilder
Source§fn default() -> ResourcesBuilder
fn default() -> ResourcesBuilder
Auto Trait Implementations§
impl Freeze for ResourcesBuilder
impl RefUnwindSafe for ResourcesBuilder
impl Send for ResourcesBuilder
impl Sync for ResourcesBuilder
impl Unpin for ResourcesBuilder
impl UnwindSafe for ResourcesBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)