History

Enable script debugging

Brad Davis committed 10 years ago

Commit 40316491 parent 0de721e6 files · +111 −17Browse files

Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 6 files). Show all

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+72−6
@@ -17,12 +17,18 @@ · 16 unmodified lines
1717 #include <QtCore/QFileInfo>
1818 #include <QtCore/QTimer>
1919 #include <QtCore/QThread>
20+#include <QtCore/QRegularExpression>
21+
22+#include <QtWidgets/QMainWindow>
23+#include <QtWidgets/QApplication>
24+
2025 #include <QtNetwork/QNetworkRequest>
2126 #include <QtNetwork/QNetworkReply>
22−#include <QtScript/QScriptEngine>
27+
2328 #include <QtScript/QScriptValue>
2429 #include <QtScript/QScriptValueIterator>
25−#include <QtCore/QStringList>
30+
31+#include <QtScriptTools/QScriptEngineDebugger>
2632
2733 #include <AudioConstants.h>
2834 #include <AudioEffectOptions.h>
@@ -34,6 +40,7 @@ · 5 unmodified lines
3440 #include <NodeList.h>
3541 #include <udt/PacketHeaders.h>
3642 #include <UUID.h>
43+#include <ui/Menu.h>
3744
3845 #include <controllers/ScriptingInterface.h>
3946 #include <AnimationObject.h>
@@ -169,6 +176,61 @@ void ScriptEngine::disconnectNonEssentialSignals() { · 129 unmodified lines
169176 }
170177 }
171178
179+void ScriptEngine::runDebuggable() {
180+ init();
181+ _isRunning = true;
182+ _debuggable = true;
183+ _debugger = new QScriptEngineDebugger(this);
184+ _debugger->attachTo(this);
185+ static QMenu* scriptDebugMenu = qobject_cast<QMainWindow*>(qApp->activeWindow())->menuBar()->addMenu("Script Debug");
186+ scriptDebugMenu->addMenu(_fileNameString)->addMenu(_debugger->createStandardMenu(qApp->activeWindow()));
187+ _debugger->action(QScriptEngineDebugger::InterruptAction)->trigger();
188+ QScriptValue result = evaluate(_scriptContents, _fileNameString);
189+
190+ _lastUpdate = usecTimestampNow();
191+ QTimer* timer = new QTimer(this);
192+ connect(this, &ScriptEngine::finished, [this, timer] {
193+ disconnect(timer);
194+ });
195+
196+ connect(timer, &QTimer::timeout, [this, timer] {
197+ if (_isFinished) {
198+ if (!_isRunning) {
199+ return;
200+ }
201+ stopAllTimers(); // make sure all our timers are stopped if the script is ending
202+ if (_wantSignals) {
203+ emit scriptEnding();
204+ emit finished(_fileNameString, this);
205+ }
206+ _isRunning = false;
207+ if (_wantSignals) {
208+ emit runningStateChanged();
209+ emit doneRunning();
210+ }
211+ timer->deleteLater();
212+ return;
213+ }
214+
215+ qint64 now = usecTimestampNow();
216+ // we check for 'now' in the past in case people set their clock back
217+ if (_lastUpdate < now) {
218+ float deltaTime = (float)(now - _lastUpdate) / (float)USECS_PER_SECOND;
219+ if (!_isFinished) {
220+ if (_wantSignals) {
221+ emit update(deltaTime);
222+ }
223+ }
224+ }
225+ _lastUpdate = now;
226+ // Debug and clear exceptions
227+ hadUncaughtExceptions(*this, _fileNameString);
228+ });
229+
230+ timer->start(10);
231+}
232+
233+
172234 void ScriptEngine::runInThread() {
173235 Q_ASSERT_X(!_isThreaded, "ScriptEngine::runInThread()", "runInThread should not be called more than once");
174236
@@ -260,6 +322,10 @@ void ScriptEngine::loadURL(const QUrl& scriptURL, bool reload) { · 85 unmodified lines
260322 // FIXME - switch this to the new model of ScriptCache callbacks
261323 void ScriptEngine::scriptContentsAvailable(const QUrl& url, const QString& scriptContents) {
262324 _scriptContents = scriptContents;
325+ static const QString DEBUG_FLAG("#debug");
326+ if (QRegularExpression(DEBUG_FLAG).match(scriptContents).hasMatch()) {
327+ _debuggable = true;
328+ }
263329 if (_wantSignals) {
264330 emit scriptLoaded(url.toString());
265331 }
@@ -723,7 +789,7 @@ void ScriptEngine::run() { · 457 unmodified lines
723789 auto nodeList = DependencyManager::get<NodeList>();
724790 auto entityScriptingInterface = DependencyManager::get<EntityScriptingInterface>();
725791
726− qint64 lastUpdate = usecTimestampNow();
792+ _lastUpdate = usecTimestampNow();
727793
728794 // TODO: Integrate this with signals/slots instead of reimplementing throttling for ScriptEngine
729795 while (!_isFinished) {
@@ -771,15 +837,15 @@ void ScriptEngine::run() { · 41 unmodified lines
771837 qint64 now = usecTimestampNow();
772838
773839 // we check for 'now' in the past in case people set their clock back
774− if (lastUpdate < now) {
775− float deltaTime = (float) (now - lastUpdate) / (float) USECS_PER_SECOND;
840+ if (_lastUpdate < now) {
841+ float deltaTime = (float) (now - _lastUpdate) / (float) USECS_PER_SECOND;
776842 if (!_isFinished) {
777843 if (_wantSignals) {
778844 emit update(deltaTime);
779845 }
780846 }
781847 }
782− lastUpdate = now;
848+ _lastUpdate = now;
783849
784850 // Debug and clear exceptions
785851 hadUncaughtExceptions(*this, _fileNameString);