Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 99 files). Show all
Unified · Split
| @@ -24,6 +24,8 @@ · 23 unmodified lines | |||
| 24 | 24 | ||
| 25 | #include <QtWidgets/QMainWindow> | 25 | #include <QtWidgets/QMainWindow> |
| 26 | #include <QtWidgets/QApplication> | 26 | #include <QtWidgets/QApplication> |
| 27 | #include <QtWidgets/QMenuBar> | ||
| 28 | #include <QtWidgets/QMenu> | ||
| 27 | 29 | ||
| 28 | #include <QtNetwork/QNetworkRequest> | 30 | #include <QtNetwork/QNetworkRequest> |
| 29 | #include <QtNetwork/QNetworkReply> | 31 | #include <QtNetwork/QNetworkReply> |
| @@ -48,7 +50,6 @@ · 18 unmodified lines | |||
| 48 | #include <ScriptAvatarData.h> | 50 | #include <ScriptAvatarData.h> |
| 49 | #include <udt/PacketHeaders.h> | 51 | #include <udt/PacketHeaders.h> |
| 50 | #include <UUID.h> | 52 | #include <UUID.h> |
| 51 | #include <ui/Menu.h> | ||
| 52 | 53 | ||
| 53 | #include <controllers/ScriptingInterface.h> | 54 | #include <controllers/ScriptingInterface.h> |
| 54 | #include <AnimationObject.h> | 55 | #include <AnimationObject.h> |
| @@ -70,7 +71,6 @@ · 15 unmodified lines | |||
| 70 | #include "WebSocketClass.h" | 71 | #include "WebSocketClass.h" |
| 71 | #include "RecordingScriptingInterface.h" | 72 | #include "RecordingScriptingInterface.h" |
| 72 | #include "ScriptEngines.h" | 73 | #include "ScriptEngines.h" |
| 73 | #include "TabletScriptingInterface.h" | ||
| 74 | #include "ModelScriptingInterface.h" | 74 | #include "ModelScriptingInterface.h" |
| 75 | 75 | ||
| 76 | 76 | ||
| @@ -172,7 +172,7 @@ ScriptEngine::ScriptEngine(Context context, const QString& scriptContents, const · 95 unmodified lines | |||
| 172 | emit unhandledException(exception); | 172 | emit unhandledException(exception); |
| 173 | } | 173 | } |
| 174 | }, Qt::DirectConnection); | 174 | }, Qt::DirectConnection); |
| 175 | 175 | ||
| 176 | setProcessEventsInterval(MSECS_PER_SECOND); | 176 | setProcessEventsInterval(MSECS_PER_SECOND); |
| 177 | if (isEntityServerScript()) { | 177 | if (isEntityServerScript()) { |
| 178 | qCDebug(scriptengine) << "isEntityServerScript() -- limiting maxRetries to 1"; | 178 | qCDebug(scriptengine) << "isEntityServerScript() -- limiting maxRetries to 1"; |
| @@ -281,7 +281,7 @@ void ScriptEngine::runDebuggable() { · 102 unmodified lines | |||
| 281 | scriptDebugMenu = nullptr; | 281 | scriptDebugMenu = nullptr; |
| 282 | } | 282 | } |
| 283 | } | 283 | } |
| 284 | disconnect(timer); | 284 | disconnect(timer); |
| 285 | }); | 285 | }); |
| 286 | 286 | ||
| 287 | connect(timer, &QTimer::timeout, [this, timer] { | 287 | connect(timer, &QTimer::timeout, [this, timer] { |
| @@ -340,7 +340,7 @@ void ScriptEngine::runInThread() { · 52 unmodified lines | |||
| 340 | QThread* workerThread = new QThread(); | 340 | QThread* workerThread = new QThread(); |
| 341 | workerThread->setObjectName(QString("js:") + getFilename().replace("about:","")); | 341 | workerThread->setObjectName(QString("js:") + getFilename().replace("about:","")); |
| 342 | moveToThread(workerThread); | 342 | moveToThread(workerThread); |
| 343 | 343 | ||
| 344 | // NOTE: If you connect any essential signals for proper shutdown or cleanup of | 344 | // NOTE: If you connect any essential signals for proper shutdown or cleanup of |
| 345 | // the script engine, make sure to add code to "reconnect" them to the | 345 | // the script engine, make sure to add code to "reconnect" them to the |
| 346 | // disconnectNonEssentialSignals() method | 346 | // disconnectNonEssentialSignals() method |
| @@ -697,8 +697,6 @@ void ScriptEngine::init() { · 350 unmodified lines | |||
| 697 | // constants | 697 | // constants |
| 698 | globalObject().setProperty("TREE_SCALE", newVariant(QVariant(TREE_SCALE))); | 698 | globalObject().setProperty("TREE_SCALE", newVariant(QVariant(TREE_SCALE))); |
| 699 | 699 | ||
| 700 | registerGlobalObject("Tablet", DependencyManager::get<TabletScriptingInterface>().data()); | ||
| 701 | qScriptRegisterMetaType(this, tabletToScriptValue, tabletFromScriptValue); | ||
| 702 | registerGlobalObject("Assets", &_assetScriptingInterface); | 700 | registerGlobalObject("Assets", &_assetScriptingInterface); |
| 703 | registerGlobalObject("Resources", DependencyManager::get<ResourceScriptingInterface>().data()); | 701 | registerGlobalObject("Resources", DependencyManager::get<ResourceScriptingInterface>().data()); |
| 704 | 702 | ||
| @@ -1047,26 +1045,26 @@ void ScriptEngine::run() { · 342 unmodified lines | |||
| 1047 | auto beforeSleep = clock::now(); | 1045 | auto beforeSleep = clock::now(); |
| 1048 | 1046 | ||
| 1049 | // Throttle to SCRIPT_FPS | 1047 | // Throttle to SCRIPT_FPS |
| 1050 | // We'd like to try to keep the script at a solid SCRIPT_FPS update rate. And so we will | 1048 | // We'd like to try to keep the script at a solid SCRIPT_FPS update rate. And so we will |
| 1051 | // calculate a sleepUntil to be the time from our start time until the original target | 1049 | // calculate a sleepUntil to be the time from our start time until the original target |
| 1052 | // sleepUntil for this frame. This approach will allow us to "catch up" in the event | 1050 | // sleepUntil for this frame. This approach will allow us to "catch up" in the event |
| 1053 | // that some of our script udpates/frames take a little bit longer than the target average | 1051 | // that some of our script udpates/frames take a little bit longer than the target average |
| 1054 | // to execute. | 1052 | // to execute. |
| 1055 | // NOTE: if we go to variable SCRIPT_FPS, then we will need to reconsider this approach | 1053 | // NOTE: if we go to variable SCRIPT_FPS, then we will need to reconsider this approach |
| 1056 | const std::chrono::microseconds TARGET_SCRIPT_FRAME_DURATION(USECS_PER_SECOND / SCRIPT_FPS + 1); | 1054 | const std::chrono::microseconds TARGET_SCRIPT_FRAME_DURATION(USECS_PER_SECOND / SCRIPT_FPS + 1); |
| 1057 | clock::time_point targetSleepUntil(startTime + (thisFrame++ * TARGET_SCRIPT_FRAME_DURATION)); | 1055 | clock::time_point targetSleepUntil(startTime + (thisFrame++ * TARGET_SCRIPT_FRAME_DURATION)); |
| 1058 | 1056 | ||
| 1059 | // However, if our sleepUntil is not at least our average update and timer execution time | 1057 | // However, if our sleepUntil is not at least our average update and timer execution time |
| 1060 | // into the future it means our script is taking too long in its updates, and we want to | 1058 | // into the future it means our script is taking too long in its updates, and we want to |
| 1061 | // punish the script a little bit. So we will force the sleepUntil to be at least our | 1059 | // punish the script a little bit. So we will force the sleepUntil to be at least our |
| 1062 | // averageUpdate + averageTimerPerFrame time into the future. | 1060 | // averageUpdate + averageTimerPerFrame time into the future. |
| 1063 | auto averageUpdate = totalUpdates / thisFrame; | 1061 | auto averageUpdate = totalUpdates / thisFrame; |
| 1064 | auto averageTimerPerFrame = _totalTimerExecution / thisFrame; | 1062 | auto averageTimerPerFrame = _totalTimerExecution / thisFrame; |
| 1065 | auto averageTimerAndUpdate = averageUpdate + averageTimerPerFrame; | 1063 | auto averageTimerAndUpdate = averageUpdate + averageTimerPerFrame; |
| 1066 | auto sleepUntil = std::max(targetSleepUntil, beforeSleep + averageTimerAndUpdate); | 1064 | auto sleepUntil = std::max(targetSleepUntil, beforeSleep + averageTimerAndUpdate); |
| 1067 | 1065 | ||
| 1068 | // We don't want to actually sleep for too long, because it causes our scripts to hang | 1066 | // We don't want to actually sleep for too long, because it causes our scripts to hang |
| 1069 | // on shutdown and stop... so we want to loop and sleep until we've spent our time in | 1067 | // on shutdown and stop... so we want to loop and sleep until we've spent our time in |
| 1070 | // purgatory, constantly checking to see if our script was asked to end | 1068 | // purgatory, constantly checking to see if our script was asked to end |
| 1071 | bool processedEvents = false; | 1069 | bool processedEvents = false; |
| 1072 | while (!_isFinished && clock::now() < sleepUntil) { | 1070 | while (!_isFinished && clock::now() < sleepUntil) { |
| @@ -1414,7 +1412,7 @@ QString ScriptEngine::_requireResolve(const QString& moduleId, const QString& re · 341 unmodified lines | |||
| 1414 | if (!IS_THREADSAFE_INVOCATION(thread(), __FUNCTION__)) { | 1412 | if (!IS_THREADSAFE_INVOCATION(thread(), __FUNCTION__)) { |
| 1415 | return QString(); | 1413 | return QString(); |
| 1416 | } | 1414 | } |
| 1417 | QUrl defaultScriptsLoc = defaultScriptsLocation(); | 1415 | QUrl defaultScriptsLoc = PathUtils::defaultScriptsLocation(); |
| 1418 | QUrl url(moduleId); | 1416 | QUrl url(moduleId); |
| 1419 | 1417 | ||
| 1420 | auto displayId = moduleId; | 1418 | auto displayId = moduleId; |
| @@ -1480,7 +1478,7 @@ QString ScriptEngine::_requireResolve(const QString& moduleId, const QString& re · 59 unmodified lines | |||
| 1480 | canonical.setPath(file.canonicalFilePath()); | 1478 | canonical.setPath(file.canonicalFilePath()); |
| 1481 | } | 1479 | } |
| 1482 | 1480 | ||
| 1483 | bool disallowOutsideFiles = !defaultScriptsLocation().isParentOf(canonical) && !currentSandboxURL.isLocalFile(); | 1481 | bool disallowOutsideFiles = !PathUtils::defaultScriptsLocation().isParentOf(canonical) && !currentSandboxURL.isLocalFile(); |
| 1484 | if (disallowOutsideFiles && !PathUtils::isDescendantOf(canonical, currentSandboxURL)) { | 1482 | if (disallowOutsideFiles && !PathUtils::isDescendantOf(canonical, currentSandboxURL)) { |
| 1485 | return throwResolveError(makeError(message.arg( | 1483 | return throwResolveError(makeError(message.arg( |
| 1486 | QString("path '%1' outside of origin script '%2' '%3'") | 1484 | QString("path '%1' outside of origin script '%2' '%3'") |
| @@ -1765,7 +1763,7 @@ void ScriptEngine::include(const QStringList& includeFiles, QScriptValue callbac · 278 unmodified lines | |||
| 1765 | return; | 1763 | return; |
| 1766 | } | 1764 | } |
| 1767 | if (DependencyManager::get<ScriptEngines>()->isStopped()) { | 1765 | if (DependencyManager::get<ScriptEngines>()->isStopped()) { |
| 1768 | scriptWarningMessage("Script.include() while shutting down is ignored... includeFiles:" | 1766 | scriptWarningMessage("Script.include() while shutting down is ignored... includeFiles:" |
| 1769 | + includeFiles.join(",") + "parent script:" + getFilename()); | 1767 | + includeFiles.join(",") + "parent script:" + getFilename()); |
| 1770 | return; // bail early | 1768 | return; // bail early |
| 1771 | } | 1769 | } |
| @@ -1777,7 +1775,7 @@ void ScriptEngine::include(const QStringList& includeFiles, QScriptValue callbac · 5 unmodified lines | |||
| 1777 | bool isStandardLibrary = false; | 1775 | bool isStandardLibrary = false; |
| 1778 | if (file.startsWith("/~/")) { | 1776 | if (file.startsWith("/~/")) { |
| 1779 | thisURL = expandScriptUrl(QUrl::fromLocalFile(expandScriptPath(file))); | 1777 | thisURL = expandScriptUrl(QUrl::fromLocalFile(expandScriptPath(file))); |
| 1780 | QUrl defaultScriptsLoc = defaultScriptsLocation(); | 1778 | QUrl defaultScriptsLoc = PathUtils::defaultScriptsLocation(); |
| 1781 | if (!defaultScriptsLoc.isParentOf(thisURL)) { | 1779 | if (!defaultScriptsLoc.isParentOf(thisURL)) { |
| 1782 | scriptWarningMessage("Script.include() -- skipping" + file + "-- outside of standard libraries"); | 1780 | scriptWarningMessage("Script.include() -- skipping" + file + "-- outside of standard libraries"); |
| 1783 | continue; | 1781 | continue; |
| @@ -1789,7 +1787,7 @@ void ScriptEngine::include(const QStringList& includeFiles, QScriptValue callbac · 5 unmodified lines | |||
| 1789 | 1787 | ||
| 1790 | bool disallowOutsideFiles = thisURL.isLocalFile() && !isStandardLibrary && !currentSandboxURL.isLocalFile(); | 1788 | bool disallowOutsideFiles = thisURL.isLocalFile() && !isStandardLibrary && !currentSandboxURL.isLocalFile(); |
| 1791 | if (disallowOutsideFiles && !PathUtils::isDescendantOf(thisURL, currentSandboxURL)) { | 1789 | if (disallowOutsideFiles && !PathUtils::isDescendantOf(thisURL, currentSandboxURL)) { |
| 1792 | scriptWarningMessage("Script.include() ignoring file path" + thisURL.toString() | 1790 | scriptWarningMessage("Script.include() ignoring file path" + thisURL.toString() |
| 1793 | + "outside of original entity script" + currentSandboxURL.toString()); | 1791 | + "outside of original entity script" + currentSandboxURL.toString()); |
| 1794 | } else { | 1792 | } else { |
| 1795 | // We could also check here for CORS, but we don't yet. | 1793 | // We could also check here for CORS, but we don't yet. |
| @@ -1859,7 +1857,7 @@ void ScriptEngine::include(const QStringList& includeFiles, QScriptValue callbac · 63 unmodified lines | |||
| 1859 | 1857 | ||
| 1860 | void ScriptEngine::include(const QString& includeFile, QScriptValue callback) { | 1858 | void ScriptEngine::include(const QString& includeFile, QScriptValue callback) { |
| 1861 | if (DependencyManager::get<ScriptEngines>()->isStopped()) { | 1859 | if (DependencyManager::get<ScriptEngines>()->isStopped()) { |
| 1862 | scriptWarningMessage("Script.include() while shutting down is ignored... includeFile:" | 1860 | scriptWarningMessage("Script.include() while shutting down is ignored... includeFile:" |
| 1863 | + includeFile + "parent script:" + getFilename()); | 1861 | + includeFile + "parent script:" + getFilename()); |
| 1864 | return; // bail early | 1862 | return; // bail early |
| 1865 | } | 1863 | } |
| @@ -1877,12 +1875,12 @@ void ScriptEngine::load(const QString& loadFile) { · 11 unmodified lines | |||
| 1877 | return; | 1875 | return; |
| 1878 | } | 1876 | } |
| 1879 | if (DependencyManager::get<ScriptEngines>()->isStopped()) { | 1877 | if (DependencyManager::get<ScriptEngines>()->isStopped()) { |
| 1880 | scriptWarningMessage("Script.load() while shutting down is ignored... loadFile:" | 1878 | scriptWarningMessage("Script.load() while shutting down is ignored... loadFile:" |
| 1881 | + loadFile + "parent script:" + getFilename()); | 1879 | + loadFile + "parent script:" + getFilename()); |
| 1882 | return; // bail early | 1880 | return; // bail early |
| 1883 | } | 1881 | } |
| 1884 | if (!currentEntityIdentifier.isInvalidID()) { | 1882 | if (!currentEntityIdentifier.isInvalidID()) { |
| 1885 | scriptWarningMessage("Script.load() from entity script is ignored... loadFile:" | 1883 | scriptWarningMessage("Script.load() from entity script is ignored... loadFile:" |
| 1886 | + loadFile + "parent script:" + getFilename() + "entity: " + currentEntityIdentifier.toString()); | 1884 | + loadFile + "parent script:" + getFilename() + "entity: " + currentEntityIdentifier.toString()); |
| 1887 | return; // bail early | 1885 | return; // bail early |
| 1888 | } | 1886 | } |
| @@ -2563,7 +2561,7 @@ void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QS · 674 unmodified lines | |||
| 2563 | qCDebug(scriptengine) << "ScriptEngine::callEntityScriptMethod() called on correct thread [" << thread() << "] " | 2561 | qCDebug(scriptengine) << "ScriptEngine::callEntityScriptMethod() called on correct thread [" << thread() << "] " |
| 2564 | "entityID:" << entityID << "methodName:" << methodName << "otherID:" << otherID << "collision: collision"; | 2562 | "entityID:" << entityID << "methodName:" << methodName << "otherID:" << otherID << "collision: collision"; |
| 2565 | #endif | 2563 | #endif |
| 2566 | 2564 | ||
| 2567 | if (HIFI_AUTOREFRESH_FILE_SCRIPTS) { | 2565 | if (HIFI_AUTOREFRESH_FILE_SCRIPTS) { |
| 2568 | refreshFileScript(entityID); | 2566 | refreshFileScript(entityID); |
| 2569 | } | 2567 | } |