28 *errorString = QStringLiteral(
"Magic bytes in the header do not match");
33 *errorString = QString::fromUtf8(
"V4 data structure version mismatch. Found %1 expected %2")
38 switch (sourceTimeStamp) {
53 if (!expectedSourceTimeStamp.isValid()) {
54 expectedSourceTimeStamp = QFileInfo(QCoreApplication::applicationFilePath()).lastModified();
55 if (!expectedSourceTimeStamp.isValid()) {
57 QStringLiteral(
"Failed to get valid timestamp from application executable");
61 if (expectedSourceTimeStamp.toMSecsSinceEpoch() != sourceTimeStamp) {
63 QStringLiteral(
"QML source file has a different time stamp than cached file.");
76 QCryptographicHash *hash, QHash<quintptr, QByteArray> *checksums)
const
78 std::vector<
int> keys (size());
80 for (
auto it = constBegin(), end = constEnd(); it != end; ++it) {
84 std::sort(keys.begin(), keys.end());
86 if (!
this->operator[](key)->addToHash(hash, checksums))
120 static const QByteArray envCachePath = qgetenv(
"QML_DISK_CACHE_PATH");
122 const QString localSourcePath = QQmlFile::urlToLocalFileOrQrc(url);
123 const QString cacheFileSuffix
124 = QFileInfo(localSourcePath + QLatin1Char(
'c')).completeSuffix();
125 QCryptographicHash fileNameHash(QCryptographicHash::Sha1);
126 fileNameHash.addData(localSourcePath.toUtf8());
127 QString directory = envCachePath.isEmpty()
128 ? QStandardPaths::writableLocation(QStandardPaths::CacheLocation)
129 + QLatin1String(
"/qmlcache/")
130 : QString::fromLocal8Bit(envCachePath) + QLatin1String(
"/");
131 QDir::root().mkpath(directory);
132 return directory + QString::fromUtf8(fileNameHash.result().toHex())
133 + QLatin1Char(
'.') + cacheFileSuffix;
144 const QUrl &url,
const QDateTime &sourceTimeStamp,
147 if (!QQmlFile::isLocalFile(url)) {
148 *errorString = QStringLiteral(
"File has to be a local file.");
152 const QString sourcePath = QQmlFile::urlToLocalFileOrQrc(url);
153 auto cacheFile = std::make_unique<CompilationUnitMapper>();
155 const QStringList cachePaths = { sourcePath + QLatin1Char(
'c'), localCacheFilePath(url) };
156 for (
const QString &cachePath : cachePaths) {
157 Unit *mappedUnit = cacheFile->get(cachePath, sourceTimeStamp, errorString);
161 const Unit *oldData = unitData();
162 const Unit *
const oldDataPtr
163 = (oldData && !(oldData->flags & Unit::StaticData))
167 auto dataPtrRevert = qScopeGuard([
this, oldData](){
168 setUnitData(oldData);
170 setUnitData(mappedUnit);
172 if (mappedUnit->sourceTimeStamp == -1) {
173 const QByteArray checksum = sourceChecksum();
174 if (checksum.size() !=
sizeof(mappedUnit->sourceChecksum)
175 || memcmp(mappedUnit->sourceChecksum, checksum.constData(), checksum.size()) != 0) {
176 *errorString = QStringLiteral(
177 "QML source file has a different content checksum than cached file.");
182 if (mappedUnit->sourceFileIndex != 0) {
183 if (mappedUnit->sourceFileIndex >=
184 mappedUnit->stringTableSize + dynamicStrings.size()) {
185 *errorString = QStringLiteral(
"QML source file index is invalid.");
189 QQmlFile::urlToLocalFileOrQrc(stringAt(mappedUnit->sourceFileIndex))) {
190 *errorString = QStringLiteral(
"QML source file has moved to a different location.");
195 dataPtrRevert.dismiss();
196 free(
const_cast<Unit*>(oldDataPtr));
197 if (
const RuntimeDataBlock *mappedRuntimeData = mappedUnit->runtimeData()) {
198 runtimeData = mappedRuntimeData;
199 runtimeDataStorage.clear();
202 backingFile = std::move(cacheFile);
213 if (!QQmlFile::isLocalFile(unitUrl)) {
214 *errorString = QStringLiteral(
"File has to be a local file.");
219 const QByteArray checksum = sourceChecksum();
221 *errorString = QStringLiteral(
"Failed to compute source code checksum");
231 memcpy(&header, base,
sizeof(
Unit));
232 header.sourceTimeStamp = -1;
234 return writeWithRuntimeData(unitUrl, &header, errorString);
261 const QUrl &unitUrl,
Unit *header, QString *errorString)
const
264 const quint32 baseSize = base->runtimeDataSize ? quint32(base->offsetToRuntimeData)
265 : quint32(base->unitSize);
267 const quint32 runtimeDataSize = quint32(runtimeDataStorage.size());
270 header->offsetToRuntimeData = runtimeDataSize > 0 ? runtimeDataOffset : 0;
271 header->runtimeDataSize = runtimeDataSize;
272 header->unitSize = runtimeDataSize > 0 ? runtimeDataOffset + runtimeDataSize : baseSize;
274 const QString cachePath = localCacheFilePath(unitUrl);
275 if (!SaveableUnitPointer::writeDataToFile(
276 cachePath,
reinterpret_cast<
const char *>(header),
sizeof(
Unit),
277 reinterpret_cast<
const char *>(base) +
sizeof(
Unit),
sizeof(
Unit),
278 baseSize -
sizeof(
Unit), runtimeDataStorage.constData(), runtimeDataOffset,
279 runtimeDataSize, errorString)) {
283 CompilationUnitMapper::invalidate(cachePath);
309 if (propertyCaches.needsVMEMetaObject(0)) {
311 if (type.isValid()) {
314 qmlType = QQmlMetaType::findCompositeType(
315 url(),
this, (unitData()->flags & CompiledData::Unit::IsSingleton)
316 ? QQmlMetaType::Singleton
317 : QQmlMetaType::NonSingleton);
320 QQmlMetaType::registerInternalCompositeType(
this);
323 auto *typeRef =
resolvedTypes.value(obj->inheritedTypeNameIndex);
325 qmlType = typeRef->type();
354 const uint nObjects =
qmlData->nObjects;
358 const auto objectSpan = [&](uint nBindings) -> quint32 {
359 return alignedTo<RuntimeObject>(
sizeof(RuntimeObject) + nBindings *
sizeof(RuntimeBinding)
360 + nBindings *
sizeof(RuntimeBindingIndex));
364 for (uint i = 0; i < nObjects; ++i)
367 runtimeDataStorage = QByteArray(size, 0);
368 char *base = runtimeDataStorage.data();
372 block->nObjects = nObjects;
374 quint32_le *objectOffsetTable =
reinterpret_cast<quint32_le *>(base +
sizeof(
RuntimeDataBlock));
375 quint32 nextOffset =
sizeof(
RuntimeDataBlock) + nObjects *
sizeof(quint32_le);
377 for (uint i = 0; i < nObjects; ++i) {
379 const uint nBindings = object->nBindings;
380 objectOffsetTable[i] = nextOffset;
383 runtimeObject->offsetToBindingOrder
385 runtimeObject->objectId = -1;
386 nextOffset += objectSpan(nBindings);
403 const qsizetype tableOffset = runtimeDataStorage.size();
404 runtimeDataStorage.resize(tableOffset + count *
sizeof(quint32_le));
406 char *base = runtimeDataStorage.data();
411 runtimeObject->nNamedObjectsInComponent = count;
412 runtimeObject->offsetToNamedObjectsInComponent
413 = tableOffset - (
reinterpret_cast<
char *>(runtimeObject) - base);
414 return runtimeObject->namedObjectsInComponentTable();