From 848bdf3f01b2129b62675341471a08eff4eea010 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 13 Sep 2020 22:12:11 +0800 Subject: [PATCH 01/16] Add combine_with_exported_module. --- codegen/src/lib.rs | 14 ++++++++++++++ codegen/src/module.rs | 1 + codegen/src/rhai_module.rs | 14 ++++++++++++++ doc/src/plugins/module.md | 3 +++ doc/src/rust/packages/create.md | 11 +++++++---- src/packages/arithmetic.rs | 6 +++--- src/packages/array_basic.rs | 2 +- src/packages/eval.rs | 2 +- src/packages/fn_basic.rs | 2 +- src/packages/logic.rs | 2 +- src/packages/map_basic.rs | 2 +- src/packages/math_basic.rs | 4 ++-- src/packages/string_more.rs | 2 +- src/packages/time_basic.rs | 2 +- tests/plugins.rs | 14 +++++++++++++- xxx.rs | 0 16 files changed, 64 insertions(+), 17 deletions(-) create mode 100644 xxx.rs diff --git a/codegen/src/lib.rs b/codegen/src/lib.rs index b9d8880d..3763b47e 100644 --- a/codegen/src/lib.rs +++ b/codegen/src/lib.rs @@ -152,6 +152,20 @@ pub fn exported_module(module_path: proc_macro::TokenStream) -> proc_macro::Toke proc_macro::TokenStream::from(tokens) } +#[proc_macro] +pub fn combine_with_exported_module(args: proc_macro::TokenStream) -> proc_macro::TokenStream { + #[allow(unused_variables)] + let (module_expr, export_name, module_path) = match crate::register::parse_register_macro(args) + { + Ok(triple) => triple, + Err(e) => return e.to_compile_error().into(), + }; + let tokens = quote! { + #module_path::rhai_generate_into_module(#module_expr); + }; + proc_macro::TokenStream::from(tokens) +} + #[proc_macro] pub fn register_exported_fn(args: proc_macro::TokenStream) -> proc_macro::TokenStream { let (engine_expr, export_name, rust_modpath) = match crate::register::parse_register_macro(args) diff --git a/codegen/src/module.rs b/codegen/src/module.rs index 1ebc25b8..d89bfbb9 100644 --- a/codegen/src/module.rs +++ b/codegen/src/module.rs @@ -191,6 +191,7 @@ impl Parse for Module { } } +#[allow(dead_code)] impl Module { pub fn attrs(&self) -> Option<&Vec> { self.mod_all.as_ref().map(|m| &m.attrs) diff --git a/codegen/src/rhai_module.rs b/codegen/src/rhai_module.rs index d318f351..911c7edc 100644 --- a/codegen/src/rhai_module.rs +++ b/codegen/src/rhai_module.rs @@ -17,6 +17,7 @@ pub(crate) fn generate_body( let mut set_fn_stmts: Vec = Vec::new(); let mut set_const_stmts: Vec = Vec::new(); let mut add_mod_blocks: Vec = Vec::new(); + let mut set_mod_blocks: Vec = Vec::new(); let str_type_path = syn::parse2::(quote! { str }).unwrap(); for (const_name, const_expr) in consts { @@ -54,6 +55,14 @@ pub(crate) fn generate_body( }) .unwrap(), ); + set_mod_blocks.push( + syn::parse2::(quote! { + #(#cfg_attrs)* { + self::#module_name::rhai_generate_into_module(m); + } + }) + .unwrap(), + ); } // NB: these are token streams, because reparsing messes up "> >" vs ">>" @@ -137,6 +146,11 @@ pub(crate) fn generate_body( #(#add_mod_blocks)* m } + pub fn rhai_generate_into_module(m: &mut Module) { + #(#set_fn_stmts)* + #(#set_const_stmts)* + #(#set_mod_blocks)* + } } }) .unwrap(); diff --git a/doc/src/plugins/module.md b/doc/src/plugins/module.md index 40232140..24107fff 100644 --- a/doc/src/plugins/module.md +++ b/doc/src/plugins/module.md @@ -24,6 +24,9 @@ use rhai::plugins::*; // a "prelude" import for macros #[export_module] mod my_module { + // This constant will be registered as a the constant variable 'SOME_NUMBER'. + pub const SOME_NUMBER: i64 = 42; + // This function will be registered as 'greet'. pub fn greet(name: &str) -> String { format!("hello, {}!", name) diff --git a/doc/src/rust/packages/create.md b/doc/src/rust/packages/create.md index d212d38f..4f55cc13 100644 --- a/doc/src/rust/packages/create.md +++ b/doc/src/rust/packages/create.md @@ -63,8 +63,8 @@ By far the easiest way to create a custom module is to call `Module::merge_flatt In fact, this exactly is how Rhai's built-in packages, such as `BasicMathPackage`, are implemented. -`rhai::plugins::exported_module!` generates a module from the [plugins][plugin module] definition, -and `Module::merge_flatten` consumes its, adding all its registered functions into the package itself. +`rhai::plugins::combine_with_exported_module!` adds all functions and constants from the +[plugins][plugin module] definition into the package itself. ```rust // Import necessary types and traits. @@ -94,7 +94,10 @@ def_package!(rhai:MyPackage:"My own personal super package", module, { BasicArrayPackage::init(module); BasicMapPackage::init(module); - // Merge the plugin module into the custom package. - module.merge_flatten(exported_module!(my_module)); + // Merge all registered functions and constants from the plugin module into the custom package. + // + // The text string name in the middle parameter can be anything and is reserved for future use; + // it is recommended to be an ID string that uniquely identifies the module. + combine_with_exported_module!(module, "my-functions", my_module)); }); ``` diff --git a/src/packages/arithmetic.rs b/src/packages/arithmetic.rs index d72e2e8b..f963e778 100644 --- a/src/packages/arithmetic.rs +++ b/src/packages/arithmetic.rs @@ -185,7 +185,7 @@ macro_rules! gen_signed_functions { macro_rules! reg_functions { ($mod_name:ident += $root:ident ; $($arg_type:ident),+ ) => { $( - $mod_name.combine_flatten(exported_module!($root::$arg_type::functions)); + combine_with_exported_module!($mod_name, "arithmetic", $root::$arg_type::functions); )* } } @@ -208,8 +208,8 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { // Basic arithmetic for floating-point #[cfg(not(feature = "no_float"))] { - lib.combine_flatten(exported_module!(f32_functions)); - lib.combine_flatten(exported_module!(f64_functions)); + combine_with_exported_module!(lib, "f32", f32_functions); + combine_with_exported_module!(lib, "f64", f64_functions); } }); diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index 767c4c5f..05599eac 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -58,7 +58,7 @@ macro_rules! reg_functions { } def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { - lib.combine_flatten(exported_module!(array_functions)); + combine_with_exported_module!(lib, "array", array_functions); reg_functions!(lib += basic; INT, bool, char, ImmutableString, FnPtr, Array, Unit); diff --git a/src/packages/eval.rs b/src/packages/eval.rs index acebf216..bb5716d8 100644 --- a/src/packages/eval.rs +++ b/src/packages/eval.rs @@ -5,7 +5,7 @@ use crate::plugin::*; use crate::result::EvalAltResult; def_package!(crate:EvalPackage:"Disable 'eval'.", lib, { - lib.combine_flatten(exported_module!(eval_override)); + combine_with_exported_module!(lib, "eval", eval_override); }); #[export_module] diff --git a/src/packages/fn_basic.rs b/src/packages/fn_basic.rs index 61d18371..5ffb6cb1 100644 --- a/src/packages/fn_basic.rs +++ b/src/packages/fn_basic.rs @@ -3,7 +3,7 @@ use crate::fn_native::FnPtr; use crate::plugin::*; def_package!(crate:BasicFnPackage:"Basic Fn functions.", lib, { - lib.combine_flatten(exported_module!(fn_ptr_functions)); + combine_with_exported_module!(lib, "FnPtr", fn_ptr_functions); }); #[export_module] diff --git a/src/packages/logic.rs b/src/packages/logic.rs index 48e46047..554f30e3 100644 --- a/src/packages/logic.rs +++ b/src/packages/logic.rs @@ -45,7 +45,7 @@ macro_rules! gen_cmp_functions { macro_rules! reg_functions { ($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { $( - $mod_name.combine_flatten(exported_module!($root::$arg_type::functions)); + combine_with_exported_module!($mod_name, "logic", $root::$arg_type::functions); )* } } diff --git a/src/packages/map_basic.rs b/src/packages/map_basic.rs index a5983268..fde57db7 100644 --- a/src/packages/map_basic.rs +++ b/src/packages/map_basic.rs @@ -9,7 +9,7 @@ use crate::plugin::*; use crate::stdlib::vec::Vec; def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, { - lib.combine_flatten(exported_module!(map_functions)); + combine_with_exported_module!(lib, "map", map_functions); }); #[export_module] diff --git a/src/packages/math_basic.rs b/src/packages/math_basic.rs index ddbb640c..2fd2a9c4 100644 --- a/src/packages/math_basic.rs +++ b/src/packages/math_basic.rs @@ -48,10 +48,10 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, { #[cfg(not(feature = "no_float"))] { // Floating point functions - lib.combine_flatten(exported_module!(float_functions)); + combine_with_exported_module!(lib, "float", float_functions); // Trig functions - lib.combine_flatten(exported_module!(trig_functions)); + combine_with_exported_module!(lib, "trig", trig_functions); reg_functions!(lib += basic_to_float::to_float(INT)); diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 5dfd4897..55fa4be0 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -57,7 +57,7 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str #[cfg(not(feature = "no_float"))] reg_functions!(lib += float; f32, f64); - lib.combine_flatten(exported_module!(string_functions)); + combine_with_exported_module!(lib, "string", string_functions); lib.set_raw_fn( "pad", diff --git a/src/packages/time_basic.rs b/src/packages/time_basic.rs index 80ba2637..caa5fc47 100644 --- a/src/packages/time_basic.rs +++ b/src/packages/time_basic.rs @@ -21,7 +21,7 @@ use instant::Instant; def_package!(crate:BasicTimePackage:"Basic timing utilities.", lib, { // Register date/time functions - lib.combine_flatten(exported_module!(time_functions)); + combine_with_exported_module!(lib, "time", time_functions); }); #[export_module] diff --git a/tests/plugins.rs b/tests/plugins.rs index cac8718f..928f0381 100644 --- a/tests/plugins.rs +++ b/tests/plugins.rs @@ -1,5 +1,6 @@ #![cfg(not(any(feature = "no_index", feature = "no_module")))] +use rhai::module_resolvers::StaticModuleResolver; use rhai::plugin::*; use rhai::{Engine, EvalAltResult, INT}; @@ -10,6 +11,8 @@ mod test { pub mod special_array_package { use rhai::{Array, INT}; + pub const MYSTIC_NUMBER: INT = 42 as INT; + #[cfg(not(feature = "no_object"))] pub mod feature { use rhai::{Array, Dynamic, EvalAltResult}; @@ -66,7 +69,7 @@ fn test_plugins_package() -> Result<(), Box> { let mut engine = Engine::new(); let mut m = Module::new(); - m.combine_flatten(exported_module!(test::special_array_package)); + combine_with_exported_module!(&mut m, "test", test::special_array_package); engine.load_package(m); reg_functions!(engine += greet::single(INT, bool, char)); @@ -83,5 +86,14 @@ fn test_plugins_package() -> Result<(), Box> { "6 kitties" ); + let mut resolver = StaticModuleResolver::new(); + resolver.insert("test", exported_module!(test::special_array_package)); + + engine.set_module_resolver(Some(resolver)); + assert_eq!( + engine.eval::(r#"import "test" as test; test::MYSTIC_NUMBER"#)?, + 42 + ); + Ok(()) } diff --git a/xxx.rs b/xxx.rs new file mode 100644 index 00000000..e69de29b From 6dc5a81d539e6cccb1194d3b843f57aaf15acfd7 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 13 Sep 2020 22:36:24 +0800 Subject: [PATCH 02/16] Add cast to type for constants to avoid mis-typing. --- codegen/src/module.rs | 3 ++- codegen/src/rhai_module.rs | 6 +++--- tests/plugins.rs | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/codegen/src/module.rs b/codegen/src/module.rs index d89bfbb9..85b1278c 100644 --- a/codegen/src/module.rs +++ b/codegen/src/module.rs @@ -140,12 +140,13 @@ impl Parse for Module { ref expr, ident, attrs, + ty, .. }) => { // #[cfg] attributes are not allowed on const declarations crate::attrs::deny_cfg_attr(&attrs)?; if let syn::Visibility::Public(_) = vis { - consts.push((ident.to_string(), expr.as_ref().clone())); + consts.push((ident.to_string(), ty.clone(), expr.as_ref().clone())); } } _ => {} diff --git a/codegen/src/rhai_module.rs b/codegen/src/rhai_module.rs index 911c7edc..165f7438 100644 --- a/codegen/src/rhai_module.rs +++ b/codegen/src/rhai_module.rs @@ -6,7 +6,7 @@ use crate::attrs::ExportScope; use crate::function::ExportedFn; use crate::module::Module; -pub(crate) type ExportedConst = (String, syn::Expr); +pub(crate) type ExportedConst = (String, Box, syn::Expr); pub(crate) fn generate_body( fns: &mut [ExportedFn], @@ -20,11 +20,11 @@ pub(crate) fn generate_body( let mut set_mod_blocks: Vec = Vec::new(); let str_type_path = syn::parse2::(quote! { str }).unwrap(); - for (const_name, const_expr) in consts { + for (const_name, const_type, const_expr) in consts { let const_literal = syn::LitStr::new(&const_name, proc_macro2::Span::call_site()); set_const_stmts.push( syn::parse2::(quote! { - m.set_var(#const_literal, #const_expr); + m.set_var(#const_literal, (#const_expr) as #const_type); }) .unwrap(), ); diff --git a/tests/plugins.rs b/tests/plugins.rs index 928f0381..2bee84e4 100644 --- a/tests/plugins.rs +++ b/tests/plugins.rs @@ -11,7 +11,7 @@ mod test { pub mod special_array_package { use rhai::{Array, INT}; - pub const MYSTIC_NUMBER: INT = 42 as INT; + pub const MYSTIC_NUMBER: INT = 42; #[cfg(not(feature = "no_object"))] pub mod feature { From f1d85ae99c287966e8e22c1a4911793a33ff9762 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 13 Sep 2020 22:12:11 +0800 Subject: [PATCH 03/16] Add combine_with_exported_module. --- tests/plugins.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/plugins.rs b/tests/plugins.rs index 2bee84e4..928f0381 100644 --- a/tests/plugins.rs +++ b/tests/plugins.rs @@ -11,7 +11,7 @@ mod test { pub mod special_array_package { use rhai::{Array, INT}; - pub const MYSTIC_NUMBER: INT = 42; + pub const MYSTIC_NUMBER: INT = 42 as INT; #[cfg(not(feature = "no_object"))] pub mod feature { From 6126f0cb101e29646d20348fd5a10a6395970a7b Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 13 Sep 2020 22:36:24 +0800 Subject: [PATCH 04/16] Add cast to type for constants to avoid mis-typing. --- tests/plugins.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/plugins.rs b/tests/plugins.rs index 928f0381..2bee84e4 100644 --- a/tests/plugins.rs +++ b/tests/plugins.rs @@ -11,7 +11,7 @@ mod test { pub mod special_array_package { use rhai::{Array, INT}; - pub const MYSTIC_NUMBER: INT = 42 as INT; + pub const MYSTIC_NUMBER: INT = 42; #[cfg(not(feature = "no_object"))] pub mod feature { From d57ce9c0508db7ca31ff6b6d48a28d26a7c72e73 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 13 Sep 2020 22:36:40 +0800 Subject: [PATCH 05/16] Remove warning about unused constants in modules. --- codegen/src/module.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/codegen/src/module.rs b/codegen/src/module.rs index 85b1278c..4b829693 100644 --- a/codegen/src/module.rs +++ b/codegen/src/module.rs @@ -282,6 +282,7 @@ impl Module { // Regenerate the module with the new content added. Ok(quote! { #(#mod_attrs)* + #[allow(dead_code)] pub mod #mod_name { #(#orig_content)* #(#inner_modules)* From 654da2db8a2a051d57bae4c5d7e144af5821dafb Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 13 Sep 2020 23:13:07 +0800 Subject: [PATCH 06/16] Fix tests. --- codegen/src/test/module.rs | 4 ++-- doc/src/rust/packages/create.md | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/codegen/src/test/module.rs b/codegen/src/test/module.rs index 2bf48752..677cd040 100644 --- a/codegen/src/test/module.rs +++ b/codegen/src/test/module.rs @@ -109,7 +109,7 @@ mod module_tests { assert_eq!(item_mod.submodules().len(), 1); assert_eq!(&item_mod.submodules()[0].consts()[0].0, "MYSTIC_NUMBER"); assert_eq!( - item_mod.submodules()[0].consts()[0].1, + item_mod.submodules()[0].consts()[0].2, syn::parse2::(quote! { 42 }).unwrap() ); } @@ -170,7 +170,7 @@ mod module_tests { assert_eq!(item_mod.consts().len(), 1); assert_eq!(&item_mod.consts()[0].0, "MYSTIC_NUMBER"); assert_eq!( - item_mod.consts()[0].1, + item_mod.consts()[0].2, syn::parse2::(quote! { 42 }).unwrap() ); } diff --git a/doc/src/rust/packages/create.md b/doc/src/rust/packages/create.md index 4f55cc13..53ac17a0 100644 --- a/doc/src/rust/packages/create.md +++ b/doc/src/rust/packages/create.md @@ -66,6 +66,9 @@ In fact, this exactly is how Rhai's built-in packages, such as `BasicMathPackage `rhai::plugins::combine_with_exported_module!` adds all functions and constants from the [plugins][plugin module] definition into the package itself. +All sub-modules are _flattened_ (i.e. all functions and constants defined within sub-modules are registered +at the top level) and so there will not be any sub-modules added to the package. + ```rust // Import necessary types and traits. use rhai::{ @@ -84,6 +87,14 @@ mod my_module { pub fn get_num() -> i64 { 42 } + + // This is a sub-module, but if using combine_with_exported_module!, it will + // be flattened and all functions registered at the top level. + pub mod my_sub_module { + pub fn get_sub_num() -> i64 { + 0 + } + } } // Define the package 'MyPackage'. @@ -96,8 +107,17 @@ def_package!(rhai:MyPackage:"My own personal super package", module, { // Merge all registered functions and constants from the plugin module into the custom package. // + // Functions in the sub-module 'my_sub_module' are flattened and registered at the top level + // instead of in a sub-module. + // // The text string name in the middle parameter can be anything and is reserved for future use; // it is recommended to be an ID string that uniquely identifies the module. + // + // This call ends up registering three functions at the top level of the package: + // 1) greet + // 2) get_num + // 3) get_sub_num (flattened from sub-module 'my_sub_module') + // combine_with_exported_module!(module, "my-functions", my_module)); }); ``` From 52298bd96c2ee6b357ef69aba6f58cd530594f5e Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 14 Sep 2020 11:40:15 +0800 Subject: [PATCH 07/16] Streamline rhai_generate. --- codegen/src/lib.rs | 2 +- codegen/src/rhai_module.rs | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/codegen/src/lib.rs b/codegen/src/lib.rs index 3763b47e..4426f173 100644 --- a/codegen/src/lib.rs +++ b/codegen/src/lib.rs @@ -161,7 +161,7 @@ pub fn combine_with_exported_module(args: proc_macro::TokenStream) -> proc_macro Err(e) => return e.to_compile_error().into(), }; let tokens = quote! { - #module_path::rhai_generate_into_module(#module_expr); + #module_path::rhai_generate_into_module(#module_expr, true); }; proc_macro::TokenStream::from(tokens) } diff --git a/codegen/src/rhai_module.rs b/codegen/src/rhai_module.rs index 165f7438..66f23390 100644 --- a/codegen/src/rhai_module.rs +++ b/codegen/src/rhai_module.rs @@ -17,7 +17,7 @@ pub(crate) fn generate_body( let mut set_fn_stmts: Vec = Vec::new(); let mut set_const_stmts: Vec = Vec::new(); let mut add_mod_blocks: Vec = Vec::new(); - let mut set_mod_blocks: Vec = Vec::new(); + let mut set_flattened_mod_blocks: Vec = Vec::new(); let str_type_path = syn::parse2::(quote! { str }).unwrap(); for (const_name, const_type, const_expr) in consts { @@ -55,10 +55,10 @@ pub(crate) fn generate_body( }) .unwrap(), ); - set_mod_blocks.push( + set_flattened_mod_blocks.push( syn::parse2::(quote! { #(#cfg_attrs)* { - self::#module_name::rhai_generate_into_module(m); + self::#module_name::rhai_generate_into_module(m, flatten); } }) .unwrap(), @@ -138,18 +138,22 @@ pub(crate) fn generate_body( pub mod generate_info { #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); - #(#set_fn_stmts)* - #(#set_const_stmts)* - #(#add_mod_blocks)* + rhai_generate_into_module(&mut m, false); m } - pub fn rhai_generate_into_module(m: &mut Module) { + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { #(#set_fn_stmts)* #(#set_const_stmts)* - #(#set_mod_blocks)* + + if flatten { + #(#set_flattened_mod_blocks)* + } else { + #(#add_mod_blocks)* + } } } }) From c63f30a6a23c11ca261550c0140dca3ebf760496 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 14 Sep 2020 11:40:23 +0800 Subject: [PATCH 08/16] Fix tests. --- codegen/src/test/function.rs | 17 +- codegen/src/test/module.rs | 508 ++++++++++++++++++++++++++++------- xxx.rs | 0 3 files changed, 424 insertions(+), 101 deletions(-) delete mode 100644 xxx.rs diff --git a/codegen/src/test/function.rs b/codegen/src/test/function.rs index 5430b311..7e22ea88 100644 --- a/codegen/src/test/function.rs +++ b/codegen/src/test/function.rs @@ -237,11 +237,14 @@ mod generate_tests { let expected = expected.to_string(); if &actual != &expected { let mut counter = 0; - let iter = actual - .chars() - .zip(expected.chars()) - .inspect(|_| counter += 1) - .skip_while(|(a, e)| *a == *e); + let iter = actual.chars().zip(expected.chars()).skip_while(|(a, e)| { + if *a == *e { + counter += 1; + true + } else { + false + } + }); let (actual_diff, expected_diff) = { let mut actual_diff = String::new(); let mut expected_diff = String::new(); @@ -252,8 +255,10 @@ mod generate_tests { (actual_diff, expected_diff) }; eprintln!("actual != expected, diverge at char {}", counter); + eprintln!(" actual: {}", actual_diff); + eprintln!("expected: {}", expected_diff); + assert!(false); } - assert_eq!(actual, expected); } #[test] diff --git a/codegen/src/test/module.rs b/codegen/src/test/module.rs index 677cd040..5e9b5433 100644 --- a/codegen/src/test/module.rs +++ b/codegen/src/test/module.rs @@ -218,12 +218,15 @@ mod generate_tests { let expected = expected.to_string(); if &actual != &expected { let mut counter = 0; - let iter = actual - .chars() - .zip(expected.chars()) - .inspect(|_| counter += 1) - .skip_while(|(a, e)| *a == *e); - let (_actual_diff, _expected_diff) = { + let iter = actual.chars().zip(expected.chars()).skip_while(|(a, e)| { + if *a == *e { + counter += 1; + true + } else { + false + } + }); + let (actual_diff, expected_diff) = { let mut actual_diff = String::new(); let mut expected_diff = String::new(); for (a, e) in iter.take(50) { @@ -233,8 +236,11 @@ mod generate_tests { (actual_diff, expected_diff) }; eprintln!("actual != expected, diverge at char {}", counter); + eprintln!(" actual: {}", actual_diff); + eprintln!("expected: {}", expected_diff); + assert!(false); } - assert_eq!(actual, expected); + //assert_eq!(actual, expected); } #[test] @@ -244,14 +250,20 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod empty { #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + if flatten {} else {} + } } }; @@ -270,18 +282,24 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_fn { pub fn get_mystic_number() -> INT { 42 } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); + m + } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { m.set_fn("get_mystic_number", FnAccess::Public, &[], CallableFunction::from_plugin(get_mystic_number_token())); - m + if flatten {} else {} } #[allow(non_camel_case_types)] struct get_mystic_number_token(); @@ -327,18 +345,24 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_fn { pub fn add_one_to(x: INT) -> INT { x + 1 } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); + m + } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { m.set_fn("add_one_to", FnAccess::Public, &[core::any::TypeId::of::()], CallableFunction::from_plugin(add_one_to_token())); - m + if flatten {} else {} } #[allow(non_camel_case_types)] struct add_one_to_token(); @@ -391,6 +415,7 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod two_fns { pub fn add_one_to(x: INT) -> INT { x + 1 @@ -402,17 +427,21 @@ mod generate_tests { #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); + m + } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { m.set_fn("add_n", FnAccess::Public, &[core::any::TypeId::of::()], CallableFunction::from_plugin(add_one_to_token())); m.set_fn("add_n", FnAccess::Public, &[core::any::TypeId::of::(), core::any::TypeId::of::()], CallableFunction::from_plugin(add_n_to_token())); - m + if flatten {} else {} } - #[allow(non_camel_case_types)] struct add_one_to_token(); impl PluginFunction for add_one_to_token { @@ -488,19 +517,25 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_fn { pub fn add_together(x: INT, y: INT) -> INT { x + y } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); + m + } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { m.set_fn("add_together", FnAccess::Public, &[core::any::TypeId::of::(), core::any::TypeId::of::()], CallableFunction::from_plugin(add_together_token())); - m + if flatten {} else {} } #[allow(non_camel_case_types)] struct add_together_token(); @@ -550,15 +585,21 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_fn { pub fn add_together(x: INT, y: INT) -> INT { x + y } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); + m + } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { m.set_fn("add", FnAccess::Public, &[core::any::TypeId::of::(), core::any::TypeId::of::()], CallableFunction::from_plugin(add_together_token())); @@ -568,7 +609,7 @@ mod generate_tests { m.set_fn("add_together", FnAccess::Public, &[core::any::TypeId::of::(), core::any::TypeId::of::()], CallableFunction::from_plugin(add_together_token())); - m + if flatten {} else {} } #[allow(non_camel_case_types)] struct add_together_token(); @@ -606,6 +647,44 @@ mod generate_tests { assert_streams_eq(item_mod.generate(), expected_tokens); } + #[test] + fn one_constant_type_module() { + let input_tokens: TokenStream = quote! { + pub mod one_constant { + #[derive(Debug, Clone)] + pub struct Foo(pub INT); + + pub const MYSTIC_NUMBER: Foo = Foo(42); + } + }; + + let expected_tokens = quote! { + #[allow(dead_code)] + pub mod one_constant { + #[derive(Debug, Clone)] + pub struct Foo(pub INT); + + pub const MYSTIC_NUMBER: Foo = Foo(42); + #[allow(unused_imports)] + use super::*; + + pub fn rhai_module_generate() -> Module { + let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); + m + } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + m.set_var("MYSTIC_NUMBER", (Foo(42)) as Foo); + if flatten {} else {} + } + } + }; + + let item_mod = syn::parse2::(input_tokens).unwrap(); + assert_streams_eq(item_mod.generate(), expected_tokens); + } + #[test] fn one_constant_module() { let input_tokens: TokenStream = quote! { @@ -615,16 +694,22 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_constant { pub const MYSTIC_NUMBER: INT = 42; #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); - m.set_var("MYSTIC_NUMBER", 42); + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + m.set_var("MYSTIC_NUMBER", (42) as INT); + if flatten {} else {} + } } }; @@ -642,17 +727,23 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_constant { pub use rhai::INT; pub const MYSTIC_NUMBER: INT = 42; #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); - m.set_var("MYSTIC_NUMBER", 42); + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + m.set_var("MYSTIC_NUMBER", (42) as INT); + if flatten {} else {} + } } }; @@ -671,17 +762,23 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_fn { fn get_mystic_number() -> INT { 42 } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + if flatten {} else {} + } } }; @@ -701,17 +798,23 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_fn { pub fn get_mystic_number() -> INT { 42 } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + if flatten {} else {} + } } }; @@ -734,6 +837,7 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_fn { pub fn get_mystic_number() -> INT { 42 @@ -743,12 +847,17 @@ mod generate_tests { } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); + m + } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { m.set_fn("get_mystic_number", FnAccess::Public, &[], CallableFunction::from_plugin(get_mystic_number_token())); - m + if flatten {} else {} } #[allow(non_camel_case_types)] struct get_mystic_number_token(); @@ -792,15 +901,21 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_constant { const MYSTIC_NUMBER: INT = 42; #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + if flatten {} else {} + } } }; @@ -819,19 +934,25 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod str_fn { pub fn print_out_to(x: &str) { x + 1 } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); + m + } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { m.set_fn("print_out_to", FnAccess::Public, &[core::any::TypeId::of::()], CallableFunction::from_plugin(print_out_to_token())); - m + if flatten {} else {} } #[allow(non_camel_case_types)] struct print_out_to_token(); @@ -878,19 +999,25 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod ref_fn { pub fn increment(x: &mut FLOAT) { *x += 1.0 as FLOAT; } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); + m + } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { m.set_fn("increment", FnAccess::Public, &[core::any::TypeId::of::()], CallableFunction::from_plugin(increment_token())); - m + if flatten {} else {} } #[allow(non_camel_case_types)] struct increment_token(); @@ -939,20 +1066,27 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_fn { + #[allow(dead_code)] pub mod it_is { pub fn increment(x: &mut FLOAT) { *x += 1.0 as FLOAT; } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); + m + } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { m.set_fn("increment", FnAccess::Public, &[core::any::TypeId::of::()], CallableFunction::from_plugin(increment_token())); - m + if flatten {} else {} } #[allow(non_camel_case_types)] struct increment_token(); @@ -984,12 +1118,20 @@ mod generate_tests { } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); - { m.set_sub_module("it_is", self::it_is::rhai_module_generate()); } + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + if flatten { + { self::it_is::rhai_generate_into_module(m, flatten); } + } else { + { m.set_sub_module("it_is", self::it_is::rhai_module_generate()); } + } + } } }; @@ -1011,21 +1153,28 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_fn { #[cfg(not(feature = "no_float"))] + #[allow(dead_code)] pub mod it_is { pub fn increment(x: &mut FLOAT) { *x += 1.0 as FLOAT; } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); + m + } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { m.set_fn("increment", FnAccess::Public, &[core::any::TypeId::of::()], CallableFunction::from_plugin(increment_token())); - m + if flatten {} else {} } #[allow(non_camel_case_types)] struct increment_token(); @@ -1057,14 +1206,24 @@ mod generate_tests { } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); - #[cfg(not(feature = "no_float"))] { - m.set_sub_module("it_is", self::it_is::rhai_module_generate()); - } + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + if flatten { + #[cfg(not(feature = "no_float"))] { + self::it_is::rhai_generate_into_module(m, flatten); + } + } else { + #[cfg(not(feature = "no_float"))] { + m.set_sub_module("it_is", self::it_is::rhai_module_generate()); + } + } + } } }; @@ -1084,18 +1243,24 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_fn { pub fn int_foo(x: &mut u64) -> u64 { (*x) * (*x) } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); + m + } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { m.set_fn("get$square", FnAccess::Public, &[core::any::TypeId::of::()], CallableFunction::from_plugin(int_foo_token())); - m + if flatten {} else {} } #[allow(non_camel_case_types)] struct int_foo_token(); @@ -1143,20 +1308,26 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_fn { pub fn int_foo(x: &mut u64) -> u64 { (*x) * (*x) } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); + m + } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { m.set_fn("square", FnAccess::Public, &[core::any::TypeId::of::()], CallableFunction::from_plugin(int_foo_token())); m.set_fn("get$square", FnAccess::Public, &[core::any::TypeId::of::()], CallableFunction::from_plugin(int_foo_token())); - m + if flatten {} else {} } #[allow(non_camel_case_types)] struct int_foo_token(); @@ -1204,20 +1375,26 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_fn { pub fn int_foo(x: &mut u64, y: u64) { *x = y * y } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); + m + } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { m.set_fn("set$squared", FnAccess::Public, &[core::any::TypeId::of::(), core::any::TypeId::of::()], CallableFunction::from_plugin(int_foo_token())); - m + if flatten {} else {} } #[allow(non_camel_case_types)] struct int_foo_token(); @@ -1266,15 +1443,21 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_fn { pub fn int_foo(x: &mut u64, y: u64) { *x = y * y } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); + m + } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { m.set_fn("set_sq", FnAccess::Public, &[core::any::TypeId::of::(), core::any::TypeId::of::()], @@ -1283,7 +1466,7 @@ mod generate_tests { &[core::any::TypeId::of::(), core::any::TypeId::of::()], CallableFunction::from_plugin(int_foo_token())); - m + if flatten {} else {} } #[allow(non_camel_case_types)] struct int_foo_token(); @@ -1332,20 +1515,26 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_index_fn { pub fn get_by_index(x: &mut MyCollection, i: u64) -> FLOAT { x.get(i) } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); + m + } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { m.set_fn("index$get$", FnAccess::Public, &[core::any::TypeId::of::(), core::any::TypeId::of::()], CallableFunction::from_plugin(get_by_index_token())); - m + if flatten {} else {} } #[allow(non_camel_case_types)] struct get_by_index_token(); @@ -1395,15 +1584,21 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_index_fn { pub fn get_by_index(x: &mut MyCollection, i: u64) -> FLOAT { x.get(i) } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); + m + } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { m.set_fn("get", FnAccess::Public, &[core::any::TypeId::of::(), core::any::TypeId::of::()], @@ -1412,7 +1607,7 @@ mod generate_tests { &[core::any::TypeId::of::(), core::any::TypeId::of::()], CallableFunction::from_plugin(get_by_index_token())); - m + if flatten {} else {} } #[allow(non_camel_case_types)] struct get_by_index_token(); @@ -1462,21 +1657,27 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_index_fn { pub fn set_by_index(x: &mut MyCollection, i: u64, item: FLOAT) { x.entry(i).set(item) } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); + m + } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { m.set_fn("index$set$", FnAccess::Public, &[core::any::TypeId::of::(), core::any::TypeId::of::(), core::any::TypeId::of::()], CallableFunction::from_plugin(set_by_index_token())); - m + if flatten {} else {} } #[allow(non_camel_case_types)] struct set_by_index_token(); @@ -1528,15 +1729,21 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_index_fn { pub fn set_by_index(x: &mut MyCollection, i: u64, item: FLOAT) { x.entry(i).set(item) } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); + rhai_generate_into_module(&mut m, false); + m + } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { m.set_fn("set", FnAccess::Public, &[core::any::TypeId::of::(), core::any::TypeId::of::(), @@ -1547,7 +1754,7 @@ mod generate_tests { core::any::TypeId::of::(), core::any::TypeId::of::()], CallableFunction::from_plugin(set_by_index_token())); - m + if flatten {} else {} } #[allow(non_camel_case_types)] struct set_by_index_token(); @@ -1598,26 +1805,41 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod one_constant { + #[allow(dead_code)] pub mod it_is { pub const MYSTIC_NUMBER: INT = 42; #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); - m.set_var("MYSTIC_NUMBER", 42); + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + m.set_var("MYSTIC_NUMBER", (42) as INT); + if flatten {} else {} + } } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); - { m.set_sub_module("it_is", self::it_is::rhai_module_generate()); } + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + if flatten { + { self::it_is::rhai_generate_into_module(m, flatten); } + } else { + { m.set_sub_module("it_is", self::it_is::rhai_module_generate()); } + } + } } }; @@ -1639,38 +1861,60 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod two_constants { + #[allow(dead_code)] pub mod first_is { pub const MYSTIC_NUMBER: INT = 42; #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); - m.set_var("MYSTIC_NUMBER", 42); + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + m.set_var("MYSTIC_NUMBER", (42) as INT); + if flatten {} else {} + } } + #[allow(dead_code)] pub mod second_is { pub const SPECIAL_CPU_NUMBER: INT = 68000; #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); - m.set_var("SPECIAL_CPU_NUMBER", 68000); + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + m.set_var("SPECIAL_CPU_NUMBER", (68000) as INT); + if flatten {} else {} + } } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); - { m.set_sub_module("first_is", self::first_is::rhai_module_generate()); } - { m.set_sub_module("second_is", self::second_is::rhai_module_generate()); } + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + if flatten { + { self::first_is::rhai_generate_into_module(m, flatten); } + { self::second_is::rhai_generate_into_module(m, flatten); } + } else { + { m.set_sub_module("first_is", self::first_is::rhai_module_generate()); } + { m.set_sub_module("second_is", self::second_is::rhai_module_generate()); } + } + } } }; @@ -1711,112 +1955,186 @@ mod generate_tests { }; let expected_tokens = quote! { + #[allow(dead_code)] pub mod heap_root { pub const VALUE: INT = 100; + #[allow(dead_code)] pub mod left { pub const VALUE: INT = 19; + #[allow(dead_code)] pub mod left { pub const VALUE: INT = 17; + #[allow(dead_code)] pub mod left { pub const VALUE: INT = 2; #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); - m.set_var("VALUE", 2); + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + m.set_var("VALUE", (2) as INT); + if flatten {} else {} + } } + #[allow(dead_code)] pub mod right { pub const VALUE: INT = 7; #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); - m.set_var("VALUE", 7); + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + m.set_var("VALUE", (7) as INT); + if flatten {} else {} + } } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); - m.set_var("VALUE", 17); - { m.set_sub_module("left", self::left::rhai_module_generate()); } - { m.set_sub_module("right", self::right::rhai_module_generate()); } + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + m.set_var("VALUE", (17) as INT); + + if flatten { + { self::left::rhai_generate_into_module(m, flatten); } + { self::right::rhai_generate_into_module(m, flatten); } + } else { + { m.set_sub_module("left", self::left::rhai_module_generate()); } + { m.set_sub_module("right", self::right::rhai_module_generate()); } + } + } } + #[allow(dead_code)] pub mod right { pub const VALUE: INT = 3; #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); - m.set_var("VALUE", 3); + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + m.set_var("VALUE", (3) as INT); + if flatten {} else {} + } } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); - m.set_var("VALUE", 19); - { m.set_sub_module("left", self::left::rhai_module_generate()); } - { m.set_sub_module("right", self::right::rhai_module_generate()); } + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + m.set_var("VALUE", (19) as INT); + + if flatten { + { self::left::rhai_generate_into_module(m, flatten); } + { self::right::rhai_generate_into_module(m, flatten); } + } else { + { m.set_sub_module("left", self::left::rhai_module_generate()); } + { m.set_sub_module("right", self::right::rhai_module_generate()); } + } + } } + #[allow(dead_code)] pub mod right { pub const VALUE: INT = 36; + #[allow(dead_code)] pub mod left { pub const VALUE: INT = 25; #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); - m.set_var("VALUE", 25); + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + m.set_var("VALUE", (25) as INT); + if flatten {} else {} + } } + #[allow(dead_code)] pub mod right { pub const VALUE: INT = 1; #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); - m.set_var("VALUE", 1); + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + m.set_var("VALUE", (1) as INT); + if flatten {} else {} + } } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); - m.set_var("VALUE", 36); - { m.set_sub_module("left", self::left::rhai_module_generate()); } - { m.set_sub_module("right", self::right::rhai_module_generate()); } + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + m.set_var("VALUE", (36) as INT); + + if flatten { + { self::left::rhai_generate_into_module(m, flatten); } + { self::right::rhai_generate_into_module(m, flatten); } + } else { + { m.set_sub_module("left", self::left::rhai_module_generate()); } + { m.set_sub_module("right", self::right::rhai_module_generate()); } + } + } } #[allow(unused_imports)] use super::*; - #[allow(unused_mut)] + pub fn rhai_module_generate() -> Module { let mut m = Module::new(); - m.set_var("VALUE", 100); - { m.set_sub_module("left", self::left::rhai_module_generate()); } - { m.set_sub_module("right", self::right::rhai_module_generate()); } + rhai_generate_into_module(&mut m, false); m } + #[allow(unused_mut)] + pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { + m.set_var("VALUE", (100) as INT); + + if flatten { + { self::left::rhai_generate_into_module(m, flatten); } + { self::right::rhai_generate_into_module(m, flatten); } + } else { + { m.set_sub_module("left", self::left::rhai_module_generate()); } + { m.set_sub_module("right", self::right::rhai_module_generate()); } + } + } } }; diff --git a/xxx.rs b/xxx.rs deleted file mode 100644 index e69de29b..00000000 From 7ceb624ec191c937c49a675af9d981ee0fd8a926 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 13 Sep 2020 22:12:11 +0800 Subject: [PATCH 09/16] Add combine_with_exported_module. --- codegen/src/lib.rs | 2 +- codegen/src/rhai_module.rs | 11 ++++++++--- doc/src/rust/packages/create.md | 12 ------------ tests/plugins.rs | 2 +- xxx.rs | 0 5 files changed, 10 insertions(+), 17 deletions(-) create mode 100644 xxx.rs diff --git a/codegen/src/lib.rs b/codegen/src/lib.rs index 4426f173..3763b47e 100644 --- a/codegen/src/lib.rs +++ b/codegen/src/lib.rs @@ -161,7 +161,7 @@ pub fn combine_with_exported_module(args: proc_macro::TokenStream) -> proc_macro Err(e) => return e.to_compile_error().into(), }; let tokens = quote! { - #module_path::rhai_generate_into_module(#module_expr, true); + #module_path::rhai_generate_into_module(#module_expr); }; proc_macro::TokenStream::from(tokens) } diff --git a/codegen/src/rhai_module.rs b/codegen/src/rhai_module.rs index 66f23390..1661df43 100644 --- a/codegen/src/rhai_module.rs +++ b/codegen/src/rhai_module.rs @@ -17,7 +17,7 @@ pub(crate) fn generate_body( let mut set_fn_stmts: Vec = Vec::new(); let mut set_const_stmts: Vec = Vec::new(); let mut add_mod_blocks: Vec = Vec::new(); - let mut set_flattened_mod_blocks: Vec = Vec::new(); + let mut set_mod_blocks: Vec = Vec::new(); let str_type_path = syn::parse2::(quote! { str }).unwrap(); for (const_name, const_type, const_expr) in consts { @@ -55,10 +55,10 @@ pub(crate) fn generate_body( }) .unwrap(), ); - set_flattened_mod_blocks.push( + set_mod_blocks.push( syn::parse2::(quote! { #(#cfg_attrs)* { - self::#module_name::rhai_generate_into_module(m, flatten); + self::#module_name::rhai_generate_into_module(m); } }) .unwrap(), @@ -155,6 +155,11 @@ pub(crate) fn generate_body( #(#add_mod_blocks)* } } + pub fn rhai_generate_into_module(m: &mut Module) { + #(#set_fn_stmts)* + #(#set_const_stmts)* + #(#set_mod_blocks)* + } } }) .unwrap(); diff --git a/doc/src/rust/packages/create.md b/doc/src/rust/packages/create.md index 53ac17a0..c4ee27e4 100644 --- a/doc/src/rust/packages/create.md +++ b/doc/src/rust/packages/create.md @@ -66,9 +66,6 @@ In fact, this exactly is how Rhai's built-in packages, such as `BasicMathPackage `rhai::plugins::combine_with_exported_module!` adds all functions and constants from the [plugins][plugin module] definition into the package itself. -All sub-modules are _flattened_ (i.e. all functions and constants defined within sub-modules are registered -at the top level) and so there will not be any sub-modules added to the package. - ```rust // Import necessary types and traits. use rhai::{ @@ -107,17 +104,8 @@ def_package!(rhai:MyPackage:"My own personal super package", module, { // Merge all registered functions and constants from the plugin module into the custom package. // - // Functions in the sub-module 'my_sub_module' are flattened and registered at the top level - // instead of in a sub-module. - // // The text string name in the middle parameter can be anything and is reserved for future use; // it is recommended to be an ID string that uniquely identifies the module. - // - // This call ends up registering three functions at the top level of the package: - // 1) greet - // 2) get_num - // 3) get_sub_num (flattened from sub-module 'my_sub_module') - // combine_with_exported_module!(module, "my-functions", my_module)); }); ``` diff --git a/tests/plugins.rs b/tests/plugins.rs index 2bee84e4..928f0381 100644 --- a/tests/plugins.rs +++ b/tests/plugins.rs @@ -11,7 +11,7 @@ mod test { pub mod special_array_package { use rhai::{Array, INT}; - pub const MYSTIC_NUMBER: INT = 42; + pub const MYSTIC_NUMBER: INT = 42 as INT; #[cfg(not(feature = "no_object"))] pub mod feature { diff --git a/xxx.rs b/xxx.rs new file mode 100644 index 00000000..e69de29b From d1d3aaf374db6e604523d180eb22f183211f6863 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 13 Sep 2020 22:36:24 +0800 Subject: [PATCH 10/16] Add cast to type for constants to avoid mis-typing. --- tests/plugins.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/plugins.rs b/tests/plugins.rs index 928f0381..2bee84e4 100644 --- a/tests/plugins.rs +++ b/tests/plugins.rs @@ -11,7 +11,7 @@ mod test { pub mod special_array_package { use rhai::{Array, INT}; - pub const MYSTIC_NUMBER: INT = 42 as INT; + pub const MYSTIC_NUMBER: INT = 42; #[cfg(not(feature = "no_object"))] pub mod feature { From 1060ff35fd9a2c42173ddbbe1408ddb72a4bc8e6 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 13 Sep 2020 23:13:07 +0800 Subject: [PATCH 11/16] Fix tests. --- doc/src/rust/packages/create.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/doc/src/rust/packages/create.md b/doc/src/rust/packages/create.md index c4ee27e4..53ac17a0 100644 --- a/doc/src/rust/packages/create.md +++ b/doc/src/rust/packages/create.md @@ -66,6 +66,9 @@ In fact, this exactly is how Rhai's built-in packages, such as `BasicMathPackage `rhai::plugins::combine_with_exported_module!` adds all functions and constants from the [plugins][plugin module] definition into the package itself. +All sub-modules are _flattened_ (i.e. all functions and constants defined within sub-modules are registered +at the top level) and so there will not be any sub-modules added to the package. + ```rust // Import necessary types and traits. use rhai::{ @@ -104,8 +107,17 @@ def_package!(rhai:MyPackage:"My own personal super package", module, { // Merge all registered functions and constants from the plugin module into the custom package. // + // Functions in the sub-module 'my_sub_module' are flattened and registered at the top level + // instead of in a sub-module. + // // The text string name in the middle parameter can be anything and is reserved for future use; // it is recommended to be an ID string that uniquely identifies the module. + // + // This call ends up registering three functions at the top level of the package: + // 1) greet + // 2) get_num + // 3) get_sub_num (flattened from sub-module 'my_sub_module') + // combine_with_exported_module!(module, "my-functions", my_module)); }); ``` From 3dc599c93601950e7837d486e86b110e4558ff2d Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 14 Sep 2020 11:40:15 +0800 Subject: [PATCH 12/16] Streamline rhai_generate. --- codegen/src/lib.rs | 2 +- codegen/src/rhai_module.rs | 11 +++-------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/codegen/src/lib.rs b/codegen/src/lib.rs index 3763b47e..4426f173 100644 --- a/codegen/src/lib.rs +++ b/codegen/src/lib.rs @@ -161,7 +161,7 @@ pub fn combine_with_exported_module(args: proc_macro::TokenStream) -> proc_macro Err(e) => return e.to_compile_error().into(), }; let tokens = quote! { - #module_path::rhai_generate_into_module(#module_expr); + #module_path::rhai_generate_into_module(#module_expr, true); }; proc_macro::TokenStream::from(tokens) } diff --git a/codegen/src/rhai_module.rs b/codegen/src/rhai_module.rs index 1661df43..66f23390 100644 --- a/codegen/src/rhai_module.rs +++ b/codegen/src/rhai_module.rs @@ -17,7 +17,7 @@ pub(crate) fn generate_body( let mut set_fn_stmts: Vec = Vec::new(); let mut set_const_stmts: Vec = Vec::new(); let mut add_mod_blocks: Vec = Vec::new(); - let mut set_mod_blocks: Vec = Vec::new(); + let mut set_flattened_mod_blocks: Vec = Vec::new(); let str_type_path = syn::parse2::(quote! { str }).unwrap(); for (const_name, const_type, const_expr) in consts { @@ -55,10 +55,10 @@ pub(crate) fn generate_body( }) .unwrap(), ); - set_mod_blocks.push( + set_flattened_mod_blocks.push( syn::parse2::(quote! { #(#cfg_attrs)* { - self::#module_name::rhai_generate_into_module(m); + self::#module_name::rhai_generate_into_module(m, flatten); } }) .unwrap(), @@ -155,11 +155,6 @@ pub(crate) fn generate_body( #(#add_mod_blocks)* } } - pub fn rhai_generate_into_module(m: &mut Module) { - #(#set_fn_stmts)* - #(#set_const_stmts)* - #(#set_mod_blocks)* - } } }) .unwrap(); From c692cd7e359e74fbf7602431c7e615fec98ead27 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 14 Sep 2020 11:40:23 +0800 Subject: [PATCH 13/16] Fix tests. --- xxx.rs | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 xxx.rs diff --git a/xxx.rs b/xxx.rs deleted file mode 100644 index e69de29b..00000000 From 5e4ba9c0163cdf6b6b26eb6a7d934bdb47444eef Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 17 Sep 2020 11:37:23 +0800 Subject: [PATCH 14/16] Swich back to assert_eq! for token stream tests. --- codegen/src/test/function.rs | 5 ++++- codegen/src/test/module.rs | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/codegen/src/test/function.rs b/codegen/src/test/function.rs index 7e22ea88..a8ded0c0 100644 --- a/codegen/src/test/function.rs +++ b/codegen/src/test/function.rs @@ -245,6 +245,8 @@ mod generate_tests { false } }); + eprintln!("actual != expected, diverge at char {}", counter); + /* let (actual_diff, expected_diff) = { let mut actual_diff = String::new(); let mut expected_diff = String::new(); @@ -254,11 +256,12 @@ mod generate_tests { } (actual_diff, expected_diff) }; - eprintln!("actual != expected, diverge at char {}", counter); eprintln!(" actual: {}", actual_diff); eprintln!("expected: {}", expected_diff); assert!(false); + */ } + assert_eq!(actual, expected); } #[test] diff --git a/codegen/src/test/module.rs b/codegen/src/test/module.rs index 5e9b5433..6743b6db 100644 --- a/codegen/src/test/module.rs +++ b/codegen/src/test/module.rs @@ -226,6 +226,8 @@ mod generate_tests { false } }); + eprintln!("actual != expected, diverge at char {}", counter); + /* let (actual_diff, expected_diff) = { let mut actual_diff = String::new(); let mut expected_diff = String::new(); @@ -235,12 +237,12 @@ mod generate_tests { } (actual_diff, expected_diff) }; - eprintln!("actual != expected, diverge at char {}", counter); eprintln!(" actual: {}", actual_diff); eprintln!("expected: {}", expected_diff); assert!(false); + */ } - //assert_eq!(actual, expected); + assert_eq!(actual, expected); } #[test] From c07c4561eb8cd74e6ce28ad4fb948b2264ed2b28 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 17 Sep 2020 11:40:31 +0800 Subject: [PATCH 15/16] Remove unused_variables. --- codegen/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/codegen/src/lib.rs b/codegen/src/lib.rs index 4426f173..5da8e1b4 100644 --- a/codegen/src/lib.rs +++ b/codegen/src/lib.rs @@ -154,8 +154,7 @@ pub fn exported_module(module_path: proc_macro::TokenStream) -> proc_macro::Toke #[proc_macro] pub fn combine_with_exported_module(args: proc_macro::TokenStream) -> proc_macro::TokenStream { - #[allow(unused_variables)] - let (module_expr, export_name, module_path) = match crate::register::parse_register_macro(args) + let (module_expr, _export_name, module_path) = match crate::register::parse_register_macro(args) { Ok(triple) => triple, Err(e) => return e.to_compile_error().into(), From 92be487e8072382edd58c580bcb2b05118dbade0 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 17 Sep 2020 11:56:10 +0800 Subject: [PATCH 16/16] Implement constants via calling the constant. --- codegen/src/module.rs | 1 - codegen/src/rhai_module.rs | 5 ++- codegen/src/test/function.rs | 4 +- codegen/src/test/module.rs | 76 +++++++++--------------------------- 4 files changed, 23 insertions(+), 63 deletions(-) diff --git a/codegen/src/module.rs b/codegen/src/module.rs index 4b829693..85b1278c 100644 --- a/codegen/src/module.rs +++ b/codegen/src/module.rs @@ -282,7 +282,6 @@ impl Module { // Regenerate the module with the new content added. Ok(quote! { #(#mod_attrs)* - #[allow(dead_code)] pub mod #mod_name { #(#orig_content)* #(#inner_modules)* diff --git a/codegen/src/rhai_module.rs b/codegen/src/rhai_module.rs index 66f23390..1859ba42 100644 --- a/codegen/src/rhai_module.rs +++ b/codegen/src/rhai_module.rs @@ -20,11 +20,12 @@ pub(crate) fn generate_body( let mut set_flattened_mod_blocks: Vec = Vec::new(); let str_type_path = syn::parse2::(quote! { str }).unwrap(); - for (const_name, const_type, const_expr) in consts { + for (const_name, _, _) in consts { let const_literal = syn::LitStr::new(&const_name, proc_macro2::Span::call_site()); + let const_ref = syn::Ident::new(&const_name, proc_macro2::Span::call_site()); set_const_stmts.push( syn::parse2::(quote! { - m.set_var(#const_literal, (#const_expr) as #const_type); + m.set_var(#const_literal, #const_ref); }) .unwrap(), ); diff --git a/codegen/src/test/function.rs b/codegen/src/test/function.rs index a8ded0c0..a4a54959 100644 --- a/codegen/src/test/function.rs +++ b/codegen/src/test/function.rs @@ -237,7 +237,7 @@ mod generate_tests { let expected = expected.to_string(); if &actual != &expected { let mut counter = 0; - let iter = actual.chars().zip(expected.chars()).skip_while(|(a, e)| { + let _iter = actual.chars().zip(expected.chars()).skip_while(|(a, e)| { if *a == *e { counter += 1; true @@ -250,7 +250,7 @@ mod generate_tests { let (actual_diff, expected_diff) = { let mut actual_diff = String::new(); let mut expected_diff = String::new(); - for (a, e) in iter.take(50) { + for (a, e) in _iter.take(50) { actual_diff.push(a); expected_diff.push(e); } diff --git a/codegen/src/test/module.rs b/codegen/src/test/module.rs index 6743b6db..b560e94b 100644 --- a/codegen/src/test/module.rs +++ b/codegen/src/test/module.rs @@ -218,7 +218,7 @@ mod generate_tests { let expected = expected.to_string(); if &actual != &expected { let mut counter = 0; - let iter = actual.chars().zip(expected.chars()).skip_while(|(a, e)| { + let _iter = actual.chars().zip(expected.chars()).skip_while(|(a, e)| { if *a == *e { counter += 1; true @@ -231,7 +231,7 @@ mod generate_tests { let (actual_diff, expected_diff) = { let mut actual_diff = String::new(); let mut expected_diff = String::new(); - for (a, e) in iter.take(50) { + for (a, e) in _iter.take(50) { actual_diff.push(a); expected_diff.push(e); } @@ -252,7 +252,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod empty { #[allow(unused_imports)] use super::*; @@ -284,7 +283,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_fn { pub fn get_mystic_number() -> INT { 42 @@ -347,7 +345,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_fn { pub fn add_one_to(x: INT) -> INT { x + 1 @@ -417,7 +414,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod two_fns { pub fn add_one_to(x: INT) -> INT { x + 1 @@ -519,7 +515,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_fn { pub fn add_together(x: INT, y: INT) -> INT { x + y @@ -587,7 +582,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_fn { pub fn add_together(x: INT, y: INT) -> INT { x + y @@ -661,7 +655,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_constant { #[derive(Debug, Clone)] pub struct Foo(pub INT); @@ -677,7 +670,7 @@ mod generate_tests { } #[allow(unused_mut)] pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { - m.set_var("MYSTIC_NUMBER", (Foo(42)) as Foo); + m.set_var("MYSTIC_NUMBER", MYSTIC_NUMBER); if flatten {} else {} } } @@ -696,7 +689,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_constant { pub const MYSTIC_NUMBER: INT = 42; #[allow(unused_imports)] @@ -709,7 +701,7 @@ mod generate_tests { } #[allow(unused_mut)] pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { - m.set_var("MYSTIC_NUMBER", (42) as INT); + m.set_var("MYSTIC_NUMBER", MYSTIC_NUMBER); if flatten {} else {} } } @@ -729,7 +721,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_constant { pub use rhai::INT; pub const MYSTIC_NUMBER: INT = 42; @@ -743,7 +734,7 @@ mod generate_tests { } #[allow(unused_mut)] pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { - m.set_var("MYSTIC_NUMBER", (42) as INT); + m.set_var("MYSTIC_NUMBER", MYSTIC_NUMBER); if flatten {} else {} } } @@ -764,7 +755,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_fn { fn get_mystic_number() -> INT { 42 @@ -800,7 +790,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_fn { pub fn get_mystic_number() -> INT { 42 @@ -839,7 +828,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_fn { pub fn get_mystic_number() -> INT { 42 @@ -903,7 +891,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_constant { const MYSTIC_NUMBER: INT = 42; #[allow(unused_imports)] @@ -936,7 +923,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod str_fn { pub fn print_out_to(x: &str) { x + 1 @@ -1001,7 +987,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod ref_fn { pub fn increment(x: &mut FLOAT) { *x += 1.0 as FLOAT; @@ -1068,9 +1053,7 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_fn { - #[allow(dead_code)] pub mod it_is { pub fn increment(x: &mut FLOAT) { *x += 1.0 as FLOAT; @@ -1155,10 +1138,9 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_fn { #[cfg(not(feature = "no_float"))] - #[allow(dead_code)] + pub mod it_is { pub fn increment(x: &mut FLOAT) { *x += 1.0 as FLOAT; @@ -1245,7 +1227,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_fn { pub fn int_foo(x: &mut u64) -> u64 { (*x) * (*x) @@ -1310,7 +1291,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_fn { pub fn int_foo(x: &mut u64) -> u64 { (*x) * (*x) @@ -1377,7 +1357,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_fn { pub fn int_foo(x: &mut u64, y: u64) { *x = y * y @@ -1445,7 +1424,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_fn { pub fn int_foo(x: &mut u64, y: u64) { *x = y * y @@ -1517,7 +1495,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_index_fn { pub fn get_by_index(x: &mut MyCollection, i: u64) -> FLOAT { x.get(i) @@ -1586,7 +1563,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_index_fn { pub fn get_by_index(x: &mut MyCollection, i: u64) -> FLOAT { x.get(i) @@ -1659,7 +1635,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_index_fn { pub fn set_by_index(x: &mut MyCollection, i: u64, item: FLOAT) { x.entry(i).set(item) @@ -1731,7 +1706,6 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_index_fn { pub fn set_by_index(x: &mut MyCollection, i: u64, item: FLOAT) { x.entry(i).set(item) @@ -1807,9 +1781,7 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod one_constant { - #[allow(dead_code)] pub mod it_is { pub const MYSTIC_NUMBER: INT = 42; #[allow(unused_imports)] @@ -1822,7 +1794,7 @@ mod generate_tests { } #[allow(unused_mut)] pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { - m.set_var("MYSTIC_NUMBER", (42) as INT); + m.set_var("MYSTIC_NUMBER", MYSTIC_NUMBER); if flatten {} else {} } } @@ -1863,9 +1835,7 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod two_constants { - #[allow(dead_code)] pub mod first_is { pub const MYSTIC_NUMBER: INT = 42; #[allow(unused_imports)] @@ -1878,11 +1848,10 @@ mod generate_tests { } #[allow(unused_mut)] pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { - m.set_var("MYSTIC_NUMBER", (42) as INT); + m.set_var("MYSTIC_NUMBER", MYSTIC_NUMBER); if flatten {} else {} } } - #[allow(dead_code)] pub mod second_is { pub const SPECIAL_CPU_NUMBER: INT = 68000; #[allow(unused_imports)] @@ -1895,7 +1864,7 @@ mod generate_tests { } #[allow(unused_mut)] pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { - m.set_var("SPECIAL_CPU_NUMBER", (68000) as INT); + m.set_var("SPECIAL_CPU_NUMBER", SPECIAL_CPU_NUMBER); if flatten {} else {} } } @@ -1957,16 +1926,12 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(dead_code)] pub mod heap_root { pub const VALUE: INT = 100; - #[allow(dead_code)] pub mod left { pub const VALUE: INT = 19; - #[allow(dead_code)] pub mod left { pub const VALUE: INT = 17; - #[allow(dead_code)] pub mod left { pub const VALUE: INT = 2; #[allow(unused_imports)] @@ -1979,11 +1944,10 @@ mod generate_tests { } #[allow(unused_mut)] pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { - m.set_var("VALUE", (2) as INT); + m.set_var("VALUE", VALUE); if flatten {} else {} } } - #[allow(dead_code)] pub mod right { pub const VALUE: INT = 7; #[allow(unused_imports)] @@ -1996,7 +1960,7 @@ mod generate_tests { } #[allow(unused_mut)] pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { - m.set_var("VALUE", (7) as INT); + m.set_var("VALUE", VALUE); if flatten {} else {} } } @@ -2010,7 +1974,7 @@ mod generate_tests { } #[allow(unused_mut)] pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { - m.set_var("VALUE", (17) as INT); + m.set_var("VALUE", VALUE); if flatten { { self::left::rhai_generate_into_module(m, flatten); } @@ -2021,7 +1985,6 @@ mod generate_tests { } } } - #[allow(dead_code)] pub mod right { pub const VALUE: INT = 3; #[allow(unused_imports)] @@ -2034,7 +1997,7 @@ mod generate_tests { } #[allow(unused_mut)] pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { - m.set_var("VALUE", (3) as INT); + m.set_var("VALUE", VALUE); if flatten {} else {} } } @@ -2048,7 +2011,7 @@ mod generate_tests { } #[allow(unused_mut)] pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { - m.set_var("VALUE", (19) as INT); + m.set_var("VALUE", VALUE); if flatten { { self::left::rhai_generate_into_module(m, flatten); } @@ -2059,10 +2022,8 @@ mod generate_tests { } } } - #[allow(dead_code)] pub mod right { pub const VALUE: INT = 36; - #[allow(dead_code)] pub mod left { pub const VALUE: INT = 25; #[allow(unused_imports)] @@ -2075,11 +2036,10 @@ mod generate_tests { } #[allow(unused_mut)] pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { - m.set_var("VALUE", (25) as INT); + m.set_var("VALUE", VALUE); if flatten {} else {} } } - #[allow(dead_code)] pub mod right { pub const VALUE: INT = 1; #[allow(unused_imports)] @@ -2092,7 +2052,7 @@ mod generate_tests { } #[allow(unused_mut)] pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { - m.set_var("VALUE", (1) as INT); + m.set_var("VALUE", VALUE); if flatten {} else {} } } @@ -2106,7 +2066,7 @@ mod generate_tests { } #[allow(unused_mut)] pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { - m.set_var("VALUE", (36) as INT); + m.set_var("VALUE", VALUE); if flatten { { self::left::rhai_generate_into_module(m, flatten); } @@ -2127,7 +2087,7 @@ mod generate_tests { } #[allow(unused_mut)] pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { - m.set_var("VALUE", (100) as INT); + m.set_var("VALUE", VALUE); if flatten { { self::left::rhai_generate_into_module(m, flatten); }