Showing the changes to libraries/script-engine/src/ScriptEngines.cpp (1 of 260 files). Show all
Unified · Split
| @@ -12,6 +12,7 @@ · 11 unmodified lines | ||
| 12 | 12 | #include <QtCore/QSharedPointer> |
| 13 | 13 | |
| 14 | 14 | #include <QtWidgets/QApplication> |
| 15 | +#include <QtCore/QThread> | |
| 15 | 16 | |
| 16 | 17 | #include <shared/QtHelpers.h> |
| 17 | 18 | #include <SettingHandle.h> |
| @@ -19,6 +20,7 @@ · 1 unmodified line | ||
| 19 | 20 | #include <PathUtils.h> |
| 20 | 21 | #include <shared/FileUtils.h> |
| 21 | 22 | |
| 23 | +#include "ScriptCache.h" | |
| 22 | 24 | #include "ScriptEngine.h" |
| 23 | 25 | #include "ScriptEngineLogging.h" |
| 24 | 26 | |
| @@ -65,7 +67,7 @@ void ScriptEngines::onErrorLoadingScript(const QString& url) { · 40 unmodified lines | ||
| 65 | 67 | emit errorLoadingScript(url); |
| 66 | 68 | } |
| 67 | 69 | |
| 68 | −ScriptEngines::ScriptEngines(ScriptEngine::Context context, const QUrl& defaultScriptsOverride) | |
| 70 | +ScriptEngines::ScriptEngines(ScriptManager::Context context, const QUrl& defaultScriptsOverride) | |
| 69 | 71 | : _context(context), _defaultScriptsOverride(defaultScriptsOverride) |
| 70 | 72 | { |
| 71 | 73 | scriptGatekeeper.initialize(); |
| @@ -138,53 +140,53 @@ QUrl expandScriptUrl(const QUrl& rawScriptURL) { · 66 unmodified lines | ||
| 138 | 140 | |
| 139 | 141 | QObject* scriptsModel(); |
| 140 | 142 | |
| 141 | −void ScriptEngines::addScriptEngine(ScriptEnginePointer engine) { | |
| 143 | +void ScriptEngines::addScriptEngine(ScriptManagerPointer manager) { | |
| 142 | 144 | if (!_isStopped) { |
| 143 | 145 | QMutexLocker locker(&_allScriptsMutex); |
| 144 | − _allKnownScriptEngines.insert(engine); | |
| 146 | + _allKnownScriptManagers.insert(manager); | |
| 145 | 147 | } |
| 146 | 148 | } |
| 147 | 149 | |
| 148 | −void ScriptEngines::removeScriptEngine(ScriptEnginePointer engine) { | |
| 150 | +void ScriptEngines::removeScriptEngine(ScriptManagerPointer manager) { | |
| 149 | 151 | // If we're not already in the middle of stopping all scripts, then we should remove ourselves |
| 150 | 152 | // from the list of running scripts. We don't do this if we're in the process of stopping all scripts |
| 151 | 153 | // because that method removes scripts from its list as it iterates them |
| 152 | 154 | if (!_isStopped) { |
| 153 | 155 | QMutexLocker locker(&_allScriptsMutex); |
| 154 | − _allKnownScriptEngines.remove(engine); | |
| 156 | + _allKnownScriptManagers.remove(manager); | |
| 155 | 157 | } |
| 156 | 158 | } |
| 157 | 159 | |
| 158 | 160 | void ScriptEngines::shutdownScripting() { |
| 159 | 161 | _isStopped = true; |
| 160 | 162 | QMutexLocker locker(&_allScriptsMutex); |
| 161 | − qCDebug(scriptengine) << "Stopping all scripts.... currently known scripts:" << _allKnownScriptEngines.size(); | |
| 163 | + qCDebug(scriptengine) << "Stopping all scripts.... currently known scripts:" << _allKnownScriptManagers.size(); | |
| 162 | 164 | |
| 163 | − QMutableSetIterator<ScriptEnginePointer> i(_allKnownScriptEngines); | |
| 165 | + QMutableSetIterator<ScriptManagerPointer> i(_allKnownScriptManagers); | |
| 164 | 166 | while (i.hasNext()) { |
| 165 | − ScriptEnginePointer scriptEngine = i.next(); | |
| 166 | − QString scriptName = scriptEngine->getFilename(); | |
| 167 | + ScriptManagerPointer scriptManager = i.next(); | |
| 168 | + QString scriptName = scriptManager->getFilename(); | |
| 167 | 169 | |
| 168 | 170 | // NOTE: typically all script engines are running. But there's at least one known exception to this, the |
| 169 | 171 | // "entities sandbox" which is only used to evaluate entities scripts to test their validity before using |
| 170 | 172 | // them. We don't need to stop scripts that aren't running. |
| 171 | 173 | // TODO: Scripts could be shut down faster if we spread them across a threadpool. |
| 172 | − if (scriptEngine->isRunning()) { | |
| 174 | + if (scriptManager->isRunning()) { | |
| 173 | 175 | qCDebug(scriptengine) << "about to shutdown script:" << scriptName; |
| 174 | 176 | |
| 175 | 177 | // We disconnect any script engine signals from the application because we don't want to do any |
| 176 | 178 | // extra stopScript/loadScript processing that the Application normally does when scripts start |
| 177 | 179 | // and stop. We can safely short circuit this because we know we're in the "quitting" process |
| 178 | − scriptEngine->disconnect(this); | |
| 180 | + scriptManager->disconnect(this); | |
| 179 | 181 | |
| 180 | 182 | // Gracefully stop the engine's scripting thread |
| 181 | − scriptEngine->stop(); | |
| 183 | + scriptManager->stop(); | |
| 182 | 184 | |
| 183 | 185 | // We need to wait for the engine to be done running before we proceed, because we don't |
| 184 | 186 | // want any of the scripts final "scriptEnding()" or pending "update()" methods from accessing |
| 185 | 187 | // any application state after we leave this stopAllScripts() method |
| 186 | 188 | qCDebug(scriptengine) << "waiting on script:" << scriptName; |
| 187 | − scriptEngine->waitTillDoneRunning(true); | |
| 189 | + scriptManager->waitTillDoneRunning(true); | |
| 188 | 190 | qCDebug(scriptengine) << "done waiting on script:" << scriptName; |
| 189 | 191 | } |
| 190 | 192 | // Once the script is stopped, we can remove it from our set |
| @@ -372,8 +374,8 @@ void ScriptEngines::saveScripts() { · 181 unmodified lines | ||
| 372 | 374 | QVariantList list; |
| 373 | 375 | |
| 374 | 376 | { |
| 375 | − QReadLocker lock(&_scriptEnginesHashLock); | |
| 376 | − for (auto it = _scriptEnginesHash.begin(); it != _scriptEnginesHash.end(); ++it) { | |
| 377 | + QReadLocker lock(&_scriptManagersHashLock); | |
| 378 | + for (auto it = _scriptManagersHash.begin(); it != _scriptManagersHash.end(); ++it) { | |
| 377 | 379 | // Save user-loaded scripts, only if they are set to quit when finished |
| 378 | 380 | if (it.value() && it.value()->isUserLoaded() && !it.value()->isQuitWhenFinished()) { |
| 379 | 381 | auto normalizedUrl = normalizeScriptURL(it.key()); |
| @@ -390,8 +392,8 @@ void ScriptEngines::saveScripts() { · 10 unmodified lines | ||
| 390 | 392 | } |
| 391 | 393 | |
| 392 | 394 | QStringList ScriptEngines::getRunningScripts() { |
| 393 | − QReadLocker lock(&_scriptEnginesHashLock); | |
| 394 | − QList<QUrl> urls = _scriptEnginesHash.keys(); | |
| 395 | + QReadLocker lock(&_scriptManagersHashLock); | |
| 396 | + QList<QUrl> urls = _scriptManagersHash.keys(); | |
| 395 | 397 | QStringList result; |
| 396 | 398 | for (auto url : urls) { |
| 397 | 399 | result.append(url.toString()); |
| @@ -400,33 +402,33 @@ QStringList ScriptEngines::getRunningScripts() { · 2 unmodified lines | ||
| 400 | 402 | } |
| 401 | 403 | |
| 402 | 404 | void ScriptEngines::stopAllScripts(bool restart) { |
| 403 | − QReadLocker lock(&_scriptEnginesHashLock); | |
| 405 | + QReadLocker lock(&_scriptManagersHashLock); | |
| 404 | 406 | |
| 405 | 407 | if (_isReloading) { |
| 406 | 408 | return; |
| 407 | 409 | } |
| 408 | 410 | |
| 409 | − for (QHash<QUrl, ScriptEnginePointer>::const_iterator it = _scriptEnginesHash.constBegin(); | |
| 410 | − it != _scriptEnginesHash.constEnd(); it++) { | |
| 411 | − ScriptEnginePointer scriptEngine = it.value(); | |
| 411 | + for (QHash<QUrl, ScriptManagerPointer>::const_iterator it = _scriptManagersHash.constBegin(); | |
| 412 | + it != _scriptManagersHash.constEnd(); it++) { | |
| 413 | + ScriptManagerPointer scriptManager = it.value(); | |
| 412 | 414 | // skip already stopped scripts |
| 413 | − if (scriptEngine->isFinished() || scriptEngine->isStopping()) { | |
| 415 | + if (scriptManager->isFinished() || scriptManager->isStopping()) { | |
| 414 | 416 | continue; |
| 415 | 417 | } |
| 416 | 418 | |
| 417 | 419 | bool isOverrideScript = it.key().toString().compare(this->_defaultScriptsOverride.toString()) == 0; |
| 418 | 420 | // queue user scripts if restarting |
| 419 | − if (restart && (scriptEngine->isUserLoaded() || isOverrideScript)) { | |
| 421 | + if (restart && (scriptManager->isUserLoaded() || isOverrideScript)) { | |
| 420 | 422 | _isReloading = true; |
| 421 | − ScriptEngine::Type type = scriptEngine->getType(); | |
| 423 | + ScriptManager::Type type = scriptManager->getType(); | |
| 422 | 424 | |
| 423 | − connect(scriptEngine.data(), &ScriptEngine::finished, this, [this, type, isOverrideScript] (QString scriptName) { | |
| 425 | + connect(scriptManager.data(), &ScriptManager::finished, this, [this, type, isOverrideScript](QString scriptName) { | |
| 424 | 426 | reloadScript(scriptName, !isOverrideScript)->setType(type); |
| 425 | 427 | }); |
| 426 | 428 | } |
| 427 | 429 | |
| 428 | 430 | // stop all scripts |
| 429 | − scriptEngine->stop(); | |
| 431 | + scriptManager->stop(); | |
| 430 | 432 | } |
| 431 | 433 | |
| 432 | 434 | if (restart) { |
| @@ -446,23 +448,23 @@ bool ScriptEngines::stopScript(const QString& rawScriptURL, bool restart) { · 13 unmodified lines | ||
| 446 | 448 | scriptURL = normalizeScriptURL(QUrl::fromLocalFile(rawScriptURL)); |
| 447 | 449 | } |
| 448 | 450 | |
| 449 | − QReadLocker lock(&_scriptEnginesHashLock); | |
| 450 | − if (_scriptEnginesHash.contains(scriptURL)) { | |
| 451 | − ScriptEnginePointer scriptEngine = _scriptEnginesHash.value(scriptURL); | |
| 451 | + QReadLocker lock(&_scriptManagersHashLock); | |
| 452 | + if (_scriptManagersHash.contains(scriptURL)) { | |
| 453 | + ScriptManagerPointer scriptManager = _scriptManagersHash.value(scriptURL); | |
| 452 | 454 | if (restart) { |
| 453 | − bool isUserLoaded = scriptEngine->isUserLoaded(); | |
| 454 | − ScriptEngine::Type type = scriptEngine->getType(); | |
| 455 | + bool isUserLoaded = scriptManager->isUserLoaded(); | |
| 456 | + ScriptManager::Type type = scriptManager->getType(); | |
| 455 | 457 | auto scriptCache = DependencyManager::get<ScriptCache>(); |
| 456 | 458 | scriptCache->deleteScript(scriptURL); |
| 457 | 459 | |
| 458 | − if (!scriptEngine->isStopping()) { | |
| 459 | − connect(scriptEngine.data(), &ScriptEngine::finished, | |
| 460 | − this, [this, isUserLoaded, type](QString scriptName, ScriptEnginePointer engine) { | |
| 460 | + if (!scriptManager->isStopping()) { | |
| 461 | + connect(scriptManager.data(), &ScriptManager::finished, | |
| 462 | + this, [this, isUserLoaded, type](QString scriptName, ScriptManagerPointer manager) { | |
| 461 | 463 | reloadScript(scriptName, isUserLoaded)->setType(type); |
| 462 | 464 | }); |
| 463 | 465 | } |
| 464 | 466 | } |
| 465 | − scriptEngine->stop(); | |
| 467 | + scriptManager->stop(); | |
| 466 | 468 | stoppedScript = true; |
| 467 | 469 | } |
| 468 | 470 | } |
| @@ -480,11 +482,11 @@ void ScriptEngines::reloadAllScripts() { · 11 unmodified lines | ||
| 480 | 482 | stopAllScripts(true); |
| 481 | 483 | } |
| 482 | 484 | |
| 483 | −ScriptEnginePointer ScriptEngines::loadScript(const QUrl& scriptFilename, bool isUserLoaded, bool loadScriptFromEditor, | |
| 485 | +ScriptManagerPointer ScriptEngines::loadScript(const QUrl& scriptFilename, bool isUserLoaded, bool loadScriptFromEditor, | |
| 484 | 486 | bool activateMainWindow, bool reload, bool quitWhenFinished) { |
| 485 | 487 | if (thread() != QThread::currentThread()) { |
| 486 | − ScriptEnginePointer result { nullptr }; | |
| 487 | − BLOCKING_INVOKE_METHOD(this, "loadScript", Q_RETURN_ARG(ScriptEnginePointer, result), | |
| 488 | + ScriptManagerPointer result { nullptr }; | |
| 489 | + BLOCKING_INVOKE_METHOD(this, "loadScript", Q_RETURN_ARG(ScriptManagerPointer, result), | |
| 488 | 490 | Q_ARG(QUrl, scriptFilename), |
| 489 | 491 | Q_ARG(bool, isUserLoaded), |
| 490 | 492 | Q_ARG(bool, loadScriptFromEditor), |
| @@ -508,41 +510,41 @@ ScriptEnginePointer ScriptEngines::loadScript(const QUrl& scriptFilename, bool i · 17 unmodified lines | ||
| 508 | 510 | |
| 509 | 511 | scriptUrl = QUrl(FileUtils::selectFile(scriptUrl.toString())); |
| 510 | 512 | |
| 511 | − auto scriptEngine = getScriptEngine(scriptUrl); | |
| 512 | − if (scriptEngine && !scriptEngine->isStopping()) { | |
| 513 | − return scriptEngine; | |
| 513 | + auto scriptManager = getScriptEngine(scriptUrl); | |
| 514 | + if (scriptManager && !scriptManager->isStopping()) { | |
| 515 | + return scriptManager; | |
| 514 | 516 | } |
| 515 | 517 | |
| 516 | − scriptEngine = scriptEngineFactory(_context, NO_SCRIPT, "about:" + scriptFilename.fileName()); | |
| 517 | − scriptEngine->setUserLoaded(isUserLoaded); | |
| 518 | − scriptEngine->setQuitWhenFinished(quitWhenFinished); | |
| 518 | + scriptManager = scriptManagerFactory(_context, NO_SCRIPT, "about:" + scriptFilename.fileName()); | |
| 519 | + scriptManager->setUserLoaded(isUserLoaded); | |
| 520 | + scriptManager->setQuitWhenFinished(quitWhenFinished); | |
| 519 | 521 | |
| 520 | 522 | if (scriptFilename.isEmpty() || !scriptUrl.isValid()) { |
| 521 | − launchScriptEngine(scriptEngine); | |
| 523 | + launchScriptEngine(scriptManager); | |
| 522 | 524 | } else { |
| 523 | 525 | // connect to the appropriate signals of this script engine |
| 524 | − connect(scriptEngine.data(), &ScriptEngine::scriptLoaded, this, &ScriptEngines::onScriptEngineLoaded); | |
| 525 | − connect(scriptEngine.data(), &ScriptEngine::errorLoadingScript, this, &ScriptEngines::onScriptEngineError); | |
| 526 | + connect(scriptManager.data(), &ScriptManager::scriptLoaded, this, &ScriptEngines::onScriptEngineLoaded); | |
| 527 | + connect(scriptManager.data(), &ScriptManager::errorLoadingScript, this, &ScriptEngines::onScriptEngineError); | |
| 526 | 528 | |
| 527 | 529 | // Shutdown Interface when script finishes, if requested |
| 528 | 530 | if (quitWhenFinished) { |
| 529 | − connect(scriptEngine.data(), &ScriptEngine::finished, this, &ScriptEngines::quitWhenFinished); | |
| 531 | + connect(scriptManager.data(), &ScriptManager::finished, this, &ScriptEngines::quitWhenFinished); | |
| 530 | 532 | } |
| 531 | 533 | |
| 532 | 534 | // get the script engine object to load the script at the designated script URL |
| 533 | − scriptEngine->loadURL(scriptUrl, reload); | |
| 535 | + scriptManager->loadURL(scriptUrl, reload); | |
| 534 | 536 | } |
| 535 | 537 | |
| 536 | − return scriptEngine; | |
| 538 | + return scriptManager; | |
| 537 | 539 | } |
| 538 | 540 | |
| 539 | −ScriptEnginePointer ScriptEngines::getScriptEngine(const QUrl& rawScriptURL) { | |
| 540 | − ScriptEnginePointer result; | |
| 541 | +ScriptManagerPointer ScriptEngines::getScriptEngine(const QUrl& rawScriptURL) { | |
| 542 | + ScriptManagerPointer result; | |
| 541 | 543 | { |
| 542 | − QReadLocker lock(&_scriptEnginesHashLock); | |
| 544 | + QReadLocker lock(&_scriptManagersHashLock); | |
| 543 | 545 | const QUrl scriptURL = normalizeScriptURL(rawScriptURL); |
| 544 | − auto it = _scriptEnginesHash.find(scriptURL); | |
| 545 | − if (it != _scriptEnginesHash.end()) { | |
| 546 | + auto it = _scriptManagersHash.find(scriptURL); | |
| 547 | + if (it != _scriptManagersHash.end()) { | |
| 546 | 548 | result = it.value(); |
| 547 | 549 | } |
| 548 | 550 | } |
| @@ -552,16 +554,15 @@ ScriptEnginePointer ScriptEngines::getScriptEngine(const QUrl& rawScriptURL) { · 3 unmodified lines | ||
| 552 | 554 | // FIXME - change to new version of ScriptCache loading notification |
| 553 | 555 | void ScriptEngines::onScriptEngineLoaded(const QString& rawScriptURL) { |
| 554 | 556 | UserActivityLogger::getInstance().loadedScript(rawScriptURL); |
| 555 | − QSharedPointer<BaseScriptEngine> baseScriptEngine = qobject_cast<ScriptEngine*>(sender())->sharedFromThis(); | |
| 556 | − ScriptEnginePointer scriptEngine = qSharedPointerCast<ScriptEngine>(baseScriptEngine); | |
| 557 | + ScriptManagerPointer scriptEngine = qobject_cast<ScriptManager*>(sender())->sharedFromThis(); | |
| 557 | 558 | |
| 558 | 559 | launchScriptEngine(scriptEngine); |
| 559 | 560 | |
| 560 | 561 | { |
| 561 | − QWriteLocker lock(&_scriptEnginesHashLock); | |
| 562 | + QWriteLocker lock(&_scriptManagersHashLock); | |
| 562 | 563 | QUrl url = QUrl(rawScriptURL); |
| 563 | 564 | QUrl normalized = normalizeScriptURL(url); |
| 564 | − _scriptEnginesHash.insert(normalized, scriptEngine); | |
| 565 | + _scriptManagersHash.insert(normalized, scriptEngine); | |
| 565 | 566 | } |
| 566 | 567 | |
| 567 | 568 | // Update settings with new script |
| @@ -573,40 +574,40 @@ void ScriptEngines::quitWhenFinished() { · 5 unmodified lines | ||
| 573 | 574 | qApp->quit(); |
| 574 | 575 | } |
| 575 | 576 | |
| 576 | −int ScriptEngines::runScriptInitializers(ScriptEnginePointer scriptEngine) { | |
| 577 | − auto nativeCount = DependencyManager::get<ScriptInitializers>()->runScriptInitializers(scriptEngine.data()); | |
| 578 | − return nativeCount + ScriptInitializerMixin<ScriptEnginePointer>::runScriptInitializers(scriptEngine); | |
| 577 | +int ScriptEngines::runScriptInitializers(ScriptManagerPointer scriptManager) { | |
| 578 | + auto nativeCount = DependencyManager::get<ScriptInitializers>()->runScriptInitializers(scriptManager->engine().data()); | |
| 579 | + return nativeCount + ScriptInitializerMixin<ScriptManagerPointer>::runScriptInitializers(scriptManager); | |
| 579 | 580 | } |
| 580 | 581 | |
| 581 | −void ScriptEngines::launchScriptEngine(ScriptEnginePointer scriptEngine) { | |
| 582 | − connect(scriptEngine.data(), &ScriptEngine::finished, this, &ScriptEngines::onScriptFinished, Qt::DirectConnection); | |
| 583 | − connect(scriptEngine.data(), &ScriptEngine::loadScript, [this](const QString& scriptName, bool userLoaded) { | |
| 582 | +void ScriptEngines::launchScriptEngine(ScriptManagerPointer scriptManager) { | |
| 583 | + connect(scriptManager.data(), &ScriptManager::finished, this, &ScriptEngines::onScriptFinished, Qt::DirectConnection); | |
| 584 | + connect(scriptManager.data(), &ScriptManager::loadScript, [this](const QString& scriptName, bool userLoaded) { | |
| 584 | 585 | loadScript(scriptName, userLoaded); |
| 585 | 586 | }); |
| 586 | − connect(scriptEngine.data(), &ScriptEngine::reloadScript, [this](const QString& scriptName, bool userLoaded) { | |
| 587 | + connect(scriptManager.data(), &ScriptManager::reloadScript, [this](const QString& scriptName, bool userLoaded) { | |
| 587 | 588 | loadScript(scriptName, userLoaded, false, false, true); |
| 588 | 589 | }); |
| 589 | 590 | |
| 590 | 591 | // register our application services and set it off on its own thread |
| 591 | − runScriptInitializers(scriptEngine); | |
| 592 | − scriptEngine->runInThread(); | |
| 592 | + runScriptInitializers(scriptManager); | |
| 593 | + scriptManager->runInThread(); | |
| 593 | 594 | } |
| 594 | 595 | |
| 595 | −void ScriptEngines::onScriptFinished(const QString& rawScriptURL, ScriptEnginePointer engine) { | |
| 596 | +void ScriptEngines::onScriptFinished(const QString& rawScriptURL, ScriptManagerPointer manager) { | |
| 596 | 597 | bool removed = false; |
| 597 | 598 | { |
| 598 | − QWriteLocker lock(&_scriptEnginesHashLock); | |
| 599 | + QWriteLocker lock(&_scriptManagersHashLock); | |
| 599 | 600 | const QUrl scriptURL = normalizeScriptURL(QUrl(rawScriptURL)); |
| 600 | − for (auto it = _scriptEnginesHash.find(scriptURL); it != _scriptEnginesHash.end(); ++it) { | |
| 601 | − if (it.value() == engine) { | |
| 602 | − _scriptEnginesHash.erase(it); | |
| 601 | + for (auto it = _scriptManagersHash.find(scriptURL); it != _scriptManagersHash.end(); ++it) { | |
| 602 | + if (it.value() == manager) { | |
| 603 | + _scriptManagersHash.erase(it); | |
| 603 | 604 | removed = true; |
| 604 | 605 | break; |
| 605 | 606 | } |
| 606 | 607 | } |
| 607 | 608 | } |
| 608 | 609 | |
| 609 | − removeScriptEngine(engine); | |
| 610 | + removeScriptEngine(manager); | |
| 610 | 611 | |
| 611 | 612 | if (removed && !_isReloading) { |
| 612 | 613 | // Update settings with removed script |