Rewrite Connections signal handlers at compile time.
authorMichael Brasser <michael.brasser@nokia.com>
Thu, 24 Nov 2011 04:43:09 +0000 (14:43 +1000)
committerQt by Nokia <qt-info@nokia.com>
Sun, 27 Nov 2011 23:05:04 +0000 (00:05 +0100)
Task-number: QTBUG-22726
Change-Id: I039d54661bbf7d44912c894bf0cc1d70023a9187
Reviewed-by: Aaron Kennedy <aaron.kennedy@nokia.com>
src/declarative/qml/qdeclarativecompiler.cpp
src/declarative/qml/qdeclarativecompiler_p.h
src/declarative/qml/qdeclarativecustomparser.cpp
src/declarative/qml/qdeclarativecustomparser_p.h
src/declarative/qml/qdeclarativeexpression.cpp
src/declarative/qml/qdeclarativeexpression_p.h
src/declarative/util/qdeclarativeconnections.cpp

index a2eae79..9b80c2c 100644 (file)
@@ -2494,6 +2494,12 @@ int QDeclarativeCompiler::rewriteBinding(const QDeclarativeScript::Variant& valu
     return output->indexForString(rewrite);
 }
 
+QString QDeclarativeCompiler::rewriteSignalHandler(const QString &handler, const QString &name)
+{
+    QDeclarativeRewrite::RewriteSignalHandler rewriteSignalHandler;
+    return rewriteSignalHandler(handler, name);
+}
+
 // Ensures that the dynamic meta specification on obj is valid
 bool QDeclarativeCompiler::checkDynamicMeta(QDeclarativeScript::Object *obj)
 {
index 1ee5afb..9a67f32 100644 (file)
@@ -280,6 +280,7 @@ public:
     int evaluateEnum(const QByteArray& script) const; // for QDeclarativeCustomParser::evaluateEnum
     const QMetaObject *resolveType(const QString& name) const; // for QDeclarativeCustomParser::resolveType
     int rewriteBinding(const QDeclarativeScript::Variant& value, const QString& name); // for QDeclarativeCustomParser::rewriteBinding
+    QString rewriteSignalHandler(const QString &handler, const QString &name);  // for QDeclarativeCustomParser::rewriteSignalHandler
 
 private:
     typedef QDeclarativeCompiledData::Instruction Instruction;
index 9d643d3..788a670 100644 (file)
@@ -307,4 +307,13 @@ QDeclarativeBinding::Identifier QDeclarativeCustomParser::rewriteBinding(const Q
     return compiler->rewriteBinding(value, name);
 }
 
+/*!
+    Returns a rewritten \a handler. \a name
+    is used as the name of the rewritten function.
+*/
+QString QDeclarativeCustomParser::rewriteSignalHandler(const QString &handler, const QString &name)
+{
+    return compiler->rewriteSignalHandler(handler, name);
+}
+
 QT_END_NAMESPACE
index 83e1319..5a9220a 100644 (file)
@@ -143,6 +143,7 @@ protected:
     const QMetaObject *resolveType(const QString&) const;
 
     QDeclarativeBinding::Identifier rewriteBinding(const QDeclarativeScript::Variant&, const QString&);
+    QString rewriteSignalHandler(const QString&, const QString&);
 
 private:
     QList<QDeclarativeError> exceptions;
index 0895ffa..bf2552d 100644 (file)
@@ -158,6 +158,12 @@ QDeclarativeExpressionPrivate::evalFunction(QDeclarativeContextData *ctxt, QObje
     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)
+{
+    return new QDeclarativeExpression(ctxt, object, expr, isRewritten, url, lineNumber, *new QDeclarativeExpressionPrivate);
+}
+
 /*!
     \class QDeclarativeExpression
     \since 4.7
index bfe031f..7a2bda2 100644 (file)
@@ -193,6 +193,8 @@ public:
     static v8::Persistent<v8::Function> evalFunction(QDeclarativeContextData *ctxt, QObject *scope, 
                                                      const QString &code, const QString &filename, int line,
                                                      v8::Persistent<v8::Object> *qmlscope = 0);
+    static QDeclarativeExpression *create(QDeclarativeContextData *, QObject *, const QString &, bool,
+                                          const QString &, int);
 
     bool expressionFunctionValid:1;
     bool extractExpressionFromFunction:1;
index aa944f3..ee2c6ed 100644 (file)
@@ -41,7 +41,7 @@
 
 #include "qdeclarativeconnections_p.h"
 
-#include <qdeclarativeexpression.h>
+#include <private/qdeclarativeexpression_p.h>
 #include <private/qdeclarativeproperty_p.h>
 #include <private/qdeclarativeboundsignal_p.h>
 #include <qdeclarativecontext.h>
@@ -225,7 +225,7 @@ QDeclarativeConnectionsParser::compile(const QList<QDeclarativeCustomParserPrope
                 QDeclarativeScript::Variant v = qvariant_cast<QDeclarativeScript::Variant>(value);
                 if (v.isScript()) {
                     ds << propName;
-                    ds << v.asScript();
+                    ds << rewriteSignalHandler(v.asScript(), propName);
                     ds << propLine;
                 } else {
                     error(props.at(ii), QDeclarativeConnections::tr("Connections: script expected"));
@@ -265,10 +265,18 @@ void QDeclarativeConnections::connectSignals()
         if (prop.isValid() && (prop.type() & QDeclarativeProperty::SignalProperty)) {
             QDeclarativeBoundSignal *signal =
                 new QDeclarativeBoundSignal(target(), prop.method(), this);
-            QDeclarativeExpression *expression = new QDeclarativeExpression(qmlContext(this), 0, script);
+
+            QString location;
+            QDeclarativeContextData *ctxtdata = 0;
             QDeclarativeData *ddata = QDeclarativeData::get(this);
-            if (ddata && ddata->outerContext && !ddata->outerContext->url.isEmpty())
-                expression->setSourceLocation(ddata->outerContext->url.toString(), line);
+            if (ddata) {
+                ctxtdata = ddata->outerContext;
+                if (ctxtdata && !ctxtdata->url.isEmpty())
+                    location = ddata->outerContext->url.toString();
+            }
+
+            QDeclarativeExpression *expression = ctxtdata ?
+                QDeclarativeExpressionPrivate::create(ctxtdata, 0, script, true, location, line) : 0;
             signal->setExpression(expression);
             d->boundsignals += signal;
         } else {