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 | |
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')
-rw-r--r-- | src/qml/jsruntime/qv4engine.cpp | 4 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4engine_p.h | 2 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4include.cpp | 4 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4include_p.h | 4 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4qmlcontext.cpp | 114 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4qmlcontext_p.h | 18 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4qobjectwrapper.cpp | 51 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4qobjectwrapper_p.h | 26 |
8 files changed, 131 insertions, 92 deletions
diff --git a/src/qml/jsruntime/qv4engine.cpp b/src/qml/jsruntime/qv4engine.cpp index d4725ce7c2..5edf2fdea7 100644 --- a/src/qml/jsruntime/qv4engine.cpp +++ b/src/qml/jsruntime/qv4engine.cpp @@ -1198,13 +1198,13 @@ QObject *ExecutionEngine::qmlScopeObject() const return ctx->qml()->scopeObject; } -QQmlContextData *ExecutionEngine::callingQmlContext() const +QQmlRefPointer<QQmlContextData> ExecutionEngine::callingQmlContext() const { Heap::QmlContext *ctx = qmlContext(); if (!ctx) return nullptr; - return ctx->qml()->context->contextData(); + return ctx->qml()->context; } StackTrace ExecutionEngine::stackTrace(int frameLimit) const diff --git a/src/qml/jsruntime/qv4engine_p.h b/src/qml/jsruntime/qv4engine_p.h index 21971c1c23..9d734a16a0 100644 --- a/src/qml/jsruntime/qv4engine_p.h +++ b/src/qml/jsruntime/qv4engine_p.h @@ -609,7 +609,7 @@ public: Heap::QmlContext *qmlContext() const; QObject *qmlScopeObject() const; - QQmlContextData *callingQmlContext() const; + QQmlRefPointer<QQmlContextData> callingQmlContext() const; StackTrace stackTrace(int frameLimit = -1) const; diff --git a/src/qml/jsruntime/qv4include.cpp b/src/qml/jsruntime/qv4include.cpp index 745ffab0cd..17512cf4ff 100644 --- a/src/qml/jsruntime/qv4include.cpp +++ b/src/qml/jsruntime/qv4include.cpp @@ -208,9 +208,9 @@ QV4::ReturnedValue QV4Include::method_include(const QV4::FunctionObject *b, cons if (!argc) RETURN_UNDEFINED(); - QQmlContextData *context = scope.engine->callingQmlContext(); + QQmlRefPointer<QQmlContextData> context = scope.engine->callingQmlContext(); - if ((!context || !context->isJSContext) && scope.engine->qmlEngine()) + if ((!context || !context->isJSContext()) && scope.engine->qmlEngine()) RETURN_RESULT(scope.engine->throwError(QString::fromUtf8("Qt.include(): Can only be called from JavaScript files"))); QV4::ScopedValue callbackFunction(scope, QV4::Value::undefinedValue()); diff --git a/src/qml/jsruntime/qv4include_p.h b/src/qml/jsruntime/qv4include_p.h index 70ccfbf223..9d0a17a5cc 100644 --- a/src/qml/jsruntime/qv4include_p.h +++ b/src/qml/jsruntime/qv4include_p.h @@ -53,11 +53,11 @@ #include <QtCore/qobject.h> #include <QtCore/qurl.h> - -#include <private/qqmlcontext_p.h> +#include <QtCore/qpointer.h> #include <private/qv4value_p.h> #include <private/qv4context_p.h> +#include <private/qv4persistent_p.h> QT_BEGIN_NAMESPACE diff --git a/src/qml/jsruntime/qv4qmlcontext.cpp b/src/qml/jsruntime/qv4qmlcontext.cpp index 4538900d21..9863edead0 100644 --- a/src/qml/jsruntime/qv4qmlcontext.cpp +++ b/src/qml/jsruntime/qv4qmlcontext.cpp @@ -64,23 +64,25 @@ using namespace QV4; DEFINE_OBJECT_VTABLE(QQmlContextWrapper); DEFINE_MANAGED_VTABLE(QmlContext); -void Heap::QQmlContextWrapper::init(QQmlContextData *context, QObject *scopeObject) +void Heap::QQmlContextWrapper::init(QQmlRefPointer<QQmlContextData> context, QObject *scopeObject) { Object::init(); - this->context = new QQmlContextDataRef(context); + this->context = context.take(); this->scopeObject.init(scopeObject); } void Heap::QQmlContextWrapper::destroy() { - delete context; + context->release(); + context = nullptr; scopeObject.destroy(); Object::destroy(); } -static OptionalReturnedValue searchContextProperties(QV4::ExecutionEngine *v4, QQmlContextData *context, String *name, - bool *hasProperty, Value *base, QV4::Lookup *lookup, - QV4::Lookup *originalLookup, QQmlEnginePrivate *ep) +static OptionalReturnedValue searchContextProperties( + QV4::ExecutionEngine *v4, const QQmlRefPointer<QQmlContextData> &context, String *name, + bool *hasProperty, Value *base, QV4::Lookup *lookup, QV4::Lookup *originalLookup, + QQmlEnginePrivate *ep) { const QV4::IdentifierHash &properties = context->propertyNames(); if (properties.count() == 0) @@ -91,7 +93,7 @@ static OptionalReturnedValue searchContextProperties(QV4::ExecutionEngine *v4, Q if (propertyIdx == -1) return OptionalReturnedValue(); - if (propertyIdx < context->idValueCount) { + if (propertyIdx < context->numIdValues()) { if (hasProperty) *hasProperty = true; @@ -104,16 +106,16 @@ static OptionalReturnedValue searchContextProperties(QV4::ExecutionEngine *v4, Q } if (ep->propertyCapture) - ep->propertyCapture->captureProperty(&context->idValues[propertyIdx].bindings); - return OptionalReturnedValue(QV4::QObjectWrapper::wrap(v4, context->idValues[propertyIdx])); + ep->propertyCapture->captureProperty(context->idValueBindings(propertyIdx)); + return OptionalReturnedValue(QV4::QObjectWrapper::wrap(v4, context->idValue(propertyIdx))); } QQmlContextPrivate *cp = context->asQQmlContextPrivate(); if (ep->propertyCapture) - ep->propertyCapture->captureProperty(context->asQQmlContext(), -1, propertyIdx + cp->notifyIndex); + ep->propertyCapture->captureProperty(context->asQQmlContext(), -1, propertyIdx + cp->notifyIndex()); - const QVariant &value = cp->propertyValues.at(propertyIdx); + const QVariant &value = cp->propertyValue(propertyIdx); if (hasProperty) *hasProperty = true; if (value.userType() == qMetaTypeId<QList<QObject*> >()) { @@ -122,7 +124,7 @@ static OptionalReturnedValue searchContextProperties(QV4::ExecutionEngine *v4, Q QQmlContextPrivate::context_at); return OptionalReturnedValue(QmlListWrapper::create(v4, prop, qMetaTypeId<QQmlListProperty<QObject> >())); } - return OptionalReturnedValue(v4->fromVariant(cp->propertyValues.at(propertyIdx))); + return OptionalReturnedValue(v4->fromVariant(cp->propertyValue(propertyIdx))); } ReturnedValue QQmlContextWrapper::getPropertyAndBase(const QQmlContextWrapper *resource, PropertyKey id, const Value *receiver, bool *hasProperty, Value *base, Lookup *lookup) @@ -133,7 +135,7 @@ ReturnedValue QQmlContextWrapper::getPropertyAndBase(const QQmlContextWrapper *r QV4::ExecutionEngine *v4 = resource->engine(); QV4::Scope scope(v4); - if (v4->callingQmlContext() != *resource->d()->context) { + if (v4->callingQmlContext().data() != resource->d()->context) { if (resource->d()->module) { Scoped<Module> module(scope, resource->d()->module); bool hasProp = false; @@ -158,8 +160,8 @@ ReturnedValue QQmlContextWrapper::getPropertyAndBase(const QQmlContextWrapper *r // It's possible we could delay the calculation of the "actual" context (in the case // of sub contexts) until it is definitely needed. - QQmlContextData *context = resource->getContext(); - QQmlContextData *expressionContext = context; + QQmlRefPointer<QQmlContextData> context = resource->getContext(); + QQmlRefPointer<QQmlContextData> expressionContext = context; if (!context) { if (hasProperty) @@ -211,9 +213,9 @@ ReturnedValue QQmlContextWrapper::getPropertyAndBase(const QQmlContextWrapper *r return result->asReturnedValue(); } - if (context->imports && name->startsWithUpper()) { + if (context->imports() && name->startsWithUpper()) { // Search for attached properties, enums and imported scripts - QQmlTypeNameCache::Result r = context->imports->query(name, QQmlImport::AllowRecursion); + QQmlTypeNameCache::Result r = context->imports()->query(name, QQmlImport::AllowRecursion); if (r.isValid()) { if (hasProperty) @@ -224,7 +226,7 @@ ReturnedValue QQmlContextWrapper::getPropertyAndBase(const QQmlContextWrapper *r lookup->qmlContextPropertyGetter = QQmlContextWrapper::lookupScript; return lookup->qmlContextPropertyGetter(lookup, v4, base); } - QV4::ScopedObject scripts(scope, context->importedScripts.valueRef()); + QV4::ScopedObject scripts(scope, context->importedScripts().valueRef()); if (scripts) return scripts->get(r.scriptIndex); return QV4::Encode::null(); @@ -250,7 +252,7 @@ ReturnedValue QQmlContextWrapper::getPropertyAndBase(const QQmlContextWrapper *r } result = QQmlTypeWrapper::create(v4, scopeObject, r.type); } else if (r.importNamespace) { - result = QQmlTypeWrapper::create(v4, scopeObject, context->imports, r.importNamespace); + result = QQmlTypeWrapper::create(v4, scopeObject, context->imports(), r.importNamespace); } if (lookup) { lookup->qmlTypeLookup.qmlTypeWrapper = static_cast<Heap::Object*>(result->heapObject()); @@ -268,7 +270,7 @@ ReturnedValue QQmlContextWrapper::getPropertyAndBase(const QQmlContextWrapper *r decltype(lookup->qmlContextPropertyGetter) contextGetterFunction = QQmlContextWrapper::lookupContextObjectProperty; // minor optimization so we don't potentially try two property lookups on the same object - if (scopeObject == context->contextObject) { + if (scopeObject == context->contextObject()) { scopeObject = nullptr; contextGetterFunction = QQmlContextWrapper::lookupScopeObjectProperty; } @@ -310,22 +312,24 @@ ReturnedValue QQmlContextWrapper::getPropertyAndBase(const QQmlContextWrapper *r // Search context object - if (context->contextObject) { + if (QObject *contextObject = context->contextObject()) { bool hasProp = false; QQmlPropertyData *propertyData = nullptr; - result = QV4::QObjectWrapper::getQmlProperty(v4, context, context->contextObject, - name, QV4::QObjectWrapper::CheckRevision, &hasProp, &propertyData); + result = QV4::QObjectWrapper::getQmlProperty(v4, context, contextObject, + name, QV4::QObjectWrapper::CheckRevision, + &hasProp, &propertyData); if (hasProp) { if (hasProperty) *hasProperty = true; if (base) - *base = QV4::QObjectWrapper::wrap(v4, context->contextObject); + *base = QV4::QObjectWrapper::wrap(v4, contextObject); if (propertyData) { if (lookup) { - QQmlData *ddata = QQmlData::get(context->contextObject, false); + QQmlData *ddata = QQmlData::get(contextObject, false); if (ddata && ddata->propertyCache) { - ScopedValue val(scope, base ? *base : Value::fromReturnedValue(QV4::QObjectWrapper::wrap(v4, context->contextObject))); + ScopedValue val(scope, base ? *base + : Value::fromReturnedValue(QV4::QObjectWrapper::wrap(v4, contextObject))); const QObjectWrapper *That = static_cast<const QObjectWrapper *>(val->objectValue()); lookup->qobjectLookup.ic = That->internalClass(); lookup->qobjectLookup.propertyCache = ddata->propertyCache; @@ -342,7 +346,7 @@ ReturnedValue QQmlContextWrapper::getPropertyAndBase(const QQmlContextWrapper *r } } - context = context->parent; + context = context->parent(); // As the hierarchy of contexts is not stable, we can't do accelerated lookups beyond // the immediate QML context (of the .qml file). @@ -370,7 +374,7 @@ ReturnedValue QQmlContextWrapper::getPropertyAndBase(const QQmlContextWrapper *r return result->asReturnedValue(); } - expressionContext->unresolvedNames = true; + expressionContext->setUnresolvedNames(true); return Encode::undefined(); } @@ -402,8 +406,8 @@ bool QQmlContextWrapper::virtualPut(Managed *m, PropertyKey id, const Value &val // It's possible we could delay the calculation of the "actual" context (in the case // of sub contexts) until it is definitely needed. - QQmlContextData *context = wrapper->getContext(); - QQmlContextData *expressionContext = context; + QQmlRefPointer<QQmlContextData> context = wrapper->getContext(); + QQmlRefPointer<QQmlContextData> expressionContext = context; if (!context) return false; @@ -419,7 +423,7 @@ bool QQmlContextWrapper::virtualPut(Managed *m, PropertyKey id, const Value &val if (properties.count()) { const int propertyIndex = properties.value(name); if (propertyIndex != -1) { - if (propertyIndex < context->idValueCount) { + if (propertyIndex < context->numIdValues()) { v4->throwError(QLatin1String("left-hand side of assignment operator is not an lvalue")); return false; } @@ -434,14 +438,15 @@ bool QQmlContextWrapper::virtualPut(Managed *m, PropertyKey id, const Value &val scopeObject = nullptr; // Search context object - if (context->contextObject && - QV4::QObjectWrapper::setQmlProperty(v4, context, context->contextObject, name, QV4::QObjectWrapper::CheckRevision, value)) + if (context->contextObject() && + QV4::QObjectWrapper::setQmlProperty(v4, context, context->contextObject(), name, + QV4::QObjectWrapper::CheckRevision, value)) return true; - context = context->parent; + context = context->parent(); } - expressionContext->unresolvedNames = true; + expressionContext->setUnresolvedNames(true); QString error = QLatin1String("Invalid write to global property \"") + name->toQString() + QLatin1Char('"'); @@ -502,11 +507,11 @@ ReturnedValue QQmlContextWrapper::lookupScript(Lookup *l, ExecutionEngine *engin if (!qmlContext) return QV4::Encode::null(); - QQmlContextData *context = qmlContext->qmlContext(); + QQmlRefPointer<QQmlContextData> context = qmlContext->qmlContext(); if (!context) return QV4::Encode::null(); - QV4::ScopedObject scripts(scope, context->importedScripts.valueRef()); + QV4::ScopedObject scripts(scope, context->importedScripts().valueRef()); if (!scripts) return QV4::Encode::null(); return scripts->get(l->qmlContextScriptLookup.scriptIndex); @@ -527,7 +532,7 @@ ReturnedValue QQmlContextWrapper::lookupIdObject(Lookup *l, ExecutionEngine *eng if (!qmlContext) return QV4::Encode::null(); - QQmlContextData *context = qmlContext->qmlContext(); + QQmlRefPointer<QQmlContextData> context = qmlContext->qmlContext(); if (!context) return QV4::Encode::null(); @@ -535,9 +540,9 @@ ReturnedValue QQmlContextWrapper::lookupIdObject(Lookup *l, ExecutionEngine *eng const int objectId = l->qmlContextIdObjectLookup.objectId; if (qmlEngine->propertyCapture) - qmlEngine->propertyCapture->captureProperty(&context->idValues[objectId].bindings); + qmlEngine->propertyCapture->captureProperty(context->idValueBindings(objectId)); - return QV4::QObjectWrapper::wrap(engine, context->idValues[objectId]); + return QV4::QObjectWrapper::wrap(engine, context->idValue(objectId)); } ReturnedValue QQmlContextWrapper::lookupScopeObjectProperty(Lookup *l, ExecutionEngine *engine, Value *base) @@ -576,11 +581,11 @@ ReturnedValue QQmlContextWrapper::lookupContextObjectProperty(Lookup *l, Executi if (!qmlContext) return QV4::Encode::undefined(); - QQmlContextData *context = qmlContext->qmlContext(); + QQmlRefPointer<QQmlContextData> context = qmlContext->qmlContext(); if (!context) return QV4::Encode::undefined(); - QObject *contextObject = context->contextObject; + QObject *contextObject = context->contextObject(); if (!contextObject) return QV4::Encode::undefined(); @@ -621,11 +626,11 @@ ReturnedValue QQmlContextWrapper::lookupInParentContextHierarchy(Lookup *l, Exec if (!qmlContext) return QV4::Encode::undefined(); - QQmlContextData *context = qmlContext->qmlContext(); + QQmlRefPointer<QQmlContextData> context = qmlContext->qmlContext(); if (!context) return QV4::Encode::undefined(); - QQmlContextData *expressionContext = context; + QQmlRefPointer<QQmlContextData> expressionContext = context; QQmlEnginePrivate *ep = QQmlEnginePrivate::get(engine->qmlEngine()); @@ -635,18 +640,19 @@ ReturnedValue QQmlContextWrapper::lookupInParentContextHierarchy(Lookup *l, Exec ScopedValue result(scope); - for (context = context->parent; context; context = context->parent) { + for (context = context->parent(); context; context = context->parent()) { if (auto property = searchContextProperties(engine, context, name, nullptr, base, nullptr, nullptr, ep)) return *property; // Search context object - if (context->contextObject) { + if (QObject *contextObject = context->contextObject()) { bool hasProp = false; - result = QV4::QObjectWrapper::getQmlProperty(engine, context, context->contextObject, - name, QV4::QObjectWrapper::CheckRevision, &hasProp); + result = QV4::QObjectWrapper::getQmlProperty( + engine, context, contextObject, name, + QV4::QObjectWrapper::CheckRevision, &hasProp); if (hasProp) { if (base) - *base = QV4::QObjectWrapper::wrap(engine, context->contextObject); + *base = QV4::QObjectWrapper::wrap(engine, contextObject); return result->asReturnedValue(); } @@ -658,7 +664,7 @@ ReturnedValue QQmlContextWrapper::lookupInParentContextHierarchy(Lookup *l, Exec if (hasProp) return result->asReturnedValue(); - expressionContext->unresolvedNames = true; + expressionContext->setUnresolvedNames(true); return Encode::undefined(); } @@ -692,11 +698,15 @@ void Heap::QmlContext::init(QV4::ExecutionContext *outerContext, QV4::QQmlContex this->activation.set(internalClass->engine, qml->d()); } -Heap::QmlContext *QmlContext::create(ExecutionContext *parent, QQmlContextData *context, QObject *scopeObject) +Heap::QmlContext *QmlContext::create( + ExecutionContext *parent, QQmlRefPointer<QQmlContextData> context, + QObject *scopeObject) { Scope scope(parent); - Scoped<QQmlContextWrapper> qml(scope, scope.engine->memoryManager->allocate<QQmlContextWrapper>(context, scopeObject)); + Scoped<QQmlContextWrapper> qml( + scope, scope.engine->memoryManager->allocate<QQmlContextWrapper>( + std::move(context), scopeObject)); Heap::QmlContext *c = scope.engine->memoryManager->alloc<QmlContext>(parent, qml); Q_ASSERT(c->vtable() == staticVTable()); return c; diff --git a/src/qml/jsruntime/qv4qmlcontext_p.h b/src/qml/jsruntime/qv4qmlcontext_p.h index e3e7239fe5..ba7c5da146 100644 --- a/src/qml/jsruntime/qv4qmlcontext_p.h +++ b/src/qml/jsruntime/qv4qmlcontext_p.h @@ -56,7 +56,7 @@ #include <private/qv4object_p.h> #include <private/qv4context_p.h> -#include <private/qqmlcontext_p.h> +#include <private/qqmlcontextdata_p.h> QT_BEGIN_NAMESPACE @@ -72,10 +72,11 @@ namespace Heap { DECLARE_HEAP_OBJECT(QQmlContextWrapper, Object) { DECLARE_MARKOBJECTS(QQmlContextWrapper); - void init(QQmlContextData *context, QObject *scopeObject); + void init(QQmlRefPointer<QQmlContextData> context, QObject *scopeObject); void destroy(); - QQmlContextDataRef *context; + // This has to be a plain pointer because object needs to be a POD type. + QQmlContextData *context; QQmlQPointer<QObject> scopeObject; }; @@ -97,7 +98,7 @@ struct Q_QML_EXPORT QQmlContextWrapper : Object V4_INTERNALCLASS(QmlContextWrapper) inline QObject *getScopeObject() const { return d()->scopeObject; } - inline QQmlContextData *getContext() const { return *d()->context; } + inline QQmlRefPointer<QQmlContextData> getContext() const { return d()->context; } static ReturnedValue getPropertyAndBase(const QQmlContextWrapper *resource, PropertyKey id, const Value *receiver, bool *hasProperty, Value *base, Lookup *lookup = nullptr); @@ -120,13 +121,16 @@ struct Q_QML_EXPORT QmlContext : public ExecutionContext V4_MANAGED(QmlContext, ExecutionContext) V4_INTERNALCLASS(QmlContext) - static Heap::QmlContext *create(QV4::ExecutionContext *parent, QQmlContextData *context, QObject *scopeObject); + static Heap::QmlContext *create( + QV4::ExecutionContext *parent, QQmlRefPointer<QQmlContextData> context, + QObject *scopeObject); QObject *qmlScope() const { return d()->qml()->scopeObject; } - QQmlContextData *qmlContext() const { - return *d()->qml()->context; + + QQmlRefPointer<QQmlContextData> qmlContext() const { + return d()->qml()->context; } }; 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; diff --git a/src/qml/jsruntime/qv4qobjectwrapper_p.h b/src/qml/jsruntime/qv4qobjectwrapper_p.h index ac9cad2bdb..6d12271eb8 100644 --- a/src/qml/jsruntime/qv4qobjectwrapper_p.h +++ b/src/qml/jsruntime/qv4qobjectwrapper_p.h @@ -165,10 +165,19 @@ struct Q_QML_EXPORT QObjectWrapper : public Object QObject *object() const { return d()->object(); } - ReturnedValue getQmlProperty(QQmlContextData *qmlContext, String *name, RevisionMode revisionMode, bool *hasProperty = nullptr, bool includeImports = false) const; - static ReturnedValue getQmlProperty(ExecutionEngine *engine, QQmlContextData *qmlContext, QObject *object, String *name, RevisionMode revisionMode, bool *hasProperty = nullptr, QQmlPropertyData **property = nullptr); - - static bool setQmlProperty(ExecutionEngine *engine, QQmlContextData *qmlContext, QObject *object, String *name, RevisionMode revisionMode, const Value &value); + ReturnedValue getQmlProperty( + const QQmlRefPointer<QQmlContextData> &qmlContext, String *name, + RevisionMode revisionMode, bool *hasProperty = nullptr, + bool includeImports = false) const; + \ + static ReturnedValue getQmlProperty( + ExecutionEngine *engine, const QQmlRefPointer<QQmlContextData> &qmlContext, + QObject *object, String *name, RevisionMode revisionMode, bool *hasProperty = nullptr, + QQmlPropertyData **property = nullptr); + + static bool setQmlProperty( + ExecutionEngine *engine, const QQmlRefPointer<QQmlContextData> &qmlContext, + QObject *object, String *name, RevisionMode revisionMode, const Value &value); static ReturnedValue wrap(ExecutionEngine *engine, QObject *object); static void markWrapper(QObject *object, MarkStack *markStack); @@ -193,8 +202,13 @@ protected: static bool virtualIsEqualTo(Managed *that, Managed *o); static ReturnedValue create(ExecutionEngine *engine, QObject *object); - static QQmlPropertyData *findProperty(ExecutionEngine *engine, QObject *o, QQmlContextData *qmlContext, String *name, RevisionMode revisionMode, QQmlPropertyData *local); - QQmlPropertyData *findProperty(ExecutionEngine *engine, QQmlContextData *qmlContext, String *name, RevisionMode revisionMode, QQmlPropertyData *local) const; + static QQmlPropertyData *findProperty( + ExecutionEngine *engine, QObject *o, const QQmlRefPointer<QQmlContextData> &qmlContext, + String *name, RevisionMode revisionMode, QQmlPropertyData *local); + + QQmlPropertyData *findProperty( + ExecutionEngine *engine, const QQmlRefPointer<QQmlContextData> &qmlContext, + String *name, RevisionMode revisionMode, QQmlPropertyData *local) const; static ReturnedValue virtualGet(const Managed *m, PropertyKey id, const Value *receiver, bool *hasProperty); static bool virtualPut(Managed *m, PropertyKey id, const Value &value, Value *receiver); |