Skip to main content

wasmtime/runtime/component/
func.rs

1use crate::component::instance::Instance;
2use crate::component::matching::InstanceType;
3use crate::component::storage::storage_as_slice;
4use crate::component::types::ComponentFunc;
5use crate::component::values::Val;
6use crate::prelude::*;
7use crate::runtime::vm::component::{ComponentInstance, InstanceFlags};
8use crate::runtime::vm::{Export, VMFuncRef};
9use crate::store::StoreOpaque;
10use crate::{AsContext, AsContextMut, StoreContextMut, ValRaw};
11use core::mem::{self, MaybeUninit};
12use core::ptr::NonNull;
13use wasmtime_environ::component::{
14    CanonicalOptions, ExportIndex, InterfaceType, MAX_FLAT_PARAMS, MAX_FLAT_RESULTS, OptionsIndex,
15    TypeFuncIndex, TypeTuple,
16};
17
18mod host;
19mod options;
20mod typed;
21pub use self::host::*;
22pub use self::options::*;
23pub use self::typed::*;
24
25/// A WebAssembly component function which can be called.
26///
27/// This type is the dual of [`wasmtime::Func`](crate::Func) for component
28/// functions. An instance of [`Func`] represents a component function from a
29/// component [`Instance`](crate::component::Instance). Like with
30/// [`wasmtime::Func`](crate::Func) it's possible to call functions either
31/// synchronously or asynchronously and either typed or untyped.
32#[derive(Copy, Clone, Debug)]
33#[repr(C)] // here for the C API.
34pub struct Func {
35    instance: Instance,
36    index: ExportIndex,
37}
38
39// Double-check that the C representation in `component/instance.h` matches our
40// in-Rust representation here in terms of size/alignment/etc.
41const _: () = {
42    #[repr(C)]
43    struct T(u64, u32);
44    #[repr(C)]
45    struct C(T, u32);
46    assert!(core::mem::size_of::<C>() == core::mem::size_of::<Func>());
47    assert!(core::mem::align_of::<C>() == core::mem::align_of::<Func>());
48    assert!(core::mem::offset_of!(Func, instance) == 0);
49};
50
51impl Func {
52    pub(crate) fn from_lifted_func(instance: Instance, index: ExportIndex) -> Func {
53        Func { instance, index }
54    }
55
56    /// Attempt to cast this [`Func`] to a statically typed [`TypedFunc`] with
57    /// the provided `Params` and `Return`.
58    ///
59    /// This function will perform a type-check at runtime that the [`Func`]
60    /// takes `Params` as parameters and returns `Return`. If the type-check
61    /// passes then a [`TypedFunc`] will be returned which can be used to
62    /// invoke the function in an efficient, statically-typed, and ergonomic
63    /// manner.
64    ///
65    /// The `Params` type parameter here is a tuple of the parameters to the
66    /// function. A function which takes no arguments should use `()`, a
67    /// function with one argument should use `(T,)`, etc. Note that all
68    /// `Params` must also implement the [`Lower`] trait since they're going
69    /// into wasm.
70    ///
71    /// The `Return` type parameter is the return value of this function. A
72    /// return value of `()` means that there's no return (similar to a Rust
73    /// unit return) and otherwise a type `T` can be specified. Note that the
74    /// `Return` must also implement the [`Lift`] trait since it's coming from
75    /// wasm.
76    ///
77    /// Types specified here must implement the [`ComponentType`] trait. This
78    /// trait is implemented for built-in types to Rust such as integer
79    /// primitives, floats, `Option<T>`, `Result<T, E>`, strings, `Vec<T>`, and
80    /// more. As parameters you'll be passing native Rust types.
81    ///
82    /// See the documentation for [`ComponentType`] for more information about
83    /// supported types.
84    ///
85    /// # Errors
86    ///
87    /// If the function does not actually take `Params` as its parameters or
88    /// return `Return` then an error will be returned.
89    ///
90    /// This function will return an [`OutOfMemory`][crate::OutOfMemory] error when
91    /// memory allocation fails. See the `OutOfMemory` type's documentation for
92    /// details on Wasmtime's out-of-memory handling.
93    ///
94    /// # Panics
95    ///
96    /// This function will panic if `self` is not owned by the `store`
97    /// specified.
98    ///
99    /// # Examples
100    ///
101    /// Calling a function which takes no parameters and has no return value:
102    ///
103    /// ```
104    /// # use wasmtime::component::Func;
105    /// # use wasmtime::Store;
106    /// # fn foo(func: &Func, store: &mut Store<()>) -> wasmtime::Result<()> {
107    /// let typed = func.typed::<(), ()>(&store)?;
108    /// typed.call(store, ())?;
109    /// # Ok(())
110    /// # }
111    /// ```
112    ///
113    /// Calling a function which takes one string parameter and returns a
114    /// string:
115    ///
116    /// ```
117    /// # use wasmtime::component::Func;
118    /// # use wasmtime::Store;
119    /// # fn foo(func: &Func, mut store: Store<()>) -> wasmtime::Result<()> {
120    /// let typed = func.typed::<(&str,), (String,)>(&store)?;
121    /// let ret = typed.call(&mut store, ("Hello, ",))?.0;
122    /// println!("returned string was: {}", ret);
123    /// # Ok(())
124    /// # }
125    /// ```
126    ///
127    /// Calling a function which takes multiple parameters and returns a boolean:
128    ///
129    /// ```
130    /// # use wasmtime::component::Func;
131    /// # use wasmtime::Store;
132    /// # fn foo(func: &Func, mut store: Store<()>) -> wasmtime::Result<()> {
133    /// let typed = func.typed::<(u32, Option<&str>, &[u8]), (bool,)>(&store)?;
134    /// let ok: bool = typed.call(&mut store, (1, Some("hello"), b"bytes!"))?.0;
135    /// println!("return value was: {ok}");
136    /// # Ok(())
137    /// # }
138    /// ```
139    pub fn typed<Params, Return>(&self, store: impl AsContext) -> Result<TypedFunc<Params, Return>>
140    where
141        Params: ComponentNamedList + Lower,
142        Return: ComponentNamedList + Lift,
143    {
144        self._typed(store.as_context().0, None)
145    }
146
147    pub(crate) fn _typed<Params, Return>(
148        &self,
149        store: &StoreOpaque,
150        instance: Option<&ComponentInstance>,
151    ) -> Result<TypedFunc<Params, Return>>
152    where
153        Params: ComponentNamedList + Lower,
154        Return: ComponentNamedList + Lift,
155    {
156        self.typecheck::<Params, Return>(store, instance)?;
157        unsafe { Ok(TypedFunc::new_unchecked(*self)) }
158    }
159
160    fn typecheck<Params, Return>(
161        &self,
162        store: &StoreOpaque,
163        instance: Option<&ComponentInstance>,
164    ) -> Result<()>
165    where
166        Params: ComponentNamedList + Lower,
167        Return: ComponentNamedList + Lift,
168    {
169        let cx = InstanceType::new(instance.unwrap_or_else(|| self.instance.id().get(store)));
170        let ty = &cx.types[self.ty_index(store)];
171
172        Params::typecheck(&InterfaceType::Tuple(ty.params), &cx)
173            .context("type mismatch with parameters")?;
174        Return::typecheck(&InterfaceType::Tuple(ty.results), &cx)
175            .context("type mismatch with results")?;
176
177        Ok(())
178    }
179
180    /// Get the type of this function.
181    pub fn ty(&self, store: impl AsContext) -> ComponentFunc {
182        self.ty_(store.as_context().0)
183    }
184
185    fn ty_(&self, store: &StoreOpaque) -> ComponentFunc {
186        let cx = InstanceType::new(self.instance.id().get(store));
187        let ty = self.ty_index(store);
188        ComponentFunc::from(ty, &cx)
189    }
190
191    fn ty_index(&self, store: &StoreOpaque) -> TypeFuncIndex {
192        let instance = self.instance.id().get(store);
193        let (ty, _, _) = instance.component().export_lifted_function(self.index);
194        ty
195    }
196
197    /// Invokes this function with the `params` given and returns the result.
198    ///
199    /// The `params` provided must match the parameters that this function takes
200    /// in terms of their types and the number of parameters. Results will be
201    /// written to the `results` slice provided if the call completes
202    /// successfully. The initial types of the values in `results` are ignored
203    /// and values are overwritten to write the result. It's required that the
204    /// size of `results` exactly matches the number of results that this
205    /// function produces.
206    ///
207    /// This will also call the corresponding `post-return` function, if any.
208    ///
209    /// For more detailed information see the documentation of
210    /// [`TypedFunc::call`].
211    ///
212    /// # Errors
213    ///
214    /// Returns an error in situations including but not limited to:
215    ///
216    /// * `params` is not the right size or if the values have the wrong type
217    /// * `results` is not the right size
218    /// * A trap occurs while executing the function
219    /// * The function calls a host function which returns an error
220    /// * The `store` used requires the use of [`Func::call_async`] instead. See
221    ///   [store documentation](crate#async) for more information.
222    ///
223    /// See [`TypedFunc::call`] for more information in addition to
224    /// [`wasmtime::Func::call`](crate::Func::call).
225    ///
226    /// This function will return an [`OutOfMemory`][crate::OutOfMemory] error when
227    /// memory allocation fails. See the `OutOfMemory` type's documentation for
228    /// details on Wasmtime's out-of-memory handling.
229    ///
230    /// # Panics
231    ///
232    /// Panics if `store` does not own this function.
233    pub fn call(
234        &self,
235        mut store: impl AsContextMut,
236        params: &[Val],
237        results: &mut [Val],
238    ) -> Result<()> {
239        let mut store = store.as_context_mut();
240        store.0.validate_sync_call()?;
241        self.call_impl(store.as_context_mut(), params, results)?;
242        Ok(())
243    }
244
245    /// Exactly like [`Self::call`] except for use on async stores.
246    ///
247    /// # Errors
248    ///
249    /// This function will return an [`OutOfMemory`][crate::OutOfMemory] error when
250    /// memory allocation fails. See the `OutOfMemory` type's documentation for
251    /// details on Wasmtime's out-of-memory handling.
252    ///
253    /// # Panics
254    ///
255    /// Panics if `store` does not own this function.
256    #[cfg(feature = "async")]
257    pub async fn call_async(
258        &self,
259        mut store: impl AsContextMut<Data: Send>,
260        params: &[Val],
261        results: &mut [Val],
262    ) -> Result<()> {
263        let mut store = store.as_context_mut();
264
265        #[cfg(feature = "component-model-async")]
266        if store.0.concurrency_support() {
267            let call = self.start_call_concurrent(&mut store, params, results)?;
268            return store
269                .run_concurrent_trap_on_idle(async |store| {
270                    self.finish_call_concurrent(store, call).await
271                })
272                .await?;
273        }
274
275        store
276            .on_fiber(|store| self.call_impl(store, params, results))
277            .await?
278    }
279
280    pub(crate) fn check_params_results<T>(
281        &self,
282        store: StoreContextMut<T>,
283        params: &[Val],
284        results: &mut [Val],
285    ) -> Result<()> {
286        let ty = self.ty(&store);
287        if ty.params().len() != params.len() {
288            bail!(
289                "expected {} argument(s), got {}",
290                ty.params().len(),
291                params.len(),
292            );
293        }
294
295        if ty.results().len() != results.len() {
296            bail!(
297                "expected {} result(s), got {}",
298                ty.results().len(),
299                results.len(),
300            );
301        }
302
303        Ok(())
304    }
305
306    fn call_impl(
307        &self,
308        mut store: impl AsContextMut,
309        params: &[Val],
310        results: &mut [Val],
311    ) -> Result<()> {
312        let mut store = store.as_context_mut();
313
314        self.check_params_results(store.as_context_mut(), params, results)?;
315
316        if self.abi_async(store.0) {
317            unreachable!(
318                "async-lifted exports should have failed validation \
319                 when `component-model-async` feature disabled"
320            );
321        }
322
323        // SAFETY: the chosen representations of type parameters to `call_raw`
324        // here should be generally safe to work with:
325        //
326        // * parameters use `MaybeUninit<[MaybeUninit<ValRaw>; MAX_FLAT_PARAMS]>`
327        //   which represents the maximal possible number of parameters that can
328        //   be passed to lifted component functions. This is modeled with
329        //   `MaybeUninit` to represent how it all starts as uninitialized and
330        //   thus can't be safely read during lowering.
331        //
332        // * results are modeled as `[ValRaw; MAX_FLAT_RESULTS]` which
333        //   represents the maximal size of values that can be returned. Note
334        //   that if the function doesn't actually have a return value then the
335        //   `ValRaw` inside the array will have undefined contents. That is
336        //   safe in Rust, however, due to `ValRaw` being a `union`. The
337        //   contents should dynamically not be read due to the type of the
338        //   function used here matching the actual lift.
339        let (_, post_return_arg) = unsafe {
340            self.call_raw(
341                store.as_context_mut(),
342                |cx, ty, dst: &mut MaybeUninit<[MaybeUninit<ValRaw>; MAX_FLAT_PARAMS]>| {
343                    // SAFETY: it's safe to assume that
344                    // `MaybeUninit<array-of-maybe-uninit>` is initialized because
345                    // each individual element is still considered uninitialized.
346                    let dst: &mut [MaybeUninit<ValRaw>] = dst.assume_init_mut();
347                    Self::lower_args(cx, params, ty, dst)
348                },
349                |cx, results_ty, src: &[ValRaw; MAX_FLAT_RESULTS]| {
350                    let max_flat = MAX_FLAT_RESULTS;
351                    for (result, slot) in
352                        Self::lift_results(cx, results_ty, src, max_flat)?.zip(results)
353                    {
354                        *slot = result?;
355                    }
356                    Ok(())
357                },
358            )?
359        };
360
361        self.post_return_impl(store, post_return_arg)
362    }
363
364    pub(crate) fn lifted_core_func(&self, store: &mut StoreOpaque) -> NonNull<VMFuncRef> {
365        let def = {
366            let instance = self.instance.id().get(store);
367            let (_ty, def, _options) = instance.component().export_lifted_function(self.index);
368            def.clone()
369        };
370        match self.instance.lookup_vmdef(store, &def) {
371            Export::Function(f) => f.vm_func_ref(store),
372            _ => unreachable!(),
373        }
374    }
375
376    pub(crate) fn post_return_core_func(&self, store: &StoreOpaque) -> Option<NonNull<VMFuncRef>> {
377        let instance = self.instance.id().get(store);
378        let component = instance.component();
379        let (_ty, _def, options) = component.export_lifted_function(self.index);
380        let post_return = component.env_component().options[options].post_return;
381        post_return.map(|i| instance.runtime_post_return(i))
382    }
383
384    pub(crate) fn abi_async(&self, store: &StoreOpaque) -> bool {
385        let instance = self.instance.id().get(store);
386        let component = instance.component();
387        let (_ty, _def, options) = component.export_lifted_function(self.index);
388        component.env_component().options[options].async_
389    }
390
391    pub(crate) fn abi_info<'a>(
392        &self,
393        store: &'a StoreOpaque,
394    ) -> (
395        OptionsIndex,
396        InstanceFlags,
397        TypeFuncIndex,
398        &'a CanonicalOptions,
399    ) {
400        let vminstance = self.instance.id().get(store);
401        let component = vminstance.component();
402        let (ty, _def, options_index) = component.export_lifted_function(self.index);
403        let raw_options = &component.env_component().options[options_index];
404        (
405            options_index,
406            vminstance.instance_flags(raw_options.instance),
407            ty,
408            raw_options,
409        )
410    }
411
412    /// Invokes the underlying wasm function, lowering arguments and lifting the
413    /// result.
414    ///
415    /// The `lower` function and `lift` function provided here are what actually
416    /// do the lowering and lifting. The `LowerParams` and `LowerReturn` types
417    /// are what will be allocated on the stack for this function call. They
418    /// should be appropriately sized for the lowering/lifting operation
419    /// happening.
420    ///
421    /// # Safety
422    ///
423    /// The safety of this function relies on the correct definitions of the
424    /// `LowerParams` and `LowerReturn` type. They must match the type of `self`
425    /// for the params/results that are going to be produced. Additionally
426    /// these types must be representable with a sequence of `ValRaw` values.
427    unsafe fn call_raw<T, Return, LowerParams, LowerReturn>(
428        &self,
429        mut store: StoreContextMut<'_, T>,
430        lower: impl FnOnce(
431            &mut LowerContext<'_, T>,
432            InterfaceType,
433            &mut MaybeUninit<LowerParams>,
434        ) -> Result<()>,
435        lift: impl FnOnce(&mut LiftContext<'_>, InterfaceType, &LowerReturn) -> Result<Return>,
436    ) -> Result<(Return, ValRaw)>
437    where
438        LowerParams: Copy,
439        LowerReturn: Copy,
440    {
441        let export = self.lifted_core_func(store.0);
442        let (_options, _flags, _ty, raw_options) = self.abi_info(store.0);
443        let instance = self.instance.runtime_instance(raw_options.instance);
444
445        if !store.0.may_enter(instance)? {
446            bail!(crate::Trap::CannotEnterComponent);
447        }
448
449        let async_type = self.abi_async(store.0);
450        store.0.enter_guest_sync_call(None, async_type, instance)?;
451
452        #[repr(C)]
453        union Union<Params: Copy, Return: Copy> {
454            params: Params,
455            ret: Return,
456        }
457
458        let space = &mut MaybeUninit::<Union<LowerParams, LowerReturn>>::uninit();
459
460        // Double-check the size/alignment of `space`, just in case.
461        //
462        // Note that this alone is not enough to guarantee the validity of the
463        // `unsafe` block below, but it's definitely required. In any case LLVM
464        // should be able to trivially see through these assertions and remove
465        // them in release mode.
466        let val_size = mem::size_of::<ValRaw>();
467        let val_align = mem::align_of::<ValRaw>();
468        assert!(mem::size_of_val(space) % val_size == 0);
469        assert!(mem::size_of_val(map_maybe_uninit!(space.params)) % val_size == 0);
470        assert!(mem::size_of_val(map_maybe_uninit!(space.ret)) % val_size == 0);
471        assert!(mem::align_of_val(space) == val_align);
472        assert!(mem::align_of_val(map_maybe_uninit!(space.params)) == val_align);
473        assert!(mem::align_of_val(map_maybe_uninit!(space.ret)) == val_align);
474
475        self.with_lower_context(store.as_context_mut(), |cx, ty| {
476            lower(cx, ty, map_maybe_uninit!(space.params))
477        })?;
478
479        // SAFETY: We are providing the guarantee that all the inputs are valid.
480        // The various pointers passed in for the function are all valid since
481        // they're coming from our store, and the `params_and_results` should
482        // have the correct layout for the core wasm function we're calling.
483        // Note that this latter point relies on the correctness of this module
484        // and `ComponentType` implementations, hence `ComponentType` being an
485        // `unsafe` trait.
486        unsafe {
487            crate::Func::call_unchecked_raw(
488                &mut store,
489                export,
490                NonNull::new(core::ptr::slice_from_raw_parts_mut(
491                    space.as_mut_ptr().cast(),
492                    mem::size_of_val(space) / mem::size_of::<ValRaw>(),
493                ))
494                .unwrap(),
495            )?;
496        }
497
498        // Validate that the task, after returning, has no more active borrows
499        // as they're required to have been dropped by this point.
500        store
501            .0
502            .component_resource_tables(Some(self.instance))
503            .validate_scope_exit()?;
504
505        // SAFETY: We're relying on the correctness of the structure of
506        // `LowerReturn` and the type-checking performed to acquire the
507        // `TypedFunc` to make this safe. It should be the case that
508        // `LowerReturn` is the exact representation of the return value when
509        // interpreted as `[ValRaw]`, and additionally they should have the
510        // correct types for the function we just called (which filled in the
511        // return values).
512        let ret: &LowerReturn = unsafe { map_maybe_uninit!(space.ret).assume_init_ref() };
513
514        // Lift the result into the host while managing post-return state
515        // here as well.
516        //
517        // After a successful lift the return value of the function, which
518        // is currently required to be 0 or 1 values according to the
519        // canonical ABI, is saved within the `Store`'s `FuncData`. This'll
520        // later get used in post-return.
521        let val = self.with_lift_context(store.0, |cx, ty| lift(cx, ty, ret))?;
522
523        // SAFETY: it's a contract of this function that `LowerReturn` is an
524        // appropriate representation of the result of this function.
525        let ret_slice = unsafe { storage_as_slice(ret) };
526
527        Ok((
528            val,
529            match ret_slice.len() {
530                0 => ValRaw::i32(0),
531                1 => ret_slice[0],
532                _ => unreachable!(),
533            },
534        ))
535    }
536
537    #[doc(hidden)]
538    #[deprecated(note = "no longer needs to be called; this function has no effect")]
539    pub fn post_return(&self, _store: impl AsContextMut) -> Result<()> {
540        Ok(())
541    }
542
543    #[doc(hidden)]
544    #[deprecated(note = "no longer needs to be called; this function has no effect")]
545    #[cfg(feature = "async")]
546    pub async fn post_return_async(&self, _store: impl AsContextMut<Data: Send>) -> Result<()> {
547        Ok(())
548    }
549
550    pub(crate) fn post_return_impl(&self, mut store: impl AsContextMut, arg: ValRaw) -> Result<()> {
551        let mut store = store.as_context_mut();
552
553        let index = self.index;
554        let vminstance = self.instance.id().get(store.0);
555        let component = vminstance.component();
556        let (_ty, _def, options) = component.export_lifted_function(index);
557        let post_return = self.post_return_core_func(store.0);
558        let flags = vminstance.instance_flags(component.env_component().options[options].instance);
559
560        unsafe {
561            call_post_return(&mut store, post_return, arg, flags)?;
562            store.0.exit_guest_sync_call()?;
563        }
564        Ok(())
565    }
566
567    pub(crate) fn lower_args<T>(
568        cx: &mut LowerContext<'_, T>,
569        params: &[Val],
570        params_ty: InterfaceType,
571        dst: &mut [MaybeUninit<ValRaw>],
572    ) -> Result<()> {
573        let params_ty = match params_ty {
574            InterfaceType::Tuple(i) => &cx.types[i],
575            _ => unreachable!(),
576        };
577        if params_ty.abi.flat_count(MAX_FLAT_PARAMS).is_some() {
578            let dst = &mut dst.iter_mut();
579
580            params
581                .iter()
582                .zip(params_ty.types.iter())
583                .try_for_each(|(param, ty)| param.lower(cx, *ty, dst))
584        } else {
585            Self::store_args(cx, &params_ty, params, dst)
586        }
587    }
588
589    fn store_args<T>(
590        cx: &mut LowerContext<'_, T>,
591        params_ty: &TypeTuple,
592        args: &[Val],
593        dst: &mut [MaybeUninit<ValRaw>],
594    ) -> Result<()> {
595        let size = usize::try_from(params_ty.abi.size32).unwrap();
596        let ptr = cx.realloc(0, 0, params_ty.abi.align32, size)?;
597        let mut offset = ptr;
598        for (ty, arg) in params_ty.types.iter().zip(args) {
599            let abi = cx.types.canonical_abi(ty);
600            arg.store(cx, *ty, abi.next_field32_size(&mut offset))?;
601        }
602
603        dst[0].write(ValRaw::i64(ptr as i64));
604
605        Ok(())
606    }
607
608    pub(crate) fn lift_results<'a, 'b>(
609        cx: &'a mut LiftContext<'b>,
610        results_ty: InterfaceType,
611        src: &'a [ValRaw],
612        max_flat: usize,
613    ) -> Result<Box<dyn Iterator<Item = Result<Val>> + 'a>> {
614        let results_ty = match results_ty {
615            InterfaceType::Tuple(i) => &cx.types[i],
616            _ => unreachable!(),
617        };
618        if results_ty.abi.flat_count(max_flat).is_some() {
619            let mut flat = src.iter();
620            Ok(try_new::<Box<_>>(
621                results_ty
622                    .types
623                    .iter()
624                    .map(move |ty| Val::lift(cx, *ty, &mut flat)),
625            )?)
626        } else {
627            let iter = Self::load_results(cx, results_ty, &mut src.iter())?;
628            Ok(try_new::<Box<_>>(iter)?)
629        }
630    }
631
632    fn load_results<'a, 'b>(
633        cx: &'a mut LiftContext<'b>,
634        results_ty: &'a TypeTuple,
635        src: &mut core::slice::Iter<'_, ValRaw>,
636    ) -> Result<impl Iterator<Item = Result<Val>> + use<'a, 'b>> {
637        // FIXME(#4311): needs to read an i64 for memory64
638        let ptr = usize::try_from(src.next().unwrap().get_u32())?;
639        if ptr % usize::try_from(results_ty.abi.align32)? != 0 {
640            bail!("return pointer not aligned");
641        }
642
643        let bytes = cx
644            .memory()
645            .get(ptr..)
646            .and_then(|b| b.get(..usize::try_from(results_ty.abi.size32).unwrap()))
647            .ok_or_else(|| crate::format_err!("pointer out of bounds of memory"))?;
648
649        let mut offset = 0;
650        Ok(results_ty.types.iter().map(move |ty| {
651            let abi = cx.types.canonical_abi(ty);
652            let offset = abi.next_field32_size(&mut offset);
653            Val::load(cx, *ty, &bytes[offset..][..abi.size32 as usize])
654        }))
655    }
656
657    #[cfg(feature = "component-model-async")]
658    pub(crate) fn instance(self) -> Instance {
659        self.instance
660    }
661
662    /// Creates a `LowerContext` using the configuration values of this lifted
663    /// function.
664    ///
665    /// The `lower` closure provided should perform the actual lowering and
666    /// return the result of the lowering operation which is then returned from
667    /// this function as well.
668    pub(crate) fn with_lower_context<T>(
669        self,
670        mut store: StoreContextMut<T>,
671        lower: impl FnOnce(&mut LowerContext<T>, InterfaceType) -> Result<()>,
672    ) -> Result<()> {
673        let (options_idx, mut flags, ty, _) = self.abi_info(store.0);
674
675        // Perform the actual lowering, where while this is running the
676        // component is forbidden from calling imports.
677        unsafe {
678            debug_assert!(flags.may_leave());
679            flags.set_may_leave(false);
680        }
681        let mut cx = LowerContext::new(store.as_context_mut(), options_idx, self.instance);
682        let param_ty = InterfaceType::Tuple(cx.types[ty].params);
683        let result = lower(&mut cx, param_ty);
684        unsafe { flags.set_may_leave(true) };
685        result
686    }
687
688    /// Creates a `LiftContext` using the configuration values with this lifted
689    /// function.
690    ///
691    /// The closure `lift` provided should actually perform the lift itself and
692    /// the result of that closure is returned from this function call as well.
693    pub(crate) fn with_lift_context<R>(
694        self,
695        store: &mut StoreOpaque,
696        lift: impl FnOnce(&mut LiftContext, InterfaceType) -> Result<R>,
697    ) -> Result<R> {
698        let (options, _flags, ty, _) = self.abi_info(store);
699        let mut cx = LiftContext::new(store, options, self.instance);
700        let ty = InterfaceType::Tuple(cx.types[ty].results);
701        lift(&mut cx, ty)
702    }
703}
704
705pub(crate) unsafe fn call_post_return(
706    mut store: impl AsContextMut,
707    func: Option<NonNull<VMFuncRef>>,
708    arg: ValRaw,
709    mut flags: InstanceFlags,
710) -> Result<()> {
711    unsafe {
712        // Post return functions are forbidden from calling imports or
713        // intrinsics.
714        flags.set_may_leave(false);
715
716        // If the function actually had a `post-return` configured in its
717        // canonical options that's executed here.
718        if let Some(func) = func {
719            crate::Func::call_unchecked_raw(
720                &mut store.as_context_mut(),
721                func,
722                core::slice::from_ref(&arg).into(),
723            )?;
724        }
725
726        // And finally if everything completed successfully then the "may
727        // leave" flags is set to `true` again here which enables further
728        // use of the component.
729        flags.set_may_leave(true);
730    }
731
732    Ok(())
733}