| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the QtSCriptTools module of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** 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 are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "qscriptdebuggerscriptedconsolecommand_p.h" | 
|---|
| 43 | #include "qscriptdebuggerconsolecommand_p_p.h" | 
|---|
| 44 | #include "qscriptdebuggerconsolecommandjob_p.h" | 
|---|
| 45 | #include "qscriptdebuggerconsolecommandjob_p_p.h" | 
|---|
| 46 | #include "qscriptmessagehandlerinterface_p.h" | 
|---|
| 47 | #include "qscriptdebuggerconsoleglobalobject_p.h" | 
|---|
| 48 | #include "qscriptdebuggerresponse_p.h" | 
|---|
| 49 | #include "qscriptdebuggervalue_p.h" | 
|---|
| 50 | #include "qscriptdebuggervalueproperty_p.h" | 
|---|
| 51 | #include "qscriptdebuggercommandschedulerinterface_p.h" | 
|---|
| 52 |  | 
|---|
| 53 | #include <QtCore/qstring.h> | 
|---|
| 54 | #include <QtCore/qstringlist.h> | 
|---|
| 55 | #include <QtScript/qscriptengine.h> | 
|---|
| 56 | #include <QtScript/qscriptvalue.h> | 
|---|
| 57 | #include <QtScript/qscriptvalueiterator.h> | 
|---|
| 58 | #include <QtScript/qscriptcontextinfo.h> | 
|---|
| 59 | #include <QtCore/qdebug.h> | 
|---|
| 60 |  | 
|---|
| 61 | Q_DECLARE_METATYPE(QScriptDebuggerResponse) | 
|---|
| 62 | Q_DECLARE_METATYPE(QScriptBreakpointData) | 
|---|
| 63 | Q_DECLARE_METATYPE(QScriptBreakpointMap) | 
|---|
| 64 | Q_DECLARE_METATYPE(QScriptScriptData) | 
|---|
| 65 | Q_DECLARE_METATYPE(QScriptScriptMap) | 
|---|
| 66 | Q_DECLARE_METATYPE(QScriptContextInfo) | 
|---|
| 67 | Q_DECLARE_METATYPE(QScriptDebuggerValue) | 
|---|
| 68 | Q_DECLARE_METATYPE(QScriptDebuggerValueProperty) | 
|---|
| 69 | Q_DECLARE_METATYPE(QScriptDebuggerValuePropertyList) | 
|---|
| 70 | Q_DECLARE_METATYPE(QScriptDebuggerConsoleCommand*) | 
|---|
| 71 | Q_DECLARE_METATYPE(QScriptDebuggerConsoleCommandList) | 
|---|
| 72 | Q_DECLARE_METATYPE(QScriptDebuggerConsoleCommandGroupData) | 
|---|
| 73 | Q_DECLARE_METATYPE(QScriptDebuggerConsoleCommandGroupMap) | 
|---|
| 74 |  | 
|---|
| 75 | QT_BEGIN_NAMESPACE | 
|---|
| 76 |  | 
|---|
| 77 | static QScriptValue debuggerResponseToScriptValue(QScriptEngine *eng, const QScriptDebuggerResponse &in) | 
|---|
| 78 | { | 
|---|
| 79 | QScriptValue out = eng->newObject(); | 
|---|
| 80 | out.setProperty(QString::fromLatin1("result"), qScriptValueFromValue(eng, in.result())); | 
|---|
| 81 | out.setProperty(QString::fromLatin1("error"), QScriptValue(eng, in.error())); | 
|---|
| 82 | out.setProperty(QString::fromLatin1("async"), QScriptValue(eng, in.async())); | 
|---|
| 83 | return out; | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | static void debuggerResponseFromScriptValue(const QScriptValue &, QScriptDebuggerResponse &) | 
|---|
| 87 | { | 
|---|
| 88 | Q_ASSERT(0); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | static QScriptValue breakpointDataToScriptValue(QScriptEngine *eng, const QScriptBreakpointData &in) | 
|---|
| 92 | { | 
|---|
| 93 | QScriptValue out = eng->newObject(); | 
|---|
| 94 | out.setProperty(QString::fromLatin1("scriptId"), QScriptValue(eng, qsreal(in.scriptId()))); | 
|---|
| 95 | out.setProperty(QString::fromLatin1("fileName"), QScriptValue(eng, in.fileName())); | 
|---|
| 96 | out.setProperty(QString::fromLatin1("lineNumber"), QScriptValue(eng, in.lineNumber())); | 
|---|
| 97 | out.setProperty(QString::fromLatin1("enabled"), QScriptValue(eng, in.isEnabled())); | 
|---|
| 98 | out.setProperty(QString::fromLatin1("singleShot"), QScriptValue(eng, in.isSingleShot())); | 
|---|
| 99 | out.setProperty(QString::fromLatin1("ignoreCount"), QScriptValue(eng, in.ignoreCount())); | 
|---|
| 100 | out.setProperty(QString::fromLatin1("condition"), QScriptValue(eng, in.condition())); | 
|---|
| 101 | return out; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | static void breakpointDataFromScriptValue(const QScriptValue &in, QScriptBreakpointData &out) | 
|---|
| 105 | { | 
|---|
| 106 | QScriptValue scriptId = in.property(QString::fromLatin1("scriptId")); | 
|---|
| 107 | if (scriptId.isValid()) | 
|---|
| 108 | out.setScriptId((qint64)scriptId.toNumber()); | 
|---|
| 109 | out.setFileName(in.property(QString::fromLatin1("fileName")).toString()); | 
|---|
| 110 | out.setLineNumber(in.property(QString::fromLatin1("lineNumber")).toInt32()); | 
|---|
| 111 | QScriptValue enabled = in.property(QString::fromLatin1("enabled")); | 
|---|
| 112 | if (enabled.isValid()) | 
|---|
| 113 | out.setEnabled(enabled.toBoolean()); | 
|---|
| 114 | QScriptValue singleShot = in.property(QString::fromLatin1("singleShot")); | 
|---|
| 115 | if (singleShot.isValid()) | 
|---|
| 116 | out.setSingleShot(singleShot.toBoolean()); | 
|---|
| 117 | out.setIgnoreCount(in.property(QString::fromLatin1("ignoreCount")).toInt32()); | 
|---|
| 118 | out.setCondition(in.property(QString::fromLatin1("condition")).toString()); | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | static QScriptValue breakpointMapToScriptValue(QScriptEngine *eng, const QScriptBreakpointMap &in) | 
|---|
| 122 | { | 
|---|
| 123 | QScriptValue out = eng->newObject(); | 
|---|
| 124 | QScriptBreakpointMap::const_iterator it; | 
|---|
| 125 | for (it = in.constBegin(); it != in.constEnd(); ++it) { | 
|---|
| 126 | out.setProperty(QString::number(it.key()), qScriptValueFromValue(eng, it.value())); | 
|---|
| 127 | } | 
|---|
| 128 | return out; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | static void breakpointMapFromScriptValue(const QScriptValue &, QScriptBreakpointMap &) | 
|---|
| 132 | { | 
|---|
| 133 | Q_ASSERT(0); | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | static QScriptValue scriptDataToScriptValue(QScriptEngine *eng, const QScriptScriptData &in) | 
|---|
| 137 | { | 
|---|
| 138 | QScriptValue out = eng->newObject(); | 
|---|
| 139 | out.setProperty(QString::fromLatin1("contents"), QScriptValue(eng, in.contents())); | 
|---|
| 140 | out.setProperty(QString::fromLatin1("fileName"), QScriptValue(eng, in.fileName())); | 
|---|
| 141 | out.setProperty(QString::fromLatin1("baseLineNumber"), QScriptValue(eng, in.baseLineNumber())); | 
|---|
| 142 | return out; | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | static void scriptDataFromScriptValue(const QScriptValue &in, QScriptScriptData &out) | 
|---|
| 146 | { | 
|---|
| 147 | QString contents = in.property(QString::fromLatin1("contents")).toString(); | 
|---|
| 148 | QString fileName = in.property(QString::fromLatin1("fileName")).toString(); | 
|---|
| 149 | int baseLineNumber = in.property(QString::fromLatin1("baseLineNumber")).toInt32(); | 
|---|
| 150 | QScriptScriptData tmp(contents, fileName, baseLineNumber); | 
|---|
| 151 | out = tmp; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | static QScriptValue scriptMapToScriptValue(QScriptEngine *eng, const QScriptScriptMap &in) | 
|---|
| 155 | { | 
|---|
| 156 | QScriptValue out = eng->newObject(); | 
|---|
| 157 | QScriptScriptMap::const_iterator it; | 
|---|
| 158 | for (it = in.constBegin(); it != in.constEnd(); ++it) { | 
|---|
| 159 | out.setProperty(QString::number(it.key()), qScriptValueFromValue(eng, it.value())); | 
|---|
| 160 | } | 
|---|
| 161 | return out; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | static void scriptMapFromScriptValue(const QScriptValue &, QScriptScriptMap &) | 
|---|
| 165 | { | 
|---|
| 166 | Q_ASSERT(0); | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | static QScriptValue consoleCommandToScriptValue( | 
|---|
| 170 | QScriptEngine *eng, QScriptDebuggerConsoleCommand* const &in) | 
|---|
| 171 | { | 
|---|
| 172 | if (!in) | 
|---|
| 173 | return eng->undefinedValue(); | 
|---|
| 174 | QScriptValue out = eng->newObject(); | 
|---|
| 175 | out.setProperty(QString::fromLatin1("name"), QScriptValue(eng, in->name())); | 
|---|
| 176 | out.setProperty(QString::fromLatin1("group"), QScriptValue(eng, in->group())); | 
|---|
| 177 | out.setProperty(QString::fromLatin1("shortDescription"), QScriptValue(eng, in->shortDescription())); | 
|---|
| 178 | out.setProperty(QString::fromLatin1("longDescription"), QScriptValue(eng, in->longDescription())); | 
|---|
| 179 | out.setProperty(QString::fromLatin1("aliases"), qScriptValueFromValue(eng, in->aliases())); | 
|---|
| 180 | out.setProperty(QString::fromLatin1("seeAlso"), qScriptValueFromValue(eng, in->seeAlso())); | 
|---|
| 181 | return out; | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | static void consoleCommandFromScriptValue( | 
|---|
| 185 | const QScriptValue &, QScriptDebuggerConsoleCommand* &) | 
|---|
| 186 | { | 
|---|
| 187 | Q_ASSERT(0); | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | static QScriptValue consoleCommandGroupDataToScriptValue( | 
|---|
| 191 | QScriptEngine *eng, const QScriptDebuggerConsoleCommandGroupData &in) | 
|---|
| 192 | { | 
|---|
| 193 | QScriptValue out = eng->newObject(); | 
|---|
| 194 | out.setProperty(QString::fromLatin1("longDescription"), QScriptValue(eng, in.longDescription())); | 
|---|
| 195 | out.setProperty(QString::fromLatin1("shortDescription"), QScriptValue(eng, in.shortDescription())); | 
|---|
| 196 | return out; | 
|---|
| 197 | } | 
|---|
| 198 |  | 
|---|
| 199 | static void consoleCommandGroupDataFromScriptValue( | 
|---|
| 200 | const QScriptValue &, QScriptDebuggerConsoleCommandGroupData &) | 
|---|
| 201 | { | 
|---|
| 202 | Q_ASSERT(0); | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | static QScriptValue consoleCommandGroupMapToScriptValue( | 
|---|
| 206 | QScriptEngine *eng, const QScriptDebuggerConsoleCommandGroupMap &in) | 
|---|
| 207 | { | 
|---|
| 208 | QScriptValue out = eng->newObject(); | 
|---|
| 209 | QScriptDebuggerConsoleCommandGroupMap::const_iterator it; | 
|---|
| 210 | for (it = in.constBegin(); it != in.constEnd(); ++it) { | 
|---|
| 211 | out.setProperty(it.key(), qScriptValueFromValue(eng, it.value())); | 
|---|
| 212 | } | 
|---|
| 213 | return out; | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | static void consoleCommandGroupMapFromScriptValue( | 
|---|
| 217 | const QScriptValue &, QScriptDebuggerConsoleCommandGroupMap &) | 
|---|
| 218 | { | 
|---|
| 219 | Q_ASSERT(0); | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 | static QScriptValue contextInfoToScriptValue(QScriptEngine *eng, const QScriptContextInfo &in) | 
|---|
| 223 | { | 
|---|
| 224 | QScriptValue out = eng->newObject(); | 
|---|
| 225 | out.setProperty(QString::fromLatin1("scriptId"), QScriptValue(eng, qsreal(in.scriptId()))); | 
|---|
| 226 | out.setProperty(QString::fromLatin1("fileName"), QScriptValue(eng, in.fileName())); | 
|---|
| 227 | out.setProperty(QString::fromLatin1("lineNumber"), QScriptValue(eng, in.lineNumber())); | 
|---|
| 228 | out.setProperty(QString::fromLatin1("columnNumber"), QScriptValue(eng, in.columnNumber())); | 
|---|
| 229 | out.setProperty(QString::fromLatin1("functionName"), QScriptValue(eng, in.functionName())); | 
|---|
| 230 | return out; | 
|---|
| 231 | } | 
|---|
| 232 |  | 
|---|
| 233 | static void contextInfoFromScriptValue(const QScriptValue &, QScriptContextInfo &) | 
|---|
| 234 | { | 
|---|
| 235 | Q_ASSERT(0); | 
|---|
| 236 | } | 
|---|
| 237 |  | 
|---|
| 238 | static QScriptValue debuggerScriptValuePropertyToScriptValue(QScriptEngine *eng, const QScriptDebuggerValueProperty &in) | 
|---|
| 239 | { | 
|---|
| 240 | QScriptValue out = eng->newObject(); | 
|---|
| 241 | out.setProperty(QString::fromLatin1("name"), QScriptValue(eng, in.name())); | 
|---|
| 242 | out.setProperty(QString::fromLatin1("value"), qScriptValueFromValue(eng, in.value())); | 
|---|
| 243 | out.setProperty(QString::fromLatin1("valueAsString"), QScriptValue(eng, in.valueAsString())); | 
|---|
| 244 | out.setProperty(QString::fromLatin1("flags"), QScriptValue(eng, static_cast<int>(in.flags()))); | 
|---|
| 245 | return out; | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|
| 248 | static void debuggerScriptValuePropertyFromScriptValue(const QScriptValue &in, QScriptDebuggerValueProperty &out) | 
|---|
| 249 | { | 
|---|
| 250 | QString name = in.property(QString::fromLatin1("name")).toString(); | 
|---|
| 251 | QScriptDebuggerValue value = qscriptvalue_cast<QScriptDebuggerValue>(in.property(QString::fromLatin1("value"))); | 
|---|
| 252 | QString valueAsString = in.property(QString::fromLatin1("valueAsString")).toString(); | 
|---|
| 253 | int flags = in.property(QString::fromLatin1("flags")).toInt32(); | 
|---|
| 254 | QScriptDebuggerValueProperty tmp(name, value, valueAsString, QScriptValue::PropertyFlags(flags)); | 
|---|
| 255 | out = tmp; | 
|---|
| 256 | } | 
|---|
| 257 |  | 
|---|
| 258 | /*! | 
|---|
| 259 | \since 4.5 | 
|---|
| 260 | \class QScriptDebuggerScriptedConsoleCommand | 
|---|
| 261 | \internal | 
|---|
| 262 |  | 
|---|
| 263 | \brief The QScriptDebuggerScriptedConsoleCommand class encapsulates a command defined in a script. | 
|---|
| 264 | */ | 
|---|
| 265 |  | 
|---|
| 266 | class QScriptDebuggerScriptedConsoleCommandPrivate | 
|---|
| 267 | : public QScriptDebuggerConsoleCommandPrivate | 
|---|
| 268 | { | 
|---|
| 269 | Q_DECLARE_PUBLIC(QScriptDebuggerScriptedConsoleCommand) | 
|---|
| 270 | public: | 
|---|
| 271 | QScriptDebuggerScriptedConsoleCommandPrivate(); | 
|---|
| 272 | ~QScriptDebuggerScriptedConsoleCommandPrivate(); | 
|---|
| 273 |  | 
|---|
| 274 | QString name; | 
|---|
| 275 | QString group; | 
|---|
| 276 | QString shortDescription; | 
|---|
| 277 | QString longDescription; | 
|---|
| 278 | QStringList aliases; | 
|---|
| 279 | QStringList seeAlso; | 
|---|
| 280 | QStringList argumentTypes; | 
|---|
| 281 | QStringList subCommands; | 
|---|
| 282 | QScriptEngine *engine; | 
|---|
| 283 | QScriptValue execFunction; | 
|---|
| 284 | QScriptValue responseFunction; | 
|---|
| 285 | }; | 
|---|
| 286 |  | 
|---|
| 287 | QScriptDebuggerScriptedConsoleCommandPrivate::QScriptDebuggerScriptedConsoleCommandPrivate() | 
|---|
| 288 | { | 
|---|
| 289 | engine = 0; | 
|---|
| 290 | } | 
|---|
| 291 |  | 
|---|
| 292 | QScriptDebuggerScriptedConsoleCommandPrivate::~QScriptDebuggerScriptedConsoleCommandPrivate() | 
|---|
| 293 | { | 
|---|
| 294 | delete engine; | 
|---|
| 295 | } | 
|---|
| 296 |  | 
|---|
| 297 | QScriptDebuggerScriptedConsoleCommand::QScriptDebuggerScriptedConsoleCommand( | 
|---|
| 298 | const QString &name, const QString &group, | 
|---|
| 299 | const QString &shortDescription, const QString &longDescription, | 
|---|
| 300 | const QStringList &aliases, const QStringList &seeAlso, | 
|---|
| 301 | const QStringList &argumentTypes, const QStringList &subCommands, | 
|---|
| 302 | const QScriptValue &execFunction, const QScriptValue &responseFunction) | 
|---|
| 303 | : QScriptDebuggerConsoleCommand(*new QScriptDebuggerScriptedConsoleCommandPrivate) | 
|---|
| 304 | { | 
|---|
| 305 | Q_D(QScriptDebuggerScriptedConsoleCommand); | 
|---|
| 306 | d->name = name; | 
|---|
| 307 | d->group = group; | 
|---|
| 308 | d->shortDescription = shortDescription; | 
|---|
| 309 | d->longDescription = longDescription; | 
|---|
| 310 | d->aliases = aliases; | 
|---|
| 311 | d->seeAlso = seeAlso; | 
|---|
| 312 | d->argumentTypes = argumentTypes; | 
|---|
| 313 | d->subCommands = subCommands; | 
|---|
| 314 | d->execFunction = execFunction; | 
|---|
| 315 | d->responseFunction = responseFunction; | 
|---|
| 316 | d->engine = execFunction.engine(); | 
|---|
| 317 |  | 
|---|
| 318 | qScriptRegisterMetaType<QScriptBreakpointData>(d->engine, breakpointDataToScriptValue, breakpointDataFromScriptValue); | 
|---|
| 319 | qScriptRegisterMetaType<QScriptBreakpointMap>(d->engine, breakpointMapToScriptValue, breakpointMapFromScriptValue); | 
|---|
| 320 | qScriptRegisterMetaType<QScriptScriptData>(d->engine, scriptDataToScriptValue, scriptDataFromScriptValue); | 
|---|
| 321 | qScriptRegisterMetaType<QScriptScriptMap>(d->engine, scriptMapToScriptValue, scriptMapFromScriptValue); | 
|---|
| 322 | qScriptRegisterMetaType<QScriptContextInfo>(d->engine, contextInfoToScriptValue, contextInfoFromScriptValue); | 
|---|
| 323 | qScriptRegisterMetaType<QScriptDebuggerValueProperty>(d->engine, debuggerScriptValuePropertyToScriptValue, debuggerScriptValuePropertyFromScriptValue); | 
|---|
| 324 | qScriptRegisterSequenceMetaType<QScriptDebuggerValuePropertyList>(d->engine); | 
|---|
| 325 | qScriptRegisterMetaType<QScriptDebuggerResponse>(d->engine, debuggerResponseToScriptValue, debuggerResponseFromScriptValue); | 
|---|
| 326 | qScriptRegisterMetaType<QScriptDebuggerConsoleCommand*>(d->engine, consoleCommandToScriptValue, consoleCommandFromScriptValue); | 
|---|
| 327 | qScriptRegisterSequenceMetaType<QScriptDebuggerConsoleCommandList>(d->engine); | 
|---|
| 328 | qScriptRegisterMetaType<QScriptDebuggerConsoleCommandGroupData>(d->engine, consoleCommandGroupDataToScriptValue, consoleCommandGroupDataFromScriptValue); | 
|---|
| 329 | qScriptRegisterMetaType<QScriptDebuggerConsoleCommandGroupMap>(d->engine, consoleCommandGroupMapToScriptValue, consoleCommandGroupMapFromScriptValue); | 
|---|
| 330 | // ### can't do this, if it's an object ID the conversion will be incorrect since | 
|---|
| 331 | // ### the object ID refers to an object in a different engine! | 
|---|
| 332 | //    qScriptRegisterMetaType(d->engine, debuggerScriptValueToScriptValue, debuggerScriptValueFromScriptValue); | 
|---|
| 333 | } | 
|---|
| 334 |  | 
|---|
| 335 | QScriptDebuggerScriptedConsoleCommand::~QScriptDebuggerScriptedConsoleCommand() | 
|---|
| 336 | { | 
|---|
| 337 | } | 
|---|
| 338 |  | 
|---|
| 339 | class QScriptDebuggerScriptedConsoleCommandJobPrivate; | 
|---|
| 340 | class QScriptDebuggerScriptedConsoleCommandJob | 
|---|
| 341 | : public QScriptDebuggerConsoleCommandJob, | 
|---|
| 342 | public QScriptDebuggerCommandSchedulerInterface | 
|---|
| 343 | { | 
|---|
| 344 | public: | 
|---|
| 345 | QScriptDebuggerScriptedConsoleCommandJob( | 
|---|
| 346 | QScriptDebuggerScriptedConsoleCommandPrivate *command, | 
|---|
| 347 | const QStringList &arguments, | 
|---|
| 348 | QScriptDebuggerConsole *console, | 
|---|
| 349 | QScriptMessageHandlerInterface *messageHandler, | 
|---|
| 350 | QScriptDebuggerCommandSchedulerInterface *commandScheduler); | 
|---|
| 351 | ~QScriptDebuggerScriptedConsoleCommandJob(); | 
|---|
| 352 |  | 
|---|
| 353 | int scheduleCommand( | 
|---|
| 354 | const QScriptDebuggerCommand &command, | 
|---|
| 355 | QScriptDebuggerResponseHandlerInterface *responseHandler); | 
|---|
| 356 |  | 
|---|
| 357 | void start(); | 
|---|
| 358 | void handleResponse(const QScriptDebuggerResponse &response, | 
|---|
| 359 | int commandId); | 
|---|
| 360 |  | 
|---|
| 361 | private: | 
|---|
| 362 | Q_DECLARE_PRIVATE(QScriptDebuggerScriptedConsoleCommandJob) | 
|---|
| 363 | Q_DISABLE_COPY(QScriptDebuggerScriptedConsoleCommandJob) | 
|---|
| 364 | }; | 
|---|
| 365 |  | 
|---|
| 366 | class QScriptDebuggerScriptedConsoleCommandJobPrivate | 
|---|
| 367 | : public QScriptDebuggerConsoleCommandJobPrivate | 
|---|
| 368 | { | 
|---|
| 369 | public: | 
|---|
| 370 | QScriptDebuggerScriptedConsoleCommandJobPrivate() {} | 
|---|
| 371 | ~QScriptDebuggerScriptedConsoleCommandJobPrivate() {} | 
|---|
| 372 |  | 
|---|
| 373 | QScriptDebuggerScriptedConsoleCommandPrivate *command; | 
|---|
| 374 | QStringList arguments; | 
|---|
| 375 | int commandCount; | 
|---|
| 376 | }; | 
|---|
| 377 |  | 
|---|
| 378 | QScriptDebuggerScriptedConsoleCommandJob::QScriptDebuggerScriptedConsoleCommandJob( | 
|---|
| 379 | QScriptDebuggerScriptedConsoleCommandPrivate *command, | 
|---|
| 380 | const QStringList &arguments, | 
|---|
| 381 | QScriptDebuggerConsole *console, | 
|---|
| 382 | QScriptMessageHandlerInterface *messageHandler, | 
|---|
| 383 | QScriptDebuggerCommandSchedulerInterface *commandScheduler) | 
|---|
| 384 | : QScriptDebuggerConsoleCommandJob(*new QScriptDebuggerScriptedConsoleCommandJobPrivate, | 
|---|
| 385 | console, messageHandler, commandScheduler) | 
|---|
| 386 | { | 
|---|
| 387 | Q_D(QScriptDebuggerScriptedConsoleCommandJob); | 
|---|
| 388 | d->command = command; | 
|---|
| 389 | d->arguments = arguments; | 
|---|
| 390 | } | 
|---|
| 391 |  | 
|---|
| 392 | QScriptDebuggerScriptedConsoleCommandJob::~QScriptDebuggerScriptedConsoleCommandJob() | 
|---|
| 393 | { | 
|---|
| 394 | } | 
|---|
| 395 |  | 
|---|
| 396 | int QScriptDebuggerScriptedConsoleCommandJob::scheduleCommand( | 
|---|
| 397 | const QScriptDebuggerCommand &command, | 
|---|
| 398 | QScriptDebuggerResponseHandlerInterface *responseHandler) | 
|---|
| 399 | { | 
|---|
| 400 | Q_D(QScriptDebuggerScriptedConsoleCommandJob); | 
|---|
| 401 | ++d->commandCount; | 
|---|
| 402 | return commandScheduler()->scheduleCommand(command, responseHandler); | 
|---|
| 403 | } | 
|---|
| 404 |  | 
|---|
| 405 | void QScriptDebuggerScriptedConsoleCommandJob::start() | 
|---|
| 406 | { | 
|---|
| 407 | Q_D(QScriptDebuggerScriptedConsoleCommandJob); | 
|---|
| 408 | QScriptEngine *engine = d->command->engine; | 
|---|
| 409 | QScriptValueList args; | 
|---|
| 410 | for (int i = 0; i < d->arguments.size(); ++i) | 
|---|
| 411 | args.append(QScriptValue(engine, d->arguments.at(i))); | 
|---|
| 412 | QScriptDebuggerConsoleGlobalObject *global; | 
|---|
| 413 | #if QT_VERSION >= 0x040500 | 
|---|
| 414 | global = qobject_cast<QScriptDebuggerConsoleGlobalObject*>(engine->globalObject().toQObject()); | 
|---|
| 415 | #else | 
|---|
| 416 | global = qobject_cast<QScriptDebuggerConsoleGlobalObject*>(engine->globalObject().scope().toQObject()); | 
|---|
| 417 | #endif | 
|---|
| 418 | Q_ASSERT(global != 0); | 
|---|
| 419 | global->setScheduler(this); | 
|---|
| 420 | global->setResponseHandler(this); | 
|---|
| 421 | global->setMessageHandler(d->messageHandler); | 
|---|
| 422 | global->setConsole(d->console); | 
|---|
| 423 | d->commandCount = 0; | 
|---|
| 424 | QScriptValue ret = d->command->execFunction.call(QScriptValue(), args); | 
|---|
| 425 | global->setScheduler(0); | 
|---|
| 426 | global->setResponseHandler(0); | 
|---|
| 427 | global->setMessageHandler(0); | 
|---|
| 428 | global->setConsole(0); | 
|---|
| 429 | if (ret.isError()) { | 
|---|
| 430 | qWarning("*** internal error: %s", qPrintable(ret.toString())); | 
|---|
| 431 | } | 
|---|
| 432 | if (d->commandCount == 0) | 
|---|
| 433 | finish(); | 
|---|
| 434 | } | 
|---|
| 435 |  | 
|---|
| 436 | void QScriptDebuggerScriptedConsoleCommandJob::handleResponse( | 
|---|
| 437 | const QScriptDebuggerResponse &response, | 
|---|
| 438 | int commandId) | 
|---|
| 439 | { | 
|---|
| 440 | Q_D(QScriptDebuggerScriptedConsoleCommandJob); | 
|---|
| 441 | // ### generalize | 
|---|
| 442 | QScriptEngine *engine = d->command->engine; | 
|---|
| 443 | QScriptValueList args; | 
|---|
| 444 | args.append(qScriptValueFromValue(engine, response)); | 
|---|
| 445 | args.append(QScriptValue(engine, commandId)); | 
|---|
| 446 | QScriptDebuggerConsoleGlobalObject *global; | 
|---|
| 447 | #if QT_VERSION >= 0x040500 | 
|---|
| 448 | global = qobject_cast<QScriptDebuggerConsoleGlobalObject*>(engine->globalObject().toQObject()); | 
|---|
| 449 | #else | 
|---|
| 450 | global = qobject_cast<QScriptDebuggerConsoleGlobalObject*>(engine->globalObject().scope().toQObject()); | 
|---|
| 451 | #endif | 
|---|
| 452 | Q_ASSERT(global != 0); | 
|---|
| 453 | global->setScheduler(this); | 
|---|
| 454 | global->setResponseHandler(this); | 
|---|
| 455 | global->setMessageHandler(d->messageHandler); | 
|---|
| 456 | global->setConsole(d->console); | 
|---|
| 457 | d->commandCount = 0; | 
|---|
| 458 | QScriptValue ret = d->command->responseFunction.call(QScriptValue(), args); | 
|---|
| 459 | global->setScheduler(0); | 
|---|
| 460 | global->setResponseHandler(0); | 
|---|
| 461 | global->setMessageHandler(0); | 
|---|
| 462 | global->setConsole(0); | 
|---|
| 463 | if (ret.isError()) { | 
|---|
| 464 | qWarning("*** internal error: %s", qPrintable(ret.toString())); | 
|---|
| 465 | } | 
|---|
| 466 | if (d->commandCount == 0) | 
|---|
| 467 | finish(); | 
|---|
| 468 | } | 
|---|
| 469 |  | 
|---|
| 470 | /*! | 
|---|
| 471 | \reimp | 
|---|
| 472 | */ | 
|---|
| 473 | QString QScriptDebuggerScriptedConsoleCommand::name() const | 
|---|
| 474 | { | 
|---|
| 475 | Q_D(const QScriptDebuggerScriptedConsoleCommand); | 
|---|
| 476 | return d->name; | 
|---|
| 477 | } | 
|---|
| 478 |  | 
|---|
| 479 | /*! | 
|---|
| 480 | \reimp | 
|---|
| 481 | */ | 
|---|
| 482 | QString QScriptDebuggerScriptedConsoleCommand::group() const | 
|---|
| 483 | { | 
|---|
| 484 | Q_D(const QScriptDebuggerScriptedConsoleCommand); | 
|---|
| 485 | return d->group; | 
|---|
| 486 | } | 
|---|
| 487 |  | 
|---|
| 488 | /*! | 
|---|
| 489 | \reimp | 
|---|
| 490 | */ | 
|---|
| 491 | QString QScriptDebuggerScriptedConsoleCommand::shortDescription() const | 
|---|
| 492 | { | 
|---|
| 493 | Q_D(const QScriptDebuggerScriptedConsoleCommand); | 
|---|
| 494 | return d->shortDescription; | 
|---|
| 495 | } | 
|---|
| 496 |  | 
|---|
| 497 | /*! | 
|---|
| 498 | \reimp | 
|---|
| 499 | */ | 
|---|
| 500 | QString QScriptDebuggerScriptedConsoleCommand::longDescription() const | 
|---|
| 501 | { | 
|---|
| 502 | Q_D(const QScriptDebuggerScriptedConsoleCommand); | 
|---|
| 503 | return d->longDescription; | 
|---|
| 504 | } | 
|---|
| 505 |  | 
|---|
| 506 | /*! | 
|---|
| 507 | \reimp | 
|---|
| 508 | */ | 
|---|
| 509 | QStringList QScriptDebuggerScriptedConsoleCommand::aliases() const | 
|---|
| 510 | { | 
|---|
| 511 | Q_D(const QScriptDebuggerScriptedConsoleCommand); | 
|---|
| 512 | return d->aliases; | 
|---|
| 513 | } | 
|---|
| 514 |  | 
|---|
| 515 | /*! | 
|---|
| 516 | \reimp | 
|---|
| 517 | */ | 
|---|
| 518 | QStringList QScriptDebuggerScriptedConsoleCommand::seeAlso() const | 
|---|
| 519 | { | 
|---|
| 520 | Q_D(const QScriptDebuggerScriptedConsoleCommand); | 
|---|
| 521 | return d->seeAlso; | 
|---|
| 522 | } | 
|---|
| 523 |  | 
|---|
| 524 | /*! | 
|---|
| 525 | \reimp | 
|---|
| 526 | */ | 
|---|
| 527 | QStringList QScriptDebuggerScriptedConsoleCommand::argumentTypes() const | 
|---|
| 528 | { | 
|---|
| 529 | Q_D(const QScriptDebuggerScriptedConsoleCommand); | 
|---|
| 530 | return d->argumentTypes; | 
|---|
| 531 | } | 
|---|
| 532 |  | 
|---|
| 533 | /*! | 
|---|
| 534 | \reimp | 
|---|
| 535 | */ | 
|---|
| 536 | QStringList QScriptDebuggerScriptedConsoleCommand::subCommands() const | 
|---|
| 537 | { | 
|---|
| 538 | Q_D(const QScriptDebuggerScriptedConsoleCommand); | 
|---|
| 539 | return d->subCommands; | 
|---|
| 540 | } | 
|---|
| 541 |  | 
|---|
| 542 | /*! | 
|---|
| 543 | \reimp | 
|---|
| 544 | */ | 
|---|
| 545 | QScriptDebuggerConsoleCommandJob *QScriptDebuggerScriptedConsoleCommand::createJob( | 
|---|
| 546 | const QStringList &arguments, | 
|---|
| 547 | QScriptDebuggerConsole *console, | 
|---|
| 548 | QScriptMessageHandlerInterface *messageHandler, | 
|---|
| 549 | QScriptDebuggerCommandSchedulerInterface *commandScheduler) | 
|---|
| 550 | { | 
|---|
| 551 | Q_D(QScriptDebuggerScriptedConsoleCommand); | 
|---|
| 552 | return new QScriptDebuggerScriptedConsoleCommandJob( | 
|---|
| 553 | d, arguments, console, messageHandler, commandScheduler); | 
|---|
| 554 | } | 
|---|
| 555 |  | 
|---|
| 556 | /*! | 
|---|
| 557 | Parses a command defined by the given \a program. | 
|---|
| 558 | Returns an object that encapsulates the command, or 0 if parsing failed. | 
|---|
| 559 | */ | 
|---|
| 560 | QScriptDebuggerScriptedConsoleCommand *QScriptDebuggerScriptedConsoleCommand::parse( | 
|---|
| 561 | const QString &program, const QString &fileName, | 
|---|
| 562 | QScriptMessageHandlerInterface *messageHandler) | 
|---|
| 563 | { | 
|---|
| 564 | QScriptEngine *engine = new QScriptEngine(); | 
|---|
| 565 | // create a custom global object | 
|---|
| 566 | QScriptDebuggerConsoleGlobalObject *cppGlobal = new QScriptDebuggerConsoleGlobalObject(); | 
|---|
| 567 | QScriptValue global = engine->newQObject(cppGlobal, | 
|---|
| 568 | QScriptEngine::ScriptOwnership, | 
|---|
| 569 | #if QT_VERSION >= 0x040500 | 
|---|
| 570 | QScriptEngine::ExcludeSuperClassContents); | 
|---|
| 571 | #else | 
|---|
| 572 | QScriptEngine::ExcludeSuperClassMethods | 
|---|
| 573 | | QScriptEngine::ExcludeSuperClassProperties); | 
|---|
| 574 | #endif | 
|---|
| 575 | { | 
|---|
| 576 | QScriptValueIterator it(engine->globalObject()); | 
|---|
| 577 | while (it.hasNext()) { | 
|---|
| 578 | it.next(); | 
|---|
| 579 | global.setProperty(it.scriptName(), it.value(), it.flags()); | 
|---|
| 580 | } | 
|---|
| 581 | } | 
|---|
| 582 | #if QT_VERSION >= 0x040500 | 
|---|
| 583 | engine->setGlobalObject(global); | 
|---|
| 584 | #else | 
|---|
| 585 | engine->globalObject().setScope(global); | 
|---|
| 586 | global = engine->globalObject(); | 
|---|
| 587 | #endif | 
|---|
| 588 |  | 
|---|
| 589 | cppGlobal->setMessageHandler(messageHandler); | 
|---|
| 590 | QScriptValue ret = engine->evaluate(program, fileName); | 
|---|
| 591 | cppGlobal->setMessageHandler(0); | 
|---|
| 592 | if (engine->hasUncaughtException()) { | 
|---|
| 593 | messageHandler->message(QtCriticalMsg, ret.toString(), fileName, | 
|---|
| 594 | engine->uncaughtExceptionLineNumber()); | 
|---|
| 595 | delete engine; | 
|---|
| 596 | return 0; | 
|---|
| 597 | } | 
|---|
| 598 |  | 
|---|
| 599 | QScriptValue name = global.property(QLatin1String("name")); | 
|---|
| 600 | if (!name.isString()) { | 
|---|
| 601 | messageHandler->message(QtCriticalMsg, QLatin1String("command definition lacks a name"), fileName); | 
|---|
| 602 | delete engine; | 
|---|
| 603 | return 0; | 
|---|
| 604 | } | 
|---|
| 605 | QString nameStr = name.toString(); | 
|---|
| 606 |  | 
|---|
| 607 | QScriptValue group = global.property(QLatin1String("group")); | 
|---|
| 608 | if (!group.isString()) { | 
|---|
| 609 | messageHandler->message(QtCriticalMsg, QString::fromLatin1("definition of command \"%0\" lacks a group name") | 
|---|
| 610 | .arg(nameStr), fileName); | 
|---|
| 611 | delete engine; | 
|---|
| 612 | return 0; | 
|---|
| 613 | } | 
|---|
| 614 | QString groupStr = group.toString(); | 
|---|
| 615 |  | 
|---|
| 616 | QScriptValue shortDesc = global.property(QLatin1String("shortDescription")); | 
|---|
| 617 | if (!shortDesc.isString()) { | 
|---|
| 618 | messageHandler->message(QtCriticalMsg, QString::fromLatin1("definition of command \"%0\" lacks shortDescription") | 
|---|
| 619 | .arg(nameStr), fileName); | 
|---|
| 620 | delete engine; | 
|---|
| 621 | return 0; | 
|---|
| 622 | } | 
|---|
| 623 | QString shortDescStr = shortDesc.toString(); | 
|---|
| 624 |  | 
|---|
| 625 | QScriptValue longDesc = global.property(QLatin1String("longDescription")); | 
|---|
| 626 | if (!longDesc.isString()) { | 
|---|
| 627 | messageHandler->message(QtCriticalMsg, QString::fromLatin1("definition of command \"%0\" lacks longDescription") | 
|---|
| 628 | .arg(nameStr), fileName); | 
|---|
| 629 | delete engine; | 
|---|
| 630 | return 0; | 
|---|
| 631 | } | 
|---|
| 632 | QString longDescStr = longDesc.toString(); | 
|---|
| 633 |  | 
|---|
| 634 | QStringList aliases; | 
|---|
| 635 | qScriptValueToSequence(global.property(QLatin1String("aliases")), aliases); | 
|---|
| 636 |  | 
|---|
| 637 | QStringList seeAlso; | 
|---|
| 638 | qScriptValueToSequence(global.property(QLatin1String("seeAlso")), seeAlso); | 
|---|
| 639 |  | 
|---|
| 640 | QStringList argTypes; | 
|---|
| 641 | qScriptValueToSequence(global.property(QLatin1String("argumentTypes")), argTypes); | 
|---|
| 642 |  | 
|---|
| 643 | QStringList subCommands; | 
|---|
| 644 | qScriptValueToSequence(global.property(QLatin1String("subCommands")), subCommands); | 
|---|
| 645 |  | 
|---|
| 646 | QScriptValue execFunction = global.property(QLatin1String("execute")); | 
|---|
| 647 | if (!execFunction.isFunction()) { | 
|---|
| 648 | messageHandler->message(QtCriticalMsg, QString::fromLatin1("definition of command \"%0\" lacks execute() function") | 
|---|
| 649 | .arg(nameStr), fileName); | 
|---|
| 650 | delete engine; | 
|---|
| 651 | return 0; | 
|---|
| 652 | } | 
|---|
| 653 |  | 
|---|
| 654 | QScriptValue responseFunction = global.property(QLatin1String("handleResponse")); | 
|---|
| 655 |  | 
|---|
| 656 | QScriptDebuggerScriptedConsoleCommand *result = new QScriptDebuggerScriptedConsoleCommand( | 
|---|
| 657 | nameStr, groupStr, | 
|---|
| 658 | shortDescStr, longDescStr, | 
|---|
| 659 | aliases, seeAlso, | 
|---|
| 660 | argTypes, subCommands, | 
|---|
| 661 | execFunction, responseFunction); | 
|---|
| 662 | return result; | 
|---|
| 663 | } | 
|---|
| 664 |  | 
|---|
| 665 | QT_END_NAMESPACE | 
|---|