Start working on host-side functions
This commit is contained in:
parent
71e0555763
commit
53e56f1284
5 changed files with 232 additions and 30 deletions
|
@ -5,15 +5,15 @@ use quote::{format_ident, quote};
|
|||
use syn::{parse_macro_input, FnArg, ItemFn, Type, Visibility};
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn bind(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||
pub fn export(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||
if !args.is_empty() {
|
||||
panic!("The bind attribute does not take any arguments");
|
||||
panic!("The export 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");
|
||||
panic!("The export attribute only works for public functions");
|
||||
}
|
||||
|
||||
let inner_fn_name = format_ident!("{}", inner_fn.sig.ident);
|
||||
|
@ -53,6 +53,7 @@ pub fn bind(args: TokenStream, function: TokenStream) -> TokenStream {
|
|||
#inner_fn
|
||||
|
||||
#[no_mangle]
|
||||
// TODO: switch len from usize to u32?
|
||||
pub extern "C" fn #outer_fn_name(ptr: *const u8, len: usize) -> *const ::plugin::__Buffer {
|
||||
// setup
|
||||
let buffer = ::plugin::__Buffer { ptr, len };
|
||||
|
@ -73,3 +74,70 @@ pub fn bind(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");
|
||||
// }
|
||||
|
||||
// let inner_fn = parse_macro_input!(function as ItemFn);
|
||||
|
||||
// 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);
|
||||
// let t: Vec<Type> = inner_fn
|
||||
// .sig
|
||||
// .inputs
|
||||
// .iter()
|
||||
// .map(|x| match x {
|
||||
// FnArg::Receiver(_) => {
|
||||
// panic!("all arguments must have specified types, no `self` allowed")
|
||||
// }
|
||||
// FnArg::Typed(item) => *item.ty.clone(),
|
||||
// })
|
||||
// .collect();
|
||||
|
||||
// // this is cursed...
|
||||
// let (args, ty) = if variadic != 1 {
|
||||
// (
|
||||
// quote! {
|
||||
// #( data.#i ),*
|
||||
// },
|
||||
// quote! {
|
||||
// ( #( #t ),* )
|
||||
// },
|
||||
// )
|
||||
// } else {
|
||||
// let ty = &t[0];
|
||||
// (quote! { data }, quote! { #ty })
|
||||
// };
|
||||
|
||||
// TokenStream::from(quote! {
|
||||
// #[no_mangle]
|
||||
// #inner_fn
|
||||
|
||||
// #[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() };
|
||||
|
||||
// // 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();
|
||||
// }
|
||||
// })
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue