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_p.h
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
5#ifndef QQMLCONTEXTDATA_P_H
6#define QQMLCONTEXTDATA_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <private/qqmlcontext_p.h>
20#include <private/qqmlguard_p.h>
21#include <private/qqmlnotifier_p.h>
22#include <private/qqmltypenamecache_p.h>
23#include <private/qtqmlglobal_p.h>
24#include <private/qv4executablecompilationunit_p.h>
25#include <private/qv4identifierhash_p.h>
26
27QT_BEGIN_NAMESPACE
28
29class QQmlComponentAttached;
30class QQmlGuardedContextData;
31class QQmlJavaScriptExpression;
33
34class Q_QML_EXPORT QQmlContextData
35{
36public:
37 static QQmlRefPointer<QQmlContextData> createRefCounted(
38 const QQmlRefPointer<QQmlContextData> &parent)
39 {
40 return QQmlRefPointer<QQmlContextData>(new QQmlContextData(RefCounted, nullptr, parent),
41 QQmlRefPointer<QQmlContextData>::Adopt);
42 }
43
44 // Owned by the parent. When the parent is reset to nullptr, it will be deref'd.
45 static QQmlRefPointer<QQmlContextData> createChild(
46 const QQmlRefPointer<QQmlContextData> &parent)
47 {
48 Q_ASSERT(!parent.isNull());
49 return QQmlRefPointer<QQmlContextData>(new QQmlContextData(OwnedByParent, nullptr, parent));
50 }
51
52 void addref() const { ++m_refCount; }
53 void release() const { if (--m_refCount == 0) delete this; }
54 int count() const { return m_refCount; }
55 int refCount() const { return m_refCount; }
56
57 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &typeCompilationUnit() const
58 {
59 return m_typeCompilationUnit;
60 }
61 int componentObjectIndex() const { return m_componentObjectIndex; }
62 void setTypeCompilationUnit(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &unit);
63
64 void initFromTypeCompilationUnit(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &unit,
65 int subComponentIndex);
66
67 static QQmlRefPointer<QQmlContextData> get(QQmlContext *context) {
68 return QQmlContextPrivate::get(context)->m_data;
69 }
70
71 void emitDestruction();
72 void clearExpressions();
73 void clearContextRecursively();
74 void clearChildrenAndSiblings();
75 void clearImportedScripts();
76 void clearOwnedObjects();
77 void clearContextGuards();
78 void clearIdValues();
79 void invalidate();
80 void invalidateAfterContextObjectLost();
81
82 bool isValid() const
83 {
84 if (!m_engine)
85 return false;
86
87 if (!m_isInternal)
88 return true;
89
90 if (!m_contextObject.isT1())
91 return true;
92
93 if (QObject *contextObject = m_contextObject.asT1())
94 return !QObjectPrivate::get(contextObject)->wasDeleted;
95
96 return true;
97 }
98
99 bool isInternal() const { return m_isInternal; }
100 void setInternal(bool isInternal) { m_isInternal = isInternal; }
101
102 bool isJSContext() const { return m_isJSContext; }
103 void setJSContext(bool isJSContext) { m_isJSContext = isJSContext; }
104
105 bool isPragmaLibraryContext() const { return m_isPragmaLibraryContext; }
106 void setPragmaLibraryContext(bool library) { m_isPragmaLibraryContext = library; }
107
108 QQmlRefPointer<QQmlContextData> parent() const { return m_parent; }
109 void clearParent()
110 {
111 if (!m_parent)
112 return;
113
114 m_parent = nullptr;
115 if (m_ownedByParent) {
116 m_ownedByParent = false;
117 release();
118 }
119 }
120
121 void refreshExpressions();
122
123 void addOwnedObject(QQmlData *ownedObject);
124 QQmlData *ownedObjects() const { return m_ownedObjects; }
125 void setOwnedObjects(QQmlData *ownedObjects) { m_ownedObjects = ownedObjects; }
126
127 enum QmlObjectKind {
128 OrdinaryObject,
129 DocumentRoot,
130 };
131 void installContext(QQmlData *ddata, QmlObjectKind kind);
132
133 QUrl resolvedUrl(const QUrl &) const;
134
135 // My containing QQmlContext. If isInternal is true this owns publicContext.
136 // If internal is false publicContext owns this.
137 QQmlContext *asQQmlContext()
138 {
139 if (!m_publicContext)
140 m_publicContext = new QQmlContext(*new QQmlContextPrivate(this));
141 return m_publicContext;
142 }
143
144 QQmlContextPrivate *asQQmlContextPrivate()
145 {
146 return QQmlContextPrivate::get(asQQmlContext());
147 }
148
149 QObject *contextObject() const
150 {
151 return m_contextObject.isT1() ? m_contextObject.asT1() : nullptr;
152 }
153
154 void setContextObject(QObject *contextObject)
155 {
156 Q_ASSERT(!m_contextObject.isT2());
157 m_contextObject = contextObject;
158 }
159
160 const QQmlPropertyCache *lostContextObjectCache() const
161 {
162 return m_contextObject.isT2() ? m_contextObject.asT2() : nullptr;
163 }
164
165 template<typename HandleSelf, typename HandleLinked>
166 void deepClearContextObject(
167 QObject *contextObject, HandleSelf &&handleSelf, HandleLinked &&handleLinked) {
168 for (QQmlContextData *lc = m_linkedContext.data(); lc; lc = lc->m_linkedContext.data()) {
169 handleLinked(lc);
170 if (lc->m_contextObject == contextObject)
171 lc->clearContextObject();
172 }
173
174 handleSelf(this);
175 if (m_contextObject == contextObject)
176 clearContextObject();
177 }
178
179 void deepClearContextObject(QObject *contextObject)
180 {
181 deepClearContextObject(
182 contextObject,
183 [](QQmlContextData *self) { self->emitDestruction(); },
184 [](QQmlContextData *){});
185 }
186
187 QQmlEngine *engine() const { return m_engine; }
188 void setEngine(QQmlEngine *engine) { m_engine = engine; }
189
190 QQmlContext *publicContext() const { return m_publicContext; }
191 void clearPublicContext()
192 {
193 if (!m_publicContext)
194 return;
195
196 m_publicContext = nullptr;
197 if (m_ownedByPublicContext) {
198 m_ownedByPublicContext = false;
199 release();
200 }
201 }
202
203 int propertyIndex(const QString &name) const
204 {
205 ensurePropertyNames();
206 return m_propertyNameCache.value(name);
207 }
208
209 int propertyIndex(QV4::Heap::String *name) const
210 {
211 ensurePropertyNames();
212 return m_propertyNameCache.value(name);
213 }
214
215 QString propertyName(int index) const
216 {
217 ensurePropertyNames();
218 return m_propertyNameCache.key<QString>(index);
219 }
220
221 void addPropertyNameAndIndex(const QString &name, int index)
222 {
223 Q_ASSERT(m_propertyNameCache.isValid());
224 m_propertyNameCache.add(name, index);
225 }
226
227 void setExpressions(QQmlJavaScriptExpression *expressions) { m_expressions = expressions; }
228 QQmlJavaScriptExpression *expressions() const { return m_expressions; }
229 QQmlJavaScriptExpression *takeExpressions()
230 {
231 QQmlJavaScriptExpression *expressions = m_expressions;
232 m_expressions = nullptr;
233 return expressions;
234 }
235
236 void setChildContexts(const QQmlRefPointer<QQmlContextData> &childContexts)
237 {
238 m_childContexts = childContexts.data();
239 }
240 QQmlRefPointer<QQmlContextData> childContexts() const { return m_childContexts; }
241 QQmlRefPointer<QQmlContextData> takeChildContexts()
242 {
243 QQmlRefPointer<QQmlContextData> childContexts = m_childContexts;
244 m_childContexts = nullptr;
245 return childContexts;
246 }
247 QQmlRefPointer<QQmlContextData> nextChild() const { return m_nextChild; }
248
249 int numIdValues() const { return m_idValueCount; }
250 void setIdValue(int index, QObject *idValue);
251 bool isIdValueSet(int index) const { return m_idValues[index].wasSet(); }
252 QQmlNotifier *idValueBindings(int index) const { return m_idValues[index].bindings(); }
253 QObject *idValue(int index) const { return m_idValues[index].data(); }
254
255 // Return the outermost id for obj, if any.
256 QString findObjectId(const QObject *obj) const;
257
258 // url() and urlString() prefer the CU's URL over explicitly set baseUrls. They
259 // don't search the context hierarchy.
260 // baseUrl() and baseUrlString() search the context hierarchy and prefer explicit
261 // base URLs over CU Urls.
262
263 QUrl url() const;
264 QString urlString() const;
265
266 void setBaseUrlString(const QString &baseUrlString) { m_baseUrlString = baseUrlString; }
267 QString baseUrlString() const
268 {
269 for (const QQmlContextData *data = this; data; data = data->m_parent) {
270 if (!data->m_baseUrlString.isEmpty())
271 return data->m_baseUrlString;
272 if (data->m_typeCompilationUnit)
273 return data->m_typeCompilationUnit->finalUrlString();
274 }
275 return QString();
276 }
277
278 void setBaseUrl(const QUrl &baseUrl) { m_baseUrl = baseUrl; }
279 QUrl baseUrl() const
280 {
281 for (const QQmlContextData *data = this; data; data = data->m_parent) {
282 if (!data->m_baseUrl.isEmpty())
283 return data->m_baseUrl;
284 if (data->m_typeCompilationUnit)
285 return data->m_typeCompilationUnit->finalUrl();
286 }
287 return QUrl();
288 }
289
290 QQmlRefPointer<QQmlTypeNameCache> imports() const { return m_imports; }
291 void setImports(const QQmlRefPointer<QQmlTypeNameCache> &imports) { m_imports = imports; }
292
293 QQmlIncubatorPrivate *incubator() const { return m_hasExtraObject ? nullptr : m_incubator; }
294 void setIncubator(QQmlIncubatorPrivate *incubator)
295 {
296 Q_ASSERT(!m_hasExtraObject || m_extraObject == nullptr);
297 m_hasExtraObject = false;
298 m_incubator = incubator;
299 }
300
301 QObject *extraObject() const { return m_hasExtraObject ? m_extraObject : nullptr; }
302 void setExtraObject(QObject *extraObject)
303 {
304 Q_ASSERT(m_hasExtraObject || m_incubator == nullptr);
305 m_hasExtraObject = true;
306 m_extraObject = extraObject;
307 }
308
309 bool isRootObjectInCreation() const { return m_isRootObjectInCreation; }
310 void setRootObjectInCreation(bool rootInCreation) { m_isRootObjectInCreation = rootInCreation; }
311
312 QV4::ReturnedValue importedScripts() const
313 {
314 if (m_hasWeakImportedScripts)
315 return m_weakImportedScripts.value();
316 else
317 return m_importedScripts.value();
318 }
319 void setImportedScripts(QV4::ExecutionEngine *engine, QV4::Value scripts) {
320 // setImportedScripts should not be called on an invalidated context
321 Q_ASSERT(!m_hasWeakImportedScripts);
322 m_importedScripts.set(engine, scripts);
323 }
324
325 /*
326 we can safely pass a ReturnedValue here, as setImportedScripts will directly store
327 scripts in a persistentValue, without any intermediate allocation that could trigger
328 a gc run
329 */
330 void setImportedScripts(QV4::ExecutionEngine *engine, QV4::ReturnedValue scripts) {
331 // setImportedScripts should not be called on an invalidated context
332 Q_ASSERT(!m_hasWeakImportedScripts);
333 m_importedScripts.set(engine, scripts);
334 }
335
336 QQmlRefPointer<QQmlContextData> linkedContext() const { return m_linkedContext; }
337 void setLinkedContext(const QQmlRefPointer<QQmlContextData> &context) { m_linkedContext = context; }
338
339 bool hasUnresolvedNames() const { return m_unresolvedNames; }
340 void setUnresolvedNames(bool hasUnresolvedNames) { m_unresolvedNames = hasUnresolvedNames; }
341
342 QQmlComponentAttached *componentAttacheds() const { return m_componentAttacheds; }
343 void addComponentAttached(QQmlComponentAttached *attached);
344
345 void addExpression(QQmlJavaScriptExpression *expression);
346
347 bool valueTypesAreAddressable() const {
348 return m_typeCompilationUnit && m_typeCompilationUnit->valueTypesAreAddressable();
349 }
350
351 bool valueTypesAreAssertable() const {
352 return m_typeCompilationUnit && m_typeCompilationUnit->valueTypesAreAssertable();
353 }
354
355private:
356 friend class QQmlGuardedContextData;
357 friend class QQmlContextPrivate;
358
359 enum Ownership {
360 RefCounted,
361 OwnedByParent,
362 OwnedByPublicContext
363 };
364
365 // id guards
366 struct ContextGuard : public QQmlGuard<QObject>
367 {
368 enum Tag {
369 NoTag,
370 ObjectWasSet
371 };
372
373 inline ContextGuard() : QQmlGuard<QObject>(&ContextGuard::objectDestroyedImpl, nullptr), m_context(nullptr) {}
374 inline ContextGuard &operator=(QObject *obj);
375
376 inline bool wasSet() const;
377
378 QQmlNotifier *bindings() { return &m_bindings; }
379 void setContext(const QQmlRefPointer<QQmlContextData> &context)
380 {
381 m_context = context.data();
382 }
383
384 private:
385 inline static void objectDestroyedImpl(QQmlGuardImpl *);
386 // Not refcounted, as it always belongs to the QQmlContextData.
387 QTaggedPointer<QQmlContextData, Tag> m_context;
388 QQmlNotifier m_bindings;
389 };
390
391 // It's OK to pass a half-created publicContext here. We will not dereference it during
392 // construction.
393 QQmlContextData(
394 Ownership ownership, QQmlContext *publicContext,
395 const QQmlRefPointer<QQmlContextData> &parent, QQmlEngine *engine = nullptr)
396 : m_parent(parent.data()),
397 m_engine(engine ? engine : (parent.isNull() ? nullptr : parent->engine())),
398 m_isInternal(false), m_isJSContext(false), m_isPragmaLibraryContext(false),
399 m_unresolvedNames(false), m_hasEmittedDestruction(false), m_isRootObjectInCreation(false),
400 m_ownedByParent(ownership == OwnedByParent),
401 m_ownedByPublicContext(ownership == OwnedByPublicContext), m_hasExtraObject(false),
402 m_hasWeakImportedScripts(false), m_dummy(0), m_publicContext(publicContext), m_incubator(nullptr)
403 {
404 Q_ASSERT(!m_ownedByParent || !m_ownedByPublicContext);
405 if (!m_parent)
406 return;
407
408 m_nextChild = m_parent->m_childContexts;
409 if (m_nextChild)
410 m_nextChild->m_prevChild = &m_nextChild;
411 m_prevChild = &m_parent->m_childContexts;
412 m_parent->m_childContexts = this;
413 }
414
415 ~QQmlContextData();
416
417 bool hasExpressionsToRun(bool isGlobalRefresh) const
418 {
419 return m_expressions && (!isGlobalRefresh || m_unresolvedNames);
420 }
421
422 void refreshExpressionsRecursive(bool isGlobal);
423 void refreshExpressionsRecursive(QQmlJavaScriptExpression *);
424 void initPropertyNames() const;
425
426 void ensurePropertyNames() const
427 {
428 if (!m_propertyNameCache.isValid())
429 initPropertyNames();
430 Q_ASSERT(m_propertyNameCache.isValid());
431 }
432
433 // My parent context and engine
434 QQmlContextData *m_parent = nullptr;
435 QQmlEngine *m_engine = nullptr;
436
437 mutable quint32 m_refCount = 1;
438 quint32 m_isInternal:1;
439 quint32 m_isJSContext:1;
440 quint32 m_isPragmaLibraryContext:1;
441 quint32 m_unresolvedNames:1; // True if expressions in this context failed to resolve a toplevel name
442 quint32 m_hasEmittedDestruction:1;
443 quint32 m_isRootObjectInCreation:1;
444 quint32 m_ownedByParent:1;
445 quint32 m_ownedByPublicContext:1;
446 quint32 m_hasExtraObject:1; // used in QQmlDelegateModelItem::dataForObject to find the corresponding QQmlDelegateModelItem of an object
447 quint32 m_hasWeakImportedScripts:1;
448 Q_DECL_UNUSED_MEMBER quint32 m_dummy:22;
449 QQmlContext *m_publicContext = nullptr;
450
451 union {
452 // The incubator that is constructing this context if any
453 QQmlIncubatorPrivate *m_incubator;
454 // a pointer to extra data, currently only used in QQmlDelegateModel
455 QObject *m_extraObject;
456 };
457
458 // Compilation unit for contexts that belong to a compiled type.
459 QQmlRefPointer<QV4::ExecutableCompilationUnit> m_typeCompilationUnit;
460
461 // object index in CompiledData::Unit to component that created this context
462 int m_componentObjectIndex = -1;
463
464 // flag indicates whether the context owns the cache (after mutation) or not.
465 mutable QV4::IdentifierHash m_propertyNameCache;
466
467 void clearContextObject();
468
469 // The type of the context object (its property cache) is retained
470 // when the context object is lost.
471 QBiPointer<QObject, const QQmlPropertyCache> m_contextObject;
472
473 // Any script blocks that exist on this context
474 union {
475 /* an invalidated context transitions from a strong reference to the scripts
476 to a weak one, so that the context doesn't needlessly holds on to the scripts,
477 but closures can still access them if needed
478 */
479 QV4::PersistentValue m_importedScripts = {}; // This is a JS Array
480 QV4::WeakValue m_weakImportedScripts;
481 };
482
483 QUrl m_baseUrl;
484 QString m_baseUrlString;
485
486 // List of imports that apply to this context
487 QQmlRefPointer<QQmlTypeNameCache> m_imports;
488
489 // My children, not refcounted as that would create cyclic references
490 QQmlContextData *m_childContexts = nullptr;
491
492 // My peers in parent's childContexts list; not refcounted
493 QQmlContextData *m_nextChild = nullptr;
494 QQmlContextData **m_prevChild = nullptr;
495
496 // Expressions that use this context
497 QQmlJavaScriptExpression *m_expressions = nullptr;
498
499 // Doubly-linked list of objects that are owned by this context
500 QQmlData *m_ownedObjects = nullptr;
501
502 // Doubly-linked list of context guards (XXX merge with contextObjects)
503 QQmlGuardedContextData *m_contextGuards = nullptr;
504
505 ContextGuard *m_idValues = nullptr;
506 int m_idValueCount = 0;
507
508 // Linked contexts. this owns linkedContext.
509 QQmlRefPointer<QQmlContextData> m_linkedContext;
510
511 // Linked list of uses of the Component attached property in this context
512 QQmlComponentAttached *m_componentAttacheds = nullptr;
513};
514
515QQmlContextData::ContextGuard &QQmlContextData::ContextGuard::operator=(QObject *obj)
516{
517 QQmlGuard<QObject>::operator=(obj);
518 m_context.setTag(ObjectWasSet);
519 m_bindings.notify(); // For alias connections
520 return *this;
521}
522
523 void QQmlContextData::ContextGuard::objectDestroyedImpl(QQmlGuardImpl *impl)
524{
525 auto This = static_cast<QQmlContextData::ContextGuard *>(impl);
526 if (QObject *contextObject = This->m_context->contextObject()) {
527 if (!QObjectPrivate::get(contextObject)->wasDeleted)
528 This->m_bindings.notify();
529 }
530}
531
532bool QQmlContextData::ContextGuard::wasSet() const
533{
534 return m_context.tag() == ObjectWasSet;
535}
536
537QT_END_NAMESPACE
538
539#endif // QQMLCONTEXTDATA_P_H
Runtime embodiment of a single QML component instance's context.
friend class QQmlIncubatorPrivate
Combined button and popup list for selecting options.