Optimised thingy
This commit is contained in:
+6
-2
@@ -5,5 +5,9 @@ edition = "2024"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.99"
|
anyhow = "1.0.99"
|
||||||
wasmtime = "36.0.2"
|
wasmtime = { version = "36.0.2", optional = true }
|
||||||
wasmtime-wasi = { version = "36.0.2", features = ["preview1"] }
|
wasmtime-wasi = { version = "36.0.2", features = ["preview1"], optional = true }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["loader"]
|
||||||
|
loader = ["wasmtime", "wasmtime-wasi"]
|
||||||
|
|||||||
+27
@@ -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 mod loader;
|
||||||
|
|
||||||
pub trait PluginApi {
|
pub trait PluginApi {
|
||||||
fn init(&mut self);
|
fn init(&mut self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum PluginInstance {
|
||||||
|
#[cfg(feature = "loader")]
|
||||||
|
Wasm(Instance, Store<WasiP1Ctx>),
|
||||||
|
StatiC(Box<dyn PluginApi>),
|
||||||
|
}
|
||||||
|
|
||||||
|
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_export]
|
||||||
macro_rules! export_plugin {
|
macro_rules! export_plugin {
|
||||||
($plugin_type:ty) => {
|
($plugin_type:ty) => {
|
||||||
|
|||||||
+2
-21
@@ -1,14 +1,9 @@
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use wasmtime::*;
|
use wasmtime::*;
|
||||||
use wasmtime_wasi::{WasiCtxBuilder, preview1::WasiP1Ctx};
|
use wasmtime_wasi::WasiCtxBuilder;
|
||||||
|
|
||||||
use crate::PluginApi;
|
use crate::PluginInstance;
|
||||||
|
|
||||||
pub enum PluginInstance {
|
|
||||||
Wasm(Instance, Store<WasiP1Ctx>),
|
|
||||||
StatiC(Box<dyn PluginApi>),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn load_plugin(wasm_path: &str) -> anyhow::Result<PluginInstance> {
|
pub fn load_plugin(wasm_path: &str) -> anyhow::Result<PluginInstance> {
|
||||||
let engine = Engine::default();
|
let engine = Engine::default();
|
||||||
@@ -47,17 +42,3 @@ pub fn load_plugins(arr: &mut Vec<PluginInstance>, path: &Path) {
|
|||||||
eprintln!("{:?} is not a directory", 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(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Generated
+16
@@ -2,6 +2,22 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anyhow"
|
||||||
|
version = "1.0.99"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b0674a1ddeecb70197781e945de4b3b8ffb61fa939a5597bcf48503737663100"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "test-plugin"
|
name = "test-plugin"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"voxa-server",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "voxa-server"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
]
|
||||||
|
|||||||
@@ -4,4 +4,7 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
crate-type = ["cdylib"]
|
crate-type = ["cdylib"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
voxa-server = { path = "../", default-features = false }
|
||||||
+2
-19
@@ -1,26 +1,9 @@
|
|||||||
pub trait Plugin {
|
use voxa_server::{PluginApi, export_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)]
|
#[derive(Default)]
|
||||||
pub struct MyPlugin;
|
pub struct MyPlugin;
|
||||||
|
|
||||||
impl Plugin for MyPlugin {
|
impl PluginApi for MyPlugin {
|
||||||
fn init(&mut self) {
|
fn init(&mut self) {
|
||||||
println!("MyPlugin initialized!");
|
println!("MyPlugin initialized!");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user