From 4b2b1fc12137450d34e8324c0dbe2af1e90b9e6a Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Thu, 16 Aug 2018 13:06:22 +0200 Subject: Add support for compiling ES modules ahead of time This is also pretty straight-forward by adding .mjs as supported extension in the qmake and cmake support. This also tweaks qv4engine.cpp to share the same module compilation function across all code paths. Change-Id: Ia0e23c78a794f2330ecf8f991ee6ea948f4ac89d Reviewed-by: Qt CI Bot Reviewed-by: Lars Knoll --- tests/auto/qml/qmlcachegen/jsmoduleimport.qml | 6 +++++ tests/auto/qml/qmlcachegen/qmlcachegen.pro | 2 ++ tests/auto/qml/qmlcachegen/script.mjs | 4 ++++ tests/auto/qml/qmlcachegen/tst_qmlcachegen.cpp | 31 ++++++++++++++++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 tests/auto/qml/qmlcachegen/jsmoduleimport.qml create mode 100644 tests/auto/qml/qmlcachegen/script.mjs (limited to 'tests/auto/qml/qmlcachegen') diff --git a/tests/auto/qml/qmlcachegen/jsmoduleimport.qml b/tests/auto/qml/qmlcachegen/jsmoduleimport.qml new file mode 100644 index 0000000000..c1fad7fee2 --- /dev/null +++ b/tests/auto/qml/qmlcachegen/jsmoduleimport.qml @@ -0,0 +1,6 @@ +import QtQml 2.0 +import "script.mjs" as Script + +QtObject { + property bool ok: Script.ok() +} diff --git a/tests/auto/qml/qmlcachegen/qmlcachegen.pro b/tests/auto/qml/qmlcachegen/qmlcachegen.pro index 6dee2a0454..7f7b3128cf 100644 --- a/tests/auto/qml/qmlcachegen/qmlcachegen.pro +++ b/tests/auto/qml/qmlcachegen/qmlcachegen.pro @@ -19,4 +19,6 @@ RESOURCES += Enums.qml # QTBUG-46375 !win32: RESOURCES += trickypaths_umlaut.qrc +RESOURCES += jsmoduleimport.qml script.mjs + QT += core-private qml-private testlib diff --git a/tests/auto/qml/qmlcachegen/script.mjs b/tests/auto/qml/qmlcachegen/script.mjs new file mode 100644 index 0000000000..459c336125 --- /dev/null +++ b/tests/auto/qml/qmlcachegen/script.mjs @@ -0,0 +1,4 @@ + +export function ok() { + return true +} diff --git a/tests/auto/qml/qmlcachegen/tst_qmlcachegen.cpp b/tests/auto/qml/qmlcachegen/tst_qmlcachegen.cpp index 17c12b87e8..6c399f6874 100644 --- a/tests/auto/qml/qmlcachegen/tst_qmlcachegen.cpp +++ b/tests/auto/qml/qmlcachegen/tst_qmlcachegen.cpp @@ -59,6 +59,7 @@ private slots: void qrcScriptImport(); void fsScriptImport(); + void moduleScriptImport(); void enums(); @@ -538,6 +539,36 @@ void tst_qmlcachegen::fsScriptImport() QCOMPARE(obj->property("value").toInt(), 42); } +void tst_qmlcachegen::moduleScriptImport() +{ + QQmlEngine engine; + CleanlyLoadingComponent component(&engine, QUrl("qrc:///jsmoduleimport.qml")); + QVERIFY2(!component.isError(), qPrintable(component.errorString())); + QScopedPointer obj(component.create()); + QVERIFY(!obj.isNull()); + QTRY_VERIFY(obj->property("ok").toBool()); + + QVERIFY(QFile::exists(":/script.mjs")); + QCOMPARE(QFileInfo(":/script.mjs").size(), 0); + + { + auto componentPrivate = QQmlComponentPrivate::get(&component); + QVERIFY(componentPrivate); + auto compilationUnit = componentPrivate->compilationUnit->dependentScripts.first()->compilationUnit(); + QVERIFY(compilationUnit); + auto unitData = compilationUnit->unitData(); + QVERIFY(unitData); + QVERIFY(unitData->flags & QV4::CompiledData::Unit::StaticData); + QVERIFY(unitData->flags & QV4::CompiledData::Unit::IsESModule); + + QQmlMetaType::CachedUnitLookupError error = QQmlMetaType::CachedUnitLookupError::NoError; + const QV4::CompiledData::Unit *unitFromResources = QQmlMetaType::findCachedCompilationUnit(QUrl("qrc:/script.mjs"), &error); + QVERIFY(unitFromResources); + + QCOMPARE(unitFromResources, compilationUnit->unitData()); + } +} + void tst_qmlcachegen::enums() { QQmlEngine engine; -- cgit v1.2.3