1 use crate::prelude::*; 2 use std::borrow::Cow; 3 4 use super::{canonicalize_nan32, canonicalize_nan64, unwrap_val}; 5 use wasm_wave::wasm::{WasmFunc, WasmType, WasmTypeKind, WasmValue, WasmValueError}; 6 7 impl WasmType for crate::ValType { 8 fn kind(&self) -> WasmTypeKind { 9 match self { 10 Self::I32 => WasmTypeKind::S32, 11 Self::I64 => WasmTypeKind::S64, 12 Self::F32 => WasmTypeKind::F32, 13 Self::F64 => WasmTypeKind::F64, 14 Self::V128 => WasmTypeKind::Tuple, 15 16 Self::Ref(_) => WasmTypeKind::Unsupported, 17 } 18 } 19 20 fn tuple_element_types(&self) -> Box<dyn Iterator<Item = Self> + '_> { 21 match *self { 22 Self::V128 => {} 23 _ => panic!("tuple_element_types called on non-tuple type"), 24 } 25 Box::new([Self::I64, Self::I64].into_iter()) 26 } 27 } 28 29 impl WasmValue for crate::Val { 30 type Type = crate::ValType; 31 32 fn kind(&self) -> WasmTypeKind { 33 match self { 34 Self::I32(_) => WasmTypeKind::S32, 35 Self::I64(_) => WasmTypeKind::S64, 36 Self::F32(_) => WasmTypeKind::F32, 37 Self::F64(_) => WasmTypeKind::F64, 38 Self::V128(_) => WasmTypeKind::Tuple, 39 Self::FuncRef(_) => WasmTypeKind::Unsupported, 40 Self::ExternRef(_) => WasmTypeKind::Unsupported, 41 Self::AnyRef(_) => WasmTypeKind::Unsupported, 42 } 43 } 44 45 fn make_s32(val: i32) -> Self { 46 Self::I32(val) 47 } 48 fn make_s64(val: i64) -> Self { 49 Self::I64(val) 50 } 51 fn make_f32(val: f32) -> Self { 52 let val = canonicalize_nan32(val); 53 Self::F32(val.to_bits()) 54 } 55 fn make_f64(val: f64) -> Self { 56 let val = canonicalize_nan64(val); 57 Self::F64(val.to_bits()) 58 } 59 fn make_tuple( 60 ty: &Self::Type, 61 vals: impl IntoIterator<Item = Self>, 62 ) -> Result<Self, WasmValueError> { 63 match *ty { 64 Self::Type::V128 => {} 65 _ => { 66 return Err(WasmValueError::Other( 67 "tuples only used for v128 (v64x2)".to_string(), 68 )); 69 } 70 } 71 let [l_val, h_val]: [Self; 2] = vals 72 .into_iter() 73 .collect::<Vec<_>>() 74 .try_into() 75 .map_err(|_| WasmValueError::Other("expected 2 values".to_string()))?; 76 77 let (Some(l), Some(h)) = (l_val.i64(), h_val.i64()) else { 78 return Err(WasmValueError::Other("expected 2 i64s (v64x2)".to_string())); 79 }; 80 Ok(Self::V128(((h as u128) << 64 | (l as u128)).into())) 81 } 82 83 fn unwrap_s32(&self) -> i32 { 84 *unwrap_val!(self, Self::I32, "s32") 85 } 86 87 fn unwrap_s64(&self) -> i64 { 88 *unwrap_val!(self, Self::I64, "s64") 89 } 90 91 fn unwrap_f32(&self) -> f32 { 92 let val = f32::from_bits(*unwrap_val!(self, Self::F32, "f32")); 93 canonicalize_nan32(val) 94 } 95 96 fn unwrap_f64(&self) -> f64 { 97 let val = f64::from_bits(*unwrap_val!(self, Self::F64, "f64")); 98 canonicalize_nan64(val) 99 } 100 #[allow(clippy::cast_possible_truncation)] 101 fn unwrap_tuple(&self) -> Box<dyn Iterator<Item = Cow<Self>> + '_> { 102 let v = unwrap_val!(self, Self::V128, "tuple").as_u128(); 103 let low = v as i64; 104 let high = (v >> 64) as i64; 105 Box::new( 106 [Self::I64(low), Self::I64(high)] 107 .into_iter() 108 .map(Cow::Owned), 109 ) 110 } 111 } 112 113 impl WasmFunc for crate::FuncType { 114 type Type = crate::ValType; 115 116 fn params(&self) -> Box<dyn Iterator<Item = Self::Type> + '_> { 117 Box::new(self.params()) 118 } 119 120 fn results(&self) -> Box<dyn Iterator<Item = Self::Type> + '_> { 121 Box::new(self.results()) 122 } 123 } 124 125 #[cfg(test)] 126 mod tests { 127 #[test] 128 fn core_vals_smoke_test() { 129 use crate::Val; 130 for (val, want) in [ 131 (Val::I32(10), "10"), 132 (Val::I64(-10), "-10"), 133 (1.5f32.into(), "1.5"), 134 (f32::NAN.into(), "nan"), 135 (f32::INFINITY.into(), "inf"), 136 (f32::NEG_INFINITY.into(), "-inf"), 137 ((-1.5f64).into(), "-1.5"), 138 (f32::NAN.into(), "nan"), 139 (f32::INFINITY.into(), "inf"), 140 (f32::NEG_INFINITY.into(), "-inf"), 141 ( 142 Val::V128(0x1234567890abcdef1122334455667788.into()), 143 "(1234605616436508552, 1311768467294899695)", 144 ), 145 ] { 146 let got = wasm_wave::to_string(&val) 147 .unwrap_or_else(|err| panic!("failed to serialize {val:?}: {err}")); 148 assert_eq!(got, want, "for {val:?}"); 149 } 150 } 151 } 152