From 29d19fbed1e7c5c0efc22bf7b784c2ffb6e74430 Mon Sep 17 00:00:00 2001 From: DaylilyZeleen <735170336@qq.com> Date: Thu, 18 Jan 2024 17:23:21 +0800 Subject: [PATCH] Fix object return value of builtin types' methods. --- binding_generator.py | 19 +++++++++++++++++-- include/godot_cpp/core/builtin_ptrcall.hpp | 12 ++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/binding_generator.py b/binding_generator.py index 27721352..af667fe6 100644 --- a/binding_generator.py +++ b/binding_generator.py @@ -964,8 +964,20 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl result.append(method_signature + "{") method_call = "\t" + need_additional_right_bracke = False if "return_type" in method: - method_call += f'return internal::_call_builtin_method_ptr_ret<{correct_type(method["return_type"])}>(' + return_type = method["return_type"] + if not is_variant(return_type) and not is_pod_type(return_type) and not is_enum(return_type): + if is_refcounted(return_type): + # RefCounted + method_call += f"return Ref<{return_type}>::_gde_internal_constructor(" + need_additional_right_bracke = True + else: + # Object + method_call += f"return " + method_call += f"internal::_call_builtin_method_ptr_ret_obj<{return_type}>(" + else: + method_call += f"return internal::_call_builtin_method_ptr_ret<{correct_type(return_type)}>(" else: method_call += "internal::_call_builtin_method_ptr_no_ret(" method_call += f'_method_bindings.method_{method["name"]}, ' @@ -986,7 +998,10 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl result += encode arguments.append(arg_name) method_call += ", ".join(arguments) - method_call += ");" + if need_additional_right_bracke: + method_call += "));" + else: + method_call += ");" result.append(method_call) result.append("}") diff --git a/include/godot_cpp/core/builtin_ptrcall.hpp b/include/godot_cpp/core/builtin_ptrcall.hpp index 87311b8a..19250d84 100644 --- a/include/godot_cpp/core/builtin_ptrcall.hpp +++ b/include/godot_cpp/core/builtin_ptrcall.hpp @@ -32,6 +32,7 @@ #define GODOT_BUILTIN_PTRCALL_HPP #include +#include #include @@ -39,6 +40,17 @@ namespace godot { namespace internal { +template +O *_call_builtin_method_ptr_ret_obj(const GDExtensionPtrBuiltInMethod method, GDExtensionTypePtr base, const Args &...args) { + GodotObject *ret = nullptr; + std::array call_args = { { (GDExtensionConstTypePtr)args... } }; + method(base, call_args.data(), &ret, sizeof...(Args)); + if (ret == nullptr) { + return nullptr; + } + return reinterpret_cast(internal::get_object_instance_binding(ret)); +} + template void _call_builtin_constructor(const GDExtensionPtrConstructor constructor, GDExtensionTypePtr base, Args... args) { std::array call_args = { { (GDExtensionConstTypePtr)args... } };