1 use crate::component::func::HostFunc;
2 use crate::component::matching::InstanceType;
3 use crate::component::store::{ComponentInstanceId, StoreComponentInstanceId};
4 use crate::component::{
5     Component, ComponentExportIndex, ComponentNamedList, Func, Lift, Lower, ResourceType,
6     TypedFunc, types::ComponentItem,
7 };
8 use crate::instance::OwnedImports;
9 use crate::linker::DefinitionType;
10 use crate::prelude::*;
11 use crate::runtime::vm::component::{
12     CallContexts, ComponentInstance, ResourceTables, TypedResource, TypedResourceIndex,
13 };
14 use crate::runtime::vm::{self, VMFuncRef};
15 use crate::store::{AsStoreOpaque, StoreOpaque};
16 use crate::{AsContext, AsContextMut, Engine, Module, StoreContextMut};
17 use alloc::sync::Arc;
18 use core::marker;
19 use core::pin::Pin;
20 use core::ptr::NonNull;
21 use wasmtime_environ::{EngineOrModuleTypeIndex, component::*};
22 use wasmtime_environ::{EntityIndex, EntityType, PrimaryMap};
23 
24 /// An instantiated component.
25 ///
26 /// This type represents an instantiated [`Component`](super::Component).
27 /// Instances have exports which can be accessed through functions such as
28 /// [`Instance::get_func`] or [`Instance::get_export`]. Instances are owned by a
29 /// [`Store`](crate::Store) and all methods require a handle to the store.
30 ///
31 /// Component instances are created through
32 /// [`Linker::instantiate`](super::Linker::instantiate) and its family of
33 /// methods.
34 ///
35 /// This type is similar to the core wasm version
36 /// [`wasmtime::Instance`](crate::Instance) except that it represents an
37 /// instantiated component instead of an instantiated module.
38 #[derive(Copy, Clone, Debug)]
39 #[repr(transparent)]
40 pub struct Instance {
41     id: StoreComponentInstanceId,
42 }
43 
44 // Double-check that the C representation in `component/instance.h` matches our
45 // in-Rust representation here in terms of size/alignment/etc.
46 const _: () = {
47     #[repr(C)]
48     struct C(u64, u32);
49     assert!(core::mem::size_of::<C>() == core::mem::size_of::<Instance>());
50     assert!(core::mem::align_of::<C>() == core::mem::align_of::<Instance>());
51     assert!(core::mem::offset_of!(Instance, id) == 0);
52 };
53 
54 impl Instance {
55     /// Creates a raw `Instance` from the internal identifiers within the store.
56     pub(crate) fn from_wasmtime(store: &StoreOpaque, id: ComponentInstanceId) -> Instance {
57         Instance {
58             id: StoreComponentInstanceId::new(store.id(), id),
59         }
60     }
61 
62     /// Looks up an exported function by name within this [`Instance`].
63     ///
64     /// The `store` argument provided must be the store that this instance
65     /// lives within and the `name` argument is the lookup key by which to find
66     /// the exported function. If the function is found then `Some` is returned
67     /// and otherwise `None` is returned.
68     ///
69     /// The `name` here can be a string such as `&str` or it can be a
70     /// [`ComponentExportIndex`] which is loaded prior from a [`Component`].
71     ///
72     /// # Panics
73     ///
74     /// Panics if `store` does not own this instance.
75     ///
76     /// # Examples
77     ///
78     /// Looking up a function which is exported from the root of a component:
79     ///
80     /// ```
81     /// use wasmtime::{Engine, Store};
82     /// use wasmtime::component::{Component, Linker};
83     ///
84     /// # fn main() -> wasmtime::Result<()> {
85     /// let engine = Engine::default();
86     /// let component = Component::new(
87     ///     &engine,
88     ///     r#"
89     ///         (component
90     ///             (core module $m
91     ///                 (func (export "f"))
92     ///             )
93     ///             (core instance $i (instantiate $m))
94     ///             (func (export "f")
95     ///                 (canon lift (core func $i "f")))
96     ///         )
97     ///     "#,
98     /// )?;
99     ///
100     /// // Look up the function by name
101     /// let mut store = Store::new(&engine, ());
102     /// let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
103     /// let func = instance.get_func(&mut store, "f").unwrap();
104     ///
105     /// // The function can also be looked up by an index via a precomputed index.
106     /// let export = component.get_export_index(None, "f").unwrap();
107     /// let func = instance.get_func(&mut store, &export).unwrap();
108     /// # Ok(())
109     /// # }
110     /// ```
111     ///
112     /// Looking up a function which is exported from a nested instance:
113     ///
114     /// ```
115     /// use wasmtime::{Engine, Store};
116     /// use wasmtime::component::{Component, Linker};
117     ///
118     /// # fn main() -> wasmtime::Result<()> {
119     /// let engine = Engine::default();
120     /// let component = Component::new(
121     ///     &engine,
122     ///     r#"
123     ///         (component
124     ///             (core module $m
125     ///                 (func (export "f"))
126     ///             )
127     ///             (core instance $i (instantiate $m))
128     ///             (func $f
129     ///                 (canon lift (core func $i "f")))
130     ///
131     ///             (instance $i
132     ///                 (export "f" (func $f)))
133     ///             (export "i" (instance $i))
134     ///         )
135     ///     "#,
136     /// )?;
137     ///
138     /// // First look up the exported instance, then use that to lookup the
139     /// // exported function.
140     /// let instance_index = component.get_export_index(None, "i").unwrap();
141     /// let func_index = component.get_export_index(Some(&instance_index), "f").unwrap();
142     ///
143     /// // Then use `func_index` at runtime.
144     /// let mut store = Store::new(&engine, ());
145     /// let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
146     /// let func = instance.get_func(&mut store, &func_index).unwrap();
147     ///
148     /// // Alternatively the `instance` can be used directly in conjunction with
149     /// // the `get_export_index` method.
150     /// let instance_index = instance.get_export_index(&mut store, None, "i").unwrap();
151     /// let func_index = instance.get_export_index(&mut store, Some(&instance_index), "f").unwrap();
152     /// let func = instance.get_func(&mut store, &func_index).unwrap();
153     /// # Ok(())
154     /// # }
155     /// ```
156     pub fn get_func(
157         &self,
158         mut store: impl AsContextMut,
159         name: impl InstanceExportLookup,
160     ) -> Option<Func> {
161         let store = store.as_context_mut().0;
162         let instance = self.id.get(store);
163         let component = instance.component();
164 
165         // Validate that `name` exists within `self.`
166         let index = name.lookup(component)?;
167 
168         // Validate that `index` is indeed a lifted function.
169         match &component.env_component().export_items[index] {
170             Export::LiftedFunction { .. } => {}
171             _ => return None,
172         }
173 
174         // And package up the indices!
175         Some(Func::from_lifted_func(*self, index))
176     }
177 
178     /// Looks up an exported [`Func`] value by name and with its type.
179     ///
180     /// This function is a convenience wrapper over [`Instance::get_func`] and
181     /// [`Func::typed`]. For more information see the linked documentation.
182     ///
183     /// Returns an error if `name` isn't a function export or if the export's
184     /// type did not match `Params` or `Results`
185     ///
186     /// # Panics
187     ///
188     /// Panics if `store` does not own this instance.
189     pub fn get_typed_func<Params, Results>(
190         &self,
191         mut store: impl AsContextMut,
192         name: impl InstanceExportLookup,
193     ) -> Result<TypedFunc<Params, Results>>
194     where
195         Params: ComponentNamedList + Lower,
196         Results: ComponentNamedList + Lift,
197     {
198         let f = self
199             .get_func(store.as_context_mut(), name)
200             .ok_or_else(|| anyhow!("failed to find function export"))?;
201         Ok(f.typed::<Params, Results>(store)
202             .with_context(|| format!("failed to convert function to given type"))?)
203     }
204 
205     /// Looks up an exported module by name within this [`Instance`].
206     ///
207     /// The `store` argument provided must be the store that this instance
208     /// lives within and the `name` argument is the lookup key by which to find
209     /// the exported module. If the module is found then `Some` is returned
210     /// and otherwise `None` is returned.
211     ///
212     /// The `name` here can be a string such as `&str` or it can be a
213     /// [`ComponentExportIndex`] which is loaded prior from a [`Component`].
214     ///
215     /// For some examples see [`Instance::get_func`] for loading values from a
216     /// component.
217     ///
218     /// # Panics
219     ///
220     /// Panics if `store` does not own this instance.
221     pub fn get_module(
222         &self,
223         mut store: impl AsContextMut,
224         name: impl InstanceExportLookup,
225     ) -> Option<Module> {
226         let store = store.as_context_mut().0;
227         let (instance, export) = self.lookup_export(store, name)?;
228         match export {
229             Export::ModuleStatic { index, .. } => {
230                 Some(instance.component().static_module(*index).clone())
231             }
232             Export::ModuleImport { import, .. } => match instance.runtime_import(*import) {
233                 RuntimeImport::Module(m) => Some(m.clone()),
234                 _ => unreachable!(),
235             },
236             _ => None,
237         }
238     }
239 
240     /// Looks up an exported resource type by name within this [`Instance`].
241     ///
242     /// The `store` argument provided must be the store that this instance
243     /// lives within and the `name` argument is the lookup key by which to find
244     /// the exported resource. If the resource is found then `Some` is returned
245     /// and otherwise `None` is returned.
246     ///
247     /// The `name` here can be a string such as `&str` or it can be a
248     /// [`ComponentExportIndex`] which is loaded prior from a [`Component`].
249     ///
250     /// For some examples see [`Instance::get_func`] for loading values from a
251     /// component.
252     ///
253     /// # Panics
254     ///
255     /// Panics if `store` does not own this instance.
256     pub fn get_resource(
257         &self,
258         mut store: impl AsContextMut,
259         name: impl InstanceExportLookup,
260     ) -> Option<ResourceType> {
261         let store = store.as_context_mut().0;
262         let (instance, export) = self.lookup_export(store, name)?;
263         match export {
264             Export::Type(TypeDef::Resource(id)) => {
265                 Some(InstanceType::new(instance).resource_type(*id))
266             }
267             Export::Type(_)
268             | Export::LiftedFunction { .. }
269             | Export::ModuleStatic { .. }
270             | Export::ModuleImport { .. }
271             | Export::Instance { .. } => None,
272         }
273     }
274 
275     /// A methods similar to [`Component::get_export`] except for this
276     /// instance.
277     ///
278     /// This method will lookup the `name` provided within the `instance`
279     /// provided and return a [`ComponentItem`] describing the export,
280     /// and [`ComponentExportIndex`] which can be passed other `get_*`
281     /// functions like [`Instance::get_func`].
282     ///
283     /// The [`ComponentItem`] is more expensive to compute than the
284     /// [`ComponentExportIndex`]. If you are not consuming the
285     /// [`ComponentItem`], use [`Instance::get_export_index`] instead.
286     ///
287     /// # Panics
288     ///
289     /// Panics if `store` does not own this instance.
290     pub fn get_export(
291         &self,
292         mut store: impl AsContextMut,
293         instance: Option<&ComponentExportIndex>,
294         name: &str,
295     ) -> Option<(ComponentItem, ComponentExportIndex)> {
296         self._get_export(store.as_context_mut().0, instance, name)
297     }
298 
299     fn _get_export(
300         &self,
301         store: &StoreOpaque,
302         instance: Option<&ComponentExportIndex>,
303         name: &str,
304     ) -> Option<(ComponentItem, ComponentExportIndex)> {
305         let data = self.id().get(store);
306         let component = data.component();
307         let index = component.lookup_export_index(instance, name)?;
308         let item = ComponentItem::from_export(
309             &store.engine(),
310             &component.env_component().export_items[index],
311             &InstanceType::new(data),
312         );
313         Some((
314             item,
315             ComponentExportIndex {
316                 id: data.component().id(),
317                 index,
318             },
319         ))
320     }
321 
322     /// A methods similar to [`Component::get_export_index`] except for this
323     /// instance.
324     ///
325     /// This method will lookup the `name` provided within the `instance`
326     /// provided and return a [`ComponentExportIndex`] which can be passed
327     /// other `get_*` functions like [`Instance::get_func`].
328     ///
329     /// If you need the [`ComponentItem`] corresponding to this export, use
330     /// the [`Instance::get_export`] instead.
331     ///
332     /// # Panics
333     ///
334     /// Panics if `store` does not own this instance.
335     pub fn get_export_index(
336         &self,
337         mut store: impl AsContextMut,
338         instance: Option<&ComponentExportIndex>,
339         name: &str,
340     ) -> Option<ComponentExportIndex> {
341         let data = self.id().get(store.as_context_mut().0);
342         let index = data.component().lookup_export_index(instance, name)?;
343         Some(ComponentExportIndex {
344             id: data.component().id(),
345             index,
346         })
347     }
348 
349     fn lookup_export<'a>(
350         &self,
351         store: &'a StoreOpaque,
352         name: impl InstanceExportLookup,
353     ) -> Option<(&'a ComponentInstance, &'a Export)> {
354         let data = self.id().get(store);
355         let index = name.lookup(data.component())?;
356         Some((data, &data.component().env_component().export_items[index]))
357     }
358 
359     /// Returns the [`InstancePre`] that was used to create this instance.
360     pub fn instance_pre<T>(&self, store: impl AsContext<Data = T>) -> InstancePre<T> {
361         // This indexing operation asserts the Store owns the Instance.
362         // Therefore, the InstancePre<T> must match the Store<T>.
363         let data = self.id().get(store.as_context().0);
364 
365         // SAFETY: calling this method safely here relies on matching the `T`
366         // in `InstancePre<T>` to the store itself, which is happening in the
367         // type signature just above by ensuring the store's data is `T` which
368         // matches the return value.
369         unsafe { data.instance_pre() }
370     }
371 
372     pub(crate) fn id(&self) -> StoreComponentInstanceId {
373         self.id
374     }
375 
376     /// Implementation of the `resource.new` intrinsic for `i32`
377     /// representations.
378     pub(crate) fn resource_new32(
379         self,
380         store: &mut StoreOpaque,
381         caller: RuntimeComponentInstanceIndex,
382         ty: TypeResourceTableIndex,
383         rep: u32,
384     ) -> Result<u32> {
385         self.id().get(store).check_may_leave(caller)?;
386         let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
387         resource_tables(calls, instance).resource_new(TypedResource::Component { ty, rep })
388     }
389 
390     /// Implementation of the `resource.rep` intrinsic for `i32`
391     /// representations.
392     pub(crate) fn resource_rep32(
393         self,
394         store: &mut StoreOpaque,
395         caller: RuntimeComponentInstanceIndex,
396         ty: TypeResourceTableIndex,
397         index: u32,
398     ) -> Result<u32> {
399         self.id().get(store).check_may_leave(caller)?;
400         let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
401         resource_tables(calls, instance).resource_rep(TypedResourceIndex::Component { ty, index })
402     }
403 
404     /// Implementation of the `resource.drop` intrinsic.
405     pub(crate) fn resource_drop(
406         self,
407         store: &mut StoreOpaque,
408         caller: RuntimeComponentInstanceIndex,
409         ty: TypeResourceTableIndex,
410         index: u32,
411     ) -> Result<Option<u32>> {
412         self.id().get(store).check_may_leave(caller)?;
413         let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
414         resource_tables(calls, instance).resource_drop(TypedResourceIndex::Component { ty, index })
415     }
416 
417     pub(crate) fn resource_transfer_own(
418         self,
419         store: &mut StoreOpaque,
420         index: u32,
421         src: TypeResourceTableIndex,
422         dst: TypeResourceTableIndex,
423     ) -> Result<u32> {
424         let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
425         let mut tables = resource_tables(calls, instance);
426         let rep = tables.resource_lift_own(TypedResourceIndex::Component { ty: src, index })?;
427         tables.resource_lower_own(TypedResource::Component { ty: dst, rep })
428     }
429 
430     pub(crate) fn resource_transfer_borrow(
431         self,
432         store: &mut StoreOpaque,
433         index: u32,
434         src: TypeResourceTableIndex,
435         dst: TypeResourceTableIndex,
436     ) -> Result<u32> {
437         let dst_owns_resource = self.id().get(store).resource_owned_by_own_instance(dst);
438         let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
439         let mut tables = resource_tables(calls, instance);
440         let rep = tables.resource_lift_borrow(TypedResourceIndex::Component { ty: src, index })?;
441         // Implement `lower_borrow`'s special case here where if a borrow's
442         // resource type is owned by `dst` then the destination receives the
443         // representation directly rather than a handle to the representation.
444         //
445         // This can perhaps become a different libcall in the future to avoid
446         // this check at runtime since we know at compile time whether the
447         // destination type owns the resource, but that's left as a future
448         // refactoring if truly necessary.
449         if dst_owns_resource {
450             return Ok(rep);
451         }
452         tables.resource_lower_borrow(TypedResource::Component { ty: dst, rep })
453     }
454 
455     pub(crate) fn resource_enter_call(self, store: &mut StoreOpaque) {
456         let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
457         resource_tables(calls, instance).enter_call()
458     }
459 
460     pub(crate) fn resource_exit_call(self, store: &mut StoreOpaque) -> Result<()> {
461         let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
462         resource_tables(calls, instance).exit_call()
463     }
464 
465     pub(crate) fn lookup_vmdef(&self, store: &mut StoreOpaque, def: &CoreDef) -> vm::Export {
466         lookup_vmdef(store, self.id.instance(), def)
467     }
468 
469     pub(crate) fn options<'a>(
470         &self,
471         store: &'a StoreOpaque,
472         options: OptionsIndex,
473     ) -> &'a CanonicalOptions {
474         &self.id.get(store).component().env_component().options[options]
475     }
476 
477     fn options_memory_raw(
478         &self,
479         store: &StoreOpaque,
480         options: OptionsIndex,
481     ) -> Option<NonNull<vm::VMMemoryDefinition>> {
482         let instance = self.id.get(store);
483         let options = &instance.component().env_component().options[options];
484         let memory = match options.data_model {
485             CanonicalOptionsDataModel::Gc { .. } => return None,
486             CanonicalOptionsDataModel::LinearMemory(o) => match o.memory {
487                 Some(m) => m,
488                 None => return None,
489             },
490         };
491 
492         Some(instance.runtime_memory(memory))
493     }
494 
495     pub(crate) fn options_memory<'a>(
496         &self,
497         store: &'a StoreOpaque,
498         options: OptionsIndex,
499     ) -> &'a [u8] {
500         let memory = match self.options_memory_raw(store, options) {
501             Some(m) => m,
502             None => return &[],
503         };
504         // SAFETY: we're borrowing the entire `StoreOpaque` which owns the
505         // memory allocation to return the result of memory. That means that the
506         // lifetime connection here should be safe and the actual ptr/length are
507         // trusted parts of the runtime here.
508         unsafe {
509             let memory = memory.as_ref();
510             core::slice::from_raw_parts(memory.base.as_ptr(), memory.current_length())
511         }
512     }
513 
514     pub(crate) fn options_memory_mut<'a>(
515         &self,
516         store: &'a mut StoreOpaque,
517         options: OptionsIndex,
518     ) -> &'a mut [u8] {
519         let memory = match self.options_memory_raw(store, options) {
520             Some(m) => m,
521             None => return &mut [],
522         };
523         // SAFETY: See `options_memory` comment above, and note that this is
524         // taking `&mut StoreOpaque` to thread the lifetime through instead.
525         unsafe {
526             let memory = memory.as_ref();
527             core::slice::from_raw_parts_mut(memory.base.as_ptr(), memory.current_length())
528         }
529     }
530 
531     /// Helper function to simultaneously get a borrow to this instance's
532     /// component as well as the store that this component is contained within.
533     ///
534     /// Note that this function signature is not possible with safe Rust, so
535     /// this is using `unsafe` internally.
536     pub(crate) fn component_and_store_mut<'a, S>(
537         &self,
538         store: &'a mut S,
539     ) -> (&'a Component, &'a mut S)
540     where
541         S: AsStoreOpaque,
542     {
543         let store_opaque = store.as_store_opaque();
544         let instance = self.id.get_mut(store_opaque);
545         let component = instance.component();
546 
547         // SAFETY: the goal of this function is to derive a pointer from
548         // `&mut S`, here `&Component`, and then return both so they can both be
549         // used at the same time. In general this is not safe operation since
550         // the original mutable pointer could be mutated or overwritten which
551         // would invalidate the derived pointer.
552         //
553         // In this case though we have a few guarantees which should make this
554         // safe:
555         //
556         // * Embedders never have the ability to overwrite a `StoreOpaque`. For
557         //   example the closest thing of `StoreContextMut` wraps up the
558         //   reference internally so it's inaccessible to the outside world.
559         //   This means that while mutations can still happen it's not possible
560         //   to overwrite a `StoreOpaque` directly.
561         //
562         // * Components are referred to by `vm::ComponentInstance` which holds a
563         //   strong reference. All `ComponentInstance` structures are allocated
564         //   within the store and unconditionally live as long as the entire
565         //   store itself. This means that there's no worry of the rooting
566         //   container going away or otherwise getting deallocated.
567         //
568         // * The `ComponentInstance` container has an invariant that after
569         //   creation the component used to create it cannot be changed. This is
570         //   enforced through `Pin<&mut ComponentInstance>` which disallows
571         //   mutable access to the `component` field, instead only allowing
572         //   read-only access.
573         //
574         // Putting all of this together it's not possible for a component,
575         // within a component instance, within a store, to be deallocated or mutated while
576         // a store is in use. Consequently it should be safe to simultaneously
577         // have a borrow to both at the same time, even if the store has a
578         // mutable borrow itself.
579         unsafe {
580             let component: *const Component = component;
581             (&*component, store)
582         }
583     }
584 }
585 
586 /// Translates a `CoreDef`, a definition of a core wasm item, to an
587 /// [`Export`] which is the runtime core wasm definition.
588 pub(crate) fn lookup_vmdef(
589     store: &mut StoreOpaque,
590     id: ComponentInstanceId,
591     def: &CoreDef,
592 ) -> vm::Export {
593     match def {
594         CoreDef::Export(e) => lookup_vmexport(store, id, e),
595         CoreDef::Trampoline(idx) => {
596             let funcref = store
597                 .store_data_mut()
598                 .component_instance_mut(id)
599                 .trampoline_func_ref(*idx);
600             // SAFETY: the `funcref` is owned by `store` and is valid within
601             // that store, so it's safe to create a `Func`.
602             vm::Export::Function(unsafe { crate::Func::from_vm_func_ref(store.id(), funcref) })
603         }
604         CoreDef::InstanceFlags(idx) => {
605             let id = StoreComponentInstanceId::new(store.id(), id);
606             vm::Export::Global(crate::Global::from_component_flags(id, *idx))
607         }
608         CoreDef::UnsafeIntrinsic(intrinsic) => {
609             let funcref = store
610                 .store_data_mut()
611                 .component_instance_mut(id)
612                 .unsafe_intrinsic_func_ref(*intrinsic);
613             // SAFETY: as above, the `funcref` is owned by `store` and is valid
614             // within that store, so it's safe to create a `Func`.
615             vm::Export::Function(unsafe { crate::Func::from_vm_func_ref(store.id(), funcref) })
616         }
617     }
618 }
619 
620 /// Translates a `CoreExport<T>`, an export of some core instance within
621 /// this component, to the actual runtime definition of that item.
622 pub(crate) fn lookup_vmexport<T>(
623     store: &mut StoreOpaque,
624     id: ComponentInstanceId,
625     item: &CoreExport<T>,
626 ) -> vm::Export
627 where
628     T: Copy + Into<EntityIndex>,
629 {
630     let store_id = store.id();
631     let id = store
632         .store_data_mut()
633         .component_instance_mut(id)
634         .instance(item.instance);
635     let (instance, registry) = store.instance_and_module_registry_mut(id);
636     let idx = match &item.item {
637         ExportItem::Index(idx) => (*idx).into(),
638 
639         // FIXME: ideally at runtime we don't actually do any name lookups
640         // here. This will only happen when the host supplies an imported
641         // module so while the structure can't be known at compile time we
642         // do know at `InstancePre` time, for example, what all the host
643         // imports are. In theory we should be able to, as part of
644         // `InstancePre` construction, perform all name=>index mappings
645         // during that phase so the actual instantiation of an `InstancePre`
646         // skips all string lookups. This should probably only be
647         // investigated if this becomes a performance issue though.
648         ExportItem::Name(name) => instance.env_module().exports[name],
649     };
650     // SAFETY: the `store_id` owns this instance and all exports contained
651     // within.
652     unsafe { instance.get_export_by_index_mut(registry, store_id, idx) }
653 }
654 
655 fn resource_tables<'a>(
656     calls: &'a mut CallContexts,
657     instance: Pin<&'a mut ComponentInstance>,
658 ) -> ResourceTables<'a> {
659     ResourceTables {
660         host_table: None,
661         calls,
662         guest: Some(instance.instance_states()),
663     }
664 }
665 
666 /// Trait used to lookup the export of a component instance.
667 ///
668 /// This trait is used as an implementation detail of [`Instance::get_func`]
669 /// and related `get_*` methods. Notable implementors of this trait are:
670 ///
671 /// * `str`
672 /// * `String`
673 /// * [`ComponentExportIndex`]
674 ///
675 /// Note that this is intended to be a `wasmtime`-sealed trait so it shouldn't
676 /// need to be implemented externally.
677 pub trait InstanceExportLookup {
678     #[doc(hidden)]
679     fn lookup(&self, component: &Component) -> Option<ExportIndex>;
680 }
681 
682 impl<T> InstanceExportLookup for &T
683 where
684     T: InstanceExportLookup + ?Sized,
685 {
686     fn lookup(&self, component: &Component) -> Option<ExportIndex> {
687         T::lookup(self, component)
688     }
689 }
690 
691 impl InstanceExportLookup for str {
692     fn lookup(&self, component: &Component) -> Option<ExportIndex> {
693         component
694             .env_component()
695             .exports
696             .get(self, &NameMapNoIntern)
697             .copied()
698     }
699 }
700 
701 impl InstanceExportLookup for String {
702     fn lookup(&self, component: &Component) -> Option<ExportIndex> {
703         str::lookup(self, component)
704     }
705 }
706 
707 struct Instantiator<'a> {
708     component: &'a Component,
709     id: ComponentInstanceId,
710     core_imports: OwnedImports,
711     imports: &'a PrimaryMap<RuntimeImportIndex, RuntimeImport>,
712 }
713 
714 pub(crate) enum RuntimeImport {
715     Func(Arc<HostFunc>),
716     Module(Module),
717     Resource {
718         ty: ResourceType,
719 
720         // A strong reference to the host function that represents the
721         // destructor for this resource. At this time all resources here are
722         // host-defined resources. Note that this is itself never read because
723         // the funcref below points to it.
724         //
725         // Also note that the `Arc` here is used to support the same host
726         // function being used across multiple instances simultaneously. Or
727         // otherwise this makes `InstancePre::instantiate` possible to create
728         // separate instances all sharing the same host function.
729         _dtor: Arc<crate::func::HostFunc>,
730 
731         // A raw function which is filled out (including `wasm_call`) which
732         // points to the internals of the `_dtor` field. This is read and
733         // possibly executed by wasm.
734         dtor_funcref: VMFuncRef,
735     },
736 }
737 
738 pub type ImportedResources = PrimaryMap<ResourceIndex, ResourceType>;
739 
740 impl<'a> Instantiator<'a> {
741     fn new(
742         component: &'a Component,
743         store: &mut StoreOpaque,
744         imports: &'a Arc<PrimaryMap<RuntimeImportIndex, RuntimeImport>>,
745     ) -> Result<Instantiator<'a>> {
746         let env_component = component.env_component();
747         store.register_component(component)?;
748         let imported_resources: ImportedResources =
749             PrimaryMap::with_capacity(env_component.imported_resources.len());
750 
751         let instance = ComponentInstance::new(
752             store.store_data().components.next_component_instance_id(),
753             component,
754             Arc::new(imported_resources),
755             imports,
756             store.traitobj(),
757         );
758         let id = store.store_data_mut().push_component_instance(instance);
759 
760         Ok(Instantiator {
761             component,
762             imports,
763             core_imports: OwnedImports::empty(),
764             id,
765         })
766     }
767 
768     async fn run<T>(&mut self, store: &mut StoreContextMut<'_, T>) -> Result<()> {
769         let env_component = self.component.env_component();
770 
771         // Before all initializers are processed configure all destructors for
772         // host-defined resources. No initializer will correspond to these and
773         // it's required to happen before they're needed, so execute this first.
774         for (idx, import) in env_component.imported_resources.iter() {
775             let (ty, func_ref) = match &self.imports[*import] {
776                 RuntimeImport::Resource {
777                     ty, dtor_funcref, ..
778                 } => (*ty, NonNull::from(dtor_funcref)),
779                 _ => unreachable!(),
780             };
781             let i = self.instance_resource_types_mut(store.0).push(ty);
782             assert_eq!(i, idx);
783             self.instance_mut(store.0)
784                 .set_resource_destructor(idx, Some(func_ref));
785         }
786 
787         // Next configure all `VMFuncRef`s for trampolines that this component
788         // will require. These functions won't actually get used until their
789         // associated state has been initialized through the global initializers
790         // below, but the funcrefs can all be configured here.
791         for (idx, sig) in env_component.trampolines.iter() {
792             let ptrs = self.component.trampoline_ptrs(idx);
793             let signature = match self.component.signatures().shared_type(*sig) {
794                 Some(s) => s,
795                 None => panic!("found unregistered signature: {sig:?}"),
796             };
797 
798             self.instance_mut(store.0).set_trampoline(
799                 idx,
800                 ptrs.wasm_call,
801                 ptrs.array_call,
802                 signature,
803             );
804         }
805 
806         // Initialize the unsafe intrinsics used by this component, if any.
807         for (i, module_ty) in env_component
808             .unsafe_intrinsics
809             .iter()
810             .enumerate()
811             .filter_map(|(i, ty)| ty.expand().map(|ty| (i, ty)))
812         {
813             let i = u32::try_from(i).unwrap();
814             let intrinsic = UnsafeIntrinsic::from_u32(i);
815             let ptrs = self.component.unsafe_intrinsic_ptrs(intrinsic).expect(
816                 "should have intrinsic pointers given that we assigned the intrinsic a type",
817             );
818             let shared_ty = self
819                 .component
820                 .signatures()
821                 .shared_type(module_ty)
822                 .expect("should have a shared type");
823             self.instance_mut(store.0).set_intrinsic(
824                 intrinsic,
825                 ptrs.wasm_call,
826                 ptrs.array_call,
827                 shared_ty,
828             );
829         }
830 
831         for initializer in env_component.initializers.iter() {
832             match initializer {
833                 GlobalInitializer::InstantiateModule(m) => {
834                     let module;
835                     let imports = match m {
836                         // Since upvars are statically know we know that the
837                         // `args` list is already in the right order.
838                         InstantiateModule::Static(idx, args) => {
839                             module = self.component.static_module(*idx);
840                             self.build_imports(store.0, module, args.iter())
841                         }
842 
843                         // With imports, unlike upvars, we need to do runtime
844                         // lookups with strings to determine the order of the
845                         // imports since it's whatever the actual module
846                         // requires.
847                         //
848                         // FIXME: see the note in `ExportItem::Name` handling
849                         // above for how we ideally shouldn't do string lookup
850                         // here.
851                         InstantiateModule::Import(idx, args) => {
852                             module = match &self.imports[*idx] {
853                                 RuntimeImport::Module(m) => m,
854                                 _ => unreachable!(),
855                             };
856                             let args = module
857                                 .imports()
858                                 .map(|import| &args[import.module()][import.name()]);
859                             self.build_imports(store.0, module, args)
860                         }
861                     };
862 
863                     // Note that the unsafety here should be ok because the
864                     // validity of the component means that type-checks have
865                     // already been performed. This means that the unsafety due
866                     // to imports having the wrong type should not happen here.
867                     //
868                     // Also note we are calling new_started_impl because we have
869                     // already checked for asyncness and are running on a fiber
870                     // if required.
871 
872                     let i = unsafe {
873                         crate::Instance::new_started(store, module, imports.as_ref()).await?
874                     };
875                     self.instance_mut(store.0).push_instance_id(i.id());
876                 }
877 
878                 GlobalInitializer::LowerImport { import, index } => {
879                     let func = match &self.imports[*import] {
880                         RuntimeImport::Func(func) => func,
881                         _ => unreachable!(),
882                     };
883                     self.instance_mut(store.0)
884                         .set_lowering(*index, func.lowering());
885                 }
886 
887                 GlobalInitializer::ExtractTable(table) => self.extract_table(store.0, table),
888 
889                 GlobalInitializer::ExtractMemory(mem) => self.extract_memory(store.0, mem),
890 
891                 GlobalInitializer::ExtractRealloc(realloc) => {
892                     self.extract_realloc(store.0, realloc)
893                 }
894 
895                 GlobalInitializer::ExtractCallback(callback) => {
896                     self.extract_callback(store.0, callback)
897                 }
898 
899                 GlobalInitializer::ExtractPostReturn(post_return) => {
900                     self.extract_post_return(store.0, post_return)
901                 }
902 
903                 GlobalInitializer::Resource(r) => self.resource(store.0, r),
904             }
905         }
906         Ok(())
907     }
908 
909     fn resource(&mut self, store: &mut StoreOpaque, resource: &Resource) {
910         let dtor = resource
911             .dtor
912             .as_ref()
913             .map(|dtor| lookup_vmdef(store, self.id, dtor));
914         let dtor = dtor.map(|export| match export {
915             crate::runtime::vm::Export::Function(f) => f.vm_func_ref(store),
916             _ => unreachable!(),
917         });
918         let index = self
919             .component
920             .env_component()
921             .resource_index(resource.index);
922         let instance = self.instance(store);
923         let ty = ResourceType::guest(store.id(), instance, resource.index);
924         self.instance_mut(store)
925             .set_resource_destructor(index, dtor);
926         let i = self.instance_resource_types_mut(store).push(ty);
927         debug_assert_eq!(i, index);
928     }
929 
930     fn extract_memory(&mut self, store: &mut StoreOpaque, memory: &ExtractMemory) {
931         let import = match lookup_vmexport(store, self.id, &memory.export) {
932             crate::runtime::vm::Export::Memory(memory) => memory.vmimport(store),
933             crate::runtime::vm::Export::SharedMemory(_, import) => import,
934             _ => unreachable!(),
935         };
936         self.instance_mut(store)
937             .set_runtime_memory(memory.index, import.from.as_non_null());
938     }
939 
940     fn extract_realloc(&mut self, store: &mut StoreOpaque, realloc: &ExtractRealloc) {
941         let func_ref = match lookup_vmdef(store, self.id, &realloc.def) {
942             crate::runtime::vm::Export::Function(f) => f.vm_func_ref(store),
943             _ => unreachable!(),
944         };
945         self.instance_mut(store)
946             .set_runtime_realloc(realloc.index, func_ref);
947     }
948 
949     fn extract_callback(&mut self, store: &mut StoreOpaque, callback: &ExtractCallback) {
950         let func_ref = match lookup_vmdef(store, self.id, &callback.def) {
951             crate::runtime::vm::Export::Function(f) => f.vm_func_ref(store),
952             _ => unreachable!(),
953         };
954         self.instance_mut(store)
955             .set_runtime_callback(callback.index, func_ref);
956     }
957 
958     fn extract_post_return(&mut self, store: &mut StoreOpaque, post_return: &ExtractPostReturn) {
959         let func_ref = match lookup_vmdef(store, self.id, &post_return.def) {
960             crate::runtime::vm::Export::Function(f) => f.vm_func_ref(store),
961             _ => unreachable!(),
962         };
963         self.instance_mut(store)
964             .set_runtime_post_return(post_return.index, func_ref);
965     }
966 
967     fn extract_table(&mut self, store: &mut StoreOpaque, table: &ExtractTable) {
968         let export = match lookup_vmexport(store, self.id, &table.export) {
969             crate::runtime::vm::Export::Table(t) => t,
970             _ => unreachable!(),
971         };
972         let import = export.vmimport(store);
973         self.instance_mut(store)
974             .set_runtime_table(table.index, import);
975     }
976 
977     fn build_imports<'b>(
978         &mut self,
979         store: &mut StoreOpaque,
980         module: &Module,
981         args: impl Iterator<Item = &'b CoreDef>,
982     ) -> &OwnedImports {
983         self.core_imports.clear();
984         self.core_imports.reserve(module);
985         let mut imports = module.compiled_module().module().imports();
986 
987         for arg in args {
988             // The general idea of Wasmtime is that at runtime type-checks for
989             // core wasm instantiations internally within a component are
990             // unnecessary and superfluous. Naturally though mistakes may be
991             // made, so double-check this property of wasmtime in debug mode.
992 
993             if cfg!(debug_assertions) {
994                 let (imp_module, imp_name, expected) = imports.next().unwrap();
995                 self.assert_type_matches(store, module, arg, imp_module, imp_name, expected);
996             }
997 
998             // The unsafety here should be ok since the `export` is loaded
999             // directly from an instance which should only give us valid export
1000             // items.
1001             let export = lookup_vmdef(store, self.id, arg);
1002             self.core_imports.push_export(store, &export);
1003         }
1004         debug_assert!(imports.next().is_none());
1005 
1006         &self.core_imports
1007     }
1008 
1009     fn assert_type_matches(
1010         &self,
1011         store: &mut StoreOpaque,
1012         module: &Module,
1013         arg: &CoreDef,
1014         imp_module: &str,
1015         imp_name: &str,
1016         expected: EntityType,
1017     ) {
1018         let export = lookup_vmdef(store, self.id, arg);
1019 
1020         // If this value is a core wasm function then the type check is inlined
1021         // here. This can otherwise fail `Extern::from_wasmtime_export` because
1022         // there's no guarantee that there exists a trampoline for `f` so this
1023         // can't fall through to the case below
1024         if let crate::runtime::vm::Export::Function(f) = &export {
1025             let expected = match expected.unwrap_func() {
1026                 EngineOrModuleTypeIndex::Engine(e) => Some(e),
1027                 EngineOrModuleTypeIndex::Module(m) => module.signatures().shared_type(m),
1028                 EngineOrModuleTypeIndex::RecGroup(_) => unreachable!(),
1029             };
1030             let actual = unsafe { f.vm_func_ref(store).as_ref().type_index };
1031             assert_eq!(
1032                 expected,
1033                 Some(actual),
1034                 "type mismatch for import {imp_module:?} {imp_name:?}!!!\n\n\
1035                  expected {:#?}\n\n\
1036                  found {:#?}",
1037                 expected.and_then(|e| store.engine().signatures().borrow(e)),
1038                 store.engine().signatures().borrow(actual)
1039             );
1040             return;
1041         }
1042 
1043         let val = crate::Extern::from_wasmtime_export(export, store);
1044         let ty = DefinitionType::from(store, &val);
1045         crate::types::matching::MatchCx::new(module.engine())
1046             .definition(&expected, &ty)
1047             .expect("unexpected typecheck failure");
1048     }
1049 
1050     /// Convenience helper to return the `&ComponentInstance` that's being
1051     /// instantiated.
1052     fn instance<'b>(&self, store: &'b StoreOpaque) -> &'b ComponentInstance {
1053         store.store_data().component_instance(self.id)
1054     }
1055 
1056     /// Same as [`Self::instance`], but for mutability.
1057     fn instance_mut<'b>(&self, store: &'b mut StoreOpaque) -> Pin<&'b mut ComponentInstance> {
1058         store.store_data_mut().component_instance_mut(self.id)
1059     }
1060 
1061     // NB: This method is only intended to be called during the instantiation
1062     // process because the `Arc::get_mut` here is fallible and won't generally
1063     // succeed once the instance has been handed to the embedder. Before that
1064     // though it should be guaranteed that the single owning reference currently
1065     // lives within the `ComponentInstance` that's being built.
1066     fn instance_resource_types_mut<'b>(
1067         &self,
1068         store: &'b mut StoreOpaque,
1069     ) -> &'b mut ImportedResources {
1070         Arc::get_mut(self.instance_mut(store).resource_types_mut()).unwrap()
1071     }
1072 }
1073 
1074 /// A "pre-instantiated" [`Instance`] which has all of its arguments already
1075 /// supplied and is ready to instantiate.
1076 ///
1077 /// This structure represents an efficient form of instantiation where import
1078 /// type-checking and import lookup has all been resolved by the time that this
1079 /// type is created. This type is primarily created through the
1080 /// [`Linker::instantiate_pre`](crate::component::Linker::instantiate_pre)
1081 /// method.
1082 pub struct InstancePre<T: 'static> {
1083     component: Component,
1084     imports: Arc<PrimaryMap<RuntimeImportIndex, RuntimeImport>>,
1085     resource_types: Arc<PrimaryMap<ResourceIndex, ResourceType>>,
1086     _marker: marker::PhantomData<fn() -> T>,
1087 }
1088 
1089 // `InstancePre`'s clone does not require `T: Clone`
1090 impl<T: 'static> Clone for InstancePre<T> {
1091     fn clone(&self) -> Self {
1092         Self {
1093             component: self.component.clone(),
1094             imports: self.imports.clone(),
1095             resource_types: self.resource_types.clone(),
1096             _marker: self._marker,
1097         }
1098     }
1099 }
1100 
1101 impl<T: 'static> InstancePre<T> {
1102     /// This function is `unsafe` since there's no guarantee that the
1103     /// `RuntimeImport` items provided are guaranteed to work with the `T` of
1104     /// the store.
1105     ///
1106     /// Additionally there is no static guarantee that the `imports` provided
1107     /// satisfy the imports of the `component` provided.
1108     pub(crate) unsafe fn new_unchecked(
1109         component: Component,
1110         imports: Arc<PrimaryMap<RuntimeImportIndex, RuntimeImport>>,
1111         resource_types: Arc<PrimaryMap<ResourceIndex, ResourceType>>,
1112     ) -> InstancePre<T> {
1113         InstancePre {
1114             component,
1115             imports,
1116             resource_types,
1117             _marker: marker::PhantomData,
1118         }
1119     }
1120 
1121     /// Returns the underlying component that will be instantiated.
1122     pub fn component(&self) -> &Component {
1123         &self.component
1124     }
1125 
1126     #[doc(hidden)]
1127     /// Returns the type at which the underlying component will be
1128     /// instantiated. This contains the instantiated type information which
1129     /// was determined by the Linker.
1130     pub fn instance_type(&self) -> InstanceType<'_> {
1131         InstanceType {
1132             types: &self.component.types(),
1133             resources: &self.resource_types,
1134         }
1135     }
1136 
1137     /// Returns the underlying engine.
1138     pub fn engine(&self) -> &Engine {
1139         self.component.engine()
1140     }
1141 
1142     /// Performs the instantiation process into the store specified.
1143     //
1144     // TODO: needs more docs
1145     pub fn instantiate(&self, store: impl AsContextMut<Data = T>) -> Result<Instance> {
1146         assert!(
1147             !store.as_context().async_support(),
1148             "must use async instantiation when async support is enabled"
1149         );
1150         vm::assert_ready(self._instantiate(store))
1151     }
1152     /// Performs the instantiation process into the store specified.
1153     ///
1154     /// Exactly like [`Self::instantiate`] except for use on async stores.
1155     //
1156     // TODO: needs more docs
1157     #[cfg(feature = "async")]
1158     pub async fn instantiate_async(&self, store: impl AsContextMut<Data = T>) -> Result<Instance> {
1159         self._instantiate(store).await
1160     }
1161 
1162     async fn _instantiate(&self, mut store: impl AsContextMut<Data = T>) -> Result<Instance> {
1163         let mut store = store.as_context_mut();
1164         store
1165             .engine()
1166             .allocator()
1167             .increment_component_instance_count()?;
1168         let mut instantiator = Instantiator::new(&self.component, store.0, &self.imports)?;
1169         instantiator.run(&mut store).await.map_err(|e| {
1170             store
1171                 .engine()
1172                 .allocator()
1173                 .decrement_component_instance_count();
1174             e
1175         })?;
1176         let instance = Instance::from_wasmtime(store.0, instantiator.id);
1177         store.0.push_component_instance(instance);
1178         Ok(instance)
1179     }
1180 }
1181