| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the QtSCriptTools module of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at qt-info@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qscriptdebuggerconsole_p.h"
|
|---|
| 43 | #include "qscriptdebuggerconsolecommandjob_p.h"
|
|---|
| 44 | #include "qscriptdebuggerconsolecommandmanager_p.h"
|
|---|
| 45 | #include "qscriptdebuggerscriptedconsolecommand_p.h"
|
|---|
| 46 | #include "qscriptmessagehandlerinterface_p.h"
|
|---|
| 47 | #include "qscriptbreakpointdata_p.h"
|
|---|
| 48 | #include "qscriptdebuggerresponse_p.h"
|
|---|
| 49 | #include "qscriptdebuggervalueproperty_p.h"
|
|---|
| 50 | #include "qscriptscriptdata_p.h"
|
|---|
| 51 |
|
|---|
| 52 | #include <QtCore/qdir.h>
|
|---|
| 53 | #include <QtCore/qfileinfo.h>
|
|---|
| 54 | #include <QtCore/qstring.h>
|
|---|
| 55 | #include <QtCore/qstringlist.h>
|
|---|
| 56 | #include <QtCore/qdebug.h>
|
|---|
| 57 | #include <QtScript/qscriptcontextinfo.h>
|
|---|
| 58 | #include <QtScript/qscriptengine.h>
|
|---|
| 59 |
|
|---|
| 60 | Q_DECLARE_METATYPE(QScriptDebuggerResponse)
|
|---|
| 61 | Q_DECLARE_METATYPE(QScriptBreakpointData)
|
|---|
| 62 | Q_DECLARE_METATYPE(QScriptBreakpointMap)
|
|---|
| 63 | Q_DECLARE_METATYPE(QScriptScriptData)
|
|---|
| 64 | Q_DECLARE_METATYPE(QScriptScriptMap)
|
|---|
| 65 | Q_DECLARE_METATYPE(QScriptContextInfo)
|
|---|
| 66 | Q_DECLARE_METATYPE(QScriptDebuggerValue)
|
|---|
| 67 | Q_DECLARE_METATYPE(QScriptDebuggerValueProperty)
|
|---|
| 68 | Q_DECLARE_METATYPE(QScriptDebuggerValuePropertyList)
|
|---|
| 69 | Q_DECLARE_METATYPE(QScriptDebuggerConsoleCommand*)
|
|---|
| 70 | Q_DECLARE_METATYPE(QScriptDebuggerConsoleCommandList)
|
|---|
| 71 | Q_DECLARE_METATYPE(QScriptDebuggerConsoleCommandGroupData)
|
|---|
| 72 | Q_DECLARE_METATYPE(QScriptDebuggerConsoleCommandGroupMap)
|
|---|
| 73 |
|
|---|
| 74 | QT_BEGIN_NAMESPACE
|
|---|
| 75 |
|
|---|
| 76 | static QScriptValue debuggerResponseToScriptValue(QScriptEngine *eng, const QScriptDebuggerResponse &in)
|
|---|
| 77 | {
|
|---|
| 78 | QScriptValue out = eng->newObject();
|
|---|
| 79 | out.setProperty(QString::fromLatin1("result"), qScriptValueFromValue(eng, in.result()));
|
|---|
| 80 | out.setProperty(QString::fromLatin1("error"), QScriptValue(eng, in.error()));
|
|---|
| 81 | out.setProperty(QString::fromLatin1("async"), QScriptValue(eng, in.async()));
|
|---|
| 82 | return out;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | static void debuggerResponseFromScriptValue(const QScriptValue &, QScriptDebuggerResponse &)
|
|---|
| 86 | {
|
|---|
| 87 | Q_ASSERT(0);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | static QScriptValue breakpointDataToScriptValue(QScriptEngine *eng, const QScriptBreakpointData &in)
|
|---|
| 91 | {
|
|---|
| 92 | QScriptValue out = eng->newObject();
|
|---|
| 93 | out.setProperty(QString::fromLatin1("scriptId"), QScriptValue(eng, qsreal(in.scriptId())));
|
|---|
| 94 | out.setProperty(QString::fromLatin1("fileName"), QScriptValue(eng, in.fileName()));
|
|---|
| 95 | out.setProperty(QString::fromLatin1("lineNumber"), QScriptValue(eng, in.lineNumber()));
|
|---|
| 96 | out.setProperty(QString::fromLatin1("enabled"), QScriptValue(eng, in.isEnabled()));
|
|---|
| 97 | out.setProperty(QString::fromLatin1("singleShot"), QScriptValue(eng, in.isSingleShot()));
|
|---|
| 98 | out.setProperty(QString::fromLatin1("ignoreCount"), QScriptValue(eng, in.ignoreCount()));
|
|---|
| 99 | out.setProperty(QString::fromLatin1("condition"), QScriptValue(eng, in.condition()));
|
|---|
| 100 | return out;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | static void breakpointDataFromScriptValue(const QScriptValue &in, QScriptBreakpointData &out)
|
|---|
| 104 | {
|
|---|
| 105 | QScriptValue scriptId = in.property(QString::fromLatin1("scriptId"));
|
|---|
| 106 | if (scriptId.isValid())
|
|---|
| 107 | out.setScriptId((qint64)scriptId.toNumber());
|
|---|
| 108 | out.setFileName(in.property(QString::fromLatin1("fileName")).toString());
|
|---|
| 109 | out.setLineNumber(in.property(QString::fromLatin1("lineNumber")).toInt32());
|
|---|
| 110 | QScriptValue enabled = in.property(QString::fromLatin1("enabled"));
|
|---|
| 111 | if (enabled.isValid())
|
|---|
| 112 | out.setEnabled(enabled.toBoolean());
|
|---|
| 113 | QScriptValue singleShot = in.property(QString::fromLatin1("singleShot"));
|
|---|
| 114 | if (singleShot.isValid())
|
|---|
| 115 | out.setSingleShot(singleShot.toBoolean());
|
|---|
| 116 | out.setIgnoreCount(in.property(QString::fromLatin1("ignoreCount")).toInt32());
|
|---|
| 117 | out.setCondition(in.property(QString::fromLatin1("condition")).toString());
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | static QScriptValue breakpointMapToScriptValue(QScriptEngine *eng, const QScriptBreakpointMap &in)
|
|---|
| 121 | {
|
|---|
| 122 | QScriptValue out = eng->newObject();
|
|---|
| 123 | QScriptBreakpointMap::const_iterator it;
|
|---|
| 124 | for (it = in.constBegin(); it != in.constEnd(); ++it) {
|
|---|
| 125 | out.setProperty(QString::number(it.key()), qScriptValueFromValue(eng, it.value()));
|
|---|
| 126 | }
|
|---|
| 127 | return out;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | static void breakpointMapFromScriptValue(const QScriptValue &, QScriptBreakpointMap &)
|
|---|
| 131 | {
|
|---|
| 132 | Q_ASSERT(0);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | static QScriptValue scriptDataToScriptValue(QScriptEngine *eng, const QScriptScriptData &in)
|
|---|
| 136 | {
|
|---|
| 137 | QScriptValue out = eng->newObject();
|
|---|
| 138 | out.setProperty(QString::fromLatin1("contents"), QScriptValue(eng, in.contents()));
|
|---|
| 139 | out.setProperty(QString::fromLatin1("fileName"), QScriptValue(eng, in.fileName()));
|
|---|
| 140 | out.setProperty(QString::fromLatin1("baseLineNumber"), QScriptValue(eng, in.baseLineNumber()));
|
|---|
| 141 | return out;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | static void scriptDataFromScriptValue(const QScriptValue &in, QScriptScriptData &out)
|
|---|
| 145 | {
|
|---|
| 146 | QString contents = in.property(QString::fromLatin1("contents")).toString();
|
|---|
| 147 | QString fileName = in.property(QString::fromLatin1("fileName")).toString();
|
|---|
| 148 | int baseLineNumber = in.property(QString::fromLatin1("baseLineNumber")).toInt32();
|
|---|
| 149 | QScriptScriptData tmp(contents, fileName, baseLineNumber);
|
|---|
| 150 | out = tmp;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | static QScriptValue scriptMapToScriptValue(QScriptEngine *eng, const QScriptScriptMap &in)
|
|---|
| 154 | {
|
|---|
| 155 | QScriptValue out = eng->newObject();
|
|---|
| 156 | QScriptScriptMap::const_iterator it;
|
|---|
| 157 | for (it = in.constBegin(); it != in.constEnd(); ++it) {
|
|---|
| 158 | out.setProperty(QString::number(it.key()), qScriptValueFromValue(eng, it.value()));
|
|---|
| 159 | }
|
|---|
| 160 | return out;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | static void scriptMapFromScriptValue(const QScriptValue &, QScriptScriptMap &)
|
|---|
| 164 | {
|
|---|
| 165 | Q_ASSERT(0);
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | static QScriptValue consoleCommandToScriptValue(
|
|---|
| 169 | QScriptEngine *eng, QScriptDebuggerConsoleCommand* const &in)
|
|---|
| 170 | {
|
|---|
| 171 | if (!in)
|
|---|
| 172 | return eng->undefinedValue();
|
|---|
| 173 | QScriptValue out = eng->newObject();
|
|---|
| 174 | out.setProperty(QString::fromLatin1("name"), QScriptValue(eng, in->name()));
|
|---|
| 175 | out.setProperty(QString::fromLatin1("group"), QScriptValue(eng, in->group()));
|
|---|
| 176 | out.setProperty(QString::fromLatin1("shortDescription"), QScriptValue(eng, in->shortDescription()));
|
|---|
| 177 | out.setProperty(QString::fromLatin1("longDescription"), QScriptValue(eng, in->longDescription()));
|
|---|
| 178 | out.setProperty(QString::fromLatin1("aliases"), qScriptValueFromValue(eng, in->aliases()));
|
|---|
| 179 | out.setProperty(QString::fromLatin1("seeAlso"), qScriptValueFromValue(eng, in->seeAlso()));
|
|---|
| 180 | return out;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | static void consoleCommandFromScriptValue(
|
|---|
| 184 | const QScriptValue &, QScriptDebuggerConsoleCommand* &)
|
|---|
| 185 | {
|
|---|
| 186 | Q_ASSERT(0);
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | static QScriptValue consoleCommandGroupDataToScriptValue(
|
|---|
| 190 | QScriptEngine *eng, const QScriptDebuggerConsoleCommandGroupData &in)
|
|---|
| 191 | {
|
|---|
| 192 | QScriptValue out = eng->newObject();
|
|---|
| 193 | out.setProperty(QString::fromLatin1("longDescription"), QScriptValue(eng, in.longDescription()));
|
|---|
| 194 | out.setProperty(QString::fromLatin1("shortDescription"), QScriptValue(eng, in.shortDescription()));
|
|---|
| 195 | return out;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | static void consoleCommandGroupDataFromScriptValue(
|
|---|
| 199 | const QScriptValue &, QScriptDebuggerConsoleCommandGroupData &)
|
|---|
| 200 | {
|
|---|
| 201 | Q_ASSERT(0);
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | static QScriptValue consoleCommandGroupMapToScriptValue(
|
|---|
| 205 | QScriptEngine *eng, const QScriptDebuggerConsoleCommandGroupMap &in)
|
|---|
| 206 | {
|
|---|
| 207 | QScriptValue out = eng->newObject();
|
|---|
| 208 | QScriptDebuggerConsoleCommandGroupMap::const_iterator it;
|
|---|
| 209 | for (it = in.constBegin(); it != in.constEnd(); ++it) {
|
|---|
| 210 | out.setProperty(it.key(), qScriptValueFromValue(eng, it.value()));
|
|---|
| 211 | }
|
|---|
| 212 | return out;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | static void consoleCommandGroupMapFromScriptValue(
|
|---|
| 216 | const QScriptValue &, QScriptDebuggerConsoleCommandGroupMap &)
|
|---|
| 217 | {
|
|---|
| 218 | Q_ASSERT(0);
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | static QScriptValue contextInfoToScriptValue(QScriptEngine *eng, const QScriptContextInfo &in)
|
|---|
| 222 | {
|
|---|
| 223 | QScriptValue out = eng->newObject();
|
|---|
| 224 | out.setProperty(QString::fromLatin1("scriptId"), QScriptValue(eng, qsreal(in.scriptId())));
|
|---|
| 225 | out.setProperty(QString::fromLatin1("fileName"), QScriptValue(eng, in.fileName()));
|
|---|
| 226 | out.setProperty(QString::fromLatin1("lineNumber"), QScriptValue(eng, in.lineNumber()));
|
|---|
| 227 | out.setProperty(QString::fromLatin1("columnNumber"), QScriptValue(eng, in.columnNumber()));
|
|---|
| 228 | out.setProperty(QString::fromLatin1("functionName"), QScriptValue(eng, in.functionName()));
|
|---|
| 229 | return out;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | static void contextInfoFromScriptValue(const QScriptValue &, QScriptContextInfo &)
|
|---|
| 233 | {
|
|---|
| 234 | Q_ASSERT(0);
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | static QScriptValue debuggerScriptValuePropertyToScriptValue(QScriptEngine *eng, const QScriptDebuggerValueProperty &in)
|
|---|
| 238 | {
|
|---|
| 239 | QScriptValue out = eng->newObject();
|
|---|
| 240 | out.setProperty(QString::fromLatin1("name"), QScriptValue(eng, in.name()));
|
|---|
| 241 | out.setProperty(QString::fromLatin1("value"), qScriptValueFromValue(eng, in.value()));
|
|---|
| 242 | out.setProperty(QString::fromLatin1("valueAsString"), QScriptValue(eng, in.valueAsString()));
|
|---|
| 243 | out.setProperty(QString::fromLatin1("flags"), QScriptValue(eng, static_cast<int>(in.flags())));
|
|---|
| 244 | return out;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | static void debuggerScriptValuePropertyFromScriptValue(const QScriptValue &in, QScriptDebuggerValueProperty &out)
|
|---|
| 248 | {
|
|---|
| 249 | QString name = in.property(QString::fromLatin1("name")).toString();
|
|---|
| 250 | QScriptDebuggerValue value = qscriptvalue_cast<QScriptDebuggerValue>(in.property(QString::fromLatin1("value")));
|
|---|
| 251 | QString valueAsString = in.property(QString::fromLatin1("valueAsString")).toString();
|
|---|
| 252 | int flags = in.property(QString::fromLatin1("flags")).toInt32();
|
|---|
| 253 | QScriptDebuggerValueProperty tmp(name, value, valueAsString, QScriptValue::PropertyFlags(flags));
|
|---|
| 254 | out = tmp;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | /*!
|
|---|
| 258 | \since 4.5
|
|---|
| 259 | \class QScriptDebuggerConsole
|
|---|
| 260 | \internal
|
|---|
| 261 |
|
|---|
| 262 | \brief The QScriptDebuggerConsole class provides the core functionality of a debugger console.
|
|---|
| 263 | */
|
|---|
| 264 |
|
|---|
| 265 | class QScriptDebuggerConsolePrivate
|
|---|
| 266 | {
|
|---|
| 267 | Q_DECLARE_PUBLIC(QScriptDebuggerConsole)
|
|---|
| 268 | public:
|
|---|
| 269 | QScriptDebuggerConsolePrivate(QScriptDebuggerConsole*);
|
|---|
| 270 | ~QScriptDebuggerConsolePrivate();
|
|---|
| 271 |
|
|---|
| 272 | void loadScriptedCommands(const QString &scriptsPath,
|
|---|
| 273 | QScriptMessageHandlerInterface *messageHandler);
|
|---|
| 274 | QScriptDebuggerConsoleCommandJob *createJob(
|
|---|
| 275 | const QString &command,
|
|---|
| 276 | QScriptMessageHandlerInterface *messageHandler,
|
|---|
| 277 | QScriptDebuggerCommandSchedulerInterface *commandScheduler);
|
|---|
| 278 |
|
|---|
| 279 | QScriptEngine *commandEngine;
|
|---|
| 280 | QScriptDebuggerConsoleCommandManager *commandManager;
|
|---|
| 281 | QString commandPrefix;
|
|---|
| 282 | QString input;
|
|---|
| 283 | QStringList commandHistory;
|
|---|
| 284 | int currentFrameIndex;
|
|---|
| 285 | qint64 currentScriptId;
|
|---|
| 286 | int currentLineNumber;
|
|---|
| 287 | int evaluateAction;
|
|---|
| 288 | qint64 sessionId;
|
|---|
| 289 |
|
|---|
| 290 | QScriptDebuggerConsole *q_ptr;
|
|---|
| 291 | };
|
|---|
| 292 |
|
|---|
| 293 | QScriptDebuggerConsolePrivate::QScriptDebuggerConsolePrivate(QScriptDebuggerConsole* parent)
|
|---|
| 294 | : q_ptr(parent)
|
|---|
| 295 | {
|
|---|
| 296 | sessionId = 0;
|
|---|
| 297 | currentFrameIndex = 0;
|
|---|
| 298 | currentScriptId = -1;
|
|---|
| 299 | currentLineNumber = -1;
|
|---|
| 300 | evaluateAction = 0;
|
|---|
| 301 | commandPrefix = QLatin1String(".");
|
|---|
| 302 | commandManager = new QScriptDebuggerConsoleCommandManager();
|
|---|
| 303 |
|
|---|
| 304 | commandEngine = new QScriptEngine;
|
|---|
| 305 | qScriptRegisterMetaType<QScriptBreakpointData>(commandEngine, breakpointDataToScriptValue, breakpointDataFromScriptValue);
|
|---|
| 306 | qScriptRegisterMetaType<QScriptBreakpointMap>(commandEngine, breakpointMapToScriptValue, breakpointMapFromScriptValue);
|
|---|
| 307 | qScriptRegisterMetaType<QScriptScriptData>(commandEngine, scriptDataToScriptValue, scriptDataFromScriptValue);
|
|---|
| 308 | qScriptRegisterMetaType<QScriptScriptMap>(commandEngine, scriptMapToScriptValue, scriptMapFromScriptValue);
|
|---|
| 309 | qScriptRegisterMetaType<QScriptContextInfo>(commandEngine, contextInfoToScriptValue, contextInfoFromScriptValue);
|
|---|
| 310 | qScriptRegisterMetaType<QScriptDebuggerValueProperty>(commandEngine, debuggerScriptValuePropertyToScriptValue, debuggerScriptValuePropertyFromScriptValue);
|
|---|
| 311 | qScriptRegisterSequenceMetaType<QScriptDebuggerValuePropertyList>(commandEngine);
|
|---|
| 312 | qScriptRegisterMetaType<QScriptDebuggerResponse>(commandEngine, debuggerResponseToScriptValue, debuggerResponseFromScriptValue);
|
|---|
| 313 | qScriptRegisterMetaType<QScriptDebuggerConsoleCommand*>(commandEngine, consoleCommandToScriptValue, consoleCommandFromScriptValue);
|
|---|
| 314 | qScriptRegisterSequenceMetaType<QScriptDebuggerConsoleCommandList>(commandEngine);
|
|---|
| 315 | qScriptRegisterMetaType<QScriptDebuggerConsoleCommandGroupData>(commandEngine, consoleCommandGroupDataToScriptValue, consoleCommandGroupDataFromScriptValue);
|
|---|
| 316 | qScriptRegisterMetaType<QScriptDebuggerConsoleCommandGroupMap>(commandEngine, consoleCommandGroupMapToScriptValue, consoleCommandGroupMapFromScriptValue);
|
|---|
| 317 | // ### can't do this, if it's an object ID the conversion will be incorrect since
|
|---|
| 318 | // ### the object ID refers to an object in a different engine!
|
|---|
| 319 | // qScriptRegisterMetaType(commandEngine, debuggerScriptValueToScriptValue, debuggerScriptValueFromScriptValue);
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | QScriptDebuggerConsolePrivate::~QScriptDebuggerConsolePrivate()
|
|---|
| 323 | {
|
|---|
| 324 | delete commandManager;
|
|---|
| 325 | delete commandEngine;
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | /*!
|
|---|
| 329 | Loads command definitions from scripts located in the given \a scriptsPath.
|
|---|
| 330 | */
|
|---|
| 331 | void QScriptDebuggerConsolePrivate::loadScriptedCommands(
|
|---|
| 332 | const QString &scriptsPath,
|
|---|
| 333 | QScriptMessageHandlerInterface *messageHandler)
|
|---|
| 334 | {
|
|---|
| 335 | QDir dir(scriptsPath);
|
|---|
| 336 | QFileInfoList entries = dir.entryInfoList(QStringList()
|
|---|
| 337 | << QLatin1String("*.qs"));
|
|---|
| 338 | for (int i = 0; i < entries.size(); ++i) {
|
|---|
| 339 | const QFileInfo &fi = entries.at(i);
|
|---|
| 340 | QString fileName = fi.fileName();
|
|---|
| 341 | QFile file(scriptsPath + QLatin1Char('/') + fileName);
|
|---|
| 342 | if (!file.open(QIODevice::ReadOnly))
|
|---|
| 343 | continue;
|
|---|
| 344 | QTextStream stream(&file);
|
|---|
| 345 | QString program = stream.readAll();
|
|---|
| 346 | QScriptDebuggerScriptedConsoleCommand *command;
|
|---|
| 347 | command = QScriptDebuggerScriptedConsoleCommand::parse(
|
|---|
| 348 | program, fileName, commandEngine, messageHandler);
|
|---|
| 349 | if (!command)
|
|---|
| 350 | continue;
|
|---|
| 351 | commandManager->addCommand(command);
|
|---|
| 352 | }
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 | /*!
|
|---|
| 357 | Creates a job that will execute the given debugger \a command.
|
|---|
| 358 | Returns the new job, or 0 if the command is undefined.
|
|---|
| 359 | */
|
|---|
| 360 | QScriptDebuggerConsoleCommandJob *QScriptDebuggerConsolePrivate::createJob(
|
|---|
| 361 | const QString &command, QScriptMessageHandlerInterface *messageHandler,
|
|---|
| 362 | QScriptDebuggerCommandSchedulerInterface *commandScheduler)
|
|---|
| 363 | {
|
|---|
| 364 | QString name;
|
|---|
| 365 | int i = command.indexOf(QLatin1Char(' '));
|
|---|
| 366 | if (i == -1) {
|
|---|
| 367 | name = command;
|
|---|
| 368 | i = name.size();
|
|---|
| 369 | } else {
|
|---|
| 370 | name = command.left(i);
|
|---|
| 371 | }
|
|---|
| 372 | if (name.isEmpty())
|
|---|
| 373 | return 0;
|
|---|
| 374 | QScriptDebuggerConsoleCommand *cmd = commandManager->findCommand(name);
|
|---|
| 375 | if (!cmd) {
|
|---|
| 376 | // try to auto-complete
|
|---|
| 377 | QStringList completions = commandManager->completions(name);
|
|---|
| 378 | if (!completions.isEmpty()) {
|
|---|
| 379 | if (completions.size() > 1) {
|
|---|
| 380 | QString msg;
|
|---|
| 381 | msg.append(QString::fromLatin1("Ambiguous command \"%0\": ")
|
|---|
| 382 | .arg(name));
|
|---|
| 383 | for (int j = 0; j < completions.size(); ++j) {
|
|---|
| 384 | if (j > 0)
|
|---|
| 385 | msg.append(QLatin1String(", "));
|
|---|
| 386 | msg.append(completions.at(j));
|
|---|
| 387 | }
|
|---|
| 388 | msg.append(QLatin1Char('.'));
|
|---|
| 389 | messageHandler->message(QtWarningMsg, msg);
|
|---|
| 390 | return 0;
|
|---|
| 391 | }
|
|---|
| 392 | cmd = commandManager->findCommand(completions.at(0));
|
|---|
| 393 | Q_ASSERT(cmd != 0);
|
|---|
| 394 | }
|
|---|
| 395 | if (!cmd) {
|
|---|
| 396 | messageHandler->message(
|
|---|
| 397 | QtWarningMsg,
|
|---|
| 398 | QString::fromLatin1("Undefined command \"%0\". Try \"help\".")
|
|---|
| 399 | .arg(name));
|
|---|
| 400 | return 0;
|
|---|
| 401 | }
|
|---|
| 402 | }
|
|---|
| 403 | QStringList args;
|
|---|
| 404 | QString tmp = command.mid(i+1);
|
|---|
| 405 | if (cmd->argumentTypes().contains(QString::fromLatin1("script"))) {
|
|---|
| 406 | if (!tmp.isEmpty())
|
|---|
| 407 | args.append(tmp);
|
|---|
| 408 | } else {
|
|---|
| 409 | args = tmp.split(QLatin1Char(' '), QString::SkipEmptyParts);
|
|---|
| 410 | }
|
|---|
| 411 | return cmd->createJob(args, q_func(), messageHandler, commandScheduler);
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | QScriptDebuggerConsole::QScriptDebuggerConsole()
|
|---|
| 415 | : d_ptr(new QScriptDebuggerConsolePrivate(this))
|
|---|
| 416 | {
|
|---|
| 417 | }
|
|---|
| 418 |
|
|---|
| 419 | QScriptDebuggerConsole::~QScriptDebuggerConsole()
|
|---|
| 420 | {
|
|---|
| 421 | }
|
|---|
| 422 |
|
|---|
| 423 | void QScriptDebuggerConsole::loadScriptedCommands(const QString &scriptsPath,
|
|---|
| 424 | QScriptMessageHandlerInterface *messageHandler)
|
|---|
| 425 | {
|
|---|
| 426 | Q_D(QScriptDebuggerConsole);
|
|---|
| 427 | d->loadScriptedCommands(scriptsPath, messageHandler);
|
|---|
| 428 | }
|
|---|
| 429 |
|
|---|
| 430 | QScriptDebuggerConsoleCommandManager *QScriptDebuggerConsole::commandManager() const
|
|---|
| 431 | {
|
|---|
| 432 | Q_D(const QScriptDebuggerConsole);
|
|---|
| 433 | return d->commandManager;
|
|---|
| 434 | }
|
|---|
| 435 |
|
|---|
| 436 | bool QScriptDebuggerConsole::hasIncompleteInput() const
|
|---|
| 437 | {
|
|---|
| 438 | Q_D(const QScriptDebuggerConsole);
|
|---|
| 439 | return !d->input.isEmpty();
|
|---|
| 440 | }
|
|---|
| 441 |
|
|---|
| 442 | QString QScriptDebuggerConsole::incompleteInput() const
|
|---|
| 443 | {
|
|---|
| 444 | Q_D(const QScriptDebuggerConsole);
|
|---|
| 445 | return d->input;
|
|---|
| 446 | }
|
|---|
| 447 |
|
|---|
| 448 | void QScriptDebuggerConsole::setIncompleteInput(const QString &input)
|
|---|
| 449 | {
|
|---|
| 450 | Q_D(QScriptDebuggerConsole);
|
|---|
| 451 | d->input = input;
|
|---|
| 452 | }
|
|---|
| 453 |
|
|---|
| 454 | QString QScriptDebuggerConsole::commandPrefix() const
|
|---|
| 455 | {
|
|---|
| 456 | Q_D(const QScriptDebuggerConsole);
|
|---|
| 457 | return d->commandPrefix;
|
|---|
| 458 | }
|
|---|
| 459 |
|
|---|
| 460 | /*!
|
|---|
| 461 | Consumes the given line of \a input. If the input starts with the
|
|---|
| 462 | command prefix, it is regarded as a debugger command; otherwise the
|
|---|
| 463 | input is evaluated as a plain script.
|
|---|
| 464 | */
|
|---|
| 465 | QScriptDebuggerConsoleCommandJob *QScriptDebuggerConsole::consumeInput(
|
|---|
| 466 | const QString &input, QScriptMessageHandlerInterface *messageHandler,
|
|---|
| 467 | QScriptDebuggerCommandSchedulerInterface *commandScheduler)
|
|---|
| 468 | {
|
|---|
| 469 | Q_D(QScriptDebuggerConsole);
|
|---|
| 470 | static const int maximumHistoryCount = 100;
|
|---|
| 471 | QString cmd;
|
|---|
| 472 | if (d->input.isEmpty() && input.isEmpty()) {
|
|---|
| 473 | if (d->commandHistory.isEmpty())
|
|---|
| 474 | return 0;
|
|---|
| 475 | cmd = d->commandHistory.first();
|
|---|
| 476 | } else {
|
|---|
| 477 | cmd = input;
|
|---|
| 478 | }
|
|---|
| 479 | if (d->input.isEmpty() && cmd.startsWith(d->commandPrefix)) {
|
|---|
| 480 | if (!input.isEmpty()) {
|
|---|
| 481 | d->commandHistory.prepend(cmd);
|
|---|
| 482 | if (d->commandHistory.size() > maximumHistoryCount)
|
|---|
| 483 | d->commandHistory.removeLast();
|
|---|
| 484 | }
|
|---|
| 485 | cmd.remove(0, d->commandPrefix.length());
|
|---|
| 486 | return d->createJob(cmd, messageHandler, commandScheduler);
|
|---|
| 487 | }
|
|---|
| 488 | d->input += cmd;
|
|---|
| 489 | d->input += QLatin1Char('\n');
|
|---|
| 490 | QScriptSyntaxCheckResult check = QScriptEngine::checkSyntax(d->input);
|
|---|
| 491 | if (check.state() == QScriptSyntaxCheckResult::Intermediate)
|
|---|
| 492 | return false;
|
|---|
| 493 | d->input.chop(1); // remove the last \n
|
|---|
| 494 | cmd = QString();
|
|---|
| 495 | cmd.append(d->commandPrefix);
|
|---|
| 496 | cmd.append(QString::fromLatin1("eval "));
|
|---|
| 497 | cmd.append(d->input);
|
|---|
| 498 | d->commandHistory.prepend(cmd);
|
|---|
| 499 | if (d->commandHistory.size() > maximumHistoryCount)
|
|---|
| 500 | d->commandHistory.removeLast();
|
|---|
| 501 | d->input.clear();
|
|---|
| 502 | cmd.remove(0, d->commandPrefix.length());
|
|---|
| 503 | return d->createJob(cmd, messageHandler, commandScheduler);
|
|---|
| 504 | }
|
|---|
| 505 |
|
|---|
| 506 | int QScriptDebuggerConsole::currentFrameIndex() const
|
|---|
| 507 | {
|
|---|
| 508 | Q_D(const QScriptDebuggerConsole);
|
|---|
| 509 | return d->currentFrameIndex;
|
|---|
| 510 | }
|
|---|
| 511 |
|
|---|
| 512 | void QScriptDebuggerConsole::setCurrentFrameIndex(int index)
|
|---|
| 513 | {
|
|---|
| 514 | Q_D(QScriptDebuggerConsole);
|
|---|
| 515 | d->currentFrameIndex = index;
|
|---|
| 516 | }
|
|---|
| 517 |
|
|---|
| 518 | qint64 QScriptDebuggerConsole::currentScriptId() const
|
|---|
| 519 | {
|
|---|
| 520 | Q_D(const QScriptDebuggerConsole);
|
|---|
| 521 | return d->currentScriptId;
|
|---|
| 522 | }
|
|---|
| 523 |
|
|---|
| 524 | void QScriptDebuggerConsole::setCurrentScriptId(qint64 id)
|
|---|
| 525 | {
|
|---|
| 526 | Q_D(QScriptDebuggerConsole);
|
|---|
| 527 | d->currentScriptId = id;
|
|---|
| 528 | }
|
|---|
| 529 |
|
|---|
| 530 | int QScriptDebuggerConsole::currentLineNumber() const
|
|---|
| 531 | {
|
|---|
| 532 | Q_D(const QScriptDebuggerConsole);
|
|---|
| 533 | return d->currentLineNumber;
|
|---|
| 534 | }
|
|---|
| 535 |
|
|---|
| 536 | void QScriptDebuggerConsole::setCurrentLineNumber(int lineNumber)
|
|---|
| 537 | {
|
|---|
| 538 | Q_D(QScriptDebuggerConsole);
|
|---|
| 539 | d->currentLineNumber = lineNumber;
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 | int QScriptDebuggerConsole::evaluateAction() const
|
|---|
| 543 | {
|
|---|
| 544 | Q_D(const QScriptDebuggerConsole);
|
|---|
| 545 | return d->evaluateAction;
|
|---|
| 546 | }
|
|---|
| 547 |
|
|---|
| 548 | void QScriptDebuggerConsole::setEvaluateAction(int action)
|
|---|
| 549 | {
|
|---|
| 550 | Q_D(QScriptDebuggerConsole);
|
|---|
| 551 | d->evaluateAction = action;
|
|---|
| 552 | }
|
|---|
| 553 |
|
|---|
| 554 | qint64 QScriptDebuggerConsole::sessionId() const
|
|---|
| 555 | {
|
|---|
| 556 | Q_D(const QScriptDebuggerConsole);
|
|---|
| 557 | return d->sessionId;
|
|---|
| 558 | }
|
|---|
| 559 |
|
|---|
| 560 | void QScriptDebuggerConsole::bumpSessionId()
|
|---|
| 561 | {
|
|---|
| 562 | Q_D(QScriptDebuggerConsole);
|
|---|
| 563 | ++d->sessionId;
|
|---|
| 564 | }
|
|---|
| 565 |
|
|---|
| 566 | void QScriptDebuggerConsole::showDebuggerInfoMessage(
|
|---|
| 567 | QScriptMessageHandlerInterface *messageHandler)
|
|---|
| 568 | {
|
|---|
| 569 | messageHandler->message(
|
|---|
| 570 | QtDebugMsg,
|
|---|
| 571 | QString::fromLatin1(
|
|---|
| 572 | "Welcome to the Qt Script debugger.\n"
|
|---|
| 573 | "Debugger commands start with a . (period).\n"
|
|---|
| 574 | "Any other input will be evaluated by the script interpreter.\n"
|
|---|
| 575 | "Type \".help\" for help.\n"));
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | /*!
|
|---|
| 579 | \reimp
|
|---|
| 580 | */
|
|---|
| 581 | int QScriptDebuggerConsole::historyCount() const
|
|---|
| 582 | {
|
|---|
| 583 | Q_D(const QScriptDebuggerConsole);
|
|---|
| 584 | return d->commandHistory.size();
|
|---|
| 585 | }
|
|---|
| 586 |
|
|---|
| 587 | /*!
|
|---|
| 588 | \reimp
|
|---|
| 589 | */
|
|---|
| 590 | QString QScriptDebuggerConsole::historyAt(int index) const
|
|---|
| 591 | {
|
|---|
| 592 | Q_D(const QScriptDebuggerConsole);
|
|---|
| 593 | return d->commandHistory.value(index);
|
|---|
| 594 | }
|
|---|
| 595 |
|
|---|
| 596 | /*!
|
|---|
| 597 | \reimp
|
|---|
| 598 | */
|
|---|
| 599 | void QScriptDebuggerConsole::changeHistoryAt(int index, const QString &newHistory)
|
|---|
| 600 | {
|
|---|
| 601 | Q_D(QScriptDebuggerConsole);
|
|---|
| 602 | d->commandHistory[index] = newHistory;
|
|---|
| 603 | }
|
|---|
| 604 |
|
|---|
| 605 | QT_END_NAMESPACE
|
|---|