Rewrite signal handlers at compile time.
authorMichael Brasser <michael.brasser@nokia.com>
Thu, 17 Nov 2011 02:53:26 +0000 (12:53 +1000)
committerQt by Nokia <qt-info@nokia.com>
Sun, 20 Nov 2011 22:23:38 +0000 (23:23 +0100)
Task-number: QTBUG-22726
Change-Id: If2c3c00c646b2ed6b6899573a0b487c8deeaad3f
Reviewed-by: Kai Koehne <kai.koehne@nokia.com>
Reviewed-by: Aaron Kennedy <aaron.kennedy@nokia.com>
src/declarative/qml/qdeclarativecompiler.cpp
src/declarative/qml/qdeclarativeinstruction_p.h
src/declarative/qml/qdeclarativerewrite.cpp
src/declarative/qml/qdeclarativerewrite_p.h
src/declarative/qml/qdeclarativevme.cpp
tests/auto/declarative/debugger/qdeclarativeenginedebug/tst_qdeclarativeenginedebug.cpp

index d3069ec..e7d9b80 100644 (file)
@@ -1227,10 +1227,11 @@ void QDeclarativeCompiler::genObjectBody(QDeclarativeScript::Object *obj)
 
             Instruction::StoreSignal store;
             store.signalIndex = prop->index;
-            store.value =
-                output->indexForString(v->value.asScript().trimmed());
+            QDeclarativeRewrite::RewriteSignalHandler rewriteSignalHandler;
+            const QString &rewrite =
+                    rewriteSignalHandler(v->value.asScript().trimmed(), prop->name().toString());
+            store.value = output->indexForString(rewrite);
             store.context = v->signalExpressionContextStack;
-            store.name = output->indexForByteArray(prop->name().toUtf8());
             store.line = v->location.start.line;
             output->addInstruction(store);
 
index 8940e99..d73ce46 100644 (file)
@@ -382,7 +382,6 @@ union QDeclarativeInstruction
         int signalIndex;
         int value;
         short context;
-        int name;
         ushort line;
     };
     struct instr_assignSignalObject {
index 98bd34f..dab7c3d 100644 (file)
@@ -266,6 +266,11 @@ void RewriteBinding::endVisit(AST::LocalForEachStatement *)
     --_inLoop;
 }
 
+QString RewriteSignalHandler::operator()(const QString &code, const QString &name)
+{
+    return QStringLiteral("(function ") + name + QStringLiteral("() { ") + code + QStringLiteral(" })");
+}
+
 } // namespace QDeclarativeRewrite
 
 QT_END_NAMESPACE
index c57e995..b5ea44f 100644 (file)
@@ -119,6 +119,12 @@ private:
     int _inLoop;
 };
 
+class RewriteSignalHandler
+{
+public:
+    QString operator()(const QString &code, const QString &name);
+};
+
 } // namespace QDeclarativeRewrite
 
 QT_END_NAMESPACE
index 8f11b28..24d2ef4 100644 (file)
@@ -863,9 +863,7 @@ QObject *QDeclarativeVME::run(QList<QDeclarativeError> *errors,
 
             QDeclarativeBoundSignal *bs = new QDeclarativeBoundSignal(target, signal, target);
             QDeclarativeExpression *expr = 
-                new QDeclarativeExpression(CTXT, context, PRIMITIVES.at(instr.value));
-            expr->setSourceLocation(COMP->name, instr.line);
-            static_cast<QDeclarativeExpressionPrivate *>(QObjectPrivate::get(expr))->name = QString::fromUtf8(DATAS.at(instr.name));
+                new QDeclarativeExpression(CTXT, context, PRIMITIVES.at(instr.value), true, COMP->name, instr.line, *new QDeclarativeExpressionPrivate);
             bs->setExpression(expr);
         QML_END_INSTR(StoreSignal)
 
index 65333cc..f48b281 100644 (file)
@@ -53,6 +53,7 @@
 #include <QtDeclarative/qquickitem.h>
 
 #include <private/qdeclarativebinding_p.h>
+#include <private/qdeclarativeboundsignal_p.h>
 #include <private/qdeclarativeenginedebug_p.h>
 #include <private/qdeclarativedebugservice_p.h>
 #include <private/qdeclarativemetatype_p.h>
@@ -209,7 +210,15 @@ void tst_QDeclarativeEngineDebug::recursiveObjectTest(QObject *o, const QDeclara
 
         // signal properties are fake - they are generated from QDeclarativeBoundSignal children
         if (p.name().startsWith("on") && p.name().length() > 2 && p.name()[2].isUpper()) {
-            QVERIFY(p.value().toString().startsWith('{') && p.value().toString().endsWith('}'));
+            QList<QDeclarativeBoundSignal*> signalHandlers = o->findChildren<QDeclarativeBoundSignal*>();
+            QString signal = p.value().toString();
+            bool found = false;
+            for (int i = 0; i < signalHandlers.count(); ++i)
+                if (signalHandlers.at(i)->expression()->expression() == signal) {
+                    found = true;
+                    break;
+                }
+            QVERIFY(found);
             QVERIFY(p.valueTypeName().isEmpty());
             QVERIFY(p.binding().isEmpty());
             QVERIFY(!p.hasNotifySignal());
@@ -1031,7 +1040,7 @@ void tst_QDeclarativeEngineDebug::setBindingForObject()
     QDeclarativeDebugPropertyReference onEnteredRef = findProperty(mouseAreaObject.properties(), "onEntered");
 
     QCOMPARE(onEnteredRef.name(), QString("onEntered"));
-    QCOMPARE(onEnteredRef.value(),  QVariant("{ console.log('hello') }"));
+    QCOMPARE(onEnteredRef.value(),  QVariant("(function onEntered() { { console.log('hello') } })"));
 
     m_dbg->setBindingForObject(mouseAreaObject.debugId(), "onEntered", "{console.log('hello, world') }", false) ;