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