From 85dcfcae790ea9cf2f59e5eb3913a10d29728038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amanda=20Hamblin-Tru=C3=A9?= Date: Tue, 27 Jun 2023 11:43:12 +0200 Subject: qmljs: Port to QCommandLineParser Additionally, added a sanity check to return EXIT_FAILURE if JIT and interpreter options are both enabled. Change-Id: I55433963f3539dac8cc4193ec64ec257bcc48f6f Reviewed-by: Ulf Hermann --- tools/qmljs/qmljs.cpp | 70 ++++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 32 deletions(-) (limited to 'tools/qmljs/qmljs.cpp') 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 #include #include +#include #include #include #include @@ -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 script; - if (cache && QFile::exists(fn + QLatin1Char('c'))) { + if (useCache && QFile::exists(fn + QLatin1Char('c'))) { QQmlRefPointer 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(unit->unitData())->sourceTimeStamp = QFileInfo(fn).lastModified().toMSecsSinceEpoch(); } -- cgit v1.2.3