History

more hacking on script shutdown behavior

ZappoMan committed 11 years ago

Commit fd93b991 parent 6be8f4c4 files · +231 −57Browse files

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

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+214−50
@@ -94,33 +94,32 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam · 93 unmodified lines
94 _isUserLoaded(false),94 _isUserLoaded(false),
95 _arrayBufferClass(new ArrayBufferClass(this))95 _arrayBufferClass(new ArrayBufferClass(this))
96{96{
97 qDebug() << "ScriptEngine::ScriptEngine() " << getFilename() << "[" << ((void*)this) << "]" << " -------- BEGIN -----------";
98 _allScriptsMutex.lock();97 _allScriptsMutex.lock();
99 _allKnownScriptEngines.insert(this);98 _allKnownScriptEngines.insert(this);
100 _allScriptsMutex.unlock();99 _allScriptsMutex.unlock();
101 qDebug() << "ScriptEngine::ScriptEngine() " << getFilename() << " -------- DONE -----------";
102}100}
103 101
104ScriptEngine::~ScriptEngine() {102ScriptEngine::~ScriptEngine() {
105 qDebug() << "ScriptEngine::~ScriptEngine() " << getFilename() << "[" << ((void*)this) << "]" << " -------- BEGIN -----------";103 qDebug() << "ScriptEngine::~ScriptEngine() ------- BEGIN ------- script:" << getFilename();
104 // If we're not already in the middle of stopping all scripts, then we should remove ourselves
105 // from the list of running scripts. We don't do this if we're in the process of stopping all scripts
106 // because that method removes scripts from its list as it iterates them
106 if (!_stoppingAllScripts) {107 if (!_stoppingAllScripts) {
107 _allScriptsMutex.lock();108 _allScriptsMutex.lock();
108 qDebug() << "ScriptEngine::~ScriptEngine() " << getFilename() << " removing from list of scripts";
109 _allKnownScriptEngines.remove(this);109 _allKnownScriptEngines.remove(this);
110 _allScriptsMutex.unlock();110 _allScriptsMutex.unlock();
111 } else {
112 qDebug() << "ScriptEngine::~ScriptEngine() " << getFilename() << " NOT removing from list of scripts here...";
113 }111 }
114 qDebug() << "ScriptEngine::~ScriptEngine() " << getFilename() << " -------- DONE -----------";112 qDebug() << "ScriptEngine::~ScriptEngine() ------- END ------- script:" << getFilename();
115}113}
116 114
117QSet<ScriptEngine*> ScriptEngine::_allKnownScriptEngines;115QSet<ScriptEngine*> ScriptEngine::_allKnownScriptEngines;
118QMutex ScriptEngine::_allScriptsMutex;116QMutex ScriptEngine::_allScriptsMutex;
119bool ScriptEngine::_stoppingAllScripts = false;117bool ScriptEngine::_stoppingAllScripts = false;
118bool ScriptEngine::_doneRunningThisScript = false;
120 119
121void ScriptEngine::stopAllScripts(QObject* application) {120void ScriptEngine::stopAllScripts(QObject* application) {
121 qDebug() << "ScriptEngine::stopAllScripts() ------- BEGIN -------";
122 122
123 qDebug() << "ScriptEngine::stopAllScripts() -------- BEGIN -----------";
124 _allScriptsMutex.lock();123 _allScriptsMutex.lock();
125 _stoppingAllScripts = true;124 _stoppingAllScripts = true;
126 125
@@ -128,44 +127,92 @@ void ScriptEngine::stopAllScripts(QObject* application) { · 1 unmodified line
128 while (i.hasNext()) {127 while (i.hasNext()) {
129 ScriptEngine* scriptEngine = i.next();128 ScriptEngine* scriptEngine = i.next();
130 129
131 qDebug() << "ScriptEngine::stopAllScripts() scriptEngine: " << (void*)scriptEngine;
132
133 QString scriptName = scriptEngine->getFilename();130 QString scriptName = scriptEngine->getFilename();
134 131
135 qDebug() << "ScriptEngine::stopAllScripts() considering script: " << scriptName << "evaluatePending:" << scriptEngine->evaluatePending();132 qDebug() << "ScriptEngine::stopAllScripts() considering:" << scriptName
133 << "isRunning():" << scriptEngine->isRunning()
134 << "evaluatePending():" << scriptEngine->evaluatePending();
136 135
137 // NOTE: typically all script engines are running. But there's at least one known exception to this, the136 // NOTE: typically all script engines are running. But there's at least one known exception to this, the
138 // "entities sandbox" which is only used to evaluate entities scripts to test their validity before using137 // "entities sandbox" which is only used to evaluate entities scripts to test their validity before using
139 // them. We don't need to stop scripts that aren't running. 138 // them. We don't need to stop scripts that aren't running.
140 if (scriptEngine->isRunning()) {139 if (scriptEngine->isRunning()) {
140
141 // If the script is running, but still evaluating then we need to wait for its evaluation step to
142 // complete. After that we can handle the stop process appropriately
141 if (scriptEngine->evaluatePending()) {143 if (scriptEngine->evaluatePending()) {
142 qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__ << "waiting for evaluation to complete....";144 while (scriptEngine->evaluatePending()) {
143 QEventLoop loop;145 QEventLoop loop;
144 QObject::connect(scriptEngine, &ScriptEngine::evaluationFinished, &loop, &QEventLoop::quit);146 QObject::connect(scriptEngine, &ScriptEngine::evaluationFinished, &loop, &QEventLoop::quit);
145 loop.exec();147 qDebug() << "ScriptEngine::stopAllScripts() while(evaluatePending()) STARTING loop.exec() script:" << scriptName;
146 qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__ << "evaluation complete....";148 loop.exec();
149 qDebug() << "ScriptEngine::stopAllScripts() while(evaluatePending()) AFTER loop.exec() script:" << scriptName;
150 }
147 }151 }
148 152
149 qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__;
150
151 QEventLoop loop;
152 QObject::connect(scriptEngine, &ScriptEngine::doneRunning, &loop, &QEventLoop::quit);
153
154 qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__;
155 scriptEngine->disconnect(application);153 scriptEngine->disconnect(application);
156 qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__;
157 scriptEngine->stop();154 scriptEngine->stop();
158 qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__;155
159 loop.exec();156 qDebug() << "ScriptEngine::stopAllScripts() -- WAITING for waitTillDoneRunning() script:" << scriptName;
160 qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__;157 scriptEngine->waitTillDoneRunning();
158 qDebug() << "ScriptEngine::stopAllScripts() -- AFTER waitTillDoneRunning() script:" << scriptName;
161 i.remove();159 i.remove();
162 } else {160 } else {
163 qDebug() << "ScriptEngine::stopAllScripts() script: " << scriptName << " was not running... skipping it's stop() call.";
164 }161 }
165 }162 }
166 _stoppingAllScripts = false;163 _stoppingAllScripts = false;
167 _allScriptsMutex.unlock();164 _allScriptsMutex.unlock();
168 qDebug() << "ScriptEngine::stopAllScripts() -------- DONE -----------";165 qDebug() << "ScriptEngine::stopAllScripts() ------- DONE -------";
166}
167
168
169void ScriptEngine::waitTillDoneRunning() {
170 QString scriptName = getFilename();
171 qDebug() << "ScriptEngine::waitTillDoneRunning() ------- BEGIN ------- script:" << scriptName;
172 qDebug() << " _isFinished:" << _isFinished;
173 qDebug() << " _isRunning:" << _isRunning;
174 qDebug() << " _isInitialized:" << _isInitialized;
175 qDebug() << " _evaluatesPending:" << _evaluatesPending;
176
177 if (_isRunning) {
178 qDebug() << "ScriptEngine::waitTillDoneRunning() SETTING _doneRunningThisScript = false ################################### line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
179 _doneRunningThisScript = false;
180 _isWaitingForDoneRunning = true;
181
182 // What can we do here???
183 // we will be calling this on the main Application thread, inside of stopAllScripts()
184 // we want the application thread to continue to process events, because the script will
185 // likely need to marshall messages across to the main thread.
186 while (!_doneRunningThisScript && _isRunning) {
187
188 // and run a QEventLoop???
189 QEventLoop loop;
190 QObject::connect(this, &ScriptEngine::doneRunning, &loop, &QEventLoop::quit);
191 qDebug() << "ScriptEngine::waitTillDoneRunning() STARTING loop.exec() script:" << scriptName;
192 qDebug() << " _doneRunningThisScript:" << _doneRunningThisScript;
193 qDebug() << " _isRunning:" << _isRunning;
194 qDebug() << " _isWaitingForDoneRunning:" << _isWaitingForDoneRunning;
195 qDebug() << " _isFinished:" << _isFinished;
196 qDebug() << " _isInitialized:" << _isInitialized;
197 qDebug() << " _evaluatesPending:" << _evaluatesPending;
198 loop.exec();
199 qDebug() << "ScriptEngine::waitTillDoneRunning() AFTER loop.exec() script:" << scriptName;
200
201 // process events
202 qDebug() << "ScriptEngine::waitTillDoneRunning() STARTING QCoreApplication::processEvents() script:" << scriptName;
203 QCoreApplication::processEvents();
204 qDebug() << "ScriptEngine::waitTillDoneRunning() AFTER QCoreApplication::processEvents() script:" << scriptName;
205
206 if (_doneRunningThisScript) {
207 qDebug() << "ScriptEngine::waitTillDoneRunning() _doneRunningThisScript after processEvents... breaking!!! script:" << scriptName;
208 break;
209 }
210 }
211
212 _isWaitingForDoneRunning = false;
213 }
214
215 qDebug() << "ScriptEngine::waitTillDoneRunning() ------- DONE ------- script:" << scriptName;
169}216}
170 217
171QString ScriptEngine::getFilename() const { 218QString ScriptEngine::getFilename() const {
@@ -377,12 +424,23 @@ void ScriptEngine::registerGetterSetter(const QString& name, QScriptEngine::Func · 205 unmodified lines
377}424}
378 425
379void ScriptEngine::evaluate() {426void ScriptEngine::evaluate() {
427 if (_stoppingAllScripts) {
428 qDebug() << "ScriptEngine::evaluate() while shutting down is ignored...";
429 qDebug() << " parent script:" << getFilename() << "[" << this << "]";
430 return; // bail early
431 }
432
433 qDebug() << "ScriptEngine::evaluate() -- BEGIN script:" << getFilename() << "[" << this << "]";
380 if (!_isInitialized) {434 if (!_isInitialized) {
381 init();435 init();
382 }436 }
383 437
438 qDebug() << "ScriptEngine::evaluate() -- about to call own evaluate(program) script:" << getFilename() << "[" << this << "]";
384 QScriptValue result = evaluate(_scriptContents);439 QScriptValue result = evaluate(_scriptContents);
440 qDebug() << "ScriptEngine::evaluate() -- AFTER own evaluate(program) script:" << getFilename() << "[" << this << "]";
385 441
442 // TODO: why do we check this twice? It seems like the call to clearExcpetions() in the lower level evaluate call
443 // will cause this code to never actually run...
386 if (hasUncaughtException()) {444 if (hasUncaughtException()) {
387 int line = uncaughtExceptionLineNumber();445 int line = uncaughtExceptionLineNumber();
388 qDebug() << "Uncaught exception at (" << _fileNameString << ") line" << line << ":" << result.toString();446 qDebug() << "Uncaught exception at (" << _fileNameString << ") line" << line << ":" << result.toString();
@@ -392,17 +450,30 @@ void ScriptEngine::evaluate() { · 3 unmodified lines
392}450}
393 451
394QScriptValue ScriptEngine::evaluate(const QString& program, const QString& fileName, int lineNumber) {452QScriptValue ScriptEngine::evaluate(const QString& program, const QString& fileName, int lineNumber) {
395 qDebug() << "ScriptEngine::evaluate() " << getFilename() << " line:" << __LINE__;453 if (_stoppingAllScripts) {
396 _evaluatePending = true;454 qDebug() << "ScriptEngine::evaluate(program) while shutting down is ignored...";
455 qDebug() << " parent script:" << getFilename() << "[" << this << "]";
456 return QScriptValue(); // bail early
457 }
458
459 qDebug() << "ScriptEngine::evaluate(program) -- BEGIN script:" << getFilename() << "_evaluatesPending:" << _evaluatesPending << "[" << this << "]";
460 _evaluatesPending++;
461 qDebug() << "ScriptEngine::evaluate(program) -- after _evaluatesPending++ script:" << getFilename() << "_evaluatesPending:" << _evaluatesPending << "[" << this << "]";
462 qDebug() << "ScriptEngine::evaluate(program) -- BEFORE QScriptEngine::evaluate() script:" << getFilename() << "_evaluatesPending:" << _evaluatesPending << "[" << this << "]";
397 QScriptValue result = QScriptEngine::evaluate(program, fileName, lineNumber);463 QScriptValue result = QScriptEngine::evaluate(program, fileName, lineNumber);
464 qDebug() << "ScriptEngine::evaluate(program) -- AFTER QScriptEngine::evaluate() script:" << getFilename() << "_evaluatesPending:" << _evaluatesPending << "[" << this << "]";
398 if (hasUncaughtException()) {465 if (hasUncaughtException()) {
399 int line = uncaughtExceptionLineNumber();466 int line = uncaughtExceptionLineNumber();
400 qDebug() << "Uncaught exception at (" << _fileNameString << " : " << fileName << ") line" << line << ": " << result.toString();467 qDebug() << "Uncaught exception at (" << _fileNameString << " : " << fileName << ") line" << line << ": " << result.toString();
401 }468 }
469 _evaluatesPending--;
470 qDebug() << "ScriptEngine::evaluate(program) -- after _evaluatesPending-- script:" << getFilename() << "_evaluatesPending:" << _evaluatesPending << "[" << this << "]";
471
472 qDebug() << "ScriptEngine::evaluate(program) -- ABOUT TO emit evaluationFinished() script:" << getFilename() << "_evaluatesPending:" << _evaluatesPending << "[" << this << "]";
402 emit evaluationFinished(result, hasUncaughtException());473 emit evaluationFinished(result, hasUncaughtException());
474 qDebug() << "ScriptEngine::evaluate(program) -- AFTER emit evaluationFinished() script:" << getFilename() << "_evaluatesPending:" << _evaluatesPending << "[" << this << "]";
403 clearExceptions();475 clearExceptions();
404 _evaluatePending = false;476 qDebug() << "ScriptEngine::evaluate(program) -- DONE script:" << getFilename() << "_evaluatesPending:" << _evaluatesPending << "[" << this << "]";
405 qDebug() << "ScriptEngine::evaluate() " << getFilename() << " line:" << __LINE__;
406 return result;477 return result;
407}478}
408 479
@@ -419,49 +490,60 @@ void ScriptEngine::sendAvatarBillboardPacket() { · 10 unmodified lines
419}490}
420 491
421void ScriptEngine::run() {492void ScriptEngine::run() {
422 qDebug() << "ScriptEngine::run() " << getFilename() << " -------- BEGIN -----------";493 // TODO: can we add a short circuit for _stoppingAllScripts here? What does it mean to not start running if
494 // we're in the process of stopping?
423 495
496 qDebug() << "ScriptEngine::run() line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
424 if (!_isInitialized) {497 if (!_isInitialized) {
425 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
426 init();498 init();
427 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
428 }499 }
429 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;500 qDebug() << "ScriptEngine::run() line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
430 _isRunning = true;501 _isRunning = true;
431 _isFinished = false;502 _isFinished = false;
432 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
433 emit runningStateChanged();503 emit runningStateChanged();
434 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
435 504
436 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;505 qDebug() << "ScriptEngine::run() line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
437 QScriptValue result = evaluate(_scriptContents);506 QScriptValue result = evaluate(_scriptContents);
438 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;507 qDebug() << "ScriptEngine::run() line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
439 508
440 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
441 QElapsedTimer startTime;509 QElapsedTimer startTime;
442 startTime.start();510 startTime.start();
443 511
444 int thisFrame = 0;512 int thisFrame = 0;
445 513
446 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
447 auto nodeList = DependencyManager::get<NodeList>();514 auto nodeList = DependencyManager::get<NodeList>();
448 515
449 qint64 lastUpdate = usecTimestampNow();516 qint64 lastUpdate = usecTimestampNow();
450 517
451 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;518 qDebug() << "ScriptEngine::run() line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
519
452 while (!_isFinished) {520 while (!_isFinished) {
521 if (_isWaitingForDoneRunning) {
522 qDebug() << "ScriptEngine::run() ** _isWaitingForDoneRunning == true ** line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
523 }
524
453 int usecToSleep = (thisFrame++ * SCRIPT_DATA_CALLBACK_USECS) - startTime.nsecsElapsed() / 1000; // nsec to usec525 int usecToSleep = (thisFrame++ * SCRIPT_DATA_CALLBACK_USECS) - startTime.nsecsElapsed() / 1000; // nsec to usec
454 if (usecToSleep > 0) {526 if (usecToSleep > 0) {
455 usleep(usecToSleep);527 usleep(usecToSleep);
456 }528 }
457 529
458 if (_isFinished) {530 if (_isFinished) {
531 qDebug() << "ScriptEngine::run() line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
459 break;532 break;
460 }533 }
461 534
535 if (_isWaitingForDoneRunning) {
536 qDebug() << "ScriptEngine::run() ** _isWaitingForDoneRunning == true ** line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
537 }
538
462 QCoreApplication::processEvents();539 QCoreApplication::processEvents();
463 540
541 if (_isWaitingForDoneRunning) {
542 qDebug() << "ScriptEngine::run() ** _isWaitingForDoneRunning == true ** line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
543 }
544
464 if (_isFinished) {545 if (_isFinished) {
546 qDebug() << "ScriptEngine::run() line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
465 break;547 break;
466 }548 }
467 549
@@ -589,16 +671,38 @@ void ScriptEngine::run() { · 121 unmodified lines
589 emit update(deltaTime);671 emit update(deltaTime);
590 }672 }
591 lastUpdate = now;673 lastUpdate = now;
674
675 if (_isWaitingForDoneRunning) {
676 qDebug() << "ScriptEngine::run() ** _isWaitingForDoneRunning == true ** line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
677 }
678
679 }
680
681 if (_isWaitingForDoneRunning) {
682 qDebug() << "ScriptEngine::run() ** _isWaitingForDoneRunning == true ** line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
592 }683 }
593 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;684
685 qDebug() << "ScriptEngine::run() line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
594 686
595 stopAllTimers(); // make sure all our timers are stopped if the script is ending687 stopAllTimers(); // make sure all our timers are stopped if the script is ending
688 qDebug() << "ScriptEngine::run() line:" << __LINE__ << " script:" << getFilename();
689
690 if (_isWaitingForDoneRunning) {
691 qDebug() << "ScriptEngine::run() ** _isWaitingForDoneRunning == true ** line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
692 }
596 693
597 emit scriptEnding();694 emit scriptEnding();
695 qDebug() << "ScriptEngine::run() line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
598 696
599 // kill the avatar identity timer697 // kill the avatar identity timer
600 delete _avatarIdentityTimer;698 delete _avatarIdentityTimer;
601 699
700 qDebug() << "ScriptEngine::run() line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
701
702 if (_isWaitingForDoneRunning) {
703 qDebug() << "ScriptEngine::run() ** _isWaitingForDoneRunning == true ** line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
704 }
705
602 if (_entityScriptingInterface.getEntityPacketSender()->serversExist()) {706 if (_entityScriptingInterface.getEntityPacketSender()->serversExist()) {
603 // release the queue of edit entity messages.707 // release the queue of edit entity messages.
604 _entityScriptingInterface.getEntityPacketSender()->releaseQueuedMessages();708 _entityScriptingInterface.getEntityPacketSender()->releaseQueuedMessages();
@@ -609,19 +713,44 @@ void ScriptEngine::run() { · 4 unmodified lines
609 }713 }
610 }714 }
611 715
716 qDebug() << "ScriptEngine::run() line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
717
612 // If we were on a thread, then wait till it's done718 // If we were on a thread, then wait till it's done
613 if (thread()) {719 if (thread()) {
720 qDebug() << "ScriptEngine::run() line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
614 thread()->quit();721 thread()->quit();
722 qDebug() << "ScriptEngine::run() line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
723 }
724
725 qDebug() << "ScriptEngine::run() line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
726
727 if (_isWaitingForDoneRunning) {
728 qDebug() << "ScriptEngine::run() ** _isWaitingForDoneRunning == true ** line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
615 }729 }
616 730
617 emit finished(_fileNameString);731 emit finished(_fileNameString);
618 732
619 _isRunning = false;733 if (_isWaitingForDoneRunning) {
734 qDebug() << "ScriptEngine::run() ** _isWaitingForDoneRunning == true ** line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
735 }
620 736
737 _isRunning = false;
621 emit runningStateChanged();738 emit runningStateChanged();
622 739
740 qDebug() << "ABOUT TO emit doneRunning() script:" << getFilename() << "[" << this << "]";
623 emit doneRunning();741 emit doneRunning();
624 qDebug() << "ScriptEngine::run() " << getFilename() << " -------- DONE -----------" << " line:" << __LINE__;742 qDebug() << "AFTER emit doneRunning() script:" << getFilename() << "[" << this << "]";
743
744 if (_isWaitingForDoneRunning) {
745 qDebug() << "ScriptEngine::run() ** _isWaitingForDoneRunning == true ** line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
746 }
747
748 qDebug() << "ScriptEngine::run() SETTING _doneRunningThisScript = true ################################### line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
749 _doneRunningThisScript = true;
750
751 if (_isWaitingForDoneRunning) {
752 qDebug() << "ScriptEngine::run() ** _isWaitingForDoneRunning == true ** line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]";
753 }
625}754}
626 755
627// NOTE: This is private because it must be called on the same thread that created the timers, which is why756// NOTE: This is private because it must be called on the same thread that created the timers, which is why
@@ -636,9 +765,10 @@ void ScriptEngine::stopAllTimers() { · 8 unmodified lines
636}765}
637 766
638void ScriptEngine::stop() {767void ScriptEngine::stop() {
639 qDebug() << "ScriptEngine::stop() " << getFilename() << "[" << ((void*)this) << "]";768 qDebug() << "ScriptEngine::stop() -- START -- line:" << __LINE__ << " script:" << getFilename();
640 _isFinished = true;769 _isFinished = true;
641 emit runningStateChanged();770 emit runningStateChanged();
771 qDebug() << "ScriptEngine::stop() -- DONE -- line:" << __LINE__ << " script:" << getFilename();
642}772}
643 773
644void ScriptEngine::timerFired() {774void ScriptEngine::timerFired() {
@@ -674,10 +804,22 @@ QObject* ScriptEngine::setupTimerWithInterval(const QScriptValue& function, int · 29 unmodified lines
674}804}
675 805
676QObject* ScriptEngine::setInterval(const QScriptValue& function, int intervalMS) {806QObject* ScriptEngine::setInterval(const QScriptValue& function, int intervalMS) {
807 if (_stoppingAllScripts) {
808 qDebug() << "Script.setInterval() while shutting down is ignored...";
809 qDebug() << " parent script:" << getFilename() << "[" << this << "]";
810 return NULL; // bail early
811 }
812
677 return setupTimerWithInterval(function, intervalMS, false);813 return setupTimerWithInterval(function, intervalMS, false);
678}814}
679 815
680QObject* ScriptEngine::setTimeout(const QScriptValue& function, int timeoutMS) {816QObject* ScriptEngine::setTimeout(const QScriptValue& function, int timeoutMS) {
817 if (_stoppingAllScripts) {
818 qDebug() << "Script.setTimeout() while shutting down is ignored...";
819 qDebug() << " parent script:" << getFilename() << "[" << this << "]";
820 return NULL; // bail early
821 }
822
681 return setupTimerWithInterval(function, timeoutMS, true);823 return setupTimerWithInterval(function, timeoutMS, true);
682}824}
683 825
@@ -723,6 +865,12 @@ void ScriptEngine::print(const QString& message) { · 39 unmodified lines
723// If no callback is specified, the included files will be loaded synchronously and will block execution until865// If no callback is specified, the included files will be loaded synchronously and will block execution until
724// all of the files have finished loading.866// all of the files have finished loading.
725void ScriptEngine::include(const QStringList& includeFiles, QScriptValue callback) {867void ScriptEngine::include(const QStringList& includeFiles, QScriptValue callback) {
868 if (_stoppingAllScripts) {
869 qDebug() << "Script.include() while shutting down is ignored...";
870 qDebug() << " includeFiles:" << includeFiles;
871 qDebug() << " parent script:" << getFilename() << "[" << this << "]";
872 return; // bail early
873 }
726 QList<QUrl> urls;874 QList<QUrl> urls;
727 for (QString file : includeFiles) {875 for (QString file : includeFiles) {
728 urls.append(resolvePath(file));876 urls.append(resolvePath(file));
@@ -736,7 +884,9 @@ void ScriptEngine::include(const QStringList& includeFiles, QScriptValue callbac · 7 unmodified lines
736 if (contents.isNull()) {884 if (contents.isNull()) {
737 qDebug() << "Error loading file: " << url;885 qDebug() << "Error loading file: " << url;
738 } else {886 } else {
887 qDebug() << "ScriptEngine::include() BEFORE evaluate() line:" << __LINE__ << " parent script:" << getFilename() << "[" << this << "]" << " url:" << url << "_evaluatesPending:" << _evaluatesPending;
739 QScriptValue result = evaluate(contents, url.toString());888 QScriptValue result = evaluate(contents, url.toString());
889 qDebug() << "ScriptEngine::include() AFTER evaluate() line:" << __LINE__ << " parent script:" << getFilename() << "[" << this << "]" << " url:" << url << "_evaluatesPending:" << _evaluatesPending;
740 }890 }
741 }891 }
742 892
@@ -762,17 +912,31 @@ void ScriptEngine::include(const QStringList& includeFiles, QScriptValue callbac · 19 unmodified lines
762}912}
763 913
764void ScriptEngine::include(const QString& includeFile, QScriptValue callback) {914void ScriptEngine::include(const QString& includeFile, QScriptValue callback) {
915 if (_stoppingAllScripts) {
916 qDebug() << "Script.include() while shutting down is ignored...";
917 qDebug() << " includeFile:" << includeFile;
918 qDebug() << " parent script:" << getFilename() << "[" << this << "]";
919 return; // bail early
920 }
921
765 QStringList urls;922 QStringList urls;
766 urls.append(includeFile);923 urls.append(includeFile);
767 include(urls, callback);924 include(urls, callback);
768}925}
769 926
927// NOTE: The load() command is similar to the include() command except that it loads the script
928// as a stand-alone script. To accomplish this, the ScriptEngine class just emits a signal which
929// the Application or other context will connect to in order to know to actually load the script
770void ScriptEngine::load(const QString& loadFile) {930void ScriptEngine::load(const QString& loadFile) {
771 qDebug() << "ScriptEngine::load() " << getFilename() << "[" << ((void*)this) << "]" << "line:" << __LINE__;931 if (_stoppingAllScripts) {
932 qDebug() << "Script.load() while shutting down is ignored...";
933 qDebug() << " loadFile:" << loadFile;
934 qDebug() << " parent script:" << getFilename() << "[" << this << "]";
935 return; // bail early
936 }
937
772 QUrl url = resolvePath(loadFile);938 QUrl url = resolvePath(loadFile);
773 qDebug() << "ScriptEngine::load() " << getFilename() << "[" << ((void*)this) << "]" << "line:" << __LINE__;
774 emit loadScript(url.toString(), false);939 emit loadScript(url.toString(), false);
775 qDebug() << "ScriptEngine::load() " << getFilename() << "[" << ((void*)this) << "]" << "line:" << __LINE__;
776}940}
777 941
778void ScriptEngine::nodeKilled(SharedNodePointer node) {942void ScriptEngine::nodeKilled(SharedNodePointer node) {