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