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

View file

@ -308,15 +308,17 @@ fn roc_result_after_on_err() {
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
fn roc_result_after_err() {
assert_evals_to!(indoc!(
r#"
assert_evals_to!(
indoc!(
r#"
result : Result Str I64
result =
Result.onErr (Ok "already a string") \num ->
if num < 0 then Ok "negative!" else Err -num
result
"#),
"#
),
RocResult::ok(RocStr::from("already a string")),
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 std::convert::TryInto;
@ -99,7 +100,9 @@ where
E: FromWasm32Memory + Wasm32Sized,
{
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);
if tag == 1 {
let value = <T as FromWasm32Memory>::decode(memory, offset);