Skip to content

Commit 6a3753c

Browse files
Fix object return value of builtin types' methods.
1 parent 0ddef6e commit 6a3753c

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

binding_generator.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -964,8 +964,19 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
964964
result.append(method_signature + "{")
965965

966966
method_call = "\t"
967+
is_ref = False
968+
967969
if "return_type" in method:
968-
method_call += f'return internal::_call_builtin_method_ptr_ret<{correct_type(method["return_type"])}>('
970+
return_type = method["return_type"]
971+
if is_enum(return_type):
972+
method_call += f"return ({get_gdextension_type(correct_type(return_type))})internal::_call_builtin_method_ptr_ret<int64_t>("
973+
elif is_pod_type(return_type) or is_variant(return_type):
974+
method_call += f"return internal::_call_builtin_method_ptr_ret<{get_gdextension_type(correct_type(return_type))}>("
975+
elif is_refcounted(return_type):
976+
method_call += f"return Ref<{return_type}>::_gde_internal_constructor(internal::_call_builtin_method_ptr_ret_obj<{return_type}>("
977+
is_ref = True
978+
else:
979+
method_call += f"return internal::_call_builtin_method_ptr_ret_obj<{return_type}>("
969980
else:
970981
method_call += "internal::_call_builtin_method_ptr_no_ret("
971982
method_call += f'_method_bindings.method_{method["name"]}, '
@@ -986,6 +997,9 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
986997
result += encode
987998
arguments.append(arg_name)
988999
method_call += ", ".join(arguments)
1000+
1001+
if is_ref:
1002+
method_call += ")" # Close Ref<> constructor.
9891003
method_call += ");"
9901004

9911005
result.append(method_call)

include/godot_cpp/core/builtin_ptrcall.hpp

+12
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,25 @@
3232
#define GODOT_BUILTIN_PTRCALL_HPP
3333

3434
#include <gdextension_interface.h>
35+
#include <godot_cpp/core/object.hpp>
3536

3637
#include <array>
3738

3839
namespace godot {
3940

4041
namespace internal {
4142

43+
template <class O, class... Args>
44+
O *_call_builtin_method_ptr_ret_obj(const GDExtensionPtrBuiltInMethod method, GDExtensionTypePtr base, const Args &...args) {
45+
GodotObject *ret = nullptr;
46+
std::array<GDExtensionConstTypePtr, sizeof...(Args)> call_args = { { (GDExtensionConstTypePtr)args... } };
47+
method(base, call_args.data(), &ret, sizeof...(Args));
48+
if (ret == nullptr) {
49+
return nullptr;
50+
}
51+
return reinterpret_cast<O *>(internal::get_object_instance_binding(ret));
52+
}
53+
4254
template <class... Args>
4355
void _call_builtin_constructor(const GDExtensionPtrConstructor constructor, GDExtensionTypePtr base, Args... args) {
4456
std::array<GDExtensionConstTypePtr, sizeof...(Args)> call_args = { { (GDExtensionConstTypePtr)args... } };

0 commit comments

Comments
 (0)