scriptengine.cpp 25.3 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
hjk's avatar
hjk committed
3 4
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22
** 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.
23
**
hjk's avatar
hjk committed
24 25
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
con's avatar
con committed
29 30 31

#include "scriptengine.h"

Friedemann Kleint's avatar
Friedemann Kleint committed
32
#include "debuggerstartparameters.h"
hjk's avatar
hjk committed
33
#include "breakhandler.h"
con's avatar
con committed
34
#include "debuggerconstants.h"
hjk's avatar
hjk committed
35
#include "debuggercore.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"
43
#include "debuggertooltipmanager.h"
con's avatar
con committed
44

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

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

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

58 59 60
#include <QApplication>
#include <QMessageBox>
#include <QToolTip>
con's avatar
con committed
61

62 63 64 65 66 67 68
#include <QScriptContext>
#include <QScriptClassPropertyIterator>
#include <QScriptContextInfo>
#include <QScriptEngine>
#include <QScriptEngineAgent>
#include <QScriptValue>
#include <QScriptValueIterator>
con's avatar
con committed
69

70 71 72 73

namespace Debugger {
namespace Internal {

74 75 76 77 78
enum { debugScript = 0 };

#define SDEBUG(s) do { if (debugScript) qDebug() << s; } while (0)
#define XSDEBUG(s) qDebug() << s

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

85
class ScriptAgent : public QScriptEngineAgent
con's avatar
con committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
{
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);

hjk's avatar
hjk committed
103 104
    void showMessage(const QString &msg);

con's avatar
con committed
105 106 107
private:
    void maybeBreakNow(bool byFunction);

108
    ScriptEngine *q;
hjk's avatar
hjk committed
109 110
    int m_depth;
    int m_contextDepth;
con's avatar
con committed
111 112 113
};

ScriptAgent::ScriptAgent(ScriptEngine *debugger, QScriptEngine *script)
hjk's avatar
hjk committed
114
  : QScriptEngineAgent(script), q(debugger), m_depth(0), m_contextDepth(0)
con's avatar
con committed
115 116
{}

hjk's avatar
hjk committed
117 118 119 120 121 122
void ScriptAgent::showMessage(const QString &msg)
{
    SDEBUG(msg);
    q->showMessage(msg, LogMisc);
}

con's avatar
con committed
123 124
void ScriptAgent::contextPop()
{
hjk's avatar
hjk committed
125 126
    //showMessage(_("ContextPop: %1").arg(m_contextDepth));
    --m_contextDepth;
con's avatar
con committed
127 128 129 130
}

void ScriptAgent::contextPush()
{
hjk's avatar
hjk committed
131 132
    ++m_contextDepth;
    //showMessage(_("ContextPush: %1 ").arg(m_contextDepth));
con's avatar
con committed
133 134 135 136
}

void ScriptAgent::exceptionCatch(qint64 scriptId, const QScriptValue & exception)
{
137 138
    Q_UNUSED(scriptId)
    Q_UNUSED(exception)
hjk's avatar
hjk committed
139 140
    showMessage(_("An exception was caught on %1: '%2'").
                   arg(scriptId).arg(exception.toString()));
con's avatar
con committed
141 142 143 144 145
}

void ScriptAgent::exceptionThrow(qint64 scriptId, const QScriptValue &exception,
    bool hasHandler)
{
146 147 148
    Q_UNUSED(scriptId)
    Q_UNUSED(exception)
    Q_UNUSED(hasHandler)
hjk's avatar
hjk committed
149 150
    showMessage(_("An exception occurred on %1: '%2'").
                   arg(scriptId).arg(exception.toString()));
con's avatar
con committed
151 152 153 154
}

void ScriptAgent::functionEntry(qint64 scriptId)
{
155
    Q_UNUSED(scriptId)
hjk's avatar
hjk committed
156 157
    ++m_depth;
    //showMessage(_("Function entry occurred on %1, depth: %2").arg(scriptId));
Friedemann Kleint's avatar
Friedemann Kleint committed
158
    q->checkForBreakCondition(true);
con's avatar
con committed
159 160 161 162
}

void ScriptAgent::functionExit(qint64 scriptId, const QScriptValue &returnValue)
{
163 164
    Q_UNUSED(scriptId)
    Q_UNUSED(returnValue)
hjk's avatar
hjk committed
165 166 167
    --m_depth;
    //showMessage(_("Function exit occurred on %1: '%2'").
    //              arg(scriptId).arg(returnValue.toString()));
con's avatar
con committed
168 169 170 171
}

void ScriptAgent::positionChange(qint64 scriptId, int lineNumber, int columnNumber)
{
172 173 174
    Q_UNUSED(scriptId)
    Q_UNUSED(lineNumber)
    Q_UNUSED(columnNumber)
hjk's avatar
hjk committed
175
    //showMessage(_("Position: %1").arg(lineNumber));
Friedemann Kleint's avatar
Friedemann Kleint committed
176
    q->checkForBreakCondition(false);
con's avatar
con committed
177 178 179 180 181
}

void ScriptAgent::scriptLoad(qint64 scriptId, const QString &program,
    const QString &fileName, int baseLineNumber)
{
182 183 184 185
    Q_UNUSED(scriptId)
    Q_UNUSED(program)
    Q_UNUSED(fileName)
    Q_UNUSED(baseLineNumber)
hjk's avatar
hjk committed
186
    showMessage(_("Loaded: %1 id: %2").arg(fileName).arg(scriptId));
con's avatar
con committed
187 188 189 190
}

void ScriptAgent::scriptUnload(qint64 scriptId)
{
191
    Q_UNUSED(scriptId)
hjk's avatar
hjk committed
192
    showMessage(_("Unload script id %1 ").arg(scriptId));
con's avatar
con committed
193 194 195 196 197 198 199 200 201
}


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

202
ScriptEngine::ScriptEngine(const DebuggerStartParameters &startParameters)
203
    : DebuggerEngine(startParameters)
con's avatar
con committed
204
{
Friedemann Kleint's avatar
Friedemann Kleint committed
205
    setObjectName(QLatin1String("ScriptEngine"));
con's avatar
con committed
206 207 208 209 210 211
}

ScriptEngine::~ScriptEngine()
{
}

212
void ScriptEngine::executeDebuggerCommand(const QString &command, DebuggerLanguages languages)
con's avatar
con committed
213
{
214
    Q_UNUSED(command)
215
    Q_UNUSED(languages)
216
    XSDEBUG("FIXME:  ScriptEngine::executeDebuggerCommand()");
con's avatar
con committed
217 218
}

hjk's avatar
hjk committed
219
void ScriptEngine::shutdownInferior()
con's avatar
con committed
220
{
hjk's avatar
hjk committed
221 222 223 224
    QTC_ASSERT(state() == InferiorShutdownRequested, qDebug() << state());
    SDEBUG("ScriptEngine::shutdownInferior()");
    m_scriptEngine->setAgent(0);
    //m_scriptAgent.reset(0);
con's avatar
con committed
225 226
    m_stopped = false;
    m_stopOnNextLine = false;
Friedemann Kleint's avatar
Friedemann Kleint committed
227 228
    if (m_scriptEngine->isEvaluating())
        m_scriptEngine->abortEvaluation();
hjk's avatar
hjk committed
229 230
    notifyInferiorShutdownOk();
}
Friedemann Kleint's avatar
Friedemann Kleint committed
231

hjk's avatar
hjk committed
232 233 234
void ScriptEngine::shutdownEngine()
{
    QTC_ASSERT(state() == EngineShutdownRequested, qDebug() << state());
Friedemann Kleint's avatar
Friedemann Kleint committed
235
    m_scriptEngine->setAgent(0);
hjk's avatar
hjk committed
236
    notifyEngineShutdownOk();
con's avatar
con committed
237 238
}

hjk's avatar
hjk committed
239
void ScriptEngine::setupEngine()
con's avatar
con committed
240
{
hjk's avatar
hjk committed
241
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());
hjk's avatar
hjk committed
242
    showMessage(_("STARTING SCRIPT DEBUGGER"), LogMisc);
Friedemann Kleint's avatar
Friedemann Kleint committed
243
    if (m_scriptEngine.isNull())
hjk's avatar
hjk committed
244
        m_scriptEngine = Core::ICore::scriptManager()->scriptEngine();
245
    QTC_CHECK(!m_scriptAgent);
hjk's avatar
hjk committed
246
    m_scriptAgent.reset(new ScriptAgent(this, m_scriptEngine.data()));
Friedemann Kleint's avatar
Friedemann Kleint committed
247
    m_scriptEngine->setAgent(m_scriptAgent.data());
hjk's avatar
hjk committed
248
    //m_scriptEngine->setAgent(new ScriptAgent(this, m_scriptEngine.data()));
Friedemann Kleint's avatar
Friedemann Kleint committed
249 250
    /* Keep the gui alive (have the engine call processEvents() while the script
     * is run in the foreground). */
251 252
    m_scriptEngine->setProcessEventsInterval(1 /*ms*/);

con's avatar
con committed
253 254 255
    m_stopped = false;
    m_stopOnNextLine = false;
    m_scriptEngine->abortEvaluation();
256

257
    notifyEngineSetupOk();
hjk's avatar
hjk committed
258
}
Friedemann Kleint's avatar
Friedemann Kleint committed
259

hjk's avatar
hjk committed
260 261
void ScriptEngine::setupInferior()
{
hjk's avatar
hjk committed
262
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
263
    m_scriptFileName = QFileInfo(startParameters().executable).absoluteFilePath();
hjk's avatar
hjk committed
264
    showMessage(_("SCRIPT FILE: ") + m_scriptFileName);
con's avatar
con committed
265
    QFile scriptFile(m_scriptFileName);
Friedemann Kleint's avatar
Friedemann Kleint committed
266
    if (!scriptFile.open(QIODevice::ReadOnly|QIODevice::Text)) {
267 268 269 270
        showMessageBox(QMessageBox::Critical, tr("Error:"),
            _("Cannot open script file %1:\n%2").
          arg(m_scriptFileName, scriptFile.errorString()));
        notifyInferiorSetupFailed();
hjk's avatar
hjk committed
271 272
        return;
    }
con's avatar
con committed
273 274 275 276
    QTextStream stream(&scriptFile);
    m_scriptContents = stream.readAll();
    scriptFile.close();
    attemptBreakpointSynchronization();
hjk's avatar
hjk committed
277
    notifyInferiorSetupOk();
con's avatar
con committed
278 279 280 281
}

void ScriptEngine::continueInferior()
{
hjk's avatar
hjk committed
282 283
    QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
    notifyInferiorRunRequested();
hjk's avatar
hjk committed
284
    SDEBUG("ScriptEngine::continueInferior()");
con's avatar
con committed
285 286 287 288
    m_stopped = false;
    m_stopOnNextLine = false;
}

Friedemann Kleint's avatar
Friedemann Kleint committed
289 290 291 292 293
static const char *qtExtensionsC[] = {
    "qt.core", "qt.gui", "qt.xml", "qt.svg", "qt.network",
    "qt.sql", "qt.opengl", "qt.webkit", "qt.xmlpatterns", "qt.uitools"
};

hjk's avatar
hjk committed
294
void ScriptEngine::importExtensions()
con's avatar
con committed
295
{
Friedemann Kleint's avatar
Friedemann Kleint committed
296 297 298 299 300 301
    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()))
hjk's avatar
hjk committed
302
        return; // true;
303 304
    QDir dir(QLatin1String("/home/apoenitz/dev/qtscriptgenerator"));
    if (!dir.cd(QLatin1String("plugins"))) {
305
        fprintf(stderr, "plugins folder does not exist -- did you build the bindings?\n");
hjk's avatar
hjk committed
306
        return; // false;
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    }
    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.",
330 331
                     qPrintable(failExtensions.join(QLatin1String(", "))),
                        qPrintable(dir.absolutePath()));
332 333
        }
    }
hjk's avatar
hjk committed
334
    return; // failExtensions.isEmpty();
Friedemann Kleint's avatar
Friedemann Kleint committed
335
}
336

hjk's avatar
hjk committed
337
void ScriptEngine::runEngine()
Friedemann Kleint's avatar
Friedemann Kleint committed
338
{
hjk's avatar
hjk committed
339 340
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << state());
    notifyEngineRunAndInferiorRunOk();
hjk's avatar
hjk committed
341 342
    showStatusMessage(tr("Running requested..."), 5000);
    showMessage(QLatin1String("Running: ") + m_scriptFileName, LogMisc);
Friedemann Kleint's avatar
Friedemann Kleint committed
343
    importExtensions();
344 345
    const QScriptValue result =
        m_scriptEngine->evaluate(m_scriptContents, m_scriptFileName);
346
    QString msg;
Friedemann Kleint's avatar
Friedemann Kleint committed
347
    if (m_scriptEngine->hasUncaughtException()) {
hjk's avatar
hjk committed
348
        msg = _("An exception occurred during execution at line: %1\n%2\n")
349 350 351 352
            .arg(m_scriptEngine->uncaughtExceptionLineNumber())
            .arg(m_scriptEngine->uncaughtException().toString());
        msg += m_scriptEngine->uncaughtExceptionBacktrace()
            .join(QString(QLatin1Char('\n')));
Friedemann Kleint's avatar
Friedemann Kleint committed
353
    } else {
hjk's avatar
hjk committed
354
        msg = _("Evaluation returns '%1'").arg(result.toString());
Friedemann Kleint's avatar
Friedemann Kleint committed
355
    }
356
    showMessage(msg, LogMisc);
hjk's avatar
hjk committed
357 358
    showMessage(_("This was the outermost function."));
    notifyInferiorExited();
con's avatar
con committed
359 360 361 362 363
}

void ScriptEngine::interruptInferior()
{
    m_stopOnNextLine = true;
364
    XSDEBUG("ScriptEngine::interruptInferior()");
con's avatar
con committed
365 366
}

hjk's avatar
hjk committed
367
void ScriptEngine::executeStep()
con's avatar
con committed
368
{
hjk's avatar
hjk committed
369 370
    QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
    notifyInferiorRunRequested();
371
    //SDEBUG("ScriptEngine::stepExec()");
con's avatar
con committed
372 373 374 375
    m_stopped = false;
    m_stopOnNextLine = true;
}

hjk's avatar
hjk committed
376
void ScriptEngine::executeStepI()
con's avatar
con committed
377
{
hjk's avatar
hjk committed
378 379
    QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
    notifyInferiorRunRequested();
380
    //SDEBUG("ScriptEngine::stepIExec()");
con's avatar
con committed
381 382 383 384
    m_stopped = false;
    m_stopOnNextLine = true;
}

hjk's avatar
hjk committed
385
void ScriptEngine::executeStepOut()
con's avatar
con committed
386
{
hjk's avatar
hjk committed
387 388
    QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
    notifyInferiorRunRequested();
389
    //SDEBUG("ScriptEngine::stepOutExec()");
con's avatar
con committed
390 391 392 393
    m_stopped = false;
    m_stopOnNextLine = true;
}

hjk's avatar
hjk committed
394
void ScriptEngine::executeNext()
con's avatar
con committed
395
{
hjk's avatar
hjk committed
396 397
    QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
    notifyInferiorRunRequested();
398
    //SDEBUG("ScriptEngine::nextExec()");
con's avatar
con committed
399 400 401 402
    m_stopped = false;
    m_stopOnNextLine = true;
}

hjk's avatar
hjk committed
403
void ScriptEngine::executeNextI()
con's avatar
con committed
404
{
hjk's avatar
hjk committed
405 406
    QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
    notifyInferiorRunRequested();
407
    //SDEBUG("ScriptEngine::nextIExec()");
con's avatar
con committed
408 409 410 411
    m_stopped = false;
    m_stopOnNextLine = true;
}

412
void ScriptEngine::executeRunToLine(const ContextData &data)
con's avatar
con committed
413
{
hjk's avatar
hjk committed
414 415
    QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
    notifyInferiorRunRequested();
416
    Q_UNUSED(data)
hjk's avatar
hjk committed
417
    SDEBUG("FIXME:  ScriptEngine::runToLineExec()");
con's avatar
con committed
418 419
}

hjk's avatar
hjk committed
420
void ScriptEngine::executeRunToFunction(const QString &functionName)
con's avatar
con committed
421
{
hjk's avatar
hjk committed
422 423
    QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
    notifyInferiorRunRequested();
424
    Q_UNUSED(functionName)
425
    XSDEBUG("FIXME:  ScriptEngine::runToFunctionExec()");
con's avatar
con committed
426 427
}

428
void ScriptEngine::executeJumpToLine(const ContextData &data)
con's avatar
con committed
429
{
hjk's avatar
hjk committed
430 431
    QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
    notifyInferiorRunRequested();
432
    Q_UNUSED(data)
433
    XSDEBUG("FIXME:  ScriptEngine::jumpToLineExec()");
con's avatar
con committed
434 435 436 437
}

void ScriptEngine::activateFrame(int index)
{
438
    Q_UNUSED(index)
con's avatar
con committed
439 440
}

hjk's avatar
hjk committed
441
void ScriptEngine::selectThread(ThreadId threadId)
con's avatar
con committed
442
{
hjk's avatar
hjk committed
443
    Q_UNUSED(threadId)
con's avatar
con committed
444 445
}

446
bool ScriptEngine::acceptsBreakpoint(BreakpointModelId id) const
447 448 449 450 451
{
    const QString fileName = breakHandler()->fileName(id);
    return fileName.endsWith(QLatin1String(".js"));
}

con's avatar
con committed
452 453
void ScriptEngine::attemptBreakpointSynchronization()
{
hjk's avatar
hjk committed
454 455
    QTC_ASSERT(false, /* FIXME */);
/*
456
    BreakHandler *handler = breakHandler();
con's avatar
con committed
457 458 459 460 461 462 463 464
    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()) {
465
            data->bpNumber = QByteArray::number(index + 1);
con's avatar
con committed
466 467
            updateNeeded = true;
        }
468 469
        if (!data->fileName.isEmpty() && data->markerFileName().isEmpty()) {
            data->setMarkerFileName(data->fileName);
470
            data->setMarkerLineNumber(data->lineNumber);
con's avatar
con committed
471 472 473 474 475
            updateNeeded = true;
        }
    }
    if (updateNeeded)
        handler->updateMarkers();
hjk's avatar
hjk committed
476
*/
con's avatar
con committed
477 478 479 480
}

void ScriptEngine::loadSymbols(const QString &moduleName)
{
481
    Q_UNUSED(moduleName)
con's avatar
con committed
482 483 484 485 486 487 488 489 490 491
}

void ScriptEngine::loadAllSymbols()
{
}

void ScriptEngine::reloadModules()
{
}

hjk's avatar
hjk committed
492
void ScriptEngine::requestModuleSymbols(const QString & /*moduleName*/)
493 494
{
}
con's avatar
con committed
495 496 497 498 499 500 501 502


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

Friedemann Kleint's avatar
Friedemann Kleint committed
503

con's avatar
con committed
504 505 506 507
static WatchData m_toolTip;
static QPoint m_toolTipPos;
static QHash<QString, WatchData> m_toolTipCache;

508
bool ScriptEngine::setToolTipExpression(const QPoint &mousePos,
509
    TextEditor::ITextEditor *editor, const DebuggerToolTipContext &ctx)
con's avatar
con committed
510
{
511 512
    Q_UNUSED(mousePos)
    Q_UNUSED(editor)
con's avatar
con committed
513

hjk's avatar
hjk committed
514
    if (state() != InferiorStopOk) {
hjk's avatar
hjk committed
515
        //SDEBUG("SUPPRESSING DEBUGGER TOOLTIP, INFERIOR NOT STOPPED");
516
        return false;
con's avatar
con committed
517
    }
518
    // Check mime type and get expression (borrowing some C++ - functions)
hjk's avatar
hjk committed
519
    const QString javaScriptMimeType =
Friedemann Kleint's avatar
Friedemann Kleint committed
520
        QLatin1String("application/javascript");
521
    if (!editor->document() || editor->document()->mimeType() != javaScriptMimeType)
522
        return false;
con's avatar
con committed
523

524 525
    int line;
    int column;
526
    QString exp = cppExpressionAt(editor, ctx.position, &line, &column);
con's avatar
con committed
527 528 529 530 531 532 533 534 535 536 537

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

    QToolTip::hideText();
538
    if (exp.isEmpty() || exp.startsWith(QLatin1Char('#')))  {
con's avatar
con committed
539
        QToolTip::hideText();
540
        return false;
con's avatar
con committed
541 542 543
    }

    if (!hasLetterOrNumber(exp)) {
544
        QToolTip::showText(m_toolTipPos, tr("'%1' contains no identifier.").arg(exp));
545
        return false;
con's avatar
con committed
546 547
    }

548
    if (exp.startsWith(QLatin1Char('"')) && exp.endsWith(QLatin1Char('"'))) {
549
        QToolTip::showText(m_toolTipPos, tr("String literal %1.").arg(exp));
550
        return false;
con's avatar
con committed
551 552
    }

553 554
    if (exp.startsWith(QLatin1String("++")) || exp.startsWith(QLatin1String("--")))
        exp.remove(0, 2);
con's avatar
con committed
555

556 557
    if (exp.endsWith(QLatin1String("++")) || exp.endsWith(QLatin1String("--")))
        exp.remove(0, 2);
con's avatar
con committed
558

559
    if (exp.startsWith(QLatin1Char('<')) || exp.startsWith(QLatin1Char('[')))
560
        return false;
con's avatar
con committed
561 562 563

    if (hasSideEffects(exp)) {
        QToolTip::showText(m_toolTipPos,
564
            tr("Cowardly refusing to evaluate expression '%1' "
565
               "with potential side effects.").arg(exp));
566
        return false;
con's avatar
con committed
567 568 569
    }

#if 0
hjk's avatar
hjk committed
570
    //if (m_manager->status() != InferiorStopOk)
con's avatar
con committed
571 572 573 574 575 576 577 578 579
    //    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
580
    return false;
con's avatar
con committed
581 582 583 584 585 586 587 588 589
}


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

590 591
void ScriptEngine::assignValueInDebugger(const Internal::WatchData *,
                                         const QString &expression, const QVariant &value)
con's avatar
con committed
592
{
593 594
    SDEBUG("ASSIGNING: " << (expression + QLatin1Char('=') + value.toString()));
    m_scriptEngine->evaluate(expression + QLatin1Char('=') + value.toString());
595
    updateLocals();
con's avatar
con committed
596 597
}

Friedemann Kleint's avatar
Friedemann Kleint committed
598 599
bool ScriptEngine::checkForBreakCondition(bool byFunction)
{
hjk's avatar
hjk committed
600 601 602 603 604
    // FIXME: Should that ever happen after setAgent(0) in shutdownInferior()?
    // In practice, it does, so chicken out.
    if (targetState() == DebuggerFinished)
        return false;

Friedemann Kleint's avatar
Friedemann Kleint committed
605 606
    const QScriptContext *context = m_scriptEngine->currentContext();
    const QScriptContextInfo info(context);
con's avatar
con committed
607

Friedemann Kleint's avatar
Friedemann Kleint committed
608 609 610
    // Update breakpoints
    const QString functionName = info.functionName();
    const QString fileName = info.fileName();
611 612
    const int lineNumber = byFunction
        ? info.functionStartLineNumber() : info.lineNumber();
hjk's avatar
hjk committed
613
    SDEBUG(Q_FUNC_INFO << byFunction << functionName << lineNumber << fileName);
con's avatar
con committed
614
    if (m_stopOnNextLine) {
Friedemann Kleint's avatar
Friedemann Kleint committed
615
        // Interrupt inferior
con's avatar
con committed
616 617
        m_stopOnNextLine = false;
    } else {
Friedemann Kleint's avatar
Friedemann Kleint committed
618 619
        if (byFunction && functionName.isEmpty())
            return false;
hjk's avatar
hjk committed
620
        BreakHandler *handler = breakHandler();
621
        BreakpointModelId id = byFunction ?
hjk's avatar
hjk committed
622 623
           handler->findBreakpointByFunction(functionName) :
           handler->findBreakpointByFileAndLine(fileName, lineNumber, false);
con's avatar
con committed
624

625
        // Skip disabled breakpoint.
hjk's avatar
hjk committed
626
        if (!handler->isEnabled(id))
627 628
            return false;

hjk's avatar
hjk committed
629
        BreakpointResponse br;
630
        // We just run into a breakpoint.
hjk's avatar
hjk committed
631
        //SDEBUG("RESOLVING BREAKPOINT AT " << fileName << lineNumber);
632 633 634
        br.lineNumber = lineNumber;
        br.fileName = fileName;
        br.functionName = functionName;
635
        handler->notifyBreakpointInsertOk(id);
hjk's avatar
hjk committed
636
        handler->setResponse(id, br);
con's avatar
con committed
637
    }
hjk's avatar
hjk committed
638
    notifyInferiorSpontaneousStop();
Friedemann Kleint's avatar
Friedemann Kleint committed
639 640
    SDEBUG("Stopped at " << lineNumber << fileName);
    showStatusMessage(tr("Stopped at %1:%2.").arg(fileName).arg(lineNumber), 5000);
641
    gotoLocation(Location(fileName, lineNumber));
642
    updateLocals();
Friedemann Kleint's avatar
Friedemann Kleint committed
643
    return true;
644
}
con's avatar
con committed
645

646 647 648
void ScriptEngine::updateLocals()
{
    QScriptContext *context = m_scriptEngine->currentContext();
hjk's avatar
hjk committed
649
    SDEBUG(Q_FUNC_INFO);
con's avatar
con committed
650 651 652

    //
    // Build stack
653
    //
con's avatar
con committed
654 655 656
    QList<StackFrame> stackFrames;
    int i = 0;
    for (QScriptContext *c = context; c; c = c->parentContext(), ++i) {
Friedemann Kleint's avatar
Friedemann Kleint committed
657
        const QScriptContextInfo info(c);
con's avatar
con committed
658 659 660 661 662 663 664 665
        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
666
            frame.function = QLatin1String("<global scope>");
con's avatar
con committed
667 668 669
        //frame.address = ...;
        stackFrames.append(frame);
    }
670
    stackHandler()->setFrames(stackFrames);
con's avatar
con committed
671 672

    //
673
    // Build locals, deactivate agent meanwhile.
con's avatar
con committed
674
    //
675 676
    m_scriptEngine->setAgent(0);

con's avatar
con committed
677
    WatchData data;
Arvid Ephraim Picciani's avatar
Arvid Ephraim Picciani committed
678 679
    data.id = m_watchIdCounter++;
    m_watchIdToScriptValue.insert(data.id, context->activationObject());
con's avatar
con committed
680
    data.iname = "local";
hjk's avatar
hjk committed
681
    data.name = _(data.iname);
Arvid Ephraim Picciani's avatar
Arvid Ephraim Picciani committed
682

hjk's avatar
hjk committed
683
    updateSubItem(data);
hjk's avatar
hjk committed
684
    // FIXME: Use an extra thread. This here is evil.
con's avatar
con committed
685
    m_stopped = true;
hjk's avatar
hjk committed
686
    showStatusMessage(tr("Stopped."), 5000);
con's avatar
con committed
687
    while (m_stopped) {
hjk's avatar
hjk committed
688
        //SDEBUG("LOOPING");
con's avatar
con committed
689 690
        QApplication::processEvents();
    }
691 692 693
    // Clear any exceptions occurred during locals evaluation.
    m_scriptEngine->clearExceptions();
    m_scriptEngine->setAgent(m_scriptAgent.data());
hjk's avatar
hjk committed
694
    notifyInferiorRunOk();
con's avatar
con committed
695 696
}

697
void ScriptEngine::updateWatchData(const WatchData &data, const WatchUpdateFlags &flags)
con's avatar
con committed
698
{
hjk's avatar
hjk committed
699
    Q_UNUSED(flags);
hjk's avatar
hjk committed
700
    updateSubItem(data);
701 702 703 704 705 706 707 708 709 710
}

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
711 712 713 714 715
}

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

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

    if (data.isChildrenNeeded()) {
Arvid Ephraim Picciani's avatar
Arvid Ephraim Picciani committed
778
        QScriptValueIterator it(ob);
con's avatar
con committed
779 780 781
        while (it.hasNext()) {
            it.next();
            WatchData data1;
782
            data1.iname = data.iname + '.' + it.name().toLatin1();
783
            data1.exp = it.name().toLatin1();
con's avatar
con committed
784
            data1.name = it.name();
Arvid Ephraim Picciani's avatar
Arvid Ephraim Picciani committed
785 786
            data.id = m_watchIdCounter++;
            m_watchIdToScriptValue.insert(data.id, it.value());
787
            if (watchHandler()->isExpandedIName(data1.iname)) {
con's avatar
con committed
788
                data1.setChildrenNeeded();
789
            } else {
con's avatar
con committed
790
                data1.setChildrenUnneeded();
791 792
            }
            children.push_back(data1);
con's avatar
con committed
793
        }
794
        data.setHasChildren(!children.isEmpty());
con's avatar
con committed
795 796 797
        data.setChildrenUnneeded();
    }

798
    if (data.isHasChildrenNeeded()) {
Arvid Ephraim Picciani's avatar
Arvid Ephraim Picciani committed
799
        QScriptValueIterator it(ob);
800
        data.setHasChildren(it.hasNext());
con's avatar
con committed
801 802
    }

803
    SDEBUG(msgDebugInsert(data, children));
hjk's avatar
hjk committed
804
    watchHandler()->insertIncompleteData(data);
805
    if (!children.isEmpty())
hjk's avatar
hjk committed
806
        watchHandler()->insertData(children);
con's avatar
con committed
807 808
}

809
DebuggerEngine *createScriptEngine(const DebuggerStartParameters &sp)
con's avatar
con committed
810
{
811
    return new ScriptEngine(sp);
con's avatar
con committed
812 813
}

814 815
} // namespace Internal
} // namespace Debugger