Skip to content

Commit b72e196

Browse files
committed
Implement metacall_box for passing function arguments with different types
1 parent 269e72f commit b72e196

File tree

5 files changed

+51
-24
lines changed

5 files changed

+51
-24
lines changed

source/ports/rs_port/src/cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,6 @@ pub fn metacallobj_untyped_to_raw(ret: Box<dyn MetaCallValue>) -> Option<*mut c_
235235
None
236236
}
237237

238-
pub fn metacall_implementer_to_traitobj(v: impl MetaCallValue) -> Box<dyn MetaCallValue> {
238+
pub fn metacall_box(v: impl MetaCallValue) -> Box<dyn MetaCallValue> {
239239
Box::new(v) as Box<dyn MetaCallValue>
240240
}

source/ports/rs_port/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ pub use types::*;
8888
#[doc(hidden)]
8989
mod init;
9090

91+
pub use cast::metacall_box;
9192
pub use init::initialize;
9293
pub use init::is_initialized;
9394

source/ports/rs_port/src/types/metacall_exception.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl MetaCallThrowable {
180180
Ok(mut value) => {
181181
value.leak = true;
182182

183-
cast::metacall_implementer_to_traitobj(value)
183+
cast::metacall_box(value)
184184
}
185185
Err(original) => original,
186186
}

source/ports/rs_port/src/types/metacall_value.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,36 @@ impl MetaCallValue for MetaCallThrowable {
429429
self.into_raw()
430430
}
431431
}
432+
/// Just a Rust barrier made for easier polymorphism.
433+
impl MetaCallValue for Box<dyn MetaCallValue> {
434+
fn get_metacall_id() -> metacall_value_id {
435+
metacall_value_id::METACALL_INVALID
436+
}
437+
fn from_metacall_raw_leak(v: *mut c_void) -> Result<Self, Box<dyn MetaCallValue>> {
438+
Ok(cast::raw_to_metacallobj_untyped_leak(v))
439+
}
440+
fn into_metacall_raw(self) -> *mut c_void {
441+
match_metacall_value!(self, {
442+
bool: bool => bool.into_metacall_raw(),
443+
char: char => char.into_metacall_raw(),
444+
num: i16 => num.into_metacall_raw(),
445+
num: i32 => num.into_metacall_raw(),
446+
num: i64 => num.into_metacall_raw(),
447+
num: f32 => num.into_metacall_raw(),
448+
num: f64 => num.into_metacall_raw(),
449+
str: String => str.into_metacall_raw(),
450+
buf: Vec<i8> => buf.into_metacall_raw(),
451+
arr: Vec<Box<dyn MetaCallValue>> => arr.into_metacall_raw(),
452+
map: HashMap<String, Box<dyn MetaCallValue>> => map.into_metacall_raw(),
453+
ptr: MetaCallPointer => ptr.into_metacall_raw(),
454+
fut: MetaCallFuture => fut.into_metacall_raw(),
455+
fun: MetaCallFunction => fun.into_metacall_raw(),
456+
null: MetaCallNull => null.into_metacall_raw(),
457+
cls: MetaCallClass => cls.into_metacall_raw(),
458+
obj: MetaCallObject => obj.into_metacall_raw(),
459+
exc: MetaCallException => exc.into_metacall_raw(),
460+
thr: MetaCallThrowable => thr.into_metacall_raw(),
461+
_ => MetaCallNull().into_metacall_raw()
462+
})
463+
}
464+
}

source/ports/rs_port/tests/metacall_test.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -81,27 +81,21 @@ fn test_float() {
8181
fn test_double() {
8282
generate_test::<f64>("test_double", 1.2345_f64);
8383
}
84-
// TODO
85-
// fn test_mixed_numbers() {
86-
// let result = ::metacall::metacall::<i64>(
87-
// "test_mixed_numbers",
88-
// [
89-
// Box::new(1 as i16) as Box<dyn MetaCallValue>,
90-
// Box::new(2 as i32) as Box<dyn MetaCallValue>,
91-
// Box::new(3 as i64) as Box<dyn MetaCallValue>,
92-
// ],
93-
// );
94-
95-
// // TODO
96-
// // ::metacall::metacall::<i64>("test_mixed_numbers", [1_i16, 2_i32, 3_i64]);
97-
// // ::metacall::metacall::<i64>("test_mixed_numbers", (1_i16, 2_i32, 3_i64));
98-
99-
// assert!(result.is_ok());
84+
fn test_mixed_numbers() {
85+
let result = ::metacall::metacall::<i64>(
86+
"test_mixed_numbers",
87+
[
88+
::metacall::metacall_box(1 as i16),
89+
::metacall::metacall_box(2 as i32),
90+
::metacall::metacall_box(3 as i64),
91+
],
92+
);
10093

101-
// if let Ok(ret) = result {
102-
// assert_eq!(ret, 6_i64)
103-
// }
104-
// }
94+
assert!(result.is_ok());
95+
if let Ok(ret) = result {
96+
assert_eq!(ret, 6_i64)
97+
}
98+
}
10599
fn test_string() {
106100
generate_test::<String>(
107101
"return_the_argument_py",
@@ -393,8 +387,7 @@ fn metacall() {
393387
test_int();
394388
test_long();
395389
test_short();
396-
// TODO
397-
// test_mixed_numbers();
390+
test_mixed_numbers();
398391
}
399392
if load::from_single_file("node", js_test_file).is_ok() {
400393
test_exception();

0 commit comments

Comments
 (0)