Path: blob/main/crates/test-util/src/component.rs
3069 views
use arbitrary::Arbitrary;1use std::mem::MaybeUninit;2use wasmtime::component::__internal::{3CanonicalAbiInfo, InstanceType, InterfaceType, LiftContext, LowerContext,4};5use wasmtime::component::{ComponentType, Lift, Lower};6use wasmtime::{Config, Engine};7use wasmtime_environ::prelude::*;89pub fn config() -> Config {10drop(env_logger::try_init());1112let mut config = Config::new();13config.wasm_component_model(true);1415// When `WASMTIME_TEST_NO_HOG_MEMORY` is set it means we're in qemu. The16// component model tests create a disproportionate number of instances so17// try to cut down on virtual memory usage by avoiding 4G reservations.18if std::env::var("WASMTIME_TEST_NO_HOG_MEMORY").is_ok() {19config.memory_reservation(0);20config.memory_guard_size(0);21}22config23}2425pub fn engine() -> Engine {26Engine::new(&config()).unwrap()27}2829pub fn async_engine() -> Engine {30Engine::default()31}3233/// Newtype wrapper for `f32` whose `PartialEq` impl considers NaNs equal to each other.34#[derive(Copy, Clone, Debug, Arbitrary)]35pub struct Float32(pub f32);3637/// Newtype wrapper for `f64` whose `PartialEq` impl considers NaNs equal to each other.38#[derive(Copy, Clone, Debug, Arbitrary)]39pub struct Float64(pub f64);4041macro_rules! forward_impls {42($($a:ty => $b:ty,)*) => ($(43unsafe impl ComponentType for $a {44type Lower = <$b as ComponentType>::Lower;4546const ABI: CanonicalAbiInfo = <$b as ComponentType>::ABI;4748#[inline]49fn typecheck(ty: &InterfaceType, types: &InstanceType<'_>) -> Result<()> {50<$b as ComponentType>::typecheck(ty, types)51}52}5354unsafe impl Lower for $a {55fn linear_lower_to_flat<U>(56&self,57cx: &mut LowerContext<'_, U>,58ty: InterfaceType,59dst: &mut MaybeUninit<Self::Lower>,60) -> Result<()> {61<$b as Lower>::linear_lower_to_flat(&self.0, cx, ty, dst)62}6364fn linear_lower_to_memory<U>(&self, cx: &mut LowerContext<'_, U>, ty: InterfaceType, offset: usize) -> Result<()> {65<$b as Lower>::linear_lower_to_memory(&self.0, cx, ty, offset)66}67}6869unsafe impl Lift for $a {70fn linear_lift_from_flat(cx: &mut LiftContext<'_>, ty: InterfaceType, src: &Self::Lower) -> Result<Self> {71Ok(Self(<$b as Lift>::linear_lift_from_flat(cx, ty, src)?))72}7374fn linear_lift_from_memory(cx: &mut LiftContext<'_>, ty: InterfaceType, bytes: &[u8]) -> Result<Self> {75Ok(Self(<$b as Lift>::linear_lift_from_memory(cx, ty, bytes)?))76}77}7879impl PartialEq for $a {80fn eq(&self, other: &Self) -> bool {81self.0 == other.0 || (self.0.is_nan() && other.0.is_nan())82}83}84)*)85}8687forward_impls! {88Float32 => f32,89Float64 => f64,90}919293