This commit is contained in:
Stephen Chung
2020-08-02 15:39:08 +08:00
parent 11f2b04152
commit 73cda2d4f9
15 changed files with 409 additions and 246 deletions

View File

@@ -6,7 +6,7 @@ pub(crate) type ExportedConst = (String, syn::Expr);
pub(crate) fn generate_body(
fns: &Vec<ExportedFn>,
consts: &Vec<ExportedConst>
consts: &Vec<ExportedConst>,
) -> proc_macro2::TokenStream {
let mut set_fn_stmts: Vec<syn::Stmt> = Vec::new();
let mut set_const_stmts: Vec<syn::Stmt> = Vec::new();
@@ -14,51 +14,67 @@ pub(crate) fn generate_body(
for (const_name, const_expr) in consts {
let const_literal = syn::LitStr::new(&const_name, proc_macro2::Span::call_site());
set_const_stmts.push(syn::parse2::<syn::Stmt>(quote! {
m.set_var(#const_literal, #const_expr);
}).unwrap());
set_const_stmts.push(
syn::parse2::<syn::Stmt>(quote! {
m.set_var(#const_literal, #const_expr);
})
.unwrap(),
);
}
// 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 {
let fn_token_name = syn::Ident::new(&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 fn_input_types: Vec<syn::Expr> = function.arg_list()
let fn_token_name = syn::Ident::new(
&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 fn_input_types: Vec<syn::Expr> = function
.arg_list()
.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() {
&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! {
rhai::ImmutableString }).unwrap(),
_ => panic!("internal error: non-string shared reference!?"),
&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! {
rhai::ImmutableString })
.unwrap()
}
_ => panic!("internal error: non-string shared reference!?"),
},
&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!?"),
}
&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!?"),
},
t => t.clone(),
};
syn::parse2::<syn::Expr>(quote! {
core::any::TypeId::of::<#arg_type>()}).unwrap()
},
}).collect();
core::any::TypeId::of::<#arg_type>()})
.unwrap()
}
})
.collect();
set_fn_stmts.push(syn::parse2::<syn::Stmt>(quote! {
m.set_fn(#fn_literal, FnAccess::Public, &[#(#fn_input_types),*],
rhai::plugin::CallableFunction::from_plugin(#fn_token_name()));
}).unwrap());
set_fn_stmts.push(
syn::parse2::<syn::Stmt>(quote! {
m.set_fn(#fn_literal, FnAccess::Public, &[#(#fn_input_types),*],
rhai::plugin::CallableFunction::from_plugin(#fn_token_name()));
})
.unwrap(),
);
gen_fn_tokens.push(quote! {
#[allow(non_camel_case_types)]
@@ -79,7 +95,8 @@ pub(crate) fn generate_body(
m
}
}
}).unwrap();
})
.unwrap();
let (_, generate_call_content) = generate_fncall.content.take().unwrap();