diff options
Diffstat (limited to 'tools/qmljs/qmljs.cpp')
| -rw-r--r-- | tools/qmljs/qmljs.cpp | 70 |
1 files changed, 38 insertions, 32 deletions
diff --git a/tools/qmljs/qmljs.cpp b/tools/qmljs/qmljs.cpp index ae01e7560f..2b12c4fc9d 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,7 +100,7 @@ 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())); @@ -113,7 +119,7 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } QScopedPointer<QV4::Script> script; - if (cache && QFile::exists(fn + QLatin1Char('c'))) { + if (useCache && QFile::exists(fn + QLatin1Char('c'))) { QQmlRefPointer<QV4::ExecutableCompilationUnit> unit = QV4::ExecutableCompilationUnit::create(); QString error; @@ -134,7 +140,7 @@ 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(); } |
