QDeclarativeIncubator::clear() and autotests
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativevme_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVEVME_P_H
43 #define QDECLARATIVEVME_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include "qdeclarativeerror.h"
57 #include "private/qbitfield_p.h"
58 #include "private/qdeclarativeinstruction_p.h"
59
60 #include <QtCore/QStack>
61 #include <QtCore/QString>
62 #include <QtCore/qelapsedtimer.h>
63 #include <QtCore/qcoreapplication.h>
64
65 #include <private/qv8_p.h>
66 #include <private/qdeclarativeengine_p.h>
67 #include <private/qfinitestack_p.h>
68
69 QT_BEGIN_NAMESPACE
70
71 class QObject;
72 class QJSValue;
73 class QDeclarativeScriptData;
74 class QDeclarativeCompiledData;
75 class QDeclarativeCompiledData;
76 class QDeclarativeContextData;
77
78 namespace QDeclarativeVMETypes {
79     struct List
80     {
81         List() : type(0) {}
82         List(int t) : type(t) {}
83
84         int type;
85         QDeclarativeListProperty<void> qListProperty;
86     };
87 }
88 Q_DECLARE_TYPEINFO(QDeclarativeVMETypes::List, Q_PRIMITIVE_TYPE  | Q_MOVABLE_TYPE);
89
90 class QDeclarativeVME
91 {
92     Q_DECLARE_TR_FUNCTIONS(QDeclarativeVME)
93 public:
94     class Interrupt {
95     public:
96         inline Interrupt();
97         inline Interrupt(bool *runWhile);
98         inline Interrupt(int nsecs);
99
100         inline void reset();
101         inline bool shouldInterrupt() const;
102     private:
103         enum Mode { None, Time, Flag };
104         Mode mode;
105         union {
106             struct {
107                 QElapsedTimer timer;
108                 int nsecs;
109             };
110             bool *runWhile;
111         };
112     };
113
114     QDeclarativeVME() : data(0), componentAttached(0) {}
115     QDeclarativeVME(void *data) : data(data), componentAttached(0) {}
116
117     void *data;
118     QDeclarativeComponentAttached *componentAttached;
119     QList<QDeclarativeEnginePrivate::FinalizeCallback> finalizeCallbacks;
120
121     void init(QDeclarativeContextData *, QDeclarativeCompiledData *, int start);
122     bool initDeferred(QObject *);
123     void reset();
124
125     QObject *execute(QList<QDeclarativeError> *errors, const Interrupt & = Interrupt());
126     bool complete(const Interrupt & = Interrupt());
127
128 private:
129     friend class QDeclarativeVMEGuard;
130
131     QObject *run(QList<QDeclarativeError> *errors, const Interrupt &
132 #ifdef QML_THREADED_VME_INTERPRETER
133                  , void ***storeJumpTable = 0
134 #endif
135                 );
136     v8::Persistent<v8::Object> run(QDeclarativeContextData *, QDeclarativeScriptData *);
137
138 #ifdef QML_THREADED_VME_INTERPRETER
139     static void **instructionJumpTable();
140     friend class QDeclarativeCompiledData;
141 #endif
142
143     QDeclarativeEngine *engine;
144
145     QFiniteStack<QObject *> objects;
146     QFiniteStack<QDeclarativeVMETypes::List> lists;
147
148     QFiniteStack<QDeclarativeAbstractBinding *> bindValues;
149     QFiniteStack<QDeclarativeParserStatus *> parserStatus;
150     QDeclarativeGuardedContextData rootContext;
151
152     struct State {
153         enum Flag { Deferred = 0x00000001 };
154
155         State() : flags(0), context(0), compiledData(0), instructionStream(0) {}
156         quint32 flags;
157         QDeclarativeContextData *context;
158         QDeclarativeCompiledData *compiledData;
159         const char *instructionStream;
160         QBitField bindingSkipList;
161     };
162
163     QStack<State> states;
164
165     static void blank(QFiniteStack<QDeclarativeParserStatus *> &);
166     static void blank(QFiniteStack<QDeclarativeAbstractBinding *> &);
167 };
168
169 // Used to check that a QDeclarativeVME that is interrupted mid-execution
170 // is still valid.  Checks all the objects and contexts have not been 
171 // deleted.
172 class QDeclarativeVMEGuard
173 {
174 public:
175     QDeclarativeVMEGuard();
176     ~QDeclarativeVMEGuard();
177
178     void guard(QDeclarativeVME *);
179     void clear();
180
181     bool isOK() const;
182
183 private:
184     int m_objectCount;
185     QDeclarativeGuard<QObject> *m_objects;
186     int m_contextCount;
187     QDeclarativeGuardedContextData *m_contexts;
188 };
189
190 QDeclarativeVME::Interrupt::Interrupt()
191 : mode(None)
192 {
193 }
194
195 QDeclarativeVME::Interrupt::Interrupt(bool *runWhile)
196 : mode(Flag), runWhile(runWhile)
197 {
198 }
199
200 QDeclarativeVME::Interrupt::Interrupt(int nsecs)
201 : mode(Time), nsecs(nsecs)
202 {
203 }
204
205 void QDeclarativeVME::Interrupt::reset()
206 {
207     if (mode == Time) 
208         timer.start();
209 }
210
211 bool QDeclarativeVME::Interrupt::shouldInterrupt() const
212 {
213     if (mode == None) {
214         return false;
215     } else if (mode == Time) {
216         return timer.nsecsElapsed() > nsecs;
217     } else if (mode == Flag) {
218         return !*runWhile;
219     } else {
220         return false;
221     }
222 }
223
224 QT_END_NAMESPACE
225
226 #endif // QDECLARATIVEVME_P_H