aboutsummaryrefslogtreecommitdiffstats
path: root/src/declarative/debugger/qv8debugservice.cpp
blob: 0020c440990aa77fb3e1609b969738620d1f551b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qv8debugservice_p.h"
#include "qdeclarativedebugservice_p_p.h"
#include "qv8debug_p.h"
#include "qv8engine_p.h"
#include "qdeclarativeengine_p.h"

#include <QtCore/QEventLoop>
#include <QtCore/QHash>
#include <QtCore/QFileInfo>

QT_BEGIN_NAMESPACE

Q_GLOBAL_STATIC(QV8DebugService, v8ServiceInstance)

void DebugMessageHandler(const v8::Debug::Message& message)
{
    v8::DebugEvent event = message.GetEvent();
    if (event != v8::Break && event != v8::Exception && event != v8::AfterCompile) {
        return;
    }

    QByteArray response(QV8Engine::toStringStatic(message.GetJSON()).toUtf8());

    QV8DebugService *service = QV8DebugService::instance();
    service->debugMessageHandler(response);

    if (event == v8::Break && !message.WillStartRunning()) {
        service->executionStopped();
    } else if (event == v8::AfterCompile) {
        service->appendSourcePath(response);
    } //TODO::v8::Exception
}

class QV8DebugServicePrivate : public QDeclarativeDebugServicePrivate
{
public:
    QJSEngine engine;
    QList<QDeclarativeEngine *> engines;
    QEventLoop loop;
    QHash<QString,QString> sourcePath;
    QHash<QString,QByteArray> requestCache;
};

QV8DebugService::QV8DebugService(QObject *parent)
    : QDeclarativeDebugService(*(new QV8DebugServicePrivate()), QLatin1String("V8Debugger"), parent)
{
    v8::Debug::SetMessageHandler2(DebugMessageHandler);
}

QV8DebugService::~QV8DebugService()
{
}

QV8DebugService *QV8DebugService::instance()
{
    return v8ServiceInstance();
}

void QV8DebugService::addEngine(QDeclarativeEngine *engine)
{
    Q_D(QV8DebugService);
    Q_ASSERT(engine);
    Q_ASSERT(!d->engines.contains(engine));

    d->engines.append(engine);
}

void QV8DebugService::removeEngine(QDeclarativeEngine *engine)
{
    Q_D(QV8DebugService);
    Q_ASSERT(engine);
    Q_ASSERT(d->engines.contains(engine));

    d->engines.removeAll(engine);
}

void QV8DebugService::debugMessageHandler(QByteArray message)
{
    sendMessage(packMessage(message));
}

void QV8DebugService::executionStopped()
{
    Q_D(QV8DebugService);

    d->loop.exec(QEventLoop::ExcludeUserInputEvents);
}

void QV8DebugService::appendSourcePath(QByteArray message)
{
    Q_D(QV8DebugService);

    QVariantMap msgMap;
    {
        QString msg(message);
        QJSValue parser = d->engine.evaluate("JSON.parse");
        QJSValue out = parser.call(QJSValue(), QJSValueList() << QJSValue(msg));
        msgMap = out.toVariant().toMap();
    }

    QString sourcePath(msgMap.value("body").toMap().value("script").toMap().value("name").toString());
    QString fileName(QFileInfo(sourcePath).fileName());

    d->sourcePath.insert(fileName, sourcePath);

    //Check if there are any pending breakpoint requests for this file
    if (d->requestCache.contains(fileName)) {
        QList<QByteArray> list = d->requestCache.values(fileName);
        d->requestCache.remove(fileName);
        foreach (QByteArray request, list) {
            request.replace(fileName.toUtf8(), sourcePath.toUtf8());
            sendDebugMessage(request);
        }
    }
}

void QV8DebugService::messageReceived(const QByteArray &message)
{
    Q_D(QV8DebugService);

    QDataStream ds(message);
    QByteArray command;
    ds >> command;

    if (command == "V8DEBUG") {
        QByteArray request;
        ds >> request;

        QVariantMap reqMap;
        {
            QString req(request);
            QJSValue parser = d->engine.evaluate("JSON.parse");
            QJSValue out = parser.call(QJSValue(), QJSValueList() << QJSValue(req));
            reqMap = out.toVariant().toMap();
        }

        QString debugCommand(reqMap.value("command").toString());

        if (debugCommand == QString("interrupt")) {
            v8::Debug::DebugBreak();

        } else {
            bool ok = true;
            if (debugCommand == QString("setbreakpoint")){
                QVariantMap arguments = reqMap.value("arguments").toMap();
                QString type(arguments.value("type").toString());
                if (type == QString("script")) {
                    QString fileName(arguments.value("target").toString());
                    //Check if the filepath has been cached
                    if (d->sourcePath.contains(fileName)) {
                        QString filePath = d->sourcePath.value(fileName);
                        request.replace(fileName.toUtf8(), filePath.toUtf8());
                    } else {
                        //Store the setbreakpoint message till filepath is resolved
                        d->requestCache.insertMulti(fileName, request);
                        ok = false;
                    }
                }
            }
            if (ok)
                sendDebugMessage(request);
        }
    }

    QDeclarativeDebugService::messageReceived(message);
}

void QV8DebugService::sendDebugMessage(const QByteArray &msg)
{
    Q_D(QV8DebugService);

    QString message(msg);
    if (d->loop.isRunning()) {
        d->loop.exit();
    }
    v8::Debug::SendCommand(message.utf16(), message.size());
}

QByteArray QV8DebugService::packMessage(QByteArray &message)
{
    QByteArray reply;
    QDataStream rs(&reply, QIODevice::WriteOnly);
    QByteArray cmd = "V8DEBUG";
    rs << cmd << message;
    return reply;
}

QT_END_NAMESPACE