77
88use  crate :: class:: RpcAttr ; 
99use  crate :: util:: { bail_fn,  ident,  safe_ident} ; 
10- use  crate :: { util,  ParseResult } ; 
10+ use  crate :: { bail ,   util,  ParseResult } ; 
1111use  proc_macro2:: { Group ,  Ident ,  TokenStream ,  TokenTree } ; 
1212use  quote:: { format_ident,  quote} ; 
1313
@@ -126,6 +126,8 @@ pub fn make_method_registration(
126126        . iter ( ) 
127127        . map ( |ident| ident. to_string ( ) ) ; 
128128
129+     let  default_parameters =
130+         validate_default_parameters ( & func_definition. signature_info . default_parameters ) ?; 
129131    // Transport #[cfg] attrs to the FFI glue to ensure functions which were conditionally 
130132    // removed from compilation don't cause errors. 
131133    let  cfg_attrs = util:: extract_cfg_attrs ( & func_definition. external_attributes ) 
@@ -158,6 +160,9 @@ pub fn make_method_registration(
158160                    & [ 
159161                        #(  #param_ident_strs ) , * 
160162                    ] , 
163+                     vec![ 
164+                        #(  <#default_parameters as  :: godot:: meta:: AsArg >. to_variant( )  ) , * 
165+                     ] 
161166                ) 
162167            } ; 
163168
@@ -175,6 +180,32 @@ pub fn make_method_registration(
175180    Ok ( registration) 
176181} 
177182
183+ fn  validate_default_parameters ( 
184+     default_parameters :  & Vec < Option < TokenStream > > , 
185+ )  -> ParseResult < Vec < TokenStream > >  { 
186+     let  mut  res = vec ! [ ] ; 
187+     let  mut  allowed = true ; 
188+     for  param in  default_parameters. iter ( ) . rev ( )  { 
189+         match  ( param,  allowed)  { 
190+             ( Some ( tk) ,  true )  => { 
191+                 res. push ( tk. clone ( ) ) ;  // toreview: if we really care about it, we can use &mut sig_info and mem::take() as we don't use this later 
192+             } 
193+             ( None ,  true )  => { 
194+                 allowed = false ; 
195+             } 
196+             ( None ,  false )  => { } 
197+             ( Some ( tk) ,  false )  => { 
198+                 return  bail ! ( 
199+                     tk, 
200+                     "opt arguments are only allowed at the end of the argument list." 
201+                 ) ; 
202+             } 
203+         } 
204+     } 
205+     res. reverse ( ) ; 
206+     Ok ( res) 
207+ } 
208+ 
178209// ---------------------------------------------------------------------------------------------------------------------------------------------- 
179210// Implementation 
180211
@@ -199,6 +230,8 @@ pub struct SignatureInfo {
199230/// 
200231/// Index points into original venial tokens (i.e. takes into account potential receiver params). 
201232pub  modified_param_types :  Vec < ( usize ,  venial:: TypeExpr ) > , 
233+     /// Contains expressions of the default values of parameters. 
234+ pub  default_parameters :  Vec < Option < TokenStream > > , 
202235} 
203236
204237impl  SignatureInfo  { 
@@ -210,6 +243,7 @@ impl SignatureInfo {
210243            param_types :  vec ! [ ] , 
211244            return_type :  quote !  {  ( )  } , 
212245            modified_param_types :  vec ! [ ] , 
246+             default_parameters :  vec ! [ ] , 
213247        } 
214248    } 
215249
@@ -412,6 +446,7 @@ pub(crate) fn into_signature_info(
412446        param_types, 
413447        return_type :  ret_type, 
414448        modified_param_types, 
449+         default_parameters :  vec ! [ ] , 
415450    } 
416451} 
417452
0 commit comments