diff options
Diffstat (limited to 'core/object')
-rw-r--r-- | core/object/class_db.cpp | 48 | ||||
-rw-r--r-- | core/object/class_db.h | 3 | ||||
-rw-r--r-- | core/object/make_virtuals.py | 11 | ||||
-rw-r--r-- | core/object/object.cpp | 2 | ||||
-rw-r--r-- | core/object/object.h | 1 |
5 files changed, 32 insertions, 33 deletions
diff --git a/core/object/class_db.cpp b/core/object/class_db.cpp index 4b3c8b123f..ac628acf81 100644 --- a/core/object/class_db.cpp +++ b/core/object/class_db.cpp @@ -497,22 +497,6 @@ void ClassDB::add_compatibility_class(const StringName &p_class, const StringNam compat_classes[p_class] = p_fallback; } -thread_local bool initializing_with_extension = false; -thread_local ObjectNativeExtension *initializing_extension = nullptr; -thread_local GDExtensionClassInstancePtr initializing_extension_instance = nullptr; - -void ClassDB::instance_get_native_extension_data(ObjectNativeExtension **r_extension, GDExtensionClassInstancePtr *r_extension_instance, Object *p_base) { - if (initializing_with_extension) { - *r_extension = initializing_extension; - *r_extension_instance = initializing_extension_instance; - initializing_with_extension = false; - initializing_extension->set_object_instance(*r_extension_instance, p_base); - } else { - *r_extension = nullptr; - *r_extension_instance = nullptr; - } -} - Object *ClassDB::instantiate(const StringName &p_class) { ClassInfo *ti; { @@ -533,21 +517,31 @@ Object *ClassDB::instantiate(const StringName &p_class) { return nullptr; } #endif - if (ti->native_extension) { - initializing_with_extension = true; - initializing_extension = ti->native_extension; - initializing_extension_instance = ti->native_extension->create_instance(ti->native_extension->class_userdata); + if (ti->native_extension && ti->native_extension->create_instance) { + return (Object *)ti->native_extension->create_instance(ti->native_extension->class_userdata); + } else { + return ti->creation_func(); } - return ti->creation_func(); } -Object *ClassDB::construct_object(Object *(*p_create_func)(), ObjectNativeExtension *p_extension) { - if (p_extension) { - initializing_with_extension = true; - initializing_extension = p_extension; - initializing_extension_instance = p_extension->create_instance(p_extension->class_userdata); +void ClassDB::set_object_extension_instance(Object *p_object, const StringName &p_class, GDExtensionClassInstancePtr p_instance) { + ERR_FAIL_COND(!p_object); + ClassInfo *ti; + { + OBJTYPE_RLOCK; + ti = classes.getptr(p_class); + if (!ti || ti->disabled || !ti->creation_func || (ti->native_extension && !ti->native_extension->create_instance)) { + if (compat_classes.has(p_class)) { + ti = classes.getptr(compat_classes[p_class]); + } + } + ERR_FAIL_COND_MSG(!ti, "Cannot get class '" + String(p_class) + "'."); + ERR_FAIL_COND_MSG(ti->disabled, "Class '" + String(p_class) + "' is disabled."); + ERR_FAIL_COND_MSG(!ti->native_extension, "Class '" + String(p_class) + "' has no native extension."); } - return p_create_func(); + + p_object->_extension = ti->native_extension; + p_object->_extension_instance = p_instance; } bool ClassDB::can_instantiate(const StringName &p_class) { diff --git a/core/object/class_db.h b/core/object/class_db.h index dae75ba564..ca9c1def29 100644 --- a/core/object/class_db.h +++ b/core/object/class_db.h @@ -211,8 +211,7 @@ public: static bool is_parent_class(const StringName &p_class, const StringName &p_inherits); static bool can_instantiate(const StringName &p_class); static Object *instantiate(const StringName &p_class); - static Object *construct_object(Object *(*p_create_func)(), ObjectNativeExtension *p_extension); - static void instance_get_native_extension_data(ObjectNativeExtension **r_extension, GDExtensionClassInstancePtr *r_extension_instance, Object *p_base); + static void set_object_extension_instance(Object *p_object, const StringName &p_class, GDExtensionClassInstancePtr p_instance); static APIType get_api_type(const StringName &p_class); diff --git a/core/object/make_virtuals.py b/core/object/make_virtuals.py index 86c2891e5d..e961745d96 100644 --- a/core/object/make_virtuals.py +++ b/core/object/make_virtuals.py @@ -1,7 +1,8 @@ proto = """ #define GDVIRTUAL$VER($RET m_name $ARG) \\ StringName _gdvirtual_##m_name##_sn = #m_name;\\ -GDNativeExtensionClassCallVirtual _gdvirtual_##m_name = (_get_extension() && _get_extension()->get_virtual) ? _get_extension()->get_virtual(_get_extension()->class_userdata, #m_name) : (GDNativeExtensionClassCallVirtual) nullptr;\\ +mutable bool _gdvirtual_##m_name##_initialized = false;\\ +mutable GDNativeExtensionClassCallVirtual _gdvirtual_##m_name = nullptr;\\ _FORCE_INLINE_ bool _gdvirtual_##m_name##_call($CALLARGS) $CONST { \\ ScriptInstance *script_instance = ((Object*)(this))->get_script_instance();\\ if (script_instance) {\\ @@ -13,6 +14,10 @@ _FORCE_INLINE_ bool _gdvirtual_##m_name##_call($CALLARGS) $CONST { \\ return true;\\ } \\ }\\ + if (unlikely(_get_extension() && !_gdvirtual_##m_name##_initialized)) {\\ + _gdvirtual_##m_name = (_get_extension() && _get_extension()->get_virtual) ? _get_extension()->get_virtual(_get_extension()->class_userdata, #m_name) : (GDNativeExtensionClassCallVirtual) nullptr;\\ + _gdvirtual_##m_name##_initialized = true;\\ + }\\ if (_gdvirtual_##m_name) {\\ $CALLPTRARGS\\ $CALLPTRRETDEF\\ @@ -28,6 +33,10 @@ _FORCE_INLINE_ bool _gdvirtual_##m_name##_overridden() const { \\ if (script_instance) {\\ return script_instance->has_method(_gdvirtual_##m_name##_sn);\\ }\\ + if (unlikely(_get_extension() && !_gdvirtual_##m_name##_initialized)) {\\ + _gdvirtual_##m_name = (_get_extension() && _get_extension()->get_virtual) ? _get_extension()->get_virtual(_get_extension()->class_userdata, #m_name) : (GDNativeExtensionClassCallVirtual) nullptr;\\ + _gdvirtual_##m_name##_initialized = true;\\ + }\\ if (_gdvirtual_##m_name) {\\ return true;\\ }\\ diff --git a/core/object/object.cpp b/core/object/object.cpp index 90bd697d37..3942dddf52 100644 --- a/core/object/object.cpp +++ b/core/object/object.cpp @@ -1838,8 +1838,6 @@ void Object::_construct_object(bool p_reference) { type_is_reference = p_reference; _instance_id = ObjectDB::add_instance(this); - ClassDB::instance_get_native_extension_data(&_extension, &_extension_instance, this); - #ifdef DEBUG_ENABLED _lock_index.init(1); #endif diff --git a/core/object/object.h b/core/object/object.h index 218bc26dec..795bbf27e8 100644 --- a/core/object/object.h +++ b/core/object/object.h @@ -284,7 +284,6 @@ struct ObjectNativeExtension { GDNativeExtensionClassCreateInstance create_instance; GDNativeExtensionClassFreeInstance free_instance; - GDNativeExtensionClassObjectInstance set_object_instance; GDNativeExtensionClassGetVirtual get_virtual; }; |