Skip to content

Commit

Permalink
refactor(runtime): add Runtime as argument for ArgumentReflection fun…
Browse files Browse the repository at this point in the history
…ctions
  • Loading branch information
Wodann committed Apr 5, 2020
1 parent 71d80c9 commit 28e2057
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 40 deletions.
6 changes: 3 additions & 3 deletions crates/mun_runtime/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ macro_rules! invoke_fn_impl {
function_name: &'s str,
$($Arg: $T,)*
) -> core::result::Result<Output, $ErrName<'s, $($T,)* Output>> {
match runtime
.borrow()
let runtime_ref = runtime.borrow();
match runtime_ref
.get_function_info(function_name)
.ok_or_else(|| format!("Failed to obtain function '{}'", function_name))
.and_then(|function_info| {
Expand All @@ -115,7 +115,7 @@ macro_rules! invoke_fn_impl {
#[allow(unused_mut, unused_variables)]
let mut idx = 0;
$(
crate::reflection::equals_argument_type(&arg_types[idx], &$Arg)
crate::reflection::equals_argument_type(&runtime_ref, &arg_types[idx], &$Arg)
.map_err(|(expected, found)| {
format!(
"Invalid argument type at index {}. Expected: {}. Found: {}.",
Expand Down
49 changes: 17 additions & 32 deletions crates/mun_runtime/src/reflection.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::type_info::HasStaticTypeInfo;
use crate::{marshal::Marshal, StructRef};
use crate::{marshal::Marshal, Runtime, StructRef};
use md5;

/// Returns whether the specified argument type matches the `type_info`.
pub fn equals_argument_type<'e, 'f, T: ArgumentReflection>(
pub fn equals_argument_type<'r, 'e, 'f, T: ArgumentReflection>(
runtime: &'r Runtime,
type_info: &'e abi::TypeInfo,
arg: &'f T,
) -> Result<(), (&'e str, &'f str)> {
if type_info.guid != arg.type_guid() {
Err((type_info.name(), arg.type_name()))
if type_info.guid != arg.type_guid(runtime) {
Err((type_info.name(), arg.type_name(runtime)))
} else {
Ok(())
}
Expand Down Expand Up @@ -55,14 +56,10 @@ pub trait ArgumentReflection: Sized {
type Marshalled: Marshal<Self>;

/// Retrieves the `Guid` of the value's type.
fn type_guid(&self) -> abi::Guid {
abi::Guid {
b: md5::compute(self.type_name()).0,
}
}
fn type_guid(&self, runtime: &Runtime) -> abi::Guid;

/// Retrieves the name of the value's type.
fn type_name(&self) -> &str;
fn type_name(&self, runtime: &Runtime) -> &str;

/// Marshals the value.
fn marshal(self) -> Self::Marshalled;
Expand All @@ -74,11 +71,11 @@ macro_rules! impl_primitive_type {
impl ArgumentReflection for $ty {
type Marshalled = Self;

fn type_guid(&self) -> abi::Guid {
fn type_guid(&self, _runtime: &Runtime) -> abi::Guid {
Self::type_info().guid
}

fn type_name(&self) -> &str {
fn type_name(&self, _runtime: &Runtime) -> &str {
Self::type_info().name()
}

Expand All @@ -90,32 +87,20 @@ macro_rules! impl_primitive_type {
impl ReturnTypeReflection for $ty {
type Marshalled = Self;

fn type_name() -> &'static str {
Self::type_info().name()
}

fn type_guid() -> abi::Guid {
Self::type_info().guid
}

fn type_name() -> &'static str {
Self::type_info().name()
}
}
)+
}
}

impl_primitive_type!(i8, i16, i32, i64, isize, u8, u16, u32, u64, usize, f32, f64, bool);

impl ArgumentReflection for () {
type Marshalled = Self;

fn type_name(&self) -> &str {
<Self as ReturnTypeReflection>::type_name()
}

fn marshal(self) -> Self::Marshalled {
self
}
}

impl ReturnTypeReflection for () {
type Marshalled = ();

Expand All @@ -130,11 +115,11 @@ where
{
type Marshalled = Self;

fn type_guid(&self) -> abi::Guid {
fn type_guid(&self, _runtime: &Runtime) -> abi::Guid {
Self::type_info().guid
}

fn type_name(&self) -> &str {
fn type_name(&self, _runtime: &Runtime) -> &str {
Self::type_info().name()
}

Expand Down Expand Up @@ -164,11 +149,11 @@ where
{
type Marshalled = Self;

fn type_guid(&self) -> abi::Guid {
fn type_guid(&self, _runtime: &Runtime) -> abi::Guid {
Self::type_info().guid
}

fn type_name(&self) -> &str {
fn type_name(&self, _runtime: &Runtime) -> &str {
Self::type_info().name()
}

Expand Down
15 changes: 10 additions & 5 deletions crates/mun_runtime/src/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl StructRef {
// Safety: If we found the `field_idx`, we are guaranteed to also have the `field_type` and
// `field_offset`.
let field_type = unsafe { struct_info.field_types().get_unchecked(field_idx) };
equals_argument_type(field_type, &value).map_err(|(expected, found)| {
equals_argument_type(&runtime_ref, field_type, &value).map_err(|(expected, found)| {
format!(
"Mismatched types for `{}::{}`. Expected: `{}`. Found: `{}`.",
type_info.name(),
Expand Down Expand Up @@ -155,7 +155,7 @@ impl StructRef {
// Safety: If we found the `field_idx`, we are guaranteed to also have the `field_type` and
// `field_offset`.
let field_type = unsafe { struct_info.field_types().get_unchecked(field_idx) };
equals_argument_type(field_type, &value).map_err(|(expected, found)| {
equals_argument_type(&runtime_ref, field_type, &value).map_err(|(expected, found)| {
format!(
"Mismatched types for `{}::{}`. Expected: `{}`. Found: `{}`.",
type_info.name(),
Expand All @@ -175,9 +175,14 @@ impl StructRef {
impl ArgumentReflection for StructRef {
type Marshalled = RawStruct;

fn type_name(&self) -> &str {
let runtime_ref = self.runtime.borrow();
let type_info = unsafe { &*runtime_ref.gc().ptr_type(self.handle.handle()).inner() };
fn type_guid(&self, runtime: &Runtime) -> abi::Guid {
abi::Guid {
b: md5::compute(self.type_name(runtime)).0,
}
}

fn type_name(&self, runtime: &Runtime) -> &str {
let type_info = unsafe { &*runtime.gc().ptr_type(self.handle.handle()).inner() };
type_info.name()
}

Expand Down

0 comments on commit 28e2057

Please sign in to comment.