|
| 1 | +use proc_macro::TokenStream; |
| 2 | +use proc_macro_crate::{FoundCrate, crate_name}; |
| 3 | +use quote::quote; |
| 4 | +use syn::parse::{Error, Parse, ParseStream, Result}; |
| 5 | +use syn::spanned::Spanned as _; |
| 6 | +use syn::{ForeignItemFn, ItemFn, LitStr, Pat, parse_macro_input}; |
| 7 | + |
| 8 | +enum NameArg { |
| 9 | + None, |
| 10 | + Name(LitStr), |
| 11 | +} |
| 12 | + |
| 13 | +impl Parse for NameArg { |
| 14 | + fn parse(input: ParseStream) -> Result<Self> { |
| 15 | + if input.is_empty() { |
| 16 | + return Ok(NameArg::None); |
| 17 | + } |
| 18 | + let name: LitStr = input.parse()?; |
| 19 | + if !input.is_empty() { |
| 20 | + return Err(Error::new(input.span(), "expected a single identifier")); |
| 21 | + } |
| 22 | + Ok(NameArg::Name(name)) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +#[proc_macro_attribute] |
| 27 | +pub fn guest_function(attr: TokenStream, item: TokenStream) -> TokenStream { |
| 28 | + let crate_name = |
| 29 | + crate_name("hyperlight-guest-bin").expect("hyperlight-guest-bin must be a dependency"); |
| 30 | + let crate_name = match crate_name { |
| 31 | + FoundCrate::Itself => quote! {crate}, |
| 32 | + FoundCrate::Name(name) => { |
| 33 | + let ident = syn::Ident::new(&name, proc_macro2::Span::call_site()); |
| 34 | + quote! {::#ident} |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + let fn_declaration = parse_macro_input!(item as ItemFn); |
| 39 | + |
| 40 | + let ident = fn_declaration.sig.ident.clone(); |
| 41 | + |
| 42 | + let exported_name = match parse_macro_input!(attr as NameArg) { |
| 43 | + NameArg::None => quote! { stringify!(#ident) }, |
| 44 | + NameArg::Name(name) => quote! { #name }, |
| 45 | + }; |
| 46 | + |
| 47 | + if let Some(syn::FnArg::Receiver(arg)) = fn_declaration.sig.inputs.first() { |
| 48 | + return Error::new( |
| 49 | + arg.span(), |
| 50 | + "Receiver (self) argument is not allowed in guest functions", |
| 51 | + ) |
| 52 | + .to_compile_error() |
| 53 | + .into(); |
| 54 | + } |
| 55 | + |
| 56 | + if fn_declaration.sig.asyncness.is_some() { |
| 57 | + return Error::new( |
| 58 | + fn_declaration.sig.asyncness.span(), |
| 59 | + "Async functions are not allowed in guest functions", |
| 60 | + ) |
| 61 | + .to_compile_error() |
| 62 | + .into(); |
| 63 | + } |
| 64 | + |
| 65 | + let output = quote! { |
| 66 | + #fn_declaration |
| 67 | + |
| 68 | + const _: () = { |
| 69 | + #[#crate_name::__private::linkme::distributed_slice(#crate_name::__private::GUEST_FUNCTION_INIT)] |
| 70 | + #[linkme(crate = #crate_name::__private::linkme)] |
| 71 | + static REGISTRATION: fn() = || { |
| 72 | + hyperlight_guest_bin::guest_function::register::register_fn(#exported_name, #ident); |
| 73 | + }; |
| 74 | + }; |
| 75 | + }; |
| 76 | + |
| 77 | + output.into() |
| 78 | +} |
| 79 | + |
| 80 | +#[proc_macro_attribute] |
| 81 | +pub fn host_function(attr: TokenStream, item: TokenStream) -> TokenStream { |
| 82 | + let crate_name = |
| 83 | + crate_name("hyperlight-guest-bin").expect("hyperlight-guest-bin must be a dependency"); |
| 84 | + let crate_name = match crate_name { |
| 85 | + FoundCrate::Itself => quote! {crate}, |
| 86 | + FoundCrate::Name(name) => { |
| 87 | + let ident = syn::Ident::new(&name, proc_macro2::Span::call_site()); |
| 88 | + quote! {::#ident} |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + let fn_declaration = parse_macro_input!(item as ForeignItemFn); |
| 93 | + |
| 94 | + let ForeignItemFn { |
| 95 | + attrs, |
| 96 | + vis, |
| 97 | + sig, |
| 98 | + semi_token: _, |
| 99 | + } = fn_declaration; |
| 100 | + |
| 101 | + let ident = sig.ident.clone(); |
| 102 | + |
| 103 | + let exported_name = match parse_macro_input!(attr as NameArg) { |
| 104 | + NameArg::None => quote! { stringify!(#ident) }, |
| 105 | + NameArg::Name(name) => quote! { #name }, |
| 106 | + }; |
| 107 | + |
| 108 | + let mut args = vec![]; |
| 109 | + for arg in sig.inputs.iter() { |
| 110 | + match arg { |
| 111 | + syn::FnArg::Receiver(_) => { |
| 112 | + return Error::new( |
| 113 | + arg.span(), |
| 114 | + "Receiver (self) argument is not allowed in guest functions", |
| 115 | + ) |
| 116 | + .to_compile_error() |
| 117 | + .into(); |
| 118 | + } |
| 119 | + syn::FnArg::Typed(arg) => { |
| 120 | + let Pat::Ident(pat) = *arg.pat.clone() else { |
| 121 | + return Error::new( |
| 122 | + arg.span(), |
| 123 | + "Only named arguments are allowed in host functions", |
| 124 | + ) |
| 125 | + .to_compile_error() |
| 126 | + .into(); |
| 127 | + }; |
| 128 | + |
| 129 | + if pat.attrs.len() > 0 { |
| 130 | + return Error::new( |
| 131 | + arg.span(), |
| 132 | + "Attributes are not allowed on host function arguments", |
| 133 | + ) |
| 134 | + .to_compile_error() |
| 135 | + .into(); |
| 136 | + } |
| 137 | + |
| 138 | + if pat.by_ref.is_some() { |
| 139 | + return Error::new( |
| 140 | + arg.span(), |
| 141 | + "By-ref arguments are not allowed in host functions", |
| 142 | + ) |
| 143 | + .to_compile_error() |
| 144 | + .into(); |
| 145 | + } |
| 146 | + |
| 147 | + if pat.mutability.is_some() { |
| 148 | + return Error::new( |
| 149 | + arg.span(), |
| 150 | + "Mutable arguments are not allowed in host functions", |
| 151 | + ) |
| 152 | + .to_compile_error() |
| 153 | + .into(); |
| 154 | + } |
| 155 | + |
| 156 | + if pat.subpat.is_some() { |
| 157 | + return Error::new( |
| 158 | + arg.span(), |
| 159 | + "Sub-patterns are not allowed in host functions", |
| 160 | + ) |
| 161 | + .to_compile_error() |
| 162 | + .into(); |
| 163 | + } |
| 164 | + |
| 165 | + let ident = pat.ident.clone(); |
| 166 | + |
| 167 | + args.push(quote! { #ident }); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + let ret: proc_macro2::TokenStream = match &sig.output { |
| 173 | + syn::ReturnType::Default => quote! { quote! { () } }, |
| 174 | + syn::ReturnType::Type(_, ty) => { |
| 175 | + quote! { #ty } |
| 176 | + } |
| 177 | + }; |
| 178 | + |
| 179 | + let output = quote! { |
| 180 | + #(#attrs)* #vis #sig { |
| 181 | + use #crate_name::__private::{ResultType, HyperlightGuestError}; |
| 182 | + use #crate_name::host_comm::call_host; |
| 183 | + <#ret as ResultType<HyperlightGuestError>>::from_result(call_host(#exported_name, (#(#args,)*))) |
| 184 | + } |
| 185 | + }; |
| 186 | + |
| 187 | + output.into() |
| 188 | +} |
0 commit comments