fix from wasm32 RocResult implementation

This commit is contained in:
Folkert 2022-07-21 20:58:19 +02:00
parent cd2311003e
commit 9cb2bd04fd
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
4 changed files with 12 additions and 5 deletions

1
Cargo.lock generated
View file

@ -4795,6 +4795,7 @@ dependencies = [
"roc_collections", "roc_collections",
"roc_constrain", "roc_constrain",
"roc_debug_flags", "roc_debug_flags",
"roc_error_macros",
"roc_gen_dev", "roc_gen_dev",
"roc_gen_llvm", "roc_gen_llvm",
"roc_gen_wasm", "roc_gen_wasm",

View file

@ -33,6 +33,7 @@ roc_can = { path = "../can" }
roc_parse = { path = "../parse" } roc_parse = { path = "../parse" }
roc_build = { path = "../build", features = ["target-aarch64", "target-x86_64", "target-wasm32"] } roc_build = { path = "../build", features = ["target-aarch64", "target-x86_64", "target-wasm32"] }
roc_target = { path = "../roc_target" } roc_target = { path = "../roc_target" }
roc_error_macros = { path = "../../error_macros" }
roc_std = { path = "../../roc_std" } roc_std = { path = "../../roc_std" }
roc_debug_flags = {path="../debug_flags"} roc_debug_flags = {path="../debug_flags"}
bumpalo = { version = "3.8.0", features = ["collections"] } bumpalo = { version = "3.8.0", features = ["collections"] }

View file

@ -308,7 +308,8 @@ fn roc_result_after_on_err() {
#[test] #[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))] #[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
fn roc_result_after_err() { fn roc_result_after_err() {
assert_evals_to!(indoc!( assert_evals_to!(
indoc!(
r#" r#"
result : Result Str I64 result : Result Str I64
result = result =
@ -316,7 +317,8 @@ fn roc_result_after_err() {
if num < 0 then Ok "negative!" else Err -num if num < 0 then Ok "negative!" else Err -num
result result
"#), "#
),
RocResult::ok(RocStr::from("already a string")), RocResult::ok(RocStr::from("already a string")),
RocResult<RocStr, i64> RocResult<RocStr, i64>
); );

View file

@ -1,4 +1,5 @@
use roc_gen_wasm::wasm32_sized::Wasm32Sized; use roc_error_macros::internal_error;
use roc_gen_wasm::{round_up_to_alignment, wasm32_sized::Wasm32Sized};
use roc_std::{RocDec, RocList, RocOrder, RocResult, RocStr}; use roc_std::{RocDec, RocList, RocOrder, RocResult, RocStr};
use std::convert::TryInto; use std::convert::TryInto;
@ -99,7 +100,9 @@ where
E: FromWasm32Memory + Wasm32Sized, E: FromWasm32Memory + Wasm32Sized,
{ {
fn decode(memory: &[u8], offset: u32) -> Self { fn decode(memory: &[u8], offset: u32) -> Self {
let tag_offset = Ord::max(T::ACTUAL_WIDTH, E::ACTUAL_WIDTH); let data_align = Ord::max(T::ALIGN_OF_WASM, E::ALIGN_OF_WASM);
let data_width = Ord::max(T::ACTUAL_WIDTH, E::ACTUAL_WIDTH);
let tag_offset = round_up_to_alignment!(data_width, data_align);
let tag = <u8 as FromWasm32Memory>::decode(memory, offset + tag_offset as u32); let tag = <u8 as FromWasm32Memory>::decode(memory, offset + tag_offset as u32);
if tag == 1 { if tag == 1 {
let value = <T as FromWasm32Memory>::decode(memory, offset); let value = <T as FromWasm32Memory>::decode(memory, offset);