Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 4 files). Show all
Unified · Split
| @@ -94,33 +94,32 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam · 93 unmodified lines | ||
| 94 | 94 | _isUserLoaded(false), |
| 95 | 95 | _arrayBufferClass(new ArrayBufferClass(this)) |
| 96 | 96 | { |
| 97 | − qDebug() << "ScriptEngine::ScriptEngine() " << getFilename() << "[" << ((void*)this) << "]" << " -------- BEGIN -----------"; | |
| 98 | 97 | _allScriptsMutex.lock(); |
| 99 | 98 | _allKnownScriptEngines.insert(this); |
| 100 | 99 | _allScriptsMutex.unlock(); |
| 101 | − qDebug() << "ScriptEngine::ScriptEngine() " << getFilename() << " -------- DONE -----------"; | |
| 102 | 100 | } |
| 103 | 101 | |
| 104 | 102 | ScriptEngine::~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 | 107 | if (!_stoppingAllScripts) { |
| 107 | 108 | _allScriptsMutex.lock(); |
| 108 | − qDebug() << "ScriptEngine::~ScriptEngine() " << getFilename() << " removing from list of scripts"; | |
| 109 | 109 | _allKnownScriptEngines.remove(this); |
| 110 | 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 | |
| 117 | 115 | QSet<ScriptEngine*> ScriptEngine::_allKnownScriptEngines; |
| 118 | 116 | QMutex ScriptEngine::_allScriptsMutex; |
| 119 | 117 | bool ScriptEngine::_stoppingAllScripts = false; |
| 118 | +bool ScriptEngine::_doneRunningThisScript = false; | |
| 120 | 119 | |
| 121 | 120 | void ScriptEngine::stopAllScripts(QObject* application) { |
| 121 | + qDebug() << "ScriptEngine::stopAllScripts() ------- BEGIN -------"; | |
| 122 | 122 | |
| 123 | − qDebug() << "ScriptEngine::stopAllScripts() -------- BEGIN -----------"; | |
| 124 | 123 | _allScriptsMutex.lock(); |
| 125 | 124 | _stoppingAllScripts = true; |
| 126 | 125 | |
| @@ -128,44 +127,92 @@ void ScriptEngine::stopAllScripts(QObject* application) { · 1 unmodified line | ||
| 128 | 127 | while (i.hasNext()) { |
| 129 | 128 | ScriptEngine* scriptEngine = i.next(); |
| 130 | 129 | |
| 131 | − qDebug() << "ScriptEngine::stopAllScripts() scriptEngine: " << (void*)scriptEngine; | |
| 132 | − | |
| 133 | 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 | 136 | // NOTE: typically all script engines are running. But there's at least one known exception to this, the |
| 138 | 137 | // "entities sandbox" which is only used to evaluate entities scripts to test their validity before using |
| 139 | 138 | // them. We don't need to stop scripts that aren't running. |
| 140 | 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 | 143 | if (scriptEngine->evaluatePending()) { |
| 142 | − qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__ << "waiting for evaluation to complete...."; | |
| 143 | − QEventLoop loop; | |
| 144 | − QObject::connect(scriptEngine, &ScriptEngine::evaluationFinished, &loop, &QEventLoop::quit); | |
| 145 | − loop.exec(); | |
| 146 | − qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__ << "evaluation complete...."; | |
| 144 | + while (scriptEngine->evaluatePending()) { | |
| 145 | + QEventLoop loop; | |
| 146 | + QObject::connect(scriptEngine, &ScriptEngine::evaluationFinished, &loop, &QEventLoop::quit); | |
| 147 | + qDebug() << "ScriptEngine::stopAllScripts() while(evaluatePending()) STARTING loop.exec() script:" << scriptName; | |
| 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 | 153 | scriptEngine->disconnect(application); |
| 156 | − qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__; | |
| 157 | 154 | scriptEngine->stop(); |
| 158 | − qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__; | |
| 159 | − loop.exec(); | |
| 160 | − qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__; | |
| 155 | + | |
| 156 | + qDebug() << "ScriptEngine::stopAllScripts() -- WAITING for waitTillDoneRunning() script:" << scriptName; | |
| 157 | + scriptEngine->waitTillDoneRunning(); | |
| 158 | + qDebug() << "ScriptEngine::stopAllScripts() -- AFTER waitTillDoneRunning() script:" << scriptName; | |
| 161 | 159 | i.remove(); |
| 162 | 160 | } else { |
| 163 | − qDebug() << "ScriptEngine::stopAllScripts() script: " << scriptName << " was not running... skipping it's stop() call."; | |
| 164 | 161 | } |
| 165 | 162 | } |
| 166 | 163 | _stoppingAllScripts = false; |
| 167 | 164 | _allScriptsMutex.unlock(); |
| 168 | − qDebug() << "ScriptEngine::stopAllScripts() -------- DONE -----------"; | |
| 165 | + qDebug() << "ScriptEngine::stopAllScripts() ------- DONE -------"; | |
| 166 | +} | |
| 167 | + | |
| 168 | + | |
| 169 | +void 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 | |
| 171 | 218 | QString ScriptEngine::getFilename() const { |
| @@ -377,12 +424,23 @@ void ScriptEngine::registerGetterSetter(const QString& name, QScriptEngine::Func · 205 unmodified lines | ||
| 377 | 424 | } |
| 378 | 425 | |
| 379 | 426 | void 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 | 434 | if (!_isInitialized) { |
| 381 | 435 | init(); |
| 382 | 436 | } |
| 383 | 437 | |
| 438 | + qDebug() << "ScriptEngine::evaluate() -- about to call own evaluate(program) script:" << getFilename() << "[" << this << "]"; | |
| 384 | 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 | 444 | if (hasUncaughtException()) { |
| 387 | 445 | int line = uncaughtExceptionLineNumber(); |
| 388 | 446 | qDebug() << "Uncaught exception at (" << _fileNameString << ") line" << line << ":" << result.toString(); |
| @@ -392,17 +450,30 @@ void ScriptEngine::evaluate() { · 3 unmodified lines | ||
| 392 | 450 | } |
| 393 | 451 | |
| 394 | 452 | QScriptValue ScriptEngine::evaluate(const QString& program, const QString& fileName, int lineNumber) { |
| 395 | − qDebug() << "ScriptEngine::evaluate() " << getFilename() << " line:" << __LINE__; | |
| 396 | − _evaluatePending = true; | |
| 453 | + if (_stoppingAllScripts) { | |
| 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 | 463 | QScriptValue result = QScriptEngine::evaluate(program, fileName, lineNumber); |
| 464 | + qDebug() << "ScriptEngine::evaluate(program) -- AFTER QScriptEngine::evaluate() script:" << getFilename() << "_evaluatesPending:" << _evaluatesPending << "[" << this << "]"; | |
| 398 | 465 | if (hasUncaughtException()) { |
| 399 | 466 | int line = uncaughtExceptionLineNumber(); |
| 400 | 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 | 473 | emit evaluationFinished(result, hasUncaughtException()); |
| 474 | + qDebug() << "ScriptEngine::evaluate(program) -- AFTER emit evaluationFinished() script:" << getFilename() << "_evaluatesPending:" << _evaluatesPending << "[" << this << "]"; | |
| 403 | 475 | clearExceptions(); |
| 404 | − _evaluatePending = false; | |
| 405 | − qDebug() << "ScriptEngine::evaluate() " << getFilename() << " line:" << __LINE__; | |
| 476 | + qDebug() << "ScriptEngine::evaluate(program) -- DONE script:" << getFilename() << "_evaluatesPending:" << _evaluatesPending << "[" << this << "]"; | |
| 406 | 477 | return result; |
| 407 | 478 | } |
| 408 | 479 | |
| @@ -419,49 +490,60 @@ void ScriptEngine::sendAvatarBillboardPacket() { · 10 unmodified lines | ||
| 419 | 490 | } |
| 420 | 491 | |
| 421 | 492 | void 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 | 497 | if (!_isInitialized) { |
| 425 | − qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__; | |
| 426 | 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 | 501 | _isRunning = true; |
| 431 | 502 | _isFinished = false; |
| 432 | − qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__; | |
| 433 | 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 | 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 | 509 | QElapsedTimer startTime; |
| 442 | 510 | startTime.start(); |
| 443 | 511 | |
| 444 | 512 | int thisFrame = 0; |
| 445 | 513 | |
| 446 | − qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__; | |
| 447 | 514 | auto nodeList = DependencyManager::get<NodeList>(); |
| 448 | 515 | |
| 449 | 516 | qint64 lastUpdate = usecTimestampNow(); |
| 450 | 517 | |
| 451 | − qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__; | |
| 518 | + qDebug() << "ScriptEngine::run() line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]"; | |
| 519 | + | |
| 452 | 520 | while (!_isFinished) { |
| 521 | + if (_isWaitingForDoneRunning) { | |
| 522 | + qDebug() << "ScriptEngine::run() ** _isWaitingForDoneRunning == true ** line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]"; | |
| 523 | + } | |
| 524 | + | |
| 453 | 525 | int usecToSleep = (thisFrame++ * SCRIPT_DATA_CALLBACK_USECS) - startTime.nsecsElapsed() / 1000; // nsec to usec |
| 454 | 526 | if (usecToSleep > 0) { |
| 455 | 527 | usleep(usecToSleep); |
| 456 | 528 | } |
| 457 | 529 | |
| 458 | 530 | if (_isFinished) { |
| 531 | + qDebug() << "ScriptEngine::run() line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]"; | |
| 459 | 532 | break; |
| 460 | 533 | } |
| 461 | 534 | |
| 535 | + if (_isWaitingForDoneRunning) { | |
| 536 | + qDebug() << "ScriptEngine::run() ** _isWaitingForDoneRunning == true ** line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]"; | |
| 537 | + } | |
| 538 | + | |
| 462 | 539 | QCoreApplication::processEvents(); |
| 463 | 540 | |
| 541 | + if (_isWaitingForDoneRunning) { | |
| 542 | + qDebug() << "ScriptEngine::run() ** _isWaitingForDoneRunning == true ** line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]"; | |
| 543 | + } | |
| 544 | + | |
| 464 | 545 | if (_isFinished) { |
| 546 | + qDebug() << "ScriptEngine::run() line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]"; | |
| 465 | 547 | break; |
| 466 | 548 | } |
| 467 | 549 | |
| @@ -589,16 +671,38 @@ void ScriptEngine::run() { · 121 unmodified lines | ||
| 589 | 671 | emit update(deltaTime); |
| 590 | 672 | } |
| 591 | 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 | 687 | 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 | 694 | emit scriptEnding(); |
| 695 | + qDebug() << "ScriptEngine::run() line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]"; | |
| 598 | 696 | |
| 599 | 697 | // kill the avatar identity timer |
| 600 | 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 | 706 | if (_entityScriptingInterface.getEntityPacketSender()->serversExist()) { |
| 603 | 707 | // release the queue of edit entity messages. |
| 604 | 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 | 718 | // If we were on a thread, then wait till it's done |
| 613 | 719 | if (thread()) { |
| 720 | + qDebug() << "ScriptEngine::run() line:" << __LINE__ << " script:" << getFilename() << "[" << this << "]"; | |
| 614 | 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 | 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 | 738 | emit runningStateChanged(); |
| 622 | 739 | |
| 740 | + qDebug() << "ABOUT TO emit doneRunning() script:" << getFilename() << "[" << this << "]"; | |
| 623 | 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 | 756 | // 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 | |
| 638 | 767 | void ScriptEngine::stop() { |
| 639 | − qDebug() << "ScriptEngine::stop() " << getFilename() << "[" << ((void*)this) << "]"; | |
| 768 | + qDebug() << "ScriptEngine::stop() -- START -- line:" << __LINE__ << " script:" << getFilename(); | |
| 640 | 769 | _isFinished = true; |
| 641 | 770 | emit runningStateChanged(); |
| 771 | + qDebug() << "ScriptEngine::stop() -- DONE -- line:" << __LINE__ << " script:" << getFilename(); | |
| 642 | 772 | } |
| 643 | 773 | |
| 644 | 774 | void ScriptEngine::timerFired() { |
| @@ -674,10 +804,22 @@ QObject* ScriptEngine::setupTimerWithInterval(const QScriptValue& function, int · 29 unmodified lines | ||
| 674 | 804 | } |
| 675 | 805 | |
| 676 | 806 | QObject* 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 | 813 | return setupTimerWithInterval(function, intervalMS, false); |
| 678 | 814 | } |
| 679 | 815 | |
| 680 | 816 | QObject* 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 | 823 | return setupTimerWithInterval(function, timeoutMS, true); |
| 682 | 824 | } |
| 683 | 825 | |
| @@ -723,6 +865,12 @@ void ScriptEngine::print(const QString& message) { · 39 unmodified lines | ||
| 723 | 865 | // If no callback is specified, the included files will be loaded synchronously and will block execution until |
| 724 | 866 | // all of the files have finished loading. |
| 725 | 867 | void 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 | 874 | QList<QUrl> urls; |
| 727 | 875 | for (QString file : includeFiles) { |
| 728 | 876 | urls.append(resolvePath(file)); |
| @@ -736,7 +884,9 @@ void ScriptEngine::include(const QStringList& includeFiles, QScriptValue callbac · 7 unmodified lines | ||
| 736 | 884 | if (contents.isNull()) { |
| 737 | 885 | qDebug() << "Error loading file: " << url; |
| 738 | 886 | } else { |
| 887 | + qDebug() << "ScriptEngine::include() BEFORE evaluate() line:" << __LINE__ << " parent script:" << getFilename() << "[" << this << "]" << " url:" << url << "_evaluatesPending:" << _evaluatesPending; | |
| 739 | 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 | |
| 764 | 914 | void 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 | 922 | QStringList urls; |
| 766 | 923 | urls.append(includeFile); |
| 767 | 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 | |
| 770 | 930 | void 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 | 938 | QUrl url = resolvePath(loadFile); |
| 773 | − qDebug() << "ScriptEngine::load() " << getFilename() << "[" << ((void*)this) << "]" << "line:" << __LINE__; | |
| 774 | 939 | emit loadScript(url.toString(), false); |
| 775 | − qDebug() << "ScriptEngine::load() " << getFilename() << "[" << ((void*)this) << "]" << "line:" << __LINE__; | |
| 776 | 940 | } |
| 777 | 941 | |
| 778 | 942 | void ScriptEngine::nodeKilled(SharedNodePointer node) { |