Update ScriptEngine to use BatchLoader

This commit is contained in:
Ryan Huffman 2015-01-22 14:14:21 -08:00
parent 958d853b56
commit 38b38eb063
2 changed files with 43 additions and 35 deletions

View file

@ -31,6 +31,7 @@
#include "AnimationObject.h" #include "AnimationObject.h"
#include "ArrayBufferViewClass.h" #include "ArrayBufferViewClass.h"
#include "BatchLoader.h"
#include "DataViewClass.h" #include "DataViewClass.h"
#include "EventTypes.h" #include "EventTypes.h"
#include "MenuItemProperties.h" #include "MenuItemProperties.h"
@ -304,7 +305,7 @@ QScriptValue ScriptEngine::evaluate(const QString& program, const QString& fileN
QScriptValue result = QScriptEngine::evaluate(program, fileName, lineNumber); QScriptValue result = QScriptEngine::evaluate(program, fileName, lineNumber);
if (hasUncaughtException()) { if (hasUncaughtException()) {
int line = uncaughtExceptionLineNumber(); int line = uncaughtExceptionLineNumber();
qDebug() << "Uncaught exception at (" << _fileNameString << ") line" << line << ": " << result.toString(); qDebug() << "Uncaught exception at (" << _fileNameString << " : " << fileName << ") line" << line << ": " << result.toString();
} }
emit evaluationFinished(result, hasUncaughtException()); emit evaluationFinished(result, hasUncaughtException());
clearExceptions(); clearExceptions();
@ -595,46 +596,52 @@ void ScriptEngine::print(const QString& message) {
emit printedMessage(message); emit printedMessage(message);
} }
void ScriptEngine::include(const QString& includeFile) { void ScriptEngine::include(const QStringList& includeFiles, QScriptValue callback) {
QUrl url = resolvePath(includeFile); QList<QUrl> urls;
QString includeContents; for (QString file : includeFiles) {
urls.append(resolvePath(file));
}
if (url.scheme() == "http" || url.scheme() == "https" || url.scheme() == "ftp") { BatchLoader* loader = new BatchLoader(urls);
QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance();
QNetworkReply* reply = networkAccessManager.get(QNetworkRequest(url)); auto evaluateScripts = [=](const QMap<QUrl, QString>& data) {
qDebug() << "Downloading included script at" << includeFile; for (QUrl url : urls) {
QEventLoop loop; QString contents = data[url];
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); qDebug() << "About to load: " << url;
loop.exec(); if (contents.isNull()) {
includeContents = reply->readAll(); qDebug() << "Error loading file: " << url;
reply->deleteLater(); } else {
} else { QScriptValue result = evaluate(contents, url.toString());
#ifdef _WIN32 }
QString fileName = url.toString();
#else
QString fileName = url.toLocalFile();
#endif
QFile scriptFile(fileName);
if (scriptFile.open(QFile::ReadOnly | QFile::Text)) {
qDebug() << "Including file:" << fileName;
QTextStream in(&scriptFile);
includeContents = in.readAll();
} else {
qDebug() << "ERROR Including file:" << fileName;
emit errorMessage("ERROR Including file:" + fileName);
} }
}
QScriptValue result = evaluate(includeContents); if (callback.isFunction()) {
if (hasUncaughtException()) { QScriptValue(callback).call();
int line = uncaughtExceptionLineNumber(); }
qDebug() << "Uncaught exception at (" << includeFile << ") line" << line << ":" << result.toString();
emit errorMessage("Uncaught exception at (" + includeFile + ") line" + QString::number(line) + ":" + result.toString()); loader->deleteLater();
clearExceptions(); };
connect(loader, &BatchLoader::finished, this, evaluateScripts);
// If we are destroyed before the loader completes, make sure to clean it up
connect(this, &QObject::destroyed, loader, &QObject::deleteLater);
loader->start();
if (!callback.isFunction() && !loader->isFinished()) {
QEventLoop loop;
QObject::connect(loader, &BatchLoader::finished, &loop, &QEventLoop::quit);
loop.exec();
} }
} }
void ScriptEngine::include(const QString& includeFile) {
QStringList urls;
urls.append(includeFile);
include(urls);
}
void ScriptEngine::load(const QString& loadFile) { void ScriptEngine::load(const QString& loadFile) {
QUrl url = resolvePath(loadFile); QUrl url = resolvePath(loadFile);
emit loadScript(url.toString(), false); emit loadScript(url.toString(), false);

View file

@ -96,6 +96,7 @@ public slots:
QObject* setTimeout(const QScriptValue& function, int timeoutMS); QObject* setTimeout(const QScriptValue& function, int timeoutMS);
void clearInterval(QObject* timer) { stopTimer(reinterpret_cast<QTimer*>(timer)); } void clearInterval(QObject* timer) { stopTimer(reinterpret_cast<QTimer*>(timer)); }
void clearTimeout(QObject* timer) { stopTimer(reinterpret_cast<QTimer*>(timer)); } void clearTimeout(QObject* timer) { stopTimer(reinterpret_cast<QTimer*>(timer)); }
void include(const QStringList& includeFiles, QScriptValue callback = QScriptValue());
void include(const QString& includeFile); void include(const QString& includeFile);
void load(const QString& loadfile); void load(const QString& loadfile);
void print(const QString& message); void print(const QString& message);