diff options
-rw-r--r-- | tests/auto/qml/qmlcachegen/tst_qmlcachegen.cpp | 74 | ||||
-rw-r--r-- | tests/auto/qml/qmldiskcache/tst_qmldiskcache.cpp | 4 | ||||
-rw-r--r-- | tools/qmlcachegen/qmlcachegen.cpp | 4 |
3 files changed, 80 insertions, 2 deletions
diff --git a/tests/auto/qml/qmlcachegen/tst_qmlcachegen.cpp b/tests/auto/qml/qmlcachegen/tst_qmlcachegen.cpp index cba5d2588e..c9e831a6c1 100644 --- a/tests/auto/qml/qmlcachegen/tst_qmlcachegen.cpp +++ b/tests/auto/qml/qmlcachegen/tst_qmlcachegen.cpp @@ -57,9 +57,12 @@ private slots: void trickyPaths_data(); void trickyPaths(); - void scriptImport(); + void qrcScriptImport(); + void fsScriptImport(); void enums(); + + void sourceFileIndices(); }; // A wrapper around QQmlComponent to ensure the temporary reference counts @@ -145,6 +148,7 @@ void tst_qmlcachegen::loadGeneratedFile() QVERIFY(cacheUnit); QVERIFY(cacheUnit->flags & QV4::CompiledData::Unit::StaticData); QVERIFY(cacheUnit->flags & QV4::CompiledData::Unit::PendingTypeCompilation); + QCOMPARE(uint(cacheUnit->sourceFileIndex), uint(0)); } QVERIFY(QFile::remove(testFilePath)); @@ -443,7 +447,7 @@ void tst_qmlcachegen::trickyPaths() QCOMPARE(obj->property("success").toInt(), 42); } -void tst_qmlcachegen::scriptImport() +void tst_qmlcachegen::qrcScriptImport() { QQmlEngine engine; CleanlyLoadingComponent component(&engine, QUrl("qrc:///jsimport.qml")); @@ -452,6 +456,60 @@ void tst_qmlcachegen::scriptImport() QTRY_COMPARE(obj->property("value").toInt(), 42); } +void tst_qmlcachegen::fsScriptImport() +{ + QTemporaryDir tempDir; + QVERIFY(tempDir.isValid()); + + const auto writeTempFile = [&tempDir](const QString &fileName, const char *contents) { + QFile f(tempDir.path() + '/' + fileName); + const bool ok = f.open(QIODevice::WriteOnly | QIODevice::Truncate); + Q_ASSERT(ok); + f.write(contents); + return f.fileName(); + }; + + const QString testFilePath = writeTempFile( + "test.qml", + "import QtQml 2.0\n" + "import \"test.js\" as ScriptTest\n" + "QtObject {\n" + " property int value: ScriptTest.value\n" + "}\n"); + + const QString scriptFilePath = writeTempFile( + "test.js", + "var value = 42" + ); + + QVERIFY(generateCache(scriptFilePath)); + QVERIFY(generateCache(testFilePath)); + + const QString scriptCacheFilePath = scriptFilePath + QLatin1Char('c'); + QVERIFY(QFile::exists(scriptFilePath)); + + { + QFile cache(scriptCacheFilePath); + QVERIFY(cache.open(QIODevice::ReadOnly)); + const QV4::CompiledData::Unit *cacheUnit = reinterpret_cast<const QV4::CompiledData::Unit *>(cache.map(/*offset*/0, sizeof(QV4::CompiledData::Unit))); + QVERIFY(cacheUnit); + QVERIFY(cacheUnit->flags & QV4::CompiledData::Unit::StaticData); + QVERIFY(!(cacheUnit->flags & QV4::CompiledData::Unit::PendingTypeCompilation)); + QCOMPARE(uint(cacheUnit->sourceFileIndex), uint(0)); + } + + // Remove source code to make sure that when loading succeeds, it is because we loaded + // the existing cache files. + QVERIFY(QFile::remove(testFilePath)); + QVERIFY(QFile::remove(scriptFilePath)); + + QQmlEngine engine; + CleanlyLoadingComponent component(&engine, QUrl::fromLocalFile(testFilePath)); + QScopedPointer<QObject> obj(component.create()); + QVERIFY(!obj.isNull()); + QCOMPARE(obj->property("value").toInt(), 42); +} + void tst_qmlcachegen::enums() { QQmlEngine engine; @@ -461,6 +519,18 @@ void tst_qmlcachegen::enums() QTRY_COMPARE(obj->property("value").toInt(), 200); } +void tst_qmlcachegen::sourceFileIndices() +{ + QVERIFY(QFile::exists(":/versionchecks.qml")); + QCOMPARE(QFileInfo(":/versionchecks.qml").size(), 0); + + QQmlMetaType::CachedUnitLookupError error = QQmlMetaType::CachedUnitLookupError::NoError; + const QV4::CompiledData::Unit *unitFromResources = QQmlMetaType::findCachedCompilationUnit(QUrl("qrc:/versionchecks.qml"), &error); + QVERIFY(unitFromResources); + QVERIFY(unitFromResources->flags & QV4::CompiledData::Unit::PendingTypeCompilation); + QCOMPARE(uint(unitFromResources->sourceFileIndex), uint(0)); +} + QTEST_GUILESS_MAIN(tst_qmlcachegen) #include "tst_qmlcachegen.moc" diff --git a/tests/auto/qml/qmldiskcache/tst_qmldiskcache.cpp b/tests/auto/qml/qmldiskcache/tst_qmldiskcache.cpp index 20eef9c322..917ac9e74e 100644 --- a/tests/auto/qml/qmldiskcache/tst_qmldiskcache.cpp +++ b/tests/auto/qml/qmldiskcache/tst_qmldiskcache.cpp @@ -495,6 +495,10 @@ void tst_qmldiskcache::recompileAfterDirectoryChange() testCompiler.clearCache(); QVERIFY2(testCompiler.compile(contents), qPrintable(testCompiler.lastErrorString)); QVERIFY2(testCompiler.verify(), qPrintable(testCompiler.lastErrorString)); + const QV4::CompiledData::Unit *unit = testCompiler.mapUnit(); + QVERIFY(unit->sourceFileIndex != 0); + const QString expectedPath = QUrl::fromLocalFile(testCompiler.testFilePath).toString(); + QCOMPARE(unit->stringAt(unit->sourceFileIndex), expectedPath); testCompiler.closeMapping(); } diff --git a/tools/qmlcachegen/qmlcachegen.cpp b/tools/qmlcachegen/qmlcachegen.cpp index b9a8763c97..34814af17c 100644 --- a/tools/qmlcachegen/qmlcachegen.cpp +++ b/tools/qmlcachegen/qmlcachegen.cpp @@ -317,6 +317,10 @@ static bool compileJSFile(const QString &inputFileName, const QString &inputFile return false; } + // Precompiled files are relocatable and the final location will be set when loading. + irDocument.jsModule.fileName.clear(); + irDocument.jsModule.finalUrl.clear(); + QmlIR::QmlUnitGenerator generator; irDocument.javaScriptCompilationUnit = v4CodeGen.generateCompilationUnit(/*generate unit*/false); |