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