57 #include <QtScript/qscriptvalueiterator.h> |
55 #include <QtScript/qscriptvalueiterator.h> |
58 #include <QtScript/qscriptcontextinfo.h> |
56 #include <QtScript/qscriptcontextinfo.h> |
59 #include <QtCore/qdebug.h> |
57 #include <QtCore/qdebug.h> |
60 |
58 |
61 Q_DECLARE_METATYPE(QScriptDebuggerResponse) |
59 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 |
60 |
75 QT_BEGIN_NAMESPACE |
61 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 |
62 |
258 /*! |
63 /*! |
259 \since 4.5 |
64 \since 4.5 |
260 \class QScriptDebuggerScriptedConsoleCommand |
65 \class QScriptDebuggerScriptedConsoleCommand |
261 \internal |
66 \internal |
277 QString longDescription; |
82 QString longDescription; |
278 QStringList aliases; |
83 QStringList aliases; |
279 QStringList seeAlso; |
84 QStringList seeAlso; |
280 QStringList argumentTypes; |
85 QStringList argumentTypes; |
281 QStringList subCommands; |
86 QStringList subCommands; |
282 QScriptEngine *engine; |
87 QScriptValue globalObject; |
283 QScriptValue execFunction; |
88 QScriptValue execFunction; |
284 QScriptValue responseFunction; |
89 QScriptValue responseFunction; |
285 }; |
90 }; |
286 |
91 |
287 QScriptDebuggerScriptedConsoleCommandPrivate::QScriptDebuggerScriptedConsoleCommandPrivate() |
92 QScriptDebuggerScriptedConsoleCommandPrivate::QScriptDebuggerScriptedConsoleCommandPrivate() |
288 { |
93 { |
289 engine = 0; |
|
290 } |
94 } |
291 |
95 |
292 QScriptDebuggerScriptedConsoleCommandPrivate::~QScriptDebuggerScriptedConsoleCommandPrivate() |
96 QScriptDebuggerScriptedConsoleCommandPrivate::~QScriptDebuggerScriptedConsoleCommandPrivate() |
293 { |
97 { |
294 delete engine; |
|
295 } |
98 } |
296 |
99 |
297 QScriptDebuggerScriptedConsoleCommand::QScriptDebuggerScriptedConsoleCommand( |
100 QScriptDebuggerScriptedConsoleCommand::QScriptDebuggerScriptedConsoleCommand( |
298 const QString &name, const QString &group, |
101 const QString &name, const QString &group, |
299 const QString &shortDescription, const QString &longDescription, |
102 const QString &shortDescription, const QString &longDescription, |
300 const QStringList &aliases, const QStringList &seeAlso, |
103 const QStringList &aliases, const QStringList &seeAlso, |
301 const QStringList &argumentTypes, const QStringList &subCommands, |
104 const QStringList &argumentTypes, const QStringList &subCommands, |
|
105 const QScriptValue &globalObject, |
302 const QScriptValue &execFunction, const QScriptValue &responseFunction) |
106 const QScriptValue &execFunction, const QScriptValue &responseFunction) |
303 : QScriptDebuggerConsoleCommand(*new QScriptDebuggerScriptedConsoleCommandPrivate) |
107 : QScriptDebuggerConsoleCommand(*new QScriptDebuggerScriptedConsoleCommandPrivate) |
304 { |
108 { |
305 Q_D(QScriptDebuggerScriptedConsoleCommand); |
109 Q_D(QScriptDebuggerScriptedConsoleCommand); |
306 d->name = name; |
110 d->name = name; |
309 d->longDescription = longDescription; |
113 d->longDescription = longDescription; |
310 d->aliases = aliases; |
114 d->aliases = aliases; |
311 d->seeAlso = seeAlso; |
115 d->seeAlso = seeAlso; |
312 d->argumentTypes = argumentTypes; |
116 d->argumentTypes = argumentTypes; |
313 d->subCommands = subCommands; |
117 d->subCommands = subCommands; |
|
118 d->globalObject = globalObject; |
314 d->execFunction = execFunction; |
119 d->execFunction = execFunction; |
315 d->responseFunction = responseFunction; |
120 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 } |
121 } |
334 |
122 |
335 QScriptDebuggerScriptedConsoleCommand::~QScriptDebuggerScriptedConsoleCommand() |
123 QScriptDebuggerScriptedConsoleCommand::~QScriptDebuggerScriptedConsoleCommand() |
336 { |
124 { |
337 } |
125 } |
572 QScriptValue ret = engine->evaluate(program, fileName); |
361 QScriptValue ret = engine->evaluate(program, fileName); |
573 cppGlobal->setMessageHandler(0); |
362 cppGlobal->setMessageHandler(0); |
574 if (engine->hasUncaughtException()) { |
363 if (engine->hasUncaughtException()) { |
575 messageHandler->message(QtCriticalMsg, ret.toString(), fileName, |
364 messageHandler->message(QtCriticalMsg, ret.toString(), fileName, |
576 engine->uncaughtExceptionLineNumber()); |
365 engine->uncaughtExceptionLineNumber()); |
577 delete engine; |
|
578 return 0; |
366 return 0; |
579 } |
367 } |
580 |
368 |
581 QScriptValue name = global.property(QLatin1String("name")); |
369 QScriptValue name = global.property(QLatin1String("name")); |
582 if (!name.isString()) { |
370 if (!name.isString()) { |
583 messageHandler->message(QtCriticalMsg, QLatin1String("command definition lacks a name"), fileName); |
371 messageHandler->message(QtCriticalMsg, QLatin1String("command definition lacks a name"), fileName); |
584 delete engine; |
|
585 return 0; |
372 return 0; |
586 } |
373 } |
587 QString nameStr = name.toString(); |
374 QString nameStr = name.toString(); |
588 |
375 |
589 QScriptValue group = global.property(QLatin1String("group")); |
376 QScriptValue group = global.property(QLatin1String("group")); |
590 if (!group.isString()) { |
377 if (!group.isString()) { |
591 messageHandler->message(QtCriticalMsg, QString::fromLatin1("definition of command \"%0\" lacks a group name") |
378 messageHandler->message(QtCriticalMsg, QString::fromLatin1("definition of command \"%0\" lacks a group name") |
592 .arg(nameStr), fileName); |
379 .arg(nameStr), fileName); |
593 delete engine; |
|
594 return 0; |
380 return 0; |
595 } |
381 } |
596 QString groupStr = group.toString(); |
382 QString groupStr = group.toString(); |
597 |
383 |
598 QScriptValue shortDesc = global.property(QLatin1String("shortDescription")); |
384 QScriptValue shortDesc = global.property(QLatin1String("shortDescription")); |
599 if (!shortDesc.isString()) { |
385 if (!shortDesc.isString()) { |
600 messageHandler->message(QtCriticalMsg, QString::fromLatin1("definition of command \"%0\" lacks shortDescription") |
386 messageHandler->message(QtCriticalMsg, QString::fromLatin1("definition of command \"%0\" lacks shortDescription") |
601 .arg(nameStr), fileName); |
387 .arg(nameStr), fileName); |
602 delete engine; |
|
603 return 0; |
388 return 0; |
604 } |
389 } |
605 QString shortDescStr = shortDesc.toString(); |
390 QString shortDescStr = shortDesc.toString(); |
606 |
391 |
607 QScriptValue longDesc = global.property(QLatin1String("longDescription")); |
392 QScriptValue longDesc = global.property(QLatin1String("longDescription")); |
608 if (!longDesc.isString()) { |
393 if (!longDesc.isString()) { |
609 messageHandler->message(QtCriticalMsg, QString::fromLatin1("definition of command \"%0\" lacks longDescription") |
394 messageHandler->message(QtCriticalMsg, QString::fromLatin1("definition of command \"%0\" lacks longDescription") |
610 .arg(nameStr), fileName); |
395 .arg(nameStr), fileName); |
611 delete engine; |
|
612 return 0; |
396 return 0; |
613 } |
397 } |
614 QString longDescStr = longDesc.toString(); |
398 QString longDescStr = longDesc.toString(); |
615 |
399 |
616 QStringList aliases; |
400 QStringList aliases; |