diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2020-02-26 16:04:23 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2020-03-23 12:00:33 +0100 |
commit | d51c007ecc8aa6256cb95cf3992e5ac34a70fa3f (patch) | |
tree | 80f90690d29ab48a128c075b6c982cea7256a150 /src/qml/jsruntime/qv4qobjectwrapper.cpp | |
parent | c83a39fd460d1787d1c17391413543bf7ca4c330 (diff) |
Encapsulate QQmlContextData
This class is not a private detail of QQmlContext. And it is incredibly
hard to see who owns what in there. Let's add some civilization ...
We enforce refcounting for QQmlContextData across the code base, with
two exceptions:
1. QQmlContextPrivate may or may not own its QQmlContextData.
2. We may request a QQmlContextData owned by its parent QQmlContextData.
For these two cases we keep flags in QQmlContextData and when the
respective field (m_parent or m_publicContext) is reset, we release()
once.
Furthermore, QQmlContextData and QQmlGuardedContextData are moved to
their own files, in order to de-spaghettify qqmlcontext_p.h and
qqmlcontext.cpp.
When the QQmlEngine is deleted, any QQmlComponents drop their object
creators now, in order to release any context data held by those.
Before, the context data would be deleted, but the object creators would
retain the dangling pointer.
[ChangeLog][QML][Important Behavior Changes] QQmlContext::baseUrl() does
what the documentation says now: It prefers explicitly set baseUrls over
compilation unit URLs. Only if no baseUrl is set, the CU's URL is
returned. It used to prefer the CU's URL.
Change-Id: Ieeb5dcb07b45d891526191321386d5443b8f5738
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml/jsruntime/qv4qobjectwrapper.cpp')
-rw-r--r-- | src/qml/jsruntime/qv4qobjectwrapper.cpp | 51 |
1 files changed, 31 insertions, 20 deletions
diff --git a/src/qml/jsruntime/qv4qobjectwrapper.cpp b/src/qml/jsruntime/qv4qobjectwrapper.cpp index a924fa2975..4e387d0380 100644 --- a/src/qml/jsruntime/qv4qobjectwrapper.cpp +++ b/src/qml/jsruntime/qv4qobjectwrapper.cpp @@ -212,13 +212,17 @@ void QObjectWrapper::initializeBindings(ExecutionEngine *engine) engine->functionPrototype()->defineDefaultProperty(QStringLiteral("disconnect"), method_disconnect); } -QQmlPropertyData *QObjectWrapper::findProperty(ExecutionEngine *engine, QQmlContextData *qmlContext, String *name, RevisionMode revisionMode, QQmlPropertyData *local) const +QQmlPropertyData *QObjectWrapper::findProperty( + ExecutionEngine *engine, const QQmlRefPointer<QQmlContextData> &qmlContext, String *name, + RevisionMode revisionMode, QQmlPropertyData *local) const { QObject *o = d()->object(); return findProperty(engine, o, qmlContext, name, revisionMode, local); } -QQmlPropertyData *QObjectWrapper::findProperty(ExecutionEngine *engine, QObject *o, QQmlContextData *qmlContext, String *name, RevisionMode revisionMode, QQmlPropertyData *local) +QQmlPropertyData *QObjectWrapper::findProperty( + ExecutionEngine *engine, QObject *o, const QQmlRefPointer<QQmlContextData> &qmlContext, + String *name, RevisionMode revisionMode, QQmlPropertyData *local) { Q_UNUSED(revisionMode); @@ -285,13 +289,14 @@ static OptionalReturnedValue getDestroyOrToStringMethod(ExecutionEngine *v4, Str return OptionalReturnedValue(QV4::QObjectMethod::create(global, qobj, index)); } -static OptionalReturnedValue getPropertyFromImports(ExecutionEngine *v4, String *name, QQmlContextData *qmlContext, QObject *qobj, - bool *hasProperty = nullptr) +static OptionalReturnedValue getPropertyFromImports( + ExecutionEngine *v4, String *name, const QQmlRefPointer<QQmlContextData> &qmlContext, + QObject *qobj, bool *hasProperty = nullptr) { - if (!qmlContext || !qmlContext->imports) + if (!qmlContext || !qmlContext->imports()) return OptionalReturnedValue(); - QQmlTypeNameCache::Result r = qmlContext->imports->query(name); + QQmlTypeNameCache::Result r = qmlContext->imports()->query(name); if (hasProperty) *hasProperty = true; @@ -304,15 +309,17 @@ static OptionalReturnedValue getPropertyFromImports(ExecutionEngine *v4, String } else if (r.type.isValid()) { return OptionalReturnedValue(QQmlTypeWrapper::create(v4, qobj,r.type, Heap::QQmlTypeWrapper::ExcludeEnums)); } else if (r.importNamespace) { - return OptionalReturnedValue(QQmlTypeWrapper::create(v4, qobj, qmlContext->imports, r.importNamespace, - Heap::QQmlTypeWrapper::ExcludeEnums)); + return OptionalReturnedValue(QQmlTypeWrapper::create( + v4, qobj, qmlContext->imports(), r.importNamespace, + Heap::QQmlTypeWrapper::ExcludeEnums)); } Q_UNREACHABLE(); return OptionalReturnedValue(); } -ReturnedValue QObjectWrapper::getQmlProperty(QQmlContextData *qmlContext, String *name, QObjectWrapper::RevisionMode revisionMode, - bool *hasProperty, bool includeImports) const +ReturnedValue QObjectWrapper::getQmlProperty( + const QQmlRefPointer<QQmlContextData> &qmlContext, String *name, + QObjectWrapper::RevisionMode revisionMode, bool *hasProperty, bool includeImports) const { // Keep this code in sync with ::virtualResolveLookupGetter @@ -355,7 +362,10 @@ ReturnedValue QObjectWrapper::getQmlProperty(QQmlContextData *qmlContext, String return getProperty(v4, d()->object(), result); } -ReturnedValue QObjectWrapper::getQmlProperty(QV4::ExecutionEngine *engine, QQmlContextData *qmlContext, QObject *object, String *name, QObjectWrapper::RevisionMode revisionMode, bool *hasProperty, QQmlPropertyData **property) +ReturnedValue QObjectWrapper::getQmlProperty( + QV4::ExecutionEngine *engine, const QQmlRefPointer<QQmlContextData> &qmlContext, + QObject *object, String *name, QObjectWrapper::RevisionMode revisionMode, bool *hasProperty, + QQmlPropertyData **property) { if (QQmlData::wasDeleted(object)) { if (hasProperty) @@ -415,8 +425,9 @@ ReturnedValue QObjectWrapper::getQmlProperty(QV4::ExecutionEngine *engine, QQmlC } -bool QObjectWrapper::setQmlProperty(ExecutionEngine *engine, QQmlContextData *qmlContext, QObject *object, String *name, - QObjectWrapper::RevisionMode revisionMode, const Value &value) +bool QObjectWrapper::setQmlProperty( + ExecutionEngine *engine, const QQmlRefPointer<QQmlContextData> &qmlContext, QObject *object, + String *name, QObjectWrapper::RevisionMode revisionMode, const Value &value) { if (QQmlData::wasDeleted(object)) return false; @@ -462,7 +473,7 @@ void QObjectWrapper::setProperty(ExecutionEngine *engine, QObject *object, QQmlP } } else { // binding assignment. - QQmlContextData *callingQmlContext = scope.engine->callingQmlContext(); + QQmlRefPointer<QQmlContextData> callingQmlContext = scope.engine->callingQmlContext(); QV4::Scoped<QQmlBindingFunction> bindingFunction(scope, (const Value &)f); @@ -561,7 +572,7 @@ void QObjectWrapper::setProperty(ExecutionEngine *engine, QObject *object, QQmlP else v = scope.engine->toVariant(value, property->propType()); - QQmlContextData *callingQmlContext = scope.engine->callingQmlContext(); + QQmlRefPointer<QQmlContextData> callingQmlContext = scope.engine->callingQmlContext(); if (!QQmlPropertyPrivate::write(object, *property, v, callingQmlContext)) { const char *valueType = (v.userType() == QMetaType::UnknownType) ? "an unknown type" @@ -702,7 +713,7 @@ QV4::ReturnedValue QObjectWrapper::virtualGet(const Managed *m, PropertyKey id, const QObjectWrapper *that = static_cast<const QObjectWrapper*>(m); Scope scope(that); ScopedString n(scope, id.asStringOrSymbol()); - QQmlContextData *qmlContext = that->engine()->callingQmlContext(); + QQmlRefPointer<QQmlContextData> qmlContext = that->engine()->callingQmlContext(); return that->getQmlProperty(qmlContext, n, IgnoreRevision, hasProperty, /*includeImports*/ true); } @@ -718,7 +729,7 @@ bool QObjectWrapper::virtualPut(Managed *m, PropertyKey id, const Value &value, if (scope.engine->hasException || QQmlData::wasDeleted(that->d()->object())) return false; - QQmlContextData *qmlContext = scope.engine->callingQmlContext(); + QQmlRefPointer<QQmlContextData> qmlContext = scope.engine->callingQmlContext(); if (!setQmlProperty(scope.engine, qmlContext, that->d()->object(), name, QV4::QObjectWrapper::IgnoreRevision, value)) { QQmlData *ddata = QQmlData::get(that->d()->object()); // Types created by QML are not extensible at run-time, but for other QObjects we can store them @@ -744,7 +755,7 @@ PropertyAttributes QObjectWrapper::virtualGetOwnProperty(const Managed *m, Prope if (!QQmlData::wasDeleted(thatObject)) { Scope scope(m); ScopedString n(scope, id.asStringOrSymbol()); - QQmlContextData *qmlContext = scope.engine->callingQmlContext(); + QQmlRefPointer<QQmlContextData> qmlContext = scope.engine->callingQmlContext(); QQmlPropertyData local; if (that->findProperty(scope.engine, qmlContext, n, IgnoreRevision, &local) || n->equals(scope.engine->id_destroy()) || n->equals(scope.engine->id_toString())) { @@ -848,7 +859,7 @@ ReturnedValue QObjectWrapper::virtualResolveLookupGetter(const Object *object, E const QObjectWrapper *This = static_cast<const QObjectWrapper *>(object); ScopedString name(scope, id.asStringOrSymbol()); - QQmlContextData *qmlContext = engine->callingQmlContext(); + QQmlRefPointer<QQmlContextData> qmlContext = engine->callingQmlContext(); QObject * const qobj = This->d()->object(); @@ -1168,7 +1179,7 @@ void QObjectWrapper::destroyObject(bool lastCall) if (ddata) { if (!h->object()->parent() && !ddata->indestructible) { if (ddata && ddata->ownContext) { - Q_ASSERT(ddata->ownContext == ddata->context); + Q_ASSERT(ddata->ownContext.data() == ddata->context); ddata->ownContext->emitDestruction(); ddata->ownContext = nullptr; ddata->context = nullptr; |