From 507a7a4e80c60fe90fb976aa60eda6881efd1016 Mon Sep 17 00:00:00 2001 From: Kai Koehne Date: Wed, 5 Oct 2011 09:20:51 +0200 Subject: [PATCH] V8Debugger: Code beautification Convert to from QString to QByteArray only when necessary. Also move private methods into private class. Change-Id: Iac28990f16c588e0172356c9395b7771f01f4817 Reviewed-on: http://codereview.qt-project.org/6022 Reviewed-by: Qt Sanity Bot Reviewed-by: Aurindam Jana --- src/declarative/debugger/qv8debugservice.cpp | 67 ++++++++++---------- src/declarative/debugger/qv8debugservice_p.h | 13 +--- src/declarative/qml/qdeclarativeboundsignal.cpp | 2 +- .../tst_qdeclarativedebugjs.cpp | 1 + 4 files changed, 39 insertions(+), 44 deletions(-) diff --git a/src/declarative/debugger/qv8debugservice.cpp b/src/declarative/debugger/qv8debugservice.cpp index 8486ddf..157bb8c 100644 --- a/src/declarative/debugger/qv8debugservice.cpp +++ b/src/declarative/debugger/qv8debugservice.cpp @@ -62,8 +62,8 @@ void DebugMessageHandler(const v8::Debug::Message& message) return; } - const QByteArray response(QV8Engine::toStringStatic( - message.GetJSON()).toUtf8()); + const QString response(QV8Engine::toStringStatic( + message.GetJSON())); QV8DebugService *service = QV8DebugService::instance(); service->debugMessageHandler(response); @@ -99,13 +99,16 @@ public: isolate->Dispose(); } + void sendDebugMessage(const QString &message); + static QByteArray packMessage(const QString &message); + bool initialized; QJSEngine *engine; v8::Isolate *isolate; QList engines; QEventLoop loop; QHash sourcePath; - QHash requestCache; + QHash requestCache; QHash eventList; }; @@ -150,9 +153,9 @@ void QV8DebugService::removeEngine(QDeclarativeEngine *engine) d->engines.removeAll(engine); } -void QV8DebugService::debugMessageHandler(QByteArray message) +void QV8DebugService::debugMessageHandler(const QString &message) { - sendMessage(packMessage(message)); + sendMessage(QV8DebugServicePrivate::packMessage(message)); } void QV8DebugService::executionStopped() @@ -164,7 +167,7 @@ void QV8DebugService::executionStopped() } } -void QV8DebugService::appendSourcePath(QByteArray message) +void QV8DebugService::appendSourcePath(const QString &message) { Q_D(QV8DebugService); @@ -174,9 +177,8 @@ void QV8DebugService::appendSourcePath(QByteArray message) receive any messages related to this operation */ { v8::Isolate::Scope i_scope(d->isolate); - QString req(message); QJSValue parser = d->engine->evaluate(QLatin1String("JSON.parse")); - QJSValue out = parser.call(QJSValue(), QJSValueList() << QJSValue(req)); + QJSValue out = parser.call(QJSValue(), QJSValueList() << QJSValue(message)); msgMap = out.toVariant().toMap(); } @@ -189,25 +191,24 @@ void QV8DebugService::appendSourcePath(QByteArray message) //Check if there are any pending breakpoint requests for this file if (d->requestCache.contains(fileName)) { - QList list = d->requestCache.values(fileName); + QList list = d->requestCache.values(fileName); d->requestCache.remove(fileName); - foreach (QByteArray request, list) { - request.replace(fileName.toUtf8(), sourcePath.toUtf8()); - sendDebugMessage(request); + foreach (QString request, list) { + request.replace(fileName, sourcePath); + d->sendDebugMessage(request); } } } -void QV8DebugService::signalEmitted(const char *signal) +void QV8DebugService::signalEmitted(const QString &signal) { //This function is only called by QDeclarativeBoundSignal //only if there is a slot connected to the signal. Hence, there //is no need for additional check. Q_D(QV8DebugService); - QString function(signal); //Parse just the name and remove the class info - if (d->eventList.key(function.left(function.indexOf(QLatin1String("("))))) { + if (d->eventList.key(signal.left(signal.indexOf(QLatin1String("("))))) { v8::Debug::DebugBreak(); } } @@ -221,8 +222,12 @@ void QV8DebugService::messageReceived(const QByteArray &message) ds >> command; if (command == "V8DEBUG") { - QByteArray request; - ds >> request; + QString request; + { + QByteArray requestArray; + ds >> requestArray; + request = QString::fromUtf8(requestArray); + } QVariantMap reqMap; /* Parse the byte string in a separate isolate @@ -230,9 +235,8 @@ void QV8DebugService::messageReceived(const QByteArray &message) receive any messages related to this operation */ { v8::Isolate::Scope i_scope(d->isolate); - QString req(request); QJSValue parser = d->engine->evaluate(QLatin1String("JSON.parse")); - QJSValue out = parser.call(QJSValue(), QJSValueList() << QJSValue(req)); + QJSValue out = parser.call(QJSValue(), QJSValueList() << QJSValue(request)); reqMap = out.toVariant().toMap(); } @@ -257,7 +261,7 @@ void QV8DebugService::messageReceived(const QByteArray &message) //Check if the filepath has been cached if (d->sourcePath.contains(fileName)) { QString filePath = d->sourcePath.value(fileName); - request.replace(fileName.toUtf8(), filePath.toUtf8()); + request.replace(fileName, filePath); } else { //Store the setbreakpoint message till filepath is resolved d->requestCache.insertMulti(fileName, request); @@ -282,8 +286,8 @@ void QV8DebugService::messageReceived(const QByteArray &message) // "success" : true // } { - v8::Isolate::Scope i_scope(d->isolate); - const QString obj("{}"); + v8::Isolate::Scope(d->isolate); + const QString obj(QLatin1String("{}")); QJSValue parser = d->engine->evaluate(QLatin1String("JSON.parse")); QJSValue jsonVal = parser.call(QJSValue(), QJSValueList() << obj); jsonVal.setProperty(QLatin1String("type"), QJSValue(QLatin1String("response"))); @@ -316,14 +320,14 @@ void QV8DebugService::messageReceived(const QByteArray &message) QJSValue stringify = d->engine->evaluate(QLatin1String("JSON.stringify")); QJSValue json = stringify.call(QJSValue(), QJSValueList() << jsonVal); - debugMessageHandler(json.toString().toUtf8()); + debugMessageHandler(json.toString()); } } } else if (debugCommand == QLatin1String("clearbreakpoint")) { //check if the breakpoint is a negative integer (event breakpoint) const QVariantMap arguments = reqMap.value(QLatin1String("arguments")).toMap(); - const int bp = arguments.value("breakpoint").toInt(); + const int bp = arguments.value(QLatin1String("breakpoint")).toInt(); if (bp < 0) { d->eventList.remove(bp); @@ -331,30 +335,27 @@ void QV8DebugService::messageReceived(const QByteArray &message) } } if (forwardRequestToV8) - sendDebugMessage(request); + d->sendDebugMessage(request); } } QDeclarativeDebugService::messageReceived(message); } -void QV8DebugService::sendDebugMessage(const QByteArray &msg) +void QV8DebugServicePrivate::sendDebugMessage(const QString &message) { - Q_D(QV8DebugService); + if (loop.isRunning()) + loop.exit(); - const QString message(msg); - if (d->loop.isRunning()) { - d->loop.exit(); - } v8::Debug::SendCommand(message.utf16(), message.size()); } -QByteArray QV8DebugService::packMessage(QByteArray &message) +QByteArray QV8DebugServicePrivate::packMessage(const QString &message) { QByteArray reply; QDataStream rs(&reply, QIODevice::WriteOnly); QByteArray cmd("V8DEBUG"); - rs << cmd << message; + rs << cmd << message.toUtf8(); return reply; } diff --git a/src/declarative/debugger/qv8debugservice_p.h b/src/declarative/debugger/qv8debugservice_p.h index 09b8f66..861e5f5 100644 --- a/src/declarative/debugger/qv8debugservice_p.h +++ b/src/declarative/debugger/qv8debugservice_p.h @@ -53,8 +53,6 @@ // We mean it. // -#include - #include "private/qdeclarativedebugservice_p.h" QT_BEGIN_HEADER @@ -64,7 +62,6 @@ QT_BEGIN_NAMESPACE QT_MODULE(Declarative) class QDeclarativeEngine; -class QJSEngine; class QV8DebugServicePrivate; class QV8DebugService : public QDeclarativeDebugService @@ -79,21 +76,17 @@ public: void addEngine(QDeclarativeEngine *); void removeEngine(QDeclarativeEngine *); - void debugMessageHandler(QByteArray message); + void debugMessageHandler(const QString &message); void executionStopped(); - void appendSourcePath(QByteArray message); + void appendSourcePath(const QString &message); - void signalEmitted(const char *signal); + void signalEmitted(const QString &signal); protected: void messageReceived(const QByteArray &); private: - void sendDebugMessage(const QByteArray &msg); - QByteArray packMessage(QByteArray &message); - -private: Q_DISABLE_COPY(QV8DebugService) Q_DECLARE_PRIVATE(QV8DebugService) }; diff --git a/src/declarative/qml/qdeclarativeboundsignal.cpp b/src/declarative/qml/qdeclarativeboundsignal.cpp index 1780fb7..11dec91 100644 --- a/src/declarative/qml/qdeclarativeboundsignal.cpp +++ b/src/declarative/qml/qdeclarativeboundsignal.cpp @@ -174,7 +174,7 @@ int QDeclarativeBoundSignal::qt_metacall(QMetaObject::Call c, int id, void **a) QDeclarativeDebugTrace::startRange(QDeclarativeDebugTrace::HandlingSignal); QDeclarativeDebugTrace::rangeData(QDeclarativeDebugTrace::HandlingSignal, QLatin1String(m_signal.signature()) % QLatin1String(": ") % m_expression->expression()); QDeclarativeDebugTrace::rangeLocation(QDeclarativeDebugTrace::HandlingSignal, m_expression->sourceFile(), m_expression->lineNumber()); - QV8DebugService::instance()->signalEmitted(m_signal.signature()); + QV8DebugService::instance()->signalEmitted(QString::fromAscii(m_signal.signature())); } m_isEvaluating = true; if (!m_paramsValid) { diff --git a/tests/auto/declarative/qdeclarativedebugjs/tst_qdeclarativedebugjs.cpp b/tests/auto/declarative/qdeclarativedebugjs/tst_qdeclarativedebugjs.cpp index d76fa74..14e13a8 100644 --- a/tests/auto/declarative/qdeclarativedebugjs/tst_qdeclarativedebugjs.cpp +++ b/tests/auto/declarative/qdeclarativedebugjs/tst_qdeclarativedebugjs.cpp @@ -297,6 +297,7 @@ void QJSDebugProcess::processAppOutput() continue; } } +// qWarning() << line; } m_mutex.unlock(); } -- 1.7.2.5