/****************************************************************************
**
-** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
-** All rights reserved.
-** Contact: Nokia Corporation (qt-info@nokia.com)
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
**
**
**
+**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qdeclarativeexpression.h"
-#include "private/qdeclarativeexpression_p.h"
+#include "qdeclarativeexpression_p.h"
-#include "private/qdeclarativeengine_p.h"
-#include "private/qdeclarativecontext_p.h"
-#include "private/qdeclarativerewrite_p.h"
-#include "private/qdeclarativescriptstring_p.h"
-#include "private/qdeclarativecompiler_p.h"
+#include "qdeclarativeengine_p.h"
+#include "qdeclarativecontext_p.h"
+#include "qdeclarativerewrite_p.h"
+#include "qdeclarativescriptstring_p.h"
+#include "qdeclarativecompiler_p.h"
#include <QtCore/qdebug.h>
QDeclarativeJavaScriptExpression::QDeclarativeJavaScriptExpression()
: m_requiresThisObject(0), m_useSharedContext(0), m_notifyOnValueChanged(0),
- m_scopeObject(0), m_notifyObject(0), m_notifyIndex(-1)
+ m_scopeObject(0), guardCapture(0)
{
}
QDeclarativeJavaScriptExpression::~QDeclarativeJavaScriptExpression()
{
+ if (guardCapture) guardCapture->expression = 0;
+ clearGuards();
}
QDeclarativeExpressionPrivate::QDeclarativeExpressionPrivate()
-: expressionFunctionValid(true), extractExpressionFromFunction(false), line(-1), dataRef(0)
+: expressionFunctionValid(true), expressionFunctionRewritten(false),
+ extractExpressionFromFunction(false), line(-1), dataRef(0)
{
}
QDeclarativeAbstractExpression::setContext(ctxt);
setScopeObject(me);
expressionFunctionValid = false;
+ expressionFunctionRewritten = false;
}
void QDeclarativeExpressionPrivate::init(QDeclarativeContextData *ctxt, v8::Handle<v8::Function> func,
v8function = qPersistentNew<v8::Function>(func);
setUseSharedContext(false);
expressionFunctionValid = true;
+ expressionFunctionRewritten = false;
extractExpressionFromFunction = true;
}
-void QDeclarativeExpressionPrivate::init(QDeclarativeContextData *ctxt, const QString &expr, bool isRewritten,
- QObject *me, const QString &srcUrl, int lineNumber)
+void QDeclarativeExpressionPrivate::init(QDeclarativeContextData *ctxt, const QString &expr,
+ bool isRewritten, QObject *me, const QString &srcUrl,
+ int lineNumber, int columnNumber)
{
url = srcUrl;
line = lineNumber;
+ column = columnNumber;
expression = expr;
- if (!isRewritten) {
- expressionFunctionValid = false;
- } else {
- v8function = evalFunction(ctxt, me, expression, url, line, &v8qmlscope);
- expressionFunctionValid = true;
- }
+ expressionFunctionValid = false;
+ expressionFunctionRewritten = isRewritten;
QDeclarativeAbstractExpression::setContext(ctxt);
setScopeObject(me);
// XXX TODO: Implement script caching, like we used to do with QScriptProgram in the
// QtScript days
v8::HandleScope handle_scope;
- v8::Context::Scope ctxtscope(ep->v8engine.context());
+ v8::Context::Scope ctxtscope(ep->v8engine()->context());
v8::TryCatch tc;
- v8::Local<v8::Object> scopeobject = ep->v8engine.qmlScope(ctxt, scope);
- v8::Local<v8::Script> script = ep->v8engine.qmlModeCompile(code, filename, line);
+ v8::Local<v8::Object> scopeobject = ep->v8engine()->qmlScope(ctxt, scope);
+ v8::Local<v8::Script> script = ep->v8engine()->qmlModeCompile(code, filename, line);
+ if (tc.HasCaught()) return v8::Persistent<v8::Function>();
v8::Local<v8::Value> result = script->Run(scopeobject);
if (tc.HasCaught()) return v8::Persistent<v8::Function>();
if (qmlscope) *qmlscope = qPersistentNew<v8::Object>(scopeobject);
return qPersistentNew<v8::Function>(v8::Local<v8::Function>::Cast(result));
}
+QDeclarativeExpression *QDeclarativeExpressionPrivate::create(QDeclarativeContextData *ctxt, QObject *object, const QString &expr, bool isRewritten,
+ const QString &url, int lineNumber, int columnNumber)
+{
+ return new QDeclarativeExpression(ctxt, object, expr, isRewritten, url, lineNumber, columnNumber, *new QDeclarativeExpressionPrivate);
+}
+
/*!
\class QDeclarativeExpression
\since 4.7
\endcode
*/
-static int QDeclarativeExpression_notifyIdx = -1;
-
/*!
Create an invalid QDeclarativeExpression.
QDeclarativeExpression::QDeclarativeExpression()
: QObject(*new QDeclarativeExpressionPrivate, 0)
{
- Q_D(QDeclarativeExpression);
-
- if (QDeclarativeExpression_notifyIdx == -1)
- QDeclarativeExpression_notifyIdx = QDeclarativeExpression::staticMetaObject.indexOfMethod("_q_notify()");
- d->setNotifyObject(this, QDeclarativeExpression_notifyIdx);
}
/*! \internal */
QDeclarativeExpression::QDeclarativeExpression(QDeclarativeContextData *ctxt,
QObject *object, const QString &expr, bool isRewritten,
- const QString &url, int lineNumber,
+ const QString &url, int lineNumber, int columnNumber,
QDeclarativeExpressionPrivate &dd)
: QObject(dd, 0)
{
Q_D(QDeclarativeExpression);
- d->init(ctxt, expr, isRewritten, object, url, lineNumber);
-
- if (QDeclarativeExpression_notifyIdx == -1)
- QDeclarativeExpression_notifyIdx = QDeclarativeExpression::staticMetaObject.indexOfMethod("_q_notify()");
- d->setNotifyObject(this, QDeclarativeExpression_notifyIdx);
+ d->init(ctxt, expr, isRewritten, object, url, lineNumber, columnNumber);
}
/*!
} else {
QDeclarativeContextData *ctxtdata = QDeclarativeContextData::get(script.context());
- QDeclarativeEnginePrivate *engine = QDeclarativeEnginePrivate::get(qmlEngine(script.scopeObject()));
+ QDeclarativeEnginePrivate *engine = QDeclarativeEnginePrivate::get(script.context()->engine());
QDeclarativeCompiledData *cdata = 0;
QDeclarativeTypeData *typeData = 0;
if (engine && ctxtdata && !ctxtdata->url.isEmpty()) {
}
if (cdata)
- d->init(ctxtdata, cdata->primitives.at(id), cdata, script.scopeObject(),
- cdata->name, script.d.data()->lineNumber);
+ d->init(ctxtdata, cdata->primitives.at(id), true, script.scopeObject(),
+ cdata->name, script.d.data()->lineNumber, script.d.data()->columnNumber);
else
defaultConstruction = true;
+ if (cdata)
+ cdata->release();
if (typeData)
typeData->release();
}
if (defaultConstruction)
d->init(QDeclarativeContextData::get(script.context()), script.script(), script.scopeObject());
-
- if (QDeclarativeExpression_notifyIdx == -1)
- QDeclarativeExpression_notifyIdx = QDeclarativeExpression::staticMetaObject.indexOfMethod("_q_notify()");
- d->setNotifyObject(this, QDeclarativeExpression_notifyIdx);
}
/*!
{
Q_D(QDeclarativeExpression);
d->init(QDeclarativeContextData::get(ctxt), expression, scope);
-
- if (QDeclarativeExpression_notifyIdx == -1)
- QDeclarativeExpression_notifyIdx = QDeclarativeExpression::staticMetaObject.indexOfMethod("_q_notify()");
- d->setNotifyObject(this, QDeclarativeExpression_notifyIdx);
}
/*!
{
Q_D(QDeclarativeExpression);
d->init(ctxt, expression, scope);
-
- if (QDeclarativeExpression_notifyIdx == -1)
- QDeclarativeExpression_notifyIdx = QDeclarativeExpression::staticMetaObject.indexOfMethod("_q_notify()");
- d->setNotifyObject(this, QDeclarativeExpression_notifyIdx);
}
/*! \internal */
{
Q_D(QDeclarativeExpression);
d->init(ctxt, expression, scope);
-
- if (QDeclarativeExpression_notifyIdx == -1)
- QDeclarativeExpression_notifyIdx = QDeclarativeExpression::staticMetaObject.indexOfMethod("_q_notify()");
- d->setNotifyObject(this, QDeclarativeExpression_notifyIdx);
}
/*!
Q_D(QDeclarativeExpression);
d->init(ctxt, function, scope);
-
- if (QDeclarativeExpression_notifyIdx == -1)
- QDeclarativeExpression_notifyIdx = QDeclarativeExpression::staticMetaObject.indexOfMethod("_q_notify()");
-
- d->setNotifyObject(this, QDeclarativeExpression_notifyIdx);
}
/*!
d->resetNotifyOnValueChanged();
d->expression = expression;
d->expressionFunctionValid = false;
+ d->expressionFunctionRewritten = false;
qPersistentDispose(d->v8function);
qPersistentDispose(d->v8qmlscope);
}
v8::Handle<v8::String> description = message->Get();
int lineNumber = message->GetLineNumber();
- Q_ASSERT(name->IsString());
-
- v8::Local<v8::String> file = name->ToString();
- if (file->Length() == 0)
+ v8::Local<v8::String> file = name->IsString()?name->ToString():v8::Local<v8::String>();
+ if (file.IsEmpty() || file->Length() == 0)
error.setUrl(QUrl(QLatin1String("<Unknown File>")));
else
error.setUrl(QUrl(QV8Engine::toStringStatic(file)));
void QDeclarativeJavaScriptExpression::setNotifyOnValueChanged(bool v)
{
m_notifyOnValueChanged = v;
- if (!v) guardList.clear();
+ if (!v) clearGuards();
}
void QDeclarativeJavaScriptExpression::resetNotifyOnValueChanged()
{
- guardList.clear();
-}
-
-void QDeclarativeJavaScriptExpression::setNotifyObject(QObject *object, int index)
-{
- guardList.clear();
-
- m_notifyObject = object;
- m_notifyIndex = index;
-
- if (!object || index == -1) {
- m_notifyObject = 0;
- m_notifyIndex = -1;
- }
+ clearGuards();
}
v8::Local<v8::Value> QDeclarativeJavaScriptExpression::evaluate(v8::Handle<v8::Function> function, bool *isUndefined)
{
Q_ASSERT(context() && context()->engine);
- Q_ASSERT(!notifyOnValueChanged() || (m_notifyObject && m_notifyIndex != -1));
if (function.IsEmpty() || function->IsUndefined()) {
if (isUndefined) *isUndefined = true;
QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(context()->engine);
- bool lastCaptureProperties = ep->captureProperties;
- QPODVector<QDeclarativeEnginePrivate::CapturedProperty> lastCapturedProperties;
- ep->captureProperties = notifyOnValueChanged();
+ Q_ASSERT(notifyOnValueChanged() || activeGuards.isEmpty());
+ GuardCapture capture(this);
+
+ QDeclarativeEnginePrivate::PropertyCapture *lastPropertyCapture = ep->propertyCapture;
+ ep->propertyCapture = notifyOnValueChanged()?&capture:0;
- if (ep->capturedProperties.count())
- ep->capturedProperties.copyAndClear(lastCapturedProperties);
+
+ if (notifyOnValueChanged())
+ capture.guards.copyAndClear(activeGuards);
QDeclarativeContextData *lastSharedContext = 0;
QObject *lastSharedScope = 0;
// All code that follows must check with watcher before it accesses data members
// incase we have been deleted.
- QDeclarativeDeleteWatcher watcher(this);
+ QDeleteWatcher watcher(this);
if (sharedContext) {
lastSharedContext = ep->sharedContext;
v8::Local<v8::Value> result;
{
v8::TryCatch try_catch;
- v8::Handle<v8::Object> This = ep->v8engine.global();
+ v8::Handle<v8::Object> This = ep->v8engine()->global();
if (scopeObject() && requiresThisObject()) {
- v8::Handle<v8::Value> value = ep->v8engine.newQObject(scopeObject());
+ v8::Handle<v8::Value> value = ep->v8engine()->newQObject(scopeObject());
if (value->IsObject()) This = v8::Handle<v8::Object>::Cast(value);
}
if (watcher.wasDeleted()) {
} else if (try_catch.HasCaught()) {
- v8::Context::Scope scope(ep->v8engine.context());
+ v8::Context::Scope scope(ep->v8engine()->context());
v8::Local<v8::Message> message = try_catch.Message();
if (!message.IsEmpty()) {
QDeclarativeExpressionPrivate::exceptionToError(message, error);
ep->sharedScope = lastSharedScope;
}
- if (!watcher.wasDeleted() && notifyOnValueChanged()) {
- guardList.updateGuards(m_notifyObject, m_notifyIndex, expressionString(),
- ep->capturedProperties);
+ if (capture.errorString) {
+ for (int ii = 0; ii < capture.errorString->count(); ++ii)
+ qWarning("%s", qPrintable(capture.errorString->at(ii)));
+ delete capture.errorString;
+ capture.errorString = 0;
}
- if (lastCapturedProperties.count())
- lastCapturedProperties.copyAndClear(ep->capturedProperties);
- else
- ep->capturedProperties.clear();
+ while (Guard *g = capture.guards.takeFirst())
+ g->Delete();
- ep->captureProperties = lastCaptureProperties;
+ ep->propertyCapture = lastPropertyCapture;
return result;
}
-void QDeclarativeJavaScriptExpression::GuardList::updateGuards(QObject *notifyObject, int notifyIndex,
- const QStringRef &expression,
- const CapturedProperties &properties)
+void QDeclarativeJavaScriptExpression::GuardCapture::captureProperty(QDeclarativeNotifier *n)
{
- Q_ASSERT(notifyObject);
- Q_ASSERT(notifyIndex != -1);
-
- if (properties.count() == 0) {
- clear();
- return;
- }
+ if (expression) {
- if (properties.count() != length) {
- QDeclarativeNotifierEndpoint *newGuardList = new QDeclarativeNotifierEndpoint[properties.count()];
+ // Try and find a matching guard
+ while (!guards.isEmpty() && !guards.first()->isConnected(n))
+ guards.takeFirst()->Delete();
- for (int ii = 0; ii < qMin(length, properties.count()); ++ii)
- endpoints[ii].copyAndClear(newGuardList[ii]);
+ Guard *g = 0;
+ if (!guards.isEmpty()) {
+ g = guards.takeFirst();
+ g->cancelNotify();
+ Q_ASSERT(g->isConnected(n));
+ } else {
+ g = Guard::New(expression);
+ g->connect(n);
+ }
- delete [] endpoints;
- endpoints = newGuardList;
- length = properties.count();
+ expression->activeGuards.append(g);
}
+}
- bool outputWarningHeader = false;
- bool noChanges = true;
- for (int ii = 0; ii < properties.count(); ++ii) {
- QDeclarativeNotifierEndpoint &guard = endpoints[ii];
- const QDeclarativeEnginePrivate::CapturedProperty &property = properties.at(ii);
-
- guard.target = notifyObject;
- guard.targetMethod = notifyIndex;
-
- if (property.notifier != 0) {
-
- if (!noChanges && guard.isConnected(property.notifier)) {
- // Nothing to do
-
- } else {
- noChanges = false;
-
- bool existing = false;
- for (int jj = 0; !existing && jj < ii; ++jj)
- if (endpoints[jj].isConnected(property.notifier))
- existing = true;
-
- if (existing) {
- // duplicate
- guard.disconnect();
- } else {
- guard.connect(property.notifier);
- }
+void QDeclarativeJavaScriptExpression::GuardCapture::captureProperty(QObject *o, int c, int n)
+{
+ if (expression) {
+ if (n == -1) {
+ if (!errorString) {
+ errorString = new QStringList;
+ QString preamble = QLatin1String("QDeclarativeExpression: Expression ") +
+ expression->expressionIdentifier() +
+ QLatin1String(" depends on non-NOTIFYable properties:");
+ errorString->append(preamble);
}
+ const QMetaObject *metaObj = o->metaObject();
+ QMetaProperty metaProp = metaObj->property(c);
- } else if (property.notifyIndex != -1) {
+ QString error = QLatin1String(" ") +
+ QString::fromUtf8(metaObj->className()) +
+ QLatin1String("::") +
+ QString::fromUtf8(metaProp.name());
+ errorString->append(error);
+ } else {
- if (!noChanges && guard.isConnected(property.object, property.notifyIndex)) {
- // Nothing to do
+ // Try and find a matching guard
+ while (!guards.isEmpty() && !guards.first()->isConnected(o, n))
+ guards.takeFirst()->Delete();
+ Guard *g = 0;
+ if (!guards.isEmpty()) {
+ g = guards.takeFirst();
+ g->cancelNotify();
+ Q_ASSERT(g->isConnected(o, n));
} else {
- noChanges = false;
-
- bool existing = false;
- for (int jj = 0; !existing && jj < ii; ++jj)
- if (endpoints[jj].isConnected(property.object, property.notifyIndex))
- existing = true;
-
- if (existing) {
- // duplicate
- guard.disconnect();
- } else {
- guard.connect(property.object, property.notifyIndex);
- }
- }
-
- } else if (!expression.isEmpty()) {
- if (!outputWarningHeader) {
- outputWarningHeader = true;
- qWarning() << "QDeclarativeExpression: Expression" << expression
- << "depends on non-NOTIFYable properties:";
+ g = Guard::New(expression);
+ g->connect(o, n);
}
- const QMetaObject *metaObj = property.object->metaObject();
- QMetaProperty metaProp = metaObj->property(property.coreIndex);
-
- qWarning().nospace() << " " << metaObj->className() << "::" << metaProp.name();
+ expression->activeGuards.append(g);
}
}
}
+void QDeclarativeJavaScriptExpression::clearGuards()
+{
+ while (Guard *g = activeGuards.takeFirst())
+ g->Delete();
+}
+
// Must be called with a valid handle scope
v8::Local<v8::Value> QDeclarativeExpressionPrivate::v8value(QObject *secondaryScope, bool *isUndefined)
{
if (!expressionFunctionValid) {
+ bool ok = true;
+
QDeclarativeRewrite::RewriteBinding rewriteBinding;
rewriteBinding.setName(name);
- bool ok = true;
- const QString code = rewriteBinding(expression, &ok);
+ QString code;
+ if (expressionFunctionRewritten)
+ code = expression;
+ else
+ code = rewriteBinding(expression, &ok);
+
if (ok) v8function = evalFunction(context(), scopeObject(), code, url, line, &v8qmlscope);
setUseSharedContext(false);
expressionFunctionValid = true;
v8::Local<v8::Value> result;
QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(context()->engine);
QObject *restoreSecondaryScope = 0;
- restoreSecondaryScope = ep->v8engine.contextWrapper()->setSecondaryScope(v8qmlscope, secondaryScope);
+ restoreSecondaryScope = ep->v8engine()->contextWrapper()->setSecondaryScope(v8qmlscope, secondaryScope);
result = evaluate(v8function, isUndefined);
- ep->v8engine.contextWrapper()->setSecondaryScope(v8qmlscope, restoreSecondaryScope);
+ ep->v8engine()->contextWrapper()->setSecondaryScope(v8qmlscope, restoreSecondaryScope);
return result;
} else {
return evaluate(v8function, isUndefined);
{
v8::HandleScope handle_scope;
+ v8::Context::Scope context_scope(ep->v8engine()->context());
v8::Local<v8::Value> result = v8value(secondaryScope, isUndefined);
- rv = ep->v8engine.toVariant(result, qMetaTypeId<QList<QObject*> >());
+ rv = ep->v8engine()->toVariant(result, qMetaTypeId<QList<QObject*> >());
}
ep->dereferenceScarceResources(); // "release" scarce resources if top-level expression evaluation is complete.
}
/*!
+ Returns the source file column number for this expression. The source location
+ must have been previously set by calling setSourceLocation().
+*/
+int QDeclarativeExpression::columnNumber() const
+{
+ Q_D(const QDeclarativeExpression);
+ return d->column;
+}
+
+/*!
Set the location of this expression to \a line of \a url. This information
is used by the script engine.
*/
-void QDeclarativeExpression::setSourceLocation(const QString &url, int line)
+void QDeclarativeExpression::setSourceLocation(const QString &url, int line, int column)
{
Q_D(QDeclarativeExpression);
d->url = url;
d->line = line;
+ d->column = column;
}
/*!
return d->error;
}
-/*! \internal */
-void QDeclarativeExpressionPrivate::_q_notify()
-{
- emitValueChanged();
-}
-
/*!
\fn void QDeclarativeExpression::valueChanged()
calling QDeclarativeExpression::evaluate()) before this signal will be emitted.
*/
-void QDeclarativeExpressionPrivate::emitValueChanged()
+void QDeclarativeExpressionPrivate::expressionChanged()
{
Q_Q(QDeclarativeExpression);
emit q->valueChanged();