Optimised thingy

This commit is contained in:
2025-08-29 01:15:40 +02:00
parent 8cffa11669
commit 68bf864a52
6 changed files with 57 additions and 43 deletions
+27
View File
@@ -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<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_rules! export_plugin {
($plugin_type:ty) => {
+2 -21
View File
@@ -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<WasiP1Ctx>),
StatiC(Box<dyn PluginApi>),
}
use crate::PluginInstance;
pub fn load_plugin(wasm_path: &str) -> anyhow::Result<PluginInstance> {
let engine = Engine::default();
@@ -47,17 +42,3 @@ pub fn load_plugins(arr: &mut Vec<PluginInstance>, 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(),
}
}
}