Work on plugin builder
This commit is contained in:
parent
53e56f1284
commit
96c2559d2c
5 changed files with 97 additions and 56 deletions
|
@ -7,7 +7,8 @@ edition = "2021"
|
|||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
syn = { version = "1.0", features = ["full"] }
|
||||
# TODO: remove "extra-traits"
|
||||
syn = { version = "1.0", features = ["full", "extra-traits"] }
|
||||
quote = "1.0"
|
||||
proc-macro2 = "1.0"
|
||||
serde = "1.0"
|
||||
|
|
|
@ -2,7 +2,7 @@ use core::panic;
|
|||
|
||||
use proc_macro::TokenStream;
|
||||
use quote::{format_ident, quote};
|
||||
use syn::{parse_macro_input, FnArg, ItemFn, Type, Visibility};
|
||||
use syn::{parse_macro_input, FnArg, ForeignItemFn, ItemFn, Type, Visibility};
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn export(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||
|
@ -11,6 +11,11 @@ pub fn export(args: TokenStream, function: TokenStream) -> TokenStream {
|
|||
}
|
||||
|
||||
let inner_fn = parse_macro_input!(function as ItemFn);
|
||||
|
||||
if !inner_fn.sig.generics.params.is_empty() {
|
||||
panic!("Exported functions can not take generic parameters");
|
||||
}
|
||||
|
||||
if let Visibility::Public(_) = inner_fn.vis {
|
||||
} else {
|
||||
panic!("The export attribute only works for public functions");
|
||||
|
@ -77,15 +82,27 @@ pub fn export(args: TokenStream, function: TokenStream) -> TokenStream {
|
|||
|
||||
#[proc_macro_attribute]
|
||||
pub fn import(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||
todo!()
|
||||
// if !args.is_empty() {
|
||||
// panic!("The import attribute does not take any arguments");
|
||||
// }
|
||||
if !args.is_empty() {
|
||||
panic!("The import attribute does not take any arguments");
|
||||
}
|
||||
|
||||
// let inner_fn = parse_macro_input!(function as ItemFn);
|
||||
let fn_declare = parse_macro_input!(function as ForeignItemFn);
|
||||
|
||||
// let inner_fn_name = format_ident!("{}", inner_fn.sig.ident);
|
||||
// // let outer_fn_name = format_ident!("__{}", inner_fn_name);
|
||||
if !fn_declare.sig.generics.params.is_empty() {
|
||||
panic!("Exported functions can not take generic parameters");
|
||||
}
|
||||
|
||||
dbg!(&fn_declare.sig);
|
||||
|
||||
// let inner_fn = ItemFn {
|
||||
// attrs: fn_declare.attrs,
|
||||
// vis: fn_declare.vis,
|
||||
// sig: fn_declare.sig,
|
||||
// block: todo!(),
|
||||
// };
|
||||
|
||||
// let inner_fn_name = format_ident!("{}", inner_fn.sig.ident);
|
||||
// let outer_fn_name = format_ident!("__{}", inner_fn_name);
|
||||
|
||||
// let variadic = inner_fn.sig.inputs.len();
|
||||
// let i = (0..variadic).map(syn::Index::from);
|
||||
|
@ -116,28 +133,35 @@ pub fn import(args: TokenStream, function: TokenStream) -> TokenStream {
|
|||
// (quote! { data }, quote! { #ty })
|
||||
// };
|
||||
|
||||
// TokenStream::from(quote! {
|
||||
// TokenStream::from(quote! {
|
||||
// extern "C" {
|
||||
// #[no_mangle]
|
||||
// #inner_fn
|
||||
// fn #outer_fn_name(ptr: *const u8, len: usize) -> *const ::plugin::__Buffer;
|
||||
// }
|
||||
|
||||
// #[no_mangle]
|
||||
// pub extern "C" fn #outer_fn_name(ptr: *const u8, len: usize) -> *const ::plugin::__Buffer {
|
||||
// // setup
|
||||
// let buffer = ::plugin::__Buffer { ptr, len };
|
||||
// let data = unsafe { buffer.to_vec() };
|
||||
// #[no_mangle]
|
||||
// fn #inner_fn_name #params -> #output {
|
||||
// println!("executing command: {}", string);
|
||||
// // serialize data
|
||||
// let data = #collect_params;
|
||||
// let data = ::plugin::bincode::serialize(&data).unwrap();
|
||||
// let buffer = unsafe { ::plugin::__Buffer::from_vec(data) };
|
||||
// let ptr = buffer.ptr;
|
||||
// let len = buffer.len;
|
||||
// // leak data to heap
|
||||
// buffer.leak_to_heap();
|
||||
// // call extern function
|
||||
// let result = unsafe { __command(ptr, len) };
|
||||
// // get result
|
||||
// let result = todo!(); // convert into box
|
||||
|
||||
// // operation
|
||||
// let data: #ty = match ::plugin::bincode::deserialize(&data) {
|
||||
// Ok(d) => d,
|
||||
// Err(e) => panic!("Data passed to function not deserializable."),
|
||||
// };
|
||||
// let result = #inner_fn_name(#args);
|
||||
// let new_data: Result<Vec<u8>, _> = ::plugin::bincode::serialize(&result);
|
||||
// let new_data = new_data.unwrap();
|
||||
|
||||
// // teardown
|
||||
// let new_buffer = unsafe { ::plugin::__Buffer::from_vec(new_data) };
|
||||
// return new_buffer.leak_to_heap();
|
||||
// }
|
||||
// })
|
||||
// // deserialize data
|
||||
// let data: Option<String> = match ::plugin::bincode::deserialize(&data) {
|
||||
// Ok(d) => d,
|
||||
// Err(e) => panic!("Data passed to function not deserializable."),
|
||||
// };
|
||||
// return data;
|
||||
// }
|
||||
// })
|
||||
todo!()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue