OSDN Git Service

749b18c67c687c7853781abbfa106aca95aa15fd
[qt-creator-jp/qt-creator-jp.git] / src / plugins / debugger / script / scriptengine.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at info@qt.nokia.com.
30 **
31 **************************************************************************/
32
33 #define QT_NO_CAST_FROM_ASCII
34
35 #include "scriptengine.h"
36
37 #include "debuggerstartparameters.h"
38 #include "breakhandler.h"
39 #include "debuggerconstants.h"
40 #include "debuggercore.h"
41 #include "debuggerdialogs.h"
42 #include "debuggerstringutils.h"
43 #include "moduleshandler.h"
44 #include "registerhandler.h"
45 #include "stackhandler.h"
46 #include "watchhandler.h"
47 #include "watchutils.h"
48 #include "debuggertooltipmanager.h"
49
50 #include <utils/qtcassert.h>
51
52 #include <texteditor/itexteditor.h>
53 #include <coreplugin/ifile.h>
54 #include <coreplugin/scriptmanager/scriptmanager.h>
55 #include <coreplugin/icore.h>
56
57 #include <QtCore/QDateTime>
58 #include <QtCore/QDebug>
59 #include <QtCore/QDir>
60 #include <QtCore/QFileInfo>
61 #include <QtCore/QTimer>
62
63 #include <QtGui/QApplication>
64 #include <QtGui/QMessageBox>
65 #include <QtGui/QToolTip>
66
67 #include <QtScript/QScriptContext>
68 #include <QtScript/QScriptClassPropertyIterator>
69 #include <QtScript/QScriptContextInfo>
70 #include <QtScript/QScriptEngine>
71 #include <QtScript/QScriptEngineAgent>
72 #include <QtScript/QScriptValue>
73 #include <QtScript/QScriptValueIterator>
74
75
76 namespace Debugger {
77 namespace Internal {
78
79 enum { debugScript = 0 };
80
81 #define SDEBUG(s) do { if (debugScript) qDebug() << s; } while (0)
82 #define XSDEBUG(s) qDebug() << s
83
84 ///////////////////////////////////////////////////////////////////////
85 //
86 // ScriptEngine
87 //
88 ///////////////////////////////////////////////////////////////////////
89
90 class ScriptAgent : public QScriptEngineAgent
91 {
92 public:
93     ScriptAgent(ScriptEngine *debugger, QScriptEngine *script);
94     ~ScriptAgent() {}
95
96     void contextPop();
97     void contextPush();
98     void exceptionCatch(qint64 scriptId, const QScriptValue &exception);
99     void exceptionThrow(qint64 scriptId, const QScriptValue & exception,
100         bool hasHandler);
101     void functionEntry(qint64 scriptId);
102     void functionExit(qint64 scriptId, const QScriptValue &returnValue);
103     void positionChange(qint64 scriptId, int lineNumber, int columnNumber);
104     void scriptLoad(qint64 id, const QString &program, const QString &fileName,
105         int baseLineNumber);
106     void scriptUnload(qint64 id);
107
108     void showMessage(const QString &msg);
109
110 private:
111     void maybeBreakNow(bool byFunction);
112
113     ScriptEngine *q;
114     int m_depth;
115     int m_contextDepth;
116 };
117
118 ScriptAgent::ScriptAgent(ScriptEngine *debugger, QScriptEngine *script)
119   : QScriptEngineAgent(script), q(debugger), m_depth(0), m_contextDepth(0)
120 {}
121
122 void ScriptAgent::showMessage(const QString &msg)
123 {
124     SDEBUG(msg);
125     q->showMessage(msg, LogMisc);
126 }
127
128 void ScriptAgent::contextPop()
129 {
130     //showMessage(_("ContextPop: %1").arg(m_contextDepth));
131     --m_contextDepth;
132 }
133
134 void ScriptAgent::contextPush()
135 {
136     ++m_contextDepth;
137     //showMessage(_("ContextPush: %1 ").arg(m_contextDepth));
138 }
139
140 void ScriptAgent::exceptionCatch(qint64 scriptId, const QScriptValue & exception)
141 {
142     Q_UNUSED(scriptId)
143     Q_UNUSED(exception)
144     showMessage(_("An exception was caught on %1: '%2'").
145                    arg(scriptId).arg(exception.toString()));
146 }
147
148 void ScriptAgent::exceptionThrow(qint64 scriptId, const QScriptValue &exception,
149     bool hasHandler)
150 {
151     Q_UNUSED(scriptId)
152     Q_UNUSED(exception)
153     Q_UNUSED(hasHandler)
154     showMessage(_("An exception occurred on %1: '%2'").
155                    arg(scriptId).arg(exception.toString()));
156 }
157
158 void ScriptAgent::functionEntry(qint64 scriptId)
159 {
160     Q_UNUSED(scriptId)
161     ++m_depth;
162     //showMessage(_("Function entry occurred on %1, depth: %2").arg(scriptId));
163     q->checkForBreakCondition(true);
164 }
165
166 void ScriptAgent::functionExit(qint64 scriptId, const QScriptValue &returnValue)
167 {
168     Q_UNUSED(scriptId)
169     Q_UNUSED(returnValue)
170     --m_depth;
171     //showMessage(_("Function exit occurred on %1: '%2'").
172     //              arg(scriptId).arg(returnValue.toString()));
173 }
174
175 void ScriptAgent::positionChange(qint64 scriptId, int lineNumber, int columnNumber)
176 {
177     Q_UNUSED(scriptId)
178     Q_UNUSED(lineNumber)
179     Q_UNUSED(columnNumber)
180     //showMessage(_("Position: %1").arg(lineNumber));
181     q->checkForBreakCondition(false);
182 }
183
184 void ScriptAgent::scriptLoad(qint64 scriptId, const QString &program,
185     const QString &fileName, int baseLineNumber)
186 {
187     Q_UNUSED(scriptId)
188     Q_UNUSED(program)
189     Q_UNUSED(fileName)
190     Q_UNUSED(baseLineNumber)
191     showMessage(_("Loaded: %1 id: %2").arg(fileName).arg(scriptId));
192 }
193
194 void ScriptAgent::scriptUnload(qint64 scriptId)
195 {
196     Q_UNUSED(scriptId)
197     showMessage(_("Unload script id %1 ").arg(scriptId));
198 }
199
200
201 ///////////////////////////////////////////////////////////////////////
202 //
203 // ScriptEngine
204 //
205 ///////////////////////////////////////////////////////////////////////
206
207 ScriptEngine::ScriptEngine(const DebuggerStartParameters &startParameters)
208     : DebuggerEngine(startParameters)
209 {
210     setObjectName(QLatin1String("ScriptEngine"));
211 }
212
213 ScriptEngine::~ScriptEngine()
214 {
215 }
216
217 void ScriptEngine::executeDebuggerCommand(const QString &command)
218 {
219     Q_UNUSED(command)
220     XSDEBUG("FIXME:  ScriptEngine::executeDebuggerCommand()");
221 }
222
223 void ScriptEngine::shutdownInferior()
224 {
225     QTC_ASSERT(state() == InferiorShutdownRequested, qDebug() << state());
226     SDEBUG("ScriptEngine::shutdownInferior()");
227     m_scriptEngine->setAgent(0);
228     //m_scriptAgent.reset(0);
229     m_stopped = false;
230     m_stopOnNextLine = false;
231     if (m_scriptEngine->isEvaluating())
232         m_scriptEngine->abortEvaluation();
233     notifyInferiorShutdownOk();
234 }
235
236 void ScriptEngine::shutdownEngine()
237 {
238     QTC_ASSERT(state() == EngineShutdownRequested, qDebug() << state());
239     m_scriptEngine->setAgent(0);
240     notifyEngineShutdownOk();
241 }
242
243 void ScriptEngine::setupEngine()
244 {
245     QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());
246     showMessage(_("STARTING SCRIPT DEBUGGER"), LogMisc);
247     if (m_scriptEngine.isNull())
248         m_scriptEngine = Core::ICore::instance()->scriptManager()->scriptEngine();
249     QTC_CHECK(!m_scriptAgent);
250     m_scriptAgent.reset(new ScriptAgent(this, m_scriptEngine.data()));
251     m_scriptEngine->setAgent(m_scriptAgent.data());
252     //m_scriptEngine->setAgent(new ScriptAgent(this, m_scriptEngine.data()));
253     /* Keep the gui alive (have the engine call processEvents() while the script
254      * is run in the foreground). */
255     m_scriptEngine->setProcessEventsInterval(1 /*ms*/);
256
257     m_stopped = false;
258     m_stopOnNextLine = false;
259     m_scriptEngine->abortEvaluation();
260
261     notifyEngineSetupOk();
262 }
263
264 void ScriptEngine::setupInferior()
265 {
266     QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
267     m_scriptFileName = QFileInfo(startParameters().executable).absoluteFilePath();
268     showMessage(_("SCRIPT FILE: ") + m_scriptFileName);
269     QFile scriptFile(m_scriptFileName);
270     if (!scriptFile.open(QIODevice::ReadOnly|QIODevice::Text)) {
271         showMessageBox(QMessageBox::Critical, tr("Error:"),
272             _("Cannot open script file %1:\n%2").
273           arg(m_scriptFileName, scriptFile.errorString()));
274         notifyInferiorSetupFailed();
275         return;
276     }
277     QTextStream stream(&scriptFile);
278     m_scriptContents = stream.readAll();
279     scriptFile.close();
280     attemptBreakpointSynchronization();
281     notifyInferiorSetupOk();
282 }
283
284 void ScriptEngine::continueInferior()
285 {
286     QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
287     notifyInferiorRunRequested();
288     SDEBUG("ScriptEngine::continueInferior()");
289     m_stopped = false;
290     m_stopOnNextLine = false;
291 }
292
293 static const char *qtExtensionsC[] = {
294     "qt.core", "qt.gui", "qt.xml", "qt.svg", "qt.network",
295     "qt.sql", "qt.opengl", "qt.webkit", "qt.xmlpatterns", "qt.uitools"
296 };
297
298 void ScriptEngine::importExtensions()
299 {
300     SDEBUG("ScriptEngine::importExtensions()");
301     QStringList extensions;
302     const int extCount = sizeof(qtExtensionsC)/sizeof(const char *);
303     for (int  e = 0; e < extCount; e++)
304         extensions.append(QLatin1String(qtExtensionsC[e]));
305     if (m_scriptEngine->importedExtensions().contains(extensions.front()))
306         return; // true;
307     QDir dir(QLatin1String("/home/apoenitz/dev/qtscriptgenerator"));
308     if (!dir.cd(QLatin1String("plugins"))) {
309         fprintf(stderr, "plugins folder does not exist -- did you build the bindings?\n");
310         return; // false;
311     }
312     QStringList paths = qApp->libraryPaths();
313     paths <<  dir.absolutePath();
314     qApp->setLibraryPaths(paths);
315     QStringList failExtensions;
316     foreach (const QString &ext, extensions) {
317         QScriptValue ret = m_scriptEngine->importExtension(ext);
318         if (ret.isError())
319             failExtensions.append(ext);
320     }
321     if (!failExtensions.isEmpty()) {
322         if (failExtensions.size() == extensions.size()) {
323             qWarning("Failed to import Qt bindings!\n"
324                      "Plugins directory searched: %s/script\n"
325                      "Make sure that the bindings have been built, "
326                      "and that this executable and the plugins are "
327                      "using compatible Qt libraries.", qPrintable(dir.absolutePath()));
328         } else {
329             qWarning("Failed to import some Qt bindings: %s\n"
330                      "Plugins directory searched: %s/script\n"
331                      "Make sure that the bindings have been built, "
332                      "and that this executable and the plugins are "
333                      "using compatible Qt libraries.",
334                      qPrintable(failExtensions.join(QLatin1String(", "))),
335                         qPrintable(dir.absolutePath()));
336         }
337     }
338     return; // failExtensions.isEmpty();
339 }
340
341 void ScriptEngine::runEngine()
342 {
343     QTC_ASSERT(state() == EngineRunRequested, qDebug() << state());
344     notifyEngineRunAndInferiorRunOk();
345     showStatusMessage(tr("Running requested..."), 5000);
346     showMessage(QLatin1String("Running: ") + m_scriptFileName, LogMisc);
347     importExtensions();
348     const QScriptValue result =
349         m_scriptEngine->evaluate(m_scriptContents, m_scriptFileName);
350     QString msg;
351     if (m_scriptEngine->hasUncaughtException()) {
352         msg = _("An exception occurred during execution at line: %1\n%2\n")
353             .arg(m_scriptEngine->uncaughtExceptionLineNumber())
354             .arg(m_scriptEngine->uncaughtException().toString());
355         msg += m_scriptEngine->uncaughtExceptionBacktrace()
356             .join(QString(QLatin1Char('\n')));
357     } else {
358         msg = _("Evaluation returns '%1'").arg(result.toString());
359     }
360     showMessage(msg, LogMisc);
361     showMessage(_("This was the outermost function."));
362     notifyInferiorExited();
363 }
364
365 void ScriptEngine::interruptInferior()
366 {
367     m_stopOnNextLine = true;
368     XSDEBUG("ScriptEngine::interruptInferior()");
369 }
370
371 void ScriptEngine::executeStep()
372 {
373     QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
374     notifyInferiorRunRequested();
375     //SDEBUG("ScriptEngine::stepExec()");
376     m_stopped = false;
377     m_stopOnNextLine = true;
378 }
379
380 void ScriptEngine::executeStepI()
381 {
382     QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
383     notifyInferiorRunRequested();
384     //SDEBUG("ScriptEngine::stepIExec()");
385     m_stopped = false;
386     m_stopOnNextLine = true;
387 }
388
389 void ScriptEngine::executeStepOut()
390 {
391     QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
392     notifyInferiorRunRequested();
393     //SDEBUG("ScriptEngine::stepOutExec()");
394     m_stopped = false;
395     m_stopOnNextLine = true;
396 }
397
398 void ScriptEngine::executeNext()
399 {
400     QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
401     notifyInferiorRunRequested();
402     //SDEBUG("ScriptEngine::nextExec()");
403     m_stopped = false;
404     m_stopOnNextLine = true;
405 }
406
407 void ScriptEngine::executeNextI()
408 {
409     QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
410     notifyInferiorRunRequested();
411     //SDEBUG("ScriptEngine::nextIExec()");
412     m_stopped = false;
413     m_stopOnNextLine = true;
414 }
415
416 void ScriptEngine::executeRunToLine(const ContextData &data)
417 {
418     QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
419     notifyInferiorRunRequested();
420     Q_UNUSED(data)
421     SDEBUG("FIXME:  ScriptEngine::runToLineExec()");
422 }
423
424 void ScriptEngine::executeRunToFunction(const QString &functionName)
425 {
426     QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
427     notifyInferiorRunRequested();
428     Q_UNUSED(functionName)
429     XSDEBUG("FIXME:  ScriptEngine::runToFunctionExec()");
430 }
431
432 void ScriptEngine::executeJumpToLine(const ContextData &data)
433 {
434     QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
435     notifyInferiorRunRequested();
436     Q_UNUSED(data)
437     XSDEBUG("FIXME:  ScriptEngine::jumpToLineExec()");
438 }
439
440 void ScriptEngine::activateFrame(int index)
441 {
442     Q_UNUSED(index)
443 }
444
445 void ScriptEngine::selectThread(int index)
446 {
447     Q_UNUSED(index)
448 }
449
450 bool ScriptEngine::acceptsBreakpoint(BreakpointModelId id) const
451 {
452     const QString fileName = breakHandler()->fileName(id);
453     return fileName.endsWith(QLatin1String(".js"));
454 }
455
456 void ScriptEngine::attemptBreakpointSynchronization()
457 {
458     QTC_ASSERT(false, /* FIXME */);
459 /*
460     BreakHandler *handler = breakHandler();
461     bool updateNeeded = false;
462     for (int index = 0; index != handler->size(); ++index) {
463         BreakpointData *data = handler->at(index);
464         if (data->pending) {
465             data->pending = false; // FIXME
466             updateNeeded = true;
467         }
468         if (data->bpNumber.isEmpty()) {
469             data->bpNumber = QByteArray::number(index + 1);
470             updateNeeded = true;
471         }
472         if (!data->fileName.isEmpty() && data->markerFileName().isEmpty()) {
473             data->setMarkerFileName(data->fileName);
474             data->setMarkerLineNumber(data->lineNumber);
475             updateNeeded = true;
476         }
477     }
478     if (updateNeeded)
479         handler->updateMarkers();
480 */
481 }
482
483 void ScriptEngine::loadSymbols(const QString &moduleName)
484 {
485     Q_UNUSED(moduleName)
486 }
487
488 void ScriptEngine::loadAllSymbols()
489 {
490 }
491
492 void ScriptEngine::reloadModules()
493 {
494 }
495
496 void ScriptEngine::requestModuleSymbols(const QString & /*moduleName*/)
497 {
498 }
499
500
501 //////////////////////////////////////////////////////////////////////
502 //
503 // Tooltip specific stuff
504 //
505 //////////////////////////////////////////////////////////////////////
506
507
508 static WatchData m_toolTip;
509 static QPoint m_toolTipPos;
510 static QHash<QString, WatchData> m_toolTipCache;
511
512 bool ScriptEngine::setToolTipExpression(const QPoint &mousePos,
513     TextEditor::ITextEditor *editor, const DebuggerToolTipContext &ctx)
514 {
515     Q_UNUSED(mousePos)
516     Q_UNUSED(editor)
517
518     if (state() != InferiorStopOk) {
519         //SDEBUG("SUPPRESSING DEBUGGER TOOLTIP, INFERIOR NOT STOPPED");
520         return false;
521     }
522     // Check mime type and get expression (borrowing some C++ - functions)
523     const QString javaScriptMimeType =
524         QLatin1String("application/javascript");
525     if (!editor->file() || editor->file()->mimeType() != javaScriptMimeType)
526         return false;
527
528     int line;
529     int column;
530     QString exp = cppExpressionAt(editor, ctx.position, &line, &column);
531
532 /*
533     if (m_toolTipCache.contains(exp)) {
534         const WatchData & data = m_toolTipCache[exp];
535         q->watchHandler()->removeChildren(data.iname);
536         insertData(data);
537         return;
538     }
539 */
540
541     QToolTip::hideText();
542     if (exp.isEmpty() || exp.startsWith(QLatin1Char('#')))  {
543         QToolTip::hideText();
544         return false;
545     }
546
547     if (!hasLetterOrNumber(exp)) {
548         QToolTip::showText(m_toolTipPos, tr("'%1' contains no identifier").arg(exp));
549         return false;
550     }
551
552     if (exp.startsWith(QLatin1Char('"')) && exp.endsWith(QLatin1Char('"'))) {
553         QToolTip::showText(m_toolTipPos, tr("String literal %1").arg(exp));
554         return false;
555     }
556
557     if (exp.startsWith(QLatin1String("++")) || exp.startsWith(QLatin1String("--")))
558         exp.remove(0, 2);
559
560     if (exp.endsWith(QLatin1String("++")) || exp.endsWith(QLatin1String("--")))
561         exp.remove(0, 2);
562
563     if (exp.startsWith(QLatin1Char('<')) || exp.startsWith(QLatin1Char('[')))
564         return false;
565
566     if (hasSideEffects(exp)) {
567         QToolTip::showText(m_toolTipPos,
568             tr("Cowardly refusing to evaluate expression '%1' "
569                "with potential side effects").arg(exp));
570         return false;
571     }
572
573 #if 0
574     //if (m_manager->status() != InferiorStopOk)
575     //    return;
576
577     // FIXME: 'exp' can contain illegal characters
578     m_toolTip = WatchData();
579     m_toolTip.exp = exp;
580     m_toolTip.name = exp;
581     m_toolTip.iname = tooltipIName;
582     insertData(m_toolTip);
583 #endif
584     return false;
585 }
586
587
588 //////////////////////////////////////////////////////////////////////
589 //
590 // Watch specific stuff
591 //
592 //////////////////////////////////////////////////////////////////////
593
594 void ScriptEngine::assignValueInDebugger(const Internal::WatchData *,
595                                          const QString &expression, const QVariant &value)
596 {
597     SDEBUG("ASSIGNING: " << (expression + QLatin1Char('=') + value.toString()));
598     m_scriptEngine->evaluate(expression + QLatin1Char('=') + value.toString());
599     updateLocals();
600 }
601
602 bool ScriptEngine::checkForBreakCondition(bool byFunction)
603 {
604     // FIXME: Should that ever happen after setAgent(0) in shutdownInferior()?
605     // In practice, it does, so chicken out.
606     if (targetState() == DebuggerFinished)
607         return false;
608
609     const QScriptContext *context = m_scriptEngine->currentContext();
610     const QScriptContextInfo info(context);
611
612     // Update breakpoints
613     const QString functionName = info.functionName();
614     const QString fileName = info.fileName();
615     const int lineNumber = byFunction
616         ? info.functionStartLineNumber() : info.lineNumber();
617     SDEBUG(Q_FUNC_INFO << byFunction << functionName << lineNumber << fileName);
618     if (m_stopOnNextLine) {
619         // Interrupt inferior
620         m_stopOnNextLine = false;
621     } else {
622         if (byFunction && functionName.isEmpty())
623             return false;
624         BreakHandler *handler = breakHandler();
625         BreakpointModelId id = byFunction ?
626            handler->findBreakpointByFunction(functionName) :
627            handler->findBreakpointByFileAndLine(fileName, lineNumber, false);
628
629         // Skip disabled breakpoint.
630         if (!handler->isEnabled(id))
631             return false;
632
633         BreakpointResponse br;
634         // We just run into a breakpoint.
635         //SDEBUG("RESOLVING BREAKPOINT AT " << fileName << lineNumber);
636         br.lineNumber = lineNumber;
637         br.fileName = fileName;
638         br.functionName = functionName;
639         handler->notifyBreakpointInsertOk(id);
640         handler->setResponse(id, br);
641     }
642     notifyInferiorSpontaneousStop();
643     SDEBUG("Stopped at " << lineNumber << fileName);
644     showStatusMessage(tr("Stopped at %1:%2.").arg(fileName).arg(lineNumber), 5000);
645     gotoLocation(Location(fileName, lineNumber));
646     updateLocals();
647     return true;
648 }
649
650 void ScriptEngine::updateLocals()
651 {
652     QScriptContext *context = m_scriptEngine->currentContext();
653     watchHandler()->beginCycle();
654     SDEBUG(Q_FUNC_INFO);
655
656     //
657     // Build stack
658     //
659     QList<StackFrame> stackFrames;
660     int i = 0;
661     for (QScriptContext *c = context; c; c = c->parentContext(), ++i) {
662         const QScriptContextInfo info(c);
663         StackFrame frame;
664         frame.level = i;
665         frame.file = info.fileName();
666         frame.function = info.functionName();
667         frame.from = QString::number(info.functionStartLineNumber());
668         frame.to = QString::number(info.functionEndLineNumber());
669         frame.line = info.lineNumber();
670         if (frame.function.isEmpty())
671             frame.function = QLatin1String("<global scope>");
672         //frame.address = ...;
673         stackFrames.append(frame);
674     }
675     stackHandler()->setFrames(stackFrames);
676
677     //
678     // Build locals, deactivate agent meanwhile.
679     //
680     m_scriptEngine->setAgent(0);
681
682     WatchData data;
683     data.id = m_watchIdCounter++;
684     m_watchIdToScriptValue.insert(data.id, context->activationObject());
685     data.iname = "local";
686     data.name = _(data.iname);
687
688     watchHandler()->beginCycle();
689     updateSubItem(data);
690     watchHandler()->endCycle();
691     // FIXME: Use an extra thread. This here is evil.
692     m_stopped = true;
693     showStatusMessage(tr("Stopped."), 5000);
694     while (m_stopped) {
695         //SDEBUG("LOOPING");
696         QApplication::processEvents();
697     }
698     // Clear any exceptions occurred during locals evaluation.
699     m_scriptEngine->clearExceptions();
700     m_scriptEngine->setAgent(m_scriptAgent.data());
701     notifyInferiorRunOk();
702 }
703
704 void ScriptEngine::updateWatchData(const WatchData &data, const WatchUpdateFlags &flags)
705 {
706     Q_UNUSED(flags);
707     updateSubItem(data);
708 }
709
710 static inline QString msgDebugInsert(const WatchData &d0, const QList<WatchData>& children)
711 {
712     QString rc;
713     QTextStream str(&rc);
714     str << "INSERTING " << d0.toString() << '\n';
715     foreach(const WatchData &c, children)
716         str << "          " << c.toString() << '\n';
717     return rc;
718 }
719
720 void ScriptEngine::updateSubItem(const WatchData &data0)
721 {
722     WatchData data = data0;
723     QList<WatchData> children;
724     //SDEBUG("\nUPDATE SUBITEM: " << data.toString() << data.scriptValue.toString());
725     QTC_ASSERT(data.isValid(), return);
726
727     const QScriptValue &ob = m_watchIdToScriptValue.value(data.id);
728     if (data.isTypeNeeded() || data.isValueNeeded()) {
729         if (ob.isArray()) {
730             data.setType("Array", false);
731             data.setValue(QString(QLatin1Char(' ')));
732         } else if (ob.isBool()) {
733             data.setType("Bool", false);
734             data.setValue(ob.toBool() ? QLatin1String("true") : QLatin1String("false"));
735             data.setHasChildren(false);
736         } else if (ob.isDate()) {
737             data.setType("Date", false);
738             data.setValue(ob.toDateTime().toString());
739             data.setHasChildren(false);
740         } else if (ob.isError()) {
741             data.setType("Error", false);
742             data.setValue(QString(QLatin1Char(' ')));
743         } else if (ob.isFunction()) {
744             data.setType("Function", false);
745             data.setValue(QString(QLatin1Char(' ')));
746         } else if (ob.isNull()) {
747             const QString nullValue = QLatin1String("<null>");
748             data.setType("<null>", false);
749             data.setValue(nullValue);
750         } else if (ob.isNumber()) {
751             data.setType("Number", false);
752             data.setValue(QString::number(ob.toNumber()));
753             data.setHasChildren(false);
754         } else if (ob.isObject()) {
755             data.setType("Object", false);
756             data.setValue(QString(QLatin1Char(' ')));
757         } else if (ob.isQMetaObject()) {
758             data.setType("QMetaObject", false);
759             data.setValue(QString(QLatin1Char(' ')));
760         } else if (ob.isQObject()) {
761             data.setType("QObject", false);
762             data.setValue(QString(QLatin1Char(' ')));
763         } else if (ob.isRegExp()) {
764             data.setType("RegExp", false);
765             data.setValue(ob.toRegExp().pattern());
766         } else if (ob.isString()) {
767             data.setType("String", false);
768             data.setValue(ob.toString());
769         } else if (ob.isVariant()) {
770             data.setType("Variant", false);
771             data.setValue(QString(QLatin1Char(' ')));
772         } else if (ob.isUndefined()) {
773             data.setType("<undefined>", false);
774             data.setValue(QLatin1String("<unknown>"));
775             data.setHasChildren(false);
776         } else {
777             const QString unknown = QLatin1String("<unknown>");
778             data.setType("<unknown>", false);
779             data.setValue(unknown);
780             data.setHasChildren(false);
781         }
782     }
783
784     if (data.isChildrenNeeded()) {
785         QScriptValueIterator it(ob);
786         while (it.hasNext()) {
787             it.next();
788             WatchData data1;
789             data1.iname = data.iname + '.' + it.name().toLatin1();
790             data1.exp = it.name().toLatin1();
791             data1.name = it.name();
792             data.id = m_watchIdCounter++;
793             m_watchIdToScriptValue.insert(data.id, it.value());
794             if (watchHandler()->isExpandedIName(data1.iname)) {
795                 data1.setChildrenNeeded();
796             } else {
797                 data1.setChildrenUnneeded();
798             }
799             children.push_back(data1);
800         }
801         data.setHasChildren(!children.isEmpty());
802         data.setChildrenUnneeded();
803     }
804
805     if (data.isHasChildrenNeeded()) {
806         QScriptValueIterator it(ob);
807         data.setHasChildren(it.hasNext());
808     }
809
810     SDEBUG(msgDebugInsert(data, children));
811     watchHandler()->insertData(data);
812     if (!children.isEmpty())
813         watchHandler()->insertBulkData(children);
814 }
815
816 DebuggerEngine *createScriptEngine(const DebuggerStartParameters &sp)
817 {
818     return new ScriptEngine(sp);
819 }
820
821 } // namespace Internal
822 } // namespace Debugger