General code cleanup.
This commit is contained in:
parent
c8e7c970d1
commit
0265af415d
@ -1,3 +1,4 @@
|
|||||||
|
use proc_macro2::{Ident, Span, TokenStream};
|
||||||
use syn::{
|
use syn::{
|
||||||
parse::{ParseStream, Parser},
|
parse::{ParseStream, Parser},
|
||||||
spanned::Spanned,
|
spanned::Spanned,
|
||||||
@ -24,14 +25,14 @@ pub trait ExportedParams: Sized {
|
|||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct AttrItem {
|
pub struct AttrItem {
|
||||||
pub key: proc_macro2::Ident,
|
pub key: Ident,
|
||||||
pub value: Option<syn::LitStr>,
|
pub value: Option<syn::LitStr>,
|
||||||
pub span: proc_macro2::Span,
|
pub span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ExportInfo {
|
pub struct ExportInfo {
|
||||||
pub item_span: proc_macro2::Span,
|
pub item_span: Span,
|
||||||
pub items: Vec<AttrItem>,
|
pub items: Vec<AttrItem>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,8 +43,7 @@ pub fn parse_attr_items(args: ParseStream) -> syn::Result<ExportInfo> {
|
|||||||
items: Vec::new(),
|
items: Vec::new(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
let arg_list = args
|
let arg_list = args.call(syn::punctuated::Punctuated::parse_separated_nonempty)?;
|
||||||
.call(syn::punctuated::Punctuated::<syn::Expr, syn::Token![,]>::parse_separated_nonempty)?;
|
|
||||||
|
|
||||||
parse_punctuated_items(arg_list)
|
parse_punctuated_items(arg_list)
|
||||||
}
|
}
|
||||||
@ -53,7 +53,8 @@ pub fn parse_punctuated_items(
|
|||||||
) -> syn::Result<ExportInfo> {
|
) -> syn::Result<ExportInfo> {
|
||||||
let list_span = arg_list.span();
|
let list_span = arg_list.span();
|
||||||
|
|
||||||
let mut attrs: Vec<AttrItem> = Vec::new();
|
let mut attrs = Vec::new();
|
||||||
|
|
||||||
for arg in arg_list {
|
for arg in arg_list {
|
||||||
let arg_span = arg.span();
|
let arg_span = arg.span();
|
||||||
let (key, value) = match arg {
|
let (key, value) = match arg {
|
||||||
@ -62,7 +63,7 @@ pub fn parse_punctuated_items(
|
|||||||
ref right,
|
ref right,
|
||||||
..
|
..
|
||||||
}) => {
|
}) => {
|
||||||
let attr_name: syn::Ident = match left.as_ref() {
|
let attr_name = match left.as_ref() {
|
||||||
syn::Expr::Path(syn::ExprPath {
|
syn::Expr::Path(syn::ExprPath {
|
||||||
path: attr_path, ..
|
path: attr_path, ..
|
||||||
}) => attr_path.get_ident().cloned().ok_or_else(|| {
|
}) => attr_path.get_ident().cloned().ok_or_else(|| {
|
||||||
@ -79,13 +80,11 @@ pub fn parse_punctuated_items(
|
|||||||
};
|
};
|
||||||
(attr_name, Some(attr_value))
|
(attr_name, Some(attr_value))
|
||||||
}
|
}
|
||||||
syn::Expr::Path(syn::ExprPath {
|
syn::Expr::Path(syn::ExprPath { path, .. }) => path
|
||||||
path: attr_path, ..
|
|
||||||
}) => attr_path
|
|
||||||
.get_ident()
|
.get_ident()
|
||||||
.cloned()
|
.cloned()
|
||||||
.map(|a| (a, None))
|
.map(|a| (a, None))
|
||||||
.ok_or_else(|| syn::Error::new(attr_path.span(), "expecting attribute name"))?,
|
.ok_or_else(|| syn::Error::new(path.span(), "expecting attribute name"))?,
|
||||||
x => return Err(syn::Error::new(x.span(), "expecting identifier")),
|
x => return Err(syn::Error::new(x.span(), "expecting identifier")),
|
||||||
};
|
};
|
||||||
attrs.push(AttrItem {
|
attrs.push(AttrItem {
|
||||||
@ -102,18 +101,16 @@ pub fn parse_punctuated_items(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn outer_item_attributes<T: ExportedParams>(
|
pub fn outer_item_attributes<T: ExportedParams>(
|
||||||
args: proc_macro2::TokenStream,
|
args: TokenStream,
|
||||||
_attr_name: &str,
|
_attr_name: &str,
|
||||||
) -> syn::Result<T> {
|
) -> syn::Result<T> {
|
||||||
if args.is_empty() {
|
if args.is_empty() {
|
||||||
return Ok(T::no_attrs());
|
return Ok(T::no_attrs());
|
||||||
}
|
}
|
||||||
|
|
||||||
let parser = syn::punctuated::Punctuated::<syn::Expr, syn::Token![,]>::parse_separated_nonempty;
|
let arg_list = syn::punctuated::Punctuated::parse_separated_nonempty.parse2(args)?;
|
||||||
let arg_list = parser.parse2(args)?;
|
|
||||||
|
|
||||||
let export_info = parse_punctuated_items(arg_list)?;
|
T::from_info(parse_punctuated_items(arg_list)?)
|
||||||
T::from_info(export_info)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn inner_item_attributes<T: ExportedParams>(
|
pub fn inner_item_attributes<T: ExportedParams>(
|
||||||
|
@ -1,3 +1,10 @@
|
|||||||
|
use proc_macro2::{Span, TokenStream};
|
||||||
|
use quote::{quote, quote_spanned, ToTokens};
|
||||||
|
use syn::{
|
||||||
|
parse::{Parse, ParseStream},
|
||||||
|
spanned::Spanned,
|
||||||
|
};
|
||||||
|
|
||||||
#[cfg(no_std)]
|
#[cfg(no_std)]
|
||||||
use alloc::format;
|
use alloc::format;
|
||||||
#[cfg(not(no_std))]
|
#[cfg(not(no_std))]
|
||||||
@ -5,12 +12,6 @@ use std::format;
|
|||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use quote::{quote, quote_spanned, ToTokens};
|
|
||||||
use syn::{
|
|
||||||
parse::{Parse, ParseStream},
|
|
||||||
spanned::Spanned,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::attrs::{ExportInfo, ExportScope, ExportedParams};
|
use crate::attrs::{ExportInfo, ExportScope, ExportedParams};
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq, Copy, Hash)]
|
#[derive(Clone, Debug, Eq, PartialEq, Copy, Hash)]
|
||||||
@ -52,7 +53,7 @@ impl Default for FnSpecialAccess {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl FnSpecialAccess {
|
impl FnSpecialAccess {
|
||||||
pub fn get_fn_name(&self) -> Option<(String, String, proc_macro2::Span)> {
|
pub fn get_fn_name(&self) -> Option<(String, String, Span)> {
|
||||||
match self {
|
match self {
|
||||||
FnSpecialAccess::None => None,
|
FnSpecialAccess::None => None,
|
||||||
FnSpecialAccess::Property(Property::Get(ref g)) => {
|
FnSpecialAccess::Property(Property::Get(ref g)) => {
|
||||||
@ -64,12 +65,12 @@ impl FnSpecialAccess {
|
|||||||
FnSpecialAccess::Index(Index::Get) => Some((
|
FnSpecialAccess::Index(Index::Get) => Some((
|
||||||
FN_IDX_GET.to_string(),
|
FN_IDX_GET.to_string(),
|
||||||
"index_get".to_string(),
|
"index_get".to_string(),
|
||||||
proc_macro2::Span::call_site(),
|
Span::call_site(),
|
||||||
)),
|
)),
|
||||||
FnSpecialAccess::Index(Index::Set) => Some((
|
FnSpecialAccess::Index(Index::Set) => Some((
|
||||||
FN_IDX_SET.to_string(),
|
FN_IDX_SET.to_string(),
|
||||||
"index_set".to_string(),
|
"index_set".to_string(),
|
||||||
proc_macro2::Span::call_site(),
|
Span::call_site(),
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -98,12 +99,12 @@ pub fn print_type(ty: &syn::Type) -> String {
|
|||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct ExportedFnParams {
|
pub struct ExportedFnParams {
|
||||||
pub name: Vec<String>,
|
pub name: Vec<String>,
|
||||||
pub return_raw: Option<proc_macro2::Span>,
|
pub return_raw: Option<Span>,
|
||||||
pub pure: Option<proc_macro2::Span>,
|
pub pure: Option<Span>,
|
||||||
pub skip: bool,
|
pub skip: bool,
|
||||||
pub special: FnSpecialAccess,
|
pub special: FnSpecialAccess,
|
||||||
pub namespace: FnNamespaceAccess,
|
pub namespace: FnNamespaceAccess,
|
||||||
pub span: Option<proc_macro2::Span>,
|
pub span: Option<Span>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const FN_GET: &str = "get$";
|
pub const FN_GET: &str = "get$";
|
||||||
@ -274,7 +275,7 @@ impl ExportedParams for ExportedFnParams {
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ExportedFn {
|
pub struct ExportedFn {
|
||||||
entire_span: proc_macro2::Span,
|
entire_span: Span,
|
||||||
signature: syn::Signature,
|
signature: syn::Signature,
|
||||||
visibility: syn::Visibility,
|
visibility: syn::Visibility,
|
||||||
pass_context: bool,
|
pass_context: bool,
|
||||||
@ -369,7 +370,7 @@ impl Parse for ExportedFn {
|
|||||||
if !is_ok {
|
if !is_ok {
|
||||||
return Err(syn::Error::new(
|
return Err(syn::Error::new(
|
||||||
ty.span(),
|
ty.span(),
|
||||||
"this type in this position passes from Rhai by value",
|
"function parameters other than the first one cannot be passed by reference",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -443,7 +444,7 @@ impl ExportedFn {
|
|||||||
!matches!(self.visibility, syn::Visibility::Inherited)
|
!matches!(self.visibility, syn::Visibility::Inherited)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn span(&self) -> &proc_macro2::Span {
|
pub fn span(&self) -> &Span {
|
||||||
&self.entire_span
|
&self.entire_span
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -456,7 +457,7 @@ impl ExportedFn {
|
|||||||
.params
|
.params
|
||||||
.name
|
.name
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| syn::LitStr::new(s, proc_macro2::Span::call_site()))
|
.map(|s| syn::LitStr::new(s, Span::call_site()))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if let Some((s, _, span)) = self.params.special.get_fn_name() {
|
if let Some((s, _, span)) = self.params.special.get_fn_name() {
|
||||||
@ -586,7 +587,7 @@ impl ExportedFn {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate(self) -> proc_macro2::TokenStream {
|
pub fn generate(self) -> TokenStream {
|
||||||
let name: syn::Ident =
|
let name: syn::Ident =
|
||||||
syn::Ident::new(&format!("rhai_fn_{}", self.name()), self.name().span());
|
syn::Ident::new(&format!("rhai_fn_{}", self.name()), self.name().span());
|
||||||
let impl_block = self.generate_impl("Token");
|
let impl_block = self.generate_impl("Token");
|
||||||
@ -603,17 +604,16 @@ impl ExportedFn {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_dynamic_fn(&self) -> proc_macro2::TokenStream {
|
pub fn generate_dynamic_fn(&self) -> TokenStream {
|
||||||
let name = self.name().clone();
|
let name = self.name().clone();
|
||||||
|
|
||||||
let mut dynamic_signature = self.signature.clone();
|
let mut dynamic_signature = self.signature.clone();
|
||||||
dynamic_signature.ident =
|
dynamic_signature.ident = syn::Ident::new("dynamic_result_fn", Span::call_site());
|
||||||
syn::Ident::new("dynamic_result_fn", proc_macro2::Span::call_site());
|
|
||||||
dynamic_signature.output = syn::parse2::<syn::ReturnType>(quote! {
|
dynamic_signature.output = syn::parse2::<syn::ReturnType>(quote! {
|
||||||
-> RhaiResult
|
-> RhaiResult
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let arguments: Vec<syn::Ident> = dynamic_signature
|
let arguments: Vec<_> = dynamic_signature
|
||||||
.inputs
|
.inputs
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|fn_arg| match fn_arg {
|
.filter_map(|fn_arg| match fn_arg {
|
||||||
@ -628,7 +628,7 @@ impl ExportedFn {
|
|||||||
let return_span = self
|
let return_span = self
|
||||||
.return_type()
|
.return_type()
|
||||||
.map(|r| r.span())
|
.map(|r| r.span())
|
||||||
.unwrap_or_else(proc_macro2::Span::call_site);
|
.unwrap_or_else(Span::call_site);
|
||||||
if self.params.return_raw.is_some() {
|
if self.params.return_raw.is_some() {
|
||||||
quote_spanned! { return_span =>
|
quote_spanned! { return_span =>
|
||||||
pub #dynamic_signature {
|
pub #dynamic_signature {
|
||||||
@ -646,16 +646,16 @@ impl ExportedFn {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_impl(&self, on_type_name: &str) -> proc_macro2::TokenStream {
|
pub fn generate_impl(&self, on_type_name: &str) -> TokenStream {
|
||||||
let sig_name = self.name().clone();
|
let sig_name = self.name().clone();
|
||||||
let arg_count = self.arg_count();
|
let arg_count = self.arg_count();
|
||||||
let is_method_call = self.mutable_receiver();
|
let is_method_call = self.mutable_receiver();
|
||||||
|
|
||||||
let mut unpack_statements: Vec<syn::Stmt> = Vec::new();
|
let mut unpack_statements = Vec::new();
|
||||||
let mut unpack_exprs: Vec<syn::Expr> = Vec::new();
|
let mut unpack_exprs = Vec::new();
|
||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
let mut input_type_names: Vec<String> = Vec::new();
|
let mut input_type_names = Vec::new();
|
||||||
let mut input_type_exprs: Vec<syn::Expr> = Vec::new();
|
let mut input_type_exprs = Vec::new();
|
||||||
|
|
||||||
let return_type = self
|
let return_type = self
|
||||||
.return_type()
|
.return_type()
|
||||||
@ -672,7 +672,7 @@ impl ExportedFn {
|
|||||||
if is_method_call {
|
if is_method_call {
|
||||||
skip_first_arg = true;
|
skip_first_arg = true;
|
||||||
let first_arg = self.arg_list().next().unwrap();
|
let first_arg = self.arg_list().next().unwrap();
|
||||||
let var = syn::Ident::new("arg0", proc_macro2::Span::call_site());
|
let var = syn::Ident::new("arg0", Span::call_site());
|
||||||
match first_arg {
|
match first_arg {
|
||||||
syn::FnArg::Typed(syn::PatType { pat, ty, .. }) => {
|
syn::FnArg::Typed(syn::PatType { pat, ty, .. }) => {
|
||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
@ -725,7 +725,7 @@ impl ExportedFn {
|
|||||||
let str_type_path = syn::parse2::<syn::Path>(quote! { str }).unwrap();
|
let str_type_path = syn::parse2::<syn::Path>(quote! { str }).unwrap();
|
||||||
let string_type_path = syn::parse2::<syn::Path>(quote! { String }).unwrap();
|
let string_type_path = syn::parse2::<syn::Path>(quote! { String }).unwrap();
|
||||||
for (i, arg) in self.arg_list().enumerate().skip(skip_first_arg as usize) {
|
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());
|
let var = syn::Ident::new(&format!("arg{}", i), Span::call_site());
|
||||||
let is_string;
|
let is_string;
|
||||||
let is_ref;
|
let is_ref;
|
||||||
match arg {
|
match arg {
|
||||||
@ -811,7 +811,7 @@ impl ExportedFn {
|
|||||||
let return_span = self
|
let return_span = self
|
||||||
.return_type()
|
.return_type()
|
||||||
.map(|r| r.span())
|
.map(|r| r.span())
|
||||||
.unwrap_or_else(proc_macro2::Span::call_site);
|
.unwrap_or_else(Span::call_site);
|
||||||
let return_expr = if self.params.return_raw.is_none() {
|
let return_expr = if self.params.return_raw.is_none() {
|
||||||
quote_spanned! { return_span =>
|
quote_spanned! { return_span =>
|
||||||
Ok(Dynamic::from(#sig_name(#(#unpack_exprs),*)))
|
Ok(Dynamic::from(#sig_name(#(#unpack_exprs),*)))
|
||||||
@ -822,7 +822,7 @@ impl ExportedFn {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let type_name = syn::Ident::new(on_type_name, proc_macro2::Span::call_site());
|
let type_name = syn::Ident::new(on_type_name, Span::call_site());
|
||||||
|
|
||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
let param_names = quote! {
|
let param_names = quote! {
|
||||||
|
@ -1,14 +1,6 @@
|
|||||||
use quote::{quote, ToTokens};
|
use quote::{quote, ToTokens};
|
||||||
use syn::{parse::Parse, parse::ParseStream};
|
use syn::{parse::Parse, parse::ParseStream};
|
||||||
|
|
||||||
use crate::function::ExportedFn;
|
|
||||||
use crate::rhai_module::ExportedConst;
|
|
||||||
|
|
||||||
#[cfg(no_std)]
|
|
||||||
use alloc::vec as new_vec;
|
|
||||||
#[cfg(not(no_std))]
|
|
||||||
use std::vec as new_vec;
|
|
||||||
|
|
||||||
#[cfg(no_std)]
|
#[cfg(no_std)]
|
||||||
use core::mem;
|
use core::mem;
|
||||||
#[cfg(not(no_std))]
|
#[cfg(not(no_std))]
|
||||||
@ -17,7 +9,8 @@ use std::mem;
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use crate::attrs::{AttrItem, ExportInfo, ExportScope, ExportedParams};
|
use crate::attrs::{AttrItem, ExportInfo, ExportScope, ExportedParams};
|
||||||
use crate::function::ExportedFnParams;
|
use crate::function::ExportedFn;
|
||||||
|
use crate::rhai_module::ExportedConst;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Hash, Default)]
|
#[derive(Debug, Clone, Eq, PartialEq, Hash, Default)]
|
||||||
pub struct ExportedModParams {
|
pub struct ExportedModParams {
|
||||||
@ -32,9 +25,7 @@ impl Parse for ExportedModParams {
|
|||||||
return Ok(ExportedModParams::default());
|
return Ok(ExportedModParams::default());
|
||||||
}
|
}
|
||||||
|
|
||||||
let info = crate::attrs::parse_attr_items(args)?;
|
Self::from_info(crate::attrs::parse_attr_items(args)?)
|
||||||
|
|
||||||
Self::from_info(info)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,8 +106,8 @@ impl Parse for Module {
|
|||||||
fn parse(input: ParseStream) -> syn::Result<Self> {
|
fn parse(input: ParseStream) -> syn::Result<Self> {
|
||||||
let mut mod_all: syn::ItemMod = input.parse()?;
|
let mut mod_all: syn::ItemMod = input.parse()?;
|
||||||
let fns: Vec<_>;
|
let fns: Vec<_>;
|
||||||
let mut consts: Vec<_> = new_vec![];
|
let mut consts = Vec::new();
|
||||||
let mut sub_modules: Vec<_> = Vec::new();
|
let mut sub_modules = Vec::new();
|
||||||
if let Some((_, ref mut content)) = mod_all.content {
|
if let Some((_, ref mut content)) = mod_all.content {
|
||||||
// Gather and parse functions.
|
// Gather and parse functions.
|
||||||
fns = content
|
fns = content
|
||||||
@ -129,11 +120,9 @@ impl Parse for Module {
|
|||||||
// #[cfg] attributes are not allowed on functions
|
// #[cfg] attributes are not allowed on functions
|
||||||
crate::attrs::deny_cfg_attr(&item_fn.attrs)?;
|
crate::attrs::deny_cfg_attr(&item_fn.attrs)?;
|
||||||
|
|
||||||
let params: ExportedFnParams =
|
let params =
|
||||||
match crate::attrs::inner_item_attributes(&mut item_fn.attrs, "rhai_fn") {
|
crate::attrs::inner_item_attributes(&mut item_fn.attrs, "rhai_fn")?;
|
||||||
Ok(p) => p,
|
|
||||||
Err(e) => return Err(e),
|
|
||||||
};
|
|
||||||
syn::parse2::<ExportedFn>(item_fn.to_token_stream())
|
syn::parse2::<ExportedFn>(item_fn.to_token_stream())
|
||||||
.and_then(|mut f| {
|
.and_then(|mut f| {
|
||||||
f.set_params(params)?;
|
f.set_params(params)?;
|
||||||
@ -155,11 +144,9 @@ impl Parse for Module {
|
|||||||
}) => {
|
}) => {
|
||||||
// #[cfg] attributes are not allowed on const declarations
|
// #[cfg] attributes are not allowed on const declarations
|
||||||
crate::attrs::deny_cfg_attr(&attrs)?;
|
crate::attrs::deny_cfg_attr(&attrs)?;
|
||||||
match vis {
|
|
||||||
syn::Visibility::Public(_) => {
|
if matches!(vis, syn::Visibility::Public(_)) {
|
||||||
consts.push((ident.to_string(), ty.clone(), expr.as_ref().clone()))
|
consts.push((ident.to_string(), ty.clone(), expr.as_ref().clone()))
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
@ -192,7 +179,7 @@ impl Parse for Module {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fns = new_vec![];
|
fns = Vec::new();
|
||||||
}
|
}
|
||||||
Ok(Module {
|
Ok(Module {
|
||||||
mod_all,
|
mod_all,
|
||||||
@ -205,7 +192,7 @@ impl Parse for Module {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Module {
|
impl Module {
|
||||||
pub fn attrs(&self) -> &Vec<syn::Attribute> {
|
pub fn attrs(&self) -> &[syn::Attribute] {
|
||||||
&self.mod_all.attrs
|
&self.mod_all.attrs
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -273,10 +260,12 @@ impl Module {
|
|||||||
|
|
||||||
// NB: sub-modules must have their new items for exporting generated in depth-first order
|
// NB: sub-modules must have their new items for exporting generated in depth-first order
|
||||||
// to avoid issues caused by re-parsing them
|
// to avoid issues caused by re-parsing them
|
||||||
let inner_modules: Vec<proc_macro2::TokenStream> = sub_modules.drain(..)
|
let inner_modules = sub_modules
|
||||||
.try_fold::<Vec<proc_macro2::TokenStream>, _,
|
.drain(..)
|
||||||
Result<Vec<proc_macro2::TokenStream>, syn::Error>>(
|
.try_fold::<_, _, Result<_, syn::Error>>(Vec::new(), |mut acc, m| {
|
||||||
Vec::new(), |mut acc, m| { acc.push(m.generate_inner()?); Ok(acc) })?;
|
acc.push(m.generate_inner()?);
|
||||||
|
Ok(acc)
|
||||||
|
})?;
|
||||||
|
|
||||||
// Regenerate the module with the new content added.
|
// Regenerate the module with the new content added.
|
||||||
Ok(quote! {
|
Ok(quote! {
|
||||||
@ -319,7 +308,7 @@ impl Module {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn content(&self) -> Option<&Vec<syn::Item>> {
|
pub fn content(&self) -> Option<&[syn::Item]> {
|
||||||
match self.mod_all {
|
match self.mod_all {
|
||||||
syn::ItemMod {
|
syn::ItemMod {
|
||||||
content: Some((_, ref vec)),
|
content: Some((_, ref vec)),
|
||||||
|
@ -22,10 +22,11 @@ type RegisterMacroInput = (syn::Expr, proc_macro2::TokenStream, syn::Path);
|
|||||||
pub fn parse_register_macro(
|
pub fn parse_register_macro(
|
||||||
args: proc_macro::TokenStream,
|
args: proc_macro::TokenStream,
|
||||||
) -> Result<RegisterMacroInput, syn::Error> {
|
) -> Result<RegisterMacroInput, syn::Error> {
|
||||||
let parser = syn::punctuated::Punctuated::<syn::Expr, syn::Token![,]>::parse_separated_nonempty;
|
let args = syn::punctuated::Punctuated::<_, syn::Token![,]>::parse_separated_nonempty
|
||||||
let args = parser.parse(args).unwrap();
|
.parse(args)
|
||||||
|
.unwrap();
|
||||||
let arg_span = args.span();
|
let arg_span = args.span();
|
||||||
let mut items: Vec<syn::Expr> = args.into_iter().collect();
|
let mut items: Vec<_> = args.into_iter().collect();
|
||||||
if items.len() != 3 {
|
if items.len() != 3 {
|
||||||
return Err(syn::Error::new(
|
return Err(syn::Error::new(
|
||||||
arg_span,
|
arg_span,
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
use std::collections::BTreeMap;
|
use proc_macro2::{Span, TokenStream};
|
||||||
|
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
|
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
use crate::attrs::ExportScope;
|
use crate::attrs::ExportScope;
|
||||||
use crate::function::{
|
use crate::function::{
|
||||||
flatten_type_groups, print_type, ExportedFn, FnNamespaceAccess, FnSpecialAccess, FN_GET,
|
flatten_type_groups, print_type, ExportedFn, FnNamespaceAccess, FnSpecialAccess, FN_GET,
|
||||||
@ -16,17 +17,17 @@ pub fn generate_body(
|
|||||||
consts: &[ExportedConst],
|
consts: &[ExportedConst],
|
||||||
sub_modules: &mut [Module],
|
sub_modules: &mut [Module],
|
||||||
parent_scope: &ExportScope,
|
parent_scope: &ExportScope,
|
||||||
) -> proc_macro2::TokenStream {
|
) -> TokenStream {
|
||||||
let mut set_fn_statements: Vec<syn::Stmt> = Vec::new();
|
let mut set_fn_statements = Vec::new();
|
||||||
let mut set_const_statements: Vec<syn::Stmt> = Vec::new();
|
let mut set_const_statements = Vec::new();
|
||||||
let mut add_mod_blocks: Vec<syn::ExprBlock> = Vec::new();
|
let mut add_mod_blocks = Vec::new();
|
||||||
let mut set_flattened_mod_blocks: Vec<syn::ExprBlock> = Vec::new();
|
let mut set_flattened_mod_blocks = Vec::new();
|
||||||
let str_type_path = syn::parse2::<syn::Path>(quote! { str }).unwrap();
|
let str_type_path = syn::parse2::<syn::Path>(quote! { str }).unwrap();
|
||||||
let string_type_path = syn::parse2::<syn::Path>(quote! { String }).unwrap();
|
let string_type_path = syn::parse2::<syn::Path>(quote! { String }).unwrap();
|
||||||
|
|
||||||
for (const_name, _, _) in consts {
|
for (const_name, _, _) in consts {
|
||||||
let const_literal = syn::LitStr::new(&const_name, proc_macro2::Span::call_site());
|
let const_literal = syn::LitStr::new(&const_name, Span::call_site());
|
||||||
let const_ref = syn::Ident::new(&const_name, proc_macro2::Span::call_site());
|
let const_ref = syn::Ident::new(&const_name, Span::call_site());
|
||||||
set_const_statements.push(
|
set_const_statements.push(
|
||||||
syn::parse2::<syn::Stmt>(quote! {
|
syn::parse2::<syn::Stmt>(quote! {
|
||||||
m.set_var(#const_literal, #const_ref);
|
m.set_var(#const_literal, #const_ref);
|
||||||
@ -41,11 +42,9 @@ pub fn generate_body(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let module_name = item_mod.module_name();
|
let module_name = item_mod.module_name();
|
||||||
let exported_name: syn::LitStr = syn::LitStr::new(
|
let exported_name: syn::LitStr =
|
||||||
item_mod.exported_name().as_ref(),
|
syn::LitStr::new(item_mod.exported_name().as_ref(), Span::call_site());
|
||||||
proc_macro2::Span::call_site(),
|
let cfg_attrs: Vec<_> = item_mod
|
||||||
);
|
|
||||||
let cfg_attrs: Vec<&syn::Attribute> = item_mod
|
|
||||||
.attrs()
|
.attrs()
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|&a| a.path.get_ident().map(|i| *i == "cfg").unwrap_or(false))
|
.filter(|&a| a.path.get_ident().map(|i| *i == "cfg").unwrap_or(false))
|
||||||
@ -71,7 +70,8 @@ pub fn generate_body(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NB: these are token streams, because re-parsing messes up "> >" vs ">>"
|
// NB: these are token streams, because re-parsing messes up "> >" vs ">>"
|
||||||
let mut gen_fn_tokens: Vec<proc_macro2::TokenStream> = Vec::new();
|
let mut gen_fn_tokens = Vec::new();
|
||||||
|
|
||||||
for function in fns {
|
for function in fns {
|
||||||
function.update_scope(&parent_scope);
|
function.update_scope(&parent_scope);
|
||||||
if function.skipped() {
|
if function.skipped() {
|
||||||
@ -83,7 +83,7 @@ pub fn generate_body(
|
|||||||
);
|
);
|
||||||
let reg_names = function.exported_names();
|
let reg_names = function.exported_names();
|
||||||
|
|
||||||
let fn_input_types: Vec<syn::Expr> = function
|
let fn_input_types: Vec<_> = function
|
||||||
.arg_list()
|
.arg_list()
|
||||||
.map(|fn_arg| match fn_arg {
|
.map(|fn_arg| match fn_arg {
|
||||||
syn::FnArg::Receiver(_) => panic!("internal error: receiver fn outside impl!?"),
|
syn::FnArg::Receiver(_) => panic!("internal error: receiver fn outside impl!?"),
|
||||||
@ -229,8 +229,8 @@ pub fn check_rename_collisions(fns: &[ExportedFn]) -> Result<(), syn::Error> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut renames = BTreeMap::<String, proc_macro2::Span>::new();
|
let mut renames = BTreeMap::new();
|
||||||
let mut fn_defs = BTreeMap::<String, proc_macro2::Span>::new();
|
let mut fn_defs = BTreeMap::new();
|
||||||
|
|
||||||
for item_fn in fns.iter() {
|
for item_fn in fns.iter() {
|
||||||
if !item_fn.params().name.is_empty() || item_fn.params().special != FnSpecialAccess::None {
|
if !item_fn.params().name.is_empty() || item_fn.params().special != FnSpecialAccess::None {
|
||||||
|
@ -126,7 +126,7 @@ mod function_tests {
|
|||||||
let err = syn::parse2::<ExportedFn>(input_tokens).unwrap_err();
|
let err = syn::parse2::<ExportedFn>(input_tokens).unwrap_err();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
format!("{}", err),
|
format!("{}", err),
|
||||||
"this type in this position passes from Rhai by value"
|
"function parameters other than the first one cannot be passed by reference"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,7 +139,7 @@ mod function_tests {
|
|||||||
let err = syn::parse2::<ExportedFn>(input_tokens).unwrap_err();
|
let err = syn::parse2::<ExportedFn>(input_tokens).unwrap_err();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
format!("{}", err),
|
format!("{}", err),
|
||||||
"this type in this position passes from Rhai by value"
|
"function parameters other than the first one cannot be passed by reference"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
error: this type in this position passes from Rhai by value
|
error: function parameters other than the first one cannot be passed by reference
|
||||||
--> ui_tests/second_shared_ref.rs:12:41
|
--> ui_tests/second_shared_ref.rs:12:41
|
||||||
|
|
|
|
||||||
12 | pub fn test_fn(input: Clonable, factor: &bool) -> bool {
|
12 | pub fn test_fn(input: Clonable, factor: &bool) -> bool {
|
||||||
|
Loading…
Reference in New Issue
Block a user