From a0b0a95f5432f8a4395c81da892ed819ae8efba1 Mon Sep 17 00:00:00 2001 From: Geoffrey Mureithi <95377562+geofmureithi@users.noreply.github.com> Date: Wed, 9 Aug 2023 11:34:39 +0300 Subject: [PATCH 1/6] Create rust.yml --- .github/workflows/rust.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .github/workflows/rust.yml diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml new file mode 100644 index 0000000..31000a2 --- /dev/null +++ b/.github/workflows/rust.yml @@ -0,0 +1,22 @@ +name: Rust + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +env: + CARGO_TERM_COLOR: always + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Build + run: cargo build --verbose + - name: Run tests + run: cargo test --verbose From 4f1f29650da271f7baf2d8207505a4ecfc7c614e Mon Sep 17 00:00:00 2001 From: Njuguna Mureithi Date: Wed, 9 Aug 2023 11:39:06 +0300 Subject: [PATCH 2/6] add: ci for checking and testing --- .github/workflows/build.yml | 30 ++++++++++++++++++++++++++++++ .github/workflows/rust.yml | 22 ---------------------- 2 files changed, 30 insertions(+), 22 deletions(-) create mode 100644 .github/workflows/build.yml delete mode 100644 .github/workflows/rust.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..6d660a3 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,30 @@ +name: Build and Test + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +env: + CARGO_TERM_COLOR: always + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Build core + run: cargo build -p plugy-core --verbose + - name: Test core + run: cargo test -p plugy-core --verbose + - name: Build macros + run: cargo build -p plugy-macros --verbose + - name: Test macros + run: cargo test -p plugy-macros --verbose + - name: Build runtime + run: cargo build -p plugy-runtime --verbose + - name: Test runtime + run: cargo test -p plugy-runtime --verbose diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml deleted file mode 100644 index 31000a2..0000000 --- a/.github/workflows/rust.yml +++ /dev/null @@ -1,22 +0,0 @@ -name: Rust - -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] - -env: - CARGO_TERM_COLOR: always - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - name: Build - run: cargo build --verbose - - name: Run tests - run: cargo test --verbose From 5159adf0a423437fa5ebb2b99c6a1928eb563a8a Mon Sep 17 00:00:00 2001 From: Njuguna Mureithi Date: Wed, 9 Aug 2023 11:52:50 +0300 Subject: [PATCH 3/6] fix: clippy issues --- .github/workflows/build.yml | 7 +++++++ Cargo.lock | 16 ++++++++++++++++ crates/plugy-core/src/guest.rs | 2 ++ crates/plugy-runtime/Cargo.toml | 1 + crates/plugy-runtime/src/lib.rs | 32 +++++++++++++------------------- 5 files changed, 39 insertions(+), 19 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6d660a3..05e2c69 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -28,3 +28,10 @@ jobs: run: cargo build -p plugy-runtime --verbose - name: Test runtime run: cargo test -p plugy-runtime --verbose + clippy: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Check the project + run: cargo clippy -Dwarnings diff --git a/Cargo.lock b/Cargo.lock index 128b3fd..3dc48b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -58,6 +58,15 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2d098ff73c1ca148721f37baad5ea6a465a13f9573aba8641fbbbae8164a54e" +[[package]] +name = "async-lock" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa24f727524730b077666307f2734b4a1a1c57acb79193127dcc8914d5242dd7" +dependencies = [ + "event-listener", +] + [[package]] name = "async-trait" version = "0.1.72" @@ -476,6 +485,12 @@ dependencies = [ "libc", ] +[[package]] +name = "event-listener" +version = "2.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" + [[package]] name = "fallible-iterator" version = "0.2.0" @@ -927,6 +942,7 @@ name = "plugy-runtime" version = "0.1.0" dependencies = [ "anyhow", + "async-lock", "bincode", "dashmap", "plugy", diff --git a/crates/plugy-core/src/guest.rs b/crates/plugy-core/src/guest.rs index de5e0f6..f69ad1c 100644 --- a/crates/plugy-core/src/guest.rs +++ b/crates/plugy-core/src/guest.rs @@ -74,6 +74,7 @@ pub extern "C" fn alloc(len: u32) -> *mut u8 { #[no_mangle] pub unsafe extern "C" fn dealloc(value: u64) { let (ptr, len) = from_bitwise(value); + #[allow(clippy::useless_transmute)] let ptr = std::mem::transmute::(ptr as _); let buffer = Vec::from_raw_parts(ptr, len as _, len as _); std::mem::drop(buffer); @@ -157,6 +158,7 @@ pub fn write_msg(value: &T) -> u64 { /// ``` pub unsafe fn read_msg(value: u64) -> T { let (ptr, len) = from_bitwise(value); + #[allow(clippy::useless_transmute)] let ptr = std::mem::transmute::(ptr as _); let buffer = Vec::from_raw_parts(ptr, len as _, len as _); bincode::deserialize(&buffer).unwrap() diff --git a/crates/plugy-runtime/Cargo.toml b/crates/plugy-runtime/Cargo.toml index 31fb52d..283d4a6 100644 --- a/crates/plugy-runtime/Cargo.toml +++ b/crates/plugy-runtime/Cargo.toml @@ -13,6 +13,7 @@ dashmap = "5.4.0" plugy-core = { path = "../plugy-core" } serde = { version = "1", features = ["derive"] } wasmtime = "10.0.1" +async-lock = "2.7.0" [dev-dependencies] plugy-macros = { path = "../plugy-macros" } diff --git a/crates/plugy-runtime/src/lib.rs b/crates/plugy-runtime/src/lib.rs index 094b5df..8580149 100644 --- a/crates/plugy-runtime/src/lib.rs +++ b/crates/plugy-runtime/src/lib.rs @@ -2,14 +2,10 @@ use dashmap::DashMap; use plugy_core::bitwise::{from_bitwise, into_bitwise}; use serde::{de::DeserializeOwned, Serialize}; use std::fmt; -use std::{ - future::Future, - marker::PhantomData, - pin::Pin, - sync::{Arc, Mutex}, -}; +use std::{future::Future, marker::PhantomData, pin::Pin, sync::Arc}; use wasmtime::{Engine, Instance, Linker, Module, Store}; -pub type Caller = Arc>>>>; +use async_lock::RwLock; +pub type Caller = Arc>>>>; /// A runtime environment for managing plugins and instances. /// @@ -30,9 +26,7 @@ pub type Caller = Arc>>>>; /// fn greet(&self); /// } /// -/// fn main() { -/// let runtime = Runtime::::new(); -/// +/// let runtime = Runtime::::new(); /// // Load and manage plugins... /// } pub struct Runtime

{ @@ -46,7 +40,7 @@ pub struct Runtime

{ #[allow(dead_code)] pub struct RuntimeModule { inner: Module, - store: Arc>>>>, + store: Caller, instance: Instance, } @@ -158,7 +152,7 @@ impl

Runtime

{ name, RuntimeModule { inner: module.clone(), - store: Arc::new(Mutex::new(store)), + store: Arc::new(RwLock::new(store)), instance, }, ); @@ -185,7 +179,7 @@ impl

Runtime

{ let module = self.modules.get(name).unwrap(); Ok(P::into_callable(PluginHandle { store: module.store.clone(), - instance: module.instance.clone(), + instance: module.instance, inner: PhantomData::, })) } @@ -204,7 +198,7 @@ impl

Runtime

{ #[derive(Debug, Clone)] pub struct PluginHandle

{ instance: Instance, - store: Arc>>>>, + store: Caller, inner: PhantomData

, } @@ -230,18 +224,18 @@ impl

PluginHandle

{ /// Returns a `Result` containing the typed function interface on success, /// or an `anyhow::Error` if the function retrieval encounters any issues. - pub fn get_func( + pub async fn get_func( &self, name: &str, ) -> anyhow::Result> { let store = self.store.clone(); let inner_wasm_fn = self.instance.get_typed_func::( - &mut *store.lock().unwrap(), + &mut *store.write().await, &format!("_plugy_guest_{name}"), )?; Ok(Func { inner_wasm_fn, - store: store.clone(), + store, input: std::marker::PhantomData::, output: std::marker::PhantomData::, }) @@ -255,7 +249,7 @@ pub trait IntoCallable

{ pub struct Func { inner_wasm_fn: wasmtime::TypedFunc, - store: Arc>>>>, + store: Caller, input: PhantomData

, output: PhantomData, } @@ -293,7 +287,7 @@ impl Func { /// or an `anyhow::Error` if the function call or deserialization encounters issues. pub async fn call_checked(&self, value: &P) -> anyhow::Result { - let mut store = self.store.lock().unwrap(); + let mut store = self.store.write().await; let RuntimeCaller { memory, alloc_fn, .. } = store.data().clone().unwrap(); From 4deab900177aa44601491698c2dacca60cf31db6 Mon Sep 17 00:00:00 2001 From: Njuguna Mureithi Date: Wed, 9 Aug 2023 11:54:59 +0300 Subject: [PATCH 4/6] fix: clippy command --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 05e2c69..6800d5f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -34,4 +34,4 @@ jobs: steps: - uses: actions/checkout@v3 - name: Check the project - run: cargo clippy -Dwarnings + run: cargo clippy From a6141775bb66eb65ba8bb3fadc0418774c4fada9 Mon Sep 17 00:00:00 2001 From: Njuguna Mureithi Date: Wed, 9 Aug 2023 12:03:30 +0300 Subject: [PATCH 5/6] fix: failing ci --- crates/plugy-runtime/src/lib.rs | 36 +-------------------------------- 1 file changed, 1 insertion(+), 35 deletions(-) diff --git a/crates/plugy-runtime/src/lib.rs b/crates/plugy-runtime/src/lib.rs index 094b5df..d429e61 100644 --- a/crates/plugy-runtime/src/lib.rs +++ b/crates/plugy-runtime/src/lib.rs @@ -31,7 +31,7 @@ pub type Caller = Arc>>>>; /// } /// /// fn main() { -/// let runtime = Runtime::::new(); +/// let runtime = Runtime::>::new(); /// /// // Load and manage plugins... /// } @@ -320,19 +320,6 @@ impl Func { /// Implementors of this trait provide the ability to asynchronously retrieve /// the Wasm module data for a plugin. /// -/// # Examples -/// -/// ```rust -/// # use plugy::runtime::PluginLoader; -/// # -/// struct MyPluginLoader; -/// -/// impl PluginLoader for MyPluginLoader { -/// fn load(&self) -> std::pin::Pin, anyhow::Error>>>> { -/// // ... (implementation details) -/// } -/// } -/// ``` pub trait PluginLoader { /// Asynchronously loads the Wasm module data for the plugin. /// @@ -344,26 +331,5 @@ pub trait PluginLoader { /// /// Returns a `Pin, anyhow::Error>>>>` /// representing the asynchronous loading process. - /// - /// # Examples - /// - /// ```rust - /// # use plugy::runtime::PluginLoader; - /// # - /// # struct MyPluginLoader; - /// # - /// # impl PluginLoader for MyPluginLoader { - /// # fn load(&self) -> std::pin::Pin, anyhow::Error>>>> { - /// # Box::pin(async { Ok(Vec::new()) }) - /// # } - /// # } - /// # - /// # #[tokio::main] - /// # async fn main() -> anyhow::Result<()> { - /// let loader = MyPluginLoader; - /// let wasm_data: Vec = loader.load().await?; - /// # Ok(()) - /// # } - /// ``` fn load(&self) -> Pin, anyhow::Error>>>>; } From 3419b6ead3bb665fed5ca31e0454a62c69dccdc9 Mon Sep 17 00:00:00 2001 From: Njuguna Mureithi Date: Wed, 9 Aug 2023 12:12:50 +0300 Subject: [PATCH 6/6] fix: missing await --- crates/plugy-macros/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/plugy-macros/src/lib.rs b/crates/plugy-macros/src/lib.rs index 5a15930..4cc08ee 100644 --- a/crates/plugy-macros/src/lib.rs +++ b/crates/plugy-macros/src/lib.rs @@ -55,7 +55,7 @@ fn generate_async_trait(trait_item: &ItemTrait) -> proc_macro2::TokenStream { .collect(); quote! { pub async fn #method_name(#method_inputs) #method_output { - let func = self.handle.get_func(#method_name_str).unwrap(); + let func = self.handle.get_func(#method_name_str).await.unwrap(); func.call_unchecked(&(#(#values),*)).await } }