1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtSCriptTools module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://www.qtsoftware.com/contact.
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qscriptdebuggercommandexecutor_p.h"
#include "qscriptdebuggerbackend_p.h"
#include "qscriptdebuggercommand_p.h"
#include "qscriptdebuggerresponse_p.h"
#include "qscriptdebuggervalue_p.h"
#include "qscriptdebuggervalueproperty_p.h"
#include "qscriptbreakpointdata_p.h"
#include "qscriptobjectsnapshot_p.h"
#include "qscriptdebuggerobjectsnapshotdelta_p.h"
#include <QtCore/qstringlist.h>
#include <QtScript/qscriptengine.h>
#include <QtScript/qscriptcontextinfo.h>
#include <QtScript/qscriptvalueiterator.h>
#include <QtCore/qdebug.h>
Q_DECLARE_METATYPE(QScriptScriptsDelta)
Q_DECLARE_METATYPE(QScriptDebuggerValueProperty)
Q_DECLARE_METATYPE(QScriptDebuggerValuePropertyList)
Q_DECLARE_METATYPE(QScriptDebuggerObjectSnapshotDelta)
QT_BEGIN_NAMESPACE
/*!
\since 4.5
\class QScriptDebuggerCommandExecutor
\internal
\brief The QScriptDebuggerCommandExecutor applies debugger commands to a back-end.
The execute() function takes a command (typically produced by a
QScriptDebuggerFrontend) and applies it to a QScriptDebuggerBackend.
\sa QScriptDebuggerCommmand
*/
class QScriptDebuggerCommandExecutorPrivate
{
public:
QScriptDebuggerCommandExecutorPrivate();
~QScriptDebuggerCommandExecutorPrivate();
};
QScriptDebuggerCommandExecutorPrivate::QScriptDebuggerCommandExecutorPrivate()
{
}
QScriptDebuggerCommandExecutorPrivate::~QScriptDebuggerCommandExecutorPrivate()
{
}
QScriptDebuggerCommandExecutor::QScriptDebuggerCommandExecutor()
: d_ptr(new QScriptDebuggerCommandExecutorPrivate())
{
}
QScriptDebuggerCommandExecutor::~QScriptDebuggerCommandExecutor()
{
delete d_ptr;
}
static bool isPrefixOf(const QString &prefix, const QString &what)
{
return ((what.length() > prefix.length())
&& what.startsWith(prefix));
}
/*!
Applies the given \a command to the given \a backend.
*/
QScriptDebuggerResponse QScriptDebuggerCommandExecutor::execute(
QScriptDebuggerBackend *backend,
const QScriptDebuggerCommand &command)
{
QScriptDebuggerResponse response;
switch (command.type()) {
case QScriptDebuggerCommand::None:
break;
case QScriptDebuggerCommand::Interrupt:
backend->interruptEvaluation();
break;
case QScriptDebuggerCommand::Continue:
if (backend->engine()->isEvaluating()) {
backend->continueEvalution();
response.setAsync(true);
}
break;
case QScriptDebuggerCommand::StepInto: {
QVariant attr = command.attribute(QScriptDebuggerCommand::StepCount);
int count = attr.isValid() ? attr.toInt() : 1;
backend->stepInto(count);
response.setAsync(true);
} break;
case QScriptDebuggerCommand::StepOver: {
QVariant attr = command.attribute(QScriptDebuggerCommand::StepCount);
int count = attr.isValid() ? attr.toInt() : 1;
backend->stepOver(count);
response.setAsync(true);
} break;
case QScriptDebuggerCommand::StepOut:
backend->stepOut();
response.setAsync(true);
break;
case QScriptDebuggerCommand::RunToLocation:
backend->runToLocation(command.fileName(), command.lineNumber());
response.setAsync(true);
break;
case QScriptDebuggerCommand::RunToLocationByID:
backend->runToLocation(command.scriptId(), command.lineNumber());
response.setAsync(true);
break;
case QScriptDebuggerCommand::ForceReturn: {
int contextIndex = command.contextIndex();
QScriptDebuggerValue value = command.scriptValue();
QScriptEngine *engine = backend->engine();
QScriptValue realValue = value.toScriptValue(engine);
backend->returnToCaller(contextIndex, realValue);
response.setAsync(true);
} break;
case QScriptDebuggerCommand::Resume:
backend->resume();
response.setAsync(true);
break;
case QScriptDebuggerCommand::SetBreakpoint: {
QScriptBreakpointData data = command.breakpointData();
if (!data.isValid())
data = QScriptBreakpointData(command.fileName(), command.lineNumber());
int id = backend->setBreakpoint(data);
response.setResult(id);
} break;
case QScriptDebuggerCommand::DeleteBreakpoint: {
int id = command.breakpointId();
if (!backend->deleteBreakpoint(id))
response.setError(QScriptDebuggerResponse::InvalidBreakpointID);
} break;
case QScriptDebuggerCommand::DeleteAllBreakpoints:
backend->deleteAllBreakpoints();
break;
case QScriptDebuggerCommand::GetBreakpoints: {
QScriptBreakpointMap bps = backend->breakpoints();
if (!bps.isEmpty())
response.setResult(bps);
} break;
case QScriptDebuggerCommand::GetBreakpointData: {
int id = command.breakpointId();
QScriptBreakpointData data = backend->breakpointData(id);
if (data.isValid())
response.setResult(data);
else
response.setError(QScriptDebuggerResponse::InvalidBreakpointID);
} break;
case QScriptDebuggerCommand::SetBreakpointData: {
int id = command.breakpointId();
QScriptBreakpointData data = command.breakpointData();
if (!backend->setBreakpointData(id, data))
response.setError(QScriptDebuggerResponse::InvalidBreakpointID);
} break;
case QScriptDebuggerCommand::GetScripts: {
QScriptScriptMap scripts = backend->scripts();
if (!scripts.isEmpty())
response.setResult(scripts);
} break;
case QScriptDebuggerCommand::GetScriptData: {
qint64 id = command.scriptId();
QScriptScriptData data = backend->scriptData(id);
if (data.isValid())
response.setResult(data);
else
response.setError(QScriptDebuggerResponse::InvalidScriptID);
} break;
case QScriptDebuggerCommand::ScriptsCheckpoint:
backend->scriptsCheckpoint();
response.setResult(qVariantFromValue(backend->scriptsDelta()));
break;
case QScriptDebuggerCommand::GetScriptsDelta:
response.setResult(qVariantFromValue(backend->scriptsDelta()));
break;
case QScriptDebuggerCommand::ResolveScript:
response.setResult(backend->resolveScript(command.fileName()));
break;
case QScriptDebuggerCommand::GetBacktrace:
response.setResult(backend->backtrace());
break;
case QScriptDebuggerCommand::GetContextCount:
response.setResult(backend->contextCount());
break;
case QScriptDebuggerCommand::GetContextState: {
QScriptContext *ctx = backend->context(command.contextIndex());
if (ctx)
response.setResult(static_cast<int>(ctx->state()));
else
response.setError(QScriptDebuggerResponse::InvalidContextIndex);
} break;
case QScriptDebuggerCommand::GetContextID: {
int idx = command.contextIndex();
if ((idx >= 0) && (idx < backend->contextCount()))
response.setResult(backend->contextIds()[idx]);
else
response.setError(QScriptDebuggerResponse::InvalidContextIndex);
} break;
case QScriptDebuggerCommand::GetContextInfo: {
QScriptContext *ctx = backend->context(command.contextIndex());
if (ctx)
response.setResult(QScriptContextInfo(ctx));
else
response.setError(QScriptDebuggerResponse::InvalidContextIndex);
} break;
case QScriptDebuggerCommand::GetThisObject: {
QScriptContext *ctx = backend->context(command.contextIndex());
if (ctx)
response.setResult(ctx->thisObject());
else
response.setError(QScriptDebuggerResponse::InvalidContextIndex);
} break;
case QScriptDebuggerCommand::GetActivationObject: {
QScriptContext *ctx = backend->context(command.contextIndex());
if (ctx)
response.setResult(ctx->activationObject());
else
response.setError(QScriptDebuggerResponse::InvalidContextIndex);
} break;
case QScriptDebuggerCommand::GetScopeChain: {
QScriptContext *ctx = backend->context(command.contextIndex());
if (ctx) {
QScriptDebuggerValueList dest;
QScriptValueList src = ctx->scopeChain();
for (int i = 0; i < src.size(); ++i)
dest.append(src.at(i));
response.setResult(dest);
} else {
response.setError(QScriptDebuggerResponse::InvalidContextIndex);
}
} break;
case QScriptDebuggerCommand::ContextsCheckpoint: {
response.setResult(qVariantFromValue(backend->contextsCheckpoint()));
} break;
case QScriptDebuggerCommand::GetPropertyExpressionValue: {
QScriptContext *ctx = backend->context(command.contextIndex());
int lineNumber = command.lineNumber();
QVariant attr = command.attribute(QScriptDebuggerCommand::UserAttribute);
QStringList path = attr.toStringList();
if (!ctx || path.isEmpty())
break;
QScriptContextInfo ctxInfo(ctx);
if (ctx->callee().isValid()
&& ((lineNumber < ctxInfo.functionStartLineNumber())
|| (lineNumber > ctxInfo.functionEndLineNumber()))) {
break;
}
QScriptValueList objects;
int pathIndex = 0;
if (path.at(0) == QLatin1String("this")) {
objects.append(ctx->thisObject());
++pathIndex;
} else {
objects << ctx->scopeChain();
}
for (int i = 0; i < objects.size(); ++i) {
QScriptValue val = objects.at(i);
for (int j = pathIndex; val.isValid() && (j < path.size()); ++j) {
val = val.property(path.at(j));
}
if (val.isValid()) {
bool hadException = (ctx->state() == QScriptContext::ExceptionState);
QString str = val.toString();
if (!hadException && backend->engine()->hasUncaughtException())
backend->engine()->clearExceptions();
response.setResult(str);
break;
}
}
} break;
case QScriptDebuggerCommand::GetCompletions: {
QScriptContext *ctx = backend->context(command.contextIndex());
QVariant attr = command.attribute(QScriptDebuggerCommand::UserAttribute);
QStringList path = attr.toStringList();
if (!ctx || path.isEmpty())
break;
QScriptValueList objects;
QString prefix = path.last();
QSet<QString> matches;
if (path.size() > 1) {
const QString &topLevelIdent = path.at(0);
QScriptValue obj;
if (topLevelIdent == QLatin1String("this")) {
obj = ctx->thisObject();
} else {
QScriptValueList scopeChain;
scopeChain = ctx->scopeChain();
for (int i = 0; i < scopeChain.size(); ++i) {
QScriptValue oo = scopeChain.at(i).property(topLevelIdent);
if (oo.isObject()) {
obj = oo;
break;
}
}
}
for (int i = 1; obj.isObject() && (i < path.size()-1); ++i)
obj = obj.property(path.at(i));
if (obj.isValid())
objects.append(obj);
} else {
objects << ctx->scopeChain();
QStringList keywords;
keywords.append(QString::fromLatin1("this"));
keywords.append(QString::fromLatin1("true"));
keywords.append(QString::fromLatin1("false"));
keywords.append(QString::fromLatin1("null"));
for (int i = 0; i < keywords.size(); ++i) {
const QString &kwd = keywords.at(i);
if (isPrefixOf(prefix, kwd))
matches.insert(kwd);
}
}
for (int i = 0; i < objects.size(); ++i) {
QScriptValue obj = objects.at(i);
while (obj.isObject()) {
QScriptValueIterator it(obj);
while (it.hasNext()) {
it.next();
QString propertyName = it.name();
if (isPrefixOf(prefix, propertyName))
matches.insert(propertyName);
}
obj = obj.prototype();
}
}
QStringList matchesList = matches.toList();
qStableSort(matchesList);
response.setResult(matchesList);
} break;
case QScriptDebuggerCommand::NewScriptObjectSnapshot: {
int id = backend->newScriptObjectSnapshot();
response.setResult(id);
} break;
case QScriptDebuggerCommand::ScriptObjectSnapshotCapture: {
int id = command.snapshotId();
QScriptObjectSnapshot *snap = backend->scriptObjectSnapshot(id);
Q_ASSERT(snap != 0);
QScriptDebuggerValue object = command.scriptValue();
Q_ASSERT(object.type() == QScriptDebuggerValue::ObjectValue);
QScriptEngine *engine = backend->engine();
QScriptValue realObject = object.toScriptValue(engine);
Q_ASSERT(realObject.isObject());
QScriptObjectSnapshot::Delta delta = snap->capture(realObject);
QScriptDebuggerObjectSnapshotDelta result;
result.removedProperties = delta.removedProperties;
bool didIgnoreExceptions = backend->ignoreExceptions();
backend->setIgnoreExceptions(true);
for (int i = 0; i < delta.changedProperties.size(); ++i) {
const QScriptValueProperty &src = delta.changedProperties.at(i);
bool hadException = engine->hasUncaughtException();
QString str = src.value().toString();
if (!hadException && engine->hasUncaughtException())
engine->clearExceptions();
QScriptDebuggerValueProperty dest(src.name(), src.value(), str, src.flags());
result.changedProperties.append(dest);
}
for (int j = 0; j < delta.addedProperties.size(); ++j) {
const QScriptValueProperty &src = delta.addedProperties.at(j);
bool hadException = engine->hasUncaughtException();
QString str = src.value().toString();
if (!hadException && engine->hasUncaughtException())
engine->clearExceptions();
QScriptDebuggerValueProperty dest(src.name(), src.value(), str, src.flags());
result.addedProperties.append(dest);
}
backend->setIgnoreExceptions(didIgnoreExceptions);
response.setResult(qVariantFromValue(result));
} break;
case QScriptDebuggerCommand::DeleteScriptObjectSnapshot: {
int id = command.snapshotId();
backend->deleteScriptObjectSnapshot(id);
} break;
case QScriptDebuggerCommand::NewScriptValueIterator: {
QScriptDebuggerValue object = command.scriptValue();
Q_ASSERT(object.type() == QScriptDebuggerValue::ObjectValue);
QScriptEngine *engine = backend->engine();
QScriptValue realObject = object.toScriptValue(engine);
Q_ASSERT(realObject.isObject());
int id = backend->newScriptValueIterator(realObject);
response.setResult(id);
} break;
case QScriptDebuggerCommand::GetPropertiesByIterator: {
int id = command.iteratorId();
int count = 1000;
QScriptValueIterator *it = backend->scriptValueIterator(id);
Q_ASSERT(it != 0);
QScriptDebuggerValuePropertyList props;
for (int i = 0; (i < count) && it->hasNext(); ++i) {
it->next();
QString name = it->name();
QScriptValue value = it->value();
QString valueAsString = value.toString();
QScriptValue::PropertyFlags flags = it->flags();
QScriptDebuggerValueProperty prp(name, value, valueAsString, flags);
props.append(prp);
}
response.setResult(props);
} break;
case QScriptDebuggerCommand::DeleteScriptValueIterator: {
int id = command.iteratorId();
backend->deleteScriptValueIterator(id);
} break;
case QScriptDebuggerCommand::Evaluate: {
int contextIndex = command.contextIndex();
QString program = command.program();
QString fileName = command.fileName();
int lineNumber = command.lineNumber();
backend->evaluate(contextIndex, program, fileName, lineNumber);
response.setAsync(true);
} break;
case QScriptDebuggerCommand::ScriptValueToString: {
QScriptDebuggerValue value = command.scriptValue();
QScriptEngine *engine = backend->engine();
QScriptValue realValue = value.toScriptValue(engine);
response.setResult(realValue.toString());
} break;
case QScriptDebuggerCommand::SetScriptValueProperty: {
QScriptDebuggerValue object = command.scriptValue();
QScriptEngine *engine = backend->engine();
QScriptValue realObject = object.toScriptValue(engine);
QScriptDebuggerValue value = command.subordinateScriptValue();
QScriptValue realValue = value.toScriptValue(engine);
QString name = command.name();
realObject.setProperty(name, realValue);
} break;
case QScriptDebuggerCommand::ClearExceptions:
backend->engine()->clearExceptions();
break;
case QScriptDebuggerCommand::UserCommand:
case QScriptDebuggerCommand::MaxUserCommand:
break;
}
return response;
}
QT_END_NAMESPACE
|