1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
use std::fmt::{self, Debug};
use std::marker::PhantomData;
use std::ptr::{self, NonNull};

use crate::core_types::GodotString;
use crate::object::memory::RefCounted;
use crate::private::get_api;
use crate::sys;

use super::GodotObject;

/// An opaque struct representing Godot objects. This should never be created on the stack.
///
/// This is an internal interface. Users are expected to use references to named generated types
/// instead.
#[repr(C)]
pub struct RawObject<T> {
    _opaque: [u8; 0],
    _marker: PhantomData<(T, *const ())>,
}

impl<T: GodotObject> RawObject<T> {
    /// Creates a typed reference from a pointer, without checking the type of the pointer.
    ///
    /// # Safety
    ///
    /// The `obj` pointer must be pointing to a valid Godot object of type `T` during the
    /// entirety of `'a`.
    #[inline]
    pub unsafe fn from_sys_ref_unchecked<'a>(obj: NonNull<sys::godot_object>) -> &'a Self {
        &*(obj.as_ptr() as *mut Self)
    }

    /// Creates a typed reference from a pointer if the pointer is pointing to an object of
    /// the correct type. Returns `None` otherwise.
    ///
    /// # Safety
    ///
    /// The `obj` pointer must be pointing to a valid Godot object during the entirety of `'a`.
    #[inline]
    pub unsafe fn try_from_sys_ref<'a>(obj: NonNull<sys::godot_object>) -> Option<&'a Self> {
        if ptr_is_class(obj.as_ptr(), T::class_name()) {
            Some(Self::from_sys_ref_unchecked(obj))
        } else {
            None
        }
    }

    /// Casts a reference to this opaque object to `*const sys::godot_object`.
    #[inline]
    pub fn sys(&self) -> NonNull<sys::godot_object> {
        // SAFETY: references should never be null
        unsafe { NonNull::new_unchecked(self as *const _ as *mut _) }
    }

    /// Checks whether the object is of a certain Godot class.
    #[inline]
    pub fn is_class<U: GodotObject>(&self) -> bool {
        self.is_class_by_name(U::class_name())
    }

    /// Checks whether the object is of a certain Godot class by name.
    #[inline]
    pub fn is_class_by_name(&self, class_name: &str) -> bool {
        unsafe { ptr_is_class(self.sys().as_ptr(), class_name) }
    }

    /// Returns the class name of this object dynamically using `Object::get_class`.
    #[inline]
    pub fn class_name(&self) -> String {
        let api = crate::private::get_api();
        let get_class_method = crate::private::ObjectMethodTable::get(api).get_class;
        let mut argument_buffer = [ptr::null() as *const libc::c_void; 0];
        let mut class_name = sys::godot_string::default();
        let ret_ptr = &mut class_name as *mut sys::godot_string;

        unsafe {
            (api.godot_method_bind_ptrcall)(
                get_class_method,
                self.sys().as_ptr(),
                argument_buffer.as_mut_ptr() as *mut _,
                ret_ptr as *mut _,
            );
        }

        let string = GodotString::from_sys(class_name);
        string.to_string()
    }

    /// Attempt to cast a Godot object to a different class type.
    #[inline]
    pub fn cast<U>(&self) -> Option<&RawObject<U>>
    where
        U: GodotObject,
    {
        unsafe { RawObject::try_from_sys_ref(self.sys()) }
    }

    /// Attempt to cast a Godot object to a different class type without checking the type at
    /// runtime.
    ///
    /// # Safety
    ///
    /// The types must be compatible.
    #[inline]
    pub unsafe fn cast_unchecked<U>(&self) -> &RawObject<U>
    where
        U: GodotObject,
    {
        RawObject::from_sys_ref_unchecked(self.sys())
    }

    /// Free the underlying object.
    ///
    /// # Safety
    ///
    /// Further operations must not be performed on the same reference.
    #[inline]
    pub unsafe fn free(&self) {
        (get_api().godot_object_destroy)(self.sys().as_ptr());
    }
}

impl<T: GodotObject<Memory = RefCounted>> RawObject<T> {
    /// Increase the reference count of the object.
    #[inline]
    pub fn add_ref(&self) {
        let api = crate::private::get_api();
        let addref_method = crate::private::ReferenceMethodTable::get(api).reference;
        let mut argument_buffer = [ptr::null() as *const libc::c_void; 0];
        let mut ok = false;
        let ok_ptr = &mut ok as *mut bool;

        unsafe {
            (api.godot_method_bind_ptrcall)(
                addref_method,
                self.sys().as_ptr(),
                argument_buffer.as_mut_ptr() as *mut _,
                ok_ptr as *mut _,
            );
        }

        // If this assertion blows up it means there is a reference counting bug
        // and we tried to increment the ref count of a dead object (who's ref
        // count is equal to zero).
        debug_assert!(ok);
    }

    /// Decrease the reference count of the object. Returns `true` if this is the last
    /// reference.
    ///
    /// # Safety
    ///
    /// Further operations must not be performed on the same reference if this is the last
    /// reference.
    #[inline]
    pub unsafe fn unref(&self) -> bool {
        let api = crate::private::get_api();
        let unref_method = crate::private::ReferenceMethodTable::get(api).unreference;

        let mut argument_buffer = [ptr::null() as *const libc::c_void; 0];
        let mut last_reference = false;
        let ret_ptr = &mut last_reference as *mut bool;
        (api.godot_method_bind_ptrcall)(
            unref_method,
            self.sys().as_ptr(),
            argument_buffer.as_mut_ptr() as *mut _,
            ret_ptr as *mut _,
        );

        last_reference
    }

    /// Decrease the reference count of the object. Frees the object and returns `true` if this
    /// is the last reference.
    ///
    /// # Safety
    ///
    /// Further operations must not be performed on the same reference if this is the last
    /// reference.
    #[inline]
    pub unsafe fn unref_and_free_if_last(&self) -> bool {
        let last_reference = self.unref();

        if last_reference {
            self.free();
        }

        last_reference
    }

    /// Initialize the reference count of the object.
    ///
    /// # Safety
    ///
    /// This function assumes that no other references are held at the time.
    #[inline]
    pub unsafe fn init_ref_count(&self) {
        let obj = self.sys().as_ptr();

        let api = crate::private::get_api();
        let init_method = crate::private::ReferenceMethodTable::get(api).init_ref;

        let mut argument_buffer = [ptr::null() as *const libc::c_void; 0];
        let mut ok = false;
        let ret_ptr = &mut ok as *mut bool;
        (api.godot_method_bind_ptrcall)(
            init_method,
            obj,
            argument_buffer.as_mut_ptr() as *mut _,
            ret_ptr as *mut _,
        );

        debug_assert!(ok);
    }
}

impl<T: GodotObject> Debug for RawObject<T> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}({:p})", T::class_name(), self.sys())
    }
}

/// Checks whether the raw object pointer is of a certain Godot class.
///
/// # Safety
///
/// The `obj` pointer must be pointing to a valid Godot object.
#[inline]
unsafe fn ptr_is_class(obj: *mut sys::godot_object, class_name: &str) -> bool {
    let api = crate::private::get_api();
    let method_bind = crate::private::ObjectMethodTable::get(api).is_class;

    let mut class_name = (api.godot_string_chars_to_utf8_with_len)(
        class_name.as_ptr() as *const _,
        class_name.len() as _,
    );

    let mut argument_buffer = [ptr::null() as *const libc::c_void; 1];
    argument_buffer[0] = (&class_name) as *const _ as *const _;

    let mut ret = false;
    let ret_ptr = &mut ret as *mut _;
    (api.godot_method_bind_ptrcall)(
        method_bind,
        obj,
        argument_buffer.as_mut_ptr() as *mut _,
        ret_ptr as *mut _,
    );

    (api.godot_string_destroy)(&mut class_name);

    ret
}