Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 53 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ lunatic-distributed-api = { workspace = true }
lunatic-error-api = { workspace = true }
lunatic-messaging-api = { workspace = true }
lunatic-networking-api = { workspace = true }
lunatic-plugin-internal = { workspace = true }
lunatic-process = { workspace = true }
lunatic-process-api = { workspace = true }
lunatic-registry-api = { workspace = true }
Expand Down Expand Up @@ -73,13 +74,19 @@ members = [
"crates/lunatic-distributed",
"crates/lunatic-error-api",
"crates/lunatic-messaging-api",
"crates/lunatic-plugin-internal",
"crates/lunatic-plugin",
"crates/lunatic-process-api",
"crates/lunatic-process",
"crates/lunatic-registry-api",
"crates/lunatic-stdout-capture",
"crates/lunatic-timer-api",
"crates/lunatic-version-api",
"crates/lunatic-wasi-api",

# Examples
"examples/counter-guest",
"examples/counter-plugin",
]

[workspace.dependencies]
Expand All @@ -90,9 +97,12 @@ lunatic-distributed-api = { path = "crates/lunatic-distributed-api", version = "
lunatic-error-api = { path = "crates/lunatic-error-api", version = "0.12" }
lunatic-messaging-api = { path = "crates/lunatic-messaging-api", version = "0.12" }
lunatic-networking-api = { path = "crates/lunatic-networking-api", version = "0.12" }
lunatic-plugin = { path = "crates/lunatic-plugin", version = "0.12" }
lunatic-plugin-internal = { path = "crates/lunatic-plugin-internal", version = "0.12" }
lunatic-process = { path = "crates/lunatic-process", version = "0.12" }
lunatic-process-api = { path = "crates/lunatic-process-api", version = "0.12" }
lunatic-registry-api = { path = "crates/lunatic-registry-api", version = "0.12" }
lunatic-runtime = { path = ".", version = "0.12" }
lunatic-stdout-capture = { path = "crates/lunatic-stdout-capture", version = "0.12" }
lunatic-timer-api = { path = "crates/lunatic-timer-api", version = "0.12" }
lunatic-version-api = { path = "crates/lunatic-version-api", version = "0.12" }
Expand Down
4 changes: 3 additions & 1 deletion benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ fn criterion_benchmark(c: &mut Criterion) {
let config = Arc::new(DefaultProcessConfig::default());
let wasmtime_config = default_config();
let runtime = WasmtimeRuntime::new(&wasmtime_config).unwrap();
let plugins = Arc::new(vec![]);

let raw_module = wat::parse_file("./wat/hello.wat").unwrap();
let module = Arc::new(
runtime
.compile_module::<DefaultProcessState>(raw_module.into())
.compile_module::<DefaultProcessState>(&plugins, raw_module.into())
.unwrap(),
);

Expand All @@ -34,6 +35,7 @@ fn criterion_benchmark(c: &mut Criterion) {
module.clone(),
config.clone(),
registry,
plugins.clone(),
)
.unwrap();
lunatic_process::wasm::spawn_wasm(
Expand Down
1 change: 1 addition & 0 deletions crates/lunatic-distributed/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ repository = "https://github.com/lunatic-solutions/lunatic/tree/main/crates"
license = "Apache-2.0/MIT"

[dependencies]
lunatic-plugin-internal = { workspace = true }
lunatic-process = { workspace = true }

anyhow = { workspace = true }
Expand Down
7 changes: 6 additions & 1 deletion crates/lunatic-distributed/src/distributed/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{net::SocketAddr, sync::Arc};

use anyhow::{anyhow, Result};

use lunatic_plugin_internal::Plugin;
use lunatic_process::{
env::{Environment, Environments},
message::{DataMessage, Message},
Expand All @@ -25,6 +26,7 @@ pub struct ServerCtx<T, E: Environment> {
pub modules: Modules<T>,
pub distributed: DistributedProcessState,
pub runtime: WasmtimeRuntime,
pub plugins: Arc<Vec<Plugin>>,
}

impl<T: 'static, E: Environment> Clone for ServerCtx<T, E> {
Expand All @@ -34,6 +36,7 @@ impl<T: 'static, E: Environment> Clone for ServerCtx<T, E> {
modules: self.modules.clone(),
distributed: self.distributed.clone(),
runtime: self.runtime.clone(),
plugins: self.plugins.clone(),
}
}
}
Expand Down Expand Up @@ -159,7 +162,9 @@ where
None => {
if let Some(bytes) = ctx.distributed.control.get_module(module_id).await {
let wasm = RawWasm::new(Some(module_id), bytes);
ctx.modules.compile(ctx.runtime.clone(), wasm).await??
ctx.modules
.compile(ctx.runtime.clone(), ctx.plugins.clone(), wasm)
.await??
} else {
return Ok(Err(ClientError::ModuleNotFound));
}
Expand Down
13 changes: 13 additions & 0 deletions crates/lunatic-plugin-internal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "lunatic-plugin-internal"
version = "0.12.0"
edition = "2021"
description = "Lunatic plugins system."
homepage = "https://lunatic.solutions"
repository = "https://github.com/lunatic-solutions/lunatic/tree/main/crates"
license = "Apache-2.0/MIT"

[dependencies]
anyhow = { workspace = true }
libloading = "0.7"
wasmtime = { workspace = true }
66 changes: 66 additions & 0 deletions crates/lunatic-plugin-internal/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::{
any::{Any, TypeId},
ffi::OsStr,
sync::Arc,
};

use anyhow::{Context, Result};
use libloading::{Library, Symbol};
use wasmtime::Linker;

type PluginIDFn = unsafe extern "C" fn() -> TypeId;
type InitFn = unsafe extern "C" fn() -> Box<dyn Any + Send + Sync>;
type RegisterFn<T> = unsafe extern "C" fn(linker: &mut Linker<T>) -> Result<()>;

pub struct Plugin {
id: TypeId,
lib: Library,
}

pub trait PluginCtx {
fn plugins(&self) -> &Arc<Vec<Plugin>>;
fn plugin_state<T: 'static>(&self, plugin: &TypeId) -> Option<&T>;
fn plugin_state_mut<T: 'static>(&mut self, plugin: &TypeId) -> Option<&mut T>;
}

impl Plugin {
/// Loads a dynamic library as a plugin.
///
/// # Safety
///
/// Plugins must specify the correct type of the exported functions.
pub unsafe fn new<P: AsRef<OsStr>>(filename: P) -> Result<Self> {
let lib = Library::new(filename)?;
let plugin_id: Symbol<PluginIDFn> = lib
.get(b"plugin_id")
.context("loading `plugin_id` export from plugin")?;
Ok(Plugin {
id: plugin_id(),
lib,
})
}

pub fn id(&self) -> TypeId {
self.id
}

pub fn init(&self) -> Result<Box<dyn Any + Send + Sync>> {
unsafe {
let init: Symbol<InitFn> = self
.lib
.get(b"init")
.context("loading `init` export from plugin")?;
Ok(init())
}
}

pub fn register<T>(&self, linker: &mut Linker<T>) -> Result<()> {
unsafe {
let register: Symbol<RegisterFn<T>> = self
.lib
.get(b"register")
.context("loading `register` export from plugin")?;
register(linker).context("calling register on plugin")
}
}
}
15 changes: 15 additions & 0 deletions crates/lunatic-plugin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "lunatic-plugin"
version = "0.12.0"
edition = "2021"
description = "Lunatic plugins system."
homepage = "https://lunatic.solutions"
repository = "https://github.com/lunatic-solutions/lunatic/tree/main/crates"
license = "Apache-2.0/MIT"

[dependencies]
lunatic-plugin-internal = { workspace = true }
lunatic-runtime = { workspace = true }

anyhow = { workspace = true }
wasmtime = { workspace = true }
Loading