Skip to content

Commit

Permalink
Add debugging environment for valgrind, refactored the code a bit, fi…
Browse files Browse the repository at this point in the history
…rst attempt for MetaCallFuture, vscode debugging in process.
  • Loading branch information
viferga committed Dec 13, 2024
1 parent f258548 commit 3b91342
Show file tree
Hide file tree
Showing 25 changed files with 275 additions and 143 deletions.
3 changes: 3 additions & 0 deletions source/ports/rs_port/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ include/

# Not ignore .vscode
!.vscode

# Ignore .env file autogenerated by CMake
.vscode/.env
5 changes: 5 additions & 0 deletions source/ports/rs_port/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
}
},
"args": [],
"envFile": "${workspaceFolder}${/}.vscode${/}.env",
"cwd": "${workspaceFolder}"
},
{
Expand All @@ -40,6 +41,7 @@
}
},
"args": [],
"envFile": "${workspaceFolder}${/}.vscode${/}.env",
"cwd": "${workspaceFolder}"
},
{
Expand All @@ -59,6 +61,7 @@
}
},
"args": [],
"envFile": "${workspaceFolder}${/}.vscode${/}.env",
"cwd": "${workspaceFolder}"
},
{
Expand All @@ -78,6 +81,7 @@
}
},
"args": [],
"envFile": "${workspaceFolder}${/}.vscode${/}.env",
"cwd": "${workspaceFolder}"
},
{
Expand All @@ -97,6 +101,7 @@
}
},
"args": [],
"envFile": "${workspaceFolder}${/}.vscode${/}.env",
"cwd": "${workspaceFolder}"
}
]
Expand Down
22 changes: 22 additions & 0 deletions source/ports/rs_port/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ project_library_path(TEST_LIB_PATH
${PROJECT_OUTPUT_DIR}
)

# Define environment variables
test_environment_variables(${target}
""
${TESTS_ENVIRONMENT_VARIABLES}
Expand All @@ -188,3 +189,24 @@ test_environment_variables(${target}
"${SANITIZER_FLAGS}"
"${CARGO_VALGRIND_FLAGS}"
)

# Create .env file for supporting vscode debugging
set(RS_PORT_DEBUG_ENVIRONMENT_VARIABLES
"LOADER_LIBRARY_PATH=${PROJECT_OUTPUT_DIR}"
"LOADER_SCRIPT_PATH=${PROJECT_OUTPUT_DIR}/scripts"
"CONFIGURATION_PATH=${PROJECT_OUTPUT_DIR}/configurations/global.json"
"SERIAL_LIBRARY_PATH=${PROJECT_OUTPUT_DIR}"
"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")

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()
2 changes: 1 addition & 1 deletion source/ports/rs_port/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn main() {
let _metacall = switch::initialize().unwrap();

// Load the file
loaders::from_single_file("ts", "sum.ts").unwrap();
load::from_single_file("ts", "sum.ts").unwrap();

// Call the sum function
let sum = metacall::<f64>("sum", [1.0, 2.0]).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion source/ports/rs_port/inline/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ macro_rules! gen_inline_macro {
let buffer = token_stream_input.to_string();

let result = quote! {{
::metacall::loaders::from_memory(stringify!($name), #buffer.to_string()).unwrap()
::metacall::load::from_memory(stringify!($name), #buffer.to_string()).unwrap()
}};

result.into()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,51 @@ pub fn metacallobj_to_raw_args(
.map(|arg| arg.into_metacall_raw())
.collect::<Vec<*mut c_void>>()
}

pub fn metacallobj_untyped_to_raw(ret: Box<dyn MetaCallValue>) -> Option<*mut c_void> {
if let Some(v) = ret.downcast_ref::<bool>() {
return Some(v.into_metacall_raw());
} else if let Some(v) = ret.downcast_ref::<char>() {
return Some(v.into_metacall_raw());
} else if let Some(v) = ret.downcast_ref::<i16>() {
return Some(v.into_metacall_raw());
} else if let Some(v) = ret.downcast_ref::<i32>() {
return Some(v.into_metacall_raw());
} else if let Some(v) = ret.downcast_ref::<i64>() {
return Some(v.into_metacall_raw());
} else if let Some(v) = ret.downcast_ref::<f32>() {
return Some(v.into_metacall_raw());
} else if let Some(v) = ret.downcast_ref::<f64>() {
return Some(v.into_metacall_raw());
} else if let Some(v) = ret.downcast_ref::<String>() {
return Some(v.clone().into_metacall_raw());
} else if let Some(v) = ret.downcast_ref::<Vec<i8>>() {
return Some(v.clone().into_metacall_raw());
} else if let Some(v) = ret.downcast_ref::<Vec<MetaCallNull>>() {
return Some(v.clone().into_metacall_raw());
} else if let Some(v) = ret.downcast_ref::<HashMap<String, MetaCallNull>>() {
return Some(v.clone().into_metacall_raw());
} else if let Some(v) = ret.downcast_ref::<MetaCallPointer>() {
return Some(v.clone().into_metacall_raw());
} else if let Some(v) = ret.downcast_ref::<MetaCallFuture>() {
return Some(v.clone().into_metacall_raw());
} else if let Some(v) = ret.downcast_ref::<MetaCallFunction>() {
return Some(v.clone().into_metacall_raw());
} else if let Some(v) = ret.downcast_ref::<MetaCallNull>() {
return Some(v.clone().into_metacall_raw());
} else if let Some(v) = ret.downcast_ref::<MetaCallClass>() {
return Some(v.clone().into_metacall_raw());
} else if let Some(v) = ret.downcast_ref::<MetaCallObject>() {
return Some(v.clone().into_metacall_raw());
} else if let Some(v) = ret.downcast_ref::<MetaCallException>() {
return Some(v.clone().into_metacall_raw());
} else if let Some(v) = ret.downcast_ref::<MetaCallThrowable>() {
return Some(v.clone().into_metacall_raw());
}

None
}

pub fn metacall_implementer_to_traitobj(v: impl MetaCallValue) -> Box<dyn MetaCallValue> {
Box::new(v) as Box<dyn MetaCallValue>
}
4 changes: 0 additions & 4 deletions source/ports/rs_port/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,3 @@ impl Clone for Box<dyn MetaCallValue + Send + Sync + '_> {
clone_box(&**self)
}
}

pub fn metacall_implementer_to_traitobj(v: impl MetaCallValue) -> Box<dyn MetaCallValue> {
Box::new(v) as Box<dyn MetaCallValue>
}
46 changes: 46 additions & 0 deletions source/ports/rs_port/src/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::{
bindings::{metacall_destroy, metacall_initialize, metacall_is_initialized},
types::MetaCallInitError,
};
use std::{ffi::c_int, ptr};

pub struct MetaCallDestroy(unsafe extern "C" fn() -> c_int);

impl Drop for MetaCallDestroy {
fn drop(&mut self) {
let code = unsafe { self.0() };

if code != 0 {
panic!("MetaCall failed to destroy with code: {}", code)
}
}
}

/// Initializes MetaCall. Always remember to store the output in a variable to avoid instant drop.
/// For example: ...
/// ```
/// // Initialize metacall at the top of your main function before loading your codes or
/// // calling any function.
/// let _metacall = metacall::initialize().unwrap();
///
///
/// ```
pub fn initialize() -> Result<MetaCallDestroy, MetaCallInitError> {
let code = unsafe { metacall_initialize() };

if code != 0 {
return Err(MetaCallInitError::new(code));
}

Ok(MetaCallDestroy(metacall_destroy))
}

pub fn is_initialized() -> bool {
let initialized = unsafe { metacall_is_initialized(ptr::null_mut()) };

if initialized == 0 {
return true;
}

false
}
20 changes: 12 additions & 8 deletions source/ports/rs_port/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
//!
//! // Load the file (Checkout the loaders module for loading multiple files
//! // or loading from string)
//! loaders::from_single_file("ts", "sum.ts").unwrap();
//! load::from_single_file("ts", "sum.ts").unwrap();
//!
//! // Call the sum function (Also checkout other metacall functions)
//! let sum = metacall::<f64>("sum", [1.0, 2.0]).unwrap();
Expand All @@ -59,34 +59,38 @@
//!
//! ```
pub(crate) mod cast;
pub(crate) mod helpers;
pub(crate) mod parsers;
pub(crate) use macros::private_macros::*;

/// Contains MetaCall loaders from file and memory. Usage example: ...
/// ```
/// // Loading a single file with Nodejs.
/// metacall::loaders::from_single_file("node", "index.js").unwrap();
/// metacall::load::from_single_file("node", "index.js").unwrap();
///
/// // Loading multiple files with Nodejs.
/// metacall::loaders::from_file("node", ["index.js", "main.js"]).unwrap();
/// metacall::load::from_file("node", ["index.js", "main.js"]).unwrap();
///
/// // Loading a string with Nodejs.
/// let script = "function greet() { return 'hi there!' }; module.exports = { greet };";
/// metacall::loaders::from_memory("node", script).unwrap();
/// metacall::load::from_memory("node", script).unwrap();
/// ```
pub mod loaders;
pub mod load;

mod types;
pub use switch::initialize;

#[doc(hidden)]
pub mod macros;

#[doc(hidden)]
pub mod switch;
pub use types::*;

#[doc(hidden)]
mod init;

pub use init::initialize;
pub use init::is_initialized;

#[path = "metacall.rs"]
mod metacall_mod;
pub use metacall_mod::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{
/// Loads a script from a single file. Usage example: ...
/// ```
/// // A Nodejs script
/// metacall::loaders::from_single_file("node", "index.js").unwrap();
/// metacall::load::from_single_file("node", "index.js").unwrap();
/// ```
pub fn from_single_file(
tag: impl ToString,
Expand All @@ -23,7 +23,7 @@ pub fn from_single_file(
/// Loads a script from file. Usage example: ...
/// ```
/// // A Nodejs script
/// metacall::loaders::from_file("node", ["index.js", "main.js"]).unwrap();
/// metacall::load::from_file("node", ["index.js", "main.js"]).unwrap();
/// ```
pub fn from_file(
tag: impl ToString,
Expand Down Expand Up @@ -71,7 +71,7 @@ pub fn from_file(
/// let script = "function greet() { return 'hi there!' }; module.exports = { greet };";
///
/// // A Nodejs script
/// metacall::loaders::from_memory("node", script).unwrap();
/// metacall::load::from_memory("node", script).unwrap();
/// ```
pub fn from_memory(tag: impl ToString, script: impl ToString) -> Result<(), MetaCallLoaderError> {
let script = script.to_string();
Expand Down
8 changes: 4 additions & 4 deletions source/ports/rs_port/src/metacall.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
bindings::{metacall_function, metacall_value_destroy, metacallfv_s},
cstring_enum, parsers,
cast, cstring_enum,
types::{MetaCallError, MetaCallNull, MetaCallValue},
};
use std::ffi::c_void;
Expand All @@ -20,7 +20,7 @@ fn metacall_inner(
return Err(MetaCallError::FunctionNotFound);
}

let mut c_args = parsers::metacallobj_to_raw_args(args);
let mut c_args = cast::metacallobj_to_raw_args(args);
let args_length = c_args.len();

let ret = unsafe { metacallfv_s(c_func, c_args.as_mut_ptr(), args_length) };
Expand All @@ -42,7 +42,7 @@ pub fn metacall_untyped(
func: impl ToString,
args: impl IntoIterator<Item = impl MetaCallValue>,
) -> Result<Box<dyn MetaCallValue>, MetaCallError> {
Ok(parsers::raw_to_metacallobj_untyped(metacall_inner(
Ok(cast::raw_to_metacallobj_untyped(metacall_inner(
func, args,
)?))
}
Expand All @@ -65,7 +65,7 @@ pub fn metacall<T: MetaCallValue>(
func: impl ToString,
args: impl IntoIterator<Item = impl MetaCallValue>,
) -> Result<T, MetaCallError> {
match parsers::raw_to_metacallobj::<T>(metacall_inner(func, args)?) {
match cast::raw_to_metacallobj::<T>(metacall_inner(func, args)?) {
Ok(ret) => Ok(ret),
Err(original) => Err(MetaCallError::FailedCasting(original)),
}
Expand Down
36 changes: 0 additions & 36 deletions source/ports/rs_port/src/switch.rs

This file was deleted.

Loading

0 comments on commit 3b91342

Please sign in to comment.