1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the tools applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39#ifndef QQMLOBJECTCREATOR_P_H
40#define QQMLOBJECTCREATOR_P_H
41
42//
43// W A R N I N G
44// -------------
45//
46// This file is not part of the Qt API. It exists purely as an
47// implementation detail. This header file may change from version to
48// version without notice, or even be removed.
49//
50// We mean it.
51//
52
53#include <private/qqmlimport_p.h>
54#include <private/qqmltypenamecache_p.h>
55#include <private/qv4compileddata_p.h>
56#include <private/qfinitestack_p.h>
57#include <private/qrecursionwatcher_p.h>
58#include <private/qqmlprofiler_p.h>
59#include <private/qv4qmlcontext_p.h>
60
61#include <qpointer.h>
62
63QT_BEGIN_NAMESPACE
64
65class QQmlAbstractBinding;
66class QQmlInstantiationInterrupt;
67class QQmlIncubatorPrivate;
68
69struct QQmlObjectCreatorSharedState : public QSharedData
70{
71 QQmlContextData *rootContext;
72 QQmlContextData *creationContext;
73 QFiniteStack<QQmlAbstractBinding::Ptr> allCreatedBindings;
74 QFiniteStack<QQmlParserStatus*> allParserStatusCallbacks;
75 QFiniteStack<QPointer<QObject> > allCreatedObjects;
76 QV4::Value *allJavaScriptObjects; // pointer to vector on JS stack to reference JS wrappers during creation phase.
77 QQmlComponentAttached *componentAttached;
78 QList<QQmlEnginePrivate::FinalizeCallback> finalizeCallbacks;
79 QQmlVmeProfiler profiler;
80 QRecursionNode recursionNode;
81};
82
83class Q_QML_PRIVATE_EXPORT QQmlObjectCreator
84{
85 Q_DECLARE_TR_FUNCTIONS(QQmlObjectCreator)
86public:
87 QQmlObjectCreator(QQmlContextData *parentContext, const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit, QQmlContextData *creationContext, QQmlIncubatorPrivate *incubator = nullptr);
88 ~QQmlObjectCreator();
89
90 QObject *create(int subComponentIndex = -1, QObject *parent = nullptr, QQmlInstantiationInterrupt *interrupt = nullptr);
91 bool populateDeferredProperties(QObject *instance, QQmlData::DeferredData *deferredData);
92 bool populateDeferredBinding(const QQmlProperty &qmlProperty, QQmlData::DeferredData *deferredData, const QV4::CompiledData::Binding *binding);
93 QQmlContextData *finalize(QQmlInstantiationInterrupt &interrupt);
94 void clear();
95
96 QQmlComponentAttached **componentAttachment() { return &sharedState->componentAttached; }
97
98 QList<QQmlEnginePrivate::FinalizeCallback> *finalizeCallbacks() { return &sharedState->finalizeCallbacks; }
99
100 QList<QQmlError> errors;
101
102 QQmlContextData *parentContextData() const { return parentContext.contextData(); }
103 QFiniteStack<QPointer<QObject> > &allCreatedObjects() { return sharedState->allCreatedObjects; }
104
105private:
106 QQmlObjectCreator(QQmlContextData *contextData, const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit, QQmlObjectCreatorSharedState *inheritedSharedState);
107
108 void init(QQmlContextData *parentContext);
109
110 QObject *createInstance(int index, QObject *parent = nullptr, bool isContextObject = false);
111
112 bool populateInstance(int index, QObject *instance,
113 QObject *bindingTarget, const QQmlPropertyData *valueTypeProperty);
114
115 void setupBindings(bool applyDeferredBindings = false);
116 bool setPropertyBinding(const QQmlPropertyData *property, const QV4::CompiledData::Binding *binding);
117 void setPropertyValue(const QQmlPropertyData *property, const QV4::CompiledData::Binding *binding);
118 void setupFunctions();
119
120 QString stringAt(int idx) const { return compilationUnit->stringAt(idx); }
121 void recordError(const QV4::CompiledData::Location &location, const QString &description);
122
123 void registerObjectWithContextById(const QV4::CompiledData::Object *object, QObject *instance) const;
124
125 inline QV4::QmlContext *currentQmlContext();
126 Q_NEVER_INLINE void createQmlContext();
127 QV4::ResolvedTypeReference *resolvedType(int id) const
128 {
129 return compilationUnit->resolvedType(id);
130 }
131
132 enum Phase {
133 Startup,
134 CreatingObjects,
135 CreatingObjectsPhase2,
136 ObjectsCreated,
137 Finalizing,
138 Done
139 } phase;
140
141 QQmlEngine *engine;
142 QV4::ExecutionEngine *v4;
143 QQmlRefPointer<QV4::ExecutableCompilationUnit> compilationUnit;
144 const QV4::CompiledData::Unit *qmlUnit;
145 QQmlGuardedContextData parentContext;
146 QQmlContextData *context;
147 const QQmlPropertyCacheVector *propertyCaches;
148 QExplicitlySharedDataPointer<QQmlObjectCreatorSharedState> sharedState;
149 bool topLevelCreator;
150 QQmlIncubatorPrivate *incubator;
151
152 QObject *_qobject;
153 QObject *_scopeObject;
154 QObject *_bindingTarget;
155
156 const QQmlPropertyData *_valueTypeProperty; // belongs to _qobjectForBindings's property cache
157 int _compiledObjectIndex;
158 const QV4::CompiledData::Object *_compiledObject;
159 QQmlData *_ddata;
160 QQmlRefPointer<QQmlPropertyCache> _propertyCache;
161 QQmlVMEMetaObject *_vmeMetaObject;
162 QQmlListProperty<void> _currentList;
163 QV4::QmlContext *_qmlContext;
164
165 friend struct QQmlObjectCreatorRecursionWatcher;
166
167 typedef std::function<bool(QQmlObjectCreatorSharedState *sharedState)> PendingAliasBinding;
168 std::vector<PendingAliasBinding> pendingAliasBindings;
169};
170
171struct QQmlObjectCreatorRecursionWatcher
172{
173 QQmlObjectCreatorRecursionWatcher(QQmlObjectCreator *creator);
174
175 bool hasRecursed() const { return watcher.hasRecursed(); }
176
177private:
178 QExplicitlySharedDataPointer<QQmlObjectCreatorSharedState> sharedState;
179 QRecursionWatcher<QQmlObjectCreatorSharedState, &QQmlObjectCreatorSharedState::recursionNode> watcher;
180};
181
182QV4::QmlContext *QQmlObjectCreator::currentQmlContext()
183{
184 if (!_qmlContext->isManaged())
185 _qmlContext->setM(QV4::QmlContext::create(v4->rootContext(), context, _scopeObject));
186
187 return _qmlContext;
188}
189
190QT_END_NAMESPACE
191
192#endif // QQMLOBJECTCREATOR_P_H
193