Working-ish plugin thingy
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
Generated
+1720
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "voxa-server"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
anyhow = "1.0.99"
|
||||||
|
wasmtime = "36.0.2"
|
||||||
+20
@@ -0,0 +1,20 @@
|
|||||||
|
pub trait Plugin {
|
||||||
|
fn init(&mut self);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! export_plugin {
|
||||||
|
($plugin_type:ty) => {
|
||||||
|
static mut PLUGIN: Option<$plugin_type> = None;
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn init() {
|
||||||
|
unsafe {
|
||||||
|
PLUGIN = Some(<$plugin_type>::default());
|
||||||
|
if let Some(plugin) = PLUGIN.as_mut() {
|
||||||
|
plugin.init();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
+20
@@ -0,0 +1,20 @@
|
|||||||
|
use wasmtime::*;
|
||||||
|
|
||||||
|
fn load_plugin(wasm_path: &str) -> anyhow::Result<()> {
|
||||||
|
let engine = Engine::default();
|
||||||
|
let mut linker = Linker::new(&engine);
|
||||||
|
let mut store = Store::new(&engine, ());
|
||||||
|
|
||||||
|
let module = Module::from_file(&engine, wasm_path)?;
|
||||||
|
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<()> {
|
||||||
|
load_plugin("plugins/test_plugin.wasm")?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
Generated
+7
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "test-plugin"
|
||||||
|
version = "0.1.0"
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "test-plugin"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
crate-type = ["cdylib"]
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
pub trait Plugin {
|
||||||
|
fn init(&mut self);
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! export_plugin {
|
||||||
|
($plugin_type:ty) => {
|
||||||
|
static PLUGIN: std::sync::Mutex<Option<$plugin_type>> = std::sync::Mutex::new(None);
|
||||||
|
|
||||||
|
#[unsafe(no_mangle)]
|
||||||
|
pub extern "C" fn init() {
|
||||||
|
let mut plugin = PLUGIN.lock().unwrap();
|
||||||
|
*plugin = Some(<$plugin_type>::default());
|
||||||
|
if let Some(plugin) = plugin.as_mut() {
|
||||||
|
plugin.init();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct MyPlugin;
|
||||||
|
|
||||||
|
impl Plugin for MyPlugin {
|
||||||
|
fn init(&mut self) {
|
||||||
|
println!("MyPlugin initialized!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export_plugin!(MyPlugin);
|
||||||
Reference in New Issue
Block a user