Skip to content

Commit ed36ed9

Browse files
committed
fix: Allow bitoperations to be deserialized
The name generated for the function must match the path that they exist atm otherwise they can't be looked up during deserialization. cc @omegablitz
1 parent f2fc254 commit ed36ed9

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

tests/serialization.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ fn roundtrip_lazy() {
207207
roundtrip(&thread, &value);
208208
}
209209

210+
#[test]
211+
fn roundtrip_list() {
212+
let thread = new_vm();
213+
let expr = r#" import! std.list "#;
214+
let (value, _) = thread
215+
.run_expr::<OpaqueValue<&Thread, Hole>>("test", &expr)
216+
.unwrap_or_else(|err| panic!("{}", err));
217+
roundtrip(&thread, &value);
218+
}
219+
210220
#[test]
211221
fn roundtrip_std_thread() {
212222
let thread = new_vm();

vm/src/primitives.rs

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,29 @@ extern "C" fn discriminant_value(thread: &Thread) -> Status {
327327

328328
#[allow(non_camel_case_types)]
329329
mod std {
330+
331+
macro_rules! bit_const_inner {
332+
($typ: ty, $($trait_: ident :: $name: ident,)*) => {
333+
$(
334+
#[allow(non_upper_case_globals)]
335+
pub const $name: fn(l: $typ, r: $typ) -> $typ = ::std::ops::$trait_::$name;
336+
)*
337+
}
338+
}
339+
340+
macro_rules! bit_const {
341+
($typ: ty) => {
342+
bit_const_inner! {
343+
$typ,
344+
BitAnd::bitand,
345+
BitOr::bitor,
346+
BitXor::bitxor,
347+
Shl::shl,
348+
Shr::shr,
349+
}
350+
};
351+
}
352+
330353
pub use crate::primitives as prim;
331354

332355
pub mod string {
@@ -338,11 +361,23 @@ mod std {
338361
pub mod array {
339362
pub use crate::primitives::array as prim;
340363
}
364+
341365
pub mod byte {
342366
pub type prim = u8;
367+
368+
bit_const! { u8 }
343369
}
344370
pub mod int {
345-
pub type prim = crate::types::VmInt;
371+
use crate::types::VmInt;
372+
373+
pub type prim = VmInt;
374+
375+
bit_const! { VmInt }
376+
377+
#[allow(non_upper_case_globals)]
378+
pub const arithmetic_shr: fn(l: VmInt, r: VmInt) -> VmInt = shr;
379+
#[allow(non_upper_case_globals)]
380+
pub const logical_shr: fn(l: u64, r: u64) -> u64 = ::std::ops::Shr::shr;
346381
}
347382
pub mod float {
348383
pub type prim = f64;
@@ -438,11 +473,11 @@ pub fn load_byte(vm: &Thread) -> Result<ExternModule> {
438473
record! {
439474
min_value => std::byte::prim::min_value(),
440475
max_value => std::byte::prim::max_value(),
441-
shl => primitive!(2, <u8 as ::std::ops::Shl<u8>>::shl),
442-
shr => primitive!(2, <u8 as ::std::ops::Shr<u8>>::shr),
443-
bitxor => primitive!(2, <u8 as ::std::ops::BitXor<u8>>::bitxor),
444-
bitand => primitive!(2, <u8 as ::std::ops::BitAnd<u8>>::bitand),
445-
bitor => primitive!(2, <u8 as ::std::ops::BitOr<u8>>::bitor),
476+
shl => primitive!(2, std::byte::shl),
477+
shr => primitive!(2, std::byte::shr),
478+
bitxor => primitive!(2, std::byte::bitxor),
479+
bitand => primitive!(2, std::byte::bitand),
480+
bitor => primitive!(2, std::byte::bitor),
446481
count_ones => primitive!(1, std::byte::prim::count_ones),
447482
count_zeros => primitive!(1, std::byte::prim::count_zeros),
448483
leading_zeros => primitive!(1, std::byte::prim::leading_zeros),
@@ -484,12 +519,12 @@ pub fn load_int(vm: &Thread) -> Result<ExternModule> {
484519
"std.int.prim.from_str_radix",
485520
|src, radix| std::int::prim::from_str_radix(src, radix).map_err(|_| ())
486521
),
487-
shl => primitive!(2, <i64 as ::std::ops::Shl<i64>>::shl),
488-
arithmetic_shr => primitive!(2, <i64 as ::std::ops::Shr<i64>>::shr),
489-
logical_shr => primitive!(2, <u64 as ::std::ops::Shr<u64>>::shr),
490-
bitxor => primitive!(2, <i64 as ::std::ops::BitXor<i64>>::bitxor),
491-
bitand => primitive!(2, <i64 as ::std::ops::BitAnd<i64>>::bitand),
492-
bitor => primitive!(2, <i64 as ::std::ops::BitOr<i64>>::bitor),
522+
shl => primitive!(2, std::int::shl),
523+
arithmetic_shr => primitive!(2, std::int::arithmetic_shr),
524+
logical_shr => primitive!(2, std::int::logical_shr),
525+
bitxor => primitive!(2, std::int::bitxor),
526+
bitand => primitive!(2, std::int::bitand),
527+
bitor => primitive!(2, std::int::bitor),
493528
count_ones => primitive!(1, std::int::prim::count_ones),
494529
count_zeros => primitive!(1, std::int::prim::count_zeros),
495530
leading_zeros => primitive!(1, std::int::prim::leading_zeros),

0 commit comments

Comments
 (0)