wasmtime/instance.rs
1use crate::linker::{Definition, DefinitionType};
2use crate::store::{InstanceId, StoreOpaque, Stored};
3use crate::types::matching;
4use crate::{
5 AsContextMut, Engine, Export, Extern, Func, Global, Memory, Module, SharedMemory,
6 StoreContextMut, Table, TypedFunc,
7};
8use anyhow::{anyhow, bail, Context, Result};
9use std::mem;
10use std::sync::Arc;
11use wasmtime_environ::{EntityType, FuncIndex, GlobalIndex, MemoryIndex, PrimaryMap, TableIndex};
12use wasmtime_runtime::{
13 Imports, InstanceAllocationRequest, StorePtr, VMContext, VMFunctionBody, VMFunctionImport,
14 VMGlobalImport, VMMemoryImport, VMOpaqueContext, VMTableImport,
15};
16
17/// An instantiated WebAssembly module.
18///
19/// This type represents the instantiation of a [`Module`]. Once instantiated
20/// you can access the [`exports`](Instance::exports) which are of type
21/// [`Extern`] and provide the ability to call functions, set globals, read
22/// memory, etc. When interacting with any wasm code you'll want to make an
23/// [`Instance`] to call any code or execute anything.
24///
25/// Instances are owned by a [`Store`](crate::Store) which is passed in at
26/// creation time. It's recommended to create instances with
27/// [`Linker::instantiate`](crate::Linker::instantiate) or similar
28/// [`Linker`](crate::Linker) methods, but a more low-level constructor is also
29/// available as [`Instance::new`].
30#[derive(Copy, Clone, Debug)]
31#[repr(transparent)]
32pub struct Instance(Stored<InstanceData>);
33
34pub(crate) struct InstanceData {
35 /// The id of the instance within the store, used to find the original
36 /// `InstanceHandle`.
37 id: InstanceId,
38 /// A lazily-populated list of exports of this instance. The order of
39 /// exports here matches the order of the exports in the the original
40 /// module.
41 exports: Vec<Option<Extern>>,
42}
43
44impl Instance {
45 /// Creates a new [`Instance`] from the previously compiled [`Module`] and
46 /// list of `imports` specified.
47 ///
48 /// This method instantiates the `module` provided with the `imports`,
49 /// following the procedure in the [core specification][inst] to
50 /// instantiate. Instantiation can fail for a number of reasons (many
51 /// specified below), but if successful the `start` function will be
52 /// automatically run (if specified in the `module`) and then the
53 /// [`Instance`] will be returned.
54 ///
55 /// Per the WebAssembly spec, instantiation includes running the module's
56 /// start function, if it has one (not to be confused with the `_start`
57 /// function, which is not run).
58 ///
59 /// Note that this is a low-level function that just performs an
60 /// instantiation. See the [`Linker`](crate::Linker) struct for an API which
61 /// provides a convenient way to link imports and provides automatic Command
62 /// and Reactor behavior.
63 ///
64 /// ## Providing Imports
65 ///
66 /// The entries in the list of `imports` are intended to correspond 1:1
67 /// with the list of imports returned by [`Module::imports`]. Before
68 /// calling [`Instance::new`] you'll want to inspect the return value of
69 /// [`Module::imports`] and, for each import type, create an [`Extern`]
70 /// which corresponds to that type. These [`Extern`] values are all then
71 /// collected into a list and passed to this function.
72 ///
73 /// Note that this function is intentionally relatively low level. For an
74 /// easier time passing imports by doing name-based resolution it's
75 /// recommended to instead use the [`Linker`](crate::Linker) type.
76 ///
77 /// ## Errors
78 ///
79 /// This function can fail for a number of reasons, including, but not
80 /// limited to:
81 ///
82 /// * The number of `imports` provided doesn't match the number of imports
83 /// returned by the `module`'s [`Module::imports`] method.
84 /// * The type of any [`Extern`] doesn't match the corresponding
85 /// [`ExternType`] entry that it maps to.
86 /// * The `start` function in the instance, if present, traps.
87 /// * Module/instance resource limits are exceeded.
88 ///
89 /// When instantiation fails it's recommended to inspect the return value to
90 /// see why it failed, or bubble it upwards. If you'd like to specifically
91 /// check for trap errors, you can use `error.downcast::<Trap>()`. For more
92 /// about error handling see the [`Trap`] documentation.
93 ///
94 /// [`Trap`]: crate::Trap
95 ///
96 /// # Panics
97 ///
98 /// This function will panic if called with a store associated with a
99 /// [`asynchronous config`](crate::Config::async_support). This function
100 /// will also panic if any [`Extern`] supplied is not owned by `store`.
101 ///
102 /// [inst]: https://webassembly.github.io/spec/core/exec/modules.html#exec-instantiation
103 /// [`ExternType`]: crate::ExternType
104 pub fn new(
105 mut store: impl AsContextMut,
106 module: &Module,
107 imports: &[Extern],
108 ) -> Result<Instance> {
109 let mut store = store.as_context_mut();
110 let imports = Instance::typecheck_externs(store.0, module, imports)?;
111 // Note that the unsafety here should be satisfied by the call to
112 // `typecheck_externs` above which satisfies the condition that all
113 // the imports are valid for this module.
114 unsafe { Instance::new_started(&mut store, module, imports.as_ref()) }
115 }
116
117 /// Same as [`Instance::new`], except for usage in [asynchronous stores].
118 ///
119 /// For more details about this function see the documentation on
120 /// [`Instance::new`]. The only difference between these two methods is that
121 /// this one will asynchronously invoke the wasm start function in case it
122 /// calls any imported function which is an asynchronous host function (e.g.
123 /// created with [`Func::new_async`](crate::Func::new_async).
124 ///
125 /// # Panics
126 ///
127 /// This function will panic if called with a store associated with a
128 /// [`synchronous config`](crate::Config::new). This is only compatible with
129 /// stores associated with an [`asynchronous
130 /// config`](crate::Config::async_support).
131 ///
132 /// This function will also panic, like [`Instance::new`], if any [`Extern`]
133 /// specified does not belong to `store`.
134 #[cfg(feature = "async")]
135 #[cfg_attr(nightlydoc, doc(cfg(feature = "async")))]
136 pub async fn new_async<T>(
137 mut store: impl AsContextMut<Data = T>,
138 module: &Module,
139 imports: &[Extern],
140 ) -> Result<Instance>
141 where
142 T: Send,
143 {
144 let mut store = store.as_context_mut();
145 let imports = Instance::typecheck_externs(store.0, module, imports)?;
146 // See `new` for notes on this unsafety
147 unsafe { Instance::new_started_async(&mut store, module, imports.as_ref()).await }
148 }
149
150 fn typecheck_externs(
151 store: &mut StoreOpaque,
152 module: &Module,
153 imports: &[Extern],
154 ) -> Result<OwnedImports> {
155 for import in imports {
156 if !import.comes_from_same_store(store) {
157 bail!("cross-`Store` instantiation is not currently supported");
158 }
159 }
160 typecheck(module, imports, |cx, ty, item| {
161 let item = DefinitionType::from(store, item);
162 cx.definition(ty, &item)
163 })?;
164 let mut owned_imports = OwnedImports::new(module);
165 for import in imports {
166 owned_imports.push(import, store);
167 }
168 Ok(owned_imports)
169 }
170
171 /// Internal function to create an instance and run the start function.
172 ///
173 /// This function's unsafety is the same as `Instance::new_raw`.
174 pub(crate) unsafe fn new_started<T>(
175 store: &mut StoreContextMut<'_, T>,
176 module: &Module,
177 imports: Imports<'_>,
178 ) -> Result<Instance> {
179 assert!(
180 !store.0.async_support(),
181 "must use async instantiation when async support is enabled",
182 );
183 Self::new_started_impl(store, module, imports)
184 }
185
186 /// Internal function to create an instance and run the start function.
187 ///
188 /// ONLY CALL THIS IF YOU HAVE ALREADY CHECKED FOR ASYNCNESS AND HANDLED
189 /// THE FIBER NONSENSE
190 pub(crate) unsafe fn new_started_impl<T>(
191 store: &mut StoreContextMut<'_, T>,
192 module: &Module,
193 imports: Imports<'_>,
194 ) -> Result<Instance> {
195 let (instance, start) = Instance::new_raw(store.0, module, imports)?;
196 if let Some(start) = start {
197 instance.start_raw(store, start)?;
198 }
199 Ok(instance)
200 }
201
202 /// Internal function to create an instance and run the start function.
203 ///
204 /// This function's unsafety is the same as `Instance::new_raw`.
205 #[cfg(feature = "async")]
206 async unsafe fn new_started_async<T>(
207 store: &mut StoreContextMut<'_, T>,
208 module: &Module,
209 imports: Imports<'_>,
210 ) -> Result<Instance>
211 where
212 T: Send,
213 {
214 assert!(
215 store.0.async_support(),
216 "must use sync instantiation when async support is disabled",
217 );
218
219 store
220 .on_fiber(|store| Self::new_started_impl(store, module, imports))
221 .await?
222 }
223
224 /// Internal function to create an instance which doesn't have its `start`
225 /// function run yet.
226 ///
227 /// This is not intended to be exposed from Wasmtime, it's intended to
228 /// refactor out common code from `new_started` and `new_started_async`.
229 ///
230 /// Note that this step needs to be run on a fiber in async mode even
231 /// though it doesn't do any blocking work because an async resource
232 /// limiter may need to yield.
233 ///
234 /// # Unsafety
235 ///
236 /// This method is unsafe because it does not type-check the `imports`
237 /// provided. The `imports` provided must be suitable for the module
238 /// provided as well.
239 unsafe fn new_raw(
240 store: &mut StoreOpaque,
241 module: &Module,
242 imports: Imports<'_>,
243 ) -> Result<(Instance, Option<FuncIndex>)> {
244 if !Engine::same(store.engine(), module.engine()) {
245 bail!("cross-`Engine` instantiation is not currently supported");
246 }
247 store.bump_resource_counts(module)?;
248
249 let compiled_module = module.compiled_module();
250
251 // Register the module just before instantiation to ensure we keep the module
252 // properly referenced while in use by the store.
253 store.modules_mut().register_module(module);
254
255 // The first thing we do is issue an instance allocation request
256 // to the instance allocator. This, on success, will give us an
257 // instance handle.
258 //
259 // Note that the `host_state` here is a pointer back to the
260 // `Instance` we'll be returning from this function. This is a
261 // circular reference so we can't construct it before we construct
262 // this instance, so we determine what the ID is and then assert
263 // it's the same later when we do actually insert it.
264 let instance_to_be = store.store_data().next_id::<InstanceData>();
265
266 let mut instance_handle =
267 store
268 .engine()
269 .allocator()
270 .allocate(InstanceAllocationRequest {
271 runtime_info: &module.runtime_info(),
272 imports,
273 host_state: Box::new(Instance(instance_to_be)),
274 store: StorePtr::new(store.traitobj()),
275 })?;
276
277 // The instance still has lots of setup, for example
278 // data/elements/start/etc. This can all fail, but even on failure
279 // the instance may persist some state via previous successful
280 // initialization. For this reason once we have an instance handle
281 // we immediately insert it into the store to keep it alive.
282 //
283 // Note that we `clone` the instance handle just to make easier
284 // working the the borrow checker here easier. Technically the `&mut
285 // instance` has somewhat of a borrow on `store` (which
286 // conflicts with the borrow on `store.engine`) but this doesn't
287 // matter in practice since initialization isn't even running any
288 // code here anyway.
289 let id = store.add_instance(instance_handle.clone(), false);
290
291 // Additionally, before we start doing fallible instantiation, we
292 // do one more step which is to insert an `InstanceData`
293 // corresponding to this instance. This `InstanceData` can be used
294 // via `Caller::get_export` if our instance's state "leaks" into
295 // other instances, even if we don't return successfully from this
296 // function.
297 //
298 // We don't actually load all exports from the instance at this
299 // time, instead preferring to lazily load them as they're demanded.
300 // For module/instance exports, though, those aren't actually
301 // stored in the instance handle so we need to immediately handle
302 // those here.
303 let instance = {
304 let exports = vec![None; compiled_module.module().exports.len()];
305 let data = InstanceData { id, exports };
306 Instance::from_wasmtime(data, store)
307 };
308
309 // double-check our guess of what the new instance's ID would be
310 // was actually correct.
311 assert_eq!(instance.0, instance_to_be);
312
313 // Now that we've recorded all information we need to about this
314 // instance within a `Store` we can start performing fallible
315 // initialization. Note that we still defer the `start` function to
316 // later since that may need to run asynchronously.
317 //
318 // If this returns an error (or if the start function traps) then
319 // any other initialization which may have succeeded which placed
320 // items from this instance into other instances should be ok when
321 // those items are loaded and run we'll have all the metadata to
322 // look at them.
323 instance_handle.initialize(
324 compiled_module.module(),
325 store.engine().config().features.bulk_memory,
326 )?;
327
328 Ok((instance, compiled_module.module().start_func))
329 }
330
331 pub(crate) fn from_wasmtime(handle: InstanceData, store: &mut StoreOpaque) -> Instance {
332 Instance(store.store_data_mut().insert(handle))
333 }
334
335 fn start_raw<T>(&self, store: &mut StoreContextMut<'_, T>, start: FuncIndex) -> Result<()> {
336 let id = store.0.store_data()[self.0].id;
337 // If a start function is present, invoke it. Make sure we use all the
338 // trap-handling configuration in `store` as well.
339 let instance = store.0.instance_mut(id);
340 let f = instance.get_exported_func(start);
341 let vmctx = instance.vmctx_ptr();
342 unsafe {
343 super::func::invoke_wasm_and_catch_traps(store, |_default_caller| {
344 let trampoline = mem::transmute::<
345 *const VMFunctionBody,
346 unsafe extern "C" fn(*mut VMOpaqueContext, *mut VMContext),
347 >(f.anyfunc.as_ref().func_ptr.as_ptr());
348 let trampoline =
349 wasmtime_runtime::prepare_host_to_wasm_trampoline(vmctx, trampoline);
350 trampoline(f.anyfunc.as_ref().vmctx, vmctx)
351 })?;
352 }
353 Ok(())
354 }
355
356 /// Returns the list of exported items from this [`Instance`].
357 ///
358 /// # Panics
359 ///
360 /// Panics if `store` does not own this instance.
361 pub fn exports<'a, T: 'a>(
362 &'a self,
363 store: impl Into<StoreContextMut<'a, T>>,
364 ) -> impl ExactSizeIterator<Item = Export<'a>> + 'a {
365 self._exports(store.into().0)
366 }
367
368 fn _exports<'a>(
369 &'a self,
370 store: &'a mut StoreOpaque,
371 ) -> impl ExactSizeIterator<Item = Export<'a>> + 'a {
372 // If this is an `Instantiated` instance then all the `exports` may not
373 // be filled in. Fill them all in now if that's the case.
374 let InstanceData { exports, id, .. } = &store[self.0];
375 if exports.iter().any(|e| e.is_none()) {
376 let module = Arc::clone(store.instance(*id).module());
377 for name in module.exports.keys() {
378 self._get_export(store, name);
379 }
380 }
381
382 let data = &store.store_data()[self.0];
383 let module = store.instance(data.id).module();
384 module
385 .exports
386 .iter()
387 .zip(&data.exports)
388 .map(|((name, _), export)| Export::new(name, export.clone().unwrap()))
389 }
390
391 /// Looks up an exported [`Extern`] value by name.
392 ///
393 /// This method will search the module for an export named `name` and return
394 /// the value, if found.
395 ///
396 /// Returns `None` if there was no export named `name`.
397 ///
398 /// # Panics
399 ///
400 /// Panics if `store` does not own this instance.
401 ///
402 /// # Why does `get_export` take a mutable context?
403 ///
404 /// This method requires a mutable context because an instance's exports are
405 /// lazily populated, and we cache them as they are accessed. This makes
406 /// instantiating a module faster, but also means this method requires a
407 /// mutable context.
408 pub fn get_export(&self, mut store: impl AsContextMut, name: &str) -> Option<Extern> {
409 self._get_export(store.as_context_mut().0, name)
410 }
411
412 fn _get_export(&self, store: &mut StoreOpaque, name: &str) -> Option<Extern> {
413 // Instantiated instances will lazily fill in exports, so we process
414 // all that lazy logic here.
415 let data = &store[self.0];
416
417 let instance = store.instance(data.id);
418 let (i, _, &index) = instance.module().exports.get_full(name)?;
419 if let Some(export) = &data.exports[i] {
420 return Some(export.clone());
421 }
422
423 let id = data.id;
424 let instance = store.instance_mut(id); // reborrow the &mut Instancehandle
425 let item =
426 unsafe { Extern::from_wasmtime_export(instance.get_export_by_index(index), store) };
427 let data = &mut store[self.0];
428 data.exports[i] = Some(item.clone());
429 Some(item)
430 }
431
432 /// Looks up an exported [`Func`] value by name.
433 ///
434 /// Returns `None` if there was no export named `name`, or if there was but
435 /// it wasn't a function.
436 ///
437 /// # Panics
438 ///
439 /// Panics if `store` does not own this instance.
440 pub fn get_func(&self, store: impl AsContextMut, name: &str) -> Option<Func> {
441 self.get_export(store, name)?.into_func()
442 }
443
444 /// Looks up an exported [`Func`] value by name and with its type.
445 ///
446 /// This function is a convenience wrapper over [`Instance::get_func`] and
447 /// [`Func::typed`]. For more information see the linked documentation.
448 ///
449 /// Returns an error if `name` isn't a function export or if the export's
450 /// type did not match `Params` or `Results`
451 ///
452 /// # Panics
453 ///
454 /// Panics if `store` does not own this instance.
455 pub fn get_typed_func<Params, Results>(
456 &self,
457 mut store: impl AsContextMut,
458 name: &str,
459 ) -> Result<TypedFunc<Params, Results>>
460 where
461 Params: crate::WasmParams,
462 Results: crate::WasmResults,
463 {
464 let f = self
465 .get_export(store.as_context_mut(), name)
466 .and_then(|f| f.into_func())
467 .ok_or_else(|| anyhow!("failed to find function export `{}`", name))?;
468 Ok(f.typed::<Params, Results>(store)
469 .with_context(|| format!("failed to convert function `{}` to given type", name))?)
470 }
471
472 /// Looks up an exported [`Table`] value by name.
473 ///
474 /// Returns `None` if there was no export named `name`, or if there was but
475 /// it wasn't a table.
476 ///
477 /// # Panics
478 ///
479 /// Panics if `store` does not own this instance.
480 pub fn get_table(&self, store: impl AsContextMut, name: &str) -> Option<Table> {
481 self.get_export(store, name)?.into_table()
482 }
483
484 /// Looks up an exported [`Memory`] value by name.
485 ///
486 /// Returns `None` if there was no export named `name`, or if there was but
487 /// it wasn't a memory.
488 ///
489 /// # Panics
490 ///
491 /// Panics if `store` does not own this instance.
492 pub fn get_memory(&self, store: impl AsContextMut, name: &str) -> Option<Memory> {
493 self.get_export(store, name)?.into_memory()
494 }
495
496 /// Looks up an exported [`SharedMemory`] value by name.
497 ///
498 /// Returns `None` if there was no export named `name`, or if there was but
499 /// it wasn't a shared memory.
500 ///
501 /// # Panics
502 ///
503 /// Panics if `store` does not own this instance.
504 pub fn get_shared_memory(
505 &self,
506 mut store: impl AsContextMut,
507 name: &str,
508 ) -> Option<SharedMemory> {
509 let mut store = store.as_context_mut();
510 self.get_export(&mut store, name)?.into_shared_memory()
511 }
512
513 /// Looks up an exported [`Global`] value by name.
514 ///
515 /// Returns `None` if there was no export named `name`, or if there was but
516 /// it wasn't a global.
517 ///
518 /// # Panics
519 ///
520 /// Panics if `store` does not own this instance.
521 pub fn get_global(&self, store: impl AsContextMut, name: &str) -> Option<Global> {
522 self.get_export(store, name)?.into_global()
523 }
524
525 #[cfg(feature = "component-model")]
526 pub(crate) fn id(&self, store: &StoreOpaque) -> InstanceId {
527 store[self.0].id
528 }
529}
530
531pub(crate) struct OwnedImports {
532 functions: PrimaryMap<FuncIndex, VMFunctionImport>,
533 tables: PrimaryMap<TableIndex, VMTableImport>,
534 memories: PrimaryMap<MemoryIndex, VMMemoryImport>,
535 globals: PrimaryMap<GlobalIndex, VMGlobalImport>,
536}
537
538impl OwnedImports {
539 fn new(module: &Module) -> OwnedImports {
540 let mut ret = OwnedImports::empty();
541 ret.reserve(module);
542 return ret;
543 }
544
545 pub(crate) fn empty() -> OwnedImports {
546 OwnedImports {
547 functions: PrimaryMap::new(),
548 tables: PrimaryMap::new(),
549 memories: PrimaryMap::new(),
550 globals: PrimaryMap::new(),
551 }
552 }
553
554 pub(crate) fn reserve(&mut self, module: &Module) {
555 let raw = module.compiled_module().module();
556 self.functions.reserve(raw.num_imported_funcs);
557 self.tables.reserve(raw.num_imported_tables);
558 self.memories.reserve(raw.num_imported_memories);
559 self.globals.reserve(raw.num_imported_globals);
560 }
561
562 #[cfg(feature = "component-model")]
563 pub(crate) fn clear(&mut self) {
564 self.functions.clear();
565 self.tables.clear();
566 self.memories.clear();
567 self.globals.clear();
568 }
569
570 fn push(&mut self, item: &Extern, store: &mut StoreOpaque) {
571 match item {
572 Extern::Func(i) => {
573 self.functions.push(i.vmimport(store));
574 }
575 Extern::Global(i) => {
576 self.globals.push(i.vmimport(store));
577 }
578 Extern::Table(i) => {
579 self.tables.push(i.vmimport(store));
580 }
581 Extern::Memory(i) => {
582 self.memories.push(i.vmimport(store));
583 }
584 Extern::SharedMemory(i) => {
585 self.memories.push(i.vmimport(store));
586 }
587 }
588 }
589
590 /// Note that this is unsafe as the validity of `item` is not verified and
591 /// it contains a bunch of raw pointers.
592 #[cfg(feature = "component-model")]
593 pub(crate) unsafe fn push_export(&mut self, item: &wasmtime_runtime::Export) {
594 match item {
595 wasmtime_runtime::Export::Function(f) => {
596 let f = f.anyfunc.as_ref();
597 self.functions.push(VMFunctionImport {
598 body: f.func_ptr,
599 vmctx: f.vmctx,
600 });
601 }
602 wasmtime_runtime::Export::Global(g) => {
603 self.globals.push(VMGlobalImport { from: g.definition });
604 }
605 wasmtime_runtime::Export::Table(t) => {
606 self.tables.push(VMTableImport {
607 from: t.definition,
608 vmctx: t.vmctx,
609 });
610 }
611 wasmtime_runtime::Export::Memory(m) => {
612 self.memories.push(VMMemoryImport {
613 from: m.definition,
614 vmctx: m.vmctx,
615 index: m.index,
616 });
617 }
618 }
619 }
620
621 pub(crate) fn as_ref(&self) -> Imports<'_> {
622 Imports {
623 tables: self.tables.values().as_slice(),
624 globals: self.globals.values().as_slice(),
625 memories: self.memories.values().as_slice(),
626 functions: self.functions.values().as_slice(),
627 }
628 }
629}
630
631/// An instance, pre-instantiation, that is ready to be instantiated.
632///
633/// This structure represents an instance *just before* it was instantiated,
634/// after all type-checking and imports have been resolved. The only thing left
635/// to do for this instance is to actually run the process of instantiation.
636///
637/// Note that an `InstancePre` may not be tied to any particular [`Store`] if
638/// none of the imports it closed over are tied to any particular [`Store`].
639///
640/// This structure is created through the [`Linker::instantiate_pre`] method,
641/// which also has some more information and examples.
642///
643/// [`Store`]: crate::Store
644/// [`Linker::instantiate_pre`]: crate::Linker::instantiate_pre
645pub struct InstancePre<T> {
646 module: Module,
647
648 /// The items which this `InstancePre` use to instantiate the `module`
649 /// provided, passed to `Instance::new_started` after inserting them into a
650 /// `Store`.
651 ///
652 /// Note that this is stored as an `Arc<[T]>` to quickly move a strong
653 /// reference to everything internally into a `Store<T>` without having to
654 /// clone each individual item.
655 items: Arc<[Definition]>,
656
657 /// A count of `Definition::HostFunc` entries in `items` above to
658 /// preallocate space in a `Store` up front for all entries to be inserted.
659 host_funcs: usize,
660
661 _marker: std::marker::PhantomData<fn() -> T>,
662}
663
664/// InstancePre's clone does not require T: Clone
665impl<T> Clone for InstancePre<T> {
666 fn clone(&self) -> Self {
667 Self {
668 module: self.module.clone(),
669 items: self.items.clone(),
670 host_funcs: self.host_funcs,
671 _marker: self._marker,
672 }
673 }
674}
675
676impl<T> InstancePre<T> {
677 /// Creates a new `InstancePre` which type-checks the `items` provided and
678 /// on success is ready to instantiate a new instance.
679 ///
680 /// # Unsafety
681 ///
682 /// This method is unsafe as the `T` of the `InstancePre<T>` is not
683 /// guaranteed to be the same as the `T` within the `Store`, the caller must
684 /// verify that.
685 pub(crate) unsafe fn new(module: &Module, items: Vec<Definition>) -> Result<InstancePre<T>> {
686 typecheck(module, &items, |cx, ty, item| cx.definition(ty, &item.ty()))?;
687
688 let host_funcs = items
689 .iter()
690 .filter(|i| match i {
691 Definition::HostFunc(_) => true,
692 _ => false,
693 })
694 .count();
695 Ok(InstancePre {
696 module: module.clone(),
697 items: items.into(),
698 host_funcs,
699 _marker: std::marker::PhantomData,
700 })
701 }
702
703 /// Returns a reference to the module that this [`InstancePre`] will be
704 /// instantiating.
705 pub fn module(&self) -> &Module {
706 &self.module
707 }
708
709 /// Instantiates this instance, creating a new instance within the provided
710 /// `store`.
711 ///
712 /// This function will run the actual process of instantiation to
713 /// completion. This will use all of the previously-closed-over items as
714 /// imports to instantiate the module that this was originally created with.
715 ///
716 /// For more information about instantiation see [`Instance::new`].
717 ///
718 /// # Panics
719 ///
720 /// Panics if any import closed over by this [`InstancePre`] isn't owned by
721 /// `store`, or if `store` has async support enabled. Additionally this
722 /// function will panic if the `store` provided comes from a different
723 /// [`Engine`] than the [`InstancePre`] originally came from.
724 pub fn instantiate(&self, mut store: impl AsContextMut<Data = T>) -> Result<Instance> {
725 let mut store = store.as_context_mut();
726 let imports =
727 pre_instantiate_raw(&mut store.0, &self.module, &self.items, self.host_funcs)?;
728
729 // This unsafety should be handled by the type-checking performed by the
730 // constructor of `InstancePre` to assert that all the imports we're passing
731 // in match the module we're instantiating.
732 unsafe { Instance::new_started(&mut store, &self.module, imports.as_ref()) }
733 }
734
735 /// Creates a new instance, running the start function asynchronously
736 /// instead of inline.
737 ///
738 /// For more information about asynchronous instantiation see the
739 /// documentation on [`Instance::new_async`].
740 ///
741 /// # Panics
742 ///
743 /// Panics if any import closed over by this [`InstancePre`] isn't owned by
744 /// `store`, or if `store` does not have async support enabled.
745 #[cfg(feature = "async")]
746 #[cfg_attr(nightlydoc, doc(cfg(feature = "async")))]
747 pub async fn instantiate_async(
748 &self,
749 mut store: impl AsContextMut<Data = T>,
750 ) -> Result<Instance>
751 where
752 T: Send,
753 {
754 let mut store = store.as_context_mut();
755 let imports =
756 pre_instantiate_raw(&mut store.0, &self.module, &self.items, self.host_funcs)?;
757
758 // This unsafety should be handled by the type-checking performed by the
759 // constructor of `InstancePre` to assert that all the imports we're passing
760 // in match the module we're instantiating.
761 unsafe { Instance::new_started_async(&mut store, &self.module, imports.as_ref()).await }
762 }
763}
764
765/// Helper function shared between
766/// `InstancePre::{instantiate,instantiate_async}`
767///
768/// This is an out-of-line function to avoid the generic on `InstancePre` and
769/// get this compiled into the `wasmtime` crate to avoid having it monomorphized
770/// elsewhere.
771fn pre_instantiate_raw(
772 store: &mut StoreOpaque,
773 module: &Module,
774 items: &Arc<[Definition]>,
775 host_funcs: usize,
776) -> Result<OwnedImports> {
777 if host_funcs > 0 {
778 // Any linker-defined function of the `Definition::HostFunc` variant
779 // will insert a function into the store automatically as part of
780 // instantiation, so reserve space here to make insertion more efficient
781 // as it won't have to realloc during the instantiation.
782 store.store_data_mut().reserve_funcs(host_funcs);
783
784 // The usage of `to_extern_store_rooted` requires that the items are
785 // rooted via another means, which happens here by cloning the list of
786 // items into the store once. This avoids cloning each individual item
787 // below.
788 store.push_rooted_funcs(items.clone());
789 }
790
791 let mut imports = OwnedImports::new(module);
792 for import in items.iter() {
793 if !import.comes_from_same_store(store) {
794 bail!("cross-`Store` instantiation is not currently supported");
795 }
796 // This unsafety should be encapsulated in the constructor of
797 // `InstancePre` where the `T` of the original item should match the
798 // `T` of the store. Additionally the rooting necessary has happened
799 // above.
800 let item = unsafe { import.to_extern_store_rooted(store) };
801 imports.push(&item, store);
802 }
803
804 Ok(imports)
805}
806
807fn typecheck<I>(
808 module: &Module,
809 imports: &[I],
810 check: impl Fn(&matching::MatchCx<'_>, &EntityType, &I) -> Result<()>,
811) -> Result<()> {
812 let env_module = module.compiled_module().module();
813 let expected = env_module.imports().count();
814 if expected != imports.len() {
815 bail!("expected {} imports, found {}", expected, imports.len());
816 }
817 let cx = matching::MatchCx {
818 signatures: module.signatures(),
819 types: module.types(),
820 engine: module.engine(),
821 };
822 for ((name, field, expected_ty), actual) in env_module.imports().zip(imports) {
823 check(&cx, &expected_ty, actual)
824 .with_context(|| format!("incompatible import type for `{name}::{field}`"))?;
825 }
826 Ok(())
827}