Skip to content

Commit

Permalink
Merge pull request #287 from Wodann/feat/type-info-data
Browse files Browse the repository at this point in the history
refactor: make type-specific data (such as StructInfo) part of TypeInfo
  • Loading branch information
Wodann authored Apr 1, 2021
2 parents 1663f99 + 26c8883 commit 034a2b4
Show file tree
Hide file tree
Showing 155 changed files with 1,111 additions and 645 deletions.
2 changes: 1 addition & 1 deletion crates/mun_abi/ffi
Submodule ffi updated 1 files
+65 −57 include/mun_abi.h
20 changes: 10 additions & 10 deletions crates/mun_abi/src/dispatch_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ mod tests {
test_utils::{
fake_dispatch_table, fake_fn_prototype, fake_type_info, FAKE_FN_NAME, FAKE_TYPE_NAME,
},
TypeGroup,
TypeInfoData,
};
use std::{ffi::CString, ptr};

Expand All @@ -130,7 +130,7 @@ mod tests {
#[test]
fn test_dispatch_table_iter_mut_some() {
let type_name = CString::new(FAKE_TYPE_NAME).expect("Invalid fake type name.");
let type_info = fake_type_info(&type_name, TypeGroup::FundamentalTypes, 1, 1);
let type_info = fake_type_info(&type_name, 1, 1, TypeInfoData::Primitive);

let return_type = Some(&type_info);
let fn_name = CString::new(FAKE_FN_NAME).expect("Invalid fake fn name.");
Expand Down Expand Up @@ -163,7 +163,7 @@ mod tests {
#[test]
fn test_dispatch_table_ptrs_mut_some() {
let type_name = CString::new(FAKE_TYPE_NAME).expect("Invalid fake type name.");
let type_info = fake_type_info(&type_name, TypeGroup::FundamentalTypes, 1, 1);
let type_info = fake_type_info(&type_name, 1, 1, TypeInfoData::Primitive);

let return_type = Some(&type_info);
let fn_name = CString::new(FAKE_FN_NAME).expect("Invalid fake fn name.");
Expand Down Expand Up @@ -192,7 +192,7 @@ mod tests {
#[test]
fn test_dispatch_table_signatures_some() {
let type_name = CString::new(FAKE_TYPE_NAME).expect("Invalid fake type name.");
let type_info = fake_type_info(&type_name, TypeGroup::FundamentalTypes, 1, 1);
let type_info = fake_type_info(&type_name, 1, 1, TypeInfoData::Primitive);

let return_type = Some(&type_info);
let fn_name = CString::new(FAKE_FN_NAME).expect("Invalid fake fn name.");
Expand All @@ -214,7 +214,7 @@ mod tests {
#[test]
fn test_dispatch_table_get_ptr_unchecked() {
let type_name = CString::new(FAKE_TYPE_NAME).expect("Invalid fake type name.");
let type_info = fake_type_info(&type_name, TypeGroup::FundamentalTypes, 1, 1);
let type_info = fake_type_info(&type_name, 1, 1, TypeInfoData::Primitive);

let return_type = Some(&type_info);
let fn_name = CString::new(FAKE_FN_NAME).expect("Invalid fake fn name.");
Expand All @@ -230,7 +230,7 @@ mod tests {
#[test]
fn test_dispatch_table_get_ptr_none() {
let type_name = CString::new(FAKE_TYPE_NAME).expect("Invalid fake type name.");
let type_info = fake_type_info(&type_name, TypeGroup::FundamentalTypes, 1, 1);
let type_info = fake_type_info(&type_name, 1, 1, TypeInfoData::Primitive);

let return_type = Some(&type_info);
let fn_name = CString::new(FAKE_FN_NAME).expect("Invalid fake fn name.");
Expand All @@ -246,7 +246,7 @@ mod tests {
#[test]
fn test_dispatch_table_get_ptr_some() {
let type_name = CString::new(FAKE_TYPE_NAME).expect("Invalid fake type name.");
let type_info = fake_type_info(&type_name, TypeGroup::FundamentalTypes, 1, 1);
let type_info = fake_type_info(&type_name, 1, 1, TypeInfoData::Primitive);

let return_type = Some(&type_info);
let fn_name = CString::new(FAKE_FN_NAME).expect("Invalid fake fn name.");
Expand All @@ -262,7 +262,7 @@ mod tests {
#[test]
fn test_dispatch_table_get_ptr_unchecked_mut() {
let type_name = CString::new(FAKE_TYPE_NAME).expect("Invalid fake type name.");
let type_info = fake_type_info(&type_name, TypeGroup::FundamentalTypes, 1, 1);
let type_info = fake_type_info(&type_name, 1, 1, TypeInfoData::Primitive);

let return_type = Some(&type_info);
let fn_name = CString::new(FAKE_FN_NAME).expect("Invalid fake fn name.");
Expand All @@ -281,7 +281,7 @@ mod tests {
#[test]
fn test_dispatch_table_get_ptr_mut_none() {
let type_name = CString::new(FAKE_TYPE_NAME).expect("Invalid fake type name.");
let type_info = fake_type_info(&type_name, TypeGroup::FundamentalTypes, 1, 1);
let type_info = fake_type_info(&type_name, 1, 1, TypeInfoData::Primitive);

let return_type = Some(&type_info);
let fn_name = CString::new(FAKE_FN_NAME).expect("Invalid fake fn name.");
Expand All @@ -297,7 +297,7 @@ mod tests {
#[test]
fn test_dispatch_table_get_ptr_mut_some() {
let type_name = CString::new(FAKE_TYPE_NAME).expect("Invalid fake type name.");
let type_info = fake_type_info(&type_name, TypeGroup::FundamentalTypes, 1, 1);
let type_info = fake_type_info(&type_name, 1, 1, TypeInfoData::Primitive);

let return_type = Some(&type_info);
let fn_name = CString::new(FAKE_FN_NAME).expect("Invalid fake fn name.");
Expand Down
6 changes: 3 additions & 3 deletions crates/mun_abi/src/function_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ mod tests {
test_utils::{
fake_fn_prototype, fake_fn_signature, fake_type_info, FAKE_FN_NAME, FAKE_TYPE_NAME,
},
TypeGroup,
TypeInfoData,
};
use std::ffi::CString;

Expand All @@ -256,7 +256,7 @@ mod tests {
#[test]
fn test_fn_signature_arg_types_some() {
let type_name = CString::new(FAKE_TYPE_NAME).expect("Invalid fake type name.");
let type_info = fake_type_info(&type_name, TypeGroup::FundamentalTypes, 1, 1);
let type_info = fake_type_info(&type_name, 1, 1, TypeInfoData::Primitive);

let arg_types = &[&type_info];
let fn_signature = fake_fn_signature(arg_types, None);
Expand All @@ -275,7 +275,7 @@ mod tests {
#[test]
fn test_fn_signature_return_type_some() {
let type_name = CString::new(FAKE_TYPE_NAME).expect("Invalid fake type name.");
let type_info = fake_type_info(&type_name, TypeGroup::FundamentalTypes, 1, 1);
let type_info = fake_type_info(&type_name, 1, 1, TypeInfoData::Primitive);

let return_type = Some(&type_info);
let fn_signature = fake_fn_signature(&[], return_type);
Expand Down
4 changes: 2 additions & 2 deletions crates/mun_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ pub use function_info::{
};
pub use module_info::ModuleInfo;
pub use struct_info::{StructInfo, StructMemoryKind};
pub use type_info::{HasStaticTypeInfo, TypeGroup, TypeInfo};
pub use type_info::{HasStaticTypeInfo, TypeInfo, TypeInfoData};

/// The Mun ABI prelude
///
/// The *prelude* contains imports that are used almost every time.
pub mod prelude {
pub use crate::{HasStaticTypeInfo, IntoFunctionDefinition, StructMemoryKind, TypeGroup};
pub use crate::{HasStaticTypeInfo, IntoFunctionDefinition, StructMemoryKind};
}

/// Defines the current ABI version
Expand Down
27 changes: 15 additions & 12 deletions crates/mun_abi/src/module_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ unsafe impl Sync for ModuleInfo {}
mod tests {
use crate::{
test_utils::{
fake_fn_prototype, fake_module_info, fake_struct_info, fake_struct_type_info,
fake_type_info, FAKE_FN_NAME, FAKE_MODULE_PATH, FAKE_STRUCT_NAME, FAKE_TYPE_NAME,
fake_fn_prototype, fake_module_info, fake_struct_info, fake_type_info, FAKE_FN_NAME,
FAKE_MODULE_PATH, FAKE_STRUCT_NAME, FAKE_TYPE_NAME,
},
FunctionDefinition, TypeGroup, TypeInfo,
FunctionDefinition, TypeInfo, TypeInfoData,
};
use std::{ffi::CString, mem, ptr};
use std::{ffi::CString, ptr};

#[test]
fn test_module_info_path() {
Expand All @@ -94,7 +94,7 @@ mod tests {
#[test]
fn test_module_info_types_some() {
let type_name = CString::new(FAKE_TYPE_NAME).expect("Invalid fake type name.");
let type_info = fake_type_info(&type_name, TypeGroup::FundamentalTypes, 1, 1);
let type_info = fake_type_info(&type_name, 1, 1, TypeInfoData::Primitive);

let return_type = Some(&type_info);
let fn_name = CString::new(FAKE_FN_NAME).expect("Invalid fake fn name.");
Expand All @@ -108,8 +108,8 @@ mod tests {

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

let module_path = CString::new(FAKE_MODULE_PATH).expect("Invalid fake module path.");
let module = fake_module_info(&module_path, functions, types);
Expand All @@ -134,11 +134,14 @@ mod tests {
for (lhs, rhs) in result_types.iter().zip(types.iter()) {
assert_eq!(lhs, rhs);
assert_eq!(lhs.name(), rhs.name());
assert_eq!(lhs.group, rhs.group);
if lhs.group == TypeGroup::StructTypes {
let lhs_struct = lhs.as_struct().unwrap();
let rhs_struct = rhs.as_struct().unwrap();
assert_eq!(lhs_struct.field_types(), rhs_struct.field_types());
assert_eq!(lhs.data.is_struct(), rhs.data.is_struct());
assert_eq!(lhs.data.is_primitive(), rhs.data.is_primitive());
if let TypeInfoData::Struct(lhs) = &lhs.data {
if let TypeInfoData::Struct(rhs) = &rhs.data {
assert_eq!(lhs.field_types(), rhs.field_types());
} else {
assert!(false);
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/mun_abi/src/struct_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{ffi::CStr, os::raw::c_char, slice, str};

/// Represents a struct declaration.
#[repr(C)]
#[derive(Debug)]
pub struct StructInfo {
/// Struct fields' names
pub field_names: *const *const c_char,
Expand Down Expand Up @@ -116,7 +117,7 @@ mod tests {
use super::StructMemoryKind;
use crate::{
test_utils::{fake_struct_info, fake_type_info, FAKE_FIELD_NAME, FAKE_TYPE_NAME},
TypeGroup,
TypeInfoData,
};
use std::ffi::CString;

Expand All @@ -137,7 +138,7 @@ mod tests {
fn test_struct_info_fields_some() {
let field_name = CString::new(FAKE_FIELD_NAME).expect("Invalid fake field name.");
let type_name = CString::new(FAKE_TYPE_NAME).expect("Invalid fake type name.");
let type_info = fake_type_info(&type_name, TypeGroup::FundamentalTypes, 1, 1);
let type_info = fake_type_info(&type_name, 1, 1, TypeInfoData::Primitive);

let field_names = &[field_name.as_ptr()];
let field_types = &[&type_info];
Expand Down
23 changes: 5 additions & 18 deletions crates/mun_abi/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
AssemblyInfo, DispatchTable, FunctionDefinition, FunctionPrototype, FunctionSignature, Guid,
ModuleInfo, StructInfo, StructMemoryKind, TypeGroup, TypeInfo,
ModuleInfo, StructInfo, StructMemoryKind, TypeInfo, TypeInfoData,
};
use std::{
ffi::{c_void, CStr},
Expand All @@ -17,12 +17,6 @@ pub(crate) const FAKE_MODULE_PATH: &str = "path::to::module";
pub(crate) const FAKE_STRUCT_NAME: &str = "StructName";
pub(crate) const FAKE_TYPE_NAME: &str = "TypeName";

/// A dummy struct for initializing a struct's `TypeInfo`
pub(crate) struct StructTypeInfo {
_type_info: TypeInfo,
_struct_info: StructInfo,
}

pub(crate) fn fake_assembly_info(
symbols: ModuleInfo,
dispatch_table: DispatchTable,
Expand Down Expand Up @@ -103,24 +97,17 @@ pub(crate) fn fake_struct_info(
}
}

pub(crate) fn fake_struct_type_info(
pub(crate) fn fake_type_info(
name: &CStr,
struct_info: StructInfo,
size: u32,
alignment: u8,
) -> StructTypeInfo {
StructTypeInfo {
_type_info: fake_type_info(name, TypeGroup::StructTypes, size, alignment),
_struct_info: struct_info,
}
}

pub(crate) fn fake_type_info(name: &CStr, group: TypeGroup, size: u32, alignment: u8) -> TypeInfo {
data: TypeInfoData,
) -> TypeInfo {
TypeInfo {
guid: FAKE_TYPE_GUID,
name: name.as_ptr(),
size_in_bits: size,
alignment,
group,
data,
}
}
Loading

0 comments on commit 034a2b4

Please sign in to comment.