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 QtDeclarative 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 QDECLARATIVEEXPRESSION_P_H
43 #define QDECLARATIVEEXPRESSION_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 "qdeclarativeexpression.h"
58 #include <private/qv8engine_p.h>
59 #include <private/qfieldlist_p.h>
60 #include <private/qdeletewatcher_p.h>
61 #include <private/qdeclarativeguard_p.h>
62 #include <private/qdeclarativeengine_p.h>
66 class QDeclarativeAbstractExpression : public QDeleteWatchable
69 QDeclarativeAbstractExpression();
70 virtual ~QDeclarativeAbstractExpression();
74 QDeclarativeContextData *context() const;
75 void setContext(QDeclarativeContextData *);
77 virtual void refresh();
80 friend class QDeclarativeContext;
81 friend class QDeclarativeContextData;
82 friend class QDeclarativeContextPrivate;
83 QDeclarativeContextData *m_context;
84 QDeclarativeAbstractExpression **m_prevExpression;
85 QDeclarativeAbstractExpression *m_nextExpression;
88 class QDeclarativeDelayedError
91 inline QDeclarativeDelayedError() : nextError(0), prevError(0) {}
92 inline ~QDeclarativeDelayedError() { removeError(); }
94 QDeclarativeError error;
96 bool addError(QDeclarativeEnginePrivate *);
98 inline void removeError() {
99 if (!prevError) return;
100 if (nextError) nextError->prevError = prevError;
101 *prevError = nextError;
107 QDeclarativeDelayedError *nextError;
108 QDeclarativeDelayedError **prevError;
111 class QDeclarativeJavaScriptExpression : public QDeclarativeAbstractExpression,
112 public QDeclarativeDelayedError
115 QDeclarativeJavaScriptExpression();
116 virtual ~QDeclarativeJavaScriptExpression();
118 v8::Local<v8::Value> evaluate(v8::Handle<v8::Function>, bool *isUndefined);
120 inline bool requiresThisObject() const;
121 inline void setRequiresThisObject(bool v);
122 inline bool useSharedContext() const;
123 inline void setUseSharedContext(bool v);
124 inline bool notifyOnValueChanged() const;
126 void setNotifyOnValueChanged(bool v);
127 void resetNotifyOnValueChanged();
129 inline QObject *scopeObject() const;
130 inline void setScopeObject(QObject *v);
132 virtual void expressionChanged() {}
135 inline virtual QString expressionIdentifier();
138 quint32 m_requiresThisObject:1;
139 quint32 m_useSharedContext:1;
140 quint32 m_notifyOnValueChanged:1;
143 QObject *m_scopeObject;
145 typedef QDeclarativeJavaScriptExpressionGuard Guard;
147 struct GuardCapture : public QDeclarativeEnginePrivate::PropertyCapture {
148 GuardCapture(QDeclarativeJavaScriptExpression *e) : expression(e), errorString(0) {
151 Q_ASSERT(guards.isEmpty());
152 Q_ASSERT(errorString == 0);
155 virtual void captureProperty(QDeclarativeNotifier *);
156 virtual void captureProperty(QObject *, int, int);
158 QDeclarativeJavaScriptExpression *expression;
159 QFieldList<Guard, &Guard::next> guards;
160 QStringList *errorString;
163 QFieldList<Guard, &Guard::next> activeGuards;
164 GuardCapture *guardCapture;
169 class QDeclarativeExpression;
171 class Q_DECLARATIVE_PRIVATE_EXPORT QDeclarativeExpressionPrivate : public QObjectPrivate, public QDeclarativeJavaScriptExpression
173 Q_DECLARE_PUBLIC(QDeclarativeExpression)
175 QDeclarativeExpressionPrivate();
176 ~QDeclarativeExpressionPrivate();
178 void init(QDeclarativeContextData *, const QString &, QObject *);
179 void init(QDeclarativeContextData *, v8::Handle<v8::Function>, QObject *);
180 void init(QDeclarativeContextData *, const QString &, bool, QObject *, const QString &, int, int);
182 QVariant value(QObject *secondaryScope = 0, bool *isUndefined = 0);
184 v8::Local<v8::Value> v8value(QObject *secondaryScope = 0, bool *isUndefined = 0);
186 static inline QDeclarativeExpressionPrivate *get(QDeclarativeExpression *expr);
187 static inline QDeclarativeExpression *get(QDeclarativeExpressionPrivate *expr);
190 virtual void expressionChanged();
192 static void exceptionToError(v8::Handle<v8::Message>, QDeclarativeError &);
193 static v8::Persistent<v8::Function> evalFunction(QDeclarativeContextData *ctxt, QObject *scope,
194 const QString &code, const QString &filename, int line,
195 v8::Persistent<v8::Object> *qmlscope = 0);
196 static QDeclarativeExpression *create(QDeclarativeContextData *, QObject *, const QString &, bool,
197 const QString &, int, int);
199 bool expressionFunctionValid:1;
200 bool expressionFunctionRewritten:1;
201 bool extractExpressionFromFunction:1;
203 inline virtual QString expressionIdentifier();
207 v8::Persistent<v8::Object> v8qmlscope;
208 v8::Persistent<v8::Function> v8function;
210 QString url; // This is a QString for a reason. QUrls are slooooooow...
213 QString name; //function name, hint for the debugger
215 QDeclarativeRefCount *dataRef;
218 bool QDeclarativeJavaScriptExpression::requiresThisObject() const
220 return m_requiresThisObject;
223 void QDeclarativeJavaScriptExpression::setRequiresThisObject(bool v)
225 m_requiresThisObject = v;
228 bool QDeclarativeJavaScriptExpression::useSharedContext() const
230 return m_useSharedContext;
233 void QDeclarativeJavaScriptExpression::setUseSharedContext(bool v)
235 m_useSharedContext = v;
238 bool QDeclarativeJavaScriptExpression::notifyOnValueChanged() const
240 return m_notifyOnValueChanged;
243 QObject *QDeclarativeJavaScriptExpression::scopeObject() const
245 return m_scopeObject;
248 void QDeclarativeJavaScriptExpression::setScopeObject(QObject *v)
253 QString QDeclarativeJavaScriptExpression::expressionIdentifier()
258 QDeclarativeExpressionPrivate *QDeclarativeExpressionPrivate::get(QDeclarativeExpression *expr)
260 return static_cast<QDeclarativeExpressionPrivate *>(QObjectPrivate::get(expr));
263 QDeclarativeExpression *QDeclarativeExpressionPrivate::get(QDeclarativeExpressionPrivate *expr)
265 return expr->q_func();
268 QString QDeclarativeExpressionPrivate::expressionIdentifier()
270 return QLatin1String("\"") + expression + QLatin1String("\"");
273 QDeclarativeJavaScriptExpressionGuard::QDeclarativeJavaScriptExpressionGuard(QDeclarativeJavaScriptExpression *e)
274 : expression(e), next(0)
276 callback = &endpointCallback;
279 void QDeclarativeJavaScriptExpressionGuard::endpointCallback(QDeclarativeNotifierEndpoint *e)
281 static_cast<QDeclarativeJavaScriptExpressionGuard *>(e)->expression->expressionChanged();
284 QDeclarativeJavaScriptExpressionGuard *
285 QDeclarativeJavaScriptExpressionGuard::New(QDeclarativeJavaScriptExpression *e)
288 return QDeclarativeEnginePrivate::get(e->context()->engine)->jsExpressionGuardPool.New(e);
291 void QDeclarativeJavaScriptExpressionGuard::Delete()
293 QRecyclePool<QDeclarativeJavaScriptExpressionGuard>::Delete(this);
298 #endif // QDECLARATIVEEXPRESSION_P_H