Revise treatment of name parameter.

This commit is contained in:
Stephen Chung 2020-08-16 18:24:42 +08:00
parent 7eb75f6790
commit 31a05f8c48
3 changed files with 22 additions and 32 deletions

View File

@ -87,9 +87,7 @@ impl Parse for ExportedFnParams {
return Err(syn::Error::new(s.span(), "extraneous value")) return Err(syn::Error::new(s.span(), "extraneous value"))
} }
("skip", None) => skip = true, ("skip", None) => skip = true,
("skip", Some(s)) => { ("skip", Some(s)) => return Err(syn::Error::new(s.span(), "extraneous value")),
return Err(syn::Error::new(s.span(), "extraneous value"))
}
(attr, _) => { (attr, _) => {
return Err(syn::Error::new( return Err(syn::Error::new(
ident.span(), ident.span(),
@ -99,7 +97,12 @@ impl Parse for ExportedFnParams {
} }
} }
Ok(ExportedFnParams { name, return_raw, skip, ..Default::default() }) Ok(ExportedFnParams {
name,
return_raw,
skip,
..Default::default()
})
} }
} }
@ -260,13 +263,8 @@ impl ExportedFn {
} }
pub fn generate(self) -> proc_macro2::TokenStream { pub fn generate(self) -> proc_macro2::TokenStream {
let name_str = if let Some(ref name) = self.params.name {
name.clone()
} else {
self.name().to_string()
};
let name: syn::Ident = let name: syn::Ident =
syn::Ident::new(&format!("rhai_fn_{}", name_str), self.name().span()); syn::Ident::new(&format!("rhai_fn_{}", self.name()), self.name().span());
let impl_block = self.generate_impl("Token"); let impl_block = self.generate_impl("Token");
let callable_block = self.generate_callable("Token"); let callable_block = self.generate_callable("Token");
let input_types_block = self.generate_input_types("Token"); let input_types_block = self.generate_input_types("Token");
@ -285,11 +283,7 @@ impl ExportedFn {
} }
pub fn generate_dynamic_fn(&self) -> proc_macro2::TokenStream { pub fn generate_dynamic_fn(&self) -> proc_macro2::TokenStream {
let name: syn::Ident = if let Some(ref name) = self.params.name { let name = self.name().clone();
syn::Ident::new(name, self.name().span())
} else {
self.name().clone()
};
let mut dynamic_signature = self.signature.clone(); let mut dynamic_signature = self.signature.clone();
dynamic_signature.ident = dynamic_signature.ident =
@ -358,10 +352,11 @@ impl ExportedFn {
} }
pub fn generate_impl(&self, on_type_name: &str) -> proc_macro2::TokenStream { pub fn generate_impl(&self, on_type_name: &str) -> proc_macro2::TokenStream {
let name: syn::Ident = if let Some(ref name) = self.params.name { let sig_name = self.name().clone();
syn::Ident::new(name, self.name().span()) let name = if let Some(ref name) = self.params.name {
name.clone()
} else { } else {
self.name().clone() self.name().to_string()
}; };
let arg_count = self.arg_count(); let arg_count = self.arg_count();
@ -485,11 +480,11 @@ impl ExportedFn {
// This allows skipping the Dynamic::from wrap. // This allows skipping the Dynamic::from wrap.
let return_expr = if !self.params.return_raw { let return_expr = if !self.params.return_raw {
quote! { quote! {
Ok(Dynamic::from(#name(#(#unpack_exprs),*))) Ok(Dynamic::from(#sig_name(#(#unpack_exprs),*)))
} }
} else { } else {
quote! { quote! {
#name(#(#unpack_exprs),*) #sig_name(#(#unpack_exprs),*)
} }
}; };

View File

@ -106,20 +106,11 @@ pub fn export_fn(
args: proc_macro::TokenStream, args: proc_macro::TokenStream,
input: proc_macro::TokenStream, input: proc_macro::TokenStream,
) -> proc_macro::TokenStream { ) -> proc_macro::TokenStream {
let output = proc_macro2::TokenStream::from(input.clone()); let mut output = proc_macro2::TokenStream::from(input.clone());
let parsed_params = parse_macro_input!(args as function::ExportedFnParams); let parsed_params = parse_macro_input!(args as function::ExportedFnParams);
let function_def = parse_macro_input!(input as function::ExportedFn); let function_def = parse_macro_input!(input as function::ExportedFn);
let mut output = if let Some(ref rename) = parsed_params.name {
// If it wasn't a function, it wouldn't have parsed earlier, so unwrap() is fine.
let mut output_fn: syn::ItemFn = syn::parse2(output.clone()).unwrap();
let new_name = syn::Ident::new(rename, output_fn.sig.ident.span());
output_fn.sig.ident = new_name;
output_fn.into_token_stream()
} else {
output
};
output.extend(function_def.generate_with_params(parsed_params)); output.extend(function_def.generate_with_params(parsed_params));
proc_macro::TokenStream::from(output) proc_macro::TokenStream::from(output)
} }

View File

@ -29,8 +29,12 @@ pub(crate) fn generate_body(
&format!("{}_token", function.name().to_string()), &format!("{}_token", function.name().to_string()),
function.name().span(), function.name().span(),
); );
let fn_literal = let reg_name = if let Some(ref name) = function.params.name {
syn::LitStr::new(&function.name().to_string(), proc_macro2::Span::call_site()); name.clone()
} else {
function.name().to_string()
};
let fn_literal = syn::LitStr::new(&reg_name, proc_macro2::Span::call_site());
let fn_input_types: Vec<syn::Expr> = function let fn_input_types: Vec<syn::Expr> = function
.arg_list() .arg_list()
.map(|fnarg| match fnarg { .map(|fnarg| match fnarg {