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