Qt 4.8
qscriptenginedebuggerfrontend.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 
46 #include "qscriptdebuggerevent_p.h"
49 
50 #include <QtCore/qcoreapplication.h>
51 #include <QtCore/qeventloop.h>
52 #include <QtCore/qlist.h>
53 #include <QtScript/qscriptvalue.h>
54 
56 
74 {
75 public:
77  : QEvent(QEvent::Type(QEvent::User+3)), m_id(id), m_command(command) {}
79  int id() const
80  { return m_id; }
82  { return m_command; }
83 private:
84  int m_id;
86 };
87 
89 {
90 public:
92  : QEvent(QEvent::Type(QEvent::User+4)), m_id(id), m_response(response) {}
94  int id() const
95  { return m_id; }
97  { return m_response; }
98 private:
99  int m_id;
101 };
102 
105 {
106 public:
109 
110  void processCommand(int id, const QScriptDebuggerCommand &command);
111  void resume();
112 
113 protected:
114  void event(const QScriptDebuggerEvent &event);
115 
116 private:
119 };
120 
123 {
125 public:
128 
129  bool event(QEvent *e);
130 
134 };
135 
138 {
140 public:
143 
144  void postCommandFinished(int id, const QScriptDebuggerResponse &response);
145  bool event(QEvent *e);
146 
148 };
149 
151 {
152  frontend = 0;
153 }
154 
156 {
157  eventLoopPool << eventLoopStack;
158  eventLoopStack.clear();
159  while (!eventLoopPool.isEmpty()) {
160  QEventLoop *eventLoop = eventLoopPool.takeFirst();
161  if (eventLoop->isRunning()) {
162  eventLoop->quit();
163  eventLoop->deleteLater();
164  } else {
165  delete eventLoop;
166  }
167  }
168 }
169 
174 {
176  if (e->type() == QEvent::User+3) {
178  QScriptDebuggerCommandExecutor *executor = q->commandExecutor();
179  QScriptDebuggerResponse response = executor->execute(q, ce->command());
180  frontend->postCommandFinished(ce->id(), response);
181  return true;
182  }
184 }
185 
198 {
200  d->frontend = frontend;
201 }
202 
204 {
205 }
206 
208 {
210  d->postEvent(new QScriptDebuggerCommandEvent(id, command));
211 }
212 
217 {
219  if (d->eventLoopPool.isEmpty())
220  d->eventLoopPool.append(new QEventLoop());
221  QEventLoop *eventLoop = d->eventLoopPool.takeFirst();
222  Q_ASSERT(!eventLoop->isRunning());
223  d->eventLoopStack.prepend(eventLoop);
224 
225  d->frontend->postEvent(new QScriptDebuggerEventEvent(event));
226 
227  // Run an event loop until resume() is called.
228  // This effectively stalls script execution and makes it possible
229  // for the debugger to inspect the execution state in the meantime.
230  eventLoop->exec();
231 
232  if (!d->eventLoopStack.isEmpty()) {
233  // the event loop was quit directly (i.e. not via resume())
234  d->eventLoopStack.takeFirst();
235  }
236  d->eventLoopPool.append(eventLoop);
237  doPendingEvaluate(/*postEvent=*/false);
238 }
239 
244 {
246  // quitting the event loops will cause event() to return (see above)
247  while (!d->eventLoopStack.isEmpty()) {
248  QEventLoop *eventLoop = d->eventLoopStack.takeFirst();
249  if (eventLoop->isRunning())
250  eventLoop->quit();
251  }
252 }
253 
255 {
256  backend = 0;
257 }
258 
260 {
261  delete backend;
262 }
263 
265  int id, const QScriptDebuggerResponse &response)
266 {
267  postEvent(new QScriptDebuggerCommandFinishedEvent(id, response));
268 }
269 
274 {
276  if (e->type() == QEvent::User+4) {
278  q->notifyCommandFinished(fe->id(), fe->response());
279  return true;
280  }
282 }
283 
286 {
287 }
288 
290 {
291  detach();
292 }
293 
298 {
300  if (d->backend)
301  d->backend->detach();
302  else
303  d->backend = new QScriptEngineDebuggerBackend(d);
304  d->backend->attachTo(engine);
305 }
306 
311 {
313  if (d->backend)
314  d->backend->detach();
315 }
316 
318 {
320  if (d->backend)
321  d->backend->traceFunction();
322  return QScriptValue();
323 }
324 
326 {
328  return d->backend;
329 }
330 
335 {
337  Q_ASSERT(d->backend != 0);
338  d->backend->processCommand(id, command);
339 }
340 
double d
Definition: qnumeric_p.h:62
QScriptEngineDebuggerFrontendPrivate * frontend
QScriptDebuggerCommandFinishedEvent(int id, const QScriptDebuggerResponse &response)
void detach()
Detaches this front-end from the current script engine.
void event(const QScriptDebuggerEvent &event)
Reimplemented Function
virtual QScriptDebuggerResponse execute(QScriptDebuggerBackend *backend, const QScriptDebuggerCommand &command)
Applies the given command to the given backend.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
EventRef event
The QScriptDebuggerFrontend class is the base class of debugger front-ends.
bool event(QEvent *e)
Reimplemented Function
The QScriptDebuggerResponse class represents a front-end&#39;s response to a QScriptDebuggerCommand.
QScriptDebuggerCommandEvent(int id, const QScriptDebuggerCommand &command)
const QScriptDebuggerCommand & command() const
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
Definition: qglobal.h:2523
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_D(Class)
Definition: qglobal.h:2482
void postCommandFinished(int id, const QScriptDebuggerResponse &response)
#define Q_Q(Class)
Definition: qglobal.h:2483
const QScriptDebuggerResponse & response() const
The QScriptEngine class provides an environment for evaluating Qt Script code.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QEventLoop class provides a means of entering and leaving an event loop.
Definition: qeventloop.h:55
void quit()
Tells the event loop to exit normally.
Definition: qeventloop.cpp:332
bool event(QEvent *e)
Reimplemented Function
The QScriptEngineDebuggerFrontend class provides an in-process debugger frontend. ...
void doPendingEvaluate(bool postEvent)
Executes the pending evaluate, if any.
void processCommand(int id, const QScriptDebuggerCommand &command)
The QScriptDebuggerCommand class represents a command issued to a QScriptDebuggerFrontend.
QScriptEngineDebuggerBackend(QScriptEngineDebuggerFrontendPrivate *frontend)
Creates a new QScriptEngineDebuggerBackend object for the given engine.
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
bool isRunning() const
Returns true if the event loop is running; otherwise returns false.
Definition: qeventloop.cpp:306
The QScriptDebuggerBackend class is the base class of debugger back-ends.
Type
This enum type defines the valid event types in Qt.
Definition: qcoreevent.h:62
The QScriptDebuggerCommandExecutor applies debugger commands to a back-end.
void attachTo(QScriptEngine *engine)
Attaches this front-end to the given engine.
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
void deleteLater()
Schedules this object for deletion.
Definition: qobject.cpp:2145
int exec(ProcessEventsFlags flags=AllEvents)
Enters the main event loop and waits until exit() is called.
Definition: qeventloop.cpp:181
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
QScriptDebuggerBackend * backend() const
void processCommand(int id, const QScriptDebuggerCommand &command)