From 7e8e6b2d5be6419374cefdd2acf5c328890f2ec3 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Thu, 17 Nov 2011 12:53:26 +1000 Subject: [PATCH] Rewrite signal handlers at compile time. Task-number: QTBUG-22726 Change-Id: If2c3c00c646b2ed6b6899573a0b487c8deeaad3f Reviewed-by: Kai Koehne Reviewed-by: Aaron Kennedy --- src/declarative/qml/qdeclarativecompiler.cpp | 7 ++++--- src/declarative/qml/qdeclarativeinstruction_p.h | 1 - src/declarative/qml/qdeclarativerewrite.cpp | 5 +++++ src/declarative/qml/qdeclarativerewrite_p.h | 6 ++++++ src/declarative/qml/qdeclarativevme.cpp | 4 +--- .../tst_qdeclarativeenginedebug.cpp | 13 +++++++++++-- 6 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/declarative/qml/qdeclarativecompiler.cpp b/src/declarative/qml/qdeclarativecompiler.cpp index d3069ec..e7d9b80 100644 --- a/src/declarative/qml/qdeclarativecompiler.cpp +++ b/src/declarative/qml/qdeclarativecompiler.cpp @@ -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); diff --git a/src/declarative/qml/qdeclarativeinstruction_p.h b/src/declarative/qml/qdeclarativeinstruction_p.h index 8940e99..d73ce46 100644 --- a/src/declarative/qml/qdeclarativeinstruction_p.h +++ b/src/declarative/qml/qdeclarativeinstruction_p.h @@ -382,7 +382,6 @@ union QDeclarativeInstruction int signalIndex; int value; short context; - int name; ushort line; }; struct instr_assignSignalObject { diff --git a/src/declarative/qml/qdeclarativerewrite.cpp b/src/declarative/qml/qdeclarativerewrite.cpp index 98bd34f..dab7c3d 100644 --- a/src/declarative/qml/qdeclarativerewrite.cpp +++ b/src/declarative/qml/qdeclarativerewrite.cpp @@ -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 diff --git a/src/declarative/qml/qdeclarativerewrite_p.h b/src/declarative/qml/qdeclarativerewrite_p.h index c57e995..b5ea44f 100644 --- a/src/declarative/qml/qdeclarativerewrite_p.h +++ b/src/declarative/qml/qdeclarativerewrite_p.h @@ -119,6 +119,12 @@ private: int _inLoop; }; +class RewriteSignalHandler +{ +public: + QString operator()(const QString &code, const QString &name); +}; + } // namespace QDeclarativeRewrite QT_END_NAMESPACE diff --git a/src/declarative/qml/qdeclarativevme.cpp b/src/declarative/qml/qdeclarativevme.cpp index 8f11b28..24d2ef4 100644 --- a/src/declarative/qml/qdeclarativevme.cpp +++ b/src/declarative/qml/qdeclarativevme.cpp @@ -863,9 +863,7 @@ QObject *QDeclarativeVME::run(QList *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(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) diff --git a/tests/auto/declarative/debugger/qdeclarativeenginedebug/tst_qdeclarativeenginedebug.cpp b/tests/auto/declarative/debugger/qdeclarativeenginedebug/tst_qdeclarativeenginedebug.cpp index 65333cc..f48b281 100644 --- a/tests/auto/declarative/debugger/qdeclarativeenginedebug/tst_qdeclarativeenginedebug.cpp +++ b/tests/auto/declarative/debugger/qdeclarativeenginedebug/tst_qdeclarativeenginedebug.cpp @@ -53,6 +53,7 @@ #include #include +#include #include #include #include @@ -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 signalHandlers = o->findChildren(); + 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) ; -- 1.7.2.5