diff options
Diffstat (limited to 'tools/qmljs/qmljs.cpp')
-rw-r--r-- | tools/qmljs/qmljs.cpp | 81 |
1 files changed, 44 insertions, 37 deletions
diff --git a/tools/qmljs/qmljs.cpp b/tools/qmljs/qmljs.cpp index ae01e7560f..8a1c4d0ded 100644 --- a/tools/qmljs/qmljs.cpp +++ b/tools/qmljs/qmljs.cpp @@ -19,6 +19,7 @@ #include <QtCore/QFile> #include <QtCore/QFileInfo> #include <QtCore/QDateTime> +#include <QtCore/qcommandlineparser.h> #include <private/qqmljsengine_p.h> #include <private/qqmljslexer_p.h> #include <private/qqmljsparser_p.h> @@ -49,43 +50,48 @@ int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QCoreApplication::setApplicationVersion(QLatin1String(QT_VERSION_STR)); - QStringList args = app.arguments(); - args.removeFirst(); - bool runAsQml = false; - bool runAsModule = false; - bool cache = false; + QCommandLineParser parser; + parser.addHelpOption(); + parser.setApplicationDescription("Utility to execute scripts in QML's V4 engine"); + parser.addVersionOption(); + parser.addPositionalArgument("files", "Files to execute.", "[files...]"); - if (!args.isEmpty()) { - if (args.constFirst() == QLatin1String("--jit")) { - qputenv("QV4_JIT_CALL_THRESHOLD", QByteArray("0")); - args.removeFirst(); - } - if (args.constFirst() == QLatin1String("--interpret")) { - qputenv("QV4_FORCE_INTERPRETER", QByteArray("1")); - args.removeFirst(); - } - if (args.constFirst() == QLatin1String("--qml")) { - runAsQml = true; - args.removeFirst(); - } + QCommandLineOption forceJit("jit", "Force JIT."); + parser.addOption(forceJit); - if (args.constFirst() == QLatin1String("--module")) { - runAsModule = true; - args.removeFirst(); - } + QCommandLineOption forceInterpreter("interpret", "Force interpreter."); + parser.addOption(forceInterpreter); - if (args.constFirst() == QLatin1String("--cache")) { - cache = true; - args.removeFirst(); - } + QCommandLineOption qml("qml", "Run as QML."); + parser.addOption(qml); + + QCommandLineOption module("module", "Run as Module."); + parser.addOption(module); - if (args.constFirst() == QLatin1String("--help")) { - std::cerr << "Usage: qmljs [|--jit|--interpret|--qml] file..." << std::endl; - return EXIT_SUCCESS; + QCommandLineOption cache("cache", "Use cache."); + parser.addOption(cache); + + parser.process(app); + + bool jitEnabled = false; + + if (parser.isSet(forceJit)) { + qputenv("QV4_JIT_CALL_THRESHOLD", QByteArray("0")); + jitEnabled = true; + } + if (parser.isSet(forceInterpreter)) { + qputenv("QV4_FORCE_INTERPRETER", QByteArray("1")); + if (jitEnabled) { + std::cerr << "You cannot use 'Force JIT' and 'Force Interpreter' at the same time."; + return EXIT_FAILURE; } } + const bool runAsQml = parser.isSet(qml); + const bool runAsModule = parser.isSet(module); + const bool useCache = parser.isSet(cache); + const QStringList args = parser.positionalArguments(); QV4::ExecutionEngine vm; @@ -94,12 +100,12 @@ int main(int argc, char *argv[]) QV4::GlobalExtensions::init(vm.globalObject, QJSEngine::ConsoleExtension | QJSEngine::GarbageCollectionExtension); - for (const QString &fn : std::as_const(args)) { + for (const QString &fn : args) { QV4::ScopedValue result(scope); if (runAsModule) { auto module = vm.loadModule(QUrl::fromLocalFile(QFileInfo(fn).absoluteFilePath())); if (module.compiled) { - if (module.compiled->instantiate(&vm)) + if (module.compiled->instantiate()) module.compiled->evaluate(); } else if (module.native) { // Nothing to do. Native modules have no global code. @@ -113,12 +119,12 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } QScopedPointer<QV4::Script> script; - if (cache && QFile::exists(fn + QLatin1Char('c'))) { - QQmlRefPointer<QV4::ExecutableCompilationUnit> unit - = QV4::ExecutableCompilationUnit::create(); + if (useCache && QFile::exists(fn + QLatin1Char('c'))) { + auto unit = QQml::makeRefPointer<QV4::CompiledData::CompilationUnit>(); QString error; if (unit->loadFromDisk(QUrl::fromLocalFile(fn), QFileInfo(fn).lastModified(), &error)) { - script.reset(new QV4::Script(&vm, nullptr, unit)); + script.reset(new QV4::Script( + &vm, nullptr, vm.insertCompilationUnit(std::move(unit)))); } else { std::cout << "Error loading" << qPrintable(fn) << "from disk cache:" << qPrintable(error) << std::endl; } @@ -134,12 +140,13 @@ int main(int argc, char *argv[]) } if (!scope.hasException()) { const auto unit = script->compilationUnit; - if (cache && unit && !(unit->unitData()->flags & QV4::CompiledData::Unit::StaticData)) { + if (useCache && unit && !(unit->unitData()->flags & QV4::CompiledData::Unit::StaticData)) { if (unit->unitData()->sourceTimeStamp == 0) { const_cast<QV4::CompiledData::Unit*>(unit->unitData())->sourceTimeStamp = QFileInfo(fn).lastModified().toMSecsSinceEpoch(); } QString saveError; - if (!unit->saveToDisk(QUrl::fromLocalFile(fn), &saveError)) { + if (!unit->baseCompilationUnit()->saveToDisk( + QUrl::fromLocalFile(fn), &saveError)) { std::cout << "Error saving JS cache file: " << qPrintable(saveError) << std::endl; } } |