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