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,10 @@ 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 ) ?; 
131+ 
132+     let  default_parameter_types = ( ) ; 
129133    // Transport #[cfg] attrs to the FFI glue to ensure functions which were conditionally 
130134    // removed from compilation don't cause errors. 
131135    let  cfg_attrs = util:: extract_cfg_attrs ( & func_definition. external_attributes ) 
@@ -158,6 +162,9 @@ pub fn make_method_registration(
158162                    & [ 
159163                        #(  #param_ident_strs ) , * 
160164                    ] , 
165+                     vec![ 
166+                         #( :: godot:: builtin:: Variant :: from( #default_parameters) ) , * 
167+                     ] 
161168                ) 
162169            } ; 
163170
@@ -175,6 +182,32 @@ pub fn make_method_registration(
175182    Ok ( registration) 
176183} 
177184
185+ fn  validate_default_parameters ( 
186+     default_parameters :  & [ Option < TokenStream > ] , 
187+ )  -> ParseResult < Vec < TokenStream > >  { 
188+     let  mut  res = vec ! [ ] ; 
189+     let  mut  allowed = true ; 
190+     for  param in  default_parameters. iter ( ) . rev ( )  { 
191+         match  ( param,  allowed)  { 
192+             ( Some ( tk) ,  true )  => { 
193+                 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 
194+             } 
195+             ( None ,  true )  => { 
196+                 allowed = false ; 
197+             } 
198+             ( None ,  false )  => { } 
199+             ( Some ( tk) ,  false )  => { 
200+                 return  bail ! ( 
201+                     tk, 
202+                     "opt arguments are only allowed at the end of the argument list." 
203+                 ) ; 
204+             } 
205+         } 
206+     } 
207+     res. reverse ( ) ; 
208+     Ok ( res) 
209+ } 
210+ 
178211// ---------------------------------------------------------------------------------------------------------------------------------------------- 
179212// Implementation 
180213
@@ -199,6 +232,8 @@ pub struct SignatureInfo {
199232/// 
200233/// Index points into original venial tokens (i.e. takes into account potential receiver params). 
201234pub  modified_param_types :  Vec < ( usize ,  venial:: TypeExpr ) > , 
235+     /// Contains expressions of the default values of parameters. 
236+ pub  default_parameters :  Vec < Option < TokenStream > > , 
202237} 
203238
204239impl  SignatureInfo  { 
@@ -210,6 +245,7 @@ impl SignatureInfo {
210245            param_types :  vec ! [ ] , 
211246            return_type :  quote !  {  ( )  } , 
212247            modified_param_types :  vec ! [ ] , 
248+             default_parameters :  vec ! [ ] , 
213249        } 
214250    } 
215251
@@ -412,6 +448,7 @@ pub(crate) fn into_signature_info(
412448        param_types, 
413449        return_type :  ret_type, 
414450        modified_param_types, 
451+         default_parameters :  vec ! [ ] , 
415452    } 
416453} 
417454
0 commit comments