summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <remi@verschelde.fr>2022-12-14 12:36:34 +0100
committerGitHub <noreply@github.com>2022-12-14 12:36:34 +0100
commiteba33c67b49905973ee7c81a709046415a41e205 (patch)
treef6d565ea6ce66cfc7a0fea8bae7d31b07781a858
parent6b381d1d2d2338c6df2e3a5e3ad114d9ece15392 (diff)
parent7502e1dc0da15334f88911a802fa6ba919b6c83c (diff)
Merge pull request #69902 from BastiaanOlij/improve_gdextension_virtual_ref
Improve logic around using Ref<T> with GDExtension virtual functions
-rw-r--r--core/extension/gdextension_interface.cpp22
-rw-r--r--core/extension/gdextension_interface.h7
-rw-r--r--core/object/ref_counted.h3
3 files changed, 32 insertions, 0 deletions
diff --git a/core/extension/gdextension_interface.cpp b/core/extension/gdextension_interface.cpp
index 197e9db1e7..94755bc4d4 100644
--- a/core/extension/gdextension_interface.cpp
+++ b/core/extension/gdextension_interface.cpp
@@ -879,6 +879,23 @@ static GDObjectInstanceID gdextension_object_get_instance_id(GDExtensionConstObj
return (GDObjectInstanceID)o->get_instance_id();
}
+static GDExtensionObjectPtr gdextension_ref_get_object(GDExtensionConstRefPtr p_ref) {
+ const Ref<RefCounted> *ref = (const Ref<RefCounted> *)p_ref;
+ if (ref == nullptr || ref->is_null()) {
+ return (GDExtensionObjectPtr) nullptr;
+ } else {
+ return (GDExtensionObjectPtr)ref->ptr();
+ }
+}
+
+static void gdextension_ref_set_object(GDExtensionRefPtr p_ref, GDExtensionObjectPtr p_object) {
+ Ref<RefCounted> *ref = (Ref<RefCounted> *)p_ref;
+ ERR_FAIL_NULL(ref);
+
+ Object *o = (RefCounted *)p_object;
+ ref->reference_ptr(o);
+}
+
static GDExtensionScriptInstancePtr gdextension_script_instance_create(const GDExtensionScriptInstanceInfo *p_info, GDExtensionScriptInstanceDataPtr p_instance_data) {
ScriptInstanceExtension *script_instance_extension = memnew(ScriptInstanceExtension);
script_instance_extension->instance = p_instance_data;
@@ -1057,6 +1074,11 @@ void gdextension_setup_interface(GDExtensionInterface *p_interface) {
gde_interface.object_get_instance_from_id = gdextension_object_get_instance_from_id;
gde_interface.object_get_instance_id = gdextension_object_get_instance_id;
+ /* REFERENCE */
+
+ gde_interface.ref_get_object = gdextension_ref_get_object;
+ gde_interface.ref_set_object = gdextension_ref_set_object;
+
/* SCRIPT INSTANCE */
gde_interface.script_instance_create = gdextension_script_instance_create;
diff --git a/core/extension/gdextension_interface.h b/core/extension/gdextension_interface.h
index b59a6b5ca4..66fca76aa8 100644
--- a/core/extension/gdextension_interface.h
+++ b/core/extension/gdextension_interface.h
@@ -154,6 +154,8 @@ typedef const void *GDExtensionMethodBindPtr;
typedef int64_t GDExtensionInt;
typedef uint8_t GDExtensionBool;
typedef uint64_t GDObjectInstanceID;
+typedef void *GDExtensionRefPtr;
+typedef const void *GDExtensionConstRefPtr;
/* VARIANT DATA I/O */
@@ -551,6 +553,11 @@ typedef struct {
GDExtensionObjectPtr (*object_get_instance_from_id)(GDObjectInstanceID p_instance_id);
GDObjectInstanceID (*object_get_instance_id)(GDExtensionConstObjectPtr p_object);
+ /* REFERENCE */
+
+ GDExtensionObjectPtr (*ref_get_object)(GDExtensionConstRefPtr p_ref);
+ void (*ref_set_object)(GDExtensionRefPtr p_ref, GDExtensionObjectPtr p_object);
+
/* SCRIPT INSTANCE */
GDExtensionScriptInstancePtr (*script_instance_create)(const GDExtensionScriptInstanceInfo *p_info, GDExtensionScriptInstanceDataPtr p_instance_data);
diff --git a/core/object/ref_counted.h b/core/object/ref_counted.h
index 71790fb825..411c589739 100644
--- a/core/object/ref_counted.h
+++ b/core/object/ref_counted.h
@@ -253,12 +253,14 @@ public:
template <class T>
struct PtrToArg<Ref<T>> {
_FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
+ // p_ptr points to a RefCounted object
return Ref<T>(const_cast<T *>(reinterpret_cast<const T *>(p_ptr)));
}
typedef Ref<T> EncodeT;
_FORCE_INLINE_ static void encode(Ref<T> p_val, const void *p_ptr) {
+ // p_ptr points to an EncodeT object which is a Ref<T> object.
*(const_cast<Ref<RefCounted> *>(reinterpret_cast<const Ref<RefCounted> *>(p_ptr))) = p_val;
}
};
@@ -268,6 +270,7 @@ struct PtrToArg<const Ref<T> &> {
typedef Ref<T> EncodeT;
_FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
+ // p_ptr points to a RefCounted object
return Ref<T>((T *)p_ptr);
}
};