From da57afb3955a9a415db2e548bc20caf14831e24b Mon Sep 17 00:00:00 2001 From: Byson94 Date: Mon, 20 Oct 2025 12:06:01 +0530 Subject: [PATCH] feat: change fnonce of register_function to fn --- Cargo.toml | 2 +- crates/ewwii/src/plugin.rs | 4 ++-- crates/ewwii_plugin_api/Cargo.toml | 2 +- crates/ewwii_plugin_api/src/lib.rs | 4 ++-- crates/rhai_impl/src/providers/apilib/slib.rs | 18 ++++++++--------- crates/shared_utils/src/slib_store.rs | 20 +++++++++---------- 6 files changed, 25 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7561549..691abdc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ resolver = "2" shared_utils = { version = "0.1.0", path = "crates/shared_utils" } rhai_impl = { version = "0.1.0", path = "crates/rhai_impl" } -ewwii_plugin_api = { version = "0.6.0", path = "crates/ewwii_plugin_api" } +ewwii_plugin_api = { version = "0.6.1", path = "crates/ewwii_plugin_api" } anyhow = "1.0.86" ahash = "0.8.12" diff --git a/crates/ewwii/src/plugin.rs b/crates/ewwii/src/plugin.rs index a8dcf10..2a081ba 100644 --- a/crates/ewwii/src/plugin.rs +++ b/crates/ewwii/src/plugin.rs @@ -36,7 +36,7 @@ impl EwwiiAPI for EwwiiImpl { fn register_function( &self, name: String, - f: Box Dynamic + Send>, + f: Box Dynamic + Send + Sync>, ) -> Result<(), String> { let func_info = (name, f); @@ -73,7 +73,7 @@ impl EwwiiAPI for EwwiiImpl { pub(crate) enum PluginRequest { RhaiEngineAct(Box), - RegisterFunc((String, Box Dynamic + Send>)), + RegisterFunc((String, Box Dynamic + Send + Sync>)), ListWidgetIds(Sender>), WidgetRegistryAct(Box), } diff --git a/crates/ewwii_plugin_api/Cargo.toml b/crates/ewwii_plugin_api/Cargo.toml index 3e37b73..fe6d2a7 100644 --- a/crates/ewwii_plugin_api/Cargo.toml +++ b/crates/ewwii_plugin_api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ewwii_plugin_api" -version = "0.6.0" +version = "0.6.1" authors = ["byson94 "] edition = "2021" license = "GPL-3.0-or-later" diff --git a/crates/ewwii_plugin_api/src/lib.rs b/crates/ewwii_plugin_api/src/lib.rs index 1508d8c..ee70687 100644 --- a/crates/ewwii_plugin_api/src/lib.rs +++ b/crates/ewwii_plugin_api/src/lib.rs @@ -86,7 +86,7 @@ pub trait EwwiiAPI: Send + Sync { /// /// impl Plugin for DummyStructure { /// fn init(&self, host: &dyn EwwiiAPI) { - /// host.register_function(Box::new(|args| { + /// host.register_function("my_func".to_string(), Box::new(|args| { /// // Do stuff /// // - Perform things on the args (if needed) /// // - And return a value @@ -100,7 +100,7 @@ pub trait EwwiiAPI: Send + Sync { fn register_function( &self, name: String, - f: Box rhai::Dynamic + Send>, + f: Box rhai::Dynamic + Send + Sync>, ) -> Result<(), String>; // == Widget Rendering & Logic == // diff --git a/crates/rhai_impl/src/providers/apilib/slib.rs b/crates/rhai_impl/src/providers/apilib/slib.rs index 68f5724..8b2f481 100644 --- a/crates/rhai_impl/src/providers/apilib/slib.rs +++ b/crates/rhai_impl/src/providers/apilib/slib.rs @@ -20,17 +20,17 @@ pub mod slib { /// ```javascript /// import "api::slib" as slib; /// - /// let eg_output = slib::call("my_func", ["foo", 80, true]); + /// let eg_output = slib::call_fn("my_func", ["foo", 80, true]); /// ``` - pub fn call(fn_name: String, args: Array) -> Dynamic { - // TODO: - // - // - Find the function with the name - // - Call that function the args (pass it directly) - + pub fn call_fn(fn_name: String, args: Array) -> Dynamic { match shared_utils::slib_store::call_registered(&fn_name, args) { - Some(d) => d, - None => Dynamic::default() + Ok(Some(d)) => d, + Ok(None) => Dynamic::default(), + Err(e) => { + log::error!("Error calling function: {}", e); + + Dynamic::default() + } } } } diff --git a/crates/shared_utils/src/slib_store.rs b/crates/shared_utils/src/slib_store.rs index 6019ed8..11c1960 100644 --- a/crates/shared_utils/src/slib_store.rs +++ b/crates/shared_utils/src/slib_store.rs @@ -3,28 +3,28 @@ use std::collections::HashMap; use std::sync::Mutex; use rhai::{Array, Dynamic}; -static FUNC_REGISTRY: Lazy Dynamic + Send>>>> = +static FUNC_REGISTRY: Lazy Dynamic + Send + Sync>>>> = Lazy::new(|| Mutex::new(HashMap::new())); pub fn register_functions( name: String, - func: Box Dynamic + Send> + func: Box Dynamic + Send + Sync>, ) -> Result<(), String> { let mut registry = FUNC_REGISTRY .lock() - .map_err(|e| e.to_string())?; // Propagate the error - + .map_err(|e| e.to_string())?; registry.insert(name, func); Ok(()) } +pub fn call_registered(name: &str, args: Array) -> Result, String> { + let registry = FUNC_REGISTRY + .lock() + .map_err(|e| e.to_string())?; - -pub fn call_registered(name: &str, args: Array) -> Option { - let mut registry = FUNC_REGISTRY.lock().unwrap(); - if let Some(func) = registry.remove(name) { - Some(func(args)) + if let Some(func) = registry.get(name) { + Ok(Some(func(args))) } else { - None + Ok(None) } }