mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-04 04:08:19 +00:00
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:
commit
301bf0f367
8 changed files with 217 additions and 36 deletions
|
@ -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]
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue