Skip to content

Commit 37542dc

Browse files
committed
Correctly handle Object * arguments that were encoded as nullptr
1 parent e55b792 commit 37542dc

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

include/godot_cpp/core/method_ptrcall.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,23 +170,23 @@ template <class T>
170170
struct PtrToArg<T *> {
171171
static_assert(std::is_base_of<Object, T>::value, "Cannot encode non-Object value as an Object");
172172
_FORCE_INLINE_ static T *convert(const void *p_ptr) {
173-
return reinterpret_cast<T *>(godot::internal::get_object_instance_binding(*reinterpret_cast<GDExtensionObjectPtr *>(const_cast<void *>(p_ptr))));
173+
return likely(p_ptr) ? reinterpret_cast<T *>(godot::internal::get_object_instance_binding(*reinterpret_cast<GDExtensionObjectPtr *>(const_cast<void *>(p_ptr)))) : nullptr;
174174
}
175175
typedef Object *EncodeT;
176176
_FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) {
177-
*reinterpret_cast<const void **>(p_ptr) = p_var ? p_var->_owner : nullptr;
177+
*reinterpret_cast<const void **>(p_ptr) = likely(p_var) ? p_var->_owner : nullptr;
178178
}
179179
};
180180

181181
template <class T>
182182
struct PtrToArg<const T *> {
183183
static_assert(std::is_base_of<Object, T>::value, "Cannot encode non-Object value as an Object");
184184
_FORCE_INLINE_ static const T *convert(const void *p_ptr) {
185-
return reinterpret_cast<const T *>(godot::internal::get_object_instance_binding(*reinterpret_cast<GDExtensionObjectPtr *>(const_cast<void *>(p_ptr))));
185+
return likely(p_ptr) ? reinterpret_cast<const T *>(godot::internal::get_object_instance_binding(*reinterpret_cast<GDExtensionObjectPtr *>(const_cast<void *>(p_ptr)))) : nullptr;
186186
}
187187
typedef const Object *EncodeT;
188188
_FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) {
189-
*reinterpret_cast<const void **>(p_ptr) = p_var ? p_var->_owner : nullptr;
189+
*reinterpret_cast<const void **>(p_ptr) = likely(p_var) ? p_var->_owner : nullptr;
190190
}
191191
};
192192

test/project/main.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ func _ready():
184184
control.queue_free()
185185
sprite.queue_free()
186186

187+
# Test that passing null for objects works as expected too.
188+
var example_null : Example = null
189+
assert_equal(example.test_object_cast_to_node(example_null), false)
190+
187191
# Test conversions to and from Variant.
188192
assert_equal(example.test_variant_vector2i_conversion(Vector2i(1, 1)), Vector2i(1, 1))
189193
assert_equal(example.test_variant_vector2i_conversion(Vector2(1.0, 1.0)), Vector2i(1, 1))

0 commit comments

Comments
 (0)