Skip to content

Commit

Permalink
Improve rs_port stage 5
Browse files Browse the repository at this point in the history
  • Loading branch information
devraymondsh committed Apr 20, 2023
1 parent 73b546f commit 66c9cc1
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 258 deletions.
7 changes: 5 additions & 2 deletions source/ports/rs_port/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ repository = "https://github.com/metacall/core/tree/develop/source/ports/rs_port
version = "0.4.0"

[lib]
name = "metacall"
crate-type = ["lib"]
path = "src/lib.rs"
# Code examples used in documentation are environment dependent. Meaning they need a set of
# stuff to be done before executing the codes. Therefore we should disable doctests.
doctest = false
edition = "2021"
name = "metacall"
path = "src/lib.rs"

[dependencies]
metacall-inline = { path = "./inline", version = "0.2.0" }
Expand Down
53 changes: 0 additions & 53 deletions source/ports/rs_port/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,61 +35,8 @@ pub(crate) mod private_macros {
}};
}

macro_rules! match_metacall_value_all {
($any:expr, $var:ident, $action:expr, [ $($type: ty),* ]) => {{
use std::{collections::HashMap, vec::Vec};

match_metacall_value!($any, {
$( $var: $type => $action, )*

// Support up to 5 dimensional vectors for type casting
$( $var: Vec<$type> => $action, )*
$( $var: Vec<Vec<$type>> => $action, )*
$( $var: Vec<Vec<Vec<$type>>> => $action, )*
$( $var: Vec<Vec<Vec<Vec<$type>>>> => $action, )*
$( $var: Vec<Vec<Vec<Vec<Vec<$type>>>>> => $action, )*
$( $var: Vec<Vec<Vec<Vec<Vec<Vec<$type>>>>>> => $action, )*

// Support up to 5 dimensional hashmaps for type casting
$( $var: HashMap::<String, $type> => $action, )*
$( $var: HashMap::<String, HashMap::<String, $type>> => $action, )*
$( $var: HashMap::<String, HashMap::<String, HashMap::<String, $type>>> => $action, )*
$(
$var: HashMap::<String, HashMap::<
String, HashMap::<
String, HashMap::<String, $type>>
>
> => $action,
)*
$(
$var: HashMap::<String, HashMap::<
String, HashMap::<
String, HashMap::<
String, HashMap::<String, $type>>
>
>
> => $action,
)*
$(
$var: HashMap::<String, HashMap::<
String, HashMap::<
String, HashMap::<
String, HashMap::<
String, HashMap::<String, $type>>
>
>
>
> => $action,
)*

_ => panic!("Unkown type: {:#?}", $any)
})
}};
}

pub(crate) use cstring;
pub(crate) use cstring_enum;
pub(crate) use match_metacall_value_all;
}

#[macro_export]
Expand Down
14 changes: 5 additions & 9 deletions source/ports/rs_port/src/parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ pub fn raw_to_metacallobj_untyped(ret: *mut c_void) -> Box<dyn MetacallValue> {
(_, 6) => metacallobj_result_wrap(f64::from_metacall_raw(ret)),
(_, 7) => metacallobj_result_wrap(String::from_metacall_raw(ret)),
(_, 8) => metacallobj_result_wrap(<Vec<i8>>::from_metacall_raw(ret)),
(_, 9) => metacallobj_result_wrap(<Vec<Box<dyn MetacallValue>>>::from_metacall_raw(ret)),
(_, 10) => metacallobj_result_wrap(
<HashMap<String, Box<dyn MetacallValue>>>::from_metacall_raw(ret),
),
(_, 9) => metacallobj_result_wrap(<Vec<MetacallNull>>::from_metacall_raw(ret)),
(_, 10) => metacallobj_result_wrap(<HashMap<String, MetacallNull>>::from_metacall_raw(ret)),
(_, 11) => metacallobj_result_wrap(<MetacallPointer>::from_metacall_raw(ret)),
(_, 12) => metacallobj_result_wrap(MetacallFuture::from_metacall_raw(ret)),
(_, 13) => metacallobj_result_wrap(MetacallFunction::from_metacall_raw(ret)),
Expand All @@ -92,12 +90,10 @@ pub fn raw_to_metacallobj_untyped_leak(ret: *mut c_void) -> Box<dyn MetacallValu
(_, 6) => metacallobj_result_wrap(f64::from_metacall_raw_leak(ret)),
(_, 7) => metacallobj_result_wrap(String::from_metacall_raw_leak(ret)),
(_, 8) => metacallobj_result_wrap(<Vec<i8>>::from_metacall_raw_leak(ret)),
(_, 9) => {
metacallobj_result_wrap(<Vec<Box<dyn MetacallValue>>>::from_metacall_raw_leak(ret))
(_, 9) => metacallobj_result_wrap(<Vec<MetacallNull>>::from_metacall_raw_leak(ret)),
(_, 10) => {
metacallobj_result_wrap(<HashMap<String, MetacallNull>>::from_metacall_raw_leak(ret))
}
(_, 10) => metacallobj_result_wrap(
<HashMap<String, Box<dyn MetacallValue>>>::from_metacall_raw_leak(ret),
),
(_, 11) => metacallobj_result_wrap(<MetacallPointer>::from_metacall_raw_leak(ret)),
(_, 12) => metacallobj_result_wrap(MetacallFuture::from_metacall_raw_leak(ret)),
(_, 13) => metacallobj_result_wrap(MetacallFunction::from_metacall_raw_leak(ret)),
Expand Down
13 changes: 10 additions & 3 deletions source/ports/rs_port/src/types/metacall_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use super::{
MetacallObject, MetacallSetAttributeError, MetacallStringConversionError, MetacallValue,
};
use crate::{bindings::*, cstring, cstring_enum, parsers};
use std::ffi::c_void;
use std::{
ffi::c_void,
fmt::{self, Debug, Formatter},
};

#[derive(Debug)]
/// Represents Metacall Class. You can get this type when returned by a function or get a class by its
/// name with [from_name](#method.from_name).
pub struct MetacallClass {
Expand All @@ -20,10 +22,15 @@ impl Clone for MetacallClass {
Self {
found_by_name: self.found_by_name,
leak: true,
value: unsafe { metacall_value_copy(self.value) },
value: self.value,
}
}
}
impl Debug for MetacallClass {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "MetacallClass {{ ... }}")
}
}

impl MetacallClass {
#[doc(hidden)]
Expand Down
42 changes: 29 additions & 13 deletions source/ports/rs_port/src/types/metacall_exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use crate::{
metacall_exception_type, metacall_throwable_value, metacall_value_create_exception,
metacall_value_destroy, metacall_value_to_exception, metacall_value_to_throwable,
},
cstring, helpers, match_metacall_value, parsers,
cstring, helpers, parsers,
};
use std::{
ffi::{c_char, c_void, CStr},
fmt::{self, Debug, Formatter},
sync::Arc,
};

#[derive(Debug)]
/// Represents Metacall exception. You can create an exception with [new](#method.new).
pub struct MetacallException {
exception_struct: Arc<metacall_exception_type>,
Expand All @@ -29,6 +29,15 @@ impl Clone for MetacallException {
}
}
}
impl Debug for MetacallException {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
f,
"MetacallException {}",
format!("{{ {} }}", self.to_string())
)
}
}

impl MetacallException {
/// Creates a new exception.
Expand Down Expand Up @@ -100,7 +109,9 @@ impl MetacallException {
}

#[doc(hidden)]
pub fn into_raw(self) -> *mut c_void {
pub fn into_raw(mut self) -> *mut c_void {
self.leak = true;

self.value
}
}
Expand All @@ -116,8 +127,7 @@ pub enum MetacallThrowableValue<T: MetacallValue> {
Specified(T),
}

#[derive(Debug)]
/// Represents Metacall throwable.
/// Represents Metacall throwable. Keep in mind that it's not supported to pass a throwable as an argument.
pub struct MetacallThrowable {
leak: bool,
value_ptr: *mut c_void,
Expand All @@ -134,6 +144,15 @@ impl Clone for MetacallThrowable {
}
}
}
impl Debug for MetacallThrowable {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
f,
"MetacallThrowable {}",
format!("{{ {} }}", self.to_string())
)
}
}

impl MetacallThrowable {
#[doc(hidden)]
Expand Down Expand Up @@ -179,7 +198,10 @@ impl MetacallThrowable {

#[doc(hidden)]
pub fn into_raw(self) -> *mut c_void {
self.value_ptr
// It's not implemented in any loader as the time of writing this block of code.
// Feel free to implement as any loader adopted accepting Throwable as an argument.

panic!("Passing MetacallThrowable as an argument is not supported!");
}
}

Expand All @@ -195,13 +217,7 @@ impl ToString for MetacallException {
impl ToString for MetacallThrowable {
fn to_string(&self) -> String {
let throwable_value = self.get_value_untyped();
format!(
"[Throwable]: {}",
match_metacall_value!(throwable_value, {
exception: MetacallException => exception.to_string(),
_ => format!("{:#?}", throwable_value)
})
)
format!("[Throwable]: {:#?}", throwable_value)
}
}

Expand Down
11 changes: 9 additions & 2 deletions source/ports/rs_port/src/types/metacall_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use crate::{
bindings::{metacall_value_destroy, metacall_value_to_function, metacallfv_s},
parsers,
};
use std::ffi::c_void;
use std::{
ffi::c_void,
fmt::{self, Debug, Formatter},
};

#[derive(Debug)]
/// Represents Metacall function.
pub struct MetacallFunction {
leak: bool,
Expand All @@ -21,6 +23,11 @@ impl Clone for MetacallFunction {
}
}
}
impl Debug for MetacallFunction {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "MetacallFunction {{ ... }}")
}
}

impl MetacallFunction {
#[doc(hidden)]
Expand Down

0 comments on commit 66c9cc1

Please sign in to comment.