Qt 4.8
qscriptdebuggerscriptedconsolecommand.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSCriptTools module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
50 
51 #include <QtCore/qstring.h>
52 #include <QtCore/qstringlist.h>
53 #include <QtScript/qscriptengine.h>
54 #include <QtScript/qscriptvalue.h>
55 #include <QtScript/qscriptvalueiterator.h>
56 #include <QtScript/qscriptcontextinfo.h>
57 #include <QtCore/qdebug.h>
58 
60 
62 
76 {
78 public:
81 
93 };
94 
96 {
97 }
98 
100 {
101 }
102 
104  const QString &name, const QString &group,
106  const QStringList &aliases, const QStringList &seeAlso,
108  const QScriptValue &globalObject,
111 {
113  d->name = name;
114  d->group = group;
115  d->shortDescription = shortDescription;
116  d->longDescription = longDescription;
117  d->aliases = aliases;
118  d->seeAlso = seeAlso;
119  d->argumentTypes = argumentTypes;
120  d->subCommands = subCommands;
121  d->globalObject = globalObject;
122  d->execFunction = execFunction;
123  d->responseFunction = responseFunction;
124 }
125 
127 {
128 }
129 
134 {
135 public:
138  const QStringList &arguments,
139  QScriptDebuggerConsole *console,
141  QScriptDebuggerCommandSchedulerInterface *commandScheduler);
143 
144  int scheduleCommand(
145  const QScriptDebuggerCommand &command,
146  QScriptDebuggerResponseHandlerInterface *responseHandler);
147 
148  void start();
149  void handleResponse(const QScriptDebuggerResponse &response,
150  int commandId);
151 
152 private:
155 };
156 
159 {
160 public:
161  QScriptDebuggerScriptedConsoleCommandJobPrivate() : command(0), commandCount(0) {}
163 
167 };
168 
171  const QStringList &arguments,
172  QScriptDebuggerConsole *console,
176  console, messageHandler, commandScheduler)
177 {
179  d->command = command;
180  d->arguments = arguments;
181 }
182 
184 {
185 }
186 
188  const QScriptDebuggerCommand &command,
190 {
192  ++d->commandCount;
193  return commandScheduler()->scheduleCommand(command, responseHandler);
194 }
195 
197 {
199  QScriptEngine *engine = d->command->globalObject.engine();
200  engine->setGlobalObject(d->command->globalObject);
201  QScriptValueList args;
202  for (int i = 0; i < d->arguments.size(); ++i)
203  args.append(QScriptValue(engine, d->arguments.at(i)));
206  Q_ASSERT(global != 0);
207  global->setScheduler(this);
208  global->setResponseHandler(this);
209  global->setMessageHandler(d->messageHandler);
210  global->setConsole(d->console);
211  d->commandCount = 0;
212  QScriptValue ret = d->command->execFunction.call(QScriptValue(), args);
213  global->setScheduler(0);
214  global->setResponseHandler(0);
215  global->setMessageHandler(0);
216  global->setConsole(0);
217  if (ret.isError()) {
218  qWarning("*** internal error: %s", qPrintable(ret.toString()));
219  }
220  if (d->commandCount == 0)
221  finish();
222 }
223 
225  const QScriptDebuggerResponse &response,
226  int commandId)
227 {
229  // ### generalize
230  QScriptEngine *engine = d->command->globalObject.engine();
231  engine->setGlobalObject(d->command->globalObject);
232  QScriptValueList args;
233  args.append(engine->toScriptValue(response));
234  args.append(QScriptValue(engine, commandId));
236  global = qobject_cast<QScriptDebuggerConsoleGlobalObject*>(d->command->globalObject.toQObject());
237  Q_ASSERT(global != 0);
238  global->setScheduler(this);
239  global->setResponseHandler(this);
240  global->setMessageHandler(d->messageHandler);
241  global->setConsole(d->console);
242  d->commandCount = 0;
243  QScriptValue ret = d->command->responseFunction.call(QScriptValue(), args);
244  global->setScheduler(0);
245  global->setResponseHandler(0);
246  global->setMessageHandler(0);
247  global->setConsole(0);
248  if (ret.isError()) {
249  qWarning("*** internal error: %s", qPrintable(ret.toString()));
250  }
251  if (d->commandCount == 0)
252  finish();
253 }
254 
259 {
261  return d->name;
262 }
263 
268 {
270  return d->group;
271 }
272 
277 {
279  return d->shortDescription;
280 }
281 
286 {
288  return d->longDescription;
289 }
290 
295 {
297  return d->aliases;
298 }
299 
304 {
306  return d->seeAlso;
307 }
308 
313 {
315  return d->argumentTypes;
316 }
317 
322 {
324  return d->subCommands;
325 }
326 
331  const QStringList &arguments,
332  QScriptDebuggerConsole *console,
335 {
338  d, arguments, console, messageHandler, commandScheduler);
339 }
340 
346  const QString &program, const QString &fileName,
348 {
349  // create a custom global object
351  QScriptValue global = engine->newQObject(cppGlobal,
354  {
356  while (it.hasNext()) {
357  it.next();
358  global.setProperty(it.scriptName(), it.value(), it.flags());
359  }
360  }
361  engine->setGlobalObject(global);
362 
363  cppGlobal->setMessageHandler(messageHandler);
364  QScriptValue ret = engine->evaluate(program, fileName);
365  cppGlobal->setMessageHandler(0);
366  if (engine->hasUncaughtException()) {
367  messageHandler->message(QtCriticalMsg, ret.toString(), fileName,
368  engine->uncaughtExceptionLineNumber());
369  return 0;
370  }
371 
372  QScriptValue name = global.property(QLatin1String("name"));
373  if (!name.isString()) {
374  messageHandler->message(QtCriticalMsg, QLatin1String("command definition lacks a name"), fileName);
375  return 0;
376  }
377  QString nameStr = name.toString();
378 
379  QScriptValue group = global.property(QLatin1String("group"));
380  if (!group.isString()) {
381  messageHandler->message(QtCriticalMsg, QString::fromLatin1("definition of command \"%0\" lacks a group name")
382  .arg(nameStr), fileName);
383  return 0;
384  }
385  QString groupStr = group.toString();
386 
387  QScriptValue shortDesc = global.property(QLatin1String("shortDescription"));
388  if (!shortDesc.isString()) {
389  messageHandler->message(QtCriticalMsg, QString::fromLatin1("definition of command \"%0\" lacks shortDescription")
390  .arg(nameStr), fileName);
391  return 0;
392  }
393  QString shortDescStr = shortDesc.toString();
394 
395  QScriptValue longDesc = global.property(QLatin1String("longDescription"));
396  if (!longDesc.isString()) {
397  messageHandler->message(QtCriticalMsg, QString::fromLatin1("definition of command \"%0\" lacks longDescription")
398  .arg(nameStr), fileName);
399  return 0;
400  }
401  QString longDescStr = longDesc.toString();
402 
405 
406  QStringList seeAlso;
407  qScriptValueToSequence(global.property(QLatin1String("seeAlso")), seeAlso);
408 
409  QStringList argTypes;
410  qScriptValueToSequence(global.property(QLatin1String("argumentTypes")), argTypes);
411 
412  QStringList subCommands;
413  qScriptValueToSequence(global.property(QLatin1String("subCommands")), subCommands);
414 
415  QScriptValue execFunction = global.property(QLatin1String("execute"));
416  if (!execFunction.isFunction()) {
417  messageHandler->message(QtCriticalMsg, QString::fromLatin1("definition of command \"%0\" lacks execute() function")
418  .arg(nameStr), fileName);
419  return 0;
420  }
421 
422  QScriptValue responseFunction = global.property(QLatin1String("handleResponse"));
423 
425  nameStr, groupStr,
426  shortDescStr, longDescStr,
427  aliases, seeAlso,
428  argTypes, subCommands,
429  global, execFunction, responseFunction);
430  return result;
431 }
432 
T qobject_cast(QObject *object)
Definition: qobject.h:375
double d
Definition: qnumeric_p.h:62
QScriptValue evaluate(const QString &program, const QString &fileName=QString(), int lineNumber=1)
Evaluates program, using lineNumber as the base line number, and returns the result of the evaluation...
QScriptValue property(const QString &name, const ResolveFlags &mode=ResolvePrototype) const
Returns the value of this QScriptValue&#39;s property with the given name, using the given mode to resolv...
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
const char * aliases[7]
#define it(className, varName)
The QScriptDebuggerResponse class represents a front-end&#39;s response to a QScriptDebuggerCommand.
bool isError() const
Returns true if this QScriptValue is an object of the Error class; otherwise returns false...
static QScriptDebuggerScriptedConsoleCommand * parse(const QString &program, const QString &fileName, QScriptEngine *engine, QScriptMessageHandlerInterface *messageHandler)
Parses a command defined by the given program.
QScriptValue toScriptValue(const T &value)
Creates a QScriptValue with the given value.
QString toString() const
Returns the string value of this QScriptValue, as defined in ECMA-262 section 9.8, "ToString".
QScriptValue globalObject() const
Returns this engine&#39;s Global Object.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
Definition: qglobal.h:2523
QScriptDebuggerScriptedConsoleCommandJob(QScriptDebuggerScriptedConsoleCommandPrivate *command, const QStringList &arguments, QScriptDebuggerConsole *console, QScriptMessageHandlerInterface *messageHandler, QScriptDebuggerCommandSchedulerInterface *commandScheduler)
QObject * toQObject() const
If this QScriptValue is a QObject, returns the QObject pointer that the QScriptValue represents; othe...
The QString class provides a Unicode character string.
Definition: qstring.h:83
QScriptDebuggerConsoleCommandJob * createJob(const QStringList &arguments, QScriptDebuggerConsole *console, QScriptMessageHandlerInterface *messageHandler, QScriptDebuggerCommandSchedulerInterface *commandScheduler)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_D(Class)
Definition: qglobal.h:2482
void setConsole(QScriptDebuggerConsole *console)
The QScriptDebuggerConsole class provides the core functionality of a debugger console.
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
The QScriptEngine class provides an environment for evaluating Qt Script code.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void setScheduler(QScriptDebuggerCommandSchedulerInterface *scheduler)
bool isString() const
Returns true if this QScriptValue is of the primitive type String; otherwise returns false...
const char * name
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
Q_CORE_EXPORT void qWarning(const char *,...)
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
int scheduleCommand(const QScriptDebuggerCommand &command, QScriptDebuggerResponseHandlerInterface *responseHandler)
void setGlobalObject(const QScriptValue &object)
Sets this engine&#39;s Global Object to be the given object.
void setResponseHandler(QScriptDebuggerResponseHandlerInterface *responseHandler)
int uncaughtExceptionLineNumber() const
Returns the line number where the last uncaught exception occurred.
#define Q_DECLARE_METATYPE(TYPE)
This macro makes the type Type known to QMetaType as long as it provides a public default constructor...
Definition: qmetatype.h:265
QScriptDebuggerScriptedConsoleCommand(const QString &name, const QString &group, const QString &shortDescription, const QString &longDescription, const QStringList &aliases, const QStringList &seeAlso, const QStringList &argumentTypes, const QStringList &subCommands, const QScriptValue &globalObject, const QScriptValue &execFunction, const QScriptValue &responseFunction)
QScriptValue newQObject(QObject *object, ValueOwnership ownership=QtOwnership, const QObjectWrapOptions &options=0)
Creates a QtScript object that wraps the given QObject object, using the given ownership.
The QScriptDebuggerCommand class represents a command issued to a QScriptDebuggerFrontend.
void setProperty(const QString &name, const QScriptValue &value, const PropertyFlags &flags=KeepExistingFlags)
Sets the value of this QScriptValue&#39;s property with the given name to the given value.
The QScriptDebuggerScriptedConsoleCommand class encapsulates a command defined in a script...
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
The QScriptValueIterator class provides a Java-style iterator for QScriptValue.
The QScriptDebuggerConsoleCommand class is the base class of console commands.
void setMessageHandler(QScriptMessageHandlerInterface *messageHandler)
void handleResponse(const QScriptDebuggerResponse &response, int commandId)
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
static void messageHandler(QtMsgType type, const char *msg)
Definition: qtestlog.cpp:121
void qScriptValueToSequence(const QScriptValue &value, Container &cont)
virtual int scheduleCommand(const QScriptDebuggerCommand &command, QScriptDebuggerResponseHandlerInterface *responseHandler)=0
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
QScriptDebuggerCommandSchedulerInterface * commandScheduler() const
bool hasUncaughtException() const
Returns true if the last script evaluation resulted in an uncaught exception; otherwise returns false...
virtual void message(QtMsgType type, const QString &text, const QString &fileName=QString(), int lineNumber=-1, int columnNumber=-1, const QVariant &data=QVariant())=0
#define qPrintable(string)
Definition: qglobal.h:1750
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
static QString fileName(const QString &fileUrl)