1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #ifndef QQMLEXPRESSION_P_H
43 #define QQMLEXPRESSION_P_H
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.
56 #include "qqmlexpression.h"
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>
68 class QQmlAbstractExpression
71 QQmlAbstractExpression();
72 virtual ~QQmlAbstractExpression();
76 QQmlContextData *context() const;
77 void setContext(QQmlContextData *);
79 virtual void refresh();
83 inline DeleteWatcher(QQmlAbstractExpression *);
84 inline ~DeleteWatcher();
85 inline bool wasDeleted() const;
87 friend class QQmlAbstractExpression;
89 QQmlAbstractExpression **_w;
90 QQmlAbstractExpression *_s;
94 friend class QQmlContext;
95 friend class QQmlContextData;
96 friend class QQmlContextPrivate;
98 QBiPointer<QQmlContextData, DeleteWatcher> m_context;
99 QQmlAbstractExpression **m_prevExpression;
100 QQmlAbstractExpression *m_nextExpression;
103 class QQmlDelayedError
106 inline QQmlDelayedError() : nextError(0), prevError(0) {}
107 inline ~QQmlDelayedError() { removeError(); }
111 bool addError(QQmlEnginePrivate *);
113 inline void removeError() {
114 if (!prevError) return;
115 if (nextError) nextError->prevError = prevError;
116 *prevError = nextError;
122 QQmlDelayedError *nextError;
123 QQmlDelayedError **prevError;
126 class QQmlJavaScriptExpression
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.
135 QString (*expressionIdentifier)(QQmlJavaScriptExpression *);
136 void (*expressionChanged)(QQmlJavaScriptExpression *);
139 QQmlJavaScriptExpression(VTable *vtable);
141 v8::Local<v8::Value> evaluate(QQmlContextData *, v8::Handle<v8::Function>,
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;
150 void setNotifyOnValueChanged(bool v);
151 void resetNotifyOnValueChanged();
153 inline QObject *scopeObject() const;
154 inline void setScopeObject(QObject *v);
156 class DeleteWatcher {
158 inline DeleteWatcher(QQmlJavaScriptExpression *);
159 inline ~DeleteWatcher();
160 inline bool wasDeleted() const;
162 friend class QQmlJavaScriptExpression;
164 QQmlJavaScriptExpression **_w;
165 QQmlJavaScriptExpression *_s;
168 inline bool hasError() const;
169 inline bool hasDelayedError() const;
170 QQmlError error() const;
172 QQmlDelayedError *delayedError();
175 ~QQmlJavaScriptExpression();
178 typedef QQmlJavaScriptExpressionGuard Guard;
179 friend class QQmlJavaScriptExpressionGuard;
181 struct GuardCapture : public QQmlEnginePrivate::PropertyCapture {
182 GuardCapture(QQmlEngine *engine, QQmlJavaScriptExpression *e)
183 : engine(engine), expression(e), errorString(0) { }
186 Q_ASSERT(guards.isEmpty());
187 Q_ASSERT(errorString == 0);
190 virtual void captureProperty(QQmlNotifier *);
191 virtual void captureProperty(QObject *, int, int);
194 QQmlJavaScriptExpression *expression;
195 QFieldList<Guard, &Guard::next> guards;
196 QStringList *errorString;
199 QPointerValuePair<VTable, QQmlDelayedError> m_vtable;
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;
211 class QQmlExpression;
213 class Q_QML_PRIVATE_EXPORT QQmlExpressionPrivate : public QObjectPrivate, public QQmlJavaScriptExpression, public QQmlAbstractExpression
215 Q_DECLARE_PUBLIC(QQmlExpression)
217 QQmlExpressionPrivate();
218 ~QQmlExpressionPrivate();
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);
225 QVariant value(QObject *secondaryScope = 0, bool *isUndefined = 0);
227 v8::Local<v8::Value> v8value(QObject *secondaryScope = 0, bool *isUndefined = 0);
229 static inline QQmlExpressionPrivate *get(QQmlExpression *expr);
230 static inline QQmlExpression *get(QQmlExpressionPrivate *expr);
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,
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);
244 static QQmlExpression *create(QQmlContextData *, QObject *, const QString &, bool,
245 const QString &, int, int);
247 bool expressionFunctionValid:1;
248 bool expressionFunctionRewritten:1;
249 bool extractExpressionFromFunction:1;
251 // "Inherited" from QQmlJavaScriptExpression
252 static QString expressionIdentifier(QQmlJavaScriptExpression *);
253 static void expressionChanged(QQmlJavaScriptExpression *);
254 virtual void expressionChanged();
257 QByteArray expressionUtf8;
259 v8::Persistent<v8::Object> v8qmlscope;
260 v8::Persistent<v8::Function> v8function;
262 QString url; // This is a QString for a reason. QUrls are slooooooow...
265 QString name; //function name, hint for the debugger
267 QQmlRefCount *dataRef;
270 QQmlAbstractExpression::DeleteWatcher::DeleteWatcher(QQmlAbstractExpression *e)
271 : _c(0), _w(0), _s(e)
273 if (e->m_context.isT1()) {
275 _c = e->m_context.asT1();
278 // Another watcher is already registered
279 _w = &e->m_context.asT2()->_s;
283 QQmlAbstractExpression::DeleteWatcher::~DeleteWatcher()
285 Q_ASSERT(*_w == 0 || (*_w == _s && _s->m_context.isT2()));
286 if (*_w && _s->m_context.asT2() == this)
290 bool QQmlAbstractExpression::DeleteWatcher::wasDeleted() const
295 QQmlJavaScriptExpression::DeleteWatcher::DeleteWatcher(QQmlJavaScriptExpression *e)
296 : _c(0), _w(0), _s(e)
298 if (e->m_scopeObject.isT1()) {
300 _c = e->m_scopeObject.asT1();
301 e->m_scopeObject = this;
303 // Another watcher is already registered
304 _w = &e->m_scopeObject.asT2()->_s;
308 QQmlJavaScriptExpression::DeleteWatcher::~DeleteWatcher()
310 Q_ASSERT(*_w == 0 || (*_w == _s && _s->m_scopeObject.isT2()));
311 if (*_w && _s->m_scopeObject.asT2() == this)
312 _s->m_scopeObject = _c;
315 bool QQmlJavaScriptExpression::DeleteWatcher::wasDeleted() const
320 bool QQmlJavaScriptExpression::requiresThisObject() const
322 return m_scopeObject.flag();
325 void QQmlJavaScriptExpression::setRequiresThisObject(bool v)
327 m_scopeObject.setFlagValue(v);
330 bool QQmlJavaScriptExpression::useSharedContext() const
332 return activeGuards.flag2();
335 void QQmlJavaScriptExpression::setUseSharedContext(bool v)
337 activeGuards.setFlag2Value(v);
340 bool QQmlJavaScriptExpression::notifyOnValueChanged() const
342 return activeGuards.flag();
345 QObject *QQmlJavaScriptExpression::scopeObject() const
347 if (m_scopeObject.isT1()) return m_scopeObject.asT1();
348 else return m_scopeObject.asT2()->_c;
351 void QQmlJavaScriptExpression::setScopeObject(QObject *v)
353 if (m_scopeObject.isT1()) m_scopeObject = v;
354 else m_scopeObject.asT2()->_c = v;
357 bool QQmlJavaScriptExpression::hasError() const
359 return m_vtable.hasValue() && m_vtable.constValue()->error.isValid();
362 bool QQmlJavaScriptExpression::hasDelayedError() const
364 return m_vtable.hasValue();
367 QQmlExpressionPrivate *QQmlExpressionPrivate::get(QQmlExpression *expr)
369 return static_cast<QQmlExpressionPrivate *>(QObjectPrivate::get(expr));
372 QQmlExpression *QQmlExpressionPrivate::get(QQmlExpressionPrivate *expr)
374 return expr->q_func();
377 QQmlJavaScriptExpressionGuard::QQmlJavaScriptExpressionGuard(QQmlJavaScriptExpression *e)
378 : expression(e), next(0)
380 callback = &endpointCallback;
383 void QQmlJavaScriptExpressionGuard::endpointCallback(QQmlNotifierEndpoint *e)
385 QQmlJavaScriptExpression *expression =
386 static_cast<QQmlJavaScriptExpressionGuard *>(e)->expression;
388 expression->m_vtable->expressionChanged(expression);
391 QQmlJavaScriptExpressionGuard *
392 QQmlJavaScriptExpressionGuard::New(QQmlJavaScriptExpression *e,
396 return QQmlEnginePrivate::get(engine)->jsExpressionGuardPool.New(e);
399 void QQmlJavaScriptExpressionGuard::Delete()
401 QRecyclePool<QQmlJavaScriptExpressionGuard>::Delete(this);
406 #endif // QQMLEXPRESSION_P_H