Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 4 files). Show all
Unified · Split
| @@ -94,33 +94,78 @@ 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 | + _allScriptsMutex.lock(); | |
| 97 | 99 | _allKnownScriptEngines.insert(this); |
| 100 | + _allScriptsMutex.unlock(); | |
| 101 | + qDebug() << "ScriptEngine::ScriptEngine() " << getFilename() << " -------- DONE -----------"; | |
| 98 | 102 | } |
| 99 | 103 | |
| 100 | 104 | ScriptEngine::~ScriptEngine() { |
| 101 | − _allKnownScriptEngines.remove(this); | |
| 105 | + qDebug() << "ScriptEngine::~ScriptEngine() " << getFilename() << "[" << ((void*)this) << "]" << " -------- BEGIN -----------"; | |
| 106 | + if (!_stoppingAllScripts) { | |
| 107 | + _allScriptsMutex.lock(); | |
| 108 | + qDebug() << "ScriptEngine::~ScriptEngine() " << getFilename() << " removing from list of scripts"; | |
| 109 | + _allKnownScriptEngines.remove(this); | |
| 110 | + _allScriptsMutex.unlock(); | |
| 111 | + } else { | |
| 112 | + qDebug() << "ScriptEngine::~ScriptEngine() " << getFilename() << " NOT removing from list of scripts here..."; | |
| 113 | + } | |
| 114 | + qDebug() << "ScriptEngine::~ScriptEngine() " << getFilename() << " -------- DONE -----------"; | |
| 102 | 115 | } |
| 103 | 116 | |
| 104 | 117 | QSet<ScriptEngine*> ScriptEngine::_allKnownScriptEngines; |
| 118 | +QMutex ScriptEngine::_allScriptsMutex; | |
| 119 | +bool ScriptEngine::_stoppingAllScripts = false; | |
| 105 | 120 | |
| 106 | 121 | void ScriptEngine::stopAllScripts(QObject* application) { |
| 122 | + | |
| 123 | + qDebug() << "ScriptEngine::stopAllScripts() -------- BEGIN -----------"; | |
| 124 | + _allScriptsMutex.lock(); | |
| 125 | + _stoppingAllScripts = true; | |
| 126 | + | |
| 107 | 127 | QMutableSetIterator<ScriptEngine*> i(_allKnownScriptEngines); |
| 108 | 128 | while (i.hasNext()) { |
| 109 | 129 | ScriptEngine* scriptEngine = i.next(); |
| 110 | 130 | |
| 131 | + qDebug() << "ScriptEngine::stopAllScripts() scriptEngine: " << (void*)scriptEngine; | |
| 132 | + | |
| 133 | + QString scriptName = scriptEngine->getFilename(); | |
| 134 | + | |
| 135 | + qDebug() << "ScriptEngine::stopAllScripts() considering script: " << scriptName << "evaluatePending:" << scriptEngine->evaluatePending(); | |
| 136 | + | |
| 111 | 137 | // NOTE: typically all script engines are running. But there's at least one known exception to this, the |
| 112 | 138 | // "entities sandbox" which is only used to evaluate entities scripts to test their validity before using |
| 113 | 139 | // them. We don't need to stop scripts that aren't running. |
| 114 | 140 | if (scriptEngine->isRunning()) { |
| 141 | + 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...."; | |
| 147 | + } | |
| 148 | + | |
| 149 | + qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__; | |
| 150 | + | |
| 115 | 151 | QEventLoop loop; |
| 116 | 152 | QObject::connect(scriptEngine, &ScriptEngine::doneRunning, &loop, &QEventLoop::quit); |
| 117 | − | |
| 153 | + | |
| 154 | + qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__; | |
| 118 | 155 | scriptEngine->disconnect(application); |
| 156 | + qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__; | |
| 119 | 157 | scriptEngine->stop(); |
| 120 | − | |
| 158 | + qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__; | |
| 121 | 159 | loop.exec(); |
| 160 | + qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__; | |
| 161 | + i.remove(); | |
| 162 | + } else { | |
| 163 | + qDebug() << "ScriptEngine::stopAllScripts() script: " << scriptName << " was not running... skipping it's stop() call."; | |
| 122 | 164 | } |
| 123 | 165 | } |
| 166 | + _stoppingAllScripts = false; | |
| 167 | + _allScriptsMutex.unlock(); | |
| 168 | + qDebug() << "ScriptEngine::stopAllScripts() -------- DONE -----------"; | |
| 124 | 169 | } |
| 125 | 170 | |
| 126 | 171 | QString ScriptEngine::getFilename() const { |
| @@ -347,6 +392,8 @@ void ScriptEngine::evaluate() { · 220 unmodified lines | ||
| 347 | 392 | } |
| 348 | 393 | |
| 349 | 394 | QScriptValue ScriptEngine::evaluate(const QString& program, const QString& fileName, int lineNumber) { |
| 395 | + qDebug() << "ScriptEngine::evaluate() " << getFilename() << " line:" << __LINE__; | |
| 396 | + _evaluatePending = true; | |
| 350 | 397 | QScriptValue result = QScriptEngine::evaluate(program, fileName, lineNumber); |
| 351 | 398 | if (hasUncaughtException()) { |
| 352 | 399 | int line = uncaughtExceptionLineNumber(); |
| @@ -354,6 +401,8 @@ QScriptValue ScriptEngine::evaluate(const QString& program, const QString& fileN · 1 unmodified line | ||
| 354 | 401 | } |
| 355 | 402 | emit evaluationFinished(result, hasUncaughtException()); |
| 356 | 403 | clearExceptions(); |
| 404 | + _evaluatePending = false; | |
| 405 | + qDebug() << "ScriptEngine::evaluate() " << getFilename() << " line:" << __LINE__; | |
| 357 | 406 | return result; |
| 358 | 407 | } |
| 359 | 408 | |
| @@ -370,30 +419,36 @@ void ScriptEngine::sendAvatarBillboardPacket() { · 10 unmodified lines | ||
| 370 | 419 | } |
| 371 | 420 | |
| 372 | 421 | void ScriptEngine::run() { |
| 422 | + qDebug() << "ScriptEngine::run() " << getFilename() << " -------- BEGIN -----------"; | |
| 423 | + | |
| 373 | 424 | if (!_isInitialized) { |
| 425 | + qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__; | |
| 374 | 426 | init(); |
| 427 | + qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__; | |
| 375 | 428 | } |
| 429 | + qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__; | |
| 376 | 430 | _isRunning = true; |
| 377 | 431 | _isFinished = false; |
| 432 | + qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__; | |
| 378 | 433 | emit runningStateChanged(); |
| 434 | + qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__; | |
| 379 | 435 | |
| 436 | + qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__; | |
| 380 | 437 | QScriptValue result = evaluate(_scriptContents); |
| 381 | − if (hasUncaughtException()) { | |
| 382 | − int line = uncaughtExceptionLineNumber(); | |
| 383 | − qDebug() << "Uncaught exception at (" << _fileNameString << ") line" << line << ":" << result.toString(); | |
| 384 | − emit errorMessage("Uncaught exception at (" + _fileNameString + ") line" + QString::number(line) + ":" + result.toString()); | |
| 385 | − clearExceptions(); | |
| 386 | − } | |
| 438 | + qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__; | |
| 387 | 439 | |
| 440 | + qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__; | |
| 388 | 441 | QElapsedTimer startTime; |
| 389 | 442 | startTime.start(); |
| 390 | 443 | |
| 391 | 444 | int thisFrame = 0; |
| 392 | 445 | |
| 446 | + qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__; | |
| 393 | 447 | auto nodeList = DependencyManager::get<NodeList>(); |
| 394 | 448 | |
| 395 | 449 | qint64 lastUpdate = usecTimestampNow(); |
| 396 | 450 | |
| 451 | + qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__; | |
| 397 | 452 | while (!_isFinished) { |
| 398 | 453 | int usecToSleep = (thisFrame++ * SCRIPT_DATA_CALLBACK_USECS) - startTime.nsecsElapsed() / 1000; // nsec to usec |
| 399 | 454 | if (usecToSleep > 0) { |
| @@ -535,6 +590,7 @@ void ScriptEngine::run() { · 135 unmodified lines | ||
| 535 | 590 | } |
| 536 | 591 | lastUpdate = now; |
| 537 | 592 | } |
| 593 | + qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__; | |
| 538 | 594 | |
| 539 | 595 | stopAllTimers(); // make sure all our timers are stopped if the script is ending |
| 540 | 596 | |
| @@ -565,6 +621,7 @@ void ScriptEngine::run() { · 24 unmodified lines | ||
| 565 | 621 | emit runningStateChanged(); |
| 566 | 622 | |
| 567 | 623 | emit doneRunning(); |
| 624 | + qDebug() << "ScriptEngine::run() " << getFilename() << " -------- DONE -----------" << " line:" << __LINE__; | |
| 568 | 625 | } |
| 569 | 626 | |
| 570 | 627 | // NOTE: This is private because it must be called on the same thread that created the timers, which is why |
| @@ -579,6 +636,7 @@ void ScriptEngine::stopAllTimers() { · 8 unmodified lines | ||
| 579 | 636 | } |
| 580 | 637 | |
| 581 | 638 | void ScriptEngine::stop() { |
| 639 | + qDebug() << "ScriptEngine::stop() " << getFilename() << "[" << ((void*)this) << "]"; | |
| 582 | 640 | _isFinished = true; |
| 583 | 641 | emit runningStateChanged(); |
| 584 | 642 | } |
| @@ -710,8 +768,11 @@ void ScriptEngine::include(const QString& includeFile, QScriptValue callback) { · 125 unmodified lines | ||
| 710 | 768 | } |
| 711 | 769 | |
| 712 | 770 | void ScriptEngine::load(const QString& loadFile) { |
| 771 | + qDebug() << "ScriptEngine::load() " << getFilename() << "[" << ((void*)this) << "]" << "line:" << __LINE__; | |
| 713 | 772 | QUrl url = resolvePath(loadFile); |
| 773 | + qDebug() << "ScriptEngine::load() " << getFilename() << "[" << ((void*)this) << "]" << "line:" << __LINE__; | |
| 714 | 774 | emit loadScript(url.toString(), false); |
| 775 | + qDebug() << "ScriptEngine::load() " << getFilename() << "[" << ((void*)this) << "]" << "line:" << __LINE__; | |
| 715 | 776 | } |
| 716 | 777 | |
| 717 | 778 | void ScriptEngine::nodeKilled(SharedNodePointer node) { |