From 4fc0fb826afb606a4c934cf07656a783756a242d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gilles=20Roudi=C3=A8re?= Date: Wed, 24 Nov 2021 10:22:12 +0100 Subject: Change gdnative interface so that Godot object initialization should be triggered from the extension side --- core/extension/gdnative_interface.cpp | 30 ++++++++++++------------------ core/extension/gdnative_interface.h | 10 +++++----- core/extension/native_extension.cpp | 1 - 3 files changed, 17 insertions(+), 24 deletions(-) (limited to 'core/extension') diff --git a/core/extension/gdnative_interface.cpp b/core/extension/gdnative_interface.cpp index 19988a26cb..0c9b344a37 100644 --- a/core/extension/gdnative_interface.cpp +++ b/core/extension/gdnative_interface.cpp @@ -827,16 +827,21 @@ static GDNativeObjectPtr gdnative_global_get_singleton(const char *p_name) { return (GDNativeObjectPtr)Engine::get_singleton()->get_singleton_object(String(p_name)); } -static void *gdnative_object_get_instance_binding(GDNativeObjectPtr p_instance, void *p_token, const GDNativeInstanceBindingCallbacks *p_callbacks) { - Object *o = (Object *)p_instance; +static void *gdnative_object_get_instance_binding(GDNativeObjectPtr p_object, void *p_token, const GDNativeInstanceBindingCallbacks *p_callbacks) { + Object *o = (Object *)p_object; return o->get_instance_binding(p_token, p_callbacks); } -static void gdnative_object_set_instance_binding(GDNativeObjectPtr p_instance, void *p_token, void *p_binding, const GDNativeInstanceBindingCallbacks *p_callbacks) { - Object *o = (Object *)p_instance; +static void gdnative_object_set_instance_binding(GDNativeObjectPtr p_object, void *p_token, void *p_binding, const GDNativeInstanceBindingCallbacks *p_callbacks) { + Object *o = (Object *)p_object; o->set_instance_binding(p_token, p_binding, p_callbacks); } +static void gdnative_object_set_instance(GDNativeObjectPtr p_object, const char *p_classname, GDExtensionClassInstancePtr p_instance) { + Object *o = (Object *)p_object; + ClassDB::set_object_extension_instance(o, p_classname, p_instance); +} + static GDNativeObjectPtr gdnative_object_get_instance_from_id(GDObjectInstanceID p_instance_id) { return (GDNativeObjectPtr)ObjectDB::get_instance(ObjectID(p_instance_id)); } @@ -866,19 +871,8 @@ static GDNativeMethodBindPtr gdnative_classdb_get_method_bind(const char *p_clas return (GDNativeMethodBindPtr)mb; } -static GDNativeClassConstructor gdnative_classdb_get_constructor(const char *p_classname, GDNativeExtensionPtr *r_extension) { - ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(StringName(p_classname)); - if (class_info) { - if (r_extension) { - *r_extension = class_info->native_extension; - } - return (GDNativeClassConstructor)class_info->creation_func; - } - return nullptr; -} - -static GDNativeObjectPtr gdnative_classdb_construct_object(GDNativeClassConstructor p_constructor, GDNativeExtensionPtr p_extension) { - return (GDNativeObjectPtr)ClassDB::construct_object((Object * (*)()) p_constructor, (ObjectNativeExtension *)p_extension); +static GDNativeObjectPtr gdnative_classdb_construct_object(const char *p_classname) { + return (GDNativeObjectPtr)ClassDB::instantiate(p_classname); } static void *gdnative_classdb_get_class_tag(const char *p_classname) { @@ -1026,6 +1020,7 @@ void gdnative_setup_interface(GDNativeInterface *p_interface) { gdni.global_get_singleton = gdnative_global_get_singleton; gdni.object_get_instance_binding = gdnative_object_get_instance_binding; gdni.object_set_instance_binding = gdnative_object_set_instance_binding; + gdni.object_set_instance = gdnative_object_set_instance; gdni.object_cast_to = gdnative_object_cast_to; gdni.object_get_instance_from_id = gdnative_object_get_instance_from_id; @@ -1033,7 +1028,6 @@ void gdnative_setup_interface(GDNativeInterface *p_interface) { /* CLASSDB */ - gdni.classdb_get_constructor = gdnative_classdb_get_constructor; gdni.classdb_construct_object = gdnative_classdb_construct_object; gdni.classdb_get_method_bind = gdnative_classdb_get_method_bind; gdni.classdb_get_class_tag = gdnative_classdb_get_class_tag; diff --git a/core/extension/gdnative_interface.h b/core/extension/gdnative_interface.h index e411a9d85b..2191d99dea 100644 --- a/core/extension/gdnative_interface.h +++ b/core/extension/gdnative_interface.h @@ -211,7 +211,7 @@ typedef const char *(*GDNativeExtensionClassToString)(GDExtensionClassInstancePt typedef void (*GDNativeExtensionClassReference)(GDExtensionClassInstancePtr p_instance); typedef void (*GDNativeExtensionClassUnreference)(GDExtensionClassInstancePtr p_instance); typedef void (*GDNativeExtensionClassCallVirtual)(GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_ret); -typedef GDExtensionClassInstancePtr (*GDNativeExtensionClassCreateInstance)(void *p_userdata); +typedef GDNativeObjectPtr (*GDNativeExtensionClassCreateInstance)(void *p_userdata); typedef void (*GDNativeExtensionClassFreeInstance)(void *p_userdata, GDExtensionClassInstancePtr p_instance); typedef void (*GDNativeExtensionClassObjectInstance)(GDExtensionClassInstancePtr p_instance, GDNativeObjectPtr p_object_instance); typedef GDNativeExtensionClassCallVirtual (*GDNativeExtensionClassGetVirtual)(void *p_userdata, const char *p_name); @@ -227,7 +227,6 @@ typedef struct { GDNativeExtensionClassUnreference unreference_func; GDNativeExtensionClassCreateInstance create_instance_func; /* this one is mandatory */ GDNativeExtensionClassFreeInstance free_instance_func; /* this one is mandatory */ - GDNativeExtensionClassObjectInstance object_instance_func; /* this one is mandatory */ GDNativeExtensionClassGetVirtual get_virtual_func; void *class_userdata; } GDNativeExtensionClassCreationInfo; @@ -428,17 +427,18 @@ typedef struct { void (*object_method_bind_ptrcall)(const GDNativeMethodBindPtr p_method_bind, GDNativeObjectPtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_ret); void (*object_destroy)(GDNativeObjectPtr p_o); GDNativeObjectPtr (*global_get_singleton)(const char *p_name); + void *(*object_get_instance_binding)(GDNativeObjectPtr p_o, void *p_token, const GDNativeInstanceBindingCallbacks *p_callbacks); void (*object_set_instance_binding)(GDNativeObjectPtr p_o, void *p_token, void *p_binding, const GDNativeInstanceBindingCallbacks *p_callbacks); + void (*object_set_instance)(GDNativeObjectPtr p_o, const char *p_classname, GDExtensionClassInstancePtr p_instance); /* p_classname should be a registered extension class and should extend the p_o object's class. */ + GDNativeObjectPtr (*object_cast_to)(const GDNativeObjectPtr p_object, void *p_class_tag); GDNativeObjectPtr (*object_get_instance_from_id)(GDObjectInstanceID p_instance_id); GDObjectInstanceID (*object_get_instance_id)(const GDNativeObjectPtr p_object); /* CLASSDB */ - - GDNativeClassConstructor (*classdb_get_constructor)(const char *p_classname, GDNativeExtensionPtr *r_extension); - GDNativeObjectPtr (*classdb_construct_object)(GDNativeClassConstructor p_constructor, GDNativeExtensionPtr p_extension); + GDNativeObjectPtr (*classdb_construct_object)(const char *p_classname); /* The passed class must be a built-in godot class, or an already-registered extension class. In both case, object_set_instance should be called to fully initialize the object. */ 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); diff --git a/core/extension/native_extension.cpp b/core/extension/native_extension.cpp index a6b0a708c3..ae7620fec4 100644 --- a/core/extension/native_extension.cpp +++ b/core/extension/native_extension.cpp @@ -156,7 +156,6 @@ void NativeExtension::_register_extension_class(const GDNativeExtensionClassLibr extension->native_extension.unreference = p_extension_funcs->unreference_func; extension->native_extension.class_userdata = p_extension_funcs->class_userdata; extension->native_extension.create_instance = p_extension_funcs->create_instance_func; - extension->native_extension.set_object_instance = p_extension_funcs->object_instance_func; extension->native_extension.free_instance = p_extension_funcs->free_instance_func; extension->native_extension.get_virtual = p_extension_funcs->get_virtual_func; -- cgit v1.2.3