From 37542dc2ec1e98fbe93e2daa8f11e7fb5428cb0e Mon Sep 17 00:00:00 2001 From: David Snopek Date: Tue, 5 Mar 2024 11:11:29 -0600 Subject: [PATCH] Correctly handle `Object *` arguments that were encoded as `nullptr` --- include/godot_cpp/core/method_ptrcall.hpp | 8 ++++---- test/project/main.gd | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/godot_cpp/core/method_ptrcall.hpp b/include/godot_cpp/core/method_ptrcall.hpp index 32f3f459..b3966140 100644 --- a/include/godot_cpp/core/method_ptrcall.hpp +++ b/include/godot_cpp/core/method_ptrcall.hpp @@ -170,11 +170,11 @@ template struct PtrToArg { static_assert(std::is_base_of::value, "Cannot encode non-Object value as an Object"); _FORCE_INLINE_ static T *convert(const void *p_ptr) { - return reinterpret_cast(godot::internal::get_object_instance_binding(*reinterpret_cast(const_cast(p_ptr)))); + return likely(p_ptr) ? reinterpret_cast(godot::internal::get_object_instance_binding(*reinterpret_cast(const_cast(p_ptr)))) : nullptr; } typedef Object *EncodeT; _FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) { - *reinterpret_cast(p_ptr) = p_var ? p_var->_owner : nullptr; + *reinterpret_cast(p_ptr) = likely(p_var) ? p_var->_owner : nullptr; } }; @@ -182,11 +182,11 @@ template struct PtrToArg { static_assert(std::is_base_of::value, "Cannot encode non-Object value as an Object"); _FORCE_INLINE_ static const T *convert(const void *p_ptr) { - return reinterpret_cast(godot::internal::get_object_instance_binding(*reinterpret_cast(const_cast(p_ptr)))); + return likely(p_ptr) ? reinterpret_cast(godot::internal::get_object_instance_binding(*reinterpret_cast(const_cast(p_ptr)))) : nullptr; } typedef const Object *EncodeT; _FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) { - *reinterpret_cast(p_ptr) = p_var ? p_var->_owner : nullptr; + *reinterpret_cast(p_ptr) = likely(p_var) ? p_var->_owner : nullptr; } }; diff --git a/test/project/main.gd b/test/project/main.gd index d2cbd261..14658c3b 100644 --- a/test/project/main.gd +++ b/test/project/main.gd @@ -184,6 +184,10 @@ func _ready(): control.queue_free() sprite.queue_free() + # Test that passing null for objects works as expected too. + var example_null : Example = null + assert_equal(example.test_object_cast_to_node(example_null), false) + # Test conversions to and from Variant. assert_equal(example.test_variant_vector2i_conversion(Vector2i(1, 1)), Vector2i(1, 1)) assert_equal(example.test_variant_vector2i_conversion(Vector2(1.0, 1.0)), Vector2i(1, 1))