Use a constant for RocCallResult tag size

This commit is contained in:
Richard Feldman 2021-04-11 09:41:42 -04:00
parent ab7d2d5912
commit f1ddf775cb
2 changed files with 6 additions and 9 deletions

View File

@ -17,8 +17,8 @@ mod repl_eval {
// relies on this size
let value: i64 = 1234;
assert_eq!(
std::mem::size_of_val(RocCallResult::Success(value)),
RocCallResult::size_of_discriminant() + std::mem::size_of_val(value)
std::mem::size_of_val(&RocCallResult::Success(value)),
roc_gen::run_roc::ROC_CALL_RESULT_DISCRIMINANT_SIZE + std::mem::size_of_val(&value)
)
}

View File

@ -2,6 +2,9 @@ use std::ffi::CString;
use std::os::raw::c_char;
use RocCallResult::*;
/// This must have the same size as the repr() of RocCallResult!
pub const ROC_CALL_RESULT_DISCRIMINANT_SIZE: usize = std::mem::size_of::<u64>();
#[repr(u64)]
pub enum RocCallResult<T> {
Success(T),
@ -26,12 +29,6 @@ impl<T: Sized> From<RocCallResult<T>> for Result<T, String> {
}
}
impl<T> RocCallResult<T> {
const fn size_of_discriminant() -> usize {
std::mem::size_of::<u64>()
}
}
#[macro_export]
macro_rules! run_jit_function {
($lib: expr, $main_fn_name: expr, $ty:ty, $transform:expr) => {{
@ -89,7 +86,7 @@ macro_rules! run_jit_function_dynamic_type {
.ok_or(format!("Unable to JIT compile `{}`", $main_fn_name))
.expect("errored");
let size = RocCallResult::size_of_discriminant() + $bytes;
let size = roc_gen::run_roc::ROC_CALL_RESULT_DISCRIMINANT_SIZE + $bytes;
let layout = std::alloc::Layout::array::<u8>(size).unwrap();
let result = std::alloc::alloc(layout);
main(result);