scriptengine.cpp 24.6 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
con's avatar
con committed
29

30
31
#define QT_NO_CAST_FROM_ASCII

con's avatar
con committed
32
33
#include "scriptengine.h"

hjk's avatar
hjk committed
34
#include "breakhandler.h"
con's avatar
con committed
35
#include "debuggerconstants.h"
36
#include "debuggerdialogs.h"
hjk's avatar
hjk committed
37
#include "debuggerstringutils.h"
con's avatar
con committed
38
39
40
41
#include "moduleshandler.h"
#include "registerhandler.h"
#include "stackhandler.h"
#include "watchhandler.h"
hjk's avatar
hjk committed
42
#include "watchutils.h"
con's avatar
con committed
43

hjk's avatar
hjk committed
44
#include <utils/qtcassert.h>
con's avatar
con committed
45

46
47
#include <texteditor/itexteditor.h>
#include <coreplugin/ifile.h>
Friedemann Kleint's avatar
Friedemann Kleint committed
48
49
#include <coreplugin/scriptmanager/scriptmanager.h>
#include <coreplugin/icore.h>
50

con's avatar
con committed
51
52
53
54
55
56
#include <QtCore/QDateTime>
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QTimer>

57
#include <QtGui/QApplication>
con's avatar
con committed
58
59
60
61
62
63
64
65
66
67
#include <QtGui/QToolTip>

#include <QtScript/QScriptContext>
#include <QtScript/QScriptClassPropertyIterator>
#include <QtScript/QScriptContextInfo>
#include <QtScript/QScriptEngine>
#include <QtScript/QScriptEngineAgent>
#include <QtScript/QScriptValue>
#include <QtScript/QScriptValueIterator>

Friedemann Kleint's avatar
Friedemann Kleint committed
68
// #define DEBUG_SCRIPT 1
hjk's avatar
hjk committed
69
70
71
72
73
#if DEBUG_SCRIPT
#   define SDEBUG(s) qDebug() << s
#else
#   define SDEBUG(s)
#endif
74
# define XSDEBUG(s) qDebug() << s
con's avatar
con committed
75

76
77
78
79

namespace Debugger {
namespace Internal {

con's avatar
con committed
80
81
82
83
84
85
///////////////////////////////////////////////////////////////////////
//
// ScriptEngine
//
///////////////////////////////////////////////////////////////////////

86
class ScriptAgent : public QScriptEngineAgent
con's avatar
con committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
{
public:
    ScriptAgent(ScriptEngine *debugger, QScriptEngine *script);
    ~ScriptAgent() {}

    void contextPop();
    void contextPush();
    void exceptionCatch(qint64 scriptId, const QScriptValue &exception);
    void exceptionThrow(qint64 scriptId, const QScriptValue & exception,
        bool hasHandler);
    void functionEntry(qint64 scriptId);
    void functionExit(qint64 scriptId, const QScriptValue &returnValue);
    void positionChange(qint64 scriptId, int lineNumber, int columnNumber);
    void scriptLoad(qint64 id, const QString &program, const QString &fileName,
        int baseLineNumber);
    void scriptUnload(qint64 id);

private:
    void maybeBreakNow(bool byFunction);

107
    ScriptEngine *q;
con's avatar
con committed
108
109
110
111
112
113
114
115
};

ScriptAgent::ScriptAgent(ScriptEngine *debugger, QScriptEngine *script)
  : QScriptEngineAgent(script), q(debugger)
{}

void ScriptAgent::contextPop()
{
hjk's avatar
hjk committed
116
    SDEBUG("ScriptAgent::contextPop: ");
con's avatar
con committed
117
118
119
120
}

void ScriptAgent::contextPush()
{
hjk's avatar
hjk committed
121
    SDEBUG("ScriptAgent::contextPush: ");
con's avatar
con committed
122
123
124
125
}

void ScriptAgent::exceptionCatch(qint64 scriptId, const QScriptValue & exception)
{
126
127
    Q_UNUSED(scriptId)
    Q_UNUSED(exception)
Friedemann Kleint's avatar
Friedemann Kleint committed
128
129
130
    const QString msg = QString::fromLatin1("An exception was caught on %1: '%2'").
                        arg(scriptId).arg(exception.toString());
    SDEBUG(msg);
131
    q->showMessage(msg, LogMisc);
con's avatar
con committed
132
133
134
135
136
}

void ScriptAgent::exceptionThrow(qint64 scriptId, const QScriptValue &exception,
    bool hasHandler)
{
137
138
139
    Q_UNUSED(scriptId)
    Q_UNUSED(exception)
    Q_UNUSED(hasHandler)
Friedemann Kleint's avatar
Friedemann Kleint committed
140
141
142
    const QString msg = QString::fromLatin1("An exception occurred on %1: '%2'").
                        arg(scriptId).arg(exception.toString());
    SDEBUG(msg);
143
    q->showMessage(msg, LogMisc);
con's avatar
con committed
144
145
146
147
}

void ScriptAgent::functionEntry(qint64 scriptId)
{
148
    Q_UNUSED(scriptId)
149
    q->showMessage(QString::fromLatin1("Function entry occurred on %1").arg(scriptId), LogMisc);
Friedemann Kleint's avatar
Friedemann Kleint committed
150
    q->checkForBreakCondition(true);
con's avatar
con committed
151
152
153
154
}

void ScriptAgent::functionExit(qint64 scriptId, const QScriptValue &returnValue)
{
155
156
    Q_UNUSED(scriptId)
    Q_UNUSED(returnValue)
Friedemann Kleint's avatar
Friedemann Kleint committed
157
158
    const QString msg = QString::fromLatin1("Function exit occurred on %1: '%2'").arg(scriptId).arg(returnValue.toString());
    SDEBUG(msg);
159
    q->showMessage(msg, LogMisc);
con's avatar
con committed
160
161
162
163
}

void ScriptAgent::positionChange(qint64 scriptId, int lineNumber, int columnNumber)
{
hjk's avatar
hjk committed
164
    SDEBUG("ScriptAgent::position: " << lineNumber);
165
166
167
    Q_UNUSED(scriptId)
    Q_UNUSED(lineNumber)
    Q_UNUSED(columnNumber)
Friedemann Kleint's avatar
Friedemann Kleint committed
168
    q->checkForBreakCondition(false);
con's avatar
con committed
169
170
171
172
173
}

void ScriptAgent::scriptLoad(qint64 scriptId, const QString &program,
    const QString &fileName, int baseLineNumber)
{
174
175
176
177
    Q_UNUSED(scriptId)
    Q_UNUSED(program)
    Q_UNUSED(fileName)
    Q_UNUSED(baseLineNumber)
178
    q->showMessage(QString::fromLatin1("Loaded: %1 id: %2")
179
        .arg(fileName).arg(scriptId), LogMisc);
con's avatar
con committed
180
181
182
183
}

void ScriptAgent::scriptUnload(qint64 scriptId)
{
184
    Q_UNUSED(scriptId)
hjk's avatar
hjk committed
185
    SDEBUG("ScriptAgent::scriptUnload: " << scriptId);
con's avatar
con committed
186
187
188
189
190
191
192
193
194
}


///////////////////////////////////////////////////////////////////////
//
// ScriptEngine
//
///////////////////////////////////////////////////////////////////////

195
196
ScriptEngine::ScriptEngine(const DebuggerStartParameters &startParameters)
    : DebuggerEngine(startParameters)
con's avatar
con committed
197
198
199
200
201
202
203
204
205
{
}

ScriptEngine::~ScriptEngine()
{
}

void ScriptEngine::executeDebuggerCommand(const QString &command)
{
206
    Q_UNUSED(command)
207
    XSDEBUG("FIXME:  ScriptEngine::executeDebuggerCommand()");
con's avatar
con committed
208
209
210
211
}

void ScriptEngine::shutdown()
{
212
    exitDebugger();
con's avatar
con committed
213
214
215
216
}

void ScriptEngine::exitDebugger()
{
Friedemann Kleint's avatar
Friedemann Kleint committed
217
218
    if (state() == DebuggerNotReady)
        return;
hjk's avatar
hjk committed
219
    SDEBUG("ScriptEngine::exitDebugger()");
con's avatar
con committed
220
221
    m_stopped = false;
    m_stopOnNextLine = false;
Friedemann Kleint's avatar
Friedemann Kleint committed
222
223
224
225
226
227
228
229
    if (m_scriptEngine->isEvaluating())
        m_scriptEngine->abortEvaluation();
    setState(InferiorShuttingDown);
    setState(InferiorShutDown);

    setState(EngineShuttingDown);
    m_scriptEngine->setAgent(0);
    setState(DebuggerNotReady);
con's avatar
con committed
230
231
}

232
void ScriptEngine::startEngine()
con's avatar
con committed
233
{
234
    QTC_ASSERT(state() == EngineStarting, qDebug() << state());
hjk's avatar
hjk committed
235
    showMessage(_("STARTING SCRIPT DEBUGGER"), LogMisc);
Friedemann Kleint's avatar
Friedemann Kleint committed
236
237
    if (m_scriptEngine.isNull())
        m_scriptEngine = Core::ICore::instance()->scriptManager()->scriptEngine();
238
    if (!m_scriptAgent)
Friedemann Kleint's avatar
Friedemann Kleint committed
239
240
241
242
        m_scriptAgent.reset(new ScriptAgent(this, m_scriptEngine.data()));
    m_scriptEngine->setAgent(m_scriptAgent.data());
    /* Keep the gui alive (have the engine call processEvents() while the script
     * is run in the foreground). */
243
244
    m_scriptEngine->setProcessEventsInterval(1 /*ms*/);

con's avatar
con committed
245
246
247
    m_stopped = false;
    m_stopOnNextLine = false;
    m_scriptEngine->abortEvaluation();
248

249
    setState(EngineStarted);
Friedemann Kleint's avatar
Friedemann Kleint committed
250
251
    setState(InferiorStarting);

252
    m_scriptFileName = QFileInfo(startParameters().executable).absoluteFilePath();
hjk's avatar
hjk committed
253
    showMessage(_("SCRIPT FILE: ") + m_scriptFileName);
con's avatar
con committed
254
    QFile scriptFile(m_scriptFileName);
Friedemann Kleint's avatar
Friedemann Kleint committed
255
    if (!scriptFile.open(QIODevice::ReadOnly|QIODevice::Text)) {
256
        showMessage(QString::fromLatin1("Cannot open %1: %2").
257
          arg(m_scriptFileName, scriptFile.errorString()), LogError);
258
        startFailed();
hjk's avatar
hjk committed
259
260
        return;
    }
con's avatar
con committed
261
262
263
264
    QTextStream stream(&scriptFile);
    m_scriptContents = stream.readAll();
    scriptFile.close();
    attemptBreakpointSynchronization();
hjk's avatar
hjk committed
265
266
    setState(InferiorRunningRequested);
    showStatusMessage(tr("Running requested..."), 5000);
267
    showMessage(QLatin1String("Running: ") + m_scriptFileName, LogMisc);
hjk's avatar
hjk committed
268
    QTimer::singleShot(0, this, SLOT(runInferior()));
269
    startSuccessful();
con's avatar
con committed
270
271
272
273
}

void ScriptEngine::continueInferior()
{
hjk's avatar
hjk committed
274
    SDEBUG("ScriptEngine::continueInferior()");
con's avatar
con committed
275
276
277
278
    m_stopped = false;
    m_stopOnNextLine = false;
}

Friedemann Kleint's avatar
Friedemann Kleint committed
279
280
281
282
283
284
static const char *qtExtensionsC[] = {
    "qt.core", "qt.gui", "qt.xml", "qt.svg", "qt.network",
    "qt.sql", "qt.opengl", "qt.webkit", "qt.xmlpatterns", "qt.uitools"
};

bool ScriptEngine::importExtensions()
con's avatar
con committed
285
{
Friedemann Kleint's avatar
Friedemann Kleint committed
286
287
288
289
290
291
292
    SDEBUG("ScriptEngine::importExtensions()");
    QStringList extensions;
    const int extCount = sizeof(qtExtensionsC)/sizeof(const char *);
    for (int  e = 0; e < extCount; e++)
        extensions.append(QLatin1String(qtExtensionsC[e]));
    if (m_scriptEngine->importedExtensions().contains(extensions.front()))
        return true;
293
294
    QDir dir(QLatin1String("/home/apoenitz/dev/qtscriptgenerator"));
    if (!dir.cd(QLatin1String("plugins"))) {
295
        fprintf(stderr, "plugins folder does not exist -- did you build the bindings?\n");
Friedemann Kleint's avatar
Friedemann Kleint committed
296
        return false;
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
    }
    QStringList paths = qApp->libraryPaths();
    paths <<  dir.absolutePath();
    qApp->setLibraryPaths(paths);
    QStringList failExtensions;
    foreach (const QString &ext, extensions) {
        QScriptValue ret = m_scriptEngine->importExtension(ext);
        if (ret.isError())
            failExtensions.append(ext);
    }
    if (!failExtensions.isEmpty()) {
        if (failExtensions.size() == extensions.size()) {
            qWarning("Failed to import Qt bindings!\n"
                     "Plugins directory searched: %s/script\n"
                     "Make sure that the bindings have been built, "
                     "and that this executable and the plugins are "
                     "using compatible Qt libraries.", qPrintable(dir.absolutePath()));
        } else {
            qWarning("Failed to import some Qt bindings: %s\n"
                     "Plugins directory searched: %s/script\n"
                     "Make sure that the bindings have been built, "
                     "and that this executable and the plugins are "
                     "using compatible Qt libraries.",
320
                     qPrintable(failExtensions.join(QLatin1String(", "))), qPrintable(dir.absolutePath()));
321
322
        }
    }
Friedemann Kleint's avatar
Friedemann Kleint committed
323
324
    return failExtensions.isEmpty();
}
325

Friedemann Kleint's avatar
Friedemann Kleint committed
326
327
328
329
330
331
332
333
void ScriptEngine::runInferior()
{
    SDEBUG("ScriptEngine::runInferior()");
    importExtensions();
    setState(InferiorRunning);
    const QScriptValue result = m_scriptEngine->evaluate(m_scriptContents, m_scriptFileName);
    setState(InferiorStopping);
    setState(InferiorStopped);
334
    QString msg;
Friedemann Kleint's avatar
Friedemann Kleint committed
335
    if (m_scriptEngine->hasUncaughtException()) {
336
337
338
339
340
        msg = QString::fromLatin1("An exception occurred during execution at line: %1\n%2\n")
            .arg(m_scriptEngine->uncaughtExceptionLineNumber())
            .arg(m_scriptEngine->uncaughtException().toString());
        msg += m_scriptEngine->uncaughtExceptionBacktrace()
            .join(QString(QLatin1Char('\n')));
Friedemann Kleint's avatar
Friedemann Kleint committed
341
    } else {
342
343
        msg = QString::fromLatin1("Evaluation returns '%1'")
            .arg(result.toString());
Friedemann Kleint's avatar
Friedemann Kleint committed
344
    }
345
    showMessage(msg, LogMisc);
Friedemann Kleint's avatar
Friedemann Kleint committed
346
    exitDebugger();
con's avatar
con committed
347
348
349
350
351
352
}

void ScriptEngine::interruptInferior()
{
    m_stopped = false;
    m_stopOnNextLine = true;
353
    XSDEBUG("ScriptEngine::interruptInferior()");
con's avatar
con committed
354
355
}

hjk's avatar
hjk committed
356
void ScriptEngine::executeStep()
con's avatar
con committed
357
{
358
    //SDEBUG("ScriptEngine::stepExec()");
con's avatar
con committed
359
360
361
362
    m_stopped = false;
    m_stopOnNextLine = true;
}

hjk's avatar
hjk committed
363
void ScriptEngine::executeStepI()
con's avatar
con committed
364
{
365
    //SDEBUG("ScriptEngine::stepIExec()");
con's avatar
con committed
366
367
368
369
    m_stopped = false;
    m_stopOnNextLine = true;
}

hjk's avatar
hjk committed
370
void ScriptEngine::executeStepOut()
con's avatar
con committed
371
{
372
    //SDEBUG("ScriptEngine::stepOutExec()");
con's avatar
con committed
373
374
375
376
    m_stopped = false;
    m_stopOnNextLine = true;
}

hjk's avatar
hjk committed
377
void ScriptEngine::executeNext()
con's avatar
con committed
378
{
379
    //SDEBUG("ScriptEngine::nextExec()");
con's avatar
con committed
380
381
382
383
    m_stopped = false;
    m_stopOnNextLine = true;
}

hjk's avatar
hjk committed
384
void ScriptEngine::executeNextI()
con's avatar
con committed
385
{
386
    //SDEBUG("ScriptEngine::nextIExec()");
con's avatar
con committed
387
388
389
390
    m_stopped = false;
    m_stopOnNextLine = true;
}

hjk's avatar
hjk committed
391
void ScriptEngine::executeRunToLine(const QString &fileName, int lineNumber)
con's avatar
con committed
392
{
393
394
    Q_UNUSED(fileName)
    Q_UNUSED(lineNumber)
hjk's avatar
hjk committed
395
    SDEBUG("FIXME:  ScriptEngine::runToLineExec()");
con's avatar
con committed
396
397
}

hjk's avatar
hjk committed
398
void ScriptEngine::executeRunToFunction(const QString &functionName)
con's avatar
con committed
399
{
400
    Q_UNUSED(functionName)
401
    XSDEBUG("FIXME:  ScriptEngine::runToFunctionExec()");
con's avatar
con committed
402
403
}

hjk's avatar
hjk committed
404
void ScriptEngine::executeJumpToLine(const QString &fileName, int lineNumber)
con's avatar
con committed
405
{
406
407
    Q_UNUSED(fileName)
    Q_UNUSED(lineNumber)
408
    XSDEBUG("FIXME:  ScriptEngine::jumpToLineExec()");
con's avatar
con committed
409
410
411
412
}

void ScriptEngine::activateFrame(int index)
{
413
    Q_UNUSED(index)
con's avatar
con committed
414
415
416
417
}

void ScriptEngine::selectThread(int index)
{
418
    Q_UNUSED(index)
con's avatar
con committed
419
420
421
422
}

void ScriptEngine::attemptBreakpointSynchronization()
{
423
    BreakHandler *handler = breakHandler();
con's avatar
con committed
424
425
426
427
428
429
430
431
    bool updateNeeded = false;
    for (int index = 0; index != handler->size(); ++index) {
        BreakpointData *data = handler->at(index);
        if (data->pending) {
            data->pending = false; // FIXME
            updateNeeded = true;
        }
        if (data->bpNumber.isEmpty()) {
432
            data->bpNumber = QByteArray::number(index + 1);
con's avatar
con committed
433
434
            updateNeeded = true;
        }
435
436
437
        if (!data->fileName.isEmpty() && data->markerFileName().isEmpty()) {
            data->setMarkerFileName(data->fileName);
            data->setMarkerLineNumber(data->lineNumber.toInt());
con's avatar
con committed
438
439
440
441
442
443
444
445
446
            updateNeeded = true;
        }
    }
    if (updateNeeded)
        handler->updateMarkers();
}

void ScriptEngine::loadSymbols(const QString &moduleName)
{
447
    Q_UNUSED(moduleName)
con's avatar
con committed
448
449
450
451
452
453
454
455
456
457
}

void ScriptEngine::loadAllSymbols()
{
}

void ScriptEngine::reloadModules()
{
}

hjk's avatar
hjk committed
458
void ScriptEngine::requestModuleSymbols(const QString & /*moduleName*/)
459
460
{
}
con's avatar
con committed
461
462
463
464
465
466
467
468


//////////////////////////////////////////////////////////////////////
//
// Tooltip specific stuff
//
//////////////////////////////////////////////////////////////////////

Friedemann Kleint's avatar
Friedemann Kleint committed
469

con's avatar
con committed
470
471
472
473
static WatchData m_toolTip;
static QPoint m_toolTipPos;
static QHash<QString, WatchData> m_toolTipCache;

hjk's avatar
hjk committed
474
475
void ScriptEngine::setToolTipExpression(const QPoint &mousePos,
    TextEditor::ITextEditor *editor, int cursorPos)
con's avatar
con committed
476
{
477
478
479
    Q_UNUSED(mousePos)
    Q_UNUSED(editor)
    Q_UNUSED(cursorPos)
con's avatar
con committed
480

hjk's avatar
hjk committed
481
    if (state() != InferiorStopped) {
hjk's avatar
hjk committed
482
        //SDEBUG("SUPPRESSING DEBUGGER TOOLTIP, INFERIOR NOT STOPPED");
con's avatar
con committed
483
484
        return;
    }
485
    // Check mime type and get expression (borrowing some C++ - functions)
hjk's avatar
hjk committed
486
    const QString javaScriptMimeType =
Friedemann Kleint's avatar
Friedemann Kleint committed
487
        QLatin1String("application/javascript");
488
489
    if (!editor->file() || editor->file()->mimeType() != javaScriptMimeType)
        return;
con's avatar
con committed
490

491
492
493
    int line;
    int column;
    QString exp = cppExpressionAt(editor, cursorPos, &line, &column);
con's avatar
con committed
494
495
496
497
498
499
500
501
502
503
504

/*
    if (m_toolTipCache.contains(exp)) {
        const WatchData & data = m_toolTipCache[exp];
        q->watchHandler()->removeChildren(data.iname);
        insertData(data);
        return;
    }
*/

    QToolTip::hideText();
505
    if (exp.isEmpty() || exp.startsWith(QLatin1Char('#')))  {
con's avatar
con committed
506
507
508
509
510
        QToolTip::hideText();
        return;
    }

    if (!hasLetterOrNumber(exp)) {
511
        QToolTip::showText(m_toolTipPos, tr("'%1' contains no identifier").arg(exp));
con's avatar
con committed
512
513
514
        return;
    }

515
    if (exp.startsWith(QLatin1Char('"')) && exp.endsWith(QLatin1Char('"'))) {
516
        QToolTip::showText(m_toolTipPos, tr("String literal %1").arg(exp));
con's avatar
con committed
517
518
519
        return;
    }

520
521
    if (exp.startsWith(QLatin1String("++")) || exp.startsWith(QLatin1String("--")))
        exp.remove(0, 2);
con's avatar
con committed
522

523
524
    if (exp.endsWith(QLatin1String("++")) || exp.endsWith(QLatin1String("--")))
        exp.remove(0, 2);
con's avatar
con committed
525

526
    if (exp.startsWith(QLatin1Char('<')) || exp.startsWith(QLatin1Char('[')))
con's avatar
con committed
527
528
529
530
        return;

    if (hasSideEffects(exp)) {
        QToolTip::showText(m_toolTipPos,
531
532
            tr("Cowardly refusing to evaluate expression '%1' "
               "with potential side effects").arg(exp));
con's avatar
con committed
533
534
535
536
        return;
    }

#if 0
hjk's avatar
hjk committed
537
    //if (m_manager->status() != InferiorStopped)
con's avatar
con committed
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
    //    return;

    // FIXME: 'exp' can contain illegal characters
    m_toolTip = WatchData();
    m_toolTip.exp = exp;
    m_toolTip.name = exp;
    m_toolTip.iname = tooltipIName;
    insertData(m_toolTip);
#endif
}


//////////////////////////////////////////////////////////////////////
//
// Watch specific stuff
//
//////////////////////////////////////////////////////////////////////

void ScriptEngine::assignValueInDebugger(const QString &expression,
    const QString &value)
{
hjk's avatar
hjk committed
559
    SDEBUG("ASSIGNING: " << (expression + QLatin1Char('=') + value));
560
    m_scriptEngine->evaluate(expression + QLatin1Char('=') + value);
561
    updateLocals();
con's avatar
con committed
562
563
}

Friedemann Kleint's avatar
Friedemann Kleint committed
564
565
static BreakpointData *findBreakPointByFunction(BreakHandler *handler,
                                                const QString &functionName)
con's avatar
con committed
566
{
Friedemann Kleint's avatar
Friedemann Kleint committed
567
568
569
570
571
572
573
574
    const int count = handler->size();
    for (int b = 0; b < count; b++) {
        BreakpointData *data = handler->at(b);
        if (data->funcName == functionName)
            return data;
    }
    return 0;
}
con's avatar
con committed
575

Friedemann Kleint's avatar
Friedemann Kleint committed
576
577
578
579
580
581
582
583
584
585
586
587
static BreakpointData *findBreakPointByFileName(BreakHandler *handler,
                                              int lineNumber,
                                              const QString &fileName)
{
    const int count = handler->size();
    for (int b = 0; b < count; b++) {
        BreakpointData *data = handler->at(b);
        if (lineNumber == data->lineNumber.toInt() && fileName == data->fileName)
            return data;
    }
    return 0;
}
con's avatar
con committed
588

Friedemann Kleint's avatar
Friedemann Kleint committed
589
590
591
592
bool ScriptEngine::checkForBreakCondition(bool byFunction)
{
    const QScriptContext *context = m_scriptEngine->currentContext();
    const QScriptContextInfo info(context);
con's avatar
con committed
593

Friedemann Kleint's avatar
Friedemann Kleint committed
594
595
596
    // Update breakpoints
    const QString functionName = info.functionName();
    const QString fileName = info.fileName();
597
598
599
600
    const int lineNumber = byFunction
        ? info.functionStartLineNumber() : info.lineNumber();
    SDEBUG("checkForBreakCondition" << byFunction << functionName
        << lineNumber << fileName);
con's avatar
con committed
601
    if (m_stopOnNextLine) {
Friedemann Kleint's avatar
Friedemann Kleint committed
602
        // Interrupt inferior
con's avatar
con committed
603
604
        m_stopOnNextLine = false;
    } else {
Friedemann Kleint's avatar
Friedemann Kleint committed
605
606
607
        if (byFunction && functionName.isEmpty())
            return false;
        BreakpointData *data = byFunction ?
608
609
           findBreakPointByFunction(breakHandler(), functionName) :
           findBreakPointByFileName(breakHandler(), lineNumber, fileName);
Friedemann Kleint's avatar
Friedemann Kleint committed
610
611
        if (!data)
            return false;
con's avatar
con committed
612

613
614
615
616
617
        // Skip disabled breakpoint.
        if (!data->enabled)
            return false;

        // We just run into a breakpoint.
hjk's avatar
hjk committed
618
        //SDEBUG("RESOLVING BREAKPOINT AT " << fileName << lineNumber);
619
        data->bpLineNumber = QByteArray::number(lineNumber);
con's avatar
con committed
620
621
        data->bpFileName = fileName;
        data->bpFuncName = functionName;
622
623
        data->setMarkerLineNumber(lineNumber);
        data->setMarkerFileName(fileName);
con's avatar
con committed
624
625
626
        data->pending = false;
        data->updateMarker();
    }
Friedemann Kleint's avatar
Friedemann Kleint committed
627
    setState(InferiorStopping);
hjk's avatar
hjk committed
628
    setState(InferiorStopped);
Friedemann Kleint's avatar
Friedemann Kleint committed
629
630
    SDEBUG("Stopped at " << lineNumber << fileName);
    showStatusMessage(tr("Stopped at %1:%2.").arg(fileName).arg(lineNumber), 5000);
hjk's avatar
hjk committed
631

632
    StackFrame frame;
633
    frame.file = fileName;
634
    frame.line = lineNumber;
635
    gotoLocation(frame, true);
636
    updateLocals();
Friedemann Kleint's avatar
Friedemann Kleint committed
637
    return true;
638
}
con's avatar
con committed
639

640
641
642
void ScriptEngine::updateLocals()
{
    QScriptContext *context = m_scriptEngine->currentContext();
643
    watchHandler()->beginCycle();
hjk's avatar
hjk committed
644
    //SDEBUG("UPDATE LOCALS");
con's avatar
con committed
645
646
647

    //
    // Build stack
648
    //
con's avatar
con committed
649
650
651
    QList<StackFrame> stackFrames;
    int i = 0;
    for (QScriptContext *c = context; c; c = c->parentContext(), ++i) {
Friedemann Kleint's avatar
Friedemann Kleint committed
652
        const QScriptContextInfo info(c);
con's avatar
con committed
653
654
655
656
657
658
659
660
        StackFrame frame;
        frame.level = i;
        frame.file = info.fileName();
        frame.function = info.functionName();
        frame.from = QString::number(info.functionStartLineNumber());
        frame.to = QString::number(info.functionEndLineNumber());
        frame.line = info.lineNumber();
        if (frame.function.isEmpty())
Friedemann Kleint's avatar
Friedemann Kleint committed
661
            frame.function = QLatin1String("<global scope>");
con's avatar
con committed
662
663
664
        //frame.address = ...;
        stackFrames.append(frame);
    }
665
    stackHandler()->setFrames(stackFrames);
con's avatar
con committed
666
667

    //
668
    // Build locals, deactivate agent meanwhile.
con's avatar
con committed
669
    //
670
671
    m_scriptEngine->setAgent(0);

con's avatar
con committed
672
673
    WatchData data;
    data.iname = "local";
674
    data.name = QString::fromLatin1(data.iname);
con's avatar
con committed
675
    data.scriptValue = context->activationObject();
676
    watchHandler()->beginCycle();
hjk's avatar
hjk committed
677
    updateSubItem(data);
678
    watchHandler()->endCycle();
con's avatar
con committed
679
680
    // FIXME: Use an extra thread. This here is evil
    m_stopped = true;
hjk's avatar
hjk committed
681
    showStatusMessage(tr("Stopped."), 5000);
con's avatar
con committed
682
    while (m_stopped) {
hjk's avatar
hjk committed
683
        //SDEBUG("LOOPING");
con's avatar
con committed
684
685
        QApplication::processEvents();
    }
686
687
688
689
690
691
    setState(InferiorRunningRequested);
    setState(InferiorRunning);
    // Clear any exceptions occurred during locals evaluation.
    m_scriptEngine->clearExceptions();
    m_scriptEngine->setAgent(m_scriptAgent.data());
    SDEBUG("Continuing");
con's avatar
con committed
692
693
}

hjk's avatar
hjk committed
694
void ScriptEngine::updateWatchData(const WatchData &data)
con's avatar
con committed
695
{
hjk's avatar
hjk committed
696
    updateSubItem(data);
697
698
699
700
701
702
703
704
705
706
}

static inline QString msgDebugInsert(const WatchData &d0, const QList<WatchData>& children)
{
    QString rc;
    QTextStream str(&rc);
    str << "INSERTING " << d0.toString() << '\n';
    foreach(const WatchData &c, children)
        str << "          " << c.toString() << '\n';
    return rc;
con's avatar
con committed
707
708
709
710
711
}

void ScriptEngine::updateSubItem(const WatchData &data0)
{
    WatchData data = data0;
712
713
    QList<WatchData> children;
    SDEBUG("\nUPDATE SUBITEM: " << data.toString() << data.scriptValue.toString());
hjk's avatar
hjk committed
714
    QTC_ASSERT(data.isValid(), return);
con's avatar
con committed
715
716

    if (data.isTypeNeeded() || data.isValueNeeded()) {
717
        const QScriptValue &ob = data.scriptValue;
con's avatar
con committed
718
        if (ob.isArray()) {
719
720
            data.setType(QLatin1String("Array"), false);
            data.setValue(QString(QLatin1Char(' ')));
con's avatar
con committed
721
        } else if (ob.isBool()) {
722
723
            data.setType(QLatin1String("Bool"), false);
            data.setValue(ob.toBool() ? QLatin1String("true") : QLatin1String("false"));
724
            data.setHasChildren(false);
con's avatar
con committed
725
        } else if (ob.isDate()) {
726
727
            data.setType(QLatin1String("Date"), false);
            data.setValue(ob.toDateTime().toString());
728
            data.setHasChildren(false);
con's avatar
con committed
729
        } else if (ob.isError()) {
730
731
            data.setType(QLatin1String("Error"), false);
            data.setValue(QString(QLatin1Char(' ')));
con's avatar
con committed
732
        } else if (ob.isFunction()) {
733
734
            data.setType(QLatin1String("Function"), false);
            data.setValue(QString(QLatin1Char(' ')));
con's avatar
con committed
735
        } else if (ob.isNull()) {
736
737
738
            const QString nullValue = QLatin1String("<null>");
            data.setType(nullValue, false);
            data.setValue(nullValue);
con's avatar
con committed
739
        } else if (ob.isNumber()) {
740
741
            data.setType(QLatin1String("Number"), false);
            data.setValue(QString::number(ob.toNumber()));
742
            data.setHasChildren(false);
con's avatar
con committed
743
        } else if (ob.isObject()) {
744
745
            data.setType(QLatin1String("Object"), false);
            data.setValue(QString(QLatin1Char(' ')));
con's avatar
con committed
746
        } else if (ob.isQMetaObject()) {
747
748
            data.setType(QLatin1String("QMetaObject"), false);
            data.setValue(QString(QLatin1Char(' ')));
con's avatar
con committed
749
        } else if (ob.isQObject()) {
750
751
            data.setType(QLatin1String("QObject"), false);
            data.setValue(QString(QLatin1Char(' ')));
con's avatar
con committed
752
        } else if (ob.isRegExp()) {
753
754
            data.setType(QLatin1String("RegExp"), false);
            data.setValue(ob.toRegExp().pattern());
con's avatar
con committed
755
        } else if (ob.isString()) {
756
757
            data.setType(QLatin1String("String"), false);
            data.setValue(ob.toString());
con's avatar
con committed
758
        } else if (ob.isVariant()) {
759
760
            data.setType(QLatin1String("Variant"), false);
            data.setValue(QString(QLatin1Char(' ')));
con's avatar
con committed
761
        } else if (ob.isUndefined()) {
762
763
            data.setType(QLatin1String("<undefined>"), false);
            data.setValue(QLatin1String("<unknown>"));
hjk's avatar
hjk committed
764
            data.setHasChildren(false);
con's avatar
con committed
765
        } else {
766
767
768
            const QString unknown = QLatin1String("<unknown>");
            data.setType(unknown, false);
            data.setValue(unknown);
hjk's avatar
hjk committed
769
            data.setHasChildren(false);
con's avatar
con committed
770
771
772
773
774
775
776
777
        }
    }

    if (data.isChildrenNeeded()) {
        QScriptValueIterator it(data.scriptValue);
        while (it.hasNext()) {
            it.next();
            WatchData data1;
778
            data1.iname = data.iname + '.' + it.name().toLatin1();
779
            data1.exp = it.name().toLatin1();
con's avatar
con committed
780
781
            data1.name = it.name();
            data1.scriptValue = it.value();
782
            if (watchHandler()->isExpandedIName(data1.iname)) {
con's avatar
con committed
783
                data1.setChildrenNeeded();
784
            } else {
con's avatar
con committed
785
                data1.setChildrenUnneeded();
786
787
            }
            children.push_back(data1);
con's avatar
con committed
788
        }
789
        data.setHasChildren(!children.isEmpty());
con's avatar
con committed
790
791
792
        data.setChildrenUnneeded();
    }

793
    if (data.isHasChildrenNeeded()) {
con's avatar
con committed
794
        QScriptValueIterator it(data.scriptValue);
795
        data.setHasChildren(it.hasNext());
con's avatar
con committed
796
797
    }

798
    SDEBUG(msgDebugInsert(data, children));
799
    watchHandler()->insertData(data);
800
    if (!children.isEmpty())
801
        watchHandler()->insertBulkData(children);
con's avatar
con committed
802
803
}

804
DebuggerEngine *createScriptEngine(const DebuggerStartParameters &sp)
con's avatar
con committed
805
{
806
    return new ScriptEngine(sp);
con's avatar
con committed
807
808
}

809
810
} // namespace Internal
} // namespace Debugger