@@ -270,3 +270,63 @@ impl PropertyInfo {
270270 }
271271 }
272272}
273+
274+ #[ derive( Debug ) ]
275+ pub struct MethodInfo {
276+ pub id : i32 ,
277+ pub method_name : StringName ,
278+ pub class_name : ClassName ,
279+ pub return_type : PropertyInfo ,
280+ pub arguments : Vec < PropertyInfo > ,
281+ pub default_arguments : Vec < Variant > ,
282+ pub flags : global:: MethodFlags ,
283+ }
284+
285+ impl MethodInfo {
286+ /// Converts to the FFI type. Keep this object allocated while using that!
287+ ///
288+ /// The struct returned by this function contains pointers into the fields of `self`. `self` should therefore not be dropped while the
289+ /// [`sys::GDExtensionMethodInfo`] is still in use.
290+ ///
291+ /// This function also leaks memory that has to be cleaned up by the caller once it is no longer used. Specifically the `arguments` and
292+ /// `default_arguments` vectors have to be reconstruced from the pointer and length and then dropped / freed.
293+ ///
294+ /// Each vector can be reconstructed with `Vec::from_raw_parts` since the pointers where created with `Vec::into_boxed_slice` which
295+ /// gurantees that vector capacity and length are equal.
296+ pub fn method_sys ( & self ) -> sys:: GDExtensionMethodInfo {
297+ use crate :: obj:: EngineEnum as _;
298+
299+ let argument_count = self . arguments . len ( ) as u32 ;
300+ let argument_vec = self
301+ . arguments
302+ . iter ( )
303+ . map ( |arg| arg. property_sys ( ) )
304+ . collect :: < Vec < _ > > ( )
305+ . into_boxed_slice ( ) ;
306+
307+ // SAFETY: dereferencing the new box pointer is fine as it is guaranteed to not be null
308+ let arguments = unsafe { ( * Box :: into_raw ( argument_vec) ) . as_mut_ptr ( ) } ;
309+
310+ let default_argument_count = self . default_arguments . len ( ) as u32 ;
311+ let default_argument_vec = self
312+ . default_arguments
313+ . iter ( )
314+ . map ( |arg| arg. var_sys ( ) )
315+ . collect :: < Vec < _ > > ( )
316+ . into_boxed_slice ( ) ;
317+
318+ // SAFETY: dereferencing the new box pointer is fine as it is guaranteed to not be null
319+ let default_arguments = unsafe { ( * Box :: into_raw ( default_argument_vec) ) . as_mut_ptr ( ) } ;
320+
321+ sys:: GDExtensionMethodInfo {
322+ id : self . id ,
323+ name : self . method_name . string_sys ( ) ,
324+ return_value : self . return_type . property_sys ( ) ,
325+ argument_count,
326+ arguments,
327+ default_argument_count,
328+ default_arguments,
329+ flags : u32:: try_from ( self . flags . ord ( ) ) . expect ( "flags should be valid" ) ,
330+ }
331+ }
332+ }
0 commit comments