From c47403233e927e0a2e0dbec9e1febbba3c73f233 Mon Sep 17 00:00:00 2001 From: Sebastian Moser Date: Sun, 1 Dec 2024 11:05:31 +0100 Subject: [PATCH] small changes in module loading code --- src/core/instance/mod.rs | 26 ++++++++++++++++++++++++-- src/core/item.rs | 3 +-- src/lib.rs | 4 ++-- src/platform/os/mod.rs | 14 ++++++++++++-- src/platform/wasm/mod.rs | 10 +++++++++- 5 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/core/instance/mod.rs b/src/core/instance/mod.rs index 4186493..3a10f2b 100644 --- a/src/core/instance/mod.rs +++ b/src/core/instance/mod.rs @@ -41,6 +41,9 @@ pub mod updater; pub mod msg_thread; pub mod module; + + + #[cfg(test)] mod tests; @@ -87,6 +90,8 @@ pub struct InstanceAsync { impl Instance { pub fn empty() -> MizeResult { + + let id_pool = Arc::new(Mutex::new(VecStringPool::default())); let namespace_pool_raw = StringPool::default(); let connections = Arc::new(Mutex::new(Vec::new())); @@ -97,6 +102,7 @@ impl Instance { let namespace = Arc::new(Mutex::new(Namespace ( namespace_pool_raw.get("mize.default.namespace") ))); let self_namespace = Arc::new(Mutex::new(Namespace ( namespace_pool_raw.get("mize.default.namespace") ))); + let mut instance = Instance { store: Arc::new(Mutex::new(Box::new(MemStore::new()))), connections, subs, id_pool, @@ -113,6 +119,8 @@ impl Instance { runtime: Arc::new(Mutex::new(Runtime::new().mize_result_msg("Could not create async runtime")?)), }; + + let instance_clone = instance.clone(); let op_rx_clone = op_rx.clone(); let closure = move || updater_thread(op_rx_clone, &instance_clone); @@ -140,12 +148,16 @@ impl Instance { pub fn new() -> MizeResult { trace!("[ {} ] Instance::new()", "CALL".yellow()); + let mut instance = Instance::empty()?; + instance.init(); + debug!("instance inited with config: {}", instance.get("0/config")?.as_data_full()?); + return Ok(instance); } @@ -306,7 +318,7 @@ impl Instance { crate::platform::any::fetch_module(self, name) } - pub fn load_module_at(&mut self, name: &str, path: PathBuf) -> MizeResult<()> { + pub fn load_module_at(&mut self, name: &str, path: String) -> MizeResult<()> { // platform specific init code crate::platform::any::load_module(self, name, Some(path)) } @@ -405,6 +417,9 @@ impl Instance { } pub fn spawn(&mut self, name: &str, func: impl FnOnce() -> MizeResult<()> + Send + 'static) -> MizeResult<()> { + + console_log!("in instance::spawn with wasm target"); + let mut threads_inner = self.threads.lock()?; let mut next_thread_id = self.next_thread_id.lock()?; @@ -433,7 +448,10 @@ impl Instance { #[cfg(feature = "os-target")] thread::spawn(move || to_spawn()); - //#[cfg(feature = "wasm-target")] + #[cfg(feature = "wasm-target")] + { + console_log!("in instance::spawn with wasm target") + } //NOT WELL SUPPORTED //crate::platform::wasm::wasm_spawn(to_spawn)?; @@ -522,6 +540,10 @@ impl Instance { trace!("wait_for_updaater_thread Thread Idle"); Ok(()) } + + pub fn report_error(err: MizeError) { + err.log() + } } impl fmt::Debug for Instance { diff --git a/src/core/item.rs b/src/core/item.rs index e37158c..4508268 100644 --- a/src/core/item.rs +++ b/src/core/item.rs @@ -428,8 +428,7 @@ pub fn get_raw_from_cbor<'a>(value: &'a CborValue, path: Vec<&String>) -> MizeRe return Ok(string.as_bytes()); }, other => { - return Err(MizeError::new() - .msg("path is empty and the value is neither Bytes nor Text")); + return Err(mize_err!("path is empty and the value: '{:?}' is neither Bytes nor Text", other)); }, }; }, diff --git a/src/lib.rs b/src/lib.rs index 711a212..693e83e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,7 +73,7 @@ pub mod platform { pub use super::os::load_module; #[cfg(feature = "wasm-target")] - pub use super::super::load_module; + pub use super::wasm::load_module; #[cfg(not(any(feature = "os-target", feature = "wasm-target")))] pub use super::super::load_module; @@ -96,7 +96,7 @@ pub mod platform { pub fn instance_init(instance: &mut core::instance::Instance) {} -pub fn load_module(instance: &mut core::instance::Instance, name: &str, path: Option) -> MizeResult<()> { Ok(()) } +pub fn load_module(instance: &mut core::instance::Instance, name: &str, path: Option) -> MizeResult<()> { Ok(()) } pub fn fetch_module(instance: &mut core::instance::Instance, name: &str) -> MizeResult { Ok("oh noooooooooooooo, something went really really wrong, if this ends up in the executable....".to_owned()) } diff --git a/src/platform/os/mod.rs b/src/platform/os/mod.rs index 69651a1..3a07742 100644 --- a/src/platform/os/mod.rs +++ b/src/platform/os/mod.rs @@ -235,9 +235,19 @@ pub fn fetch_module(instance: &mut Instance, module_name: &str) -> MizeResult) -> MizeResult<()> { +pub fn load_module(instance: &mut Instance, module_name: &str, path: Option) -> MizeResult<()> { - let module_path = format!("{}/lib/libmmize_module_{}.so", fetch_module(instance, module_name)?, module_name); + let module_dir_from_config = instance.get(format!("0/config/module_dir/{}", module_name)); + + let module_path = if path.is_some() { + format!("{}/lib/libmize_module_{}.so", path.unwrap(), module_name) + + } else if module_dir_from_config.is_ok() { + format!("{}/lib/libmize_module_{}.so", module_dir_from_config.unwrap().value_string()?, module_name) + + } else { + format!("{}/lib/libmmize_module_{}.so", fetch_module(instance, module_name)?, module_name) + }; let lib = unsafe { libloading::Library::new(module_path)? }; diff --git a/src/platform/wasm/mod.rs b/src/platform/wasm/mod.rs index 5d95308..f6ee533 100644 --- a/src/platform/wasm/mod.rs +++ b/src/platform/wasm/mod.rs @@ -52,13 +52,17 @@ macro_rules! console_log { static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; - pub fn wasm_instance_init(instance: &mut Instance) -> MizeResult<()> { console_log!("Hello world from wasm_instance_init!!!!!!!!!!"); Ok(()) } +pub fn load_module(instance: &mut Instance, name: &str, path: Option) -> MizeResult<()> { + console_log!("loading module: {}", name); + Ok(()) +} + #[wasm_bindgen] pub struct JsInstance { inner: NonNull, @@ -76,6 +80,8 @@ pub struct JsItemHandle { impl JsInstance { #[wasm_bindgen(constructor)] pub fn new() -> JsInstance { + + panic::set_hook(Box::new(console_error_panic_hook::hook)); let instance = match Instance::new() { @@ -85,6 +91,8 @@ impl JsInstance { panic!() }, }; + + let mut js_instance = JsInstance { inner: NonNull::from(Box::leak(Box::new(instance))) }; return js_instance; }