From 31a05f8c48a5df05312e80135b003e441d4471ce Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 16 Aug 2020 18:24:42 +0800 Subject: [PATCH] Revise treatment of name parameter. --- codegen/src/function.rs | 35 +++++++++++++++-------------------- codegen/src/lib.rs | 11 +---------- codegen/src/rhai_module.rs | 8 ++++++-- 3 files changed, 22 insertions(+), 32 deletions(-) diff --git a/codegen/src/function.rs b/codegen/src/function.rs index 0f6a603f..4285ece7 100644 --- a/codegen/src/function.rs +++ b/codegen/src/function.rs @@ -87,9 +87,7 @@ impl Parse for ExportedFnParams { return Err(syn::Error::new(s.span(), "extraneous value")) } ("skip", None) => skip = true, - ("skip", Some(s)) => { - return Err(syn::Error::new(s.span(), "extraneous value")) - } + ("skip", Some(s)) => return Err(syn::Error::new(s.span(), "extraneous value")), (attr, _) => { return Err(syn::Error::new( 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 { - let name_str = if let Some(ref name) = self.params.name { - name.clone() - } else { - self.name().to_string() - }; 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 callable_block = self.generate_callable("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 { - let name: syn::Ident = if let Some(ref name) = self.params.name { - syn::Ident::new(name, self.name().span()) - } else { - self.name().clone() - }; + let name = self.name().clone(); let mut dynamic_signature = self.signature.clone(); dynamic_signature.ident = @@ -358,10 +352,11 @@ impl ExportedFn { } pub fn generate_impl(&self, on_type_name: &str) -> proc_macro2::TokenStream { - let name: syn::Ident = if let Some(ref name) = self.params.name { - syn::Ident::new(name, self.name().span()) + let sig_name = self.name().clone(); + let name = if let Some(ref name) = self.params.name { + name.clone() } else { - self.name().clone() + self.name().to_string() }; let arg_count = self.arg_count(); @@ -485,11 +480,11 @@ impl ExportedFn { // This allows skipping the Dynamic::from wrap. let return_expr = if !self.params.return_raw { quote! { - Ok(Dynamic::from(#name(#(#unpack_exprs),*))) + Ok(Dynamic::from(#sig_name(#(#unpack_exprs),*))) } } else { quote! { - #name(#(#unpack_exprs),*) + #sig_name(#(#unpack_exprs),*) } }; diff --git a/codegen/src/lib.rs b/codegen/src/lib.rs index 2e5d0860..6f25be9a 100644 --- a/codegen/src/lib.rs +++ b/codegen/src/lib.rs @@ -106,20 +106,11 @@ pub fn export_fn( args: proc_macro::TokenStream, input: 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 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)); proc_macro::TokenStream::from(output) } diff --git a/codegen/src/rhai_module.rs b/codegen/src/rhai_module.rs index 08c7f11f..3d238b19 100644 --- a/codegen/src/rhai_module.rs +++ b/codegen/src/rhai_module.rs @@ -29,8 +29,12 @@ pub(crate) fn generate_body( &format!("{}_token", function.name().to_string()), function.name().span(), ); - let fn_literal = - syn::LitStr::new(&function.name().to_string(), proc_macro2::Span::call_site()); + let reg_name = if let Some(ref name) = function.params.name { + name.clone() + } else { + function.name().to_string() + }; + let fn_literal = syn::LitStr::new(®_name, proc_macro2::Span::call_site()); let fn_input_types: Vec = function .arg_list() .map(|fnarg| match fnarg {