From 4b1f305b077bb57f5118bc7a86ea9f13dc366ced Mon Sep 17 00:00:00 2001 From: kristof Date: Thu, 3 Apr 2025 12:49:08 +0200 Subject: [PATCH] ... --- README.md | 5 +- .../loadscripts/test_dynamic_loading.rs | 56 +++++++++++++++++-- rhai_engine/src/rhailoader/script_manager.rs | 23 ++++++++ 3 files changed, 78 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 05e682b..47dfa9e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ # rhaj -DSL's for our ecosystem \ No newline at end of file +```bash +#tests, following one shows how we can dynamically load the functions from a set of rhai scripts +cargo run --bin test_dynamic_loading +``` \ No newline at end of file diff --git a/rhai_engine/examples/loadscripts/test_dynamic_loading.rs b/rhai_engine/examples/loadscripts/test_dynamic_loading.rs index 74a6279..b0e3378 100644 --- a/rhai_engine/examples/loadscripts/test_dynamic_loading.rs +++ b/rhai_engine/examples/loadscripts/test_dynamic_loading.rs @@ -51,11 +51,23 @@ pub fn main() -> Result<(), String> { // Test test_utils functions test_function(&script_manager, "test_utils:reverse_string", vec![Dynamic::from("hello world")])?; - test_function(&script_manager, "test_utils:count_words", - vec![Dynamic::from("this is a test sentence")])?; + // Using direct script execution + println!("\nExecuting scripts directly:"); - test_function(&script_manager, "test_utils:factorial", - vec![Dynamic::from(5)])?; + // Direct function call with arguments as a script + execute_script(&script_manager, "test_utils:count_words(\"this is a test sentence\")")?; + + // Direct script with expression + execute_script(&script_manager, "test_utils:factorial(5)")?; + + // Run a more complex script + let complex_script = r#" + let result = test_utils:count_words("Count the words in this sentence"); + print("Word count: " + result); + result * 2 // Return double the word count + "#; + + execute_script_with_result(&script_manager, complex_script)?; println!("\n=== DYNAMIC FUNCTION LOADING TEST COMPLETE ===\n"); @@ -91,9 +103,43 @@ fn display_functions_by_script(scripts: &HashMap>) { } } -// Helper function to test a function and display its result +// Helper function to test a function call and display its result fn test_function(script_manager: &ScriptManager, name: &str, args: Vec) -> Result<(), String> { + // Call the function with the provided arguments let result = script_manager.call_function(name, args)?; + + // Print the result println!(" {}() => {}", name, result); + + Ok(()) +} + +// Execute a script directly and display the result +fn execute_script(script_manager: &ScriptManager, script: &str) -> Result<(), String> { + println!(" Executing: {}", script); + + // Use the run method to execute the script + script_manager.run(script)?; + + // For functions that return a value, we can use eval to get the result + if let Ok(result) = script_manager.eval::(script) { + println!(" Result: {}", result); + } + + Ok(()) +} + +// Execute a script and return its result +fn execute_script_with_result(script_manager: &ScriptManager, script: &str) -> Result<(), String> { + println!(" Executing script with result:"); + println!("```"); + println!("{}", script); + println!("```"); + + // Use the eval method to execute the script and get the result + let result = script_manager.eval::(script)?; + + println!(" Result: {}", result); + Ok(()) } \ No newline at end of file diff --git a/rhai_engine/src/rhailoader/script_manager.rs b/rhai_engine/src/rhailoader/script_manager.rs index c1f43e1..09bdd8a 100644 --- a/rhai_engine/src/rhailoader/script_manager.rs +++ b/rhai_engine/src/rhailoader/script_manager.rs @@ -290,4 +290,27 @@ impl ScriptManager { pub fn get_function_names(&self) -> Vec { self.functions.keys().cloned().collect() } + + /// Execute a script snippet directly + pub fn eval(&self, script: &str) -> Result + where T: 'static + Clone { + // Create a configured engine for execution + let engine = create_standard_engine(); + let mut scope = Scope::new(); + + // Execute the script and return the result + engine.eval_with_scope::(&mut scope, script) + .map_err(|e| format!("Error evaluating script: {}", e)) + } + + /// Run a script without returning a value + pub fn run(&self, script: &str) -> Result<(), String> { + // Create a configured engine for execution + let engine = create_standard_engine(); + let mut scope = Scope::new(); + + // Run the script + engine.run_with_scope(&mut scope, script) + .map_err(|e| format!("Error running script: {}", e)) + } } \ No newline at end of file