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 | 53 | #include <controllers/ScriptingInterface.h> |
| 54 | 54 | #include <AnimationObject.h> |
| 55 | 55 | |
| 56 | −#include "ArrayBufferViewClass.h" | |
| 57 | 56 | #include "AudioScriptingInterface.h" |
| 58 | 57 | #include "AssetScriptingInterface.h" |
| 59 | 58 | #include "BatchLoader.h" |
| 60 | −#include "DataViewClass.h" | |
| 61 | 59 | #include "EventTypes.h" |
| 62 | 60 | #include "FileScriptingInterface.h" // unzip project |
| 63 | 61 | #include "MenuItemProperties.h" |
| @@ -67,7 +65,6 @@ · 3 unmodified lines | ||
| 67 | 65 | #include "ScriptContext.h" |
| 68 | 66 | #include "ScriptEngineCast.h" |
| 69 | 67 | #include "ScriptEngineLogging.h" |
| 70 | −#include "TypedArrays.h" | |
| 71 | 68 | #include "XMLHttpRequestClass.h" |
| 72 | 69 | #include "WebSocketClass.h" |
| 73 | 70 | #include "RecordingScriptingInterface.h" |
| @@ -133,8 +130,10 @@ static ScriptValuePointer debugPrint(ScriptContext* context, ScriptEngine* engin · 59 unmodified lines | ||
| 133 | 130 | AbstractLoggerInterface* loggerInterface = AbstractLoggerInterface::get(); |
| 134 | 131 | if (loggerInterface && loggerInterface->showSourceDebugging()) { |
| 135 | 132 | ScriptContext* userContext = context; |
| 133 | + ScriptContextPointer parentContext; // using this variable to maintain parent variable lifespan | |
| 136 | 134 | while (userContext && userContext->functionContext()->functionType() == ScriptFunctionContext::NativeFunction) { |
| 137 | − userContext = userContext->parentContext(); | |
| 135 | + parentContext = userContext->parentContext(); | |
| 136 | + userContext = parentContext.data(); | |
| 138 | 137 | } |
| 139 | 138 | QString location; |
| 140 | 139 | if (userContext) { |
| @@ -560,9 +559,10 @@ static ScriptValuePointer scriptableResourceToScriptValue(ScriptEngine* engine, · 419 unmodified lines | ||
| 560 | 559 | // in that case it would be too difficult to tell which one should track the memory, and |
| 561 | 560 | // this serves the common case (use in a single script). |
| 562 | 561 | auto data = resource->getResource(); |
| 563 | − if (data && !resource->isInScript()) { | |
| 562 | + auto manager = engine->manager(); | |
| 563 | + if (data && manager && !resource->isInScript()) { | |
| 564 | 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 | 568 | auto object = engine->newQObject( |
| @@ -963,6 +963,11 @@ void ScriptManager::addEventHandler(const EntityItemID& entityID, const QString& · 394 unmodified lines | ||
| 963 | 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 | 971 | void ScriptManager::run() { |
| 967 | 972 | if (QThread::currentThread() != qApp->thread() && _context == Context::CLIENT_SCRIPT) { |
| 968 | 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 | 979 | auto name = filenameParts.size() > 0 ? filenameParts[filenameParts.size() - 1] : "unknown"; |
| 975 | 980 | PROFILE_SET_THREAD_NAME("Script: " + name); |
| 976 | 981 | |
| 977 | − QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines); | |
| 978 | − if (!scriptEngines || scriptEngines->isStopped()) { | |
| 982 | + if (isStopped()) { | |
| 979 | 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 | 1234 | void ScriptManager::updateMemoryCost(const qint64& deltaSize) { |
| 1231 | − if (deltaSize > 0) { | |
| 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 | − } | |
| 1235 | + _engine->updateMemoryCost(deltaSize); | |
| 1237 | 1236 | } |
| 1238 | 1237 | |
| 1239 | 1238 | void ScriptManager::timerFired() { |
| 1240 | − { | |
| 1241 | − QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines); | |
| 1242 | − if (!scriptEngines || scriptEngines->isStopped()) { | |
| 1243 | − scriptWarningMessage("Script.timerFired() while shutting down is ignored... parent script:" + getFilename()); | |
| 1244 | − return; // bail early | |
| 1245 | − } | |
| 1239 | + if (isStopped()) { | |
| 1240 | + scriptWarningMessage("Script.timerFired() while shutting down is ignored... parent script:" + getFilename()); | |
| 1241 | + return; // bail early | |
| 1246 | 1242 | } |
| 1247 | 1243 | |
| 1248 | 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 | 1290 | QObject* ScriptManager::setInterval(const ScriptValuePointer& function, int intervalMS) { |
| 1295 | − QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines); | |
| 1296 | − if (!scriptEngines || scriptEngines->isStopped()) { | |
| 1291 | + if (isStopped()) { | |
| 1297 | 1292 | scriptWarningMessage("Script.setInterval() while shutting down is ignored... parent script:" + getFilename()); |
| 1298 | 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 | 1299 | QObject* ScriptManager::setTimeout(const ScriptValuePointer& function, int timeoutMS) { |
| 1305 | − QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines); | |
| 1306 | − if (!scriptEngines || scriptEngines->isStopped()) { | |
| 1300 | + if (isStopped()) { | |
| 1307 | 1301 | scriptWarningMessage("Script.setTimeout() while shutting down is ignored... parent script:" + getFilename()); |
| 1308 | 1302 | return NULL; // bail early |
| 1309 | 1303 | } |
| @@ -1335,10 +1329,12 @@ QUrl ScriptManager::resolvePath(const QString& include) const { · 25 unmodified lines | ||
| 1335 | 1329 | // to the first absolute URL in the JS scope chain |
| 1336 | 1330 | QUrl parentURL; |
| 1337 | 1331 | auto context = _engine->currentContext(); |
| 1332 | + ScriptContextPointer parentContext; // using this variable to maintain parent variable lifespan | |
| 1338 | 1333 | do { |
| 1339 | 1334 | auto contextInfo = context->functionContext(); |
| 1340 | 1335 | parentURL = QUrl(contextInfo->fileName()); |
| 1341 | − context = context->parentContext(); | |
| 1336 | + parentContext = context->parentContext(); | |
| 1337 | + context = parentContext.data(); | |
| 1342 | 1338 | } while (parentURL.isRelative() && context); |
| 1343 | 1339 | |
| 1344 | 1340 | if (parentURL.isRelative()) { |
| @@ -1478,8 +1474,9 @@ ScriptValuePointer ScriptManager::currentModule() { · 133 unmodified lines | ||
| 1478 | 1474 | auto jsRequire = _engine->globalObject()->property("Script")->property("require"); |
| 1479 | 1475 | auto cache = jsRequire->property("cache"); |
| 1480 | 1476 | auto candidate = ScriptValuePointer(); |
| 1481 | − for (auto c = _engine->currentContext(); c && !candidate->isObject(); c = c->parentContext()) { | |
| 1482 | − auto contextInfo = c->functionContext(); | |
| 1477 | + ScriptContextPointer parentContext; // using this variable to maintain parent variable lifespan | |
| 1478 | + for (auto context = _engine->currentContext(); context && !candidate->isObject(); parentContext = context->parentContext(), context = parentContext.data()) { | |
| 1479 | + auto contextInfo = context->functionContext(); | |
| 1483 | 1480 | candidate = cache->property(contextInfo->fileName()); |
| 1484 | 1481 | } |
| 1485 | 1482 | if (!candidate->isObject()) { |
| @@ -1737,8 +1734,7 @@ void ScriptManager::include(const QStringList& includeFiles, ScriptValuePointer · 251 unmodified lines | ||
| 1737 | 1734 | if (!_engine->IS_THREADSAFE_INVOCATION(__FUNCTION__)) { |
| 1738 | 1735 | return; |
| 1739 | 1736 | } |
| 1740 | − QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines); | |
| 1741 | − if (!scriptEngines || scriptEngines->isStopped()) { | |
| 1737 | + if (isStopped()) { | |
| 1742 | 1738 | scriptWarningMessage("Script.include() while shutting down is ignored... includeFiles:" |
| 1743 | 1739 | + includeFiles.join(",") + "parent script:" + getFilename()); |
| 1744 | 1740 | return; // bail early |
| @@ -1832,8 +1828,7 @@ void ScriptManager::include(const QStringList& includeFiles, ScriptValuePointer · 87 unmodified lines | ||
| 1832 | 1828 | } |
| 1833 | 1829 | |
| 1834 | 1830 | void ScriptManager::include(const QString& includeFile, ScriptValuePointer callback) { |
| 1835 | − QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines); | |
| 1836 | − if (!scriptEngines || scriptEngines->isStopped()) { | |
| 1831 | + if (isStopped()) { | |
| 1837 | 1832 | scriptWarningMessage("Script.include() while shutting down is ignored... includeFile:" |
| 1838 | 1833 | + includeFile + "parent script:" + getFilename()); |
| 1839 | 1834 | return; // bail early |
| @@ -1851,8 +1846,7 @@ void ScriptManager::load(const QString& loadFile) { · 11 unmodified lines | ||
| 1851 | 1846 | if (!_engine->IS_THREADSAFE_INVOCATION(__FUNCTION__)) { |
| 1852 | 1847 | return; |
| 1853 | 1848 | } |
| 1854 | − QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines); | |
| 1855 | − if (!scriptEngines || scriptEngines->isStopped()) { | |
| 1849 | + if (isStopped()) { | |
| 1856 | 1850 | scriptWarningMessage("Script.load() while shutting down is ignored... loadFile:" |
| 1857 | 1851 | + loadFile + "parent script:" + getFilename()); |
| 1858 | 1852 | return; // bail early |
| @@ -2689,3 +2683,7 @@ QString ScriptManager::formatException(const ScriptValuePointer& exception, bool · 830 unmodified lines | ||
| 2689 | 2683 | } |
| 2690 | 2684 | return result; |
| 2691 | 2685 | } |
| 2686 | + | |
| 2687 | +ScriptValuePointer ScriptManager::evaluate(const QString& program, const QString& fileName) { | |
| 2688 | + return _engine->evaluate(program, fileName); | |
| 2689 | +} | |