2020-08-01 18:52:26 +02:00
|
|
|
use quote::quote;
|
|
|
|
|
|
|
|
use crate::function::ExportedFn;
|
2020-08-21 05:20:12 +02:00
|
|
|
use crate::module::Module;
|
2020-08-01 18:52:26 +02:00
|
|
|
|
|
|
|
pub(crate) type ExportedConst = (String, syn::Expr);
|
|
|
|
|
|
|
|
pub(crate) fn generate_body(
|
|
|
|
fns: &Vec<ExportedFn>,
|
2020-08-02 09:39:08 +02:00
|
|
|
consts: &Vec<ExportedConst>,
|
2020-08-21 05:20:12 +02:00
|
|
|
submodules: &Vec<Module>,
|
2020-08-01 18:52:26 +02:00
|
|
|
) -> proc_macro2::TokenStream {
|
|
|
|
let mut set_fn_stmts: Vec<syn::Stmt> = Vec::new();
|
|
|
|
let mut set_const_stmts: Vec<syn::Stmt> = Vec::new();
|
2020-08-22 06:05:18 +02:00
|
|
|
let mut add_mod_blocks: Vec<syn::ExprBlock> = Vec::new();
|
2020-08-01 18:52:26 +02:00
|
|
|
let str_type_path = syn::parse2::<syn::Path>(quote! { str }).unwrap();
|
|
|
|
|
|
|
|
for (const_name, const_expr) in consts {
|
|
|
|
let const_literal = syn::LitStr::new(&const_name, proc_macro2::Span::call_site());
|
2020-08-02 09:39:08 +02:00
|
|
|
set_const_stmts.push(
|
|
|
|
syn::parse2::<syn::Stmt>(quote! {
|
|
|
|
m.set_var(#const_literal, #const_expr);
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
);
|
2020-08-01 18:52:26 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 05:20:12 +02:00
|
|
|
for itemmod in submodules {
|
|
|
|
let module_name: &syn::Ident = itemmod.module_name().unwrap();
|
|
|
|
let exported_name: syn::LitStr = if let Some(name) = itemmod.exported_name() {
|
|
|
|
syn::LitStr::new(&name, proc_macro2::Span::call_site())
|
|
|
|
} else {
|
|
|
|
syn::LitStr::new(&module_name.to_string(), proc_macro2::Span::call_site())
|
|
|
|
};
|
2020-08-22 06:05:18 +02:00
|
|
|
let cfg_attrs: Vec<&syn::Attribute> = itemmod.attrs().unwrap().iter().filter(|&a| {
|
|
|
|
a.path.get_ident()
|
|
|
|
.map(|i| i.to_string() == "cfg")
|
|
|
|
.unwrap_or(false)
|
|
|
|
}).collect();
|
|
|
|
add_mod_blocks.push(
|
|
|
|
syn::parse2::<syn::ExprBlock>(quote! {
|
|
|
|
#(#cfg_attrs)* {
|
|
|
|
m.set_sub_module(#exported_name, self::#module_name::rhai_module_generate());
|
|
|
|
}
|
2020-08-21 05:20:12 +02:00
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-01 18:52:26 +02:00
|
|
|
// NB: these are token streams, because reparsing messes up "> >" vs ">>"
|
|
|
|
let mut gen_fn_tokens: Vec<proc_macro2::TokenStream> = Vec::new();
|
|
|
|
for function in fns {
|
2020-08-02 09:39:08 +02:00
|
|
|
let fn_token_name = syn::Ident::new(
|
2020-08-07 01:36:15 +02:00
|
|
|
&format!("{}_token", function.name().to_string()),
|
2020-08-02 09:39:08 +02:00
|
|
|
function.name().span(),
|
|
|
|
);
|
2020-08-17 17:35:04 +02:00
|
|
|
let reg_name = function
|
|
|
|
.params
|
|
|
|
.name
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(|| function.name().to_string());
|
2020-08-16 12:24:42 +02:00
|
|
|
let fn_literal = syn::LitStr::new(®_name, proc_macro2::Span::call_site());
|
2020-08-02 09:39:08 +02:00
|
|
|
let fn_input_types: Vec<syn::Expr> = function
|
|
|
|
.arg_list()
|
2020-08-01 18:52:26 +02:00
|
|
|
.map(|fnarg| match fnarg {
|
|
|
|
syn::FnArg::Receiver(_) => panic!("internal error: receiver fn outside impl!?"),
|
|
|
|
syn::FnArg::Typed(syn::PatType { ref ty, .. }) => {
|
|
|
|
let arg_type = match ty.as_ref() {
|
2020-08-02 09:39:08 +02:00
|
|
|
&syn::Type::Reference(syn::TypeReference {
|
|
|
|
mutability: None,
|
|
|
|
ref elem,
|
|
|
|
..
|
|
|
|
}) => match elem.as_ref() {
|
|
|
|
&syn::Type::Path(ref p) if p.path == str_type_path => {
|
|
|
|
syn::parse2::<syn::Type>(quote! {
|
2020-08-03 02:27:19 +02:00
|
|
|
ImmutableString })
|
2020-08-02 09:39:08 +02:00
|
|
|
.unwrap()
|
2020-08-01 18:52:26 +02:00
|
|
|
}
|
2020-08-02 09:39:08 +02:00
|
|
|
_ => panic!("internal error: non-string shared reference!?"),
|
2020-08-01 18:52:26 +02:00
|
|
|
},
|
2020-08-02 09:39:08 +02:00
|
|
|
&syn::Type::Reference(syn::TypeReference {
|
|
|
|
mutability: Some(_),
|
|
|
|
ref elem,
|
|
|
|
..
|
|
|
|
}) => match elem.as_ref() {
|
|
|
|
&syn::Type::Path(ref p) => syn::parse2::<syn::Type>(quote! {
|
|
|
|
#p })
|
|
|
|
.unwrap(),
|
|
|
|
_ => panic!("internal error: non-string shared reference!?"),
|
2020-08-01 18:52:26 +02:00
|
|
|
},
|
|
|
|
t => t.clone(),
|
|
|
|
};
|
|
|
|
syn::parse2::<syn::Expr>(quote! {
|
2020-08-02 09:39:08 +02:00
|
|
|
core::any::TypeId::of::<#arg_type>()})
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2020-08-01 18:52:26 +02:00
|
|
|
|
2020-08-02 09:39:08 +02:00
|
|
|
set_fn_stmts.push(
|
|
|
|
syn::parse2::<syn::Stmt>(quote! {
|
|
|
|
m.set_fn(#fn_literal, FnAccess::Public, &[#(#fn_input_types),*],
|
2020-08-03 02:27:19 +02:00
|
|
|
CallableFunction::from_plugin(#fn_token_name()));
|
2020-08-02 09:39:08 +02:00
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
);
|
2020-08-01 18:52:26 +02:00
|
|
|
|
|
|
|
gen_fn_tokens.push(quote! {
|
|
|
|
#[allow(non_camel_case_types)]
|
2020-08-03 02:27:19 +02:00
|
|
|
struct #fn_token_name();
|
2020-08-01 18:52:26 +02:00
|
|
|
});
|
|
|
|
gen_fn_tokens.push(function.generate_impl(&fn_token_name.to_string()));
|
2020-08-03 02:27:19 +02:00
|
|
|
gen_fn_tokens.push(function.generate_callable(&fn_token_name.to_string()));
|
|
|
|
gen_fn_tokens.push(function.generate_input_types(&fn_token_name.to_string()));
|
2020-08-01 18:52:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut generate_fncall = syn::parse2::<syn::ItemMod>(quote! {
|
|
|
|
pub mod generate_info {
|
|
|
|
#[allow(unused_imports)]
|
2020-08-03 02:27:19 +02:00
|
|
|
use super::*;
|
2020-08-01 18:52:26 +02:00
|
|
|
#[allow(unused_mut)]
|
2020-08-06 08:10:27 +02:00
|
|
|
pub fn rhai_module_generate() -> Module {
|
2020-08-01 18:52:26 +02:00
|
|
|
let mut m = Module::new();
|
|
|
|
#(#set_fn_stmts)*
|
|
|
|
#(#set_const_stmts)*
|
2020-08-22 06:05:18 +02:00
|
|
|
#(#add_mod_blocks)*
|
2020-08-01 18:52:26 +02:00
|
|
|
m
|
|
|
|
}
|
|
|
|
}
|
2020-08-02 09:39:08 +02:00
|
|
|
})
|
|
|
|
.unwrap();
|
2020-08-01 18:52:26 +02:00
|
|
|
|
|
|
|
let (_, generate_call_content) = generate_fncall.content.take().unwrap();
|
|
|
|
|
|
|
|
quote! {
|
|
|
|
#(#generate_call_content)*
|
|
|
|
#(#gen_fn_tokens)*
|
|
|
|
}
|
|
|
|
}
|