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