File tree Expand file tree Collapse file tree 7 files changed +57
-12
lines changed
itest/rust/src/object_tests Expand file tree Collapse file tree 7 files changed +57
-12
lines changed Original file line number Diff line number Diff line change @@ -325,7 +325,11 @@ impl From<&str> for GString {
325
325
326
326
unsafe {
327
327
Self :: new_with_string_uninit ( |string_ptr| {
328
+ #[ cfg( before_api = "4.3" ) ]
328
329
let ctor = interface_fn ! ( string_new_with_utf8_chars_and_len) ;
330
+ #[ cfg( since_api = "4.3" ) ]
331
+ let ctor = interface_fn ! ( string_new_with_utf8_chars_and_len2) ;
332
+
329
333
ctor (
330
334
string_ptr,
331
335
bytes. as_ptr ( ) as * const std:: ffi:: c_char ,
Original file line number Diff line number Diff line change @@ -60,11 +60,7 @@ pub(crate) fn construct_engine_object<T>() -> Gd<T>
60
60
where
61
61
T : GodotClass + Bounds < Declarer = bounds:: DeclEngine > ,
62
62
{
63
- // SAFETY: adhere to Godot API; valid class name and returned pointer is an object.
64
- unsafe {
65
- let object_ptr = sys:: interface_fn!( classdb_construct_object) ( T :: class_name ( ) . string_sys ( ) ) ;
66
- Gd :: from_obj_sys ( object_ptr)
67
- }
63
+ Gd :: new_alloc_postinited ( )
68
64
}
69
65
70
66
pub ( crate ) fn ensure_object_alive (
Original file line number Diff line number Diff line change @@ -389,11 +389,7 @@ impl Declarer for DeclEngine {
389
389
where
390
390
T : GodotDefault + Bounds < Declarer = Self > ,
391
391
{
392
- unsafe {
393
- let object_ptr =
394
- sys:: interface_fn!( classdb_construct_object) ( T :: class_name ( ) . string_sys ( ) ) ;
395
- Gd :: from_obj_sys ( object_ptr)
396
- }
392
+ Gd :: new_alloc_postinited ( )
397
393
}
398
394
}
399
395
Original file line number Diff line number Diff line change @@ -560,6 +560,24 @@ impl<T: GodotClass> Gd<T> {
560
560
// Do not increment ref-count; assumed to be return value from FFI.
561
561
sys:: ptr_then ( object_ptr, |ptr| Gd :: from_obj_sys_weak ( ptr) )
562
562
}
563
+
564
+ pub ( crate ) fn new_alloc_postinited ( ) -> Self {
565
+ #[ cfg( before_api = "4.4" ) ]
566
+ unsafe {
567
+ let object_ptr = sys:: classdb_construct_object ( T :: class_name ( ) . string_sys ( ) ) ;
568
+ Gd :: from_obj_sys ( object_ptr)
569
+ }
570
+ #[ cfg( since_api = "4.4" ) ]
571
+ unsafe {
572
+ let object_ptr =
573
+ sys:: classdb_construct_object_no_postinit ( T :: class_name ( ) . string_sys ( ) ) ;
574
+ let obj = Gd :: < T > :: from_obj_sys ( object_ptr) ;
575
+ obj. clone ( )
576
+ . upcast_object ( )
577
+ . notify ( crate :: classes:: notify:: ObjectNotification :: POSTINITIALIZE ) ;
578
+ Gd :: from_obj_sys ( object_ptr)
579
+ }
580
+ }
563
581
}
564
582
565
583
/// _The methods in this impl block are only available for objects `T` that are manually managed,
Original file line number Diff line number Diff line change @@ -33,7 +33,14 @@ pub unsafe extern "C" fn create<T: cap::GodotDefault>(
33
33
_class_userdata : * mut std:: ffi:: c_void ,
34
34
_notify_postinitialize : sys:: GDExtensionBool ,
35
35
) -> sys:: GDExtensionObjectPtr {
36
- create_custom ( T :: __godot_user_init) . unwrap_or ( std:: ptr:: null_mut ( ) )
36
+ if let Ok ( object_ptr) = create_custom ( T :: __godot_user_init) {
37
+ let mut obj = Gd :: < T > :: from_obj_sys_weak ( object_ptr) . upcast_object ( ) ;
38
+ obj. notify ( crate :: classes:: notify:: ObjectNotification :: POSTINITIALIZE ) ;
39
+ std:: mem:: forget ( obj) ;
40
+ object_ptr
41
+ } else {
42
+ std:: ptr:: null_mut ( )
43
+ }
37
44
}
38
45
39
46
#[ cfg( before_api = "4.4" ) ]
92
99
F : FnOnce ( Base < T :: Base > ) -> T ,
93
100
{
94
101
let base_class_name = T :: Base :: class_name ( ) ;
95
- let base_ptr = unsafe { interface_fn ! ( classdb_construct_object) ( base_class_name. string_sys ( ) ) } ;
102
+ #[ cfg( before_api = "4.4" ) ]
103
+ let base_ptr = unsafe { sys:: classdb_construct_object ( base_class_name. string_sys ( ) ) } ;
104
+ #[ cfg( since_api = "4.4" ) ]
105
+ let base_ptr =
106
+ unsafe { sys:: classdb_construct_object_no_postinit ( base_class_name. string_sys ( ) ) } ;
96
107
97
108
match create_rust_part_for_existing_godot_part ( make_user_instance, base_ptr) {
98
109
Ok ( _extension_ptr) => Ok ( base_ptr) ,
Original file line number Diff line number Diff line change @@ -440,6 +440,24 @@ pub fn is_main_thread() -> bool {
440
440
std:: thread:: current ( ) . id ( ) == main_thread_id ( )
441
441
}
442
442
443
+ /// # Safety
444
+ /// `class_name` is assumed to be valid.
445
+ #[ cfg( before_api = "4.4" ) ]
446
+ pub unsafe fn classdb_construct_object (
447
+ class_name : GDExtensionConstStringNamePtr ,
448
+ ) -> GDExtensionObjectPtr {
449
+ interface_fn ! ( classdb_construct_object) ( class_name)
450
+ }
451
+
452
+ /// # Safety
453
+ /// `class_name` is assumed to be valid.
454
+ #[ cfg( since_api = "4.4" ) ]
455
+ pub unsafe fn classdb_construct_object_no_postinit (
456
+ class_name : GDExtensionConstStringNamePtr ,
457
+ ) -> GDExtensionObjectPtr {
458
+ interface_fn ! ( classdb_construct_object2) ( class_name)
459
+ }
460
+
443
461
// ----------------------------------------------------------------------------------------------------------------------------------------------
444
462
// Macros to access low-level function bindings
445
463
Original file line number Diff line number Diff line change @@ -517,6 +517,8 @@ fn test_notifications() {
517
517
assert_eq ! (
518
518
obj. bind( ) . sequence,
519
519
vec![
520
+ #[ cfg( since_api = "4.4" ) ]
521
+ ReceivedEvent :: Notification ( NodeNotification :: POSTINITIALIZE ) ,
520
522
ReceivedEvent :: Notification ( NodeNotification :: UNPAUSED ) ,
521
523
ReceivedEvent :: Notification ( NodeNotification :: EDITOR_POST_SAVE ) ,
522
524
ReceivedEvent :: Ready ,
You can’t perform that action at this time.
0 commit comments