|
| 1 | +wit_bindgen_host_wasmtime_rust::export!("../../tests/runtime/unions/imports.wit"); |
| 2 | + |
| 3 | +use imports::*; |
| 4 | + |
| 5 | +#[derive(Default)] |
| 6 | +pub struct MyImports; |
| 7 | + |
| 8 | +impl Imports for MyImports { |
| 9 | + fn add_one_integer(&mut self, num: AllIntegers) -> AllIntegers { |
| 10 | + match num { |
| 11 | + AllIntegers::Bool(false) => AllIntegers::Bool(true), |
| 12 | + AllIntegers::Bool(true) => AllIntegers::Bool(false), |
| 13 | + AllIntegers::U8(n) => AllIntegers::U8(n.wrapping_add(1)), |
| 14 | + AllIntegers::U16(n) => AllIntegers::U16(n.wrapping_add(1)), |
| 15 | + AllIntegers::U32(n) => AllIntegers::U32(n.wrapping_add(1)), |
| 16 | + AllIntegers::U64(n) => AllIntegers::U64(n.wrapping_add(1)), |
| 17 | + AllIntegers::I8(n) => AllIntegers::I8(n.wrapping_add(1)), |
| 18 | + AllIntegers::I16(n) => AllIntegers::I16(n.wrapping_add(1)), |
| 19 | + AllIntegers::I32(n) => AllIntegers::I32(n.wrapping_add(1)), |
| 20 | + AllIntegers::I64(n) => AllIntegers::I64(n.wrapping_add(1)), |
| 21 | + } |
| 22 | + } |
| 23 | + fn add_one_float(&mut self, num: AllFloats) -> AllFloats { |
| 24 | + match num { |
| 25 | + AllFloats::F32(n) => AllFloats::F32(n + 1.0), |
| 26 | + AllFloats::F64(n) => AllFloats::F64(n + 1.0), |
| 27 | + } |
| 28 | + } |
| 29 | + fn replace_first_char(&mut self, text: AllTextResult, c: char) -> AllTextResult { |
| 30 | + match text { |
| 31 | + AllTextResult::Char(_) => AllTextResult::Char(c), |
| 32 | + AllTextResult::String(t) => AllTextResult::String(format!("{}{}", c, &t[1..])), |
| 33 | + } |
| 34 | + } |
| 35 | + fn identify_integer(&mut self, num: AllIntegers) -> u8 { |
| 36 | + match num { |
| 37 | + AllIntegers::Bool { .. } => 0, |
| 38 | + AllIntegers::U8 { .. } => 1, |
| 39 | + AllIntegers::U16 { .. } => 2, |
| 40 | + AllIntegers::U32 { .. } => 3, |
| 41 | + AllIntegers::U64 { .. } => 4, |
| 42 | + AllIntegers::I8 { .. } => 5, |
| 43 | + AllIntegers::I16 { .. } => 6, |
| 44 | + AllIntegers::I32 { .. } => 7, |
| 45 | + AllIntegers::I64 { .. } => 8, |
| 46 | + } |
| 47 | + } |
| 48 | + fn identify_float(&mut self, num: AllFloats) -> u8 { |
| 49 | + match num { |
| 50 | + AllFloats::F32 { .. } => 0, |
| 51 | + AllFloats::F64 { .. } => 1, |
| 52 | + } |
| 53 | + } |
| 54 | + fn identify_text(&mut self, text: AllTextResult) -> u8 { |
| 55 | + match text { |
| 56 | + AllTextResult::Char { .. } => 0, |
| 57 | + AllTextResult::String { .. } => 1, |
| 58 | + } |
| 59 | + } |
| 60 | + fn identify_duplicated(&mut self, dup: DuplicatedS32) -> u8 { |
| 61 | + match dup { |
| 62 | + DuplicatedS32::I320 { .. } => 0, |
| 63 | + DuplicatedS32::I321 { .. } => 1, |
| 64 | + DuplicatedS32::I322 { .. } => 2, |
| 65 | + } |
| 66 | + } |
| 67 | + fn add_one_duplicated(&mut self, dup: DuplicatedS32) -> DuplicatedS32 { |
| 68 | + match dup { |
| 69 | + DuplicatedS32::I320(n) => DuplicatedS32::I320(n.wrapping_add(1)), |
| 70 | + DuplicatedS32::I321(n) => DuplicatedS32::I321(n.wrapping_add(1)), |
| 71 | + DuplicatedS32::I322(n) => DuplicatedS32::I322(n.wrapping_add(1)), |
| 72 | + } |
| 73 | + } |
| 74 | + fn identify_distinguishable_num(&mut self, num: DistinguishableNum) -> u8 { |
| 75 | + match num { |
| 76 | + DistinguishableNum::F64 { .. } => 0, |
| 77 | + DistinguishableNum::I64 { .. } => 1, |
| 78 | + } |
| 79 | + } |
| 80 | + fn add_one_distinguishable_num(&mut self, num: DistinguishableNum) -> DistinguishableNum { |
| 81 | + match num { |
| 82 | + DistinguishableNum::F64(n) => DistinguishableNum::F64(n + 1.0), |
| 83 | + DistinguishableNum::I64(n) => DistinguishableNum::I64(n.wrapping_add(1)), |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +wit_bindgen_host_wasmtime_rust::import!("../../tests/runtime/unions/exports.wit"); |
| 89 | + |
| 90 | +fn run(wasm: &str) -> Result<()> { |
| 91 | + use exports::*; |
| 92 | + |
| 93 | + let (exports, mut store) = crate::instantiate( |
| 94 | + wasm,im finding all kin |
| 95 | + |linker| imports::add_to_linker(linker, |cx| -> &mut MyImports { &mut cx.imports }), |
| 96 | + |store, module, linker| Exports::instantiate(store, module, linker), |
| 97 | + )?; |
| 98 | + |
| 99 | + exports.test_imports(&mut store)?; |
| 100 | + |
| 101 | + // Booleans |
| 102 | + assert!(matches!( |
| 103 | + exports.add_one_integer(&mut store, AllIntegers::Bool(false))?, |
| 104 | + AllIntegers::Bool(true) |
| 105 | + )); |
| 106 | + assert!(matches!( |
| 107 | + exports.add_one_integer(&mut store, AllIntegers::Bool(true))?, |
| 108 | + AllIntegers::Bool(false) |
| 109 | + )); |
| 110 | + // Unsigned integers |
| 111 | + assert!(matches!( |
| 112 | + exports.add_one_integer(&mut store, AllIntegers::U8(0))?, |
| 113 | + AllIntegers::U8(1) |
| 114 | + )); |
| 115 | + assert!(matches!( |
| 116 | + exports.add_one_integer(&mut store, AllIntegers::U8(u8::MAX))?, |
| 117 | + AllIntegers::U8(0) |
| 118 | + )); |
| 119 | + assert!(matches!( |
| 120 | + exports.add_one_integer(&mut store, AllIntegers::U16(0))?, |
| 121 | + AllIntegers::U16(1) |
| 122 | + )); |
| 123 | + assert!(matches!( |
| 124 | + exports.add_one_integer(&mut store, AllIntegers::U16(u16::MAX))?, |
| 125 | + AllIntegers::U16(0) |
| 126 | + )); |
| 127 | + assert!(matches!( |
| 128 | + exports.add_one_integer(&mut store, AllIntegers::U32(0))?, |
| 129 | + AllIntegers::U32(1) |
| 130 | + )); |
| 131 | + assert!(matches!( |
| 132 | + exports.add_one_integer(&mut store, AllIntegers::U32(u32::MAX))?, |
| 133 | + AllIntegers::U32(0) |
| 134 | + )); |
| 135 | + assert!(matches!( |
| 136 | + exports.add_one_integer(&mut store, AllIntegers::U64(0))?, |
| 137 | + AllIntegers::U64(1) |
| 138 | + )); |
| 139 | + assert!(matches!( |
| 140 | + exports.add_one_integer(&mut store, AllIntegers::U64(u64::MAX))?, |
| 141 | + AllIntegers::U64(0) |
| 142 | + )); |
| 143 | + // Signed integers |
| 144 | + assert!(matches!( |
| 145 | + exports.add_one_integer(&mut store, AllIntegers::I8(0))?, |
| 146 | + AllIntegers::I8(1) |
| 147 | + )); |
| 148 | + assert!(matches!( |
| 149 | + exports.add_one_integer(&mut store, AllIntegers::I8(i8::MAX))?, |
| 150 | + AllIntegers::I8(i8::MIN) |
| 151 | + )); |
| 152 | + assert!(matches!( |
| 153 | + exports.add_one_integer(&mut store, AllIntegers::I16(0))?, |
| 154 | + AllIntegers::I16(1) |
| 155 | + )); |
| 156 | + assert!(matches!( |
| 157 | + exports.add_one_integer(&mut store, AllIntegers::I16(i16::MAX))?, |
| 158 | + AllIntegers::I16(i16::MIN) |
| 159 | + )); |
| 160 | + assert!(matches!( |
| 161 | + exports.add_one_integer(&mut store, AllIntegers::I32(0))?, |
| 162 | + AllIntegers::I32(1) |
| 163 | + )); |
| 164 | + assert!(matches!( |
| 165 | + exports.add_one_integer(&mut store, AllIntegers::I32(i32::MAX))?, |
| 166 | + AllIntegers::I32(i32::MIN) |
| 167 | + )); |
| 168 | + assert!(matches!( |
| 169 | + exports.add_one_integer(&mut store, AllIntegers::I64(0))?, |
| 170 | + AllIntegers::I64(1) |
| 171 | + )); |
| 172 | + assert!(matches!( |
| 173 | + exports.add_one_integer(&mut store, AllIntegers::I64(i64::MAX))?, |
| 174 | + AllIntegers::I64(i64::MIN) |
| 175 | + )); |
| 176 | + |
| 177 | + // Floats |
| 178 | + match exports.add_one_float(&mut store, AllFloats::F32(0.0))? { |
| 179 | + AllFloats::F32(r) => assert_eq!(r, 1.0), |
| 180 | + _ => panic!(), |
| 181 | + } |
| 182 | + match exports.add_one_float(&mut store, AllFloats::F32(420.0))? { |
| 183 | + AllFloats::F32(r) => assert_eq!(r, 421.0), |
| 184 | + _ => panic!(), |
| 185 | + } |
| 186 | + match exports.add_one_float(&mut store, AllFloats::F64(0.0))? { |
| 187 | + AllFloats::F64(r) => assert_eq!(r, 1.0), |
| 188 | + _ => panic!(), |
| 189 | + } |
| 190 | + match exports.add_one_float(&mut store, AllFloats::F64(420.0))? { |
| 191 | + AllFloats::F64(r) => assert_eq!(r, 421.0), |
| 192 | + _ => panic!(), |
| 193 | + } |
| 194 | + |
| 195 | + // Text |
| 196 | + assert!(matches!( |
| 197 | + exports.replace_first_char(&mut store, AllTextParam::Char('a'), 'z')?, |
| 198 | + AllTextResult::Char('z') |
| 199 | + )); |
| 200 | + match exports.replace_first_char(&mut store, AllTextParam::String("abc"), 'z')? { |
| 201 | + AllTextResult::String(s) => assert_eq!(s, "zbc"), |
| 202 | + _ => panic!(), |
| 203 | + } |
| 204 | + |
| 205 | + // Identify Integers |
| 206 | + assert_eq!( |
| 207 | + exports.identify_integer(&mut store, AllIntegers::Bool(false))?, |
| 208 | + 0 |
| 209 | + ); |
| 210 | + assert_eq!(exports.identify_integer(&mut store, AllIntegers::U8(0))?, 1); |
| 211 | + assert_eq!( |
| 212 | + exports.identify_integer(&mut store, AllIntegers::U16(0))?, |
| 213 | + 2 |
| 214 | + ); |
| 215 | + assert_eq!( |
| 216 | + exports.identify_integer(&mut store, AllIntegers::U32(0))?, |
| 217 | + 3 |
| 218 | + ); |
| 219 | + assert_eq!( |
| 220 | + exports.identify_integer(&mut store, AllIntegers::U64(0))?, |
| 221 | + 4 |
| 222 | + ); |
| 223 | + assert_eq!(exports.identify_integer(&mut store, AllIntegers::I8(0))?, 5); |
| 224 | + assert_eq!( |
| 225 | + exports.identify_integer(&mut store, AllIntegers::I16(0))?, |
| 226 | + 6 |
| 227 | + ); |
| 228 | + assert_eq!( |
| 229 | + exports.identify_integer(&mut store, AllIntegers::I32(0))?, |
| 230 | + 7 |
| 231 | + ); |
| 232 | + assert_eq!( |
| 233 | + exports.identify_integer(&mut store, AllIntegers::I64(0))?, |
| 234 | + 8 |
| 235 | + ); |
| 236 | + |
| 237 | + // Identify floats |
| 238 | + assert_eq!(exports.identify_float(&mut store, AllFloats::F32(0.0))?, 0); |
| 239 | + assert_eq!(exports.identify_float(&mut store, AllFloats::F64(0.0))?, 1); |
| 240 | + |
| 241 | + // Identify text |
| 242 | + assert_eq!( |
| 243 | + exports.identify_text(&mut store, AllTextParam::Char('\0'))?, |
| 244 | + 0 |
| 245 | + ); |
| 246 | + assert_eq!( |
| 247 | + exports.identify_text(&mut store, AllTextParam::String(""))?, |
| 248 | + 1 |
| 249 | + ); |
| 250 | + |
| 251 | + // Identify Duplicated |
| 252 | + assert_eq!( |
| 253 | + exports.identify_duplicated(&mut store, DuplicatedS32::I320(0))?, |
| 254 | + 0 |
| 255 | + ); |
| 256 | + assert_eq!( |
| 257 | + exports.identify_duplicated(&mut store, DuplicatedS32::I321(0))?, |
| 258 | + 1 |
| 259 | + ); |
| 260 | + assert_eq!( |
| 261 | + exports.identify_duplicated(&mut store, DuplicatedS32::I322(0))?, |
| 262 | + 2 |
| 263 | + ); |
| 264 | + |
| 265 | + assert!(matches!( |
| 266 | + exports.add_one_duplicated(&mut store, DuplicatedS32::I320(0))?, |
| 267 | + DuplicatedS32::I320(1) |
| 268 | + )); |
| 269 | + assert!(matches!( |
| 270 | + exports.add_one_duplicated(&mut store, DuplicatedS32::I321(0))?, |
| 271 | + DuplicatedS32::I321(1) |
| 272 | + )); |
| 273 | + assert!(matches!( |
| 274 | + exports.add_one_duplicated(&mut store, DuplicatedS32::I322(0))?, |
| 275 | + DuplicatedS32::I322(1) |
| 276 | + )); |
| 277 | + |
| 278 | + // Identify Distinguishable Num |
| 279 | + assert_eq!( |
| 280 | + exports.identify_distinguishable_num(&mut store, DistinguishableNum::F64(0.0))?, |
| 281 | + 0 |
| 282 | + ); |
| 283 | + assert_eq!( |
| 284 | + exports.identify_distinguishable_num(&mut store, DistinguishableNum::I64(0))?, |
| 285 | + 1 |
| 286 | + ); |
| 287 | + |
| 288 | + match exports.add_one_distinguishable_num(&mut store, DistinguishableNum::F64(0.0))? { |
| 289 | + DistinguishableNum::F64(f) => assert_eq!(f, 1.0), |
| 290 | + _ => panic!(), |
| 291 | + }; |
| 292 | + assert!(matches!( |
| 293 | + exports.add_one_distinguishable_num(&mut store, DistinguishableNum::I64(0))?, |
| 294 | + DistinguishableNum::I64(1), |
| 295 | + |
| 296 | + )); |
| 297 | + Ok(()) |
| 298 | +} |
0 commit comments