Skip to content

Commit

Permalink
feat(runtime_capi): add function for retrieving MunStructInfo from a …
Browse files Browse the repository at this point in the history
…MunTypeInfo pointer
  • Loading branch information
Wodann committed Jan 26, 2020
1 parent a41ff8c commit 4a1c4e0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/mun_runtime_capi/ffi
Submodule ffi updated 1 files
+46 −0 include/mun/runtime_capi.h
46 changes: 46 additions & 0 deletions crates/mun_runtime_capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,49 @@ pub unsafe extern "C" fn mun_runtime_update(
*updated = runtime.update();
ErrorHandle::default()
}

/// Retrieves the [`StructInfo`] corresponding to `type_info`, if the type is a struct. If
/// successful, `struct_info` is set, otherwise a non-zero error handle is returned.
///
/// If a non-zero error handle is returned, it must be manually destructed using
/// [`mun_error_destroy`].
///
/// # Safety
///
/// This function receives raw pointers as parameters. If any of the arguments is a null pointer,
/// an error will be returned. Passing pointers to invalid data, will lead to undefined behavior.
#[no_mangle]
pub unsafe extern "C" fn mun_type_info_as_struct(
type_info: *const TypeInfo,
struct_info: *mut StructInfo,
) -> ErrorHandle {
let type_info = match type_info.as_ref() {
Some(info) => info,
None => {
return HUB.errors.register(Box::new(err_msg(
"Invalid argument: `type_info` is null pointer",
)))
}
};

let struct_info = match struct_info.as_mut() {
Some(info) => info,
None => {
return HUB.errors.register(Box::new(err_msg(
"Invalid argument: `struct_info` is null pointer",
)))
}
};

match type_info.as_struct() {
Some(info) => *struct_info = info.clone(),
None => {
return HUB.errors.register(Box::new(err_msg(format!(
"Provided type `{}` is not a struct.",
type_info.name()
))))
}
}

ErrorHandle::default()
}

0 comments on commit 4a1c4e0

Please sign in to comment.