Merge branch 'master' of git://gitorious.org/qt/qtdeclarative into api_changes
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmlexpression_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 QtQml 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 QQMLEXPRESSION_P_H
43 #define QQMLEXPRESSION_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 "qqmlexpression.h"
57
58 #include <private/qv8engine_p.h>
59 #include <private/qfieldlist_p.h>
60 #include <private/qflagpointer_p.h>
61 #include <private/qdeletewatcher_p.h>
62 #include <private/qqmlguard_p.h>
63 #include <private/qpointervaluepair_p.h>
64 #include <private/qqmlengine_p.h>
65
66 QT_BEGIN_NAMESPACE
67
68 class QQmlAbstractExpression
69 {
70 public:
71     QQmlAbstractExpression();
72     virtual ~QQmlAbstractExpression();
73
74     bool isValid() const;
75
76     QQmlContextData *context() const;
77     void setContext(QQmlContextData *);
78
79     virtual void refresh();
80
81     class DeleteWatcher {
82     public:
83         inline DeleteWatcher(QQmlAbstractExpression *);
84         inline ~DeleteWatcher();
85         inline bool wasDeleted() const;
86     private:
87         friend class QQmlAbstractExpression;
88         QQmlContextData *_c;
89         QQmlAbstractExpression **_w;
90         QQmlAbstractExpression *_s;
91     };
92
93 private:
94     friend class QQmlContext;
95     friend class QQmlContextData;
96     friend class QQmlContextPrivate;
97
98     QBiPointer<QQmlContextData, DeleteWatcher> m_context;
99     QQmlAbstractExpression **m_prevExpression;
100     QQmlAbstractExpression  *m_nextExpression;
101 };
102
103 class QQmlDelayedError 
104 {
105 public:
106     inline QQmlDelayedError() : nextError(0), prevError(0) {}
107     inline ~QQmlDelayedError() { removeError(); }
108
109     QQmlError error;
110
111     bool addError(QQmlEnginePrivate *);
112
113     inline void removeError() {
114         if (!prevError) return;
115         if (nextError) nextError->prevError = prevError;
116         *prevError = nextError;
117         nextError = 0;
118         prevError = 0;
119     }
120
121 private:
122     QQmlDelayedError  *nextError;
123     QQmlDelayedError **prevError;
124 };
125
126 class QQmlJavaScriptExpression
127 {
128 public:
129     // Although this looks crazy, we implement our own "vtable" here, rather than relying on
130     // C++ virtuals, to save memory.  By doing it ourselves, we can overload the storage
131     // location that is use for the vtable to also store the rarely used delayed error.
132     // If we use C++ virtuals, we can't do this and it consts us an extra sizeof(void *) in
133     // memory for every expression.
134     struct VTable {
135         QString (*expressionIdentifier)(QQmlJavaScriptExpression *);
136         void (*expressionChanged)(QQmlJavaScriptExpression *);
137     };
138
139     QQmlJavaScriptExpression(VTable *vtable);
140
141     v8::Local<v8::Value> evaluate(QQmlContextData *, v8::Handle<v8::Function>,
142                                   bool *isUndefined);
143
144     inline bool requiresThisObject() const;
145     inline void setRequiresThisObject(bool v);
146     inline bool useSharedContext() const;
147     inline void setUseSharedContext(bool v);
148     inline bool notifyOnValueChanged() const;
149
150     void setNotifyOnValueChanged(bool v);
151     void resetNotifyOnValueChanged();
152
153     inline QObject *scopeObject() const;
154     inline void setScopeObject(QObject *v);
155
156     class DeleteWatcher {
157     public:
158         inline DeleteWatcher(QQmlJavaScriptExpression *);
159         inline ~DeleteWatcher();
160         inline bool wasDeleted() const;
161     private:
162         friend class QQmlJavaScriptExpression;
163         QObject *_c;
164         QQmlJavaScriptExpression **_w;
165         QQmlJavaScriptExpression *_s;
166     };
167
168     inline bool hasError() const;
169     inline bool hasDelayedError() const;
170     QQmlError error() const;
171     void clearError();
172     QQmlDelayedError *delayedError();
173
174 protected:
175     ~QQmlJavaScriptExpression();
176
177 private:
178     typedef QQmlJavaScriptExpressionGuard Guard;
179     friend class QQmlJavaScriptExpressionGuard;
180
181     struct GuardCapture : public QQmlEnginePrivate::PropertyCapture {
182         GuardCapture(QQmlEngine *engine, QQmlJavaScriptExpression *e)
183         : engine(engine), expression(e), errorString(0) { }
184
185         ~GuardCapture()  {
186             Q_ASSERT(guards.isEmpty());
187             Q_ASSERT(errorString == 0);
188         }
189
190         virtual void captureProperty(QQmlNotifier *);
191         virtual void captureProperty(QObject *, int, int);
192
193         QQmlEngine *engine;
194         QQmlJavaScriptExpression *expression;
195         QFieldList<Guard, &Guard::next> guards;
196         QStringList *errorString;
197     };
198
199     QPointerValuePair<VTable, QQmlDelayedError> m_vtable;
200
201     // We store some flag bits in the following flag pointers.
202     //    m_scopeObject:flag1 - requiresThisObject
203     //    activeGuards:flag1  - notifyOnValueChanged
204     //    activeGuards:flag2  - useSharedContext
205     QBiPointer<QObject, DeleteWatcher> m_scopeObject;
206     QForwardFieldList<Guard, &Guard::next> activeGuards;
207
208     void clearGuards();
209 };
210
211 class QQmlExpression;
212 class QString;
213 class Q_QML_PRIVATE_EXPORT QQmlExpressionPrivate : public QObjectPrivate, public QQmlJavaScriptExpression, public QQmlAbstractExpression
214 {
215     Q_DECLARE_PUBLIC(QQmlExpression)
216 public:
217     QQmlExpressionPrivate();
218     ~QQmlExpressionPrivate();
219
220     void init(QQmlContextData *, const QString &, QObject *);
221     void init(QQmlContextData *, v8::Handle<v8::Function>, QObject *);
222     void init(QQmlContextData *, const QString &, bool, QObject *, const QString &, int, int);
223     void init(QQmlContextData *, const QByteArray &, bool, QObject *, const QString &, int, int);
224
225     QVariant value(QObject *secondaryScope = 0, bool *isUndefined = 0);
226
227     v8::Local<v8::Value> v8value(QObject *secondaryScope = 0, bool *isUndefined = 0);
228
229     static inline QQmlExpressionPrivate *get(QQmlExpression *expr);
230     static inline QQmlExpression *get(QQmlExpressionPrivate *expr);
231
232     void _q_notify();
233
234     static void exceptionToError(v8::Handle<v8::Message>, QQmlError &);
235     static v8::Persistent<v8::Function> evalFunction(QQmlContextData *ctxt, QObject *scope, 
236                                                      const QString &code, const QString &filename,
237                                                      int line,
238                                                      v8::Persistent<v8::Object> *qmlscope = 0);
239     static v8::Persistent<v8::Function> evalFunction(QQmlContextData *ctxt, QObject *scope,
240                                                      const char *code, int codeLength,
241                                                      const QString &filename, int line,
242                                                      v8::Persistent<v8::Object> *qmlscope = 0);
243
244     static QQmlExpression *create(QQmlContextData *, QObject *, const QString &, bool,
245                                           const QString &, int, int);
246
247     bool expressionFunctionValid:1;
248     bool expressionFunctionRewritten:1;
249     bool extractExpressionFromFunction:1;
250
251     // "Inherited" from QQmlJavaScriptExpression
252     static QString expressionIdentifier(QQmlJavaScriptExpression *);
253     static void expressionChanged(QQmlJavaScriptExpression *);
254     virtual void expressionChanged();
255
256     QString expression;
257     QByteArray expressionUtf8;
258
259     v8::Persistent<v8::Object> v8qmlscope;
260     v8::Persistent<v8::Function> v8function;
261
262     QString url; // This is a QString for a reason.  QUrls are slooooooow...
263     int line;
264     int column;
265     QString name; //function name, hint for the debugger
266
267     QQmlRefCount *dataRef;
268 };
269
270 QQmlAbstractExpression::DeleteWatcher::DeleteWatcher(QQmlAbstractExpression *e)
271 : _c(0), _w(0), _s(e)
272 {
273     if (e->m_context.isT1()) {
274         _w = &_s;
275         _c = e->m_context.asT1();
276         e->m_context = this;
277     } else {
278         // Another watcher is already registered
279         _w = &e->m_context.asT2()->_s;
280     }
281 }
282
283 QQmlAbstractExpression::DeleteWatcher::~DeleteWatcher()
284 {
285     Q_ASSERT(*_w == 0 || (*_w == _s && _s->m_context.isT2()));
286     if (*_w && _s->m_context.asT2() == this)
287         _s->m_context = _c;
288 }
289
290 bool QQmlAbstractExpression::DeleteWatcher::wasDeleted() const
291 {
292     return *_w == 0;
293 }
294
295 QQmlJavaScriptExpression::DeleteWatcher::DeleteWatcher(QQmlJavaScriptExpression *e)
296 : _c(0), _w(0), _s(e)
297 {
298     if (e->m_scopeObject.isT1()) {
299         _w = &_s;
300         _c = e->m_scopeObject.asT1();
301         e->m_scopeObject = this;
302     } else {
303         // Another watcher is already registered
304         _w = &e->m_scopeObject.asT2()->_s;
305     }
306 }
307
308 QQmlJavaScriptExpression::DeleteWatcher::~DeleteWatcher()
309 {
310     Q_ASSERT(*_w == 0 || (*_w == _s && _s->m_scopeObject.isT2()));
311     if (*_w && _s->m_scopeObject.asT2() == this)
312         _s->m_scopeObject = _c;
313 }
314
315 bool QQmlJavaScriptExpression::DeleteWatcher::wasDeleted() const
316 {
317     return *_w == 0;
318 }
319
320 bool QQmlJavaScriptExpression::requiresThisObject() const 
321
322     return m_scopeObject.flag();
323 }
324
325 void QQmlJavaScriptExpression::setRequiresThisObject(bool v) 
326
327     m_scopeObject.setFlagValue(v);
328 }
329
330 bool QQmlJavaScriptExpression::useSharedContext() const 
331
332     return activeGuards.flag2();
333 }
334
335 void QQmlJavaScriptExpression::setUseSharedContext(bool v) 
336
337     activeGuards.setFlag2Value(v);
338 }
339
340 bool QQmlJavaScriptExpression::notifyOnValueChanged() const 
341
342     return activeGuards.flag();
343 }
344
345 QObject *QQmlJavaScriptExpression::scopeObject() const 
346
347     if (m_scopeObject.isT1()) return m_scopeObject.asT1();
348     else return m_scopeObject.asT2()->_c;
349 }
350
351 void QQmlJavaScriptExpression::setScopeObject(QObject *v) 
352
353     if (m_scopeObject.isT1()) m_scopeObject = v;
354     else m_scopeObject.asT2()->_c = v;
355 }
356
357 bool QQmlJavaScriptExpression::hasError() const
358 {
359     return m_vtable.hasValue() && m_vtable.constValue()->error.isValid();
360 }
361
362 bool QQmlJavaScriptExpression::hasDelayedError() const
363 {
364     return m_vtable.hasValue();
365 }
366
367 QQmlExpressionPrivate *QQmlExpressionPrivate::get(QQmlExpression *expr)
368 {
369     return static_cast<QQmlExpressionPrivate *>(QObjectPrivate::get(expr));
370 }
371
372 QQmlExpression *QQmlExpressionPrivate::get(QQmlExpressionPrivate *expr)
373 {
374     return expr->q_func();
375 }
376
377 QQmlJavaScriptExpressionGuard::QQmlJavaScriptExpressionGuard(QQmlJavaScriptExpression *e)
378 : expression(e), next(0)
379
380     callback = &endpointCallback;
381 }
382
383 void QQmlJavaScriptExpressionGuard::endpointCallback(QQmlNotifierEndpoint *e)
384 {
385     QQmlJavaScriptExpression *expression =
386         static_cast<QQmlJavaScriptExpressionGuard *>(e)->expression;
387
388     expression->m_vtable->expressionChanged(expression);
389 }
390
391 QQmlJavaScriptExpressionGuard *
392 QQmlJavaScriptExpressionGuard::New(QQmlJavaScriptExpression *e,
393                                            QQmlEngine *engine)
394 {
395     Q_ASSERT(e);
396     return QQmlEnginePrivate::get(engine)->jsExpressionGuardPool.New(e);
397 }
398
399 void QQmlJavaScriptExpressionGuard::Delete()
400 {
401     QRecyclePool<QQmlJavaScriptExpressionGuard>::Delete(this);
402 }
403
404 QT_END_NAMESPACE
405
406 #endif // QQMLEXPRESSION_P_H