Showing the changes to libraries/script-engine/src/ScriptManager.cpp (1 of 38 files). Show all
Unified · Split
| @@ -53,11 +53,9 @@ · 52 unmodified lines | |||
| 53 | #include <controllers/ScriptingInterface.h> | 53 | #include <controllers/ScriptingInterface.h> |
| 54 | #include <AnimationObject.h> | 54 | #include <AnimationObject.h> |
| 55 | 55 | ||
| 56 | #include "ArrayBufferViewClass.h" | ||
| 57 | #include "AudioScriptingInterface.h" | 56 | #include "AudioScriptingInterface.h" |
| 58 | #include "AssetScriptingInterface.h" | 57 | #include "AssetScriptingInterface.h" |
| 59 | #include "BatchLoader.h" | 58 | #include "BatchLoader.h" |
| 60 | #include "DataViewClass.h" | ||
| 61 | #include "EventTypes.h" | 59 | #include "EventTypes.h" |
| 62 | #include "FileScriptingInterface.h" // unzip project | 60 | #include "FileScriptingInterface.h" // unzip project |
| 63 | #include "MenuItemProperties.h" | 61 | #include "MenuItemProperties.h" |
| @@ -67,7 +65,6 @@ · 3 unmodified lines | |||
| 67 | #include "ScriptContext.h" | 65 | #include "ScriptContext.h" |
| 68 | #include "ScriptEngineCast.h" | 66 | #include "ScriptEngineCast.h" |
| 69 | #include "ScriptEngineLogging.h" | 67 | #include "ScriptEngineLogging.h" |
| 70 | #include "TypedArrays.h" | ||
| 71 | #include "XMLHttpRequestClass.h" | 68 | #include "XMLHttpRequestClass.h" |
| 72 | #include "WebSocketClass.h" | 69 | #include "WebSocketClass.h" |
| 73 | #include "RecordingScriptingInterface.h" | 70 | #include "RecordingScriptingInterface.h" |
| @@ -133,8 +130,10 @@ static ScriptValuePointer debugPrint(ScriptContext* context, ScriptEngine* engin · 59 unmodified lines | |||
| 133 | AbstractLoggerInterface* loggerInterface = AbstractLoggerInterface::get(); | 130 | AbstractLoggerInterface* loggerInterface = AbstractLoggerInterface::get(); |
| 134 | if (loggerInterface && loggerInterface->showSourceDebugging()) { | 131 | if (loggerInterface && loggerInterface->showSourceDebugging()) { |
| 135 | ScriptContext* userContext = context; | 132 | ScriptContext* userContext = context; |
| 133 | ScriptContextPointer parentContext; // using this variable to maintain parent variable lifespan | ||
| 136 | while (userContext && userContext->functionContext()->functionType() == ScriptFunctionContext::NativeFunction) { | 134 | while (userContext && userContext->functionContext()->functionType() == ScriptFunctionContext::NativeFunction) { |
| 137 | userContext = userContext->parentContext(); | 135 | parentContext = userContext->parentContext(); |
| 136 | userContext = parentContext.data(); | ||
| 138 | } | 137 | } |
| 139 | QString location; | 138 | QString location; |
| 140 | if (userContext) { | 139 | if (userContext) { |
| @@ -560,9 +559,10 @@ static ScriptValuePointer scriptableResourceToScriptValue(ScriptEngine* engine, · 419 unmodified lines | |||
| 560 | // in that case it would be too difficult to tell which one should track the memory, and | 559 | // in that case it would be too difficult to tell which one should track the memory, and |
| 561 | // this serves the common case (use in a single script). | 560 | // this serves the common case (use in a single script). |
| 562 | auto data = resource->getResource(); | 561 | auto data = resource->getResource(); |
| 563 | if (data && !resource->isInScript()) { | 562 | auto manager = engine->manager(); |
| 563 | if (data && manager && !resource->isInScript()) { | ||
| 564 | resource->setInScript(true); | 564 | resource->setInScript(true); |
| 565 | QObject::connect(data.data(), SIGNAL(updateSize(qint64)), engine, SLOT(updateMemoryCost(qint64))); | 565 | QObject::connect(data.data(), SIGNAL(updateSize(qint64)), manager, SLOT(updateMemoryCost(qint64))); |
| 566 | } | 566 | } |
| 567 | 567 | ||
| 568 | auto object = engine->newQObject( | 568 | auto object = engine->newQObject( |
| @@ -963,6 +963,11 @@ void ScriptManager::addEventHandler(const EntityItemID& entityID, const QString& · 394 unmodified lines | |||
| 963 | handlersForEvent << handlerData; // Note that the same handler can be added many times. See removeEntityEventHandler(). | 963 | handlersForEvent << handlerData; // Note that the same handler can be added many times. See removeEntityEventHandler(). |
| 964 | } | 964 | } |
| 965 | 965 | ||
| 966 | bool ScriptManager::isStopped() const { | ||
| 967 | QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines); | ||
| 968 | return !scriptEngines || scriptEngines->isStopped(); | ||
| 969 | } | ||
| 970 | |||
| 966 | void ScriptManager::run() { | 971 | void ScriptManager::run() { |
| 967 | if (QThread::currentThread() != qApp->thread() && _context == Context::CLIENT_SCRIPT) { | 972 | if (QThread::currentThread() != qApp->thread() && _context == Context::CLIENT_SCRIPT) { |
| 968 | // Flag that we're allowed to access local HTML files on UI created from C++ calls on this thread | 973 | // Flag that we're allowed to access local HTML files on UI created from C++ calls on this thread |
| @@ -974,8 +979,7 @@ void ScriptManager::run() { · 5 unmodified lines | |||
| 974 | auto name = filenameParts.size() > 0 ? filenameParts[filenameParts.size() - 1] : "unknown"; | 979 | auto name = filenameParts.size() > 0 ? filenameParts[filenameParts.size() - 1] : "unknown"; |
| 975 | PROFILE_SET_THREAD_NAME("Script: " + name); | 980 | PROFILE_SET_THREAD_NAME("Script: " + name); |
| 976 | 981 | ||
| 977 | QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines); | 982 | if (isStopped()) { |
| 978 | if (!scriptEngines || scriptEngines->isStopped()) { | ||
| 979 | return; // bail early - avoid setting state in init(), as evaluate() will bail too | 983 | return; // bail early - avoid setting state in init(), as evaluate() will bail too |
| 980 | } | 984 | } |
| 981 | 985 | ||
| @@ -1228,21 +1232,13 @@ void ScriptManager::callAnimationStateHandler(ScriptValuePointer callback, AnimV · 246 unmodified lines | |||
| 1228 | } | 1232 | } |
| 1229 | 1233 | ||
| 1230 | void ScriptManager::updateMemoryCost(const qint64& deltaSize) { | 1234 | void ScriptManager::updateMemoryCost(const qint64& deltaSize) { |
| 1231 | if (deltaSize > 0) { | 1235 | _engine->updateMemoryCost(deltaSize); |
| 1232 | // We've patched qt to fix https://highfidelity.atlassian.net/browse/BUGZ-46 on mac and windows only. | ||
| 1233 | #if defined(Q_OS_WIN) || defined(Q_OS_MAC) | ||
| 1234 | reportAdditionalMemoryCost(deltaSize); | ||
| 1235 | #endif | ||
| 1236 | } | ||
| 1237 | } | 1236 | } |
| 1238 | 1237 | ||
| 1239 | void ScriptManager::timerFired() { | 1238 | void ScriptManager::timerFired() { |
| 1240 | { | 1239 | if (isStopped()) { |
| 1241 | QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines); | 1240 | scriptWarningMessage("Script.timerFired() while shutting down is ignored... parent script:" + getFilename()); |
| 1242 | if (!scriptEngines || scriptEngines->isStopped()) { | 1241 | return; // bail early |
| 1243 | scriptWarningMessage("Script.timerFired() while shutting down is ignored... parent script:" + getFilename()); | ||
| 1244 | return; // bail early | ||
| 1245 | } | ||
| 1246 | } | 1242 | } |
| 1247 | 1243 | ||
| 1248 | QTimer* callingTimer = reinterpret_cast<QTimer*>(sender()); | 1244 | QTimer* callingTimer = reinterpret_cast<QTimer*>(sender()); |
| @@ -1292,8 +1288,7 @@ QObject* ScriptManager::setupTimerWithInterval(const ScriptValuePointer& functio · 43 unmodified lines | |||
| 1292 | } | 1288 | } |
| 1293 | 1289 | ||
| 1294 | QObject* ScriptManager::setInterval(const ScriptValuePointer& function, int intervalMS) { | 1290 | QObject* ScriptManager::setInterval(const ScriptValuePointer& function, int intervalMS) { |
| 1295 | QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines); | 1291 | if (isStopped()) { |
| 1296 | if (!scriptEngines || scriptEngines->isStopped()) { | ||
| 1297 | scriptWarningMessage("Script.setInterval() while shutting down is ignored... parent script:" + getFilename()); | 1292 | scriptWarningMessage("Script.setInterval() while shutting down is ignored... parent script:" + getFilename()); |
| 1298 | return NULL; // bail early | 1293 | return NULL; // bail early |
| 1299 | } | 1294 | } |
| @@ -1302,8 +1297,7 @@ QObject* ScriptManager::setInterval(const ScriptValuePointer& function, int inte · 2 unmodified lines | |||
| 1302 | } | 1297 | } |
| 1303 | 1298 | ||
| 1304 | QObject* ScriptManager::setTimeout(const ScriptValuePointer& function, int timeoutMS) { | 1299 | QObject* ScriptManager::setTimeout(const ScriptValuePointer& function, int timeoutMS) { |
| 1305 | QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines); | 1300 | if (isStopped()) { |
| 1306 | if (!scriptEngines || scriptEngines->isStopped()) { | ||
| 1307 | scriptWarningMessage("Script.setTimeout() while shutting down is ignored... parent script:" + getFilename()); | 1301 | scriptWarningMessage("Script.setTimeout() while shutting down is ignored... parent script:" + getFilename()); |
| 1308 | return NULL; // bail early | 1302 | return NULL; // bail early |
| 1309 | } | 1303 | } |
| @@ -1335,10 +1329,12 @@ QUrl ScriptManager::resolvePath(const QString& include) const { · 25 unmodified lines | |||
| 1335 | // to the first absolute URL in the JS scope chain | 1329 | // to the first absolute URL in the JS scope chain |
| 1336 | QUrl parentURL; | 1330 | QUrl parentURL; |
| 1337 | auto context = _engine->currentContext(); | 1331 | auto context = _engine->currentContext(); |
| 1332 | ScriptContextPointer parentContext; // using this variable to maintain parent variable lifespan | ||
| 1338 | do { | 1333 | do { |
| 1339 | auto contextInfo = context->functionContext(); | 1334 | auto contextInfo = context->functionContext(); |
| 1340 | parentURL = QUrl(contextInfo->fileName()); | 1335 | parentURL = QUrl(contextInfo->fileName()); |
| 1341 | context = context->parentContext(); | 1336 | parentContext = context->parentContext(); |
| 1337 | context = parentContext.data(); | ||
| 1342 | } while (parentURL.isRelative() && context); | 1338 | } while (parentURL.isRelative() && context); |
| 1343 | 1339 | ||
| 1344 | if (parentURL.isRelative()) { | 1340 | if (parentURL.isRelative()) { |
| @@ -1478,8 +1474,9 @@ ScriptValuePointer ScriptManager::currentModule() { · 133 unmodified lines | |||
| 1478 | auto jsRequire = _engine->globalObject()->property("Script")->property("require"); | 1474 | auto jsRequire = _engine->globalObject()->property("Script")->property("require"); |
| 1479 | auto cache = jsRequire->property("cache"); | 1475 | auto cache = jsRequire->property("cache"); |
| 1480 | auto candidate = ScriptValuePointer(); | 1476 | auto candidate = ScriptValuePointer(); |
| 1481 | for (auto c = _engine->currentContext(); c && !candidate->isObject(); c = c->parentContext()) { | 1477 | ScriptContextPointer parentContext; // using this variable to maintain parent variable lifespan |
| 1482 | auto contextInfo = c->functionContext(); | 1478 | for (auto context = _engine->currentContext(); context && !candidate->isObject(); parentContext = context->parentContext(), context = parentContext.data()) { |
| 1479 | auto contextInfo = context->functionContext(); | ||
| 1483 | candidate = cache->property(contextInfo->fileName()); | 1480 | candidate = cache->property(contextInfo->fileName()); |
| 1484 | } | 1481 | } |
| 1485 | if (!candidate->isObject()) { | 1482 | if (!candidate->isObject()) { |
| @@ -1737,8 +1734,7 @@ void ScriptManager::include(const QStringList& includeFiles, ScriptValuePointer · 251 unmodified lines | |||
| 1737 | if (!_engine->IS_THREADSAFE_INVOCATION(__FUNCTION__)) { | 1734 | if (!_engine->IS_THREADSAFE_INVOCATION(__FUNCTION__)) { |
| 1738 | return; | 1735 | return; |
| 1739 | } | 1736 | } |
| 1740 | QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines); | 1737 | if (isStopped()) { |
| 1741 | if (!scriptEngines || scriptEngines->isStopped()) { | ||
| 1742 | scriptWarningMessage("Script.include() while shutting down is ignored... includeFiles:" | 1738 | scriptWarningMessage("Script.include() while shutting down is ignored... includeFiles:" |
| 1743 | + includeFiles.join(",") + "parent script:" + getFilename()); | 1739 | + includeFiles.join(",") + "parent script:" + getFilename()); |
| 1744 | return; // bail early | 1740 | return; // bail early |
| @@ -1832,8 +1828,7 @@ void ScriptManager::include(const QStringList& includeFiles, ScriptValuePointer · 87 unmodified lines | |||
| 1832 | } | 1828 | } |
| 1833 | 1829 | ||
| 1834 | void ScriptManager::include(const QString& includeFile, ScriptValuePointer callback) { | 1830 | void ScriptManager::include(const QString& includeFile, ScriptValuePointer callback) { |
| 1835 | QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines); | 1831 | if (isStopped()) { |
| 1836 | if (!scriptEngines || scriptEngines->isStopped()) { | ||
| 1837 | scriptWarningMessage("Script.include() while shutting down is ignored... includeFile:" | 1832 | scriptWarningMessage("Script.include() while shutting down is ignored... includeFile:" |
| 1838 | + includeFile + "parent script:" + getFilename()); | 1833 | + includeFile + "parent script:" + getFilename()); |
| 1839 | return; // bail early | 1834 | return; // bail early |
| @@ -1851,8 +1846,7 @@ void ScriptManager::load(const QString& loadFile) { · 11 unmodified lines | |||
| 1851 | if (!_engine->IS_THREADSAFE_INVOCATION(__FUNCTION__)) { | 1846 | if (!_engine->IS_THREADSAFE_INVOCATION(__FUNCTION__)) { |
| 1852 | return; | 1847 | return; |
| 1853 | } | 1848 | } |
| 1854 | QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines); | 1849 | if (isStopped()) { |
| 1855 | if (!scriptEngines || scriptEngines->isStopped()) { | ||
| 1856 | scriptWarningMessage("Script.load() while shutting down is ignored... loadFile:" | 1850 | scriptWarningMessage("Script.load() while shutting down is ignored... loadFile:" |
| 1857 | + loadFile + "parent script:" + getFilename()); | 1851 | + loadFile + "parent script:" + getFilename()); |
| 1858 | return; // bail early | 1852 | return; // bail early |
| @@ -2689,3 +2683,7 @@ QString ScriptManager::formatException(const ScriptValuePointer& exception, bool · 830 unmodified lines | |||
| 2689 | } | 2683 | } |
| 2690 | return result; | 2684 | return result; |
| 2691 | } | 2685 | } |
| 2686 | |||
| 2687 | ScriptValuePointer ScriptManager::evaluate(const QString& program, const QString& fileName) { | ||
| 2688 | return _engine->evaluate(program, fileName); | ||
| 2689 | } | ||