Streamline rhai_generate.

This commit is contained in:
Stephen Chung 2020-09-14 11:40:15 +08:00 committed by J Henry Waugh
parent 654da2db8a
commit 52298bd96c
2 changed files with 14 additions and 10 deletions

View File

@ -161,7 +161,7 @@ pub fn combine_with_exported_module(args: proc_macro::TokenStream) -> proc_macro
Err(e) => return e.to_compile_error().into(), Err(e) => return e.to_compile_error().into(),
}; };
let tokens = quote! { 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) proc_macro::TokenStream::from(tokens)
} }

View File

@ -17,7 +17,7 @@ pub(crate) fn generate_body(
let mut set_fn_stmts: Vec<syn::Stmt> = Vec::new(); let mut set_fn_stmts: Vec<syn::Stmt> = Vec::new();
let mut set_const_stmts: Vec<syn::Stmt> = Vec::new(); let mut set_const_stmts: Vec<syn::Stmt> = Vec::new();
let mut add_mod_blocks: Vec<syn::ExprBlock> = Vec::new(); let mut add_mod_blocks: Vec<syn::ExprBlock> = Vec::new();
let mut set_mod_blocks: Vec<syn::ExprBlock> = Vec::new(); let mut set_flattened_mod_blocks: Vec<syn::ExprBlock> = Vec::new();
let str_type_path = syn::parse2::<syn::Path>(quote! { str }).unwrap(); let str_type_path = syn::parse2::<syn::Path>(quote! { str }).unwrap();
for (const_name, const_type, const_expr) in consts { for (const_name, const_type, const_expr) in consts {
@ -55,10 +55,10 @@ pub(crate) fn generate_body(
}) })
.unwrap(), .unwrap(),
); );
set_mod_blocks.push( set_flattened_mod_blocks.push(
syn::parse2::<syn::ExprBlock>(quote! { syn::parse2::<syn::ExprBlock>(quote! {
#(#cfg_attrs)* { #(#cfg_attrs)* {
self::#module_name::rhai_generate_into_module(m); self::#module_name::rhai_generate_into_module(m, flatten);
} }
}) })
.unwrap(), .unwrap(),
@ -138,18 +138,22 @@ pub(crate) fn generate_body(
pub mod generate_info { pub mod generate_info {
#[allow(unused_imports)] #[allow(unused_imports)]
use super::*; use super::*;
#[allow(unused_mut)]
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
#(#set_fn_stmts)* rhai_generate_into_module(&mut m, false);
#(#set_const_stmts)*
#(#add_mod_blocks)*
m 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_fn_stmts)*
#(#set_const_stmts)* #(#set_const_stmts)*
#(#set_mod_blocks)*
if flatten {
#(#set_flattened_mod_blocks)*
} else {
#(#add_mod_blocks)*
}
} }
} }
}) })