rhai/codegen/src/function.rs

769 lines
28 KiB
Rust
Raw Normal View History

2020-08-01 18:52:26 +02:00
#![allow(unused)]
2020-08-09 21:19:39 +02:00
2020-08-14 06:04:24 +02:00
#[cfg(no_std)]
use core::mem;
#[cfg(not(no_std))]
use std::mem;
2020-08-14 06:04:24 +02:00
#[cfg(no_std)]
use alloc::format;
#[cfg(not(no_std))]
use std::format;
use std::borrow::Cow;
2020-08-01 18:52:26 +02:00
use quote::{quote, quote_spanned};
2020-08-09 21:19:39 +02:00
use syn::{parse::Parse, parse::ParseStream, parse::Parser, spanned::Spanned};
2020-08-01 18:52:26 +02:00
use crate::attrs::{ExportInfo, ExportScope, ExportedParams};
2020-09-22 16:19:21 +02:00
use crate::rhai_module::flatten_type_groups;
2020-08-24 00:53:30 +02:00
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Index {
Get,
Set,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Property {
Get(syn::Ident),
Set(syn::Ident),
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum FnSpecialAccess {
None,
Index(Index),
Property(Property),
}
impl Default for FnSpecialAccess {
fn default() -> FnSpecialAccess {
FnSpecialAccess::None
}
}
impl FnSpecialAccess {
pub fn get_fn_name(&self) -> Option<(String, String, proc_macro2::Span)> {
match self {
FnSpecialAccess::None => None,
FnSpecialAccess::Property(Property::Get(ref g)) => {
Some((format!("get${}", g.to_string()), g.to_string(), g.span()))
}
FnSpecialAccess::Property(Property::Set(ref s)) => {
Some((format!("set${}", s.to_string()), s.to_string(), s.span()))
}
FnSpecialAccess::Index(Index::Get) => Some((
FN_IDX_GET.to_string(),
"index_get".to_string(),
proc_macro2::Span::call_site(),
)),
FnSpecialAccess::Index(Index::Set) => Some((
FN_IDX_SET.to_string(),
"index_set".to_string(),
proc_macro2::Span::call_site(),
)),
}
}
}
2020-08-08 04:19:17 +02:00
#[derive(Debug, Default)]
pub(crate) struct ExportedFnParams {
2020-09-04 05:57:40 +02:00
pub name: Option<Vec<String>>,
2020-08-09 21:19:39 +02:00
pub return_raw: bool,
pub skip: bool,
pub span: Option<proc_macro2::Span>,
pub special: FnSpecialAccess,
}
2020-08-17 17:35:04 +02:00
pub const FN_IDX_GET: &str = "index$get$";
pub const FN_IDX_SET: &str = "index$set$";
2020-08-08 04:19:17 +02:00
impl Parse for ExportedFnParams {
fn parse(args: ParseStream) -> syn::Result<Self> {
if args.is_empty() {
return Ok(ExportedFnParams::default());
}
2020-08-24 00:53:30 +02:00
let info = crate::attrs::parse_attr_items(args)?;
Self::from_info(info)
}
}
2020-08-08 04:19:17 +02:00
2020-08-24 00:53:30 +02:00
impl ExportedParams for ExportedFnParams {
fn parse_stream(args: ParseStream) -> syn::Result<Self> {
Self::parse(args)
}
fn no_attrs() -> Self {
Default::default()
}
2020-09-04 05:57:40 +02:00
fn from_info(info: crate::attrs::ExportInfo) -> syn::Result<Self> {
let ExportInfo {
item_span: span,
items: attrs,
} = info;
let mut name = Vec::new();
2020-08-09 21:19:39 +02:00
let mut return_raw = false;
let mut skip = false;
let mut special = FnSpecialAccess::None;
2020-08-24 00:53:30 +02:00
for attr in attrs {
let crate::attrs::AttrItem {
key,
value,
span: item_span,
} = attr;
2020-08-24 00:53:30 +02:00
match (key.to_string().as_ref(), value) {
2020-09-09 23:24:37 +02:00
("get", None) | ("set", None) | ("name", None) => {
return Err(syn::Error::new(key.span(), "requires value"))
}
2020-09-09 23:24:37 +02:00
("name", Some(s)) if &s.value() == FN_IDX_GET => {
return Err(syn::Error::new(
item_span,
"use attribute 'index_get' instead",
))
}
2020-09-09 23:24:37 +02:00
("name", Some(s)) if &s.value() == FN_IDX_SET => {
return Err(syn::Error::new(
item_span,
"use attribute 'index_set' instead",
))
}
2020-09-09 23:24:37 +02:00
("name", Some(s)) if s.value().starts_with("get$") => {
return Err(syn::Error::new(
item_span,
format!(
"use attribute 'getter = \"{}\"' instead",
&s.value()["get$".len()..]
),
))
}
2020-09-09 23:24:37 +02:00
("name", Some(s)) if s.value().starts_with("set$") => {
return Err(syn::Error::new(
item_span,
format!(
"use attribute 'setter = \"{}\"' instead",
&s.value()["set$".len()..]
),
))
}
2020-09-09 23:25:09 +02:00
("name", Some(s)) if s.value().contains('$') => {
return Err(syn::Error::new(
s.span(),
"Rhai function names may not contain dollar sign",
))
}
2020-09-09 23:24:37 +02:00
("name", Some(s)) if s.value().contains('.') => {
return Err(syn::Error::new(
s.span(),
"Rhai function names may not contain dot",
))
}
("name", Some(s)) => name.push(s.value()),
("set", Some(s)) => {
special = match special {
FnSpecialAccess::None => FnSpecialAccess::Property(Property::Set(
syn::Ident::new(&s.value(), s.span()),
)),
_ => return Err(syn::Error::new(item_span.span(), "conflicting setter")),
}
}
("get", Some(s)) => {
special = match special {
FnSpecialAccess::None => FnSpecialAccess::Property(Property::Get(
syn::Ident::new(&s.value(), s.span()),
)),
_ => return Err(syn::Error::new(item_span.span(), "conflicting getter")),
}
}
("index_get", None) => {
special = match special {
FnSpecialAccess::None => FnSpecialAccess::Index(Index::Get),
_ => {
return Err(syn::Error::new(item_span.span(), "conflicting index_get"))
}
}
}
("index_set", None) => {
special = match special {
FnSpecialAccess::None => FnSpecialAccess::Index(Index::Set),
_ => {
return Err(syn::Error::new(item_span.span(), "conflicting index_set"))
}
}
}
2020-08-09 21:19:39 +02:00
("return_raw", None) => return_raw = true,
("index_get", Some(s)) | ("index_set", Some(s)) | ("return_raw", Some(s)) => {
2020-08-09 21:19:39 +02:00
return Err(syn::Error::new(s.span(), "extraneous value"))
}
("skip", None) => skip = true,
2020-08-16 12:24:42 +02:00
("skip", Some(s)) => return Err(syn::Error::new(s.span(), "extraneous value")),
2020-08-09 21:19:39 +02:00
(attr, _) => {
return Err(syn::Error::new(
2020-08-24 00:53:30 +02:00
key.span(),
2020-08-09 21:19:39 +02:00
format!("unknown attribute '{}'", attr),
))
}
}
}
2020-08-08 04:19:17 +02:00
2020-08-16 12:24:42 +02:00
Ok(ExportedFnParams {
2020-09-04 05:57:40 +02:00
name: if name.is_empty() { None } else { Some(name) },
2020-08-16 12:24:42 +02:00
return_raw,
skip,
special,
span: Some(span),
2020-08-16 12:24:42 +02:00
..Default::default()
})
2020-08-08 04:19:17 +02:00
}
}
2020-08-01 18:52:26 +02:00
#[derive(Debug)]
pub(crate) struct ExportedFn {
entire_span: proc_macro2::Span,
signature: syn::Signature,
is_public: bool,
mut_receiver: bool,
params: ExportedFnParams,
2020-08-01 18:52:26 +02:00
}
impl Parse for ExportedFn {
fn parse(input: ParseStream) -> syn::Result<Self> {
let fn_all: syn::ItemFn = input.parse()?;
let entire_span = fn_all.span();
let str_type_path = syn::parse2::<syn::Path>(quote! { str }).unwrap();
// #[cfg] attributes are not allowed on functions due to what is generated for them
2020-08-24 00:53:30 +02:00
crate::attrs::deny_cfg_attr(&fn_all.attrs)?;
2020-08-01 18:52:26 +02:00
// Determine if the function is public.
2020-08-24 00:53:30 +02:00
let is_public = matches!(fn_all.vis, syn::Visibility::Public(_));
2020-08-01 18:52:26 +02:00
// Determine whether function generates a special calling convention for a mutable
// reciever.
let mut_receiver = {
if let Some(first_arg) = fn_all.sig.inputs.first() {
match first_arg {
2020-08-02 09:39:08 +02:00
syn::FnArg::Receiver(syn::Receiver {
reference: Some(_), ..
}) => true,
2020-09-22 16:19:21 +02:00
syn::FnArg::Typed(syn::PatType { ref ty, .. }) => {
match flatten_type_groups(ty.as_ref()) {
&syn::Type::Reference(syn::TypeReference {
mutability: Some(_),
..
}) => true,
&syn::Type::Reference(syn::TypeReference {
mutability: None,
ref elem,
..
}) => match flatten_type_groups(elem.as_ref()) {
&syn::Type::Path(ref p) if p.path == str_type_path => false,
_ => {
return Err(syn::Error::new(
ty.span(),
"references from Rhai in this position \
2020-08-02 09:39:08 +02:00
must be mutable",
2020-09-22 16:19:21 +02:00
))
}
},
_ => false,
}
}
2020-08-01 18:52:26 +02:00
_ => false,
}
} else {
false
}
};
// All arguments after the first must be moved except for &str.
for arg in fn_all.sig.inputs.iter().skip(1) {
let ty = match arg {
syn::FnArg::Typed(syn::PatType { ref ty, .. }) => ty,
_ => panic!("internal error: receiver argument outside of first position!?"),
};
2020-09-22 16:19:21 +02:00
let is_ok = match flatten_type_groups(ty.as_ref()) {
2020-08-02 09:39:08 +02:00
&syn::Type::Reference(syn::TypeReference {
mutability: Some(_),
..
}) => false,
&syn::Type::Reference(syn::TypeReference {
mutability: None,
ref elem,
..
2020-09-22 16:19:21 +02:00
}) => {
matches!(flatten_type_groups(elem.as_ref()), &syn::Type::Path(ref p) if p.path == str_type_path)
}
2020-08-01 18:52:26 +02:00
&syn::Type::Verbatim(_) => false,
_ => true,
};
if !is_ok {
2020-08-02 09:39:08 +02:00
return Err(syn::Error::new(
ty.span(),
"this type in this position passes from \
Rhai by value",
));
2020-08-01 18:52:26 +02:00
}
}
// No returning references or pointers.
if let syn::ReturnType::Type(_, ref rtype) = fn_all.sig.output {
match rtype.as_ref() {
2020-08-02 09:39:08 +02:00
&syn::Type::Ptr(_) => {
return Err(syn::Error::new(
fn_all.sig.output.span(),
"cannot return a pointer to Rhai",
))
}
&syn::Type::Reference(_) => {
return Err(syn::Error::new(
fn_all.sig.output.span(),
"cannot return a reference to Rhai",
))
}
_ => {}
2020-08-01 18:52:26 +02:00
}
}
Ok(ExportedFn {
entire_span,
signature: fn_all.sig,
is_public,
mut_receiver,
2020-08-08 04:19:17 +02:00
params: ExportedFnParams::default(),
2020-08-01 18:52:26 +02:00
})
}
}
impl ExportedFn {
pub(crate) fn params(&self) -> &ExportedFnParams {
&self.params
}
pub(crate) fn update_scope(&mut self, parent_scope: &ExportScope) {
let keep = match (self.params.skip, parent_scope) {
(true, _) => false,
(_, ExportScope::PubOnly) => self.is_public,
(_, ExportScope::Prefix(s)) => self.name().to_string().starts_with(s),
(_, ExportScope::All) => true,
};
self.params.skip = !keep;
}
pub(crate) fn skipped(&self) -> bool {
self.params.skip
}
pub(crate) fn signature(&self) -> &syn::Signature {
&self.signature
}
2020-08-01 18:52:26 +02:00
pub(crate) fn mutable_receiver(&self) -> bool {
self.mut_receiver
}
pub(crate) fn is_public(&self) -> bool {
self.is_public
}
pub(crate) fn span(&self) -> &proc_macro2::Span {
&self.entire_span
}
pub(crate) fn name(&self) -> &syn::Ident {
&self.signature.ident
}
pub(crate) fn exported_names(&self) -> Vec<syn::LitStr> {
let mut literals = self
.params
.name
.as_ref()
.map(|v| {
v.iter()
.map(|s| syn::LitStr::new(s, proc_macro2::Span::call_site()))
.collect()
})
.unwrap_or_else(|| Vec::new());
if let Some((s, _, span)) = self.params.special.get_fn_name() {
literals.push(syn::LitStr::new(&s, span));
}
if literals.is_empty() {
literals.push(syn::LitStr::new(
&self.signature.ident.to_string(),
self.signature.ident.span(),
));
}
literals
}
pub(crate) fn exported_name<'n>(&'n self) -> Cow<'n, str> {
if let Some(ref name) = self.params.name {
2020-09-04 05:57:40 +02:00
Cow::Borrowed(name.last().unwrap().as_str())
} else {
Cow::Owned(self.signature.ident.to_string())
}
}
2020-08-02 09:39:08 +02:00
pub(crate) fn arg_list(&self) -> impl Iterator<Item = &syn::FnArg> {
2020-08-01 18:52:26 +02:00
self.signature.inputs.iter()
}
pub(crate) fn arg_count(&self) -> usize {
self.signature.inputs.len()
}
pub(crate) fn return_type(&self) -> Option<&syn::Type> {
if let syn::ReturnType::Type(_, ref rtype) = self.signature.output {
Some(rtype)
} else {
None
}
}
2020-08-24 00:53:30 +02:00
pub fn set_params(&mut self, mut params: ExportedFnParams) -> syn::Result<()> {
// Several issues are checked here to avoid issues with diagnostics caused by raising them
// later.
//
// 1. Do not allow non-returning raw functions.
//
2020-08-24 00:53:30 +02:00
if params.return_raw
&& mem::discriminant(&self.signature.output)
== mem::discriminant(&syn::ReturnType::Default)
{
return Err(syn::Error::new(
self.signature.span(),
"return_raw functions must return Result<T>",
));
}
match params.special {
// 2a. Property getters must take only the subject as an argument.
FnSpecialAccess::Property(Property::Get(_)) if self.arg_count() != 1 => {
return Err(syn::Error::new(
self.signature.span(),
"property getter requires exactly 1 argument",
))
}
// 2b. Property getters must return a value.
FnSpecialAccess::Property(Property::Get(_)) if self.return_type().is_none() => {
return Err(syn::Error::new(
self.signature.span(),
"property getter must return a value",
))
}
// 3a. Property setters must take the subject and a new value as arguments.
FnSpecialAccess::Property(Property::Set(_)) if self.arg_count() != 2 => {
return Err(syn::Error::new(
self.signature.span(),
"property setter requires exactly 2 arguments",
))
}
// 3b. Property setters must return nothing.
FnSpecialAccess::Property(Property::Set(_)) if self.return_type().is_some() => {
return Err(syn::Error::new(
self.signature.span(),
"property setter must return no value",
))
}
// 4a. Index getters must take the subject and the accessed "index" as arguments.
FnSpecialAccess::Index(Index::Get) if self.arg_count() != 2 => {
return Err(syn::Error::new(
self.signature.span(),
"index getter requires exactly 2 arguments",
))
}
// 4b. Index getters must return a value.
FnSpecialAccess::Index(Index::Get) if self.return_type().is_none() => {
return Err(syn::Error::new(
self.signature.span(),
"index getter must return a value",
))
}
// 5a. Index setters must take the subject, "index", and new value as arguments.
FnSpecialAccess::Index(Index::Set) if self.arg_count() != 3 => {
return Err(syn::Error::new(
self.signature.span(),
"index setter requires exactly 3 arguments",
))
}
// 5b. Index setters must return nothing.
FnSpecialAccess::Index(Index::Set) if self.return_type().is_some() => {
return Err(syn::Error::new(
self.signature.span(),
"index setter must return no value",
))
}
_ => {}
}
2020-08-08 04:19:17 +02:00
self.params = params;
Ok(())
2020-08-08 04:19:17 +02:00
}
2020-08-01 18:52:26 +02:00
pub fn generate(self) -> proc_macro2::TokenStream {
let name: syn::Ident =
2020-08-16 12:24:42 +02:00
syn::Ident::new(&format!("rhai_fn_{}", self.name()), self.name().span());
2020-08-01 18:52:26 +02:00
let impl_block = self.generate_impl("Token");
2020-08-03 02:27:19 +02:00
let callable_block = self.generate_callable("Token");
let input_types_block = self.generate_input_types("Token");
let dyn_result_fn_block = self.generate_dynamic_fn();
2020-08-01 18:52:26 +02:00
quote! {
#[allow(unused)]
pub mod #name {
use super::*;
2020-08-03 02:27:19 +02:00
struct Token();
2020-08-01 18:52:26 +02:00
#impl_block
2020-08-03 02:27:19 +02:00
#callable_block
#input_types_block
#dyn_result_fn_block
}
}
}
pub fn generate_dynamic_fn(&self) -> proc_macro2::TokenStream {
2020-08-16 12:24:42 +02:00
let name = self.name().clone();
let mut dynamic_signature = self.signature.clone();
dynamic_signature.ident =
syn::Ident::new("dynamic_result_fn", proc_macro2::Span::call_site());
dynamic_signature.output = syn::parse2::<syn::ReturnType>(quote! {
-> Result<Dynamic, EvalBox>
})
.unwrap();
let arguments: Vec<syn::Ident> = dynamic_signature
.inputs
.iter()
.filter_map(|fnarg| {
if let syn::FnArg::Typed(syn::PatType { ref pat, .. }) = fnarg {
if let syn::Pat::Ident(ref ident) = pat.as_ref() {
Some(ident.ident.clone())
} else {
None
}
} else {
None
}
})
.collect();
2020-09-04 05:57:40 +02:00
let return_span = self
.return_type()
.map(|r| r.span())
.unwrap_or_else(|| proc_macro2::Span::call_site());
if !self.params.return_raw {
quote_spanned! { return_span=>
type EvalBox = Box<EvalAltResult>;
pub #dynamic_signature {
Ok(Dynamic::from(super::#name(#(#arguments),*)))
}
}
} else {
quote_spanned! { return_span=>
type EvalBox = Box<EvalAltResult>;
pub #dynamic_signature {
super::#name(#(#arguments),*)
}
2020-08-03 02:27:19 +02:00
}
}
}
pub fn generate_callable(&self, on_type_name: &str) -> proc_macro2::TokenStream {
let token_name: syn::Ident = syn::Ident::new(on_type_name, self.name().span());
let callable_fn_name: syn::Ident = syn::Ident::new(
format!("{}_callable", on_type_name.to_lowercase()).as_str(),
self.name().span(),
);
2020-08-03 02:27:19 +02:00
quote! {
pub fn #callable_fn_name() -> CallableFunction {
CallableFunction::from_plugin(#token_name())
}
}
}
pub fn generate_input_types(&self, on_type_name: &str) -> proc_macro2::TokenStream {
let token_name: syn::Ident = syn::Ident::new(on_type_name, self.name().span());
let input_types_fn_name: syn::Ident = syn::Ident::new(
format!("{}_input_types", on_type_name.to_lowercase()).as_str(),
self.name().span(),
);
2020-08-03 02:27:19 +02:00
quote! {
2020-08-14 06:04:24 +02:00
pub fn #input_types_fn_name() -> Box<[TypeId]> {
2020-08-03 02:27:19 +02:00
#token_name().input_types()
2020-08-01 18:52:26 +02:00
}
}
}
pub fn generate_impl(&self, on_type_name: &str) -> proc_macro2::TokenStream {
2020-08-16 12:24:42 +02:00
let sig_name = self.name().clone();
2020-09-04 05:57:40 +02:00
let name = self.params.name.as_ref().map_or_else(
|| self.name().to_string(),
|names| names.last().unwrap().clone(),
);
2020-08-08 16:31:15 +02:00
2020-08-01 18:52:26 +02:00
let arg_count = self.arg_count();
let is_method_call = self.mutable_receiver();
let mut unpack_stmts: Vec<syn::Stmt> = Vec::new();
let mut unpack_exprs: Vec<syn::Expr> = Vec::new();
let mut input_type_exprs: Vec<syn::Expr> = Vec::new();
let skip_first_arg;
// Handle the first argument separately if the function has a "method like" receiver
if is_method_call {
skip_first_arg = true;
let first_arg = self.arg_list().next().unwrap();
let var = syn::Ident::new("arg0", proc_macro2::Span::call_site());
match first_arg {
2020-08-02 09:39:08 +02:00
syn::FnArg::Typed(pattern) => {
2020-09-22 16:19:21 +02:00
let arg_type: &syn::Type = match flatten_type_groups(pattern.ty.as_ref()) {
&syn::Type::Reference(syn::TypeReference { ref elem, .. }) => elem.as_ref(),
p => p,
2020-08-01 18:52:26 +02:00
};
let downcast_span = quote_spanned!(
2020-08-07 01:36:15 +02:00
arg_type.span()=> &mut args[0usize].write_lock::<#arg_type>().unwrap());
2020-08-02 09:39:08 +02:00
unpack_stmts.push(
syn::parse2::<syn::Stmt>(quote! {
let #var: &mut _ = #downcast_span;
})
.unwrap(),
);
input_type_exprs.push(
syn::parse2::<syn::Expr>(quote_spanned!(
2020-08-14 06:04:24 +02:00
arg_type.span()=> TypeId::of::<#arg_type>()
2020-08-02 09:39:08 +02:00
))
.unwrap(),
);
}
2020-08-01 18:52:26 +02:00
syn::FnArg::Receiver(_) => todo!("true self parameters not implemented yet"),
}
unpack_exprs.push(syn::parse2::<syn::Expr>(quote! { #var }).unwrap());
} else {
skip_first_arg = false;
}
// Handle the rest of the arguments, which all are passed by value.
//
// The only exception is strings, which need to be downcast to ImmutableString to enable a
2020-09-19 12:18:40 +02:00
// zero-copy conversion to &str by reference, or a cloned String.
2020-08-01 18:52:26 +02:00
let str_type_path = syn::parse2::<syn::Path>(quote! { str }).unwrap();
2020-09-19 12:18:40 +02:00
let string_type_path = syn::parse2::<syn::Path>(quote! { String }).unwrap();
2020-08-01 18:52:26 +02:00
for (i, arg) in self.arg_list().enumerate().skip(skip_first_arg as usize) {
let var = syn::Ident::new(&format!("arg{}", i), proc_macro2::Span::call_site());
2020-09-19 12:18:40 +02:00
let is_string;
let is_ref;
2020-08-01 18:52:26 +02:00
match arg {
2020-08-02 09:39:08 +02:00
syn::FnArg::Typed(pattern) => {
2020-08-01 18:52:26 +02:00
let arg_type: &syn::Type = pattern.ty.as_ref();
2020-09-22 16:19:21 +02:00
let downcast_span = match flatten_type_groups(pattern.ty.as_ref()) {
2020-08-02 09:39:08 +02:00
&syn::Type::Reference(syn::TypeReference {
mutability: None,
ref elem,
..
2020-09-22 16:19:21 +02:00
}) => match flatten_type_groups(elem.as_ref()) {
2020-08-02 09:39:08 +02:00
&syn::Type::Path(ref p) if p.path == str_type_path => {
2020-09-19 12:18:40 +02:00
is_string = true;
is_ref = true;
2020-08-02 09:39:08 +02:00
quote_spanned!(arg_type.span()=>
mem::take(args[#i]).take_immutable_string().unwrap())
2020-08-01 18:52:26 +02:00
}
2020-08-02 09:39:08 +02:00
_ => panic!("internal error: why wasn't this found earlier!?"),
2020-08-01 18:52:26 +02:00
},
2020-09-19 12:18:40 +02:00
&syn::Type::Path(ref p) if p.path == string_type_path => {
is_string = true;
is_ref = false;
quote_spanned!(arg_type.span()=>
2020-09-20 08:29:10 +02:00
mem::take(args[#i]).take_string().unwrap())
2020-09-19 12:18:40 +02:00
}
2020-08-01 18:52:26 +02:00
_ => {
2020-09-19 12:18:40 +02:00
is_string = false;
is_ref = false;
2020-08-01 18:52:26 +02:00
quote_spanned!(arg_type.span()=>
2020-09-23 04:38:02 +02:00
mem::take(args[#i]).cast::<#arg_type>())
2020-08-02 09:39:08 +02:00
}
2020-08-01 18:52:26 +02:00
};
2020-08-02 09:39:08 +02:00
unpack_stmts.push(
syn::parse2::<syn::Stmt>(quote! {
let #var = #downcast_span;
})
.unwrap(),
);
2020-09-19 12:18:40 +02:00
if !is_string {
2020-08-02 09:39:08 +02:00
input_type_exprs.push(
syn::parse2::<syn::Expr>(quote_spanned!(
2020-08-14 06:04:24 +02:00
arg_type.span()=> TypeId::of::<#arg_type>()
2020-08-02 09:39:08 +02:00
))
.unwrap(),
);
2020-08-01 18:52:26 +02:00
} else {
2020-08-02 09:39:08 +02:00
input_type_exprs.push(
syn::parse2::<syn::Expr>(quote_spanned!(
2020-08-14 06:04:24 +02:00
arg_type.span()=> TypeId::of::<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
}
2020-08-01 18:52:26 +02:00
syn::FnArg::Receiver(_) => panic!("internal error: how did this happen!?"),
}
2020-09-19 12:18:40 +02:00
if !is_ref {
2020-08-01 18:52:26 +02:00
unpack_exprs.push(syn::parse2::<syn::Expr>(quote! { #var }).unwrap());
} else {
unpack_exprs.push(syn::parse2::<syn::Expr>(quote! { &#var }).unwrap());
}
}
// In method calls, the first argument will need to be mutably borrowed. Because Rust marks
// that as needing to borrow the entire array, all of the previous argument unpacking via
// clone needs to happen first.
if is_method_call {
let arg0 = unpack_stmts.remove(0);
unpack_stmts.push(arg0);
}
2020-08-09 21:19:39 +02:00
// Handle "raw returns", aka cases where the result is a dynamic or an error.
//
// This allows skipping the Dynamic::from wrap.
2020-09-04 05:57:40 +02:00
let return_span = self
.return_type()
.map(|r| r.span())
.unwrap_or_else(|| proc_macro2::Span::call_site());
2020-08-09 21:19:39 +02:00
let return_expr = if !self.params.return_raw {
quote_spanned! { return_span=>
2020-08-16 12:24:42 +02:00
Ok(Dynamic::from(#sig_name(#(#unpack_exprs),*)))
2020-08-09 21:19:39 +02:00
}
} else {
quote_spanned! { return_span=>
2020-08-16 12:24:42 +02:00
#sig_name(#(#unpack_exprs),*)
2020-08-09 21:19:39 +02:00
}
};
2020-08-01 18:52:26 +02:00
let type_name = syn::Ident::new(on_type_name, proc_macro2::Span::call_site());
quote! {
2020-08-03 02:27:19 +02:00
impl PluginFunction for #type_name {
2020-08-01 18:52:26 +02:00
fn call(&self,
args: &mut [&mut Dynamic]
2020-08-03 02:27:19 +02:00
) -> Result<Dynamic, Box<EvalAltResult>> {
debug_assert_eq!(args.len(), #arg_count,
"wrong arg count: {} != {}",
args.len(), #arg_count);
2020-08-01 18:52:26 +02:00
#(#unpack_stmts)*
2020-08-09 21:19:39 +02:00
#return_expr
2020-08-01 18:52:26 +02:00
}
fn is_method_call(&self) -> bool { #is_method_call }
fn is_varadic(&self) -> bool { false }
2020-08-03 02:27:19 +02:00
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(#type_name()) }
2020-08-14 06:04:24 +02:00
fn input_types(&self) -> Box<[TypeId]> {
new_vec![#(#input_type_exprs),*].into_boxed_slice()
2020-08-01 18:52:26 +02:00
}
}
}
}
}