diff options
author | Simon Hausmann <simon.hausmann@qt.io> | 2018-07-10 14:52:34 +0200 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@qt.io> | 2018-08-09 13:18:39 +0000 |
commit | 82da798499aa8b656e771191332864a703069739 (patch) | |
tree | 35cb1d0ef8dd3d949f8b6f6324d19ec577b3f4df /tools/qmljs | |
parent | 6510046ee32ef69d7f250fd1d829063983f93fdd (diff) |
Add initial basic support for ES6 modules
The entry point from the parsing perspective into modules is not
QV4::Script but QV4::ExecutionEngine::compileModule.
For convenience, the ESModule AST node gets a body, which is the
statement list connected between the ModuleItemList items that are not
import/export declarations.
The QV4::Module allocates a call context where the exported variables
are stored as named locals. This will also become the module namespace
object.
The imports in turn is an array of value pointers that point into the
locals array of the context of the imported modules.
The default module loading in ExecutionEngine assumes the accessibility
of module urls via QFile (so local file system or resource). This is
what qmljs also uses and QJSEngine as well via public API in the future.
The test runner compiles the modules manually and injects them, because
they need to be compiled together with the test harness code.
The QML type loader will the mechanism for injection in the future for
module imports from .qml files.
Change-Id: I93be9cfe54c651fdbd08c5e1d22d58f47284e54f
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'tools/qmljs')
-rw-r--r-- | tools/qmljs/qmljs.cpp | 46 |
1 files changed, 31 insertions, 15 deletions
diff --git a/tools/qmljs/qmljs.cpp b/tools/qmljs/qmljs.cpp index 51670fd44c..56d4a7d383 100644 --- a/tools/qmljs/qmljs.cpp +++ b/tools/qmljs/qmljs.cpp @@ -37,6 +37,7 @@ #include "private/qv4context_p.h" #include "private/qv4script_p.h" #include "private/qv4string_p.h" +#include "private/qv4module_p.h" #include "private/qqmlbuiltinfunctions_p.h" #include <QtCore/QCoreApplication> @@ -77,6 +78,7 @@ int main(int argc, char *argv[]) args.removeFirst(); bool runAsQml = false; + bool runAsModule = false; bool cache = false; if (!args.isEmpty()) { @@ -94,6 +96,11 @@ int main(int argc, char *argv[]) args.removeFirst(); } + if (args.constFirst() == QLatin1String("--module")) { + runAsModule = true; + args.removeFirst(); + } + if (args.constFirst() == QLatin1String("--cache")) { cache = true; args.removeFirst(); @@ -113,8 +120,21 @@ int main(int argc, char *argv[]) QV4::GlobalExtensions::init(vm.globalObject, QJSEngine::ConsoleExtension | QJSEngine::GarbageCollectionExtension); for (const QString &fn : qAsConst(args)) { - QFile file(fn); - if (file.open(QFile::ReadOnly)) { + QV4::ScopedValue result(scope); + if (runAsModule) { + auto moduleUnit = vm.loadModule(QUrl::fromLocalFile(QFileInfo(fn).absoluteFilePath())); + if (moduleUnit) { + if (moduleUnit->instantiate(&vm)) + moduleUnit->evaluate(); + } else { + vm.throwError(QStringLiteral("Could not load module file")); + } + } else { + QFile file(fn); + if (!file.open(QFile::ReadOnly)) { + std::cerr << "Error: cannot open file " << fn.toUtf8().constData() << std::endl; + return EXIT_FAILURE; + } QScopedPointer<QV4::Script> script; if (cache && QFile::exists(fn + QLatin1Char('c'))) { QQmlRefPointer<QV4::CompiledData::CompilationUnit> unit = QV4::Compiler::Codegen::createUnitForLoading(); @@ -133,7 +153,6 @@ int main(int argc, char *argv[]) script->parseAsBinding = runAsQml; script->parse(); } - QV4::ScopedValue result(scope); if (!scope.engine->hasException) { const auto unit = script->compilationUnit; if (cache && unit && !(unit->unitData()->flags & QV4::CompiledData::Unit::StaticData)) { @@ -149,20 +168,17 @@ int main(int argc, char *argv[]) result = script->run(); // std::cout << t.elapsed() << " ms. elapsed" << std::endl; } - if (scope.engine->hasException) { - QV4::StackTrace trace; - QV4::ScopedValue ex(scope, scope.engine->catchException(&trace)); - showException(ctx, ex, trace); - return EXIT_FAILURE; - } - if (!result->isUndefined()) { - if (! qgetenv("SHOW_EXIT_VALUE").isEmpty()) - std::cout << "exit value: " << qPrintable(result->toQString()) << std::endl; - } - } else { - std::cerr << "Error: cannot open file " << fn.toUtf8().constData() << std::endl; + } + if (scope.engine->hasException) { + QV4::StackTrace trace; + QV4::ScopedValue ex(scope, scope.engine->catchException(&trace)); + showException(ctx, ex, trace); return EXIT_FAILURE; } + if (!result->isUndefined()) { + if (! qgetenv("SHOW_EXIT_VALUE").isEmpty()) + std::cout << "exit value: " << qPrintable(result->toQString()) << std::endl; + } } return EXIT_SUCCESS; |