wasmtime_test_util/
component.rs1use arbitrary::Arbitrary;
2use std::mem::MaybeUninit;
3use wasmtime::component::__internal::{
4 CanonicalAbiInfo, InstanceType, InterfaceType, LiftContext, LowerContext,
5};
6use wasmtime::component::{ComponentType, Lift, Lower};
7use wasmtime::{Config, Engine};
8use wasmtime_environ::prelude::*;
9
10pub fn config() -> Config {
11 drop(env_logger::try_init());
12
13 let mut config = Config::new();
14 config.wasm_component_model(true);
15
16 if std::env::var("WASMTIME_TEST_NO_HOG_MEMORY").is_ok() {
20 config.memory_reservation(0);
21 config.memory_guard_size(0);
22 }
23 config
24}
25
26pub fn engine() -> Engine {
27 Engine::new(&config()).unwrap()
28}
29
30pub fn async_engine() -> Engine {
31 Engine::default()
32}
33
34#[derive(Copy, Clone, Debug, Arbitrary)]
36pub struct Float32(pub f32);
37
38#[derive(Copy, Clone, Debug, Arbitrary)]
40pub struct Float64(pub f64);
41
42macro_rules! forward_impls {
43 ($($a:ty => $b:ty,)*) => ($(
44 unsafe impl ComponentType for $a {
45 type Lower = <$b as ComponentType>::Lower;
46
47 const ABI: CanonicalAbiInfo = <$b as ComponentType>::ABI;
48
49 #[inline]
50 fn typecheck(ty: &InterfaceType, types: &InstanceType<'_>) -> Result<()> {
51 <$b as ComponentType>::typecheck(ty, types)
52 }
53 }
54
55 unsafe impl Lower for $a {
56 fn linear_lower_to_flat<U>(
57 &self,
58 cx: &mut LowerContext<'_, U>,
59 ty: InterfaceType,
60 dst: &mut MaybeUninit<Self::Lower>,
61 ) -> Result<()> {
62 <$b as Lower>::linear_lower_to_flat(&self.0, cx, ty, dst)
63 }
64
65 fn linear_lower_to_memory<U>(&self, cx: &mut LowerContext<'_, U>, ty: InterfaceType, offset: usize) -> Result<()> {
66 <$b as Lower>::linear_lower_to_memory(&self.0, cx, ty, offset)
67 }
68 }
69
70 unsafe impl Lift for $a {
71 fn linear_lift_from_flat(cx: &mut LiftContext<'_>, ty: InterfaceType, src: &Self::Lower) -> Result<Self> {
72 Ok(Self(<$b as Lift>::linear_lift_from_flat(cx, ty, src)?))
73 }
74
75 fn linear_lift_from_memory(cx: &mut LiftContext<'_>, ty: InterfaceType, bytes: &[u8]) -> Result<Self> {
76 Ok(Self(<$b as Lift>::linear_lift_from_memory(cx, ty, bytes)?))
77 }
78 }
79
80 impl PartialEq for $a {
81 fn eq(&self, other: &Self) -> bool {
82 self.0 == other.0 || (self.0.is_nan() && other.0.is_nan())
83 }
84 }
85 )*)
86}
87
88forward_impls! {
89 Float32 => f32,
90 Float64 => f64,
91}