Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 2 files). Show all
Unified · Split
| @@ -31,6 +31,7 @@ · 30 unmodified lines | ||
| 31 | 31 | |
| 32 | 32 | #include "AnimationObject.h" |
| 33 | 33 | #include "ArrayBufferViewClass.h" |
| 34 | +#include "BatchLoader.h" | |
| 34 | 35 | #include "DataViewClass.h" |
| 35 | 36 | #include "EventTypes.h" |
| 36 | 37 | #include "MenuItemProperties.h" |
| @@ -304,7 +305,7 @@ QScriptValue ScriptEngine::evaluate(const QString& program, const QString& fileN · 267 unmodified lines | ||
| 304 | 305 | QScriptValue result = QScriptEngine::evaluate(program, fileName, lineNumber); |
| 305 | 306 | if (hasUncaughtException()) { |
| 306 | 307 | int line = uncaughtExceptionLineNumber(); |
| 307 | − qDebug() << "Uncaught exception at (" << _fileNameString << ") line" << line << ": " << result.toString(); | |
| 308 | + qDebug() << "Uncaught exception at (" << _fileNameString << " : " << fileName << ") line" << line << ": " << result.toString(); | |
| 308 | 309 | } |
| 309 | 310 | emit evaluationFinished(result, hasUncaughtException()); |
| 310 | 311 | clearExceptions(); |
| @@ -595,44 +596,50 @@ void ScriptEngine::print(const QString& message) { · 284 unmodified lines | ||
| 595 | 596 | emit printedMessage(message); |
| 596 | 597 | } |
| 597 | 598 | |
| 598 | −void ScriptEngine::include(const QString& includeFile) { | |
| 599 | − QUrl url = resolvePath(includeFile); | |
| 600 | − QString includeContents; | |
| 599 | +void ScriptEngine::include(const QStringList& includeFiles, QScriptValue callback) { | |
| 600 | + QList<QUrl> urls; | |
| 601 | + for (QString file : includeFiles) { | |
| 602 | + urls.append(resolvePath(file)); | |
| 603 | + } | |
| 604 | + | |
| 605 | + BatchLoader* loader = new BatchLoader(urls); | |
| 606 | + | |
| 607 | + auto evaluateScripts = [=](const QMap<QUrl, QString>& data) { | |
| 608 | + for (QUrl url : urls) { | |
| 609 | + QString contents = data[url]; | |
| 610 | + qDebug() << "About to load: " << url; | |
| 611 | + if (contents.isNull()) { | |
| 612 | + qDebug() << "Error loading file: " << url; | |
| 613 | + } else { | |
| 614 | + QScriptValue result = evaluate(contents, url.toString()); | |
| 615 | + } | |
| 616 | + } | |
| 617 | + | |
| 618 | + if (callback.isFunction()) { | |
| 619 | + QScriptValue(callback).call(); | |
| 620 | + } | |
| 621 | + | |
| 622 | + loader->deleteLater(); | |
| 623 | + }; | |
| 624 | + | |
| 625 | + connect(loader, &BatchLoader::finished, this, evaluateScripts); | |
| 601 | 626 | |
| 602 | − if (url.scheme() == "http" || url.scheme() == "https" || url.scheme() == "ftp") { | |
| 603 | − QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance(); | |
| 604 | − QNetworkReply* reply = networkAccessManager.get(QNetworkRequest(url)); | |
| 605 | − qDebug() << "Downloading included script at" << includeFile; | |
| 627 | + // If we are destroyed before the loader completes, make sure to clean it up | |
| 628 | + connect(this, &QObject::destroyed, loader, &QObject::deleteLater); | |
| 629 | + | |
| 630 | + loader->start(); | |
| 631 | + | |
| 632 | + if (!callback.isFunction() && !loader->isFinished()) { | |
| 606 | 633 | QEventLoop loop; |
| 607 | − QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); | |
| 634 | + QObject::connect(loader, &BatchLoader::finished, &loop, &QEventLoop::quit); | |
| 608 | 635 | loop.exec(); |
| 609 | − includeContents = reply->readAll(); | |
| 610 | − reply->deleteLater(); | |
| 611 | − } else { | |
| 612 | −#ifdef _WIN32 | |
| 613 | − QString fileName = url.toString(); | |
| 614 | −#else | |
| 615 | − QString fileName = url.toLocalFile(); | |
| 616 | −#endif | |
| 617 | − | |
| 618 | − QFile scriptFile(fileName); | |
| 619 | − if (scriptFile.open(QFile::ReadOnly | QFile::Text)) { | |
| 620 | − qDebug() << "Including file:" << fileName; | |
| 621 | − QTextStream in(&scriptFile); | |
| 622 | − includeContents = in.readAll(); | |
| 623 | − } else { | |
| 624 | − qDebug() << "ERROR Including file:" << fileName; | |
| 625 | − emit errorMessage("ERROR Including file:" + fileName); | |
| 626 | − } | |
| 627 | 636 | } |
| 637 | +} | |
| 628 | 638 | |
| 629 | − QScriptValue result = evaluate(includeContents); | |
| 630 | − if (hasUncaughtException()) { | |
| 631 | − int line = uncaughtExceptionLineNumber(); | |
| 632 | − qDebug() << "Uncaught exception at (" << includeFile << ") line" << line << ":" << result.toString(); | |
| 633 | − emit errorMessage("Uncaught exception at (" + includeFile + ") line" + QString::number(line) + ":" + result.toString()); | |
| 634 | − clearExceptions(); | |
| 635 | − } | |
| 639 | +void ScriptEngine::include(const QString& includeFile) { | |
| 640 | + QStringList urls; | |
| 641 | + urls.append(includeFile); | |
| 642 | + include(urls); | |
| 636 | 643 | } |
| 637 | 644 | |
| 638 | 645 | void ScriptEngine::load(const QString& loadFile) { |