From 863c6b45a55fab2586f631bda7107163c24402b3 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 17 Nov 2020 14:29:28 +0800 Subject: [PATCH] Fine tune codegen for global exports. --- RELEASES.md | 2 +- codegen/src/function.rs | 12 +++++++---- codegen/src/rhai_module.rs | 21 ++++++++++--------- codegen/ui_tests/rhai_fn_global_multiple.rs | 2 +- .../ui_tests/rhai_fn_global_multiple.stderr | 4 ++-- 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index 7f19875a..18bb9cdd 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -15,7 +15,7 @@ New features * `Engine::register_module` to register a module as a sub-module in the global namespace. * `Module::get_fn_namespace` and `Module::set_fn_namespace` can expose a module function to the global namespace. This is convenient when registering an API for a custom type. * `set_exported_global_fn!` macro to register a plugin function and expose it to the global namespace. -* `#[rhai_fn(gobal)]` and `#[rhai_fn(internal)]` attributes to determine whether a function defined in a plugin module should be exposed to the global namespace. This is convenient when defining an API for a custom type. +* `#[rhai_fn(global)]` and `#[rhai_fn(internal)]` attributes to determine whether a function defined in a plugin module should be exposed to the global namespace. This is convenient when defining an API for a custom type. Enhancements ------------ diff --git a/codegen/src/function.rs b/codegen/src/function.rs index 64df0cd3..f1d18fcd 100644 --- a/codegen/src/function.rs +++ b/codegen/src/function.rs @@ -215,14 +215,18 @@ impl ExportedParams for ExportedFnParams { return Err(syn::Error::new(s.span(), "extraneous value")) } ("global", None) => { - if namespace.is_some() { - return Err(syn::Error::new(key.span(), "conflicting namespace")); + if let Some(ns) = namespace { + if ns != FnNamespaceAccess::Global { + return Err(syn::Error::new(key.span(), "conflicting namespace")); + } } namespace = Some(FnNamespaceAccess::Global); } ("internal", None) => { - if namespace.is_some() { - return Err(syn::Error::new(key.span(), "conflicting namespace")); + if let Some(ns) = namespace { + if ns != FnNamespaceAccess::Internal { + return Err(syn::Error::new(key.span(), "conflicting namespace")); + } } namespace = Some(FnNamespaceAccess::Internal); } diff --git a/codegen/src/rhai_module.rs b/codegen/src/rhai_module.rs index 3981e3b9..76d7648a 100644 --- a/codegen/src/rhai_module.rs +++ b/codegen/src/rhai_module.rs @@ -129,17 +129,18 @@ pub(crate) fn generate_body( } for fn_literal in reg_names { - set_fn_stmts.push( + let ns_str = syn::Ident::new( match namespace { - FnNamespaceAccess::Global => syn::parse2::(quote! { - m.set_fn(#fn_literal, FnNamespace::Global, FnAccess::Public, &[#(#fn_input_types),*], - #fn_token_name().into()); - }), - FnNamespaceAccess::Internal => syn::parse2::(quote! { - m.set_fn(#fn_literal, FnNamespace::Internal, FnAccess::Public, &[#(#fn_input_types),*], - #fn_token_name().into()); - }), - } + FnNamespaceAccess::Global => "Global", + FnNamespaceAccess::Internal => "Internal", + }, + fn_literal.span(), + ); + set_fn_stmts.push( + syn::parse2::(quote! { + m.set_fn(#fn_literal, FnNamespace::#ns_str, FnAccess::Public, &[#(#fn_input_types),*], + #fn_token_name().into()); + }) .unwrap(), ); } diff --git a/codegen/ui_tests/rhai_fn_global_multiple.rs b/codegen/ui_tests/rhai_fn_global_multiple.rs index 45e2e325..6b670112 100644 --- a/codegen/ui_tests/rhai_fn_global_multiple.rs +++ b/codegen/ui_tests/rhai_fn_global_multiple.rs @@ -9,7 +9,7 @@ pub struct Point { #[export_module] pub mod test_module { pub use super::Point; - #[rhai_fn(global, global)] + #[rhai_fn(global, internal)] pub fn test_fn(input: Point) -> bool { input.x > input.y } diff --git a/codegen/ui_tests/rhai_fn_global_multiple.stderr b/codegen/ui_tests/rhai_fn_global_multiple.stderr index f66ebbb7..93da770d 100644 --- a/codegen/ui_tests/rhai_fn_global_multiple.stderr +++ b/codegen/ui_tests/rhai_fn_global_multiple.stderr @@ -1,8 +1,8 @@ error: conflicting namespace --> $DIR/rhai_fn_global_multiple.rs:12:23 | -12 | #[rhai_fn(global, global)] - | ^^^^^^ +12 | #[rhai_fn(global, internal)] + | ^^^^^^^^ error[E0433]: failed to resolve: use of undeclared crate or module `test_module` --> $DIR/rhai_fn_global_multiple.rs:23:8