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 QtQml module 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 | |
40 | #ifndef QQMLVME_P_H |
41 | #define QQMLVME_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists purely as an |
48 | // implementation detail. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include "qqmlerror.h" |
55 | #include <private/qbitfield_p.h> |
56 | #include <private/qrecursionwatcher_p.h> |
57 | |
58 | #include <QtCore/QStack> |
59 | #include <QtCore/QString> |
60 | #include <QtCore/qelapsedtimer.h> |
61 | #include <QtCore/qcoreapplication.h> |
62 | #include <QtCore/qtypeinfo.h> |
63 | |
64 | #include <private/qqmlengine_p.h> |
65 | #include <private/qfinitestack_p.h> |
66 | |
67 | #include <atomic> |
68 | |
69 | QT_BEGIN_NAMESPACE |
70 | |
71 | class QObject; |
72 | class QJSValue; |
73 | class QQmlScriptData; |
74 | class QQmlContextData; |
75 | |
76 | namespace QQmlVMETypes { |
77 | struct List |
78 | { |
79 | List() : type(0) {} |
80 | List(int t) : type(t) {} |
81 | |
82 | int type; |
83 | QQmlListProperty<void> qListProperty; |
84 | }; |
85 | struct State { |
86 | enum Flag { Deferred = 0x00000001 }; |
87 | |
88 | State() : flags(0), context(nullptr), instructionStream(nullptr) {} |
89 | quint32 flags; |
90 | QQmlContextData *context; |
91 | const char *instructionStream; |
92 | QBitField bindingSkipList; |
93 | }; |
94 | } |
95 | Q_DECLARE_TYPEINFO(QQmlVMETypes::List, Q_PRIMITIVE_TYPE | Q_MOVABLE_TYPE); |
96 | template<> |
97 | class QTypeInfo<QQmlVMETypes::State> : public QTypeInfoMerger<QQmlVMETypes::State, QBitField> {}; //Q_DECLARE_TYPEINFO |
98 | |
99 | class QQmlInstantiationInterrupt { |
100 | public: |
101 | inline QQmlInstantiationInterrupt(); |
102 | // ### Qt 6: remove |
103 | inline QQmlInstantiationInterrupt(volatile bool *runWhile, qint64 nsecs=0); |
104 | inline QQmlInstantiationInterrupt(std::atomic<bool> *runWhile, qint64 nsecs = 0); |
105 | inline QQmlInstantiationInterrupt(qint64 nsecs); |
106 | |
107 | inline void reset(); |
108 | inline bool shouldInterrupt() const; |
109 | private: |
110 | enum Mode { None, Time, LegacyFlag, Flag }; // ### Qt 6: remove LegacyFlag |
111 | Mode mode; |
112 | QElapsedTimer timer; |
113 | qint64 nsecs = 0; |
114 | volatile bool *runWhileLegacy = nullptr; // ### Qt 6: remove |
115 | std::atomic<bool> *runWhile = nullptr; |
116 | }; |
117 | |
118 | class Q_QML_PRIVATE_EXPORT QQmlVME |
119 | { |
120 | public: |
121 | static void enableComponentComplete(); |
122 | static void disableComponentComplete(); |
123 | static bool componentCompleteEnabled(); |
124 | |
125 | private: |
126 | static bool s_enableComponentComplete; |
127 | }; |
128 | |
129 | // Used to check that a QQmlVME that is interrupted mid-execution |
130 | // is still valid. Checks all the objects and contexts have not been |
131 | // deleted. |
132 | // |
133 | // VME stands for Virtual Machine Execution. QML files used to |
134 | // be compiled to a byte code data structure that a virtual machine executed |
135 | // (for constructing the tree of QObjects and setting properties). |
136 | class QQmlVMEGuard |
137 | { |
138 | public: |
139 | QQmlVMEGuard(); |
140 | ~QQmlVMEGuard(); |
141 | |
142 | void guard(QQmlObjectCreator *); |
143 | void clear(); |
144 | |
145 | bool isOK() const; |
146 | |
147 | private: |
148 | int m_objectCount; |
149 | QPointer<QObject> *m_objects; |
150 | int m_contextCount; |
151 | QQmlGuardedContextData *m_contexts; |
152 | }; |
153 | |
154 | QQmlInstantiationInterrupt::QQmlInstantiationInterrupt() |
155 | : mode(None) |
156 | { |
157 | } |
158 | |
159 | QQmlInstantiationInterrupt::QQmlInstantiationInterrupt(volatile bool *runWhile, qint64 nsecs) |
160 | : mode(LegacyFlag), nsecs(nsecs), runWhileLegacy(runWhile) |
161 | { |
162 | } |
163 | |
164 | QQmlInstantiationInterrupt::QQmlInstantiationInterrupt(std::atomic<bool> *runWhile, qint64 nsecs) |
165 | : mode(Flag), nsecs(nsecs), runWhile(runWhile) |
166 | { |
167 | } |
168 | |
169 | QQmlInstantiationInterrupt::QQmlInstantiationInterrupt(qint64 nsecs) |
170 | : mode(Time), nsecs(nsecs) |
171 | { |
172 | } |
173 | |
174 | void QQmlInstantiationInterrupt::reset() |
175 | { |
176 | if (mode == Time || nsecs) |
177 | timer.start(); |
178 | } |
179 | |
180 | bool QQmlInstantiationInterrupt::shouldInterrupt() const |
181 | { |
182 | switch (mode) { |
183 | case None: |
184 | return false; |
185 | case Time: |
186 | return timer.nsecsElapsed() > nsecs; |
187 | case LegacyFlag: |
188 | return !*runWhileLegacy || (nsecs && timer.nsecsElapsed() > nsecs); |
189 | case Flag: |
190 | return !runWhile->load(m: std::memory_order_acquire) || (nsecs && timer.nsecsElapsed() > nsecs); |
191 | } |
192 | Q_UNREACHABLE(); |
193 | return false; |
194 | } |
195 | |
196 | QT_END_NAMESPACE |
197 | |
198 | #endif // QQMLVME_P_H |
199 | |