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
209private:
210 QQmlObjectCreator(
211 const QQmlRefPointer<QQmlContextData> &contextData,
212 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit,
213 const QString inlineComponentName,
214 QQmlObjectCreatorSharedState *inheritedSharedState, bool isContextObject);
215
216 void init(const QQmlRefPointer<QQmlContextData> &parentContext);
217
218 void initializeDData(
219 const QV4::CompiledData::Object *obj, QObject *instance, QQmlData *ddata,
220 InitFlags flags);
221 void initializePropertyCache(
222 int index, QQmlData *ddata, const QV4::ResolvedTypeReference *typeRef);
223 void initializeParent(QObject *instance, QObject *parent);
224 QObject *populateInstanceAndAliasBindings(int index, QObject *instance, InitFlags flags);
225
226 QObject *createInstance(int index, QObject *parent = nullptr, bool isContextObject = false);
227
228 QObject *initializeComponent(
229 const QV4::CompiledData::Object *obj, QObject *instance, InitFlags flags);
230 QObject *initializeComposite(
231 int index, const QV4::CompiledData::Object *obj,
232 const QV4::ResolvedTypeReference *typeRef, QObject *instance, QObject *parent,
233 InitFlags flags);
234 QObject *initializeNonComposite(
235 int index, const QV4::CompiledData::Object *obj,
236 const QV4::ResolvedTypeReference *typeRef, QObject *instance, QObject *parent,
237 InitFlags flags);
238
239 bool populateInstance(int index, QObject *instance, QObject *bindingTarget,
240 const QQmlPropertyData *valueTypeProperty,
241 const QV4::CompiledData::Binding *binding = nullptr);
242
243 // If qmlProperty and binding are null, populate all properties, otherwise only the given one.
244 void populateDeferred(QObject *instance, int deferredIndex);
245 void populateDeferred(QObject *instance, int deferredIndex,
246 const QQmlPropertyPrivate *qmlProperty,
247 const QV4::CompiledData::Binding *binding);
248
249 enum BindingMode {
250 ApplyNone = 0x0,
251 ApplyImmediate = 0x1,
252 ApplyDeferred = 0x2,
253 ApplyAll = ApplyImmediate | ApplyDeferred,
254 };
255 Q_DECLARE_FLAGS(BindingSetupFlags, BindingMode);
256
257 void setupBindings(BindingSetupFlags mode = BindingMode::ApplyImmediate);
258 bool setPropertyBinding(const QQmlPropertyData *property, const QV4::CompiledData::Binding *binding);
259 void setPropertyValue(const QQmlPropertyData *property, const QV4::CompiledData::Binding *binding);
260 void setupFunctions();
261
262 QString stringAt(int idx) const { return compilationUnit->stringAt(idx); }
263 void recordError(const QV4::CompiledData::Location &location, const QString &description);
264
265 void registerObjectWithContextById(const QV4::CompiledData::Object *object, QObject *instance) const;
266
267 inline QV4::QmlContext *currentQmlContext();
268 QV4::ResolvedTypeReference *resolvedType(int id) const
269 {
270 return compilationUnit->resolvedType(id);
271 }
272
273 enum Phase {
274 Startup,
275 CreatingObjects,
276 CreatingObjectsPhase2,
277 ObjectsCreated,
278 Finalizing,
279 Done
280 } phase;
281
282 QQmlEngine *engine;
283 QV4::ExecutionEngine *v4;
284 QString m_inlineComponentName;
285 QQmlRefPointer<QV4::ExecutableCompilationUnit> compilationUnit;
286 const QV4::CompiledData::Unit *qmlUnit;
287 QQmlGuardedContextData parentContext;
288 QQmlRefPointer<QQmlContextData> context;
289 const QQmlPropertyCacheVector *propertyCaches;
290 QQmlRefPointer<QQmlObjectCreatorSharedState> sharedState;
291 bool topLevelCreator;
292 bool isContextObject;
293 QQmlIncubatorPrivate *incubator;
294
295 QObject *_qobject;
296 QObject *_scopeObject;
297 QObject *_bindingTarget;
298
299 const QQmlPropertyData *_valueTypeProperty; // belongs to _qobjectForBindings's property cache
300 int _compiledObjectIndex;
301 const QV4::CompiledData::Object *_compiledObject;
302 QQmlData *_ddata;
303 QQmlPropertyCache::ConstPtr _propertyCache;
304 QQmlVMEMetaObject *_vmeMetaObject;
305 QQmlListProperty<QObject> _currentList;
306 QV4::QmlContext *_qmlContext;
307
309
310 typedef std::function<bool(QQmlObjectCreatorSharedState *sharedState)> PendingAliasBinding;
311 std::vector<PendingAliasBinding> pendingAliasBindings;
312
313 template<typename Functor>
314 void doPopulateDeferred(QObject *instance, int deferredIndex, Functor f)
315 {
316 QQmlData *declarativeData = QQmlData::get(instance);
317
318 // We're in the process of creating the object. We sure hope it's still alive.
319 Q_ASSERT(declarativeData && declarativeData->propertyCache);
320
321 QObject *bindingTarget = instance;
322
323 QQmlPropertyCache::ConstPtr cache = declarativeData->propertyCache;
324 QQmlVMEMetaObject *vmeMetaObject = QQmlVMEMetaObject::get(instance);
325
326 QObject *scopeObject = instance;
327 qt_ptr_swap(_scopeObject, scopeObject);
328
329 QV4::Scope valueScope(v4);
330 QScopedValueRollback<ObjectInCreationGCAnchorList> jsObjectGuard(
331 sharedState->allJavaScriptObjects, ObjectInCreationGCAnchorList(valueScope));
332
333 Q_ASSERT(topLevelCreator);
334 QV4::QmlContext *qmlContext = static_cast<QV4::QmlContext *>(valueScope.constructUndefined(1));
335
336 qt_ptr_swap(_qmlContext, qmlContext);
337
338 _propertyCache.swap(cache);
339 qt_ptr_swap(_qobject, instance);
340
341 int objectIndex = deferredIndex;
342 std::swap(_compiledObjectIndex, objectIndex);
343
344 const QV4::CompiledData::Object *obj = compilationUnit->objectAt(_compiledObjectIndex);
345 qt_ptr_swap(_compiledObject, obj);
346 qt_ptr_swap(_ddata, declarativeData);
347 qt_ptr_swap(_bindingTarget, bindingTarget);
348 qt_ptr_swap(_vmeMetaObject, vmeMetaObject);
349
350 f();
351
352 qt_ptr_swap(_vmeMetaObject, vmeMetaObject);
353 qt_ptr_swap(_bindingTarget, bindingTarget);
354 qt_ptr_swap(_ddata, declarativeData);
355 qt_ptr_swap(_compiledObject, obj);
356 std::swap(_compiledObjectIndex, objectIndex);
357 qt_ptr_swap(_qobject, instance);
358 _propertyCache.swap(cache);
359
360 qt_ptr_swap(_qmlContext, qmlContext);
361 qt_ptr_swap(_scopeObject, scopeObject);
362 }
363 void registerPostHocRequiredProperties(const QV4::CompiledData::Binding *binding);
364};
365
367{
368 QQmlObjectCreatorRecursionWatcher(QQmlObjectCreator *creator);
369
370 bool hasRecursed() const { return watcher.hasRecursed(); }
371
372private:
373 QQmlRefPointer<QQmlObjectCreatorSharedState> sharedState;
374 QRecursionWatcher<QQmlObjectCreatorSharedState, &QQmlObjectCreatorSharedState::recursionNode> watcher;
375};
376
377QV4::QmlContext *QQmlObjectCreator::currentQmlContext()
378{
379 if (!_qmlContext->isManaged())
380 _qmlContext->setM(QV4::QmlContext::create(v4->rootContext(), context, _scopeObject));
381
382 return _qmlContext;
383}
384
385Q_DECLARE_OPERATORS_FOR_FLAGS(QQmlObjectCreator::InitFlags);
386
387QT_END_NAMESPACE
388
389#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 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)