Make unshell-lib, and module system with components.

This commit is contained in:
Michael Mikovsky
2025-11-08 14:56:03 -07:00
parent 8f33945633
commit 3dba32264c
27 changed files with 461 additions and 311 deletions
+16
View File
@@ -0,0 +1,16 @@
#![allow(improper_ctypes_definitions)]
use log::{LevelFilter, Log, SetLoggerError};
#[allow(dead_code)]
pub type SetupLogger =
extern "C" fn(logger: &'static dyn Log, level: LevelFilter) -> Result<(), SetLoggerError>;
#[unsafe(no_mangle)]
pub extern "C" fn setup_logger(
logger: &'static dyn log::Log,
level: log::LevelFilter,
) -> Result<(), log::SetLoggerError> {
log::set_max_level(level);
log::set_logger(logger)
}
+88
View File
@@ -0,0 +1,88 @@
use std::{
collections::HashMap,
sync::{Arc, Mutex},
thread,
time::Duration,
};
use crate::{Component, ModuleRuntime, module::Module};
pub struct Manager {
modules: Vec<Module>,
components: HashMap<&'static str, Box<dyn Component>>,
}
// static mut MANAGER_RUNTIME: Option<Arc<Mutex<Manager>>> = None;
impl Manager {
/// Create Manager, and run initilization for each Module
#[allow(static_mut_refs)]
pub fn run<'a>(modules: Vec<Module>) {
let this: Self = Self::load_modules(modules);
let components = this.components.clone();
let this = Arc::new(Mutex::new(this));
let mut runtimes: Vec<Box<dyn ModuleRuntime>> = Vec::new();
for (name, component) in components {
let module_runtime = component.start_runtime(this.clone());
if let Some(module_runtime) = module_runtime {
info!("Initialized {}", name);
runtimes.push(module_runtime);
}
}
Self::join(&mut runtimes);
}
pub fn load_modules<'a>(modules: Vec<Module>) -> Self {
let module_count = modules.len();
let mut this = Self {
modules,
components: HashMap::new(),
};
// let mut runtimes = Vec::new();
for i in 0..module_count {
info!("Importing module {}", i);
// let this_lock = .unwrap();
let component_func = if let Ok(component_func) = this.modules[i]
.get_symbol::<fn() -> HashMap<&'static str, Box<dyn Component>>>(b"get_components")
{
component_func
} else {
warn!("get_components function not found");
continue;
};
let components = component_func();
info!("[{i}] Loaded {} components", components.len());
this.components.extend(components);
}
this
}
/// Iterateratively loop through all runtimes, until all are finished executing
pub fn join(runtimes: &mut Vec<Box<dyn ModuleRuntime>>) {
// let mut len = runtimes.len().clone();
while runtimes.len() > 0 {
runtimes.retain(|runtime| runtime.is_running());
thread::sleep(Duration::from_micros(100));
}
}
// pub extern "C" fn test1234(&self, float: f32) {
// info!("Manager Test Sucsessfull! {}", float.powf(2.));
// }
// #[allow(static_mut_refs, improper_ctypes_definitions)]
// pub extern "C" fn get_manager() -> Arc<Mutex<Manager>> {
// unsafe { MANAGER_RUNTIME.clone().unwrap() }
// }
}
+68
View File
@@ -0,0 +1,68 @@
mod logger;
mod manager;
mod module;
// use std::any::Any;
pub use logger::setup_logger;
pub use manager::Manager;
pub use module::Module;
pub trait Interface {
fn as_any(self: Box<Self>) -> Box<dyn std::any::Any>;
}
/// "Module Interface" helper macro that creates a struct with function pointers
/// Useful for defining and requiring modules' functions accross FFI boundry.
#[macro_export]
macro_rules! module_interface {
($(#[$struct_meta:meta])* $interface_name:ident { $($(#[$fn_meta:meta])* fn $fn_name:ident $(<$($gen:ident),+ $(,)?>)?($($arg:ident : $ty:ty),* $(,)?) $(-> $ret:ty)? $(where $($where_clause:tt)*)?);* $(;)? }) => {
#[repr(C)]
#[allow(non_camel_case_types)]
#[derive(Clone, Copy)]
#[allow(improper_ctypes_definitions)]
$(#[$struct_meta])*
pub struct $interface_name {
$(
// This line will FAIL TO COMPILE if you use generics in the macro input.
// You MUST use concrete types like *mut c_void for "generic" data.
$fn_name: extern "C" fn($($ty),*) $(-> $ret)?,
)*
}
impl $interface_name {
$(
#[inline(always)]
$(#[$fn_meta])* // Propagate function attributes
// This is the fix for the `impl` block.
// It adds the captured generics and where-clause to the wrapper function.
pub fn $fn_name $(<$($gen),+>)? (&self, $($arg: $ty),*) $(-> $ret)?
$(where $($where_clause)*)?
{
(self.$fn_name)($($arg),*)
}
)*
/// Create from raw function pointers
///
/// # Safety
///
/// The caller must ensure all function pointers are valid and have
/// the correct signatures
pub fn from_raw(
$($fn_name: extern "C" fn($($ty),*) $(-> $ret)?),*
) -> Self {
Self {
$($fn_name),*
}
}
}
impl crate::module::Interface for $interface_name {
fn as_any(self: Box<Self>) -> Box<dyn std::any::Any> {
self
}
}
};
}
+46
View File
@@ -0,0 +1,46 @@
use libloading::{Library, Symbol};
use crate::{ModuleError, module::logger::SetupLogger};
pub struct Module {
// name: String,
lib: Library,
}
impl Module {
pub fn new(path: &str) -> Result<Self, ModuleError> {
let lib = unsafe { Library::new(&path) }.map_err(|e| ModuleError::LibLoadingError(e))?;
let this = Self {
// name: path.to_owned(),
lib,
};
if let Ok(setup_logger) = this.get_symbol::<SetupLogger>(b"setup_logger") {
setup_logger(log::logger(), log::max_level()).map_err(|e| ModuleError::LogError(e))?;
} else {
warn!("setup_logger not found");
}
Ok(this)
}
pub fn get_symbol<T>(&self, symbol: &[u8]) -> Result<Symbol<'_, T>, ModuleError> {
let symbol = unsafe { self.lib.get::<T>(symbol) }
.map_err(|e| ModuleError::LinkError(format!("Failed to load symbol: {}", e)))?;
Ok(symbol)
}
// pub fn get_interface<T>(&self) -> Result<T, ModuleError> {
// if let Ok(interface_function) = self.get_symbol::<fn() -> T>(b"interface") {
// Ok(interface_function())
// } else {
// Err(ModuleError::LinkError(format!(
// "Interface function not found!"
// )))
// }
// }
}
// extern "C" fn test1234() {
// info!("Test1234!");
// }