From cd16d2d965cf2b7f8a11b2e1d0f33bc783bd54eb Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Wed, 24 Jan 2024 08:44:10 +0100 Subject: QtQml: Clean up QQmlTypeWrapper We can enforce that it always receives some kind of type and that the type doesn't disappear before it's deleted. We also don't have to cast a Heap::Base to get its engine. Change-Id: I7f95df64c254d6f4482a02782666e7cccea11ee8 Reviewed-by: Fabian Kosmale --- src/qml/qml/qqmltypewrapper.cpp | 41 ++++++++++++++++++++++++++++------------- src/qml/qml/qqmltypewrapper_p.h | 4 +++- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/qml/qml/qqmltypewrapper.cpp b/src/qml/qml/qqmltypewrapper.cpp index 75f3a19c2f..412c1d6419 100644 --- a/src/qml/qml/qqmltypewrapper.cpp +++ b/src/qml/qml/qqmltypewrapper.cpp @@ -23,15 +23,32 @@ using namespace QV4; DEFINE_OBJECT_VTABLE(QQmlTypeWrapper); DEFINE_OBJECT_VTABLE(QQmlScopedEnumWrapper); -void Heap::QQmlTypeWrapper::init() + +void Heap::QQmlTypeWrapper::init(TypeNameMode m, QObject *o, const QQmlTypePrivate *type) +{ + Q_ASSERT(type); + Object::init(); + mode = m; + object.init(o); + typePrivate = type; + QQmlType::refHandle(typePrivate); +} + +void Heap::QQmlTypeWrapper::init( + TypeNameMode m, QObject *o, QQmlTypeNameCache *type, const QQmlImportRef *import) { + Q_ASSERT(type); Object::init(); - mode = IncludeEnums; - object.init(); + mode = m; + object.init(o); + typeNamespace = type; + typeNamespace->addref(); + importNamespace = import; } void Heap::QQmlTypeWrapper::destroy() { + Q_ASSERT(typePrivate || typeNamespace); QQmlType::derefHandle(typePrivate); typePrivate = nullptr; if (typeNamespace) @@ -127,25 +144,23 @@ ReturnedValue QQmlTypeWrapper::create(QV4::ExecutionEngine *engine, QObject *o, Q_ASSERT(t.isValid()); Scope scope(engine); - Scoped w(scope, engine->memoryManager->allocate()); - w->d()->mode = mode; w->d()->object = o; - w->d()->typePrivate = t.priv(); - QQmlType::refHandle(w->d()->typePrivate); + Scoped w(scope, engine->memoryManager->allocate( + mode, o, t.priv())); return w.asReturnedValue(); } // Returns a type wrapper for importNamespace (of t) on o. This allows nested resolution of a type in a // namespace. -ReturnedValue QQmlTypeWrapper::create(QV4::ExecutionEngine *engine, QObject *o, const QQmlRefPointer &t, const QQmlImportRef *importNamespace, - Heap::QQmlTypeWrapper::TypeNameMode mode) +ReturnedValue QQmlTypeWrapper::create( + QV4::ExecutionEngine *engine, QObject *o, const QQmlRefPointer &t, + const QQmlImportRef *importNamespace, Heap::QQmlTypeWrapper::TypeNameMode mode) { Q_ASSERT(t); Q_ASSERT(importNamespace); Scope scope(engine); - Scoped w(scope, engine->memoryManager->allocate()); - w->d()->mode = mode; w->d()->object = o; w->d()->typeNamespace = t.data(); w->d()->importNamespace = importNamespace; - t->addref(); + Scoped w(scope, engine->memoryManager->allocate( + mode, o, t.data(), importNamespace)); return w.asReturnedValue(); } @@ -178,7 +193,7 @@ ReturnedValue QQmlTypeWrapper::virtualGet(const Managed *m, PropertyKey id, cons if (!id.isString()) return Object::virtualGet(m, id, receiver, hasProperty); - QV4::ExecutionEngine *v4 = static_cast(m)->engine(); + QV4::ExecutionEngine *v4 = m->engine(); QV4::Scope scope(v4); ScopedString name(scope, id.asStringOrSymbol()); diff --git a/src/qml/qml/qqmltypewrapper_p.h b/src/qml/qml/qqmltypewrapper_p.h index 63e170a3d5..717efaf20e 100644 --- a/src/qml/qml/qqmltypewrapper_p.h +++ b/src/qml/qml/qqmltypewrapper_p.h @@ -38,7 +38,9 @@ struct QQmlTypeWrapper : Object { ExcludeEnums }; - void init(); + void init(TypeNameMode m, QObject *o, const QQmlTypePrivate *type); + void init(TypeNameMode m, QObject *o, QQmlTypeNameCache *type, const QQmlImportRef *import); + void destroy(); TypeNameMode mode; QV4QPointer object; -- cgit v1.2.3