small changes in module loading code

This commit is contained in:
Sebastian Moser
2024-12-01 11:05:31 +01:00
parent 27f3a69e85
commit c47403233e
5 changed files with 48 additions and 9 deletions

View File

@@ -41,6 +41,9 @@ pub mod updater;
pub mod msg_thread; pub mod msg_thread;
pub mod module; pub mod module;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
@@ -87,6 +90,8 @@ pub struct InstanceAsync {
impl Instance { impl Instance {
pub fn empty() -> MizeResult<Instance> { pub fn empty() -> MizeResult<Instance> {
let id_pool = Arc::new(Mutex::new(VecStringPool::default())); let id_pool = Arc::new(Mutex::new(VecStringPool::default()));
let namespace_pool_raw = StringPool::default(); let namespace_pool_raw = StringPool::default();
let connections = Arc::new(Mutex::new(Vec::new())); 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 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 self_namespace = Arc::new(Mutex::new(Namespace ( namespace_pool_raw.get("mize.default.namespace") )));
let mut instance = Instance { let mut instance = Instance {
store: Arc::new(Mutex::new(Box::new(MemStore::new()))), store: Arc::new(Mutex::new(Box::new(MemStore::new()))),
connections, subs, id_pool, 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")?)), runtime: Arc::new(Mutex::new(Runtime::new().mize_result_msg("Could not create async runtime")?)),
}; };
let instance_clone = instance.clone(); let instance_clone = instance.clone();
let op_rx_clone = op_rx.clone(); let op_rx_clone = op_rx.clone();
let closure = move || updater_thread(op_rx_clone, &instance_clone); let closure = move || updater_thread(op_rx_clone, &instance_clone);
@@ -140,12 +148,16 @@ impl Instance {
pub fn new() -> MizeResult<Instance> { pub fn new() -> MizeResult<Instance> {
trace!("[ {} ] Instance::new()", "CALL".yellow()); trace!("[ {} ] Instance::new()", "CALL".yellow());
let mut instance = Instance::empty()?; let mut instance = Instance::empty()?;
instance.init(); instance.init();
debug!("instance inited with config: {}", instance.get("0/config")?.as_data_full()?); debug!("instance inited with config: {}", instance.get("0/config")?.as_data_full()?);
return Ok(instance); return Ok(instance);
} }
@@ -306,7 +318,7 @@ impl Instance {
crate::platform::any::fetch_module(self, name) 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 // platform specific init code
crate::platform::any::load_module(self, name, Some(path)) 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<()> { 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 threads_inner = self.threads.lock()?;
let mut next_thread_id = self.next_thread_id.lock()?; let mut next_thread_id = self.next_thread_id.lock()?;
@@ -433,7 +448,10 @@ impl Instance {
#[cfg(feature = "os-target")] #[cfg(feature = "os-target")]
thread::spawn(move || to_spawn()); thread::spawn(move || to_spawn());
//#[cfg(feature = "wasm-target")] #[cfg(feature = "wasm-target")]
{
console_log!("in instance::spawn with wasm target")
}
//NOT WELL SUPPORTED //NOT WELL SUPPORTED
//crate::platform::wasm::wasm_spawn(to_spawn)?; //crate::platform::wasm::wasm_spawn(to_spawn)?;
@@ -522,6 +540,10 @@ impl Instance {
trace!("wait_for_updaater_thread Thread Idle"); trace!("wait_for_updaater_thread Thread Idle");
Ok(()) Ok(())
} }
pub fn report_error(err: MizeError) {
err.log()
}
} }
impl fmt::Debug for Instance { impl fmt::Debug for Instance {

View File

@@ -428,8 +428,7 @@ pub fn get_raw_from_cbor<'a>(value: &'a CborValue, path: Vec<&String>) -> MizeRe
return Ok(string.as_bytes()); return Ok(string.as_bytes());
}, },
other => { other => {
return Err(MizeError::new() return Err(mize_err!("path is empty and the value: '{:?}' is neither Bytes nor Text", other));
.msg("path is empty and the value is neither Bytes nor Text"));
}, },
}; };
}, },

View File

@@ -73,7 +73,7 @@ pub mod platform {
pub use super::os::load_module; pub use super::os::load_module;
#[cfg(feature = "wasm-target")] #[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")))] #[cfg(not(any(feature = "os-target", feature = "wasm-target")))]
pub use super::super::load_module; pub use super::super::load_module;
@@ -96,7 +96,7 @@ pub mod platform {
pub fn instance_init(instance: &mut core::instance::Instance) {} pub fn instance_init(instance: &mut core::instance::Instance) {}
pub fn load_module(instance: &mut core::instance::Instance, name: &str, path: Option<PathBuf>) -> MizeResult<()> { Ok(()) } pub fn load_module(instance: &mut core::instance::Instance, name: &str, path: Option<String>) -> MizeResult<()> { Ok(()) }
pub fn fetch_module(instance: &mut core::instance::Instance, name: &str) -> MizeResult<String> { Ok("oh noooooooooooooo, something went really really wrong, if this ends up in the executable....".to_owned()) } pub fn fetch_module(instance: &mut core::instance::Instance, name: &str) -> MizeResult<String> { Ok("oh noooooooooooooo, something went really really wrong, if this ends up in the executable....".to_owned()) }

View File

@@ -235,9 +235,19 @@ pub fn fetch_module(instance: &mut Instance, module_name: &str) -> MizeResult<St
} }
} }
pub fn load_module(instance: &mut Instance, module_name: &str, path: Option<PathBuf>) -> MizeResult<()> { pub fn load_module(instance: &mut Instance, module_name: &str, path: Option<String>) -> 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)? }; let lib = unsafe { libloading::Library::new(module_path)? };

View File

@@ -52,13 +52,17 @@ macro_rules! console_log {
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
pub fn wasm_instance_init(instance: &mut Instance) -> MizeResult<()> { pub fn wasm_instance_init(instance: &mut Instance) -> MizeResult<()> {
console_log!("Hello world from wasm_instance_init!!!!!!!!!!"); console_log!("Hello world from wasm_instance_init!!!!!!!!!!");
Ok(()) Ok(())
} }
pub fn load_module(instance: &mut Instance, name: &str, path: Option<String>) -> MizeResult<()> {
console_log!("loading module: {}", name);
Ok(())
}
#[wasm_bindgen] #[wasm_bindgen]
pub struct JsInstance { pub struct JsInstance {
inner: NonNull<Instance>, inner: NonNull<Instance>,
@@ -76,6 +80,8 @@ pub struct JsItemHandle {
impl JsInstance { impl JsInstance {
#[wasm_bindgen(constructor)] #[wasm_bindgen(constructor)]
pub fn new() -> JsInstance { pub fn new() -> JsInstance {
panic::set_hook(Box::new(console_error_panic_hook::hook)); panic::set_hook(Box::new(console_error_panic_hook::hook));
let instance = match Instance::new() { let instance = match Instance::new() {
@@ -85,6 +91,8 @@ impl JsInstance {
panic!() panic!()
}, },
}; };
let mut js_instance = JsInstance { inner: NonNull::from(Box::leak(Box::new(instance))) }; let mut js_instance = JsInstance { inner: NonNull::from(Box::leak(Box::new(instance))) };
return js_instance; return js_instance;
} }