Work on macro binding generation, some cleanup needed, rename runner to plugin
This commit is contained in:
parent
dda6dcb3b8
commit
f6a9558c5c
22 changed files with 330 additions and 59 deletions
14
crates/plugin_macros/Cargo.toml
Normal file
14
crates/plugin_macros/Cargo.toml
Normal file
|
@ -0,0 +1,14 @@
|
|||
[package]
|
||||
name = "plugin_macros"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
syn = { version = "1.0", features = ["full"] }
|
||||
quote = "1.0"
|
||||
proc-macro2 = "1.0"
|
||||
serde = "1.0"
|
||||
bincode = "1.3"
|
45
crates/plugin_macros/src/lib.rs
Normal file
45
crates/plugin_macros/src/lib.rs
Normal file
|
@ -0,0 +1,45 @@
|
|||
use core::panic;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use quote::{format_ident, quote};
|
||||
use syn::{parse_macro_input, ItemFn, VisPublic, Visibility};
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn bind(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||
if !args.is_empty() {
|
||||
panic!("The bind attribute does not take any arguments");
|
||||
}
|
||||
|
||||
let inner_fn = parse_macro_input!(function as ItemFn);
|
||||
if let Visibility::Public(_) = inner_fn.vis {
|
||||
} else {
|
||||
panic!("The bind attribute only works for public functions");
|
||||
}
|
||||
|
||||
let inner_fn_name = format_ident!("{}", inner_fn.sig.ident);
|
||||
let outer_fn_name = format_ident!("__{}", inner_fn_name);
|
||||
|
||||
TokenStream::from(quote! {
|
||||
use serde;
|
||||
|
||||
#[no_mangle]
|
||||
#inner_fn
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn #outer_fn_name(ptr: *const u8, len: usize) -> *const Buffer {
|
||||
// setup
|
||||
let buffer = Buffer { ptr, len };
|
||||
let data = unsafe { buffer.to_vec() };
|
||||
|
||||
// operation
|
||||
let argument = bincode::deserialize(&data).unwrap();
|
||||
let result = #inner_fn_name(argument);
|
||||
let new_data: Result<Vec<u8>, _> = bincode::serialize(&result);
|
||||
let new_data = new_data.unwrap();
|
||||
|
||||
// teardown
|
||||
let new_buffer = unsafe { Buffer::from_vec(new_data) };
|
||||
return new_buffer.leak_to_heap();
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue