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
qqmlobjectcreator_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 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 QQMLOBJECTCREATOR_P_H
6#define QQMLOBJECTCREATOR_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/qqmlimport_p.h>
20#include <private/qqmltypenamecache_p.h>
21#include <private/qv4compileddata_p.h>
22#include <private/qrecursionwatcher_p.h>
23#include <private/qqmlprofiler_p.h>
24#include <private/qv4qmlcontext_p.h>
25#include <private/qqmlguardedcontextdata_p.h>
26#include <private/qqmlfinalizer_p.h>
27#include <private/qqmlvmemetaobject_p.h>
28
29#include <qpointer.h>
30#include <deque>
31
32QT_BEGIN_NAMESPACE
33
34class QQmlAbstractBinding;
37
42
43/*!
44\internal
45This struct contains information solely used for displaying error messages
46\variable aliasesToRequired allows us to give the user a way to know which (aliasing) properties
47can be set to set the required property
48\sa QQmlComponentPrivate::unsetRequiredPropertyToQQmlError
49*/
57
59{
61 RequiredPropertyKey(const QObject *object, const QQmlPropertyData *data)
62 : object(object)
63 , data(data)
64 {}
65
66 const QObject *object = nullptr;
67 const QQmlPropertyData *data = nullptr;
68
69private:
70 friend size_t qHash(const RequiredPropertyKey &key, size_t seed = 0)
71 {
72 return qHashMulti(seed, key.object, key.data);
73 }
74
75 friend bool operator==(const RequiredPropertyKey &a, const RequiredPropertyKey &b)
76 {
77 return a.object == b.object && a.data == b.data;
78 }
79};
80
82
93
99
100class Q_AUTOTEST_EXPORT ObjectInCreationGCAnchorList {
101public:
102 // this is a non owning view, rule of zero applies
103 ObjectInCreationGCAnchorList() = default;
104 ObjectInCreationGCAnchorList(const QV4::Scope &scope)
105 {
106 allocationScope = &scope;
107 }
108 void trackObject(QV4::ExecutionEngine *engine, QObject *instance);
109 bool canTrack() const { return allocationScope; }
110private:
111 // pointer to scope to reference JS wrappers during creation phase.
112 const QV4::Scope *allocationScope = nullptr;
113};
114
119
136
137class Q_QML_EXPORT QQmlObjectCreator
138{
139 Q_DECLARE_TR_FUNCTIONS(QQmlObjectCreator)
140public:
141 enum class InitFlag: quint8
142 {
143 None = 0x0,
144 IsDocumentRoot = 0x1,
145 IsContextObject = 0x2,
146 IsImplicitComponent = 0x4,
147 };
149
150 QQmlObjectCreator(const QQmlRefPointer<QQmlContextData> &parentContext,
151 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit,
152 const QQmlRefPointer<QQmlContextData> &creationContext,
153 const QString &inlineComponentName,
154 QQmlIncubatorPrivate *incubator = nullptr);
155 ~QQmlObjectCreator();
156
158 QObject *create(int subComponentIndex = -1, QObject *parent = nullptr,
159 QQmlInstantiationInterrupt *interrupt = nullptr, int flags = NormalObject);
160
161 bool populateDeferredProperties(QObject *instance, const QQmlData::DeferredData *deferredData);
162
163 void beginPopulateDeferred(const QQmlRefPointer<QQmlContextData> &context);
164 void populateDeferredBinding(const QQmlProperty &qmlProperty, int deferredIndex,
165 const QV4::CompiledData::Binding *binding);
166 void populateDeferredInstance(QObject *outerObject, int deferredIndex,
167 int index, QObject *instance, QObject *bindingTarget,
168 const QQmlPropertyData *valueTypeProperty,
169 const QV4::CompiledData::Binding *binding = nullptr);
171
172 bool finalize(QQmlInstantiationInterrupt &interrupt);
173 void clear();
174
176 int objectIndex, QObject *parent,
177 const QQmlRefPointer<QQmlContextData> &existingContext);
178
179 QQmlRefPointer<QQmlContextData> rootContext() const { return sharedState->rootContext; }
180 QQmlComponentAttached **componentAttachment() { return &sharedState->componentAttached; }
181
183
185 {
186 return parentContext.contextData();
187 }
188 std::vector<QQmlGuard<QObject> > &allCreatedObjects() { return sharedState->allCreatedObjects; }
189
190 RequiredProperties *requiredProperties() {return &sharedState->requiredProperties;}
191 bool componentHadTopLevelRequiredProperties() const {return sharedState->hadTopLevelRequiredProperties;}
192
193 static QQmlComponent *createComponent(
194 QQmlEngine *engine, QV4::ExecutableCompilationUnit *compilationUnit, int index,
195 QObject *parent, const QQmlRefPointer<QQmlContextData> &context);
196
197 void removePendingBinding(QObject *target, int propertyIndex) const
198 {
199 QList<DeferredQPropertyBinding> &pendingBindings = sharedState.data()->allQPropertyBindings;
200 pendingBindings.removeIf([&](const DeferredQPropertyBinding &deferred) {
201 return deferred.properyIndex == propertyIndex && deferred.target == target;
202 });
203 }
204
206 int index, QObject *instance,
207 const QQmlRefPointer<QQmlContextData> &instanceContext, InitFlags flags);
208
209 bool applyLateBinding(int index, QObject *instance,
210 const QQmlRefPointer<QQmlContextData> &instanceContext, int bindingIndex);
211
212private:
213 QQmlObjectCreator(
214 const QQmlRefPointer<QQmlContextData> &contextData,
215 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit,
216 const QString inlineComponentName,
217 QQmlObjectCreatorSharedState *inheritedSharedState, bool isContextObject);
218
219 void init(const QQmlRefPointer<QQmlContextData> &parentContext);
220
221 void initializeDData(
222 const QV4::CompiledData::Object *obj, QObject *instance, QQmlData *ddata,
223 InitFlags flags);
224 void initializePropertyCache(
225 int index, QQmlData *ddata, const QV4::ResolvedTypeReference *typeRef);
226 void initializeParent(QObject *instance, QObject *parent);
227 QObject *populateInstanceAndAliasBindings(int index, QObject *instance, InitFlags flags);
228
229 QObject *createInstance(int index, QObject *parent = nullptr, bool isContextObject = false);
230
231 QObject *initializeComponent(
232 int index, const QV4::CompiledData::Object *obj, QObject *instance, InitFlags flags);
233 QObject *initializeComposite(
234 int index, const QV4::CompiledData::Object *obj,
235 const QV4::ResolvedTypeReference *typeRef, QObject *instance, QObject *parent,
236 InitFlags flags);
237 QObject *initializeNonComposite(
238 int index, const QV4::CompiledData::Object *obj,
239 const QV4::ResolvedTypeReference *typeRef, QObject *instance, QObject *parent,
240 InitFlags flags);
241
242 bool populateInstance(int index, QObject *instance, QObject *bindingTarget,
243 const QQmlPropertyData *valueTypeProperty,
244 const QV4::CompiledData::Binding *binding = nullptr);
245
246 // If qmlProperty and binding are null, populate all properties, otherwise only the given one.
247 void populateDeferred(QObject *instance, int deferredIndex);
248 void populateDeferred(QObject *instance, int deferredIndex,
249 const QQmlPropertyPrivate *qmlProperty,
250 const QV4::CompiledData::Binding *binding);
251
252 enum BindingMode {
253 ApplyNone = 0x0,
254 ApplyImmediate = 0x1,
255 ApplyDeferred = 0x2,
256 ApplyAll = ApplyImmediate | ApplyDeferred,
257 };
258 Q_DECLARE_FLAGS(BindingSetupFlags, BindingMode);
259
260 void setupBindings(BindingSetupFlags mode = BindingMode::ApplyImmediate);
261 bool setPropertyBinding(const QQmlPropertyData *property, const QV4::CompiledData::Binding *binding);
262 void setPropertyValue(const QQmlPropertyData *property, const QV4::CompiledData::Binding *binding,
263 std::optional<int> foldedNumber = std::nullopt);
264 void setupFunctions();
265
266 QString stringAt(int idx) const { return compilationUnit->stringAt(idx); }
267 void recordError(const QV4::CompiledData::Location &location, const QString &description);
268
269 void registerObjectWithContextById(int objectIndex, QObject *instance) const;
270
271 inline QV4::QmlContext *currentQmlContext();
272 QV4::ResolvedTypeReference *resolvedType(int id) const
273 {
274 return compilationUnit->resolvedType(id);
275 }
276
277 enum Phase {
278 Startup,
279 CreatingObjects,
280 CreatingObjectsPhase2,
281 ObjectsCreated,
282 Finalizing,
283 Done
284 } phase;
285
286 QQmlEngine *engine;
287 QV4::ExecutionEngine *v4;
288 QString m_inlineComponentName;
289 QQmlRefPointer<QV4::ExecutableCompilationUnit> compilationUnit;
290 const QV4::CompiledData::Unit *qmlUnit;
291 QQmlGuardedContextData parentContext;
292 QQmlRefPointer<QQmlContextData> context;
293 const QQmlPropertyCacheVector *propertyCaches;
294 QQmlRefPointer<QQmlObjectCreatorSharedState> sharedState;
295 bool topLevelCreator;
296 bool isContextObject;
297 QQmlIncubatorPrivate *incubator;
298
299 QObject *_qobject;
300 QObject *_scopeObject;
301 QObject *_bindingTarget;
302
303 const QQmlPropertyData *_valueTypeProperty; // belongs to _qobjectForBindings's property cache
304 int _compiledObjectIndex;
305 const QV4::CompiledData::Object *_compiledObject;
306 QQmlData *_ddata;
307 QQmlPropertyCache::ConstPtr _propertyCache;
308 QQmlVMEMetaObject *_vmeMetaObject;
309 QQmlListProperty<QObject> _currentList;
310 QV4::QmlContext *_qmlContext;
311
313
314 typedef std::function<bool(QQmlObjectCreatorSharedState *sharedState)> PendingAliasBinding;
315 std::vector<PendingAliasBinding> pendingAliasBindings;
316
317 template<typename Functor>
318 void doPopulateDeferred(QObject *instance, int deferredIndex, Functor f)
319 {
320 QQmlData *declarativeData = QQmlData::get(instance);
321
322 // We're in the process of creating the object. We sure hope it's still alive.
323 Q_ASSERT(declarativeData && declarativeData->propertyCache);
324
325 QObject *bindingTarget = instance;
326
327 QQmlPropertyCache::ConstPtr cache = declarativeData->propertyCache;
328 QQmlVMEMetaObject *vmeMetaObject = QQmlVMEMetaObject::get(instance, v4);
329
330 QObject *scopeObject = instance;
331 qt_ptr_swap(_scopeObject, scopeObject);
332
333 QV4::Scope valueScope(v4);
334 QScopedValueRollback<ObjectInCreationGCAnchorList> jsObjectGuard(
335 sharedState->allJavaScriptObjects, ObjectInCreationGCAnchorList(valueScope));
336
337 Q_ASSERT(topLevelCreator);
338 QV4::QmlContext *qmlContext = static_cast<QV4::QmlContext *>(valueScope.constructUndefined(1));
339
340 qt_ptr_swap(_qmlContext, qmlContext);
341
342 _propertyCache.swap(cache);
343 qt_ptr_swap(_qobject, instance);
344
345 int objectIndex = deferredIndex;
346 std::swap(_compiledObjectIndex, objectIndex);
347
348 const QV4::CompiledData::Object *obj = compilationUnit->objectAt(_compiledObjectIndex);
349 qt_ptr_swap(_compiledObject, obj);
350 qt_ptr_swap(_ddata, declarativeData);
351 qt_ptr_swap(_bindingTarget, bindingTarget);
352 qt_ptr_swap(_vmeMetaObject, vmeMetaObject);
353
354 f();
355
356 qt_ptr_swap(_vmeMetaObject, vmeMetaObject);
357 qt_ptr_swap(_bindingTarget, bindingTarget);
358 qt_ptr_swap(_ddata, declarativeData);
359 qt_ptr_swap(_compiledObject, obj);
360 std::swap(_compiledObjectIndex, objectIndex);
361 qt_ptr_swap(_qobject, instance);
362 _propertyCache.swap(cache);
363
364 qt_ptr_swap(_qmlContext, qmlContext);
365 qt_ptr_swap(_scopeObject, scopeObject);
366 }
367 void registerPostHocRequiredProperties(const QV4::CompiledData::Binding *binding);
368};
369
371{
372 QQmlObjectCreatorRecursionWatcher(QQmlObjectCreator *creator);
373
374 bool hasRecursed() const { return watcher.hasRecursed(); }
375
376private:
377 QQmlRefPointer<QQmlObjectCreatorSharedState> sharedState;
378 QRecursionWatcher<QQmlObjectCreatorSharedState, &QQmlObjectCreatorSharedState::recursionNode> watcher;
379};
380
381QV4::QmlContext *QQmlObjectCreator::currentQmlContext()
382{
383 if (!_qmlContext->isManaged())
384 _qmlContext->setM(QV4::QmlContext::create(v4->rootContext(), context, _scopeObject));
385
386 return _qmlContext;
387}
388
389Q_DECLARE_OPERATORS_FOR_FLAGS(QQmlObjectCreator::InitFlags);
390
391QT_END_NAMESPACE
392
393#endif // QQMLOBJECTCREATOR_P_H
friend class QQmlIncubatorPrivate
QQmlComponentAttached ** componentAttachment()
Q_DECLARE_FLAGS(InitFlags, InitFlag)
bool finalize(QQmlInstantiationInterrupt &interrupt)
void repopulateBindings(int index, QObject *instance, const QQmlRefPointer< QQmlContextData > &instanceContext, InitFlags flags)
bool applyLateBinding(int index, QObject *instance, const QQmlRefPointer< QQmlContextData > &instanceContext, int bindingIndex)
bool componentHadTopLevelRequiredProperties() const
void beginPopulateDeferred(const QQmlRefPointer< QQmlContextData > &context)
QQmlRefPointer< QQmlContextData > parentContextData() const
static QQmlComponent * createComponent(QQmlEngine *engine, QV4::ExecutableCompilationUnit *compilationUnit, int index, QObject *parent, const QQmlRefPointer< QQmlContextData > &context)
QObject * create(int subComponentIndex=-1, QObject *parent=nullptr, QQmlInstantiationInterrupt *interrupt=nullptr, int flags=NormalObject)
QObject * createObjectInContext(int objectIndex, QObject *parent, const QQmlRefPointer< QQmlContextData > &existingContext)
void removePendingBinding(QObject *target, int propertyIndex) const
std::vector< QQmlGuard< QObject > > & allCreatedObjects()
void populateDeferredBinding(const QQmlProperty &qmlProperty, int deferredIndex, const QV4::CompiledData::Binding *binding)
RequiredProperties * requiredProperties()
QList< QQmlError > errors
void populateDeferredInstance(QObject *outerObject, int deferredIndex, int index, QObject *instance, QObject *bindingTarget, const QQmlPropertyData *valueTypeProperty, const QV4::CompiledData::Binding *binding=nullptr)
bool populateDeferredProperties(QObject *instance, const QQmlData::DeferredData *deferredData)
QQmlObjectCreator(const QQmlRefPointer< QQmlContextData > &parentContext, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &compilationUnit, const QQmlRefPointer< QQmlContextData > &creationContext, const QString &inlineComponentName, QQmlIncubatorPrivate *incubator=nullptr)
QQmlRefPointer< QQmlContextData > rootContext() const
void clear()
Clears the entire result set and releases any associated resources.
RequiredPropertiesAndTarget & operator=(RequiredPropertiesAndTarget &&)=default
RequiredPropertiesAndTarget(const RequiredPropertiesAndTarget &)=default
RequiredPropertiesAndTarget(QObject *target)
RequiredPropertiesAndTarget & operator=(const RequiredPropertiesAndTarget &)=default
RequiredPropertiesAndTarget(RequiredPropertiesAndTarget &&)=default
Combined button and popup list for selecting options.
Definition qjsvalue.h:24
QJSEngine engine
[0]
QUntypedPropertyBinding binding
QQmlObjectCreatorRecursionWatcher(QQmlObjectCreator *creator)
QList< DeferredQPropertyBinding > allQPropertyBindings
std::vector< QQmlGuard< QObject > > allCreatedObjects
QQmlRefPointer< QQmlContextData > rootContext
std::vector< ParserStatus > allParserStatusCallbacks
QQmlRefPointer< QQmlContextData > creationContext
QQmlComponentAttached * componentAttached
std::vector< QQmlAbstractBinding::Ptr > allCreatedBindings
QList< QQmlFinalizerHook * > finalizeHooks
ObjectInCreationGCAnchorList allJavaScriptObjects
QString stringAt(int index) const
QV4::CompiledData::Location location
QList< AliasToRequiredInfo > aliasesToRequired
friend bool operator==(const RequiredPropertyKey &a, const RequiredPropertyKey &b)
friend size_t qHash(const RequiredPropertyKey &key, size_t seed=0)
RequiredPropertyKey()=default
const QQmlPropertyData * data
RequiredPropertyKey(const QObject *object, const QQmlPropertyData *data)