From 9e1cd5a92e873717b69f4bb62fe531aec9d311f7 Mon Sep 17 00:00:00 2001 From: Raymond Date: Thu, 20 Apr 2023 00:57:18 +0330 Subject: [PATCH] Improve rs_port stage 5 --- source/ports/rs_port/Cargo.toml | 7 +- source/ports/rs_port/src/macros.rs | 53 ------- source/ports/rs_port/src/parsers.rs | 14 +- .../ports/rs_port/src/types/metacall_class.rs | 13 +- .../rs_port/src/types/metacall_exception.rs | 42 +++-- .../rs_port/src/types/metacall_function.rs | 11 +- .../rs_port/src/types/metacall_future.rs | 94 ++++++++--- .../ports/rs_port/src/types/metacall_null.rs | 14 +- .../rs_port/src/types/metacall_object.rs | 11 +- .../rs_port/src/types/metacall_pointer.rs | 32 +++- .../ports/rs_port/src/types/metacall_value.rs | 146 ++++++++++-------- source/ports/rs_port/tests/loaders_test.rs | 1 - source/ports/rs_port/tests/metacall_test.rs | 98 +++--------- 13 files changed, 278 insertions(+), 258 deletions(-) diff --git a/source/ports/rs_port/Cargo.toml b/source/ports/rs_port/Cargo.toml index 30dbe14ce2..4df2b1df4c 100644 --- a/source/ports/rs_port/Cargo.toml +++ b/source/ports/rs_port/Cargo.toml @@ -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" } diff --git a/source/ports/rs_port/src/macros.rs b/source/ports/rs_port/src/macros.rs index 31a96caf65..2604455fd3 100644 --- a/source/ports/rs_port/src/macros.rs +++ b/source/ports/rs_port/src/macros.rs @@ -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> => $action, )* - $( $var: Vec>> => $action, )* - $( $var: Vec>>> => $action, )* - $( $var: Vec>>>> => $action, )* - $( $var: Vec>>>>> => $action, )* - - // Support up to 5 dimensional hashmaps for type casting - $( $var: HashMap:: => $action, )* - $( $var: HashMap::> => $action, )* - $( $var: HashMap::>> => $action, )* - $( - $var: HashMap::> - > - > => $action, - )* - $( - $var: HashMap::> - > - > - > => $action, - )* - $( - $var: HashMap::> - > - > - > - > => $action, - )* - - _ => panic!("Unkown type: {:#?}", $any) - }) - }}; - } - pub(crate) use cstring; pub(crate) use cstring_enum; - pub(crate) use match_metacall_value_all; } #[macro_export] diff --git a/source/ports/rs_port/src/parsers.rs b/source/ports/rs_port/src/parsers.rs index f20b2a8da8..06dd1cde76 100644 --- a/source/ports/rs_port/src/parsers.rs +++ b/source/ports/rs_port/src/parsers.rs @@ -65,10 +65,8 @@ pub fn raw_to_metacallobj_untyped(ret: *mut c_void) -> Box { (_, 6) => metacallobj_result_wrap(f64::from_metacall_raw(ret)), (_, 7) => metacallobj_result_wrap(String::from_metacall_raw(ret)), (_, 8) => metacallobj_result_wrap(>::from_metacall_raw(ret)), - (_, 9) => metacallobj_result_wrap(>>::from_metacall_raw(ret)), - (_, 10) => metacallobj_result_wrap( - >>::from_metacall_raw(ret), - ), + (_, 9) => metacallobj_result_wrap(>::from_metacall_raw(ret)), + (_, 10) => metacallobj_result_wrap(>::from_metacall_raw(ret)), (_, 11) => metacallobj_result_wrap(::from_metacall_raw(ret)), (_, 12) => metacallobj_result_wrap(MetacallFuture::from_metacall_raw(ret)), (_, 13) => metacallobj_result_wrap(MetacallFunction::from_metacall_raw(ret)), @@ -92,12 +90,10 @@ pub fn raw_to_metacallobj_untyped_leak(ret: *mut c_void) -> Box metacallobj_result_wrap(f64::from_metacall_raw_leak(ret)), (_, 7) => metacallobj_result_wrap(String::from_metacall_raw_leak(ret)), (_, 8) => metacallobj_result_wrap(>::from_metacall_raw_leak(ret)), - (_, 9) => { - metacallobj_result_wrap(>>::from_metacall_raw_leak(ret)) + (_, 9) => metacallobj_result_wrap(>::from_metacall_raw_leak(ret)), + (_, 10) => { + metacallobj_result_wrap(>::from_metacall_raw_leak(ret)) } - (_, 10) => metacallobj_result_wrap( - >>::from_metacall_raw_leak(ret), - ), (_, 11) => metacallobj_result_wrap(::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)), diff --git a/source/ports/rs_port/src/types/metacall_class.rs b/source/ports/rs_port/src/types/metacall_class.rs index 6054e5d213..97170b7a46 100644 --- a/source/ports/rs_port/src/types/metacall_class.rs +++ b/source/ports/rs_port/src/types/metacall_class.rs @@ -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 { @@ -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)] diff --git a/source/ports/rs_port/src/types/metacall_exception.rs b/source/ports/rs_port/src/types/metacall_exception.rs index 9f1585f589..eca20ef452 100644 --- a/source/ports/rs_port/src/types/metacall_exception.rs +++ b/source/ports/rs_port/src/types/metacall_exception.rs @@ -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, @@ -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. @@ -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 } } @@ -116,8 +127,7 @@ pub enum MetacallThrowableValue { 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, @@ -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)] @@ -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!"); } } @@ -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) } } diff --git a/source/ports/rs_port/src/types/metacall_function.rs b/source/ports/rs_port/src/types/metacall_function.rs index 4c3328fff4..6ce4489fd1 100644 --- a/source/ports/rs_port/src/types/metacall_function.rs +++ b/source/ports/rs_port/src/types/metacall_function.rs @@ -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, @@ -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)] diff --git a/source/ports/rs_port/src/types/metacall_future.rs b/source/ports/rs_port/src/types/metacall_future.rs index 504e933f47..237f268e29 100644 --- a/source/ports/rs_port/src/types/metacall_future.rs +++ b/source/ports/rs_port/src/types/metacall_future.rs @@ -3,7 +3,11 @@ use crate::{ bindings::{metacall_await_future, metacall_value_destroy, metacall_value_to_future}, helpers, parsers, }; -use std::{ffi::c_void, ptr}; +use std::{ + ffi::c_void, + fmt::{self, Debug, Formatter}, + ptr, +}; /// Function pointer type used for resolving/rejecting Metacall futures. The first argument is the result /// and the second argument is the data that you may want to access when the function gets called. @@ -11,8 +15,8 @@ use std::{ffi::c_void, ptr}; /// [MetacallFuture reject](MetacallFuture#method.catch) for usage. pub type MetacallFutureHandler = fn(Box, Box); -#[derive(Debug)] -/// Represents MetacallFuture. Usage example: ... +/// Represents MetacallFuture. Keep in mind that it's not supported to pass a future as an argument. +/// Usage example: ... /// ``` /// use metacall::{MetacallValue, MetacallFuture, metacall}; /// @@ -48,24 +52,63 @@ impl Clone for MetacallFuture { } } } +impl Debug for MetacallFuture { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + let boxed_data = unsafe { Box::from_raw(self.data) }; + let data = if boxed_data.is::() { + None + } else { + Some(format!("{:#?}", boxed_data)) + }; + Box::leak(boxed_data); + + let resolve = if self.resolve.is_none() { + "None" + } else { + "Some" + }; + let reject = if self.reject.is_none() { + "None" + } else { + "Some" + }; + + f.debug_struct("MetacallFuture") + .field("data", &data) + .field("resolve", &resolve) + .field("reject", &reject) + .finish() + } +} + +type MetacallFutureFFIData = ( + // Resolve + Option, + // Reject + Option, + // User data + *mut dyn MetacallValue, +); unsafe extern "C" fn resolver(resolve_data: *mut c_void, upper_data: *mut c_void) -> *mut c_void { - let self_struct = *Box::from_raw(upper_data as *mut MetacallFuture); - let user_data = Box::from_raw(self_struct.data); + let (resolve, _, data) = *Box::from_raw(upper_data as *mut MetacallFutureFFIData); + let user_data = Box::from_raw(data); - (self_struct.resolve.unwrap())(parsers::raw_to_metacallobj_untyped(resolve_data), user_data); + (resolve.unwrap())( + parsers::raw_to_metacallobj_untyped_leak(resolve_data), + user_data, + ); ptr::null_mut() } unsafe extern "C" fn rejecter(reject_data: *mut c_void, upper_data: *mut c_void) -> *mut c_void { - let self_struct = *Box::from_raw(upper_data as *mut MetacallFuture); - let user_data = Box::from_raw(self_struct.data); - - (self_struct.reject.unwrap())(parsers::raw_to_metacallobj_untyped(reject_data), user_data); + let (_, reject, data) = *Box::from_raw(upper_data as *mut MetacallFutureFFIData); + let user_data = Box::from_raw(data); - if !self_struct.leak { - unsafe { metacall_value_destroy(self_struct.value) }; - } + (reject.unwrap())( + parsers::raw_to_metacallobj_untyped_leak(reject_data), + user_data, + ); ptr::null_mut() } @@ -124,32 +167,35 @@ impl MetacallFuture { pub fn await_fut(self) { let resolve_is_some = self.resolve.is_some(); let reject_is_some = self.reject.is_some(); - let value = self.value; unsafe { metacall_value_destroy(metacall_await_future( - metacall_value_to_future(value), + metacall_value_to_future(self.value), if resolve_is_some { Some(resolver) } else { None }, if reject_is_some { Some(rejecter) } else { None }, - Box::into_raw(Box::new(self)) as *mut c_void, + // TODO: Solve the memory leak that happens here + Box::into_raw(Box::new((self.resolve, self.reject, self.data))) as *mut c_void, )) }; } #[doc(hidden)] pub fn into_raw(self) -> *mut c_void { - self.value + // 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 Future as an argument. + + panic!("Passing MetacallFuture as an argument is not supported!"); } } -// impl Drop for MetacallFuture { -// fn drop(&mut self) { -// if !self.leak { -// unsafe { metacall_value_destroy(self.value) }; -// } -// } -// } +impl Drop for MetacallFuture { + fn drop(&mut self) { + if !self.leak { + unsafe { metacall_value_destroy(self.value) }; + } + } +} diff --git a/source/ports/rs_port/src/types/metacall_null.rs b/source/ports/rs_port/src/types/metacall_null.rs index b20ba51688..32c7bb462f 100644 --- a/source/ports/rs_port/src/types/metacall_null.rs +++ b/source/ports/rs_port/src/types/metacall_null.rs @@ -1,11 +1,21 @@ use crate::bindings::metacall_value_create_null; -use std::ffi::c_void; +use std::{ + ffi::c_void, + fmt::{self, Debug, Formatter}, +}; -#[derive(Clone, Debug)] +#[derive(Clone)] /// Represents NULL. +// This is a zero-sized struct. It doesn't allocate any memory and will only create a null pointer +// when needed. pub struct MetacallNull(); unsafe impl Send for MetacallNull {} unsafe impl Sync for MetacallNull {} +impl Debug for MetacallNull { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "MetacallNull {{ }}") + } +} impl MetacallNull { #[doc(hidden)] diff --git a/source/ports/rs_port/src/types/metacall_object.rs b/source/ports/rs_port/src/types/metacall_object.rs index 2c0ecb692a..cdad1ba505 100644 --- a/source/ports/rs_port/src/types/metacall_object.rs +++ b/source/ports/rs_port/src/types/metacall_object.rs @@ -9,13 +9,15 @@ use crate::{ }, cstring_enum, parsers, }; -use std::ffi::c_void; +use std::{ + ffi::c_void, + fmt::{self, Debug, Formatter}, +}; // Used for documentation. #[allow(unused_imports)] use super::MetacallClass; -#[derive(Debug)] /// Represents Metacall Object. You can get this type when returned by a function or create one from /// a class with [create_object](MetacallClass#method.create_object). pub struct MetacallObject { @@ -32,6 +34,11 @@ impl Clone for MetacallObject { } } } +impl Debug for MetacallObject { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "MetacallObject {{ ... }}") + } +} impl MetacallObject { #[doc(hidden)] diff --git a/source/ports/rs_port/src/types/metacall_pointer.rs b/source/ports/rs_port/src/types/metacall_pointer.rs index 283d2200d3..fc2f62088c 100644 --- a/source/ports/rs_port/src/types/metacall_pointer.rs +++ b/source/ports/rs_port/src/types/metacall_pointer.rs @@ -1,8 +1,13 @@ use super::MetacallValue; -use crate::bindings::{metacall_value_create_ptr, metacall_value_destroy, metacall_value_to_ptr}; -use std::ffi::c_void; +use crate::{ + bindings::{metacall_value_create_ptr, metacall_value_destroy, metacall_value_to_ptr}, + MetacallNull, +}; +use std::{ + ffi::c_void, + fmt::{self, Debug, Formatter}, +}; -#[derive(Debug)] /// Represents Metacall pointer. This type cannot be used in other languages. This type is highly /// unsafe so be careful! pub struct MetacallPointer { @@ -23,6 +28,21 @@ impl Clone for MetacallPointer { } } } +impl Debug for MetacallPointer { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + let boxed_value = unsafe { Box::from_raw(self.rust_value) }; + let value = if (*boxed_value).is::() { + None + } else { + Some(format!("{:#?}", boxed_value)) + }; + Box::leak(boxed_value); + + f.debug_struct("MetacallPointer") + .field("value", &value) + .finish() + } +} impl MetacallPointer { fn get_rust_value_ptr(value: *mut c_void) -> *mut Box { @@ -50,15 +70,15 @@ impl MetacallPointer { } /// Creates a new Metacall pointer and casts it to T. - pub fn new(ptr: impl MetacallValue) -> Result> { + pub fn new(ptr: impl MetacallValue) -> Self { let rust_value = Box::into_raw(Box::new(Box::new(ptr) as Box)); - Ok(Self { + Self { leak: false, rust_value, rust_value_leak: false, value: unsafe { metacall_value_create_ptr(rust_value.cast()) }, - }) + } } /// Consumes the Metacall pointer and returns ownership of the value without type diff --git a/source/ports/rs_port/src/types/metacall_value.rs b/source/ports/rs_port/src/types/metacall_value.rs index 590635d10e..4f03642175 100644 --- a/source/ports/rs_port/src/types/metacall_value.rs +++ b/source/ports/rs_port/src/types/metacall_value.rs @@ -6,7 +6,7 @@ use crate::{ bindings::*, cstring, helpers::{MetacallClone, MetacallDowncast}, - match_metacall_value, match_metacall_value_all, parsers, + match_metacall_value, parsers, }; use std::{ collections::HashMap, @@ -17,10 +17,62 @@ use std::{ /// Trait of any possible object in Metacall. /// Checkout [match_metacall_value](match_metacall_value) macro for -/// matching trait objects of this trait. -/// Also check [std implementors](#foreign-impls) and [other implementors](#implementors). +/// matching trait objects of this trait. Let' see what types we can use with an example: ... +/// ``` +/// // bool +/// metacall::metacall_untyped("x", [true, false]); +/// // char +/// metacall::metacall_untyped("x", ['A', 'Z']); +/// // short +/// metacall::metacall_untyped("x", [5 as i16]); +/// // int +/// metacall::metacall_untyped("x", [5 as i32]); +/// // long +/// metacall::metacall_untyped("x", [5 as i64]); +/// // float +/// metacall::metacall_untyped("x", [5.0 as f32]); +/// // double +/// metacall::metacall_untyped("x", [5.0 as f64]); +/// // string +/// metacall::metacall_untyped("x", [String::from("hi")]); +/// // buffer +/// metacall::metacall_untyped("x", [ +/// String::from("hi") +/// .as_bytes() +/// .iter() +/// .map(|&b| b as i8) +/// .collect::>() +/// ]); +/// // array +/// metacall::metacall_untyped("x", [vec![5, 6]]); +/// // map +/// let mut hashmap = std::collections::HashMap::new(); +/// hashmap.insert(String::from("hi"), String::from("there!")); +/// metacall::metacall_untyped("x", [hashmap]); +/// // pointer +/// metacall::metacall_untyped("x", [metacall::MetacallPointer::new(String::from("hi"))]); +/// // future? +/// // nope! you can't pass a future! +/// // function +/// metacall::metacall_untyped("x", [ +/// metacall::metacall_no_arg::("y").unwrap() +/// ]); +/// // null +/// metacall::metacall_untyped("x", [metacall::MetacallNull()]); +/// // class +/// metacall::metacall_untyped("x", [metacall::MetacallClass::from_name("MyClass").unwrap()]); +/// // object +/// let class = metacall::MetacallClass::from_name("MyClass").unwrap(); +/// metacall::metacall_untyped("x", [class.create_object_no_arg("myObject").unwrap()]); +/// // exception +/// metacall::metacall_untyped("x", [ +/// metacall::metacall_no_arg::("y").unwrap() +/// ]); +/// // throwable? +/// // nope! you can't pass a throwable! +/// ``` pub trait MetacallValue: MetacallClone + MetacallDowncast + Debug { - // It tries to convert the raw pointer to the value and return a trait object on failure + // It tries to convert the raw pointer to the value or return a trait object on failure. #[doc(hidden)] fn from_metacall_raw(v: *mut c_void) -> Result> where @@ -32,21 +84,21 @@ pub trait MetacallValue: MetacallClone + MetacallDowncast + Debug { Ok(value) } - // Same as `from_metacall_raw` but doesn't free the memory on drop and leaks + // Same as `from_metacall_raw` but doesn't free the memory on drop and leaks. #[doc(hidden)] fn from_metacall_raw_leak(v: *mut c_void) -> Result> where Self: Sized; - // It returns the enum index of Metacall Protocol in the core + // It returns the enum index of Metacall Protocol in the core. It's used for faster type matching. #[doc(hidden)] fn get_metacall_id() -> u32 where Self: Sized; - // It converts the value to a raw value known by the metacall core + // It converts the value to a raw value known by the metacall core. #[doc(hidden)] fn into_metacall_raw(self) -> *mut c_void; } -#[doc = "Equivalent to Metacall boolean type."] +/// Equivalent to Metacall boolean type. impl MetacallValue for bool { fn get_metacall_id() -> u32 { 0 @@ -60,7 +112,7 @@ impl MetacallValue for bool { unsafe { metacall_value_create_bool((self as c_int).try_into().unwrap()) } } } -#[doc = "Equivalent to Metacall char type."] +/// Equivalent to Metacall char type. impl MetacallValue for char { fn get_metacall_id() -> u32 { 1 @@ -74,7 +126,7 @@ impl MetacallValue for char { unsafe { metacall_value_create_char(self as c_char) } } } -#[doc = "Equivalent to Metacall short type."] +/// Equivalent to Metacall short type. impl MetacallValue for i16 { fn get_metacall_id() -> u32 { 2 @@ -88,7 +140,7 @@ impl MetacallValue for i16 { unsafe { metacall_value_create_short(self) } } } -#[doc = "Equivalent to Metacall int type."] +/// Equivalent to Metacall int type. impl MetacallValue for i32 { fn get_metacall_id() -> u32 { 3 @@ -102,7 +154,7 @@ impl MetacallValue for i32 { unsafe { metacall_value_create_int(self) } } } -#[doc = "Equivalent to Metacall long type."] +/// Equivalent to Metacall long type. impl MetacallValue for i64 { fn get_metacall_id() -> u32 { 4 @@ -116,7 +168,7 @@ impl MetacallValue for i64 { unsafe { metacall_value_create_long(self) } } } -#[doc = "Equivalent to Metacall float type."] +/// Equivalent to Metacall float type. impl MetacallValue for f32 { fn get_metacall_id() -> u32 { 5 @@ -130,7 +182,7 @@ impl MetacallValue for f32 { unsafe { metacall_value_create_float(self) } } } -#[doc = "Equivalent to Metacall double type."] +/// Equivalent to Metacall double type. impl MetacallValue for f64 { fn get_metacall_id() -> u32 { 6 @@ -144,7 +196,7 @@ impl MetacallValue for f64 { unsafe { metacall_value_create_double(self) } } } -#[doc = "Equivalent to Metacall string type."] +/// Equivalent to Metacall string type. impl MetacallValue for String { fn get_metacall_id() -> u32 { 7 @@ -161,7 +213,7 @@ impl MetacallValue for String { unsafe { metacall_value_create_string(raw.as_ptr(), self.len()) } } } -#[doc = "Equivalent to Metacall buffer type."] +/// Equivalent to Metacall buffer type. impl MetacallValue for Vec { fn get_metacall_id() -> u32 { 8 @@ -179,7 +231,7 @@ impl MetacallValue for Vec { unsafe { metacall_value_create_buffer(self.as_mut_ptr() as *mut c_void, self.len()) } } } -#[doc = "Equivalent to Metacall array type."] +/// Equivalent to Metacall array type. impl MetacallValue for Vec { fn get_metacall_id() -> u32 { 9 @@ -202,7 +254,7 @@ impl MetacallValue for Vec { unsafe { metacall_value_create_array(array.as_mut_ptr(), array.len()) } } } -#[doc = "Equivalent to Metacall map type."] +/// Equivalent to Metacall map type. impl MetacallValue for HashMap { fn get_metacall_id() -> u32 { 10 @@ -255,7 +307,7 @@ impl MetacallValue for HashMap { unsafe { metacall_value_create_map(hashmap.as_mut_ptr(), hashmap.len()) } } } -#[doc = "Equivalent to Metacall pointer type."] +/// Equivalent to Metacall pointer type. impl MetacallValue for MetacallPointer { fn get_metacall_id() -> u32 { 11 @@ -270,7 +322,7 @@ impl MetacallValue for MetacallPointer { self.into_raw() } } -#[doc = "Equivalent to Metacall future type."] +/// Equivalent to Metacall future type. impl MetacallValue for MetacallFuture { fn get_metacall_id() -> u32 { 12 @@ -285,7 +337,7 @@ impl MetacallValue for MetacallFuture { self.into_raw() } } -#[doc = "Equivalent to Metacall function type."] +/// Equivalent to Metacall function type. impl MetacallValue for MetacallFunction { fn get_metacall_id() -> u32 { 13 @@ -300,7 +352,7 @@ impl MetacallValue for MetacallFunction { self.into_raw() } } -#[doc = "Equivalent to Metacall null type."] +/// Equivalent to Metacall null type. impl MetacallValue for MetacallNull { fn get_metacall_id() -> u32 { 14 @@ -317,7 +369,7 @@ impl MetacallValue for MetacallNull { self.into_raw() } } -#[doc = "Equivalent to Metacall class type."] +/// Equivalent to Metacall class type. impl MetacallValue for MetacallClass { fn get_metacall_id() -> u32 { 15 @@ -332,7 +384,7 @@ impl MetacallValue for MetacallClass { self.into_raw() } } -#[doc = "Equivalent to Metacall object type."] +/// Equivalent to Metacall object type. impl MetacallValue for MetacallObject { fn get_metacall_id() -> u32 { 16 @@ -347,7 +399,7 @@ impl MetacallValue for MetacallObject { self.into_raw() } } -#[doc = "Equivalent to Metacall exception type."] +/// Equivalent to Metacall exception type. impl MetacallValue for MetacallException { fn get_metacall_id() -> u32 { 17 @@ -362,7 +414,7 @@ impl MetacallValue for MetacallException { self.into_raw() } } -#[doc = "Equivalent to Metacall throwable type."] +/// Equivalent to Metacall throwable type. impl MetacallValue for MetacallThrowable { fn get_metacall_id() -> u32 { 18 @@ -377,45 +429,3 @@ impl MetacallValue for MetacallThrowable { self.into_raw() } } -#[doc(hidden)] -impl MetacallValue for Box { - fn get_metacall_id() -> u32 { - // Something random - 100 - } - fn from_metacall_raw_leak(v: *mut c_void) -> Result> { - Ok(parsers::raw_to_metacallobj_untyped_leak(v)) - } - fn from_metacall_raw(v: *mut c_void) -> Result> { - Ok(parsers::raw_to_metacallobj_untyped(v)) - } - fn into_metacall_raw(self) -> *mut c_void { - match_metacall_value_all!( - self, - x, - x.into_metacall_raw(), - [ - bool, - char, - i16, - i32, - i64, - f32, - f64, - String, - Vec, - Vec>, - HashMap>, - MetacallPointer, - MetacallFuture, - MetacallFunction, - MetacallNull, - MetacallClass, - MetacallObject, - MetacallException, - MetacallThrowable, - Box - ] - ) - } -} diff --git a/source/ports/rs_port/tests/loaders_test.rs b/source/ports/rs_port/tests/loaders_test.rs index c7d2cb1d28..e915f71881 100644 --- a/source/ports/rs_port/tests/loaders_test.rs +++ b/source/ports/rs_port/tests/loaders_test.rs @@ -29,7 +29,6 @@ fn load_from_memory_test() { call_greet("load_from_memory", 1); } -// TODO: Why not putting this into a script inside scripts folder? fn load_from_file_test() { // Finding a temporary address to store the temporary js file let temp_js_pathbuf = diff --git a/source/ports/rs_port/tests/metacall_test.rs b/source/ports/rs_port/tests/metacall_test.rs index dc526e188b..8aa6e779b6 100644 --- a/source/ports/rs_port/tests/metacall_test.rs +++ b/source/ports/rs_port/tests/metacall_test.rs @@ -31,8 +31,8 @@ fn generate_test_custom_validation( expected_value: impl MetacallValue + Clone, validator: impl FnOnce(T), ) { - let expected_value = Box::new(expected_value) as Box; - let test = if !expected_value.is::() { + let expected_value_boxed = Box::new(expected_value.clone()) as Box; + let test = if !expected_value_boxed.is::() { ::metacall::metacall::(name, [expected_value]) } else { ::metacall::metacall_no_arg::(name) @@ -143,7 +143,7 @@ fn test_pointer() { generate_test_custom_validation::( "return_the_argument_py", "pointer", - MetacallPointer::new(expected_value.clone()).unwrap(), + MetacallPointer::new(expected_value.clone()), |pointer| { let receieved_value = pointer.get_value::().unwrap(); @@ -182,38 +182,24 @@ fn test_future() { "test_future_resolve", "future", MetacallNull(), - |upper_future| { - generate_test_custom_validation::( - "return_the_argument_py", - "future", - upper_future, - move |future| { - fn resolve(result: Box, data: Box) { - validate(result, data); - } + move |future| { + fn resolve(result: Box, data: Box) { + validate(result, data); + } - future.then(resolve).data(String::from("data")).await_fut(); - }, - ); + future.then(resolve).data(String::from("data")).await_fut(); }, ); generate_test_custom_validation::( "test_future_reject", "future", MetacallNull(), - |upper_future| { - generate_test_custom_validation::( - "return_the_argument_py", - "future", - upper_future, - move |future| { - fn reject(result: Box, data: Box) { - validate(result, data); - } + move |future| { + fn reject(result: Box, data: Box) { + validate(result, data); + } - future.catch(reject).data(String::from("data")).await_fut(); - }, - ); + future.catch(reject).data(String::from("data")).await_fut(); }, ); } @@ -311,60 +297,26 @@ fn test_object() { ); } fn test_exception() { - // generate_test_custom_validation::( - // "test_exception", - // "exception", - // MetacallNull(), - // |upper_exception| { - // generate_test_custom_validation::( - // "return_the_argument_js", - // "exception", - // upper_exception, - // move |exception| { - // let exception_message = exception.get_message(); - // if exception_message.as_str() != "hi there!" { - // invalid_return_value("hi there!", exception_message); - // } - // }, - // ); - // }, - // ); - generate_test_custom_validation::( "test_exception", "exception", MetacallNull(), - move |exception| { - let exception_message = exception.get_message(); - if exception_message.as_str() != "hi there!" { - invalid_return_value("hi there!", exception_message); - } + |upper_exception| { + generate_test_custom_validation::( + "return_the_argument_js", + "exception", + upper_exception, + move |exception| { + let exception_message = exception.get_message(); + if exception_message.as_str() != "hi there!" { + invalid_return_value("hi there!", exception_message); + } + }, + ); }, ); } fn test_throwable() { - // generate_test_custom_validation::( - // "test_throwable", - // "throwable", - // MetacallNull(), - // |upper_throwable: MetacallThrowable| { - // generate_test_custom_validation::( - // "return_the_argument_py", - // "throwable", - // upper_throwable, - // |throwable| { - // let exception_message = throwable - // .get_value::() - // .unwrap() - // .get_message(); - // if exception_message.as_str() != "hi there!" { - // invalid_return_value("hi there!", exception_message); - // } - // }, - // ); - // }, - // ); - generate_test_custom_validation::( "test_throwable", "throwable",