diff --git a/Cargo.toml b/Cargo.toml index c15b7b2..db4c6da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,5 +5,9 @@ edition = "2024" [dependencies] anyhow = "1.0.99" -wasmtime = "36.0.2" -wasmtime-wasi = { version = "36.0.2", features = ["preview1"] } +wasmtime = { version = "36.0.2", optional = true } +wasmtime-wasi = { version = "36.0.2", features = ["preview1"], optional = true } + +[features] +default = ["loader"] +loader = ["wasmtime", "wasmtime-wasi"] diff --git a/src/lib.rs b/src/lib.rs index 0ed26cc..8ab748c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,36 @@ +#[cfg(feature = "loader")] +use wasmtime::{Instance, Store}; +#[cfg(feature = "loader")] +use wasmtime_wasi::preview1::WasiP1Ctx; + +#[cfg(feature = "loader")] pub mod loader; pub trait PluginApi { fn init(&mut self); } +pub enum PluginInstance { + #[cfg(feature = "loader")] + Wasm(Instance, Store), + StatiC(Box), +} + +impl PluginApi for PluginInstance { + fn init(&mut self) { + match self { + #[cfg(feature = "loader")] + PluginInstance::Wasm(instance, store) => { + let init = instance + .get_typed_func::<(), ()>(&mut *store, "init") + .unwrap(); + init.call(store, ()).unwrap(); + } + PluginInstance::StatiC(plugin) => plugin.init(), + } + } +} + #[macro_export] macro_rules! export_plugin { ($plugin_type:ty) => { diff --git a/src/loader.rs b/src/loader.rs index 1fb7637..4c51ce8 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -1,14 +1,9 @@ use std::path::Path; use wasmtime::*; -use wasmtime_wasi::{WasiCtxBuilder, preview1::WasiP1Ctx}; +use wasmtime_wasi::WasiCtxBuilder; -use crate::PluginApi; - -pub enum PluginInstance { - Wasm(Instance, Store), - StatiC(Box), -} +use crate::PluginInstance; pub fn load_plugin(wasm_path: &str) -> anyhow::Result { let engine = Engine::default(); @@ -47,17 +42,3 @@ pub fn load_plugins(arr: &mut Vec, path: &Path) { 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(), - } - } -} diff --git a/test-plugin/Cargo.lock b/test-plugin/Cargo.lock index aa024b4..7436edb 100644 --- a/test-plugin/Cargo.lock +++ b/test-plugin/Cargo.lock @@ -2,6 +2,22 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "anyhow" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0674a1ddeecb70197781e945de4b3b8ffb61fa939a5597bcf48503737663100" + [[package]] name = "test-plugin" version = "0.1.0" +dependencies = [ + "voxa-server", +] + +[[package]] +name = "voxa-server" +version = "0.1.0" +dependencies = [ + "anyhow", +] diff --git a/test-plugin/Cargo.toml b/test-plugin/Cargo.toml index f8aa64c..7f3c64b 100644 --- a/test-plugin/Cargo.toml +++ b/test-plugin/Cargo.toml @@ -4,4 +4,7 @@ version = "0.1.0" edition = "2024" [lib] -crate-type = ["cdylib"] \ No newline at end of file +crate-type = ["cdylib"] + +[dependencies] +voxa-server = { path = "../", default-features = false } \ No newline at end of file diff --git a/test-plugin/src/lib.rs b/test-plugin/src/lib.rs index ba46784..0b44d44 100644 --- a/test-plugin/src/lib.rs +++ b/test-plugin/src/lib.rs @@ -1,26 +1,9 @@ -pub trait Plugin { - fn init(&mut self); -} - -macro_rules! export_plugin { - ($plugin_type:ty) => { - static PLUGIN: std::sync::Mutex> = 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(); - } - } - }; -} +use voxa_server::{PluginApi, export_plugin}; #[derive(Default)] pub struct MyPlugin; -impl Plugin for MyPlugin { +impl PluginApi for MyPlugin { fn init(&mut self) { println!("MyPlugin initialized!"); }