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