Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: object ptr indirection #97

Merged
merged 7 commits into from
Mar 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/mun_abi/c
Submodule c updated 1 files
+3 −1 include/mun_abi.h
18 changes: 15 additions & 3 deletions crates/mun_abi/src/autogen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use crate::{Privacy, StructMemoryKind, TypeGroup};
#[doc = ""]
#[doc = " GUIDs are generated by taking the MD5 hash of a type's name."]
#[doc = ""]
#[doc = " <div rustbindgen derive=\"Clone\" derive=\"Copy\" derive=\"Debug\" derive=\"Eq\" derive=\"PartialEq\"></div>"]
#[doc = " <div rustbindgen derive=\"Clone\" derive=\"Copy\" derive=\"Debug\" derive=\"Eq\" derive=\"PartialEq\" derive=\"Ord\" derive=\"PartialOrd\"></div>"]
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Guid {
#[doc = " 16-byte MD5 hash"]
pub b: [u8; 16usize],
Expand Down Expand Up @@ -236,6 +236,8 @@ pub struct StructInfo {
pub field_sizes: *const u16,
#[doc = " Number of fields"]
pub num_fields: u16,
#[doc = " Struct memory alignment"]
pub alignment: u16,
#[doc = " Struct memory kind"]
pub memory_kind: StructMemoryKind,
}
Expand Down Expand Up @@ -312,8 +314,18 @@ fn bindgen_test_layout_StructInfo() {
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<StructInfo>())).memory_kind as *const _ as usize },
unsafe { &(*(::std::ptr::null::<StructInfo>())).alignment as *const _ as usize },
42usize,
concat!(
"Offset of field: ",
stringify!(StructInfo),
"::",
stringify!(alignment)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<StructInfo>())).memory_kind as *const _ as usize },
44usize,
concat!(
"Offset of field: ",
stringify!(StructInfo),
Expand Down
84 changes: 66 additions & 18 deletions crates/mun_abi/src/autogen_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ impl StructInfo {
)
})
}

/// Returns the size of the struct
pub fn size(&self) -> usize {
baszalmstra marked this conversation as resolved.
Show resolved Hide resolved
(self.field_offsets().last().cloned().unwrap_or(0)
+ self.field_sizes().last().cloned().unwrap_or(0))
.into()
}
}

impl fmt::Display for StructInfo {
Expand Down Expand Up @@ -468,6 +475,7 @@ mod tests {
field_types: &[&TypeInfo],
field_offsets: &[u16],
field_sizes: &[u16],
alignment: u16,
memory_kind: StructMemoryKind,
) -> StructInfo {
assert!(field_names.len() == field_types.len());
Expand All @@ -481,6 +489,7 @@ mod tests {
field_offsets: field_offsets.as_ptr(),
field_sizes: field_sizes.as_ptr(),
num_fields: field_names.len() as u16,
alignment,
memory_kind,
}
}
Expand All @@ -490,7 +499,7 @@ mod tests {
#[test]
fn test_struct_info_name() {
let struct_name = CString::new(FAKE_STRUCT_NAME).expect("Invalid fake struct name.");
let struct_info = fake_struct_info(&struct_name, &[], &[], &[], &[], Default::default());
let struct_info = fake_struct_info(&struct_name, &[], &[], &[], &[], 1, Default::default());

assert_eq!(struct_info.name(), FAKE_STRUCT_NAME);
}
Expand All @@ -508,13 +517,15 @@ mod tests {
field_types,
field_offsets,
field_sizes,
1,
Default::default(),
);

assert_eq!(struct_info.field_names().count(), 0);
assert_eq!(struct_info.field_types(), field_types);
assert_eq!(struct_info.field_offsets(), field_offsets);
assert_eq!(struct_info.field_sizes(), field_sizes);
assert_eq!(struct_info.size(), 0);
}

#[test]
Expand All @@ -527,13 +538,15 @@ mod tests {
let field_types = &[&type_info];
let field_offsets = &[1];
let field_sizes = &[2];
let alignment = 1;
let struct_name = CString::new(FAKE_STRUCT_NAME).expect("Invalid fake struct name.");
let struct_info = fake_struct_info(
&struct_name,
field_names,
field_types,
field_offsets,
field_sizes,
alignment,
Default::default(),
);

Expand All @@ -543,28 +556,42 @@ mod tests {
assert_eq!(struct_info.field_types(), field_types);
assert_eq!(struct_info.field_offsets(), field_offsets);
assert_eq!(struct_info.field_sizes(), field_sizes);
assert_eq!(
struct_info.size() as u16,
field_offsets.last().unwrap() + field_sizes.last().unwrap()
)
}

fn fake_module_info(
path: &CStr,
functions: &[FunctionInfo],
types: &[&TypeInfo],
) -> ModuleInfo {
ModuleInfo {
path: path.as_ptr(),
functions: functions.as_ptr(),
num_functions: functions.len() as u32,
types: types.as_ptr().cast::<*const TypeInfo>(),
num_types: types.len() as u32,
}
#[test]
fn test_struct_info_alignment() {
let struct_name = CString::new(FAKE_STRUCT_NAME).expect("Invalid fake struct name.");
let struct_alignment = 4;
let struct_info = fake_struct_info(
&struct_name,
&[],
&[],
&[],
&[],
struct_alignment,
Default::default(),
);

assert_eq!(struct_info.alignment, struct_alignment);
}

#[test]
fn test_struct_info_memory_kind_gc() {
let struct_name = CString::new(FAKE_STRUCT_NAME).expect("Invalid fake struct name.");
let struct_memory_kind = StructMemoryKind::GC;
let struct_info =
fake_struct_info(&struct_name, &[], &[], &[], &[], struct_memory_kind.clone());
let struct_info = fake_struct_info(
&struct_name,
&[],
&[],
&[],
&[],
1,
struct_memory_kind.clone(),
);

assert_eq!(struct_info.memory_kind, struct_memory_kind);
}
Expand All @@ -573,12 +600,33 @@ mod tests {
fn test_struct_info_memory_kind_value() {
let struct_name = CString::new(FAKE_STRUCT_NAME).expect("Invalid fake struct name.");
let struct_memory_kind = StructMemoryKind::Value;
let struct_info =
fake_struct_info(&struct_name, &[], &[], &[], &[], struct_memory_kind.clone());
let struct_info = fake_struct_info(
&struct_name,
&[],
&[],
&[],
&[],
1,
struct_memory_kind.clone(),
);

assert_eq!(struct_info.memory_kind, struct_memory_kind);
}

fn fake_module_info(
path: &CStr,
functions: &[FunctionInfo],
types: &[&TypeInfo],
) -> ModuleInfo {
ModuleInfo {
path: path.as_ptr(),
functions: functions.as_ptr(),
num_functions: functions.len() as u32,
types: types.as_ptr().cast::<*const TypeInfo>(),
num_types: types.len() as u32,
}
}

const FAKE_MODULE_PATH: &str = "path::to::module";

#[test]
Expand Down Expand Up @@ -616,7 +664,7 @@ mod tests {
let functions = &[fn_info];

let struct_name = CString::new(FAKE_STRUCT_NAME).expect("Invalid fake struct name");
let struct_info = fake_struct_info(&struct_name, &[], &[], &[], &[], Default::default());
let struct_info = fake_struct_info(&struct_name, &[], &[], &[], &[], 1, Default::default());
let struct_type_info = fake_struct_type_info(&struct_name, struct_info);
let types = &[unsafe { mem::transmute(&struct_type_info) }];

Expand Down
Loading