Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlcontextdata.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
6
7#include <QtQml/qqmlengine.h>
8#include <QtQml/private/qqmlcomponentattached_p.h>
9#include <QtQml/private/qqmljavascriptexpression_p.h>
10#include <QtQml/private/qqmlguardedcontextdata_p.h>
11
13
14/*!
15 \class QQmlContextData
16 \internal
17
18 \brief Runtime embodiment of a single QML component instance's context.
19
20 A QQmlContextData holds the state that backs a QML context: the parent/child
21 links, the id values, the imports, the compilation unit, the owned objects,
22 the imported scripts and so on.
23
24 Contexts usually form at component boundaries. That is, the root object of
25 a document, an inline component or the instantiation point of a Component.
26 When looking up unqalified names, the engine travels the context hierarchy
27 and considers both context properties and the context object for each
28 context. This is the reason why you can implicitly refer to the properties
29 of root objects (recursively), but not to the properties of intermediate
30 objects in the document tree. You can, however, manually mess with the
31 context hierarchy, and some QML types (e.g. models and views) do that.
32
33 The public \l QQmlContext is a thin shell on top of QQmlContextData. The
34 relationship is deliberately asymmetric:
35
36 \list
37 \li An \e internal context (\c m_isInternal == true) is created by the engine
38 while instantiating a component. The QQmlContextData is the primary object
39 and \e owns its publicContext, which is minted lazily by asQQmlContext().
40 Almost every context created during .qml instantiation is internal.
41 \li An \e external context (\c m_isInternal == false) is created when the user
42 constructs a QQmlContext explicitly; that QQmlContext owns the
43 QQmlContextData.
44 \endlist
45
46 QQmlContextData::get() bridges public to private; asQQmlContext()/
47 publicContext() bridge back. Lifetime is reference counted (\c m_refCount,
48 addref()/release()), with the cycle-avoiding twists described below.
49
50 \section1 The context tree: parent and childContexts
51
52 Contexts form a tree that mirrors the \e{document nesting} of QML, not the
53 QObject parent hierarchy and not the visual item tree:
54
55 \list
56 \li \c m_parent -- up the tree, to the next \e{component root}.
57 \li \c m_childContexts, \c m_nextChild, \c m_prevChild -- an intrusive child
58 list with a back-link to the slot that points at each node, so insert and
59 unlink are O(1) (see the constructor).
60 \endlist
61
62 Parent and child links are \e not reference counted: that would create cyclic
63 references. A parent keeps its children alive through ownership (see below);
64 children point back at the parent weakly. The tree propagates expression
65 refreshes downward (refreshExpressions() and friends walk childContexts())
66 and resolves relative URLs upward (url()/baseUrl() walk the parent chain).
67
68 \section1 Ownership: who keeps whom alive
69
70 Because parent->child links are raw, something else must pin contexts. There
71 are three ownership modes (\c enum \c Ownership):
72
73 \list
74 \li \c RefCounted (createRefCounted()/createBareContext()) - held by whoever
75 holds the QQmlRefPointer.
76 \li \c OwnedByParent (createChild()) - released when the parent drops it via
77 clearParent().
78 \li \c OwnedByPublicContext - released via clearPublicContext().
79 \endlist
80
81 \c m_ownedByParent and \c m_ownedByPublicContext are mutually exclusive bits.
82 Component root contexts created during instantiation are \c RefCounted; the
83 field that actually pins a root context in memory is the object side's
84 \c{QQmlData::ownContext}, which is a QQmlRefPointer (see below).
85
86 \section1 Binding to a type: the compilation unit
87
88 A context created for a compiled QML type remembers:
89
90 \list
91 \li \c m_typeCompilationUnit -- the CU this context belongs to.
92 \li \c m_componentObjectIndex -- the object index within that CU of the
93 component that created the context (0 for the document root, the IC root
94 index for an inline component).
95 \endlist
96
97 initFromTypeCompilationUnit() wires these up and sizes the id table from the
98 number of named objects in the component. So a context is the runtime
99 embodiment of one component instance: it knows its CU, its root object index,
100 how many ids that component declares, and the import set (\c m_imports) used
101 for name resolution.
102
103 \section1 The object side: QQmlData::context, outerContext, ownContext
104
105 Three QQmlData fields tie a QObject into the context world:
106
107 \list
108 \li \c outerContext - the context of the enclosing component instance: where
109 the object lives, where its id (if any) is registered, and where name
110 lookup for its bindings starts. Siblings created by the same component
111 share it. Not refcounted.
112 \li \c ownContext - non-null \e only for component roots (the document root,
113 an inline-component root, or a base-type level in a composite chain). It
114 is the refcounted pointer that keeps the introduced context alive. A plain
115 child object has \c ownContext == nullptr.
116 \li \c context - the effective context for the object's own bindings:
117 \c{== outerContext} for a borrowed child, \c{== ownContext.data()} for a
118 root.
119 \endlist
120
121 installContext() establishes these (called from
122 QQmlObjectCreator::initializeDData). A context also keeps the inverse mapping:
123 a doubly-linked list of the QQmlData it owns (\c m_ownedObjects), severed in
124 clearOwnedObjects() on destruction.
125
126 Now, given this explanation we clearly have a problem: The same component
127 root object can live in and own multiple contexts. It can inherit from
128 another QML type after all, with more inner objects. It can even have
129 different IDs in different outer contexts. That's where the linked contexts
130 come into play.
131
132 \section1 Linked contexts: the composite (base-type) chain
133
134 A QML type may derive from another QML type (MyButton.qml : Button.qml :
135 C++ QQuickButton). Each composite (QML-defined) level is a separate
136 compilation unit and gets its \e own context, yet they all describe the same
137 single QObject. These per-level contexts are chained through
138 \c m_linkedContext (\c{this} owns the next link, so the chain is refcounted
139 derived->base):
140
141 \list
142 \li \c{ddata->outerContext}/\c context/\c ownContext point at the
143 most-derived type the enclosing document instantiated.
144 \li linkedContext() walks the base types. The deepest-base type is first
145 and the most-derived one last.
146 \endlist
147
148 Object creation proceeds deepest-base-first, so installContext() appends each
149 newly installed (more derived) root to the end of the linked chain. Because
150 one QObject spans the whole chain, deepClearContextObject() must sweep every
151 link, not just the head, when detaching the context object.
152
153 \section1 Ids: the id-value table and context guards
154
155 Each context owns an array of \c ContextGuard, one per id declared in its
156 component (\c m_idValues, \c m_idValueCount). The index<->name mapping is the
157 lazily filled \c m_propertyNameCache (propertyIndex()/propertyName()). A
158 ContextGuard is a QQmlGuard plus a QQmlNotifier: assigning or destroying an
159 id'd object fires the notifier so alias and id-referencing bindings
160 re-evaluate. The \c ObjectWasSet tag (wasSet()) distinguishes "slot exists but
161 empty" from "set to null". findObjectId() does the reverse name lookup.
162
163 \section1 The context object
164
165 \c m_contextObject is the scope object whose properties are in unqualified
166 scope for bindings evaluated in this context (for a root context, the root
167 instance). isValid() couples an internal context's validity to the liveness
168 of its context object.
169
170 \section1 The extra slot
171
172 A single union slot, discriminated by \c m_hasExtraObject, serves two
173 mutually exclusive purposes:
174
175 \list
176 \li \c m_incubator - while a context is built asynchronously, the
177 QQmlIncubatorPrivate driving construction.
178 \li \c m_extraObject - repurposed afterward for component-specific side data;
179 currently only QQmlDelegateModel (QQmlDelegateModelItem::dataForObject).
180 \endlist
181
182 \section1 Other per-context state
183
184 \list
185 \li \c m_expressions - intrusive list of QQmlJavaScriptExpressions evaluated
186 in this context; the basis of refreshExpressions().
187 \li \c m_importedScripts - the JS array of .import'ed scripts; downgraded
188 strong->weak on invalidation so closures keep working without pinning.
189 \li \c m_imports - the QQmlTypeNameCache for resolving type names here.
190 \li \c m_componentAttacheds - uses of the Component attached property.
191 \li \c m_contextGuards - external weak references to this context.
192 \li \c m_baseUrl/\c m_baseUrlString - explicit base-URL overrides.
193 \endlist
194*/
195
196void QQmlContextData::installContext(QQmlData *ddata, QQmlContextData::QmlObjectKind kind)
197{
198 Q_ASSERT(ddata);
199 if (kind == QQmlContextData::DocumentRoot) {
200 if (ddata->context) {
201 Q_ASSERT(ddata->context != this);
202 Q_ASSERT(ddata->outerContext);
203 Q_ASSERT(ddata->outerContext != this);
204 QQmlRefPointer<QQmlContextData> c = ddata->context;
205 while (QQmlRefPointer<QQmlContextData> linked = c->linkedContext())
206 c = linked;
207 c->setLinkedContext(this);
208 } else {
209 ddata->context = this;
210 }
211 ddata->ownContext.reset(ddata->context);
212 } else if (!ddata->context) {
213 ddata->context = this;
214 }
215
216 addOwnedObject(ddata);
217}
218
219QUrl QQmlContextData::resolvedUrl(const QUrl &src) const
220{
221 QUrl resolved;
222 if (src.isRelative() && !src.isEmpty()) {
223 const QUrl ownUrl = url();
224 if (ownUrl.isValid()) {
225 resolved = ownUrl.resolved(src);
226 } else {
227 for (QQmlRefPointer<QQmlContextData> ctxt = parent(); ctxt; ctxt = ctxt->parent()) {
228 const QUrl ctxtUrl = ctxt->url();
229 if (ctxtUrl.isValid()) {
230 resolved = ctxtUrl.resolved(src);
231 break;
232 }
233 }
234
235 if (m_engine && resolved.isEmpty())
236 resolved = m_engine->baseUrl().resolved(src);
237 }
238 } else {
239 resolved = src;
240 }
241
242 if (resolved.isEmpty()) //relative but no ctxt
243 return resolved;
244
245 return m_engine ? m_engine->interceptUrl(resolved, QQmlAbstractUrlInterceptor::UrlString)
246 : resolved;
247}
248
249void QQmlContextData::emitDestruction()
250{
251 if (!m_hasEmittedDestruction) {
252 m_hasEmittedDestruction = true;
253
254 // Emit the destruction signal - must be emitted before invalidate so that the
255 // context is still valid if bindings or resultant expression evaluation requires it
256 if (m_engine) {
257 while (m_componentAttacheds) {
258 QQmlComponentAttached *attached = m_componentAttacheds;
259 attached->removeFromList();
260 emit attached->destruction();
261 }
262
263 for (QQmlRefPointer<QQmlContextData> child = m_childContexts; !child.isNull(); child = child->m_nextChild)
264 child->emitDestruction();
265 }
266 }
267}
268
269void QQmlContextData::invalidate()
270{
271 emitDestruction();
272
273 clearChildrenAndSiblings();
274 clearImportedScripts();
275
276 m_engine = nullptr;
277 clearParent();
278}
279
280/*!
281 \internal
282
283 Invalidates this context after its context object was lost, but keeps it linked to its
284 parent. The lexically enclosing contexts of a bound component have to stay reachable for
285 looking up ids, even after the context object is gone. If the parent dies first, it
286 invalidates this context again, which then clears the link.
287
288 This only applies to internal contexts, the ones the engine creates for the components it
289 instantiates. A context that is not internal is invalidated, which also clears its link to
290 the parent.
291*/
292void QQmlContextData::invalidateAfterContextObjectLost()
293{
294 if (!m_isInternal) {
295 invalidate();
296 return;
297 }
298
299 emitDestruction();
300
301 while (m_childContexts)
302 m_childContexts->invalidate();
303 clearImportedScripts();
304
305 m_engine = nullptr;
306}
307
308void QQmlContextData::clearContextObject()
309{
310 Q_ASSERT(m_contextObject.asT1());
311 // The property cache can be missing, for example if the object is still being created.
312 const QQmlData *ddata = QQmlData::get(m_contextObject.asT1(), false);
313 const QQmlPropertyCache *propCache = ddata ? ddata->propertyCache.data() : nullptr;
314 if (propCache)
315 propCache->addref();
316 m_contextObject = propCache;
317}
318
319void QQmlContextData::clearContextRecursively()
320{
321 emitDestruction();
322 clearExpressions();
323
324 for (auto ctxIt = m_childContexts; ctxIt; ctxIt = ctxIt->m_nextChild)
325 ctxIt->clearContextRecursively();
326
327 m_engine = nullptr;
328}
329
330void QQmlContextData::clearChildrenAndSiblings()
331{
332 while (m_childContexts) {
333 Q_ASSERT(m_childContexts != this);
334 m_childContexts->invalidate();
335 }
336
337 if (m_prevChild) {
338 *m_prevChild = m_nextChild;
339 if (m_nextChild) m_nextChild->m_prevChild = m_prevChild;
340 m_nextChild = nullptr;
341 m_prevChild = nullptr;
342 }
343}
344
345void QQmlContextData::clearImportedScripts()
346{
347 if (!m_hasWeakImportedScripts) { // might be called multiple times
348 if (m_engine && !m_importedScripts.isNullOrUndefined()) {
349 QV4::Scope scope(m_engine->handle());
350 QV4::ScopedValue val(scope, m_importedScripts.value());
351 m_importedScripts.~PersistentValue();
352 new (&m_weakImportedScripts) QV4::WeakValue();
353 m_weakImportedScripts.set(m_engine->handle(), val);
354 m_hasWeakImportedScripts = true;
355 } else {
356 // clear even if the value is null/undefined, in case it was set to explicit null/undefined
357 m_importedScripts.clear();
358 }
359 }
360}
361
362void QQmlContextData::clearOwnedObjects()
363{
364 while (m_ownedObjects) {
365 QQmlData *co = m_ownedObjects;
366 m_ownedObjects = m_ownedObjects->nextContextObject;
367
368 if (co->context == this)
369 co->context = nullptr;
370 co->outerContext = nullptr;
371 co->nextContextObject = nullptr;
372 co->prevContextObject = nullptr;
373 }
374}
375
376void QQmlContextData::clearContextGuards()
377{
378 for (QQmlGuardedContextData *contextGuard = m_contextGuards; contextGuard;) {
379 // TODO: Is this dead code? Why?
380 QQmlGuardedContextData *next = contextGuard->next();
381 contextGuard->setContextData({});
382 contextGuard = next;
383 }
384 m_contextGuards = nullptr;
385}
386
387void QQmlContextData::clearIdValues()
388{
389 delete[] std::exchange(m_idValues, nullptr);
390 m_idValueCount = 0;
391}
392
393void QQmlContextData::clearExpressions()
394{
395 QQmlJavaScriptExpression *expression = m_expressions;
396 while (expression) {
397 QQmlJavaScriptExpression *nextExpression = expression->m_nextExpression;
398
399 expression->m_prevExpression = nullptr;
400 expression->m_nextExpression = nullptr;
401
402 expression->setContext(nullptr);
403
404 expression = nextExpression;
405 }
406 m_expressions = nullptr;
407}
408
409QQmlContextData::~QQmlContextData()
410{
411 Q_ASSERT(refCount() == 0);
412
413 // avoid recursion
414 addref();
415 if (!m_hasWeakImportedScripts) {
416 // avoid busy work in invalidate – we don't want to construct a weak value
417 // just to throw it away afterwards
418 m_importedScripts.clear();
419 }
420 invalidate();
421 if (m_hasWeakImportedScripts)
422 m_weakImportedScripts.~WeakValue();
423 else
424 m_importedScripts.~PersistentValue();
425 m_linkedContext.reset();
426
427 Q_ASSERT(refCount() == 1);
428 emitDestruction();
429 clearExpressions();
430 Q_ASSERT(refCount() == 1);
431
432 clearOwnedObjects();
433 Q_ASSERT(refCount() == 1);
434
435 clearContextGuards();
436 Q_ASSERT(refCount() == 1);
437
438 clearIdValues();
439
440 Q_ASSERT(refCount() == 1);
441 if (m_publicContext)
442 delete m_publicContext;
443
444 Q_ASSERT(refCount() == 1);
445
446 if (const QQmlPropertyCache *cache = m_contextObject.isT2() ? m_contextObject.asT2() : nullptr)
447 cache->release();
448}
449
450void QQmlContextData::refreshExpressionsRecursive(QQmlJavaScriptExpression *expression)
451{
452 QQmlJavaScriptExpression::DeleteWatcher w(expression);
453
454 if (expression->m_nextExpression)
455 refreshExpressionsRecursive(expression->m_nextExpression);
456
457 if (!w.wasDeleted())
458 expression->refresh();
459}
460
461void QQmlContextData::refreshExpressionsRecursive(bool isGlobal)
462{
463 // For efficiency, we try and minimize the number of guards we have to create
464 if (hasExpressionsToRun(isGlobal) && (m_nextChild || m_childContexts)) {
465 QQmlGuardedContextData guard(this);
466
467 if (m_childContexts)
468 m_childContexts->refreshExpressionsRecursive(isGlobal);
469
470 if (guard.isNull()) return;
471
472 if (m_nextChild)
473 m_nextChild->refreshExpressionsRecursive(isGlobal);
474
475 if (guard.isNull()) return;
476
477 if (hasExpressionsToRun(isGlobal))
478 refreshExpressionsRecursive(m_expressions);
479
480 } else if (hasExpressionsToRun(isGlobal)) {
481 refreshExpressionsRecursive(m_expressions);
482 } else if (m_nextChild && m_childContexts) {
483 QQmlGuardedContextData guard(this);
484 m_childContexts->refreshExpressionsRecursive(isGlobal);
485 if (!guard.isNull() && m_nextChild)
486 m_nextChild->refreshExpressionsRecursive(isGlobal);
487 } else if (m_nextChild) {
488 m_nextChild->refreshExpressionsRecursive(isGlobal);
489 } else if (m_childContexts) {
490 m_childContexts->refreshExpressionsRecursive(isGlobal);
491 }
492}
493
494// Refreshes all expressions that could possibly depend on this context. Refreshing flushes all
495// context-tree dependent caches in the expressions, and should occur every time the context tree
496// *structure* (not values) changes.
497void QQmlContextData::refreshExpressions()
498{
499 bool isGlobal = (m_parent == nullptr);
500
501 // For efficiency, we try and minimize the number of guards we have to create
502 if (hasExpressionsToRun(isGlobal) && m_childContexts) {
503 QQmlGuardedContextData guard(this);
504 m_childContexts->refreshExpressionsRecursive(isGlobal);
505 if (!guard.isNull() && hasExpressionsToRun(isGlobal))
506 refreshExpressionsRecursive(m_expressions);
507 } else if (hasExpressionsToRun(isGlobal)) {
508 refreshExpressionsRecursive(m_expressions);
509 } else if (m_childContexts) {
510 m_childContexts->refreshExpressionsRecursive(isGlobal);
511 }
512}
513
514void QQmlContextData::addOwnedObject(QQmlData *data)
515{
516 if (data->outerContext) {
517 if (data->nextContextObject)
518 data->nextContextObject->prevContextObject = data->prevContextObject;
519 if (data->prevContextObject)
520 *data->prevContextObject = data->nextContextObject;
521 else if (data->outerContext->m_ownedObjects == data)
522 data->outerContext->m_ownedObjects = data->nextContextObject;
523 }
524
525 data->outerContext = this;
526
527 data->nextContextObject = m_ownedObjects;
528 if (data->nextContextObject)
529 data->nextContextObject->prevContextObject = &data->nextContextObject;
530 data->prevContextObject = &m_ownedObjects;
531 m_ownedObjects = data;
532}
533
534void QQmlContextData::setIdValue(int idx, QObject *obj)
535{
536 m_idValues[idx] = obj;
537 m_idValues[idx].setContext(this);
538}
539
540QString QQmlContextData::findObjectId(const QObject *obj) const
541{
542 for (int ii = 0; ii < m_idValueCount; ii++) {
543 if (m_idValues[ii] == obj)
544 return propertyName(ii);
545 }
546
547 const QVariant objVariant = QVariant::fromValue(obj);
548 if (m_publicContext) {
549 QQmlContextPrivate *p = QQmlContextPrivate::get(m_publicContext);
550 for (int ii = 0; ii < p->numPropertyValues(); ++ii)
551 if (p->propertyValue(ii) == objVariant)
552 return propertyName(ii);
553 }
554
555 if (QObject *contextObject = m_contextObject.asT1()) {
556 // This is expensive, but nameForObject should really mirror contextProperty()
557 for (const QMetaObject *metaObject = contextObject->metaObject();
558 metaObject; metaObject = metaObject->superClass()) {
559 for (int i = metaObject->propertyOffset(), end = metaObject->propertyCount();
560 i != end; ++i) {
561 const QMetaProperty prop = metaObject->property(i);
562 if (prop.metaType().flags() & QMetaType::PointerToQObject
563 && prop.read(contextObject) == objVariant) {
564 return QString::fromUtf8(prop.name());
565 }
566 }
567 }
568 }
569
570 return QString();
571}
572
573void QQmlContextData::setTypeCompilationUnit(
574 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &unit)
575{
576 m_propertyNameCache = QV4::IdentifierHash();
577 delete[] std::exchange(m_idValues, nullptr);
578 initFromTypeCompilationUnit(unit, m_componentObjectIndex);
579}
580
581void QQmlContextData::initFromTypeCompilationUnit(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &unit, int subComponentIndex)
582{
583 m_typeCompilationUnit = unit;
584 m_componentObjectIndex = subComponentIndex == -1 ? /*root object*/0 : subComponentIndex;
585 Q_ASSERT(!m_idValues);
586 m_idValueCount = m_typeCompilationUnit->namedObjectCountInComponent(m_componentObjectIndex);
587 if (m_idValueCount > 0)
588 m_idValues = new ContextGuard[m_idValueCount];
589}
590
591void QQmlContextData::addComponentAttached(QQmlComponentAttached *attached)
592{
593 attached->insertIntoList(&m_componentAttacheds);
594}
595
596void QQmlContextData::addExpression(QQmlJavaScriptExpression *expression)
597{
598 expression->insertIntoList(&m_expressions);
599}
600
601void QQmlContextData::initPropertyNames() const
602{
603 if (m_typeCompilationUnit) {
604 m_propertyNameCache = m_typeCompilationUnit->namedObjectsPerComponent(m_componentObjectIndex);
605 } else {
606 auto engine = m_engine;
607 if (!engine) {
608 // in some circumstances, we run into an invalidated context. In that case, we have no engine
609 // obviously, there's also no names to be found. Ideally, we'd have a special empty IdentifierHash
610 // for this which doesn't depend on an engine being available, but that currently doesn't exist.
611 // If we're evaluating, we should however still be able to find a parent context with an engine
612 for (auto ctxt = parent(); ctxt; ctxt = ctxt->parent()) {
613 if ((engine = ctxt->engine()))
614 break;
615 }
616 }
617 Q_ASSERT(engine);
618 m_propertyNameCache = QV4::IdentifierHash(engine->handle());
619 }
620 Q_ASSERT(m_propertyNameCache.isValid());
621}
622
623QUrl QQmlContextData::url() const
624{
625 if (m_typeCompilationUnit)
626 return m_typeCompilationUnit->finalUrl();
627 return m_baseUrl;
628}
629
630QString QQmlContextData::urlString() const
631{
632 if (m_typeCompilationUnit)
633 return m_typeCompilationUnit->finalUrlString();
634 return m_baseUrlString;
635}
636
637QT_END_NAMESPACE
Combined button and popup list for selecting options.