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::VMFuncRef;
12 use crate::runtime::vm::component::{ComponentInstance, OwnedComponentInstance};
13 use crate::store::StoreOpaque;
14 use crate::{AsContext, AsContextMut, Engine, Module, StoreContextMut};
15 use alloc::sync::Arc;
16 use core::marker;
17 use core::ptr::NonNull;
18 use wasmtime_environ::{EngineOrModuleTypeIndex, component::*};
19 use wasmtime_environ::{EntityType, PrimaryMap};
20 
21 /// An instantiated component.
22 ///
23 /// This type represents an instantiated [`Component`](super::Component).
24 /// Instances have exports which can be accessed through functions such as
25 /// [`Instance::get_func`] or [`Instance::get_export`]. Instances are owned by a
26 /// [`Store`](crate::Store) and all methods require a handle to the store.
27 ///
28 /// Component instances are created through
29 /// [`Linker::instantiate`](super::Linker::instantiate) and its family of
30 /// methods.
31 ///
32 /// This type is similar to the core wasm version
33 /// [`wasmtime::Instance`](crate::Instance) except that it represents an
34 /// instantiated component instead of an instantiated module.
35 #[derive(Copy, Clone, Debug)]
36 #[repr(transparent)]
37 pub struct Instance {
38     id: StoreComponentInstanceId,
39 }
40 
41 // Double-check that the C representation in `component/instance.h` matches our
42 // in-Rust representation here in terms of size/alignment/etc.
43 const _: () = {
44     #[repr(C)]
45     struct C(u64, u32);
46     assert!(core::mem::size_of::<C>() == core::mem::size_of::<Instance>());
47     assert!(core::mem::align_of::<C>() == core::mem::align_of::<Instance>());
48     assert!(core::mem::offset_of!(Instance, id) == 0);
49 };
50 
51 impl Instance {
52     /// Creates a raw `Instance` from the internal identifiers within the store.
53     pub(crate) fn from_wasmtime(store: &StoreOpaque, id: ComponentInstanceId) -> Instance {
54         Instance {
55             id: StoreComponentInstanceId::new(store.id(), id),
56         }
57     }
58 
59     /// Looks up an exported function by name within this [`Instance`].
60     ///
61     /// The `store` argument provided must be the store that this instance
62     /// lives within and the `name` argument is the lookup key by which to find
63     /// the exported function. If the function is found then `Some` is returned
64     /// and otherwise `None` is returned.
65     ///
66     /// The `name` here can be a string such as `&str` or it can be a
67     /// [`ComponentExportIndex`] which is loaded prior from a [`Component`].
68     ///
69     /// # Panics
70     ///
71     /// Panics if `store` does not own this instance.
72     ///
73     /// # Examples
74     ///
75     /// Looking up a function which is exported from the root of a component:
76     ///
77     /// ```
78     /// use wasmtime::{Engine, Store};
79     /// use wasmtime::component::{Component, Linker};
80     ///
81     /// # fn main() -> wasmtime::Result<()> {
82     /// let engine = Engine::default();
83     /// let component = Component::new(
84     ///     &engine,
85     ///     r#"
86     ///         (component
87     ///             (core module $m
88     ///                 (func (export "f"))
89     ///             )
90     ///             (core instance $i (instantiate $m))
91     ///             (func (export "f")
92     ///                 (canon lift (core func $i "f")))
93     ///         )
94     ///     "#,
95     /// )?;
96     ///
97     /// // Look up the function by name
98     /// let mut store = Store::new(&engine, ());
99     /// let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
100     /// let func = instance.get_func(&mut store, "f").unwrap();
101     ///
102     /// // The function can also be looked up by an index via a precomputed index.
103     /// let export = component.get_export_index(None, "f").unwrap();
104     /// let func = instance.get_func(&mut store, &export).unwrap();
105     /// # Ok(())
106     /// # }
107     /// ```
108     ///
109     /// Looking up a function which is exported from a nested instance:
110     ///
111     /// ```
112     /// use wasmtime::{Engine, Store};
113     /// use wasmtime::component::{Component, Linker};
114     ///
115     /// # fn main() -> wasmtime::Result<()> {
116     /// let engine = Engine::default();
117     /// let component = Component::new(
118     ///     &engine,
119     ///     r#"
120     ///         (component
121     ///             (core module $m
122     ///                 (func (export "f"))
123     ///             )
124     ///             (core instance $i (instantiate $m))
125     ///             (func $f
126     ///                 (canon lift (core func $i "f")))
127     ///
128     ///             (instance $i
129     ///                 (export "f" (func $f)))
130     ///             (export "i" (instance $i))
131     ///         )
132     ///     "#,
133     /// )?;
134     ///
135     /// // First look up the exported instance, then use that to lookup the
136     /// // exported function.
137     /// let instance_index = component.get_export_index(None, "i").unwrap();
138     /// let func_index = component.get_export_index(Some(&instance_index), "f").unwrap();
139     ///
140     /// // Then use `func_index` at runtime.
141     /// let mut store = Store::new(&engine, ());
142     /// let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
143     /// let func = instance.get_func(&mut store, &func_index).unwrap();
144     ///
145     /// // Alternatively the `instance` can be used directly in conjunction with
146     /// // the `get_export_index` method.
147     /// let instance_index = instance.get_export_index(&mut store, None, "i").unwrap();
148     /// let func_index = instance.get_export_index(&mut store, Some(&instance_index), "f").unwrap();
149     /// let func = instance.get_func(&mut store, &func_index).unwrap();
150     /// # Ok(())
151     /// # }
152     /// ```
153     pub fn get_func(
154         &self,
155         mut store: impl AsContextMut,
156         name: impl InstanceExportLookup,
157     ) -> Option<Func> {
158         let store = store.as_context_mut().0;
159         let instance = &store[self.id];
160         let component = instance.component();
161 
162         // Validate that `name` exists within `self.`
163         let index = name.lookup(component)?;
164 
165         // Validate that `index` is indeed a lifted function.
166         match &component.env_component().export_items[index] {
167             Export::LiftedFunction { .. } => {}
168             _ => return None,
169         }
170 
171         // And package up the indices!
172         Some(Func::from_lifted_func(*self, index))
173     }
174 
175     /// Looks up an exported [`Func`] value by name and with its type.
176     ///
177     /// This function is a convenience wrapper over [`Instance::get_func`] and
178     /// [`Func::typed`]. For more information see the linked documentation.
179     ///
180     /// Returns an error if `name` isn't a function export or if the export's
181     /// type did not match `Params` or `Results`
182     ///
183     /// # Panics
184     ///
185     /// Panics if `store` does not own this instance.
186     pub fn get_typed_func<Params, Results>(
187         &self,
188         mut store: impl AsContextMut,
189         name: impl InstanceExportLookup,
190     ) -> Result<TypedFunc<Params, Results>>
191     where
192         Params: ComponentNamedList + Lower,
193         Results: ComponentNamedList + Lift,
194     {
195         let f = self
196             .get_func(store.as_context_mut(), name)
197             .ok_or_else(|| anyhow!("failed to find function export"))?;
198         Ok(f.typed::<Params, Results>(store)
199             .with_context(|| format!("failed to convert function to given type"))?)
200     }
201 
202     /// Looks up an exported module by name within this [`Instance`].
203     ///
204     /// The `store` argument provided must be the store that this instance
205     /// lives within and the `name` argument is the lookup key by which to find
206     /// the exported module. If the module is found then `Some` is returned
207     /// and otherwise `None` is returned.
208     ///
209     /// The `name` here can be a string such as `&str` or it can be a
210     /// [`ComponentExportIndex`] which is loaded prior from a [`Component`].
211     ///
212     /// For some examples see [`Instance::get_func`] for loading values from a
213     /// component.
214     ///
215     /// # Panics
216     ///
217     /// Panics if `store` does not own this instance.
218     pub fn get_module(
219         &self,
220         mut store: impl AsContextMut,
221         name: impl InstanceExportLookup,
222     ) -> Option<Module> {
223         let store = store.as_context_mut().0;
224         let (instance, export) = self.lookup_export(store, name)?;
225         match export {
226             Export::ModuleStatic { index, .. } => {
227                 Some(instance.component().static_module(*index).clone())
228             }
229             Export::ModuleImport { import, .. } => match instance.runtime_import(*import) {
230                 RuntimeImport::Module(m) => Some(m.clone()),
231                 _ => unreachable!(),
232             },
233             _ => None,
234         }
235     }
236 
237     /// Looks up an exported resource type by name within this [`Instance`].
238     ///
239     /// The `store` argument provided must be the store that this instance
240     /// lives within and the `name` argument is the lookup key by which to find
241     /// the exported resource. If the resource is found then `Some` is returned
242     /// and otherwise `None` is returned.
243     ///
244     /// The `name` here can be a string such as `&str` or it can be a
245     /// [`ComponentExportIndex`] which is loaded prior from a [`Component`].
246     ///
247     /// For some examples see [`Instance::get_func`] for loading values from a
248     /// component.
249     ///
250     /// # Panics
251     ///
252     /// Panics if `store` does not own this instance.
253     pub fn get_resource(
254         &self,
255         mut store: impl AsContextMut,
256         name: impl InstanceExportLookup,
257     ) -> Option<ResourceType> {
258         let store = store.as_context_mut().0;
259         let (instance, export) = self.lookup_export(store, name)?;
260         match export {
261             Export::Type(TypeDef::Resource(id)) => {
262                 Some(InstanceType::new(instance).resource_type(*id))
263             }
264             Export::Type(_)
265             | Export::LiftedFunction { .. }
266             | Export::ModuleStatic { .. }
267             | Export::ModuleImport { .. }
268             | Export::Instance { .. } => None,
269         }
270     }
271 
272     /// A methods similar to [`Component::get_export`] except for this
273     /// instance.
274     ///
275     /// This method will lookup the `name` provided within the `instance`
276     /// provided and return a [`ComponentItem`] describing the export,
277     /// and [`ComponentExportIndex`] which can be passed other `get_*`
278     /// functions like [`Instance::get_func`].
279     ///
280     /// The [`ComponentItem`] is more expensive to compute than the
281     /// [`ComponentExportIndex`]. If you are not consuming the
282     /// [`ComponentItem`], use [`Instance::get_export_index`] instead.
283     ///
284     /// # Panics
285     ///
286     /// Panics if `store` does not own this instance.
287     pub fn get_export(
288         &self,
289         mut store: impl AsContextMut,
290         instance: Option<&ComponentExportIndex>,
291         name: &str,
292     ) -> Option<(ComponentItem, ComponentExportIndex)> {
293         self._get_export(store.as_context_mut().0, instance, name)
294     }
295 
296     fn _get_export(
297         &self,
298         store: &StoreOpaque,
299         instance: Option<&ComponentExportIndex>,
300         name: &str,
301     ) -> Option<(ComponentItem, ComponentExportIndex)> {
302         let data = &store[self.id()];
303         let component = data.component();
304         let index = component.lookup_export_index(instance, name)?;
305         let item = ComponentItem::from_export(
306             &store.engine(),
307             &component.env_component().export_items[index],
308             &InstanceType::new(data),
309         );
310         Some((
311             item,
312             ComponentExportIndex {
313                 id: data.component().id(),
314                 index,
315             },
316         ))
317     }
318 
319     /// A methods similar to [`Component::get_export_index`] except for this
320     /// instance.
321     ///
322     /// This method will lookup the `name` provided within the `instance`
323     /// provided and return a [`ComponentExportIndex`] which can be passed
324     /// other `get_*` functions like [`Instance::get_func`].
325     ///
326     /// If you need the [`ComponentItem`] corresponding to this export, use
327     /// the [`Instance::get_export`] instead.
328     ///
329     /// # Panics
330     ///
331     /// Panics if `store` does not own this instance.
332     pub fn get_export_index(
333         &self,
334         mut store: impl AsContextMut,
335         instance: Option<&ComponentExportIndex>,
336         name: &str,
337     ) -> Option<ComponentExportIndex> {
338         let data = &store.as_context_mut()[self.id()];
339         let index = data.component().lookup_export_index(instance, name)?;
340         Some(ComponentExportIndex {
341             id: data.component().id(),
342             index,
343         })
344     }
345 
346     fn lookup_export<'a>(
347         &self,
348         store: &'a StoreOpaque,
349         name: impl InstanceExportLookup,
350     ) -> Option<(&'a ComponentInstance, &'a Export)> {
351         let data = &store[self.id()];
352         let index = name.lookup(data.component())?;
353         Some((data, &data.component().env_component().export_items[index]))
354     }
355 
356     /// Returns the [`InstancePre`] that was used to create this instance.
357     pub fn instance_pre<T>(&self, store: impl AsContext<Data = T>) -> InstancePre<T> {
358         // This indexing operation asserts the Store owns the Instance.
359         // Therefore, the InstancePre<T> must match the Store<T>.
360         let data = &store.as_context()[self.id()];
361 
362         // SAFETY: calling this method safely here relies on matching the `T`
363         // in `InstancePre<T>` to the store itself, which is happening in the
364         // type signature just above by ensuring the store's data is `T` which
365         // matches the return value.
366         unsafe { data.instance_pre() }
367     }
368 
369     /// Returns the VM/runtime state for this instance as belonging to the
370     /// store provided.
371     pub(crate) fn instance_ptr(&self, store: &StoreOpaque) -> NonNull<ComponentInstance> {
372         self.id.assert_belongs_to(store.id());
373         store.component_instance_ptr(self.id.instance())
374     }
375 
376     pub(crate) fn id(&self) -> StoreComponentInstanceId {
377         self.id
378     }
379 }
380 
381 /// Trait used to lookup the export of a component instance.
382 ///
383 /// This trait is used as an implementation detail of [`Instance::get_func`]
384 /// and related `get_*` methods. Notable implementors of this trait are:
385 ///
386 /// * `str`
387 /// * `String`
388 /// * [`ComponentExportIndex`]
389 ///
390 /// Note that this is intended to be a `wasmtime`-sealed trait so it shouldn't
391 /// need to be implemented externally.
392 pub trait InstanceExportLookup {
393     #[doc(hidden)]
394     fn lookup(&self, component: &Component) -> Option<ExportIndex>;
395 }
396 
397 impl<T> InstanceExportLookup for &T
398 where
399     T: InstanceExportLookup + ?Sized,
400 {
401     fn lookup(&self, component: &Component) -> Option<ExportIndex> {
402         T::lookup(self, component)
403     }
404 }
405 
406 impl InstanceExportLookup for str {
407     fn lookup(&self, component: &Component) -> Option<ExportIndex> {
408         component
409             .env_component()
410             .exports
411             .get(self, &NameMapNoIntern)
412             .copied()
413     }
414 }
415 
416 impl InstanceExportLookup for String {
417     fn lookup(&self, component: &Component) -> Option<ExportIndex> {
418         str::lookup(self, component)
419     }
420 }
421 
422 struct Instantiator<'a> {
423     component: &'a Component,
424     id: ComponentInstanceId,
425     core_imports: OwnedImports,
426     imports: &'a PrimaryMap<RuntimeImportIndex, RuntimeImport>,
427 }
428 
429 pub(crate) enum RuntimeImport {
430     Func(Arc<HostFunc>),
431     Module(Module),
432     Resource {
433         ty: ResourceType,
434 
435         // A strong reference to the host function that represents the
436         // destructor for this resource. At this time all resources here are
437         // host-defined resources. Note that this is itself never read because
438         // the funcref below points to it.
439         //
440         // Also note that the `Arc` here is used to support the same host
441         // function being used across multiple instances simultaneously. Or
442         // otherwise this makes `InstancePre::instantiate` possible to create
443         // separate instances all sharing the same host function.
444         _dtor: Arc<crate::func::HostFunc>,
445 
446         // A raw function which is filled out (including `wasm_call`) which
447         // points to the internals of the `_dtor` field. This is read and
448         // possibly executed by wasm.
449         dtor_funcref: VMFuncRef,
450     },
451 }
452 
453 pub type ImportedResources = PrimaryMap<ResourceIndex, ResourceType>;
454 
455 impl<'a> Instantiator<'a> {
456     fn new(
457         component: &'a Component,
458         store: &mut StoreOpaque,
459         imports: &'a Arc<PrimaryMap<RuntimeImportIndex, RuntimeImport>>,
460     ) -> Instantiator<'a> {
461         let env_component = component.env_component();
462         store.modules_mut().register_component(component);
463         let imported_resources: ImportedResources =
464             PrimaryMap::with_capacity(env_component.imported_resources.len());
465 
466         let instance = OwnedComponentInstance::new(
467             store.store_data().components.next_component_instance_id(),
468             component,
469             Arc::new(imported_resources),
470             imports,
471             store.traitobj(),
472         );
473         let id = store.store_data_mut().push_component_instance(instance);
474 
475         Instantiator {
476             component,
477             imports,
478             core_imports: OwnedImports::empty(),
479             id,
480         }
481     }
482 
483     fn run<T>(&mut self, store: &mut StoreContextMut<'_, T>) -> Result<()> {
484         let env_component = self.component.env_component();
485 
486         // Before all initializers are processed configure all destructors for
487         // host-defined resources. No initializer will correspond to these and
488         // it's required to happen before they're needed, so execute this first.
489         for (idx, import) in env_component.imported_resources.iter() {
490             let (ty, func_ref) = match &self.imports[*import] {
491                 RuntimeImport::Resource {
492                     ty, dtor_funcref, ..
493                 } => (*ty, NonNull::from(dtor_funcref)),
494                 _ => unreachable!(),
495             };
496             let i = self.instance_resource_types_mut(store.0).push(ty);
497             assert_eq!(i, idx);
498             self.instance_mut(store.0)
499                 .set_resource_destructor(idx, Some(func_ref));
500         }
501 
502         // Next configure all `VMFuncRef`s for trampolines that this component
503         // will require. These functions won't actually get used until their
504         // associated state has been initialized through the global initializers
505         // below, but the funcrefs can all be configured here.
506         for (idx, sig) in env_component.trampolines.iter() {
507             let ptrs = self.component.trampoline_ptrs(idx);
508             let signature = match self.component.signatures().shared_type(*sig) {
509                 Some(s) => s,
510                 None => panic!("found unregistered signature: {sig:?}"),
511             };
512 
513             self.instance_mut(store.0).set_trampoline(
514                 idx,
515                 ptrs.wasm_call,
516                 ptrs.array_call,
517                 signature,
518             );
519         }
520 
521         for initializer in env_component.initializers.iter() {
522             match initializer {
523                 GlobalInitializer::InstantiateModule(m) => {
524                     let module;
525                     let imports = match m {
526                         // Since upvars are statically know we know that the
527                         // `args` list is already in the right order.
528                         InstantiateModule::Static(idx, args) => {
529                             module = self.component.static_module(*idx);
530                             self.build_imports(store.0, module, args.iter())
531                         }
532 
533                         // With imports, unlike upvars, we need to do runtime
534                         // lookups with strings to determine the order of the
535                         // imports since it's whatever the actual module
536                         // requires.
537                         //
538                         // FIXME: see the note in `ExportItem::Name` handling
539                         // above for how we ideally shouldn't do string lookup
540                         // here.
541                         InstantiateModule::Import(idx, args) => {
542                             module = match &self.imports[*idx] {
543                                 RuntimeImport::Module(m) => m,
544                                 _ => unreachable!(),
545                             };
546                             let args = module
547                                 .imports()
548                                 .map(|import| &args[import.module()][import.name()]);
549                             self.build_imports(store.0, module, args)
550                         }
551                     };
552 
553                     // Note that the unsafety here should be ok because the
554                     // validity of the component means that type-checks have
555                     // already been performed. This means that the unsafety due
556                     // to imports having the wrong type should not happen here.
557                     //
558                     // Also note we are calling new_started_impl because we have
559                     // already checked for asyncness and are running on a fiber
560                     // if required.
561 
562                     let i = unsafe {
563                         crate::Instance::new_started_impl(store, module, imports.as_ref())?
564                     };
565                     self.instance_mut(store.0).push_instance_id(i.id());
566                 }
567 
568                 GlobalInitializer::LowerImport { import, index } => {
569                     let func = match &self.imports[*import] {
570                         RuntimeImport::Func(func) => func,
571                         _ => unreachable!(),
572                     };
573                     self.instance_mut(store.0)
574                         .set_lowering(*index, func.lowering());
575                 }
576 
577                 GlobalInitializer::ExtractTable(table) => self.extract_table(store.0, table),
578 
579                 GlobalInitializer::ExtractMemory(mem) => self.extract_memory(store.0, mem),
580 
581                 GlobalInitializer::ExtractRealloc(realloc) => {
582                     self.extract_realloc(store.0, realloc)
583                 }
584 
585                 GlobalInitializer::ExtractCallback(callback) => {
586                     self.extract_callback(store.0, callback)
587                 }
588 
589                 GlobalInitializer::ExtractPostReturn(post_return) => {
590                     self.extract_post_return(store.0, post_return)
591                 }
592 
593                 GlobalInitializer::Resource(r) => self.resource(store.0, r),
594             }
595         }
596         Ok(())
597     }
598 
599     fn resource(&mut self, store: &mut StoreOpaque, resource: &Resource) {
600         let instance = self.instance(store);
601         let dtor = resource
602             .dtor
603             .as_ref()
604             .map(|dtor| instance.lookup_def(store, dtor));
605         let dtor = dtor.map(|export| match export {
606             crate::runtime::vm::Export::Function(f) => f.func_ref,
607             _ => unreachable!(),
608         });
609         let index = self
610             .component
611             .env_component()
612             .resource_index(resource.index);
613         let ty = ResourceType::guest(store.id(), instance, resource.index);
614         self.instance_mut(store)
615             .set_resource_destructor(index, dtor);
616         let i = self.instance_resource_types_mut(store).push(ty);
617         debug_assert_eq!(i, index);
618     }
619 
620     fn extract_memory(&mut self, store: &mut StoreOpaque, memory: &ExtractMemory) {
621         let mem = match self.instance(store).lookup_export(store, &memory.export) {
622             crate::runtime::vm::Export::Memory(m) => m,
623             _ => unreachable!(),
624         };
625         self.instance_mut(store)
626             .set_runtime_memory(memory.index, mem.definition);
627     }
628 
629     fn extract_realloc(&mut self, store: &mut StoreOpaque, realloc: &ExtractRealloc) {
630         let func_ref = match self.instance(store).lookup_def(store, &realloc.def) {
631             crate::runtime::vm::Export::Function(f) => f.func_ref,
632             _ => unreachable!(),
633         };
634         self.instance_mut(store)
635             .set_runtime_realloc(realloc.index, func_ref);
636     }
637 
638     fn extract_callback(&mut self, store: &mut StoreOpaque, callback: &ExtractCallback) {
639         let func_ref = match self.instance(store).lookup_def(store, &callback.def) {
640             crate::runtime::vm::Export::Function(f) => f.func_ref,
641             _ => unreachable!(),
642         };
643         self.instance_mut(store)
644             .set_runtime_callback(callback.index, func_ref);
645     }
646 
647     fn extract_post_return(&mut self, store: &mut StoreOpaque, post_return: &ExtractPostReturn) {
648         let func_ref = match self.instance(store).lookup_def(store, &post_return.def) {
649             crate::runtime::vm::Export::Function(f) => f.func_ref,
650             _ => unreachable!(),
651         };
652         self.instance_mut(store)
653             .set_runtime_post_return(post_return.index, func_ref);
654     }
655 
656     fn extract_table(&mut self, store: &mut StoreOpaque, table: &ExtractTable) {
657         let export = match self.instance(store).lookup_export(store, &table.export) {
658             crate::runtime::vm::Export::Table(t) => t,
659             _ => unreachable!(),
660         };
661         self.instance_mut(store).set_runtime_table(
662             table.index,
663             export.definition,
664             export.vmctx,
665             export.index,
666         );
667     }
668 
669     fn build_imports<'b>(
670         &mut self,
671         store: &StoreOpaque,
672         module: &Module,
673         args: impl Iterator<Item = &'b CoreDef>,
674     ) -> &OwnedImports {
675         self.core_imports.clear();
676         self.core_imports.reserve(module);
677         let mut imports = module.compiled_module().module().imports();
678 
679         for arg in args {
680             // The general idea of Wasmtime is that at runtime type-checks for
681             // core wasm instantiations internally within a component are
682             // unnecessary and superfluous. Naturally though mistakes may be
683             // made, so double-check this property of wasmtime in debug mode.
684 
685             if cfg!(debug_assertions) {
686                 let (imp_module, imp_name, expected) = imports.next().unwrap();
687                 self.assert_type_matches(store, module, arg, imp_module, imp_name, expected);
688             }
689 
690             // The unsafety here should be ok since the `export` is loaded
691             // directly from an instance which should only give us valid export
692             // items.
693             let export = self.instance(store).lookup_def(store, arg);
694             unsafe {
695                 self.core_imports.push_export(&export);
696             }
697         }
698         debug_assert!(imports.next().is_none());
699 
700         &self.core_imports
701     }
702 
703     fn assert_type_matches(
704         &self,
705         store: &StoreOpaque,
706         module: &Module,
707         arg: &CoreDef,
708         imp_module: &str,
709         imp_name: &str,
710         expected: EntityType,
711     ) {
712         let export = self.instance(store).lookup_def(store, arg);
713 
714         // If this value is a core wasm function then the type check is inlined
715         // here. This can otherwise fail `Extern::from_wasmtime_export` because
716         // there's no guarantee that there exists a trampoline for `f` so this
717         // can't fall through to the case below
718         if let crate::runtime::vm::Export::Function(f) = &export {
719             let expected = match expected.unwrap_func() {
720                 EngineOrModuleTypeIndex::Engine(e) => Some(e),
721                 EngineOrModuleTypeIndex::Module(m) => module.signatures().shared_type(m),
722                 EngineOrModuleTypeIndex::RecGroup(_) => unreachable!(),
723             };
724             let actual = unsafe { f.func_ref.as_ref().type_index };
725             assert_eq!(
726                 expected,
727                 Some(actual),
728                 "type mismatch for import {imp_module:?} {imp_name:?}!!!\n\n\
729                  expected {:#?}\n\n\
730                  found {:#?}",
731                 expected.and_then(|e| store.engine().signatures().borrow(e)),
732                 store.engine().signatures().borrow(actual)
733             );
734             return;
735         }
736 
737         let val = unsafe { crate::Extern::from_wasmtime_export(export, store) };
738         let ty = DefinitionType::from(store, &val);
739         crate::types::matching::MatchCx::new(module.engine())
740             .definition(&expected, &ty)
741             .expect("unexpected typecheck failure");
742     }
743 
744     /// Convenience helper to return the `&ComponentInstance` that's being
745     /// instantiated.
746     fn instance<'b>(&self, store: &'b StoreOpaque) -> &'b ComponentInstance {
747         store.store_data().component_instance(self.id)
748     }
749 
750     /// Same as [`Self::instance`], but for mutability.
751     fn instance_mut<'b>(&self, store: &'b mut StoreOpaque) -> &'b mut ComponentInstance {
752         store.store_data_mut().component_instance_mut(self.id)
753     }
754 
755     // NB: This method is only intended to be called during the instantiation
756     // process because the `Arc::get_mut` here is fallible and won't generally
757     // succeed once the instance has been handed to the embedder. Before that
758     // though it should be guaranteed that the single owning reference currently
759     // lives within the `ComponentInstance` that's being built.
760     fn instance_resource_types_mut<'b>(
761         &self,
762         store: &'b mut StoreOpaque,
763     ) -> &'b mut ImportedResources {
764         Arc::get_mut(self.instance_mut(store).resource_types_mut()).unwrap()
765     }
766 }
767 
768 /// A "pre-instantiated" [`Instance`] which has all of its arguments already
769 /// supplied and is ready to instantiate.
770 ///
771 /// This structure represents an efficient form of instantiation where import
772 /// type-checking and import lookup has all been resolved by the time that this
773 /// type is created. This type is primarily created through the
774 /// [`Linker::instantiate_pre`](crate::component::Linker::instantiate_pre)
775 /// method.
776 pub struct InstancePre<T: 'static> {
777     component: Component,
778     imports: Arc<PrimaryMap<RuntimeImportIndex, RuntimeImport>>,
779     resource_types: Arc<PrimaryMap<ResourceIndex, ResourceType>>,
780     _marker: marker::PhantomData<fn() -> T>,
781 }
782 
783 // `InstancePre`'s clone does not require `T: Clone`
784 impl<T: 'static> Clone for InstancePre<T> {
785     fn clone(&self) -> Self {
786         Self {
787             component: self.component.clone(),
788             imports: self.imports.clone(),
789             resource_types: self.resource_types.clone(),
790             _marker: self._marker,
791         }
792     }
793 }
794 
795 impl<T: 'static> InstancePre<T> {
796     /// This function is `unsafe` since there's no guarantee that the
797     /// `RuntimeImport` items provided are guaranteed to work with the `T` of
798     /// the store.
799     ///
800     /// Additionally there is no static guarantee that the `imports` provided
801     /// satisfy the imports of the `component` provided.
802     pub(crate) unsafe fn new_unchecked(
803         component: Component,
804         imports: Arc<PrimaryMap<RuntimeImportIndex, RuntimeImport>>,
805         resource_types: Arc<PrimaryMap<ResourceIndex, ResourceType>>,
806     ) -> InstancePre<T> {
807         InstancePre {
808             component,
809             imports,
810             resource_types,
811             _marker: marker::PhantomData,
812         }
813     }
814 
815     /// Returns the underlying component that will be instantiated.
816     pub fn component(&self) -> &Component {
817         &self.component
818     }
819 
820     #[doc(hidden)]
821     /// Returns the type at which the underlying component will be
822     /// instantiated. This contains the instantiated type information which
823     /// was determined by the Linker.
824     pub fn instance_type(&self) -> InstanceType<'_> {
825         InstanceType {
826             types: &self.component.types(),
827             resources: &self.resource_types,
828         }
829     }
830 
831     /// Returns the underlying engine.
832     pub fn engine(&self) -> &Engine {
833         self.component.engine()
834     }
835 
836     /// Performs the instantiation process into the store specified.
837     //
838     // TODO: needs more docs
839     pub fn instantiate(&self, store: impl AsContextMut<Data = T>) -> Result<Instance> {
840         assert!(
841             !store.as_context().async_support(),
842             "must use async instantiation when async support is enabled"
843         );
844         self.instantiate_impl(store)
845     }
846     /// Performs the instantiation process into the store specified.
847     ///
848     /// Exactly like [`Self::instantiate`] except for use on async stores.
849     //
850     // TODO: needs more docs
851     #[cfg(feature = "async")]
852     pub async fn instantiate_async(
853         &self,
854         mut store: impl AsContextMut<Data = T>,
855     ) -> Result<Instance>
856     where
857         T: Send,
858     {
859         let mut store = store.as_context_mut();
860         assert!(
861             store.0.async_support(),
862             "must use sync instantiation when async support is disabled"
863         );
864         store.on_fiber(|store| self.instantiate_impl(store)).await?
865     }
866 
867     fn instantiate_impl(&self, mut store: impl AsContextMut<Data = T>) -> Result<Instance> {
868         let mut store = store.as_context_mut();
869         store
870             .engine()
871             .allocator()
872             .increment_component_instance_count()?;
873         let mut instantiator = Instantiator::new(&self.component, store.0, &self.imports);
874         instantiator.run(&mut store).map_err(|e| {
875             store
876                 .engine()
877                 .allocator()
878                 .decrement_component_instance_count();
879             e
880         })?;
881         let instance = Instance::from_wasmtime(store.0, instantiator.id);
882         store.0.push_component_instance(instance);
883         Ok(instance)
884     }
885 }
886