lib.rs

  1use core::panic;
  2
  3use proc_macro::TokenStream;
  4use quote::{format_ident, quote};
  5use syn::{
  6    parse_macro_input, Block, FnArg, ForeignItemFn, Ident, ItemFn, Pat, PatIdent, Type, Visibility,
  7};
  8
  9/// Attribute macro to be used guest-side within a plugin.
 10/// ```ignore
 11/// #[export]
 12/// pub fn say_hello() -> String {
 13///     "Hello from Wasm".into()
 14/// }
 15/// ```
 16/// This macro makes a function defined guest-side avaliable host-side.
 17/// Note that all arguments and return types must be `serde`.
 18#[proc_macro_attribute]
 19pub fn export(args: TokenStream, function: TokenStream) -> TokenStream {
 20    if !args.is_empty() {
 21        panic!("The export attribute does not take any arguments");
 22    }
 23
 24    let inner_fn = parse_macro_input!(function as ItemFn);
 25
 26    if !inner_fn.sig.generics.params.is_empty() {
 27        panic!("Exported functions can not take generic parameters");
 28    }
 29
 30    if let Visibility::Public(_) = inner_fn.vis {
 31    } else {
 32        panic!("The export attribute only works for public functions");
 33    }
 34
 35    let inner_fn_name = format_ident!("{}", inner_fn.sig.ident);
 36    let outer_fn_name = format_ident!("__{}", inner_fn_name);
 37
 38    let variadic = inner_fn.sig.inputs.len();
 39    let i = (0..variadic).map(syn::Index::from);
 40    let t: Vec<Type> = inner_fn
 41        .sig
 42        .inputs
 43        .iter()
 44        .map(|x| match x {
 45            FnArg::Receiver(_) => {
 46                panic!("All arguments must have specified types, no `self` allowed")
 47            }
 48            FnArg::Typed(item) => *item.ty.clone(),
 49        })
 50        .collect();
 51
 52    // this is cursed...
 53    let (args, ty) = if variadic != 1 {
 54        (
 55            quote! {
 56                #( data.#i ),*
 57            },
 58            quote! {
 59                ( #( #t ),* )
 60            },
 61        )
 62    } else {
 63        let ty = &t[0];
 64        (quote! { data }, quote! { #ty })
 65    };
 66
 67    TokenStream::from(quote! {
 68        #[no_mangle]
 69        #inner_fn
 70
 71        #[no_mangle]
 72        // TODO: switch len from usize to u32?
 73        pub extern "C" fn #outer_fn_name(packed_buffer: u64) -> u64 {
 74            // setup
 75            let data = unsafe { ::plugin::__Buffer::from_u64(packed_buffer).to_vec() };
 76
 77            // operation
 78            let data: #ty = match ::plugin::bincode::deserialize(&data) {
 79                Ok(d) => d,
 80                Err(e) => panic!("Data passed to function not deserializable."),
 81            };
 82            let result = #inner_fn_name(#args);
 83            let new_data: Result<Vec<u8>, _> = ::plugin::bincode::serialize(&result);
 84            let new_data = new_data.unwrap();
 85
 86            // teardown
 87            let new_buffer = unsafe { ::plugin::__Buffer::from_vec(new_data) }.into_u64();
 88            return new_buffer;
 89        }
 90    })
 91}
 92
 93/// Attribute macro to be used guest-side within a plugin.
 94/// ```ignore
 95/// #[import]
 96/// pub fn operating_system_name() -> String;
 97/// ```
 98/// This macro makes a function defined host-side avaliable guest-side.
 99/// Note that all arguments and return types must be `serde`.
100/// All that's provided is a signature, as the function is implemented host-side.
101#[proc_macro_attribute]
102pub fn import(args: TokenStream, function: TokenStream) -> TokenStream {
103    if !args.is_empty() {
104        panic!("The import attribute does not take any arguments");
105    }
106
107    let fn_declare = parse_macro_input!(function as ForeignItemFn);
108
109    if !fn_declare.sig.generics.params.is_empty() {
110        panic!("Exported functions can not take generic parameters");
111    }
112
113    // let inner_fn_name = format_ident!("{}", fn_declare.sig.ident);
114    let extern_fn_name = format_ident!("__{}", fn_declare.sig.ident);
115
116    let (args, tys): (Vec<Ident>, Vec<Type>) = fn_declare
117        .sig
118        .inputs
119        .clone()
120        .into_iter()
121        .map(|x| match x {
122            FnArg::Receiver(_) => {
123                panic!("All arguments must have specified types, no `self` allowed")
124            }
125            FnArg::Typed(t) => {
126                if let Pat::Ident(i) = *t.pat {
127                    (i.ident, *t.ty)
128                } else {
129                    panic!("All function arguments must be identifiers");
130                }
131            }
132        })
133        .unzip();
134
135    let body = TokenStream::from(quote! {
136        {
137            // setup
138            let data: (#( #tys ),*) = (#( #args ),*);
139            let data = ::plugin::bincode::serialize(&data).unwrap();
140            let buffer = unsafe { ::plugin::__Buffer::from_vec(data) };
141
142            // operation
143            let new_buffer = unsafe { #extern_fn_name(buffer.into_u64()) };
144            let new_data = unsafe { ::plugin::__Buffer::from_u64(new_buffer).to_vec() };
145
146            // teardown
147            match ::plugin::bincode::deserialize(&new_data) {
148                Ok(d) => d,
149                Err(e) => panic!("Data returned from function not deserializable."),
150            }
151        }
152    });
153
154    let block = parse_macro_input!(body as Block);
155
156    let inner_fn = ItemFn {
157        attrs: fn_declare.attrs,
158        vis: fn_declare.vis,
159        sig: fn_declare.sig,
160        block: Box::new(block),
161    };
162
163    TokenStream::from(quote! {
164        extern "C" {
165            fn #extern_fn_name(buffer: u64) -> u64;
166        }
167
168        #[no_mangle]
169        #inner_fn
170    })
171}