History

Merge branch 'master' into 20277

David Rowe committed 11 years ago

Commit 5474c062 parents 812fbc5 53534ce193 files · +7165 −4472Browse files

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

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+47−37
@@ -31,6 +31,7 @@ · 30 unmodified lines
3131
3232 #include "AnimationObject.h"
3333 #include "ArrayBufferViewClass.h"
34+#include "BatchLoader.h"
3435 #include "DataViewClass.h"
3536 #include "EventTypes.h"
3637 #include "MenuItemProperties.h"
@@ -111,7 +112,7 @@ void ScriptEngine::setIsAvatar(bool isAvatar) { · 74 unmodified lines
111112 _avatarIdentityTimer->start(AVATAR_IDENTITY_PACKET_SEND_INTERVAL_MSECS);
112113 _avatarBillboardTimer->start(AVATAR_BILLBOARD_PACKET_SEND_INTERVAL_MSECS);
113114 }
114−
115+
115116 if (!_isAvatar) {
116117 delete _avatarIdentityTimer;
117118 _avatarIdentityTimer = NULL;
@@ -306,7 +307,7 @@ QScriptValue ScriptEngine::evaluate(const QString& program, const QString& fileN · 188 unmodified lines
306307 QScriptValue result = QScriptEngine::evaluate(program, fileName, lineNumber);
307308 if (hasUncaughtException()) {
308309 int line = uncaughtExceptionLineNumber();
309− qDebug() << "Uncaught exception at (" << _fileNameString << ") line" << line << ": " << result.toString();
310+ qDebug() << "Uncaught exception at (" << _fileNameString << " : " << fileName << ") line" << line << ": " << result.toString();
310311 }
311312 emit evaluationFinished(result, hasUncaughtException());
312313 clearExceptions();
@@ -597,46 +598,55 @@ void ScriptEngine::print(const QString& message) { · 284 unmodified lines
597598 emit printedMessage(message);
598599 }
599600
600−void ScriptEngine::include(const QString& includeFile) {
601− QUrl url = resolvePath(includeFile);
602− QString includeContents;
601+/**
602+ * If a callback is specified, the included files will be loaded asynchronously and the callback will be called
603+ * when all of the files have finished loading.
604+ * If no callback is specified, the included files will be loaded synchronously and will block execution until
605+ * all of the files have finished loading.
606+ */
607+void ScriptEngine::include(const QStringList& includeFiles, QScriptValue callback) {
608+ QList<QUrl> urls;
609+ for (QString file : includeFiles) {
610+ urls.append(resolvePath(file));
611+ }
612+
613+ BatchLoader* loader = new BatchLoader(urls);
614+
615+ auto evaluateScripts = [=](const QMap<QUrl, QString>& data) {
616+ for (QUrl url : urls) {
617+ QString contents = data[url];
618+ if (contents.isNull()) {
619+ qDebug() << "Error loading file: " << url;
620+ } else {
621+ QScriptValue result = evaluate(contents, url.toString());
622+ }
623+ }
624+
625+ if (callback.isFunction()) {
626+ QScriptValue(callback).call();
627+ }
628+
629+ loader->deleteLater();
630+ };
603631
604− if (url.scheme() == "http" || url.scheme() == "https" || url.scheme() == "ftp") {
605− QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance();
606− QNetworkRequest networkRequest = QNetworkRequest(url);
607− networkRequest.setHeader(QNetworkRequest::UserAgentHeader, HIGH_FIDELITY_USER_AGENT);
608− QNetworkReply* reply = networkAccessManager.get(networkRequest);
609− qDebug() << "Downloading included script at" << includeFile;
632+ connect(loader, &BatchLoader::finished, this, evaluateScripts);
633+
634+ // If we are destroyed before the loader completes, make sure to clean it up
635+ connect(this, &QObject::destroyed, loader, &QObject::deleteLater);
636+
637+ loader->start();
638+
639+ if (!callback.isFunction() && !loader->isFinished()) {
610640 QEventLoop loop;
611− QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
641+ QObject::connect(loader, &BatchLoader::finished, &loop, &QEventLoop::quit);
612642 loop.exec();
613− includeContents = reply->readAll();
614− reply->deleteLater();
615− } else {
616−#ifdef _WIN32
617− QString fileName = url.toString();
618−#else
619− QString fileName = url.toLocalFile();
620−#endif
621−
622− QFile scriptFile(fileName);
623− if (scriptFile.open(QFile::ReadOnly | QFile::Text)) {
624− qDebug() << "Including file:" << fileName;
625− QTextStream in(&scriptFile);
626− includeContents = in.readAll();
627− } else {
628− qDebug() << "ERROR Including file:" << fileName;
629− emit errorMessage("ERROR Including file:" + fileName);
630− }
631643 }
644+}
632645
633− QScriptValue result = evaluate(includeContents);
634− if (hasUncaughtException()) {
635− int line = uncaughtExceptionLineNumber();
636− qDebug() << "Uncaught exception at (" << includeFile << ") line" << line << ":" << result.toString();
637− emit errorMessage("Uncaught exception at (" + includeFile + ") line" + QString::number(line) + ":" + result.toString());
638− clearExceptions();
639− }
646+void ScriptEngine::include(const QString& includeFile, QScriptValue callback) {
647+ QStringList urls;
648+ urls.append(includeFile);
649+ include(urls, callback);
640650 }
641651
642652 void ScriptEngine::load(const QString& loadFile) {