Skip to content

Commit

Permalink
Merge pull request #7 from geofmureithi/feat/plugy-arity
Browse files Browse the repository at this point in the history
feat: plugin functions can have multiple arity
  • Loading branch information
geofmureithi authored Aug 9, 2023
2 parents 1e235dd + 60acd48 commit 0f8cc0e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
14 changes: 10 additions & 4 deletions crates/plugy-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,27 @@ pub fn plugin_impl(_metadata: TokenStream, input: TokenStream) -> TokenStream {
.map(|m| {
let method_name = &m.sig.ident;
let args = &m.sig.inputs;

let types: Vec<_> = args
.iter()
.filter_map(|arg| match arg {
syn::FnArg::Receiver(_) => None,
syn::FnArg::Typed(t) => Some(t.ty.to_token_stream()),
})
.collect();
let values: Vec<_> = args
.iter()
.filter_map(|arg| match arg {
syn::FnArg::Receiver(_) => None,
syn::FnArg::Typed(t) => Some(t.to_token_stream()),
syn::FnArg::Typed(t) => Some(t.pat.to_token_stream()),
})
.collect();
let expose_name = format!("_plugy_guest_{}", method_name);
let expose_name_ident = syn::Ident::new(&expose_name, Span::call_site());
quote! {
#[no_mangle]
pub unsafe extern "C" fn #expose_name_ident(value: u64) -> u64 {
let value: #ty = plugy_core::guest::read_msg(value);
plugy_core::guest::write_msg(&#ty.#method_name(#(#values)*))
let (value, #(#values),*): (#ty, #(#types),*) = plugy_core::guest::read_msg(value);
plugy_core::guest::write_msg(&value.#method_name(#(#values),*))
}
}
})
Expand Down
9 changes: 3 additions & 6 deletions crates/plugy-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ pub type Caller = Arc<RwLock<Store<Option<RuntimeCaller<()>>>>>;
/// trait Plugin {
/// fn greet(&self);
/// }
///
/// fn main() {
/// let runtime = Runtime::<Box<dyn Plugin>>::new();
///
/// // Load and manage plugins...
/// }
/// let runtime = Runtime::<Box<dyn Plugin>>::new();
/// // Load and manage plugins...
/// ```
pub struct Runtime<P> {
engine: Engine,
linker: Linker<Option<RuntimeCaller<()>>>,
Expand Down
7 changes: 4 additions & 3 deletions examples/foo-plugin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use plugy_macros::plugin_impl;
use shared::Greeter;
use serde::Deserialize;
use shared::Greeter;

#[derive(Debug, Deserialize)]
struct FooPlugin;

#[plugin_impl]
impl Greeter for FooPlugin {
fn greet(&self) -> String {
"Hello From Foo Plugin".to_owned()
fn greet(&self, name: String, last_name: Option<String>) -> String {
let last_name = last_name.unwrap_or_default();
format!("Hello From Foo Plugin to {name} {last_name}")
}
}
4 changes: 2 additions & 2 deletions examples/runner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct FooPlugin;
async fn main() {
let runtime = Runtime::<Box<dyn Greeter>>::new().unwrap();
let handle = runtime.load(FooPlugin).await.unwrap();
let res = handle.greet().await;
let res = handle.greet("Geoff".to_owned(), Some("Mureithi".to_owned())).await;

assert_eq!(res, "Hello From Foo Plugin")
assert_eq!(res, "Hello From Foo Plugin to Geoff Mureithi")
}
4 changes: 2 additions & 2 deletions examples/shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[plugy_macros::plugin]
pub trait Greeter {
fn greet(&self) -> String;
}
fn greet(&self, name: String, last_name: Option<String>) -> String;
}

0 comments on commit 0f8cc0e

Please sign in to comment.