Skip to content

Commit

Permalink
Solve issues with future signature.
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed Dec 13, 2024
1 parent 3b91342 commit 269e72f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 15 deletions.
Empty file.
3 changes: 2 additions & 1 deletion source/ports/rs_port/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ include/
# Not ignore .vscode
!.vscode

# Ignore .env file autogenerated by CMake
# Ignore .env and config.toml file autogenerated by CMake
.vscode/.env
.cargo/config.toml
16 changes: 14 additions & 2 deletions source/ports/rs_port/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,6 @@ set(RS_PORT_DEBUG_ENVIRONMENT_VARIABLES
"DETOUR_LIBRARY_PATH=${PROJECT_OUTPUT_DIR}"
"PORT_LIBRARY_PATH=${PROJECT_OUTPUT_DIR}"
"${PROJECT_LIBRARY_PATH_NAME}=${TEST_LIB_PATH}"
"CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
"PROJECT_OUTPUT_DIR=${PROJECT_OUTPUT_DIR}"
)

set(RS_PORT_ENVIRONMENT_VARIABLES_FILE "${CMAKE_CURRENT_SOURCE_DIR}/.vscode/.env")
Expand All @@ -210,3 +208,17 @@ file(WRITE ${RS_PORT_ENVIRONMENT_VARIABLES_FILE} "")
foreach(ENV_VAR IN LISTS RS_PORT_DEBUG_ENVIRONMENT_VARIABLES)
file(APPEND ${RS_PORT_ENVIRONMENT_VARIABLES_FILE} "${ENV_VAR}\n")
endforeach()

# Create config.toml for supporting vscode debugging
set(RS_PORT_BUILD_ENVIRONMENT_VARIABLES
"CMAKE_BUILD_TYPE = { value = \"${CMAKE_BUILD_TYPE}\", force = true }"
"PROJECT_OUTPUT_DIR = { value = \"${PROJECT_OUTPUT_DIR}\", force = true }"
)

set(RS_PORT_CONFIG_ENV_FILE "${CMAKE_CURRENT_SOURCE_DIR}/config.toml")

file(WRITE ${RS_PORT_CONFIG_ENV_FILE} "[env]\n")

foreach(ENV_VAR IN LISTS RS_PORT_BUILD_ENVIRONMENT_VARIABLES)
file(APPEND ${RS_PORT_CONFIG_ENV_FILE} "${ENV_VAR}\n")
endforeach()
3 changes: 3 additions & 0 deletions source/ports/rs_port/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[env]
CMAKE_BUILD_TYPE = { value = "Debug", force = true }
PROJECT_OUTPUT_DIR = { value = "/media/sf_koyanisqaatsi/metacall-core-rs-port-improved/build", force = true }
17 changes: 6 additions & 11 deletions source/ports/rs_port/src/types/metacall_future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{
any::Any,
ffi::c_void,
fmt::{self, Debug, Formatter},
ptr::null_mut,
};

/// Function pointer type used for resolving/rejecting MetaCall futures. The first argument is the result
Expand Down Expand Up @@ -58,7 +59,7 @@ pub type MetaCallFutureHandler = fn(Box<dyn MetaCallValue>, Box<dyn Any>) -> Box
/// ```
#[repr(C)]
pub struct MetaCallFuture {
data: *mut dyn MetaCallValue,
data: *mut dyn Any,
leak: bool,
reject: Option<MetaCallFutureHandler>,
resolve: Option<MetaCallFutureHandler>,
Expand Down Expand Up @@ -147,14 +148,10 @@ unsafe extern "C" fn rejecter(reject_data: *mut c_void, upper_data: *mut c_void)
}

impl MetaCallFuture {
fn create_null_data() -> *mut dyn MetaCallValue {
Box::into_raw(cast::metacall_implementer_to_traitobj(MetaCallNull()))
}

#[doc(hidden)]
pub fn new_raw(value: *mut c_void) -> Self {
Self {
data: Self::create_null_data(),
data: null_mut::<()>(),
leak: false,
reject: None,
resolve: None,
Expand All @@ -165,7 +162,7 @@ impl MetaCallFuture {
#[doc(hidden)]
pub fn new_raw_leak(value: *mut c_void) -> Self {
Self {
data: Self::create_null_data(),
data: null_mut::<()>(),
leak: true,
reject: None,
resolve: None,
Expand Down Expand Up @@ -256,10 +253,8 @@ impl MetaCallFuture {
/// future.then(resolve).catch(reject),data(x).await_fut();
/// }
/// ```
pub fn data(mut self, data: impl MetaCallValue) -> Self {
unsafe { drop(Box::from_raw(self.data)) };

self.data = Box::into_raw(Box::new(data) as Box<dyn MetaCallValue>);
pub fn data<T: 'static>(mut self, data: T) -> Self {
self.data = Box::into_raw(Box::new(data));

self
}
Expand Down
23 changes: 23 additions & 0 deletions source/ports/rs_port/tests/metacall_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ fn test_float() {
fn test_double() {
generate_test::<f64>("test_double", 1.2345_f64);
}
// TODO
// fn test_mixed_numbers() {
// let result = ::metacall::metacall::<i64>(
// "test_mixed_numbers",
// [
// Box::new(1 as i16) as Box<dyn MetaCallValue>,
// Box::new(2 as i32) as Box<dyn MetaCallValue>,
// Box::new(3 as i64) as Box<dyn MetaCallValue>,
// ],
// );

// // TODO
// // ::metacall::metacall::<i64>("test_mixed_numbers", [1_i16, 2_i32, 3_i64]);
// // ::metacall::metacall::<i64>("test_mixed_numbers", (1_i16, 2_i32, 3_i64));

// assert!(result.is_ok());

// if let Ok(ret) = result {
// assert_eq!(ret, 6_i64)
// }
// }
fn test_string() {
generate_test::<String>(
"return_the_argument_py",
Expand Down Expand Up @@ -372,6 +393,8 @@ fn metacall() {
test_int();
test_long();
test_short();
// TODO
// test_mixed_numbers();
}
if load::from_single_file("node", js_test_file).is_ok() {
test_exception();
Expand Down
6 changes: 5 additions & 1 deletion source/ports/rs_port/tests/scripts/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ float test_float(float num)
double test_double(double num)
{
return num;
}
}
long test_mixed_numbers(short s, int i, long l)
{
return l + (long)i + (long)s;
}

0 comments on commit 269e72f

Please sign in to comment.