Better plugin loader

This commit is contained in:
2025-08-29 00:57:16 +02:00
parent fb5ca32687
commit 8cffa11669
3 changed files with 80 additions and 26 deletions
+9 -8
View File
@@ -1,19 +1,20 @@
pub trait Plugin { pub mod loader;
pub trait PluginApi {
fn init(&mut self); fn init(&mut self);
} }
#[macro_export] #[macro_export]
macro_rules! export_plugin { macro_rules! export_plugin {
($plugin_type:ty) => { ($plugin_type:ty) => {
static mut PLUGIN: Option<$plugin_type> = None; static PLUGIN: std::sync::Mutex<Option<$plugin_type>> = std::sync::Mutex::new(None);
#[no_mangle] #[unsafe(no_mangle)]
pub extern "C" fn init() { pub extern "C" fn init() {
unsafe { let mut plugin = PLUGIN.lock().unwrap();
PLUGIN = Some(<$plugin_type>::default()); *plugin = Some(<$plugin_type>::default());
if let Some(plugin) = PLUGIN.as_mut() { if let Some(plugin) = plugin.as_mut() {
plugin.init(); plugin.init();
}
} }
} }
}; };
+63
View File
@@ -0,0 +1,63 @@
use std::path::Path;
use wasmtime::*;
use wasmtime_wasi::{WasiCtxBuilder, preview1::WasiP1Ctx};
use crate::PluginApi;
pub enum PluginInstance {
Wasm(Instance, Store<WasiP1Ctx>),
StatiC(Box<dyn PluginApi>),
}
pub fn load_plugin(wasm_path: &str) -> anyhow::Result<PluginInstance> {
let engine = Engine::default();
let mut store = Store::new(
&engine,
WasiCtxBuilder::new()
.inherit_stdout()
.inherit_stderr()
.build_p1(),
);
let module = Module::from_file(&engine, wasm_path)?;
let mut linker = Linker::new(&engine);
wasmtime_wasi::preview1::add_to_linker_sync(&mut linker, |s| s)?;
let instance = linker.instantiate(&mut store, &module)?;
Ok(PluginInstance::Wasm(instance, store))
}
pub fn load_plugins(arr: &mut Vec<PluginInstance>, path: &Path) {
if path.is_dir() {
for entry in std::fs::read_dir(path).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.extension().and_then(|s| s.to_str()) == Some("wasm") {
println!("Loading plugin: {:?}", path);
match load_plugin(path.to_str().unwrap()) {
Ok(plugin) => {
arr.push(plugin);
}
Err(e) => eprintln!("Failed to load plugin {:?}: {}", path, e),
}
}
}
} else {
eprintln!("{:?} is not a directory", path);
}
}
impl PluginApi for PluginInstance {
fn init(&mut self) {
match self {
PluginInstance::Wasm(instance, store) => {
let init = instance
.get_typed_func::<(), ()>(&mut *store, "init")
.unwrap();
init.call(store, ()).unwrap();
}
PluginInstance::StatiC(plugin) => plugin.init(),
}
}
}
+8 -18
View File
@@ -1,22 +1,12 @@
use wasmtime::*; use voxa_server::PluginApi;
use wasmtime_wasi::WasiCtxBuilder;
fn load_plugin(wasm_path: &str) -> anyhow::Result<()> {
let engine = Engine::default();
let mut store = Store::new(&engine, WasiCtxBuilder::new().inherit_stdout().inherit_stderr().build_p1());
let module = Module::from_file(&engine, wasm_path)?;
let mut linker = Linker::new(&engine);
wasmtime_wasi::preview1::add_to_linker_sync(&mut linker, |s| s)?;
let instance = linker.instantiate(&mut store, &module)?;
let init = instance.get_typed_func::<(), ()>(&mut store, "init")?;
init.call(&mut store, ())?;
Ok(())
}
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
load_plugin("plugins/test_plugin.wasm")?; let mut plugins = Vec::new();
voxa_server::loader::load_plugins(&mut plugins, std::path::Path::new("./plugins"));
for plugin in &mut plugins {
plugin.init();
}
Ok(()) Ok(())
} }