wasmtime/runtime/component/
instance.rs

1use crate::component::func::HostFunc;
2use crate::component::matching::InstanceType;
3use crate::component::store::{ComponentInstanceId, StoreComponentInstanceId};
4use crate::component::{
5    Component, ComponentExportIndex, ComponentNamedList, Func, Lift, Lower, ResourceType,
6    TypedFunc, types::ComponentItem,
7};
8use crate::instance::OwnedImports;
9use crate::linker::DefinitionType;
10use crate::prelude::*;
11use crate::runtime::vm::component::{
12    CallContexts, ComponentInstance, ResourceTables, TypedResource, TypedResourceIndex,
13};
14use crate::runtime::vm::{self, VMFuncRef};
15use crate::store::StoreOpaque;
16use crate::{AsContext, AsContextMut, Engine, Module, StoreContextMut};
17use alloc::sync::Arc;
18use core::marker;
19use core::pin::Pin;
20use core::ptr::NonNull;
21use wasmtime_environ::{EngineOrModuleTypeIndex, component::*};
22use 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)]
40pub 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.
46const _: () = {
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
54impl 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        ty: TypeResourceTableIndex,
382        rep: u32,
383    ) -> Result<u32> {
384        let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
385        resource_tables(calls, instance).resource_new(TypedResource::Component { ty, rep })
386    }
387
388    /// Implementation of the `resource.rep` intrinsic for `i32`
389    /// representations.
390    pub(crate) fn resource_rep32(
391        self,
392        store: &mut StoreOpaque,
393        ty: TypeResourceTableIndex,
394        index: u32,
395    ) -> Result<u32> {
396        let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
397        resource_tables(calls, instance).resource_rep(TypedResourceIndex::Component { ty, index })
398    }
399
400    /// Implementation of the `resource.drop` intrinsic.
401    pub(crate) fn resource_drop(
402        self,
403        store: &mut StoreOpaque,
404        ty: TypeResourceTableIndex,
405        index: u32,
406    ) -> Result<Option<u32>> {
407        let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
408        resource_tables(calls, instance).resource_drop(TypedResourceIndex::Component { ty, index })
409    }
410
411    pub(crate) fn resource_transfer_own(
412        self,
413        store: &mut StoreOpaque,
414        index: u32,
415        src: TypeResourceTableIndex,
416        dst: TypeResourceTableIndex,
417    ) -> Result<u32> {
418        let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
419        let mut tables = resource_tables(calls, instance);
420        let rep = tables.resource_lift_own(TypedResourceIndex::Component { ty: src, index })?;
421        tables.resource_lower_own(TypedResource::Component { ty: dst, rep })
422    }
423
424    pub(crate) fn resource_transfer_borrow(
425        self,
426        store: &mut StoreOpaque,
427        index: u32,
428        src: TypeResourceTableIndex,
429        dst: TypeResourceTableIndex,
430    ) -> Result<u32> {
431        let dst_owns_resource = self.id().get(store).resource_owned_by_own_instance(dst);
432        let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
433        let mut tables = resource_tables(calls, instance);
434        let rep = tables.resource_lift_borrow(TypedResourceIndex::Component { ty: src, index })?;
435        // Implement `lower_borrow`'s special case here where if a borrow's
436        // resource type is owned by `dst` then the destination receives the
437        // representation directly rather than a handle to the representation.
438        //
439        // This can perhaps become a different libcall in the future to avoid
440        // this check at runtime since we know at compile time whether the
441        // destination type owns the resource, but that's left as a future
442        // refactoring if truly necessary.
443        if dst_owns_resource {
444            return Ok(rep);
445        }
446        tables.resource_lower_borrow(TypedResource::Component { ty: dst, rep })
447    }
448
449    pub(crate) fn resource_enter_call(self, store: &mut StoreOpaque) {
450        let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
451        resource_tables(calls, instance).enter_call()
452    }
453
454    pub(crate) fn resource_exit_call(self, store: &mut StoreOpaque) -> Result<()> {
455        let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
456        resource_tables(calls, instance).exit_call()
457    }
458
459    pub(crate) fn lookup_vmdef(&self, store: &mut StoreOpaque, def: &CoreDef) -> vm::Export {
460        lookup_vmdef(store, self.id.instance(), def)
461    }
462}
463
464/// Translates a `CoreDef`, a definition of a core wasm item, to an
465/// [`Export`] which is the runtime core wasm definition.
466pub(crate) fn lookup_vmdef(
467    store: &mut StoreOpaque,
468    id: ComponentInstanceId,
469    def: &CoreDef,
470) -> vm::Export {
471    match def {
472        CoreDef::Export(e) => lookup_vmexport(store, id, e),
473        CoreDef::Trampoline(idx) => {
474            let funcref = store
475                .store_data_mut()
476                .component_instance_mut(id)
477                .trampoline_func_ref(*idx);
478            // SAFETY: the `funcref` is owned by `store` and is valid within
479            // that store, so it's safe to create a `Func`.
480            vm::Export::Function(unsafe { crate::Func::from_vm_func_ref(store.id(), funcref) })
481        }
482        CoreDef::InstanceFlags(idx) => {
483            let id = StoreComponentInstanceId::new(store.id(), id);
484            vm::Export::Global(crate::Global::from_component_flags(id, *idx))
485        }
486    }
487}
488
489/// Translates a `CoreExport<T>`, an export of some core instance within
490/// this component, to the actual runtime definition of that item.
491pub(crate) fn lookup_vmexport<T>(
492    store: &mut StoreOpaque,
493    id: ComponentInstanceId,
494    item: &CoreExport<T>,
495) -> vm::Export
496where
497    T: Copy + Into<EntityIndex>,
498{
499    let store_id = store.id();
500    let id = store
501        .store_data_mut()
502        .component_instance_mut(id)
503        .instance(item.instance);
504    let instance = store.instance_mut(id);
505    let idx = match &item.item {
506        ExportItem::Index(idx) => (*idx).into(),
507
508        // FIXME: ideally at runtime we don't actually do any name lookups
509        // here. This will only happen when the host supplies an imported
510        // module so while the structure can't be known at compile time we
511        // do know at `InstancePre` time, for example, what all the host
512        // imports are. In theory we should be able to, as part of
513        // `InstancePre` construction, perform all name=>index mappings
514        // during that phase so the actual instantiation of an `InstancePre`
515        // skips all string lookups. This should probably only be
516        // investigated if this becomes a performance issue though.
517        ExportItem::Name(name) => instance.env_module().exports[name],
518    };
519    // SAFETY: the `store_id` owns this instance and all exports contained
520    // within.
521    unsafe { instance.get_export_by_index_mut(store_id, idx) }
522}
523
524fn resource_tables<'a>(
525    calls: &'a mut CallContexts,
526    instance: Pin<&'a mut ComponentInstance>,
527) -> ResourceTables<'a> {
528    ResourceTables {
529        host_table: None,
530        calls,
531        guest: Some(instance.guest_tables()),
532    }
533}
534
535/// Trait used to lookup the export of a component instance.
536///
537/// This trait is used as an implementation detail of [`Instance::get_func`]
538/// and related `get_*` methods. Notable implementors of this trait are:
539///
540/// * `str`
541/// * `String`
542/// * [`ComponentExportIndex`]
543///
544/// Note that this is intended to be a `wasmtime`-sealed trait so it shouldn't
545/// need to be implemented externally.
546pub trait InstanceExportLookup {
547    #[doc(hidden)]
548    fn lookup(&self, component: &Component) -> Option<ExportIndex>;
549}
550
551impl<T> InstanceExportLookup for &T
552where
553    T: InstanceExportLookup + ?Sized,
554{
555    fn lookup(&self, component: &Component) -> Option<ExportIndex> {
556        T::lookup(self, component)
557    }
558}
559
560impl InstanceExportLookup for str {
561    fn lookup(&self, component: &Component) -> Option<ExportIndex> {
562        component
563            .env_component()
564            .exports
565            .get(self, &NameMapNoIntern)
566            .copied()
567    }
568}
569
570impl InstanceExportLookup for String {
571    fn lookup(&self, component: &Component) -> Option<ExportIndex> {
572        str::lookup(self, component)
573    }
574}
575
576struct Instantiator<'a> {
577    component: &'a Component,
578    id: ComponentInstanceId,
579    core_imports: OwnedImports,
580    imports: &'a PrimaryMap<RuntimeImportIndex, RuntimeImport>,
581}
582
583pub(crate) enum RuntimeImport {
584    Func(Arc<HostFunc>),
585    Module(Module),
586    Resource {
587        ty: ResourceType,
588
589        // A strong reference to the host function that represents the
590        // destructor for this resource. At this time all resources here are
591        // host-defined resources. Note that this is itself never read because
592        // the funcref below points to it.
593        //
594        // Also note that the `Arc` here is used to support the same host
595        // function being used across multiple instances simultaneously. Or
596        // otherwise this makes `InstancePre::instantiate` possible to create
597        // separate instances all sharing the same host function.
598        _dtor: Arc<crate::func::HostFunc>,
599
600        // A raw function which is filled out (including `wasm_call`) which
601        // points to the internals of the `_dtor` field. This is read and
602        // possibly executed by wasm.
603        dtor_funcref: VMFuncRef,
604    },
605}
606
607pub type ImportedResources = PrimaryMap<ResourceIndex, ResourceType>;
608
609impl<'a> Instantiator<'a> {
610    fn new(
611        component: &'a Component,
612        store: &mut StoreOpaque,
613        imports: &'a Arc<PrimaryMap<RuntimeImportIndex, RuntimeImport>>,
614    ) -> Instantiator<'a> {
615        let env_component = component.env_component();
616        store.modules_mut().register_component(component);
617        let imported_resources: ImportedResources =
618            PrimaryMap::with_capacity(env_component.imported_resources.len());
619
620        let instance = ComponentInstance::new(
621            store.store_data().components.next_component_instance_id(),
622            component,
623            Arc::new(imported_resources),
624            imports,
625            store.traitobj(),
626        );
627        let id = store.store_data_mut().push_component_instance(instance);
628
629        Instantiator {
630            component,
631            imports,
632            core_imports: OwnedImports::empty(),
633            id,
634        }
635    }
636
637    fn run<T>(&mut self, store: &mut StoreContextMut<'_, T>) -> Result<()> {
638        let env_component = self.component.env_component();
639
640        // Before all initializers are processed configure all destructors for
641        // host-defined resources. No initializer will correspond to these and
642        // it's required to happen before they're needed, so execute this first.
643        for (idx, import) in env_component.imported_resources.iter() {
644            let (ty, func_ref) = match &self.imports[*import] {
645                RuntimeImport::Resource {
646                    ty, dtor_funcref, ..
647                } => (*ty, NonNull::from(dtor_funcref)),
648                _ => unreachable!(),
649            };
650            let i = self.instance_resource_types_mut(store.0).push(ty);
651            assert_eq!(i, idx);
652            self.instance_mut(store.0)
653                .set_resource_destructor(idx, Some(func_ref));
654        }
655
656        // Next configure all `VMFuncRef`s for trampolines that this component
657        // will require. These functions won't actually get used until their
658        // associated state has been initialized through the global initializers
659        // below, but the funcrefs can all be configured here.
660        for (idx, sig) in env_component.trampolines.iter() {
661            let ptrs = self.component.trampoline_ptrs(idx);
662            let signature = match self.component.signatures().shared_type(*sig) {
663                Some(s) => s,
664                None => panic!("found unregistered signature: {sig:?}"),
665            };
666
667            self.instance_mut(store.0).set_trampoline(
668                idx,
669                ptrs.wasm_call,
670                ptrs.array_call,
671                signature,
672            );
673        }
674
675        for initializer in env_component.initializers.iter() {
676            match initializer {
677                GlobalInitializer::InstantiateModule(m) => {
678                    let module;
679                    let imports = match m {
680                        // Since upvars are statically know we know that the
681                        // `args` list is already in the right order.
682                        InstantiateModule::Static(idx, args) => {
683                            module = self.component.static_module(*idx);
684                            self.build_imports(store.0, module, args.iter())
685                        }
686
687                        // With imports, unlike upvars, we need to do runtime
688                        // lookups with strings to determine the order of the
689                        // imports since it's whatever the actual module
690                        // requires.
691                        //
692                        // FIXME: see the note in `ExportItem::Name` handling
693                        // above for how we ideally shouldn't do string lookup
694                        // here.
695                        InstantiateModule::Import(idx, args) => {
696                            module = match &self.imports[*idx] {
697                                RuntimeImport::Module(m) => m,
698                                _ => unreachable!(),
699                            };
700                            let args = module
701                                .imports()
702                                .map(|import| &args[import.module()][import.name()]);
703                            self.build_imports(store.0, module, args)
704                        }
705                    };
706
707                    // Note that the unsafety here should be ok because the
708                    // validity of the component means that type-checks have
709                    // already been performed. This means that the unsafety due
710                    // to imports having the wrong type should not happen here.
711                    //
712                    // Also note we are calling new_started_impl because we have
713                    // already checked for asyncness and are running on a fiber
714                    // if required.
715
716                    let i = unsafe {
717                        crate::Instance::new_started_impl(store, module, imports.as_ref())?
718                    };
719                    self.instance_mut(store.0).push_instance_id(i.id());
720                }
721
722                GlobalInitializer::LowerImport { import, index } => {
723                    let func = match &self.imports[*import] {
724                        RuntimeImport::Func(func) => func,
725                        _ => unreachable!(),
726                    };
727                    self.instance_mut(store.0)
728                        .set_lowering(*index, func.lowering());
729                }
730
731                GlobalInitializer::ExtractTable(table) => self.extract_table(store.0, table),
732
733                GlobalInitializer::ExtractMemory(mem) => self.extract_memory(store.0, mem),
734
735                GlobalInitializer::ExtractRealloc(realloc) => {
736                    self.extract_realloc(store.0, realloc)
737                }
738
739                GlobalInitializer::ExtractCallback(callback) => {
740                    self.extract_callback(store.0, callback)
741                }
742
743                GlobalInitializer::ExtractPostReturn(post_return) => {
744                    self.extract_post_return(store.0, post_return)
745                }
746
747                GlobalInitializer::Resource(r) => self.resource(store.0, r),
748            }
749        }
750        Ok(())
751    }
752
753    fn resource(&mut self, store: &mut StoreOpaque, resource: &Resource) {
754        let dtor = resource
755            .dtor
756            .as_ref()
757            .map(|dtor| lookup_vmdef(store, self.id, dtor));
758        let dtor = dtor.map(|export| match export {
759            crate::runtime::vm::Export::Function(f) => f.vm_func_ref(store),
760            _ => unreachable!(),
761        });
762        let index = self
763            .component
764            .env_component()
765            .resource_index(resource.index);
766        let instance = self.instance(store);
767        let ty = ResourceType::guest(store.id(), instance, resource.index);
768        self.instance_mut(store)
769            .set_resource_destructor(index, dtor);
770        let i = self.instance_resource_types_mut(store).push(ty);
771        debug_assert_eq!(i, index);
772    }
773
774    fn extract_memory(&mut self, store: &mut StoreOpaque, memory: &ExtractMemory) {
775        let mem = match lookup_vmexport(store, self.id, &memory.export) {
776            crate::runtime::vm::Export::Memory { memory, .. } => memory,
777            _ => unreachable!(),
778        };
779        let import = mem.vmimport(store);
780        self.instance_mut(store)
781            .set_runtime_memory(memory.index, import.from.as_non_null());
782    }
783
784    fn extract_realloc(&mut self, store: &mut StoreOpaque, realloc: &ExtractRealloc) {
785        let func_ref = match lookup_vmdef(store, self.id, &realloc.def) {
786            crate::runtime::vm::Export::Function(f) => f.vm_func_ref(store),
787            _ => unreachable!(),
788        };
789        self.instance_mut(store)
790            .set_runtime_realloc(realloc.index, func_ref);
791    }
792
793    fn extract_callback(&mut self, store: &mut StoreOpaque, callback: &ExtractCallback) {
794        let func_ref = match lookup_vmdef(store, self.id, &callback.def) {
795            crate::runtime::vm::Export::Function(f) => f.vm_func_ref(store),
796            _ => unreachable!(),
797        };
798        self.instance_mut(store)
799            .set_runtime_callback(callback.index, func_ref);
800    }
801
802    fn extract_post_return(&mut self, store: &mut StoreOpaque, post_return: &ExtractPostReturn) {
803        let func_ref = match lookup_vmdef(store, self.id, &post_return.def) {
804            crate::runtime::vm::Export::Function(f) => f.vm_func_ref(store),
805            _ => unreachable!(),
806        };
807        self.instance_mut(store)
808            .set_runtime_post_return(post_return.index, func_ref);
809    }
810
811    fn extract_table(&mut self, store: &mut StoreOpaque, table: &ExtractTable) {
812        let export = match lookup_vmexport(store, self.id, &table.export) {
813            crate::runtime::vm::Export::Table(t) => t,
814            _ => unreachable!(),
815        };
816        let import = export.vmimport(store);
817        self.instance_mut(store)
818            .set_runtime_table(table.index, import);
819    }
820
821    fn build_imports<'b>(
822        &mut self,
823        store: &mut StoreOpaque,
824        module: &Module,
825        args: impl Iterator<Item = &'b CoreDef>,
826    ) -> &OwnedImports {
827        self.core_imports.clear();
828        self.core_imports.reserve(module);
829        let mut imports = module.compiled_module().module().imports();
830
831        for arg in args {
832            // The general idea of Wasmtime is that at runtime type-checks for
833            // core wasm instantiations internally within a component are
834            // unnecessary and superfluous. Naturally though mistakes may be
835            // made, so double-check this property of wasmtime in debug mode.
836
837            if cfg!(debug_assertions) {
838                let (imp_module, imp_name, expected) = imports.next().unwrap();
839                self.assert_type_matches(store, module, arg, imp_module, imp_name, expected);
840            }
841
842            // The unsafety here should be ok since the `export` is loaded
843            // directly from an instance which should only give us valid export
844            // items.
845            let export = lookup_vmdef(store, self.id, arg);
846            self.core_imports.push_export(store, &export);
847        }
848        debug_assert!(imports.next().is_none());
849
850        &self.core_imports
851    }
852
853    fn assert_type_matches(
854        &self,
855        store: &mut StoreOpaque,
856        module: &Module,
857        arg: &CoreDef,
858        imp_module: &str,
859        imp_name: &str,
860        expected: EntityType,
861    ) {
862        let export = lookup_vmdef(store, self.id, arg);
863
864        // If this value is a core wasm function then the type check is inlined
865        // here. This can otherwise fail `Extern::from_wasmtime_export` because
866        // there's no guarantee that there exists a trampoline for `f` so this
867        // can't fall through to the case below
868        if let crate::runtime::vm::Export::Function(f) = &export {
869            let expected = match expected.unwrap_func() {
870                EngineOrModuleTypeIndex::Engine(e) => Some(e),
871                EngineOrModuleTypeIndex::Module(m) => module.signatures().shared_type(m),
872                EngineOrModuleTypeIndex::RecGroup(_) => unreachable!(),
873            };
874            let actual = unsafe { f.vm_func_ref(store).as_ref().type_index };
875            assert_eq!(
876                expected,
877                Some(actual),
878                "type mismatch for import {imp_module:?} {imp_name:?}!!!\n\n\
879                 expected {:#?}\n\n\
880                 found {:#?}",
881                expected.and_then(|e| store.engine().signatures().borrow(e)),
882                store.engine().signatures().borrow(actual)
883            );
884            return;
885        }
886
887        let val = unsafe { crate::Extern::from_wasmtime_export(export, store) };
888        let ty = DefinitionType::from(store, &val);
889        crate::types::matching::MatchCx::new(module.engine())
890            .definition(&expected, &ty)
891            .expect("unexpected typecheck failure");
892    }
893
894    /// Convenience helper to return the `&ComponentInstance` that's being
895    /// instantiated.
896    fn instance<'b>(&self, store: &'b StoreOpaque) -> &'b ComponentInstance {
897        store.store_data().component_instance(self.id)
898    }
899
900    /// Same as [`Self::instance`], but for mutability.
901    fn instance_mut<'b>(&self, store: &'b mut StoreOpaque) -> Pin<&'b mut ComponentInstance> {
902        store.store_data_mut().component_instance_mut(self.id)
903    }
904
905    // NB: This method is only intended to be called during the instantiation
906    // process because the `Arc::get_mut` here is fallible and won't generally
907    // succeed once the instance has been handed to the embedder. Before that
908    // though it should be guaranteed that the single owning reference currently
909    // lives within the `ComponentInstance` that's being built.
910    fn instance_resource_types_mut<'b>(
911        &self,
912        store: &'b mut StoreOpaque,
913    ) -> &'b mut ImportedResources {
914        Arc::get_mut(self.instance_mut(store).resource_types_mut()).unwrap()
915    }
916}
917
918/// A "pre-instantiated" [`Instance`] which has all of its arguments already
919/// supplied and is ready to instantiate.
920///
921/// This structure represents an efficient form of instantiation where import
922/// type-checking and import lookup has all been resolved by the time that this
923/// type is created. This type is primarily created through the
924/// [`Linker::instantiate_pre`](crate::component::Linker::instantiate_pre)
925/// method.
926pub struct InstancePre<T: 'static> {
927    component: Component,
928    imports: Arc<PrimaryMap<RuntimeImportIndex, RuntimeImport>>,
929    resource_types: Arc<PrimaryMap<ResourceIndex, ResourceType>>,
930    _marker: marker::PhantomData<fn() -> T>,
931}
932
933// `InstancePre`'s clone does not require `T: Clone`
934impl<T: 'static> Clone for InstancePre<T> {
935    fn clone(&self) -> Self {
936        Self {
937            component: self.component.clone(),
938            imports: self.imports.clone(),
939            resource_types: self.resource_types.clone(),
940            _marker: self._marker,
941        }
942    }
943}
944
945impl<T: 'static> InstancePre<T> {
946    /// This function is `unsafe` since there's no guarantee that the
947    /// `RuntimeImport` items provided are guaranteed to work with the `T` of
948    /// the store.
949    ///
950    /// Additionally there is no static guarantee that the `imports` provided
951    /// satisfy the imports of the `component` provided.
952    pub(crate) unsafe fn new_unchecked(
953        component: Component,
954        imports: Arc<PrimaryMap<RuntimeImportIndex, RuntimeImport>>,
955        resource_types: Arc<PrimaryMap<ResourceIndex, ResourceType>>,
956    ) -> InstancePre<T> {
957        InstancePre {
958            component,
959            imports,
960            resource_types,
961            _marker: marker::PhantomData,
962        }
963    }
964
965    /// Returns the underlying component that will be instantiated.
966    pub fn component(&self) -> &Component {
967        &self.component
968    }
969
970    #[doc(hidden)]
971    /// Returns the type at which the underlying component will be
972    /// instantiated. This contains the instantiated type information which
973    /// was determined by the Linker.
974    pub fn instance_type(&self) -> InstanceType<'_> {
975        InstanceType {
976            types: &self.component.types(),
977            resources: &self.resource_types,
978        }
979    }
980
981    /// Returns the underlying engine.
982    pub fn engine(&self) -> &Engine {
983        self.component.engine()
984    }
985
986    /// Performs the instantiation process into the store specified.
987    //
988    // TODO: needs more docs
989    pub fn instantiate(&self, store: impl AsContextMut<Data = T>) -> Result<Instance> {
990        assert!(
991            !store.as_context().async_support(),
992            "must use async instantiation when async support is enabled"
993        );
994        self.instantiate_impl(store)
995    }
996    /// Performs the instantiation process into the store specified.
997    ///
998    /// Exactly like [`Self::instantiate`] except for use on async stores.
999    //
1000    // TODO: needs more docs
1001    #[cfg(feature = "async")]
1002    pub async fn instantiate_async(
1003        &self,
1004        mut store: impl AsContextMut<Data = T>,
1005    ) -> Result<Instance>
1006    where
1007        T: Send,
1008    {
1009        let mut store = store.as_context_mut();
1010        assert!(
1011            store.0.async_support(),
1012            "must use sync instantiation when async support is disabled"
1013        );
1014        store.on_fiber(|store| self.instantiate_impl(store)).await?
1015    }
1016
1017    fn instantiate_impl(&self, mut store: impl AsContextMut<Data = T>) -> Result<Instance> {
1018        let mut store = store.as_context_mut();
1019        store
1020            .engine()
1021            .allocator()
1022            .increment_component_instance_count()?;
1023        let mut instantiator = Instantiator::new(&self.component, store.0, &self.imports);
1024        instantiator.run(&mut store).map_err(|e| {
1025            store
1026                .engine()
1027                .allocator()
1028                .decrement_component_instance_count();
1029            e
1030        })?;
1031        let instance = Instance::from_wasmtime(store.0, instantiator.id);
1032        store.0.push_component_instance(instance);
1033        Ok(instance)
1034    }
1035}