Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 6 files). Show all
Unified · Split
| @@ -17,12 +17,18 @@ · 16 unmodified lines | ||
| 17 | 17 | #include <QtCore/QFileInfo> |
| 18 | 18 | #include <QtCore/QTimer> |
| 19 | 19 | #include <QtCore/QThread> |
| 20 | +#include <QtCore/QRegularExpression> | |
| 21 | + | |
| 22 | +#include <QtWidgets/QMainWindow> | |
| 23 | +#include <QtWidgets/QApplication> | |
| 24 | + | |
| 20 | 25 | #include <QtNetwork/QNetworkRequest> |
| 21 | 26 | #include <QtNetwork/QNetworkReply> |
| 22 | −#include <QtScript/QScriptEngine> | |
| 27 | + | |
| 23 | 28 | #include <QtScript/QScriptValue> |
| 24 | 29 | #include <QtScript/QScriptValueIterator> |
| 25 | −#include <QtCore/QStringList> | |
| 30 | + | |
| 31 | +#include <QtScriptTools/QScriptEngineDebugger> | |
| 26 | 32 | |
| 27 | 33 | #include <AudioConstants.h> |
| 28 | 34 | #include <AudioEffectOptions.h> |
| @@ -34,6 +40,7 @@ · 5 unmodified lines | ||
| 34 | 40 | #include <NodeList.h> |
| 35 | 41 | #include <udt/PacketHeaders.h> |
| 36 | 42 | #include <UUID.h> |
| 43 | +#include <ui/Menu.h> | |
| 37 | 44 | |
| 38 | 45 | #include <controllers/ScriptingInterface.h> |
| 39 | 46 | #include <AnimationObject.h> |
| @@ -169,6 +176,61 @@ void ScriptEngine::disconnectNonEssentialSignals() { · 129 unmodified lines | ||
| 169 | 176 | } |
| 170 | 177 | } |
| 171 | 178 | |
| 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 | + | |
| 172 | 234 | void ScriptEngine::runInThread() { |
| 173 | 235 | Q_ASSERT_X(!_isThreaded, "ScriptEngine::runInThread()", "runInThread should not be called more than once"); |
| 174 | 236 | |
| @@ -260,6 +322,10 @@ void ScriptEngine::loadURL(const QUrl& scriptURL, bool reload) { · 85 unmodified lines | ||
| 260 | 322 | // FIXME - switch this to the new model of ScriptCache callbacks |
| 261 | 323 | void ScriptEngine::scriptContentsAvailable(const QUrl& url, const QString& scriptContents) { |
| 262 | 324 | _scriptContents = scriptContents; |
| 325 | + static const QString DEBUG_FLAG("#debug"); | |
| 326 | + if (QRegularExpression(DEBUG_FLAG).match(scriptContents).hasMatch()) { | |
| 327 | + _debuggable = true; | |
| 328 | + } | |
| 263 | 329 | if (_wantSignals) { |
| 264 | 330 | emit scriptLoaded(url.toString()); |
| 265 | 331 | } |
| @@ -723,7 +789,7 @@ void ScriptEngine::run() { · 457 unmodified lines | ||
| 723 | 789 | auto nodeList = DependencyManager::get<NodeList>(); |
| 724 | 790 | auto entityScriptingInterface = DependencyManager::get<EntityScriptingInterface>(); |
| 725 | 791 | |
| 726 | − qint64 lastUpdate = usecTimestampNow(); | |
| 792 | + _lastUpdate = usecTimestampNow(); | |
| 727 | 793 | |
| 728 | 794 | // TODO: Integrate this with signals/slots instead of reimplementing throttling for ScriptEngine |
| 729 | 795 | while (!_isFinished) { |
| @@ -771,15 +837,15 @@ void ScriptEngine::run() { · 41 unmodified lines | ||
| 771 | 837 | qint64 now = usecTimestampNow(); |
| 772 | 838 | |
| 773 | 839 | // 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; | |
| 776 | 842 | if (!_isFinished) { |
| 777 | 843 | if (_wantSignals) { |
| 778 | 844 | emit update(deltaTime); |
| 779 | 845 | } |
| 780 | 846 | } |
| 781 | 847 | } |
| 782 | − lastUpdate = now; | |
| 848 | + _lastUpdate = now; | |
| 783 | 849 | |
| 784 | 850 | // Debug and clear exceptions |
| 785 | 851 | hadUncaughtExceptions(*this, _fileNameString); |