Fix creation (and godot-side deletion) of extended objects.

Proper initialization for godot-cpp classes with memnew.

Extension classes (i.e. the `GDCLASS` macro) behave differently from
regular wrapped classes, and requires Godot to initialize them during
object construction.

This commit update the GDCLASS macro to not create/destroy the instance
during the bindings callback, but during the extension callbacks.

When setting the object instance, the bindings instance is set to the
pointer of the extension instance so that it can later be retrieved
normally via `object_get_instance_bindings`.
pull/602/head
Fabio Alessandrelli 2021-09-21 04:01:28 +02:00 committed by Bastiaan Olij
parent cc88df05e7
commit b4632e317d
2 changed files with 15 additions and 10 deletions

View File

@ -137,6 +137,7 @@ typedef void *GDNativeStringNamePtr;
typedef void *GDNativeStringPtr; typedef void *GDNativeStringPtr;
typedef void *GDNativeObjectPtr; typedef void *GDNativeObjectPtr;
typedef void *GDNativeTypePtr; typedef void *GDNativeTypePtr;
typedef void *GDNativeExtensionPtr;
typedef void *GDNativeMethodBindPtr; typedef void *GDNativeMethodBindPtr;
typedef int64_t GDNativeInt; typedef int64_t GDNativeInt;
typedef uint8_t GDNativeBool; typedef uint8_t GDNativeBool;
@ -428,7 +429,8 @@ typedef struct {
/* CLASSDB */ /* CLASSDB */
GDNativeClassConstructor (*classdb_get_constructor)(const char *p_classname); GDNativeClassConstructor (*classdb_get_constructor)(const char *p_classname, GDNativeExtensionPtr *r_extension);
GDNativeObjectPtr (*classdb_construct_object)(GDNativeClassConstructor p_constructor, GDNativeExtensionPtr p_extension);
GDNativeMethodBindPtr (*classdb_get_method_bind)(const char *p_classname, const char *p_methodname, GDNativeInt p_hash); GDNativeMethodBindPtr (*classdb_get_method_bind)(const char *p_classname, const char *p_methodname, GDNativeInt p_hash);
void *(*classdb_get_class_tag)(const char *p_classname); void *(*classdb_get_class_tag)(const char *p_classname);

View File

@ -137,20 +137,22 @@ public:
} \ } \
\ \
static void free(void *data, GDExtensionClassInstancePtr ptr) { \ static void free(void *data, GDExtensionClassInstancePtr ptr) { \
Memory::free_static(reinterpret_cast<m_class *>(ptr)); \ if (ptr) { \
m_class *cls = reinterpret_cast<m_class *>(ptr); \
cls->~m_class(); \
::godot::Memory::free_static(cls); \
} \
} \ } \
\ \
static void set_object_instance(GDExtensionClassInstancePtr p_instance, GDNativeObjectPtr p_object_instance) { \ static void set_object_instance(GDExtensionClassInstancePtr p_instance, GDNativeObjectPtr p_object_instance) { \
reinterpret_cast<m_class *>(p_instance)->_owner = reinterpret_cast<GodotObject *>(p_object_instance); \ godot::internal::interface->object_set_instance_binding(p_object_instance, godot::internal::token, p_instance, &m_class::___binding_callbacks); \
reinterpret_cast<m_class *>(p_instance)->_owner = reinterpret_cast<godot::GodotObject *>(p_object_instance); \
} \ } \
\ \
static void *___binding_create_callback(void *p_token, void *p_instance) { \ static void *___binding_create_callback(void *p_token, void *p_instance) { \
m_class *result = new ("") m_class; \ return nullptr; \
result->_owner = reinterpret_cast<godot::GodotObject *>(p_instance); \
return result; \
} \ } \
static void ___binding_free_callback(void *p_token, void *p_instance, void *p_binding) { \ static void ___binding_free_callback(void *p_token, void *p_instance, void *p_binding) { \
Memory::free_static(reinterpret_cast<m_class *>(p_binding)); \
} \ } \
static GDNativeBool ___binding_reference_callback(void *p_token, void *p_instance, GDNativeBool p_reference) { \ static GDNativeBool ___binding_reference_callback(void *p_token, void *p_instance, GDNativeBool p_reference) { \
return true; \ return true; \
@ -162,9 +164,10 @@ public:
}; \ }; \
\ \
static m_class *_new() { \ static m_class *_new() { \
static GDNativeClassConstructor ___constructor = godot::internal::interface->classdb_get_constructor(#m_class); \ static GDNativeExtensionPtr ___extension = nullptr; \
static GDNativeClassConstructor ___constructor = godot::internal::interface->classdb_get_constructor(#m_class, &___extension); \
CHECK_CLASS_CONSTRUCTOR(___constructor, m_class); \ CHECK_CLASS_CONSTRUCTOR(___constructor, m_class); \
GDNativeObjectPtr obj = ___constructor(); \ GDNativeObjectPtr obj = godot::internal::interface->classdb_construct_object(___constructor, ___extension); \
return reinterpret_cast<m_class *>(godot::internal::interface->object_get_instance_binding(obj, godot::internal::token, &m_class::___binding_callbacks)); \ return reinterpret_cast<m_class *>(godot::internal::interface->object_get_instance_binding(obj, godot::internal::token, &m_class::___binding_callbacks)); \
} \ } \
\ \
@ -205,7 +208,7 @@ public:
___binding_reference_callback, \ ___binding_reference_callback, \
}; \ }; \
static m_class *_new() { \ static m_class *_new() { \
static GDNativeClassConstructor ___constructor = godot::internal::interface->classdb_get_constructor(#m_class); \ static GDNativeClassConstructor ___constructor = godot::internal::interface->classdb_get_constructor(#m_class, nullptr); \
CHECK_CLASS_CONSTRUCTOR(___constructor, m_class); \ CHECK_CLASS_CONSTRUCTOR(___constructor, m_class); \
GDNativeObjectPtr obj = ___constructor(); \ GDNativeObjectPtr obj = ___constructor(); \
return reinterpret_cast<m_class *>(godot::internal::interface->object_get_instance_binding(obj, godot::internal::token, &m_class::___binding_callbacks)); \ return reinterpret_cast<m_class *>(godot::internal::interface->object_get_instance_binding(obj, godot::internal::token, &m_class::___binding_callbacks)); \