History

working to clean up the QtScript implementation and move towards completion of the proxy interface

Heather Anderson committed 5 years ago

Commit 2dd9d781 parent 1e018db38 files · +2240 −923Browse files

Showing the changes to libraries/script-engine/src/ScriptManager.cpp (1 of 38 files). Show all

Unified · Split

libraries/script-engine/src/ScriptManager.cpp+31−33
@@ -53,11 +53,9 @@ · 52 unmodified lines
5353 #include <controllers/ScriptingInterface.h>
5454 #include <AnimationObject.h>
5555
56−#include "ArrayBufferViewClass.h"
5756 #include "AudioScriptingInterface.h"
5857 #include "AssetScriptingInterface.h"
5958 #include "BatchLoader.h"
60−#include "DataViewClass.h"
6159 #include "EventTypes.h"
6260 #include "FileScriptingInterface.h" // unzip project
6361 #include "MenuItemProperties.h"
@@ -67,7 +65,6 @@ · 3 unmodified lines
6765 #include "ScriptContext.h"
6866 #include "ScriptEngineCast.h"
6967 #include "ScriptEngineLogging.h"
70−#include "TypedArrays.h"
7168 #include "XMLHttpRequestClass.h"
7269 #include "WebSocketClass.h"
7370 #include "RecordingScriptingInterface.h"
@@ -133,8 +130,10 @@ static ScriptValuePointer debugPrint(ScriptContext* context, ScriptEngine* engin · 59 unmodified lines
133130 AbstractLoggerInterface* loggerInterface = AbstractLoggerInterface::get();
134131 if (loggerInterface && loggerInterface->showSourceDebugging()) {
135132 ScriptContext* userContext = context;
133+ ScriptContextPointer parentContext; // using this variable to maintain parent variable lifespan
136134 while (userContext && userContext->functionContext()->functionType() == ScriptFunctionContext::NativeFunction) {
137− userContext = userContext->parentContext();
135+ parentContext = userContext->parentContext();
136+ userContext = parentContext.data();
138137 }
139138 QString location;
140139 if (userContext) {
@@ -560,9 +559,10 @@ static ScriptValuePointer scriptableResourceToScriptValue(ScriptEngine* engine, · 419 unmodified lines
560559 // in that case it would be too difficult to tell which one should track the memory, and
561560 // this serves the common case (use in a single script).
562561 auto data = resource->getResource();
563− if (data && !resource->isInScript()) {
562+ auto manager = engine->manager();
563+ if (data && manager && !resource->isInScript()) {
564564 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)));
566566 }
567567
568568 auto object = engine->newQObject(
@@ -963,6 +963,11 @@ void ScriptManager::addEventHandler(const EntityItemID& entityID, const QString& · 394 unmodified lines
963963 handlersForEvent << handlerData; // Note that the same handler can be added many times. See removeEntityEventHandler().
964964 }
965965
966+bool ScriptManager::isStopped() const {
967+ QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines);
968+ return !scriptEngines || scriptEngines->isStopped();
969+}
970+
966971 void ScriptManager::run() {
967972 if (QThread::currentThread() != qApp->thread() && _context == Context::CLIENT_SCRIPT) {
968973 // 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
974979 auto name = filenameParts.size() > 0 ? filenameParts[filenameParts.size() - 1] : "unknown";
975980 PROFILE_SET_THREAD_NAME("Script: " + name);
976981
977− QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines);
978− if (!scriptEngines || scriptEngines->isStopped()) {
982+ if (isStopped()) {
979983 return; // bail early - avoid setting state in init(), as evaluate() will bail too
980984 }
981985
@@ -1228,21 +1232,13 @@ void ScriptManager::callAnimationStateHandler(ScriptValuePointer callback, AnimV · 246 unmodified lines
12281232 }
12291233
12301234 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);
12371236 }
12381237
12391238 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
12461242 }
12471243
12481244 QTimer* callingTimer = reinterpret_cast<QTimer*>(sender());
@@ -1292,8 +1288,7 @@ QObject* ScriptManager::setupTimerWithInterval(const ScriptValuePointer& functio · 43 unmodified lines
12921288 }
12931289
12941290 QObject* ScriptManager::setInterval(const ScriptValuePointer& function, int intervalMS) {
1295− QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines);
1296− if (!scriptEngines || scriptEngines->isStopped()) {
1291+ if (isStopped()) {
12971292 scriptWarningMessage("Script.setInterval() while shutting down is ignored... parent script:" + getFilename());
12981293 return NULL; // bail early
12991294 }
@@ -1302,8 +1297,7 @@ QObject* ScriptManager::setInterval(const ScriptValuePointer& function, int inte · 2 unmodified lines
13021297 }
13031298
13041299 QObject* ScriptManager::setTimeout(const ScriptValuePointer& function, int timeoutMS) {
1305− QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines);
1306− if (!scriptEngines || scriptEngines->isStopped()) {
1300+ if (isStopped()) {
13071301 scriptWarningMessage("Script.setTimeout() while shutting down is ignored... parent script:" + getFilename());
13081302 return NULL; // bail early
13091303 }
@@ -1335,10 +1329,12 @@ QUrl ScriptManager::resolvePath(const QString& include) const { · 25 unmodified lines
13351329 // to the first absolute URL in the JS scope chain
13361330 QUrl parentURL;
13371331 auto context = _engine->currentContext();
1332+ ScriptContextPointer parentContext; // using this variable to maintain parent variable lifespan
13381333 do {
13391334 auto contextInfo = context->functionContext();
13401335 parentURL = QUrl(contextInfo->fileName());
1341− context = context->parentContext();
1336+ parentContext = context->parentContext();
1337+ context = parentContext.data();
13421338 } while (parentURL.isRelative() && context);
13431339
13441340 if (parentURL.isRelative()) {
@@ -1478,8 +1474,9 @@ ScriptValuePointer ScriptManager::currentModule() { · 133 unmodified lines
14781474 auto jsRequire = _engine->globalObject()->property("Script")->property("require");
14791475 auto cache = jsRequire->property("cache");
14801476 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();
14831480 candidate = cache->property(contextInfo->fileName());
14841481 }
14851482 if (!candidate->isObject()) {
@@ -1737,8 +1734,7 @@ void ScriptManager::include(const QStringList& includeFiles, ScriptValuePointer · 251 unmodified lines
17371734 if (!_engine->IS_THREADSAFE_INVOCATION(__FUNCTION__)) {
17381735 return;
17391736 }
1740− QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines);
1741− if (!scriptEngines || scriptEngines->isStopped()) {
1737+ if (isStopped()) {
17421738 scriptWarningMessage("Script.include() while shutting down is ignored... includeFiles:"
17431739 + includeFiles.join(",") + "parent script:" + getFilename());
17441740 return; // bail early
@@ -1832,8 +1828,7 @@ void ScriptManager::include(const QStringList& includeFiles, ScriptValuePointer · 87 unmodified lines
18321828 }
18331829
18341830 void ScriptManager::include(const QString& includeFile, ScriptValuePointer callback) {
1835− QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines);
1836− if (!scriptEngines || scriptEngines->isStopped()) {
1831+ if (isStopped()) {
18371832 scriptWarningMessage("Script.include() while shutting down is ignored... includeFile:"
18381833 + includeFile + "parent script:" + getFilename());
18391834 return; // bail early
@@ -1851,8 +1846,7 @@ void ScriptManager::load(const QString& loadFile) { · 11 unmodified lines
18511846 if (!_engine->IS_THREADSAFE_INVOCATION(__FUNCTION__)) {
18521847 return;
18531848 }
1854− QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines);
1855− if (!scriptEngines || scriptEngines->isStopped()) {
1849+ if (isStopped()) {
18561850 scriptWarningMessage("Script.load() while shutting down is ignored... loadFile:"
18571851 + loadFile + "parent script:" + getFilename());
18581852 return; // bail early
@@ -2689,3 +2683,7 @@ QString ScriptManager::formatException(const ScriptValuePointer& exception, bool · 830 unmodified lines
26892683 }
26902684 return result;
26912685 }
2686+
2687+ScriptValuePointer ScriptManager::evaluate(const QString& program, const QString& fileName) {
2688+ return _engine->evaluate(program, fileName);
2689+}