Merge pull request #5009 from roc-lang/dev-box-box

implement Box.box and Box.unbox in the dev backend
This commit is contained in:
Folkert de Vries 2023-02-09 18:04:53 +01:00 committed by GitHub
commit 301bf0f367
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 217 additions and 36 deletions

View file

@ -9,9 +9,7 @@ use crate::helpers::wasm::assert_evals_to;
use indoc::indoc;
#[allow(unused_imports)]
use roc_std::RocList;
#[allow(unused_imports)]
use roc_std::RocStr;
use roc_std::{RocBox, RocList, RocStr};
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-dev", feature = "gen-wasm"))]
@ -3275,17 +3273,15 @@ fn box_and_unbox_string() {
}
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))]
fn box_num() {
assert_evals_to!("Box.box 123u64", RocBox::new(123), RocBox<u64>)
}
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))]
fn box_and_unbox_num() {
assert_evals_to!(
indoc!(
r#"
Box.unbox (Box.box (123u8))
"#
),
123,
u8
)
assert_evals_to!("Box.unbox (Box.box (123u64))", 123, u64)
}
#[test]

View file

@ -1,7 +1,7 @@
use roc_error_macros::internal_error;
use roc_gen_wasm::wasm32_sized::Wasm32Sized;
use roc_mono::layout::Builtin;
use roc_std::{RocDec, RocList, RocOrder, RocResult, RocStr, I128, U128};
use roc_std::{RocBox, RocDec, RocList, RocOrder, RocResult, RocStr, I128, U128};
use roc_wasm_module::round_up_to_alignment;
use std::convert::TryInto;
@ -104,6 +104,17 @@ impl<T: FromWasm32Memory + Clone> FromWasm32Memory for RocList<T> {
}
}
impl<T: FromWasm32Memory + Clone> FromWasm32Memory for RocBox<T> {
fn decode(memory: &[u8], offset: u32) -> Self {
let ptr = <u32 as FromWasm32Memory>::decode(memory, offset + 4 * Builtin::WRAPPER_PTR);
debug_assert_ne!(ptr, 0);
let value = <T as FromWasm32Memory>::decode(memory, ptr);
RocBox::new(value)
}
}
impl<T, E> FromWasm32Memory for RocResult<T, E>
where
T: FromWasm32Memory + Wasm32Sized,