Allow non-Dynamic in return_raw.

This commit is contained in:
Stephen Chung
2021-03-22 11:18:09 +08:00
parent b3bcd7bf79
commit a82f0fc738
23 changed files with 214 additions and 282 deletions

View File

@@ -279,7 +279,6 @@ pub struct ExportedFn {
signature: syn::Signature,
visibility: syn::Visibility,
pass_context: bool,
return_dynamic: bool,
mut_receiver: bool,
params: ExportedFnParams,
}
@@ -290,10 +289,6 @@ impl Parse for ExportedFn {
let entire_span = fn_all.span();
let str_type_path = syn::parse2::<syn::Path>(quote! { str }).unwrap();
let dynamic_type_path1 = syn::parse2::<syn::Path>(quote! { Dynamic }).unwrap();
let dynamic_type_path2 = syn::parse2::<syn::Path>(quote! { rhai::Dynamic }).unwrap();
let mut return_dynamic = false;
let context_type_path1 = syn::parse2::<syn::Path>(quote! { NativeCallContext }).unwrap();
let context_type_path2 =
syn::parse2::<syn::Path>(quote! { rhai::NativeCallContext }).unwrap();
@@ -400,11 +395,6 @@ impl Parse for ExportedFn {
"Rhai functions cannot return references",
))
}
syn::Type::Path(p)
if p.path == dynamic_type_path1 || p.path == dynamic_type_path2 =>
{
return_dynamic = true
}
_ => {}
}
}
@@ -413,7 +403,6 @@ impl Parse for ExportedFn {
signature: fn_all.sig,
visibility,
pass_context,
return_dynamic,
mut_receiver,
params: Default::default(),
})
@@ -520,7 +509,7 @@ impl ExportedFn {
if params.return_raw.is_some() && self.return_type().is_none() {
return Err(syn::Error::new(
params.return_raw.unwrap(),
"functions marked with 'return_raw' must return Result<Dynamic, Box<EvalAltResult>>",
"functions marked with 'return_raw' must return Result<T, Box<EvalAltResult>>",
));
}
@@ -656,13 +645,7 @@ impl ExportedFn {
if self.params.return_raw.is_some() {
quote_spanned! { return_span =>
pub #dynamic_signature {
#name(#(#arguments),*)
}
}
} else if self.return_dynamic {
quote_spanned! { return_span =>
pub #dynamic_signature {
Ok(#name(#(#arguments),*))
#name(#(#arguments),*).map(Dynamic::from)
}
}
} else {
@@ -890,18 +873,12 @@ impl ExportedFn {
.map(|r| r.span())
.unwrap_or_else(|| proc_macro2::Span::call_site());
let return_expr = if !self.params.return_raw.is_some() {
if self.return_dynamic {
quote_spanned! { return_span =>
Ok(#sig_name(#(#unpack_exprs),*))
}
} else {
quote_spanned! { return_span =>
Ok(Dynamic::from(#sig_name(#(#unpack_exprs),*)))
}
quote_spanned! { return_span =>
Ok(Dynamic::from(#sig_name(#(#unpack_exprs),*)))
}
} else {
quote_spanned! { return_span =>
#sig_name(#(#unpack_exprs),*)
#sig_name(#(#unpack_exprs),*).map(Dynamic::from)
}
};

View File

@@ -447,7 +447,7 @@ mod generate_tests {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 0usize,
"wrong arg count: {} != {}", args.len(), 0usize);
Ok(return_dynamic())
Ok(Dynamic::from(return_dynamic()))
}
fn is_method_call(&self) -> bool { false }
@@ -477,7 +477,7 @@ mod generate_tests {
}
#[allow(unused)]
pub fn dynamic_result_fn() -> Result<Dynamic, Box<EvalAltResult> > {
Ok(return_dynamic())
Ok(Dynamic::from(return_dynamic()))
}
}
};