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#ifndef QQMLOBJECTCREATOR_P_H
4#define QQMLOBJECTCREATOR_P_H
5
6//
7// W A R N I N G
8// -------------
9//
10// This file is not part of the Qt API. It exists purely as an
11// implementation detail. This header file may change from version to
12// version without notice, or even be removed.
13//
14// We mean it.
15//
16
17#include <private/qqmlimport_p.h>
18#include <private/qqmltypenamecache_p.h>
19#include <private/qv4compileddata_p.h>
20#include <private/qrecursionwatcher_p.h>
21#include <private/qqmlprofiler_p.h>
22#include <private/qv4qmlcontext_p.h>
23#include <private/qqmlguardedcontextdata_p.h>
24#include <private/qqmlfinalizer_p.h>
25#include <private/qqmlvmemetaobject_p.h>
26
27#include <qpointer.h>
28#include <deque>
29
30QT_BEGIN_NAMESPACE
31
32class QQmlAbstractBinding;
35
40
41/*!
42\internal
43This struct contains information solely used for displaying error messages
44\variable aliasesToRequired allows us to give the user a way to know which (aliasing) properties
45can be set to set the required property
46\sa QQmlComponentPrivate::unsetRequiredPropertyToQQmlError
47*/
55
57{
59 RequiredPropertyKey(const QObject *object, const QQmlPropertyData *data)
60 : object(object)
61 , data(data)
62 {}
63
64 const QObject *object = nullptr;
65 const QQmlPropertyData *data = nullptr;
66
67private:
68 friend size_t qHash(const RequiredPropertyKey &key, size_t seed = 0)
69 {
70 return qHashMulti(seed, key.object, key.data);
71 }
72
73 friend bool operator==(const RequiredPropertyKey &a, const RequiredPropertyKey &b)
74 {
75 return a.object == b.object && a.data == b.data;
76 }
77};
78
80
91
97
98class Q_AUTOTEST_EXPORT ObjectInCreationGCAnchorList {
99public:
100 // this is a non owning view, rule of zero applies
101 ObjectInCreationGCAnchorList() = default;
102 ObjectInCreationGCAnchorList(const QV4::Scope &scope)
103 {
104 allocationScope = &scope;
105 }
106 void trackObject(QV4::ExecutionEngine *engine, QObject *instance);
107 bool canTrack() const { return allocationScope; }
108private:
109 // pointer to scope to reference JS wrappers during creation phase.
110 const QV4::Scope *allocationScope = nullptr;
111};
112
117
134
135class Q_QML_EXPORT QQmlObjectCreator
136{
137 Q_DECLARE_TR_FUNCTIONS(QQmlObjectCreator)
138public:
139 QQmlObjectCreator(const QQmlRefPointer<QQmlContextData> &parentContext,
140 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit,
141 const QQmlRefPointer<QQmlContextData> &creationContext,
142 const QString &inlineComponentName,
143 QQmlIncubatorPrivate *incubator = nullptr);
144 ~QQmlObjectCreator();
145
147 QObject *create(int subComponentIndex = -1, QObject *parent = nullptr,
148 QQmlInstantiationInterrupt *interrupt = nullptr, int flags = NormalObject);
149
150 bool populateDeferredProperties(QObject *instance, const QQmlData::DeferredData *deferredData);
151
152 void beginPopulateDeferred(const QQmlRefPointer<QQmlContextData> &context);
153 void populateDeferredBinding(const QQmlProperty &qmlProperty, int deferredIndex,
154 const QV4::CompiledData::Binding *binding);
155 void populateDeferredInstance(QObject *outerObject, int deferredIndex,
156 int index, QObject *instance, QObject *bindingTarget,
157 const QQmlPropertyData *valueTypeProperty,
158 const QV4::CompiledData::Binding *binding = nullptr);
160
161 bool finalize(QQmlInstantiationInterrupt &interrupt);
162 void clear();
163
164 QQmlRefPointer<QQmlContextData> rootContext() const { return sharedState->rootContext; }
165 QQmlComponentAttached **componentAttachment() { return &sharedState->componentAttached; }
166
168
170 {
171 return parentContext.contextData();
172 }
173 std::vector<QQmlGuard<QObject> > &allCreatedObjects() { return sharedState->allCreatedObjects; }
174
175 RequiredProperties *requiredProperties() {return &sharedState->requiredProperties;}
176 bool componentHadTopLevelRequiredProperties() const {return sharedState->hadTopLevelRequiredProperties;}
177
178 static QQmlComponent *createComponent(
179 QQmlEngine *engine, QV4::ExecutableCompilationUnit *compilationUnit, int index,
180 QObject *parent, const QQmlRefPointer<QQmlContextData> &context);
181
182 void removePendingBinding(QObject *target, int propertyIndex) const
183 {
184 QList<DeferredQPropertyBinding> &pendingBindings = sharedState.data()->allQPropertyBindings;
185 pendingBindings.removeIf([&](const DeferredQPropertyBinding &deferred) {
186 return deferred.properyIndex == propertyIndex && deferred.target == target;
187 });
188 }
189
190private:
191 QQmlObjectCreator(
192 const QQmlRefPointer<QQmlContextData> &contextData,
193 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit,
194 const QString inlineComponentName,
195 QQmlObjectCreatorSharedState *inheritedSharedState, bool isContextObject);
196
197 void init(const QQmlRefPointer<QQmlContextData> &parentContext);
198
199 QObject *createInstance(int index, QObject *parent = nullptr, bool isContextObject = false);
200
201 bool populateInstance(int index, QObject *instance, QObject *bindingTarget,
202 const QQmlPropertyData *valueTypeProperty,
203 const QV4::CompiledData::Binding *binding = nullptr);
204
205 // If qmlProperty and binding are null, populate all properties, otherwise only the given one.
206 void populateDeferred(QObject *instance, int deferredIndex);
207 void populateDeferred(QObject *instance, int deferredIndex,
208 const QQmlPropertyPrivate *qmlProperty,
209 const QV4::CompiledData::Binding *binding);
210
211 enum BindingMode {
212 ApplyNone = 0x0,
213 ApplyImmediate = 0x1,
214 ApplyDeferred = 0x2,
215 ApplyAll = ApplyImmediate | ApplyDeferred,
216 };
217 Q_DECLARE_FLAGS(BindingSetupFlags, BindingMode);
218
219 void setupBindings(BindingSetupFlags mode = BindingMode::ApplyImmediate);
220 bool setPropertyBinding(const QQmlPropertyData *property, const QV4::CompiledData::Binding *binding);
221 void setPropertyValue(const QQmlPropertyData *property, const QV4::CompiledData::Binding *binding);
222 void setupFunctions();
223
224 QString stringAt(int idx) const { return compilationUnit->stringAt(idx); }
225 void recordError(const QV4::CompiledData::Location &location, const QString &description);
226
227 void registerObjectWithContextById(const QV4::CompiledData::Object *object, QObject *instance) const;
228
229 inline QV4::QmlContext *currentQmlContext();
230 QV4::ResolvedTypeReference *resolvedType(int id) const
231 {
232 return compilationUnit->resolvedType(id);
233 }
234
235 enum Phase {
236 Startup,
237 CreatingObjects,
238 CreatingObjectsPhase2,
239 ObjectsCreated,
240 Finalizing,
241 Done
242 } phase;
243
244 QQmlEngine *engine;
245 QV4::ExecutionEngine *v4;
246 QString m_inlineComponentName;
247 QQmlRefPointer<QV4::ExecutableCompilationUnit> compilationUnit;
248 const QV4::CompiledData::Unit *qmlUnit;
249 QQmlGuardedContextData parentContext;
250 QQmlRefPointer<QQmlContextData> context;
251 const QQmlPropertyCacheVector *propertyCaches;
252 QQmlRefPointer<QQmlObjectCreatorSharedState> sharedState;
253 bool topLevelCreator;
254 bool isContextObject;
255 QQmlIncubatorPrivate *incubator;
256
257 QObject *_qobject;
258 QObject *_scopeObject;
259 QObject *_bindingTarget;
260
261 const QQmlPropertyData *_valueTypeProperty; // belongs to _qobjectForBindings's property cache
262 int _compiledObjectIndex;
263 const QV4::CompiledData::Object *_compiledObject;
264 QQmlData *_ddata;
265 QQmlPropertyCache::ConstPtr _propertyCache;
266 QQmlVMEMetaObject *_vmeMetaObject;
267 QQmlListProperty<void> _currentList;
268 QV4::QmlContext *_qmlContext;
269
271
272 typedef std::function<bool(QQmlObjectCreatorSharedState *sharedState)> PendingAliasBinding;
273 std::vector<PendingAliasBinding> pendingAliasBindings;
274
275 template<typename Functor>
276 void doPopulateDeferred(QObject *instance, int deferredIndex, Functor f)
277 {
278 QQmlData *declarativeData = QQmlData::get(instance);
279
280 // We're in the process of creating the object. We sure hope it's still alive.
281 Q_ASSERT(declarativeData && declarativeData->propertyCache);
282
283 QObject *bindingTarget = instance;
284
285 QQmlPropertyCache::ConstPtr cache = declarativeData->propertyCache;
286 QQmlVMEMetaObject *vmeMetaObject = QQmlVMEMetaObject::get(instance);
287
288 QObject *scopeObject = instance;
289 qt_ptr_swap(_scopeObject, scopeObject);
290
291 QV4::Scope valueScope(v4);
292 QScopedValueRollback<ObjectInCreationGCAnchorList> jsObjectGuard(
293 sharedState->allJavaScriptObjects, ObjectInCreationGCAnchorList(valueScope));
294
295 Q_ASSERT(topLevelCreator);
296 QV4::QmlContext *qmlContext = static_cast<QV4::QmlContext *>(valueScope.constructUndefined(1));
297
298 qt_ptr_swap(_qmlContext, qmlContext);
299
300 _propertyCache.swap(cache);
301 qt_ptr_swap(_qobject, instance);
302
303 int objectIndex = deferredIndex;
304 std::swap(_compiledObjectIndex, objectIndex);
305
306 const QV4::CompiledData::Object *obj = compilationUnit->objectAt(_compiledObjectIndex);
307 qt_ptr_swap(_compiledObject, obj);
308 qt_ptr_swap(_ddata, declarativeData);
309 qt_ptr_swap(_bindingTarget, bindingTarget);
310 qt_ptr_swap(_vmeMetaObject, vmeMetaObject);
311
312 f();
313
314 qt_ptr_swap(_vmeMetaObject, vmeMetaObject);
315 qt_ptr_swap(_bindingTarget, bindingTarget);
316 qt_ptr_swap(_ddata, declarativeData);
317 qt_ptr_swap(_compiledObject, obj);
318 std::swap(_compiledObjectIndex, objectIndex);
319 qt_ptr_swap(_qobject, instance);
320 _propertyCache.swap(cache);
321
322 qt_ptr_swap(_qmlContext, qmlContext);
323 qt_ptr_swap(_scopeObject, scopeObject);
324 }
325};
326
328{
329 QQmlObjectCreatorRecursionWatcher(QQmlObjectCreator *creator);
330
331 bool hasRecursed() const { return watcher.hasRecursed(); }
332
333private:
334 QQmlRefPointer<QQmlObjectCreatorSharedState> sharedState;
335 QRecursionWatcher<QQmlObjectCreatorSharedState, &QQmlObjectCreatorSharedState::recursionNode> watcher;
336};
337
338QV4::QmlContext *QQmlObjectCreator::currentQmlContext()
339{
340 if (!_qmlContext->isManaged())
341 _qmlContext->setM(QV4::QmlContext::create(v4->rootContext(), context, _scopeObject));
342
343 return _qmlContext;
344}
345
346QT_END_NAMESPACE
347
348#endif // QQMLOBJECTCREATOR_P_H
friend class QQmlIncubatorPrivate
QQmlComponentAttached ** componentAttachment()
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)
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
Definition qjsvalue.h:23
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
QVector< AliasToRequiredInfo > aliasesToRequired
QV4::CompiledData::Location location
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)