diff --git a/compiler/gen_wasm/src/backend.rs b/compiler/gen_wasm/src/backend.rs index 3b4da4a465..e4f3e200b2 100644 --- a/compiler/gen_wasm/src/backend.rs +++ b/compiler/gen_wasm/src/backend.rs @@ -303,7 +303,7 @@ impl<'a> WasmBackend<'a> { Stmt::Switch { cond_symbol, - cond_layout: _, + cond_layout, branches, default_branch, ret_layout: _, @@ -326,16 +326,32 @@ impl<'a> WasmBackend<'a> { self.start_block(BlockType::NoResult) } + let cond_type = WasmLayout::new(cond_layout).value_type(); + // then, we jump whenever the value under scrutiny is equal to the value of a branch for (i, (value, _, _)) in branches.iter().enumerate() { // put the cond_symbol on the top of the stack self.storage .load_symbols(&mut self.code_builder, &[*cond_symbol]); - self.code_builder.i32_const(*value as i32); - - // compare the 2 topmost values - self.code_builder.i32_eq(); + match cond_type { + ValueType::I32 => { + self.code_builder.i32_const(*value as i32); + self.code_builder.i32_eq(); + } + ValueType::I64 => { + self.code_builder.i64_const(*value as i64); + self.code_builder.i64_eq(); + } + ValueType::F32 => { + self.code_builder.f32_const(f32::from_bits(*value as u32)); + self.code_builder.f32_eq(); + } + ValueType::F64 => { + self.code_builder.f64_const(f64::from_bits(*value as u64)); + self.code_builder.f64_eq(); + } + } // "break" out of `i` surrounding blocks self.code_builder.br_if(i as u32); diff --git a/compiler/test_gen/src/gen_primitives.rs b/compiler/test_gen/src/gen_primitives.rs index 2ee5823a8a..de4c4f813d 100644 --- a/compiler/test_gen/src/gen_primitives.rs +++ b/compiler/test_gen/src/gen_primitives.rs @@ -221,7 +221,7 @@ fn gen_when_one_branch() { } #[test] -#[cfg(any(feature = "gen-llvm"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))] fn gen_large_when_int() { assert_evals_to!( indoc!( @@ -243,31 +243,31 @@ fn gen_large_when_int() { ); } -// #[test] -// #[cfg(any(feature = "gen-llvm"))] -// fn gen_large_when_float() { -// assert_evals_to!( -// indoc!( -// r#" -// foo = \num -> -// when num is -// 0.5 -> 200.1 -// -3.6 -> 111.2 # TODO adding more negative numbers reproduces parsing bugs here -// 3.6 -> 789.5 -// 1.7 -> 123.3 -// 2.8 -> 456.4 -// _ -> 1000.6 +#[test] +#[cfg(any(feature = "gen-wasm"))] +fn gen_large_when_float() { + assert_evals_to!( + indoc!( + r#" + foo = \num -> + when num is + 0.5 -> 200.1 + -3.6 -> 111.2 # TODO adding more negative numbers reproduces parsing bugs here + 3.6 -> 789.5 + 1.7 -> 123.3 + 2.8 -> 456.4 + _ -> 1000.6 -// foo -3.6 -// "# -// ), -// 111.2, -// f64 -// ); -// } + foo -3.6 + "# + ), + 111.2, + f64 + ); +} #[test] -#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-dev", feature = "gen-wasm"))] fn or_pattern() { assert_evals_to!( indoc!( @@ -337,7 +337,7 @@ fn return_unnamed_fn() { } #[test] -#[cfg(any(feature = "gen-llvm"))] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))] fn gen_when_fn() { assert_evals_to!( indoc!(