5#include <private/qqmltypeloader_p.h>
7#include <private/qqmldirdata_p.h>
8#include <private/qqmlprofiler_p.h>
9#include <private/qqmlscriptblob_p.h>
10#include <private/qqmlscriptdata_p.h>
11#include <private/qqmlsourcecoordinate_p.h>
12#include <private/qqmltypedata_p.h>
13#include <private/qqmltypeloaderqmldircontent_p.h>
14#include <private/qqmltypeloaderthread_p.h>
15#include <private/qv4compiler_p.h>
16#include <private/qv4compilercontext_p.h>
17#include <private/qv4runtimecodegen_p.h>
19#include <QtQml/qqmlabstracturlinterceptor.h>
20#include <QtQml/qqmlengine.h>
21#include <QtQml/qqmlextensioninterface.h>
22#include <QtQml/qqmlfile.h>
23#include <QtQml/qqmlnetworkaccessmanagerfactory.h>
25#include <qtqml_tracepoints_p.h>
27#include <QtCore/qdir.h>
28#include <QtCore/qdirlisting.h>
29#include <QtCore/qfile.h>
30#include <QtCore/qlibraryinfo.h>
31#include <QtCore/qthread.h>
34#include "private/qcore_mac_p.h"
39#define ASSERT_LOADTHREAD()
40 Q_ASSERT(thread() && thread()->isThisThread())
41#define ASSERT_ENGINETHREAD()
42 Q_ASSERT(!engine()->jsEngine() || engine()->jsEngine()->thread()->isCurrentThread())
46Q_TRACE_POINT(qtqml, QQmlCompiling_entry,
const QUrl &url)
47Q_TRACE_POINT(qtqml, QQmlCompiling_exit)
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
79void QQmlTypeLoader::invalidate()
85#if QT_CONFIG(qml_network)
89 QQmlTypeLoaderThreadDataPtr data(&m_data);
90 data->networkReplies.clear();
94void QQmlTypeLoader::addUrlInterceptor(QQmlAbstractUrlInterceptor *urlInterceptor)
97 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
98 data->urlInterceptors.append(urlInterceptor);
101void QQmlTypeLoader::removeUrlInterceptor(QQmlAbstractUrlInterceptor *urlInterceptor)
104 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
105 data->urlInterceptors.removeOne(urlInterceptor);
108QList<QQmlAbstractUrlInterceptor *> QQmlTypeLoader::urlInterceptors()
const
111 QQmlTypeLoaderConfiguredDataConstPtr data(&m_data);
112 return data->urlInterceptors;
115static QUrl doInterceptUrl(
116 const QUrl &url, QQmlAbstractUrlInterceptor::DataType type,
117 const QList<QQmlAbstractUrlInterceptor *> &urlInterceptors)
120 for (QQmlAbstractUrlInterceptor *interceptor : urlInterceptors)
121 result = interceptor->intercept(result, type);
125QUrl QQmlTypeLoader::interceptUrl(
const QUrl &url, QQmlAbstractUrlInterceptor::DataType type)
const
129 QQmlTypeLoaderConfiguredDataConstPtr data(&m_data);
130 return doInterceptUrl(url, type, data->urlInterceptors);
133bool QQmlTypeLoader::hasUrlInterceptors()
const
136 QQmlTypeLoaderConfiguredDataConstPtr data(&m_data);
137 return !data->urlInterceptors.isEmpty();
140#if QT_CONFIG(qml_debug)
141void QQmlTypeLoader::setProfiler(QQmlProfiler *profiler)
143 ASSERT_ENGINETHREAD();
145 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
146 Q_ASSERT(!data->profiler);
147 data->profiler.reset(profiler);
152 void loadThread(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
154 loader->loadThread(blob);
156 void load(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
158 loader->ensureThread()->load(blob);
160 void loadAsync(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
162 loader->ensureThread()->loadAsync(blob);
170 void loadThread(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
172 loader->loadWithStaticDataThread(blob, data);
174 void load(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
176 loader->ensureThread()->loadWithStaticData(blob, data);
178 void loadAsync(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
180 loader->ensureThread()->loadWithStaticDataAsync(blob, data);
188 void loadThread(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
190 loader->loadWithCachedUnitThread(blob, unit);
192 void load(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
194 loader->ensureThread()->loadWithCachedUnit(blob, unit);
196 void loadAsync(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
198 loader->ensureThread()->loadWithCachedUnitAsync(blob, unit);
202template<
typename Loader>
203void QQmlTypeLoader::doLoad(
const Loader &loader,
const QQmlDataBlob::Ptr &blob, Mode mode)
207 qWarning(
"QQmlTypeLoader::doLoad(%s): %s thread", qPrintable(blob->urlString()),
208 (m_thread && m_thread->isThisThread()) ?
"Compile" :
"Engine");
210 blob->startLoading();
212 if (QQmlTypeLoaderThread *t = thread(); t && t->isThisThread()) {
213 loader.loadThread(
this, blob);
217 if (mode == Asynchronous) {
218 blob->setIsAsync(
true);
219 loader.loadAsync(
this, blob);
223 loader.load(
this, blob);
224 if (blob->isCompleteOrError())
227 if (mode == PreferSynchronous) {
228 blob->setIsAsync(
true);
232 Q_ASSERT(mode == Synchronous);
235 QQmlTypeLoaderSharedDataConstPtr lock(&m_data);
237 m_data.thread()->waitForNextMessage();
238 }
while (!blob->isCompleteOrError());
242
243
244
245
246void QQmlTypeLoader::load(
const QQmlDataBlob::Ptr &blob, Mode mode)
249 doLoad(PlainLoader(), blob, mode);
253
254
255
256
257void QQmlTypeLoader::loadWithStaticData(
258 const QQmlDataBlob::Ptr &blob,
const QByteArray &data, Mode mode)
261 doLoad(StaticLoader(data), blob, mode);
264void QQmlTypeLoader::loadWithCachedUnit(
265 const QQmlDataBlob::Ptr &blob,
const QQmlPrivate::CachedQmlUnit *unit, Mode mode)
268 doLoad(CachedLoader(unit), blob, mode);
271void QQmlTypeLoader::drop(
const QQmlDataBlob::Ptr &blob)
279 if (QQmlTypeLoaderThread *t = thread(); t && !t->isThisThread())
283void QQmlTypeLoader::loadWithStaticDataThread(
const QQmlDataBlob::Ptr &blob,
const QByteArray &data)
287 setData(blob, data, DataOrigin::Static);
290void QQmlTypeLoader::loadWithCachedUnitThread(
const QQmlDataBlob::Ptr &blob,
const QQmlPrivate::CachedQmlUnit *unit)
294 setCachedUnit(blob, unit);
297void QQmlTypeLoader::loadThread(
const QQmlDataBlob::Ptr &blob)
301 if (blob->m_url.isEmpty()) {
303 error.setDescription(QLatin1String(
"Invalid null URL"));
304 blob->setError(error);
308 if (QQmlFile::isSynchronous(blob->m_url)) {
309 const QString fileName = QQmlFile::urlToLocalFileOrQrc(blob->m_url);
310 if (!fileExists(fileName) && QFileInfo::exists(fileName)) {
313 blob->setError(QLatin1String(
"File name case mismatch"));
317 if (blob->setProgress(1.f) && blob->isAsync())
318 thread()->callDownloadProgressChanged(blob, 1.);
320 setData(blob, fileName);
323#if QT_CONFIG(qml_network)
324 QNetworkReply *reply = thread()->networkAccessManager()->get(QNetworkRequest(blob->m_url));
325 QQmlTypeLoaderNetworkReplyProxy *nrp = thread()->networkReplyProxy();
327 QQmlTypeLoaderThreadDataPtr data(&m_data);
328 data->networkReplies.insert(reply, blob);
330 if (reply->isFinished()) {
331 nrp->manualFinished(reply);
333 QObject::connect(reply, &QNetworkReply::downloadProgress,
334 nrp, &QQmlTypeLoaderNetworkReplyProxy::downloadProgress);
335 QObject::connect(reply, &QNetworkReply::finished,
336 nrp, &QQmlTypeLoaderNetworkReplyProxy::finished);
340 qWarning(
"QQmlDataBlob: requested %s", qPrintable(blob->urlString()));
346#define DATALOADER_MAXIMUM_REDIRECT_RECURSION 16
348#if QT_CONFIG(qml_network)
349void QQmlTypeLoader::networkReplyFinished(QNetworkReply *reply)
353 reply->deleteLater();
355 QQmlTypeLoaderThreadDataPtr data(&m_data);
356 QQmlRefPointer<QQmlDataBlob> blob = data->networkReplies.take(reply);
360 blob->m_redirectCount++;
362 if (blob->m_redirectCount < DATALOADER_MAXIMUM_REDIRECT_RECURSION) {
363 QVariant redirect = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
364 if (redirect.isValid()) {
365 QUrl url = reply->url().resolved(redirect.toUrl());
366 blob->m_finalUrl = url;
367 blob->m_finalUrlString.clear();
369 QNetworkReply *reply = thread()->networkAccessManager()->get(QNetworkRequest(url));
370 QObject *nrp = thread()->networkReplyProxy();
371 QObject::connect(reply, SIGNAL(finished()), nrp, SLOT(finished()));
372 data->networkReplies.insert(reply, std::move(blob));
374 qWarning(
"QQmlDataBlob: redirected to %s", qPrintable(blob->finalUrlString()));
380 if (reply->error()) {
381 blob->networkError(reply->error());
383 QByteArray data = reply->readAll();
384 setData(blob, data, DataOrigin::Device);
388void QQmlTypeLoader::networkReplyProgress(QNetworkReply *reply,
389 qint64 bytesReceived, qint64 bytesTotal)
393 QQmlTypeLoaderThreadDataConstPtr data(&m_data);
394 const QQmlRefPointer<QQmlDataBlob> blob = data->networkReplies.value(reply);
398 if (bytesTotal != 0) {
399 const qreal progress = (qreal(bytesReceived) / qreal(bytesTotal));
400 if (blob->setProgress(progress) && blob->isAsync())
401 thread()->callDownloadProgressChanged(blob, blob->progress());
407
408
409
410template<
class Interface>
412 Interface *iface, QQmlTypeLoaderThread *thread, QQmlTypeLoaderLockedData *data,
418 if (thread && thread->isThisThread())
419 thread->initializeEngine(iface, uri);
421 iface->initializeEngine(data->engine()->qmlEngine(), uri);
424void QQmlTypeLoader::initializeEngine(QQmlEngineExtensionInterface *iface,
const char *uri)
427 doInitializeEngine(iface, thread(), &m_data, uri);
430void QQmlTypeLoader::initializeEngine(QQmlExtensionInterface *iface,
const char *uri)
433 doInitializeEngine(iface, thread(), &m_data, uri);
437
438
439
440
441
442
443
444
445void QQmlTypeLoader::setData(
const QQmlDataBlob::Ptr &blob,
const QByteArray &data, DataOrigin origin)
449 QQmlDataBlob::SourceCodeData d;
450 d.inlineSourceCode = QString::fromUtf8(data);
451 d.hasInlineSourceCode =
true;
452 d.hasStaticData = (origin == DataOrigin::Static);
456void QQmlTypeLoader::setData(
const QQmlDataBlob::Ptr &blob,
const QString &fileName)
460 QQmlDataBlob::SourceCodeData d;
461 d.fileInfo = QFileInfo(fileName);
465void QQmlTypeLoader::setData(
const QQmlDataBlob::Ptr &blob,
const QQmlDataBlob::SourceCodeData &d)
469 Q_TRACE_SCOPE(QQmlCompiling, blob->url());
470 QQmlCompilingProfiler prof(profiler(), blob.data());
472 blob->m_inCallback =
true;
474 blob->dataReceived(d);
476 if (!blob->isError() && !blob->isWaiting())
477 blob->allDependenciesDone();
479 blob->m_inCallback =
false;
484void QQmlTypeLoader::setCachedUnit(
const QQmlDataBlob::Ptr &blob,
const QQmlPrivate::CachedQmlUnit *unit)
488 Q_TRACE_SCOPE(QQmlCompiling, blob->url());
489 QQmlCompilingProfiler prof(profiler(), blob.data());
491 blob->m_inCallback =
true;
493 blob->initializeFromCachedUnit(unit);
495 if (!blob->isError() && !blob->isWaiting())
496 blob->allDependenciesDone();
498 blob->m_inCallback =
false;
505#if defined(Q_OS_UNIX)
506 return (path.at(0) == QLatin1Char(
'/'));
509 return fi.isAbsolute();
515
516
517QStringList QQmlTypeLoader::importPathList(PathType type)
const
519 QQmlTypeLoaderConfiguredDataConstPtr data(&m_data);
520 if (type == LocalOrRemote)
521 return data->importPaths;
524 for (
const QString &path : data->importPaths) {
525 bool localPath = isPathAbsolute(path) || QQmlFile::isLocalFile(path);
526 if (localPath == (type == Local))
534
535
536void QQmlTypeLoader::addImportPath(
const QString &path)
538 qCDebug(lcQmlImport) <<
"addImportPath:" << path;
543 QUrl url = QUrl(path);
546 if (url.scheme() == QLatin1String(
"file")) {
547 cPath = QQmlFile::urlToLocalFileOrQrc(url);
548 }
else if (path.startsWith(QLatin1Char(
':'))) {
551 cPath = QLatin1String(
"qrc") + path;
552 cPath.replace(QLatin1Char(
'\\'), QLatin1Char(
'/'));
553 }
else if (url.isRelative() ||
554 (url.scheme().size() == 1 && QFile::exists(path)) ) {
555 QDir dir = QDir(path);
556 cPath = dir.canonicalPath();
559 cPath.replace(QLatin1Char(
'\\'), QLatin1Char(
'/'));
562 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
563 if (!cPath.isEmpty()) {
564 if (data->importPaths.contains(cPath))
565 data->importPaths.move(data->importPaths.indexOf(cPath), 0);
567 data->importPaths.prepend(cPath);
572
573
574void QQmlTypeLoader::setImportPathList(
const QStringList &paths)
576 qCDebug(lcQmlImport) <<
"setImportPathList:" << paths;
578 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
579 data->importPaths.clear();
580 for (
auto it = paths.crbegin(); it != paths.crend(); ++it)
589
590
591void QQmlTypeLoader::setPluginPathList(
const QStringList &paths)
593 qCDebug(lcQmlImport) <<
"setPluginPathList:" << paths;
594 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
595 data->pluginPaths = paths;
599
600
601void QQmlTypeLoader::addPluginPath(
const QString& path)
603 qCDebug(lcQmlImport) <<
"addPluginPath:" << path;
605 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
607 QUrl url = QUrl(path);
608 if (url.isRelative() || url.scheme() == QLatin1String(
"file")
609 || (url.scheme().size() == 1 && QFile::exists(path)) ) {
610 QDir dir = QDir(path);
611 data->pluginPaths.prepend(dir.canonicalPath());
613 data->pluginPaths.prepend(path);
617#if QT_CONFIG(qml_network)
618QQmlNetworkAccessManagerFactoryPtrConst QQmlTypeLoader::networkAccessManagerFactory()
const
620 ASSERT_ENGINETHREAD();
621 return QQmlNetworkAccessManagerFactoryPtrConst(&m_data);
624void QQmlTypeLoader::setNetworkAccessManagerFactory(QQmlNetworkAccessManagerFactory *factory)
626 ASSERT_ENGINETHREAD();
627 QQmlNetworkAccessManagerFactoryPtr(&m_data).reset(factory);
630QNetworkAccessManager *QQmlTypeLoader::createNetworkAccessManager(QObject *parent)
const
639 if (
const auto factory = QQmlNetworkAccessManagerFactoryPtrConst(&m_data))
640 return factory->create(parent);
642 return new QNetworkAccessManager(parent);
646void QQmlTypeLoader::clearQmldirInfo()
648 QQmlTypeLoaderThreadDataPtr data(&m_data);
650 auto itr = data->qmldirInfo.constBegin();
651 while (itr != data->qmldirInfo.constEnd()) {
652 const QQmlTypeLoaderThreadData::QmldirInfo *cache = *itr;
654 const QQmlTypeLoaderThreadData::QmldirInfo *nextCache = cache->next;
661 data->qmldirInfo.clear();
665 const QQmlTypeLoaderConfiguredDataPtr &data, QV4::ExecutionEngine *engine)
667 data->diskCacheOptions = engine->diskCacheOptions();
668 data->isDebugging = engine->debugger() !=
nullptr;
669 data->initialized =
true;
672void QQmlTypeLoader::startThread()
676 if (!m_data.thread()) {
681 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
682 initializeConfiguredData(data, m_data.engine());
683 m_data.createThread(
this);
687void QQmlTypeLoader::shutdownThread()
692 m_data.deleteThread();
695QQmlTypeLoader::Blob::PendingImport::PendingImport(
696 const QQmlRefPointer<Blob> &blob,
const QV4::CompiledData::Import *import,
697 QQmlImports::ImportFlags flags)
698 : uri(blob->stringAt(import->uriIndex))
699 , qualifier(blob->stringAt(import->qualifierIndex))
700 , type(
static_cast<QV4::CompiledData::Import::ImportType>(quint32(import->type)))
701 , location(import->location)
703 , version(import->version)
707QQmlTypeLoader::Blob::Blob(
const QUrl &url, QQmlDataBlob::Type type, QQmlTypeLoader *loader)
708 : QQmlDataBlob(url, type, loader)
709 , m_importCache(
new QQmlImports(), QQmlRefPointer<QQmlImports>::Adopt)
713QQmlTypeLoader::Blob::~Blob()
717bool QQmlTypeLoader::Blob::fetchQmldir(
718 const QUrl &url,
const QQmlTypeLoader::Blob::PendingImportPtr &import,
int priority,
719 QList<QQmlError> *errors)
721 assertTypeLoaderThread();
723 QQmlRefPointer<QQmlQmldirData> data = typeLoader()->getQmldir(url);
725 data->setPriority(
this, import, priority);
727 if (data->status() == Error) {
730 }
else if (data->status() == Complete) {
732 return qmldirDataAvailable(data, errors);
736 addDependency(data.data());
741
742
743
744
745void QQmlTypeLoader::Blob::importQmldirScripts(
746 const QQmlTypeLoader::Blob::PendingImportPtr &import,
747 const QQmlTypeLoaderQmldirContent &qmldir,
const QUrl &qmldirUrl)
749 assertTypeLoaderThread();
751 const auto qmldirScripts = qmldir.scripts();
752 for (
const QQmlDirParser::Script &script : qmldirScripts) {
753 const QUrl plainUrl = QUrl(script.fileName);
754 const QUrl scriptUrl = qmldirUrl.resolved(plainUrl);
755 QQmlRefPointer<QQmlScriptBlob> blob = typeLoader()->getScript(scriptUrl, plainUrl);
758 if (blob.data() ==
this)
761 addDependency(blob.data());
762 scriptImported(blob, import->location, script.nameSpace, import->qualifier);
766template<
typename URL>
768 QQmlTypeLoader::Blob *self,
769 const QQmlTypeLoader::Blob::PendingImportPtr &import,
const QString &qmldirFilePath,
770 const URL &qmldirUrl)
772 self->assertTypeLoaderThread();
774 const QQmlTypeLoaderQmldirContent qmldir = self->typeLoader()->qmldirContent(qmldirFilePath);
775 if (!import->qualifier.isEmpty())
776 self->importQmldirScripts(import, qmldir, QUrl(qmldirUrl));
778 if (qmldir.plugins().isEmpty()) {
783 auto module = QQmlMetaType::typeModule(qmldir.typeNamespace(), import->version);
785 QQmlMetaType::qmlRegisterModuleTypes(qmldir.typeNamespace());
791 const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
794 QString reason = errors->front().description();
795 if (reason.size() > 512)
796 reason = reason.first(252) + QLatin1String(
"... ...") + reason.last(252);
797 if (import->version.hasMajorVersion()) {
798 error.setDescription(QQmlImports::tr(
799 "module \"%1\" version %2.%3 cannot be imported because:\n%4")
800 .arg(import->uri).arg(import->version.majorVersion())
801 .arg(import->version.hasMinorVersion()
802 ? QString::number(import->version.minorVersion())
803 : QLatin1String(
"x"))
806 error.setDescription(QQmlImports::tr(
"module \"%1\" cannot be imported because:\n%2")
807 .arg(import->uri, reason));
809 errors->prepend(error);
812bool QQmlTypeLoader::Blob::handleLocalQmldirForImport(
813 const PendingImportPtr &import,
const QString &qmldirFilePath,
814 const QString &qmldirUrl, QList<QQmlError> *errors)
817 const QTypeRevision actualVersion = m_importCache->addLibraryImport(
818 typeLoader(), import->uri, import->qualifier, import->version, qmldirFilePath,
819 qmldirUrl, import->flags, import->precedence, errors);
820 if (!actualVersion.isValid())
824 if (actualVersion.hasMajorVersion())
825 import->version = actualVersion;
827 if (!loadImportDependencies(import, qmldirFilePath, import->flags, errors)) {
828 addDependencyImportError(import, errors);
832 postProcessQmldir(
this, import, qmldirFilePath, qmldirUrl);
836bool QQmlTypeLoader::Blob::updateQmldir(
const QQmlRefPointer<QQmlQmldirData> &data,
const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
840 assertTypeLoaderThread();
842 QString qmldirIdentifier = data->urlString();
843 QString qmldirUrl = qmldirIdentifier.left(qmldirIdentifier.lastIndexOf(QLatin1Char(
'/')) + 1);
845 typeLoader()->setQmldirContent(qmldirIdentifier, data->content());
847 const QTypeRevision version = m_importCache->updateQmldirContent(
848 typeLoader(), import->uri, import->version, import->qualifier, qmldirIdentifier,
850 if (!version.isValid())
854 if (version.hasMajorVersion())
855 import->version = version;
857 if (!loadImportDependencies(import, qmldirIdentifier, import->flags, errors))
860 import->priority = 0;
865 postProcessQmldir(
this, import, qmldirIdentifier, qmldirUrl);
869bool QQmlTypeLoader::Blob::addScriptImport(
const QQmlTypeLoader::Blob::PendingImportPtr &import)
871 assertTypeLoaderThread();
872 const QUrl url(import->uri);
873 QQmlTypeLoader *loader = typeLoader();
874 QQmlRefPointer<QQmlScriptBlob> blob = loader->getScript(finalUrl().resolved(url), url);
875 addDependency(blob.data());
876 scriptImported(blob, import->location, import->qualifier, QString());
880bool QQmlTypeLoader::Blob::addFileImport(
const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
882 assertTypeLoaderThread();
883 QQmlImports::ImportFlags flags;
885 QUrl importUrl(import->uri);
886 QString path = importUrl.path();
887 path.append(QLatin1String(path.endsWith(QLatin1Char(
'/')) ?
"qmldir" :
"/qmldir"));
888 importUrl.setPath(path);
891 if (!finalUrl().isValid() && importUrl.isRelative()) {
893 error.setDescription(
894 QString::fromLatin1(
"Can't resolve relative qmldir URL %1 on invalid base URL")
895 .arg(importUrl.toString()));
896 errors->append(error);
900 QUrl qmldirUrl = finalUrl().resolved(importUrl);
901 if (!QQmlImports::isLocal(qmldirUrl)) {
903 flags = QQmlImports::ImportIncomplete;
906 const QTypeRevision version = m_importCache->addFileImport(
907 typeLoader(), import->uri, import->qualifier, import->version, flags,
908 import->precedence,
nullptr, errors);
909 if (!version.isValid())
913 if (version.hasMajorVersion())
914 import->version = version;
916 if (flags & QQmlImports::ImportIncomplete) {
917 if (!fetchQmldir(qmldirUrl, import, 1, errors))
920 const QString qmldirFilePath = QQmlFile::urlToLocalFileOrQrc(qmldirUrl);
921 if (!loadImportDependencies(import, qmldirFilePath, import->flags, errors))
924 postProcessQmldir(
this, import, qmldirFilePath, qmldirUrl);
930bool QQmlTypeLoader::Blob::addLibraryImport(
const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
932 assertTypeLoaderThread();
934 const LocalQmldirResult qmldirResult = typeLoader()->locateLocalQmldir(
this, import, errors);
935 switch (qmldirResult) {
938 case QmldirNotFound: {
939 if (!loadImportDependencies(import, QString(), import->flags, errors)) {
940 addDependencyImportError(import, errors);
945 case QmldirInterceptedToRemote:
956 if (qmldirResult != QmldirInterceptedToRemote && registerPendingTypes(import)) {
957 if (m_importCache->addLibraryImport(
958 typeLoader(), import->uri, import->qualifier, import->version, QString(),
959 QString(), import->flags, import->precedence, errors).isValid()) {
967 m_unresolvedImports << import;
970 const QTypeRevision version = m_importCache->addLibraryImport(
971 typeLoader(), import->uri, import->qualifier, import->version, QString(),
972 QString(), import->flags | QQmlImports::ImportIncomplete, import->precedence,
975 if (!version.isValid())
979 if (version.hasMajorVersion())
980 import->version = version;
982 const bool hasInterceptors = m_typeLoader->hasUrlInterceptors();
986 QStringList remotePathList = typeLoader()->importPathList(
987 hasInterceptors ? LocalOrRemote : Remote);
988 if (!remotePathList.isEmpty()) {
991 const QStringList qmlDirPaths = QQmlImports::completeQmldirPaths(
992 import->uri, remotePathList, import->version);
993 for (
const QString &qmldirPath : qmlDirPaths) {
994 if (hasInterceptors) {
995 QUrl url = m_typeLoader->interceptUrl(
996 QQmlImports::urlFromLocalFileOrQrcOrUrl(qmldirPath),
997 QQmlAbstractUrlInterceptor::QmldirFile);
998 if (!QQmlFile::isLocalFile(url)
999 && !fetchQmldir(url, import, ++priority, errors)) {
1002 }
else if (!fetchQmldir(QUrl(qmldirPath), import, ++priority, errors)) {
1011bool QQmlTypeLoader::Blob::registerPendingTypes(
const PendingImportPtr &import)
1013 assertTypeLoaderThread();
1018 QQmlMetaType::typeModule(import->uri, import->version)
1021 || QQmlMetaType::qmlRegisterModuleTypes(import->uri)
1025 || QQmlMetaType::latestModuleVersion(import->uri).isValid();
1028bool QQmlTypeLoader::Blob::addImport(
const QV4::CompiledData::Import *import,
1029 QQmlImports::ImportFlags flags, QList<QQmlError> *errors)
1031 assertTypeLoaderThread();
1032 return addImport(std::make_shared<PendingImport>(
this, import, flags), errors);
1035bool QQmlTypeLoader::Blob::addImport(
1036 const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
1038 assertTypeLoaderThread();
1042 switch (import->type)
1044 case QV4::CompiledData::Import::ImportLibrary:
1045 return addLibraryImport(import, errors);
1046 case QV4::CompiledData::Import::ImportFile:
1047 return addFileImport(import ,errors);
1048 case QV4::CompiledData::Import::ImportScript:
1049 return addScriptImport(import);
1050 case QV4::CompiledData::Import::ImportInlineComponent:
1051 Q_UNREACHABLE_RETURN(
false);
1054 Q_UNREACHABLE_RETURN(
false);
1057void QQmlTypeLoader::Blob::dependencyComplete(
const QQmlDataBlob::Ptr &blob)
1059 assertTypeLoaderThread();
1061 if (blob->type() == QQmlDataBlob::QmldirFile) {
1062 QQmlQmldirData *data =
static_cast<QQmlQmldirData *>(blob.data());
1063 QList<QQmlError> errors;
1064 if (!qmldirDataAvailable(data, &errors)) {
1065 Q_ASSERT(errors.size());
1066 QQmlError error(errors.takeFirst());
1067 error.setUrl(m_importCache->baseUrl());
1068 const QV4::CompiledData::Location importLocation = data->importLocation(
this);
1069 error.setLine(qmlConvertSourceCoordinate<quint32,
int>(importLocation.line()));
1070 error.setColumn(qmlConvertSourceCoordinate<quint32,
int>(importLocation.column()));
1071 errors.prepend(error);
1077bool QQmlTypeLoader::Blob::loadDependentImports(
1078 const QList<QQmlDirParser::Import> &imports,
const QString &qualifier,
1079 QTypeRevision version, quint8 precedence, QQmlImports::ImportFlags flags,
1080 QList<QQmlError> *errors)
1082 assertTypeLoaderThread();
1084 for (
const auto &import : imports) {
1085 if (import.flags & QQmlDirParser::Import::Optional)
1087 auto dependencyImport = std::make_shared<PendingImport>();
1088 dependencyImport->uri = import.module;
1089 dependencyImport->qualifier = qualifier;
1090 dependencyImport->version = (import.flags & QQmlDirParser::Import::Auto)
1091 ? version : import.version;
1092 dependencyImport->flags = flags;
1093 dependencyImport->precedence = precedence;
1095 qCDebug(lcQmlImport)
1096 <<
"loading dependent import" << dependencyImport->uri <<
"version"
1097 << dependencyImport->version <<
"as" << dependencyImport->qualifier;
1099 if (!addImport(dependencyImport, errors)) {
1101 error.setDescription(
1102 QString::fromLatin1(
1103 "Failed to load dependent import \"%1\" version %2.%3")
1104 .arg(dependencyImport->uri)
1105 .arg(dependencyImport->version.majorVersion())
1106 .arg(dependencyImport->version.minorVersion()));
1107 errors->append(error);
1115bool QQmlTypeLoader::Blob::loadImportDependencies(
1116 const QQmlTypeLoader::Blob::PendingImportPtr ¤tImport,
const QString &qmldirUri,
1117 QQmlImports::ImportFlags flags, QList<QQmlError> *errors)
1119 assertTypeLoaderThread();
1121 QList<QQmlDirParser::Import> implicitImports
1122 = QQmlMetaType::moduleImports(currentImport->uri, currentImport->version);
1123 if (!qmldirUri.isEmpty())
1124 implicitImports += typeLoader()->qmldirContent(qmldirUri).imports();
1127 switch (currentImport->precedence) {
1128 case QQmlImportInstance::Implicit - 1:
1129 case QQmlImportInstance::Lowest: {
1131 error.setDescription(
1132 QString::fromLatin1(
"Too many dependent imports for %1 %2.%3")
1133 .arg(currentImport->uri)
1134 .arg(currentImport->version.majorVersion())
1135 .arg(currentImport->version.minorVersion()));
1136 errors->append(error);
1143 if (!loadDependentImports(
1144 implicitImports, currentImport->qualifier, currentImport->version,
1145 currentImport->precedence + 1, flags, errors)) {
1147 error.setDescription(
1148 QString::fromLatin1(
1149 "Failed to load dependencies for module \"%1\" version %2.%3")
1150 .arg(currentImport->uri)
1151 .arg(currentImport->version.majorVersion())
1152 .arg(currentImport->version.minorVersion()));
1153 errors->append(error);
1162 if (!QQmlTypeLoaderConfiguredDataConstPtr(m_data)->initialized)
1163 initializeConfiguredData(QQmlTypeLoaderConfiguredDataPtr(m_data), m_data->engine());
1165 return QQmlTypeLoaderConfiguredDataConstPtr(m_data);
1168bool QQmlTypeLoader::isDebugging()
1170 return configuredData(&m_data)->isDebugging;
1173bool QQmlTypeLoader::readCacheFile()
1175 return configuredData(&m_data)->diskCacheOptions & QV4::ExecutionEngine::DiskCache::QmlcRead;
1178bool QQmlTypeLoader::writeCacheFile()
1180 return configuredData(&m_data)->diskCacheOptions & QV4::ExecutionEngine::DiskCache::QmlcWrite;
1183QQmlMetaType::CacheMode QQmlTypeLoader::aotCacheMode()
1185 const QV4::ExecutionEngine::DiskCacheOptions options
1186 = configuredData(&m_data)->diskCacheOptions;
1187 if (!(options & QV4::ExecutionEngine::DiskCache::Aot))
1188 return QQmlMetaType::RejectAll;
1189 if (options & QV4::ExecutionEngine::DiskCache::AotByteCode)
1190 return QQmlMetaType::AcceptUntyped;
1191 return QQmlMetaType::RequireFullyTyped;
1194bool QQmlTypeLoader::Blob::qmldirDataAvailable(
const QQmlRefPointer<QQmlQmldirData> &data, QList<QQmlError> *errors)
1196 assertTypeLoaderThread();
1197 return data->processImports(
this, [&](
const PendingImportPtr &import) {
1198 return updateQmldir(data, import, errors);
1204 if (QDir::listSeparator() == u':') {
1206 QStringList paths = envImportPath.split(u':');
1207 bool wasEmpty =
false;
1208 for (
auto it = paths.begin(); it != paths.end();) {
1209 if (it->isEmpty()) {
1211 it = paths.erase(it);
1222 return envImportPath.split(QDir::listSeparator(), Qt::SkipEmptyParts);
1227
1228
1229QQmlTypeLoader::QQmlTypeLoader(QV4::ExecutionEngine *engine)
1232 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
1233 data->pluginPaths << QLatin1String(
".");
1246 const auto paths = QLibraryInfo::paths(QLibraryInfo::QmlImportsPath);
1247 for (
const auto &installImportsPath: paths)
1248 addImportPath(installImportsPath);
1250 const bool isPluginApplication = QCoreApplication::testAttribute(Qt::AA_PluginApplication);
1252 auto addEnvImportPath = [
this, isPluginApplication](
const char *var) {
1253 if (Q_UNLIKELY(!isPluginApplication && !qEnvironmentVariableIsEmpty(var))) {
1254 const QStringList paths = parseEnvPath(qEnvironmentVariable(var));
1255 for (
int ii = paths.size() - 1; ii >= 0; --ii)
1256 addImportPath(paths.at(ii));
1261 addEnvImportPath(
"QML_IMPORT_PATH");
1262 addEnvImportPath(
"QML2_IMPORT_PATH");
1264 addImportPath(QStringLiteral(
"qrc:/qt/qml"));
1265 addImportPath(QStringLiteral(
"qrc:/qt-project.org/imports"));
1267 if (!isPluginApplication)
1268 addImportPath(QCoreApplication::applicationDirPath());
1270 auto addEnvPluginPath = [
this, isPluginApplication](
const char *var) {
1271 if (Q_UNLIKELY(!isPluginApplication && !qEnvironmentVariableIsEmpty(var))) {
1272 const QStringList paths = parseEnvPath(qEnvironmentVariable(var));
1273 for (
int ii = paths.size() - 1; ii >= 0; --ii)
1274 addPluginPath(paths.at(ii));
1278 addEnvPluginPath(
"QML_PLUGIN_PATH");
1279#if defined(Q_OS_ANDROID)
1280 addImportPath(QStringLiteral(
"qrc:/android_rcc_bundle/qml"));
1281 addEnvPluginPath(
"QT_BUNDLED_LIBS_PATH");
1282#elif defined(Q_OS_MACOS)
1286 if (!isPluginApplication) {
1287 if (CFBundleRef bundleRef = CFBundleGetMainBundle()) {
1288 if (QCFType<CFURLRef> urlRef = CFBundleCopyResourceURL(
1289 bundleRef, QCFString(QLatin1String(
"qml")), 0, 0)) {
1290 if (QCFType<CFURLRef> absoluteUrlRef = CFURLCopyAbsoluteURL(urlRef)) {
1291 if (QCFString path = CFURLCopyFileSystemPath(
1292 absoluteUrlRef, kCFURLPOSIXPathStyle)) {
1293 if (QFile::exists(path)) {
1294 addImportPath(QDir(path).canonicalPath());
1305
1306
1307
1308QQmlTypeLoader::~QQmlTypeLoader()
1322template<
typename Blob>
1324 const QQmlTypeLoaderSharedDataPtr &data, QQmlRefPointer<Blob> &&blob,
1325 QQmlTypeLoader::Mode mode)
1327 if ((mode == QQmlTypeLoader::PreferSynchronous && QQmlFile::isSynchronous(blob->finalUrl()))
1328 || mode == QQmlTypeLoader::Synchronous) {
1339 QQmlTypeLoaderThread *thread = data.thread();
1340 if (thread && !thread->isThisThread()) {
1341 while (!blob->isCompleteOrError())
1342 thread->waitForNextMessage();
1349
1350
1351QQmlRefPointer<QQmlTypeData> QQmlTypeLoader::getType(
const QUrl &unNormalizedUrl, Mode mode)
1355 Q_ASSERT(!unNormalizedUrl.isRelative() &&
1356 (QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl).isEmpty() ||
1357 !QDir::isRelativePath(QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl))));
1359 QQmlRefPointer<QQmlTypeData> typeData;
1361 const QUrl url = QQmlMetaType::normalizedUrl(unNormalizedUrl);
1362 QQmlTypeLoaderSharedDataPtr data(&m_data);
1364 typeData = data->typeCache.value(url);
1366 return handleExisting(data, std::move(typeData), mode);
1369 if (data->typeCache.size() >= data->typeCacheTrimThreshold)
1372 typeData = QQml::makeRefPointer<QQmlTypeData>(url,
this);
1375 data->typeCache.insert(url, typeData);
1378 return finalizeBlob(std::move(typeData), mode);
1382
1383
1384
1385QQmlRefPointer<QQmlTypeData> QQmlTypeLoader::getType(
1386 const QByteArray &data,
const QUrl &url, Mode mode)
1390 QQmlRefPointer<QQmlTypeData> typeData = QQml::makeRefPointer<QQmlTypeData>(url,
this);
1391 QQmlTypeLoader::loadWithStaticData(QQmlDataBlob::Ptr(typeData.data()), data, mode);
1397 return url.fragment() == QLatin1String(
"module") || url.path().endsWith(QLatin1String(
".mjs"));
1400QQmlRefPointer<QQmlScriptBlob> QQmlTypeLoader::getScript(
const QUrl &unNormalizedUrl, Mode mode)
1404 Q_ASSERT(!unNormalizedUrl.isRelative() &&
1405 (QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl).isEmpty() ||
1406 !QDir::isRelativePath(QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl))));
1408 QQmlRefPointer<QQmlScriptBlob> scriptBlob;
1410 const QUrl url = QQmlMetaType::normalizedUrl(unNormalizedUrl);
1411 QQmlTypeLoaderSharedDataPtr data(&m_data);
1413 scriptBlob = data->scriptCache.value(url);
1415 return handleExisting(data, std::move(scriptBlob), mode);
1417 scriptBlob = QQml::makeRefPointer<QQmlScriptBlob>(url,
this, isModuleUrl(url)
1418 ? QQmlScriptBlob::IsESModule::Yes
1419 : QQmlScriptBlob::IsESModule::No);
1420 data->scriptCache.insert(url, scriptBlob);
1423 return finalizeBlob(std::move(scriptBlob), mode);
1426QQmlRefPointer<QV4::CompiledData::CompilationUnit> QQmlTypeLoader::injectModule(
1427 const QUrl &relativeUrl,
const QV4::CompiledData::Unit *unit)
1431 QQmlRefPointer<QQmlScriptBlob> blob = QQml::makeRefPointer<QQmlScriptBlob>(
1432 relativeUrl,
this, QQmlScriptBlob::IsESModule::Yes);
1433 QQmlPrivate::CachedQmlUnit cached { unit,
nullptr,
nullptr};
1436 QQmlTypeLoaderSharedDataPtr data(&m_data);
1437 data->scriptCache.insert(relativeUrl, blob);
1440 loadWithCachedUnit(blob.data(), &cached, Synchronous);
1441 Q_ASSERT(blob->isComplete());
1442 return blob->scriptData()->compilationUnit();
1446
1447
1448
1449QQmlRefPointer<QQmlScriptBlob> QQmlTypeLoader::getScript(
1450 const QUrl &unNormalizedUrl,
const QUrl &relativeUrl)
1454 Q_ASSERT(!unNormalizedUrl.isRelative() &&
1455 (QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl).isEmpty() ||
1456 !QDir::isRelativePath(QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl))));
1458 const QUrl url = QQmlMetaType::normalizedUrl(unNormalizedUrl);
1460 QQmlRefPointer<QQmlScriptBlob> scriptBlob;
1462 QQmlTypeLoaderSharedDataPtr data(&m_data);
1463 scriptBlob = data->scriptCache.value(url);
1467 if (!scriptBlob && unNormalizedUrl != relativeUrl)
1468 scriptBlob = data->scriptCache.value(relativeUrl);
1474 scriptBlob = QQml::makeRefPointer<QQmlScriptBlob>(url,
this, isModuleUrl(url)
1475 ? QQmlScriptBlob::IsESModule::Yes
1476 : QQmlScriptBlob::IsESModule::No);
1477 data->scriptCache.insert(url, scriptBlob);
1480 return finalizeBlob(std::move(scriptBlob), PreferSynchronous);
1484
1485
1486QQmlRefPointer<QQmlQmldirData> QQmlTypeLoader::getQmldir(
const QUrl &url)
1490 Q_ASSERT(!url.isRelative() &&
1491 (QQmlFile::urlToLocalFileOrQrc(url).isEmpty() ||
1492 !QDir::isRelativePath(QQmlFile::urlToLocalFileOrQrc(url))));
1494 QQmlRefPointer<QQmlQmldirData> qmldirData;
1496 QQmlTypeLoaderSharedDataPtr data(&m_data);
1497 qmldirData = data->qmldirCache.value(url);
1501 qmldirData = QQml::makeRefPointer<QQmlQmldirData>(url,
this);
1502 data->qmldirCache.insert(url, qmldirData);
1505 QQmlTypeLoader::load(QQmlDataBlob::Ptr(qmldirData.data()));
1510
1511
1512
1513
1514
1515
1516
1517
1518QString QQmlTypeLoader::absoluteFilePath(
const QString &path)
1524 if (path.at(0) == QLatin1Char(
':')) {
1526 QFileInfo fileInfo(path);
1527 return fileInfo.isFile() ? fileInfo.absoluteFilePath() : QString();
1528 }
else if (path.size() > 3 && path.at(3) == QLatin1Char(
':') &&
1529 path.startsWith(QLatin1String(
"qrc"), Qt::CaseInsensitive)) {
1531 QFileInfo fileInfo(QQmlFile::urlToLocalFileOrQrc(path));
1532 return fileInfo.isFile() ? fileInfo.absoluteFilePath() : QString();
1534#if defined(Q_OS_ANDROID)
1535 else if (path.size() > 7 && path.at(6) == QLatin1Char(
':') && path.at(7) == QLatin1Char(
'/') &&
1536 path.startsWith(QLatin1String(
"assets"), Qt::CaseInsensitive)) {
1538 QFileInfo fileInfo(QQmlFile::urlToLocalFileOrQrc(path));
1539 return fileInfo.isFile() ? fileInfo.absoluteFilePath() : QString();
1540 }
else if (path.size() > 8 && path.at(7) == QLatin1Char(
':') && path.at(8) == QLatin1Char(
'/') &&
1541 path.startsWith(QLatin1String(
"content"), Qt::CaseInsensitive)) {
1543 QFileInfo fileInfo(QQmlFile::urlToLocalFileOrQrc(path));
1544 return fileInfo.isFile() ? fileInfo.absoluteFilePath() : QString();
1548 return fileExists(path)
1549 ? QFileInfo(path).absoluteFilePath()
1555 return QDirListing::IteratorFlag::CaseSensitive | QDirListing::IteratorFlag::IncludeHidden;
1560 QCache<QString,
bool> *fileSet,
const QString &path,
const QString &file)
1562 const QDirListing listing(path, dirListingFlags());
1564 for (
const auto &entry : listing) {
1565 const QString next = entry.fileName();
1568 fileSet->insert(next,
new bool(
true));
1569 if (fileSet->totalCost() == fileSet->maxCost())
1575 if (fileSet->totalCost() < fileSet->maxCost())
1582 for (qsizetype length = path.size(); length > 0; --length) {
1583 if (path[length - 1] != QLatin1Char(
'/'))
1584 return path.left(length);
1590bool QQmlTypeLoader::fileExists(
const QString &dirPath,
const QString &file)
1600 const QString path = stripTrailingSlashes(dirPath);
1602 const QChar nullChar(QChar::Null);
1603 if (path.isEmpty() || path.contains(nullChar) || file.isEmpty() || file.contains(nullChar))
1607 QQmlTypeLoaderSharedDataPtr data(&m_data);
1608 QCache<QString,
bool> *fileSet = data->importDirCache.object(path);
1610 if (
const bool *exists = fileSet->object(file))
1615 if (fileSet->totalCost() < fileSet->maxCost())
1618 }
else if (data->importDirCache.contains(path)) {
1623 auto addToCache = [&](
const QString &path,
const QString &file) {
1624 const QDir dir(path);
1629 fileSet = dir.exists() ?
new QCache<QString,
bool> :
nullptr;
1630 const bool inserted = data->importDirCache.insert(path, fileSet);
1635 switch (populateFileSet(fileSet, dir.path(), file)) {
1636 case FileSetPopulateResult::NotFound:
1638 case FileSetPopulateResult::Found:
1640 case FileSetPopulateResult::Overflow:
1647 Q_ASSERT(fileSet->totalCost() == fileSet->maxCost());
1651 const QDirListing singleFile(dir.path(), {file}, dirListingFlags());
1652 const bool exists = singleFile.begin() != singleFile.end();
1653 fileSet->insert(file,
new bool(exists));
1654 Q_ASSERT(fileSet->totalCost() == fileSet->maxCost());
1658 if (path.at(0) == QLatin1Char(
':')) {
1660 return addToCache(path, file);
1663 if (path.size() > 3 && path.at(3) == QLatin1Char(
':')
1664 && path.startsWith(QLatin1String(
"qrc"), Qt::CaseInsensitive)) {
1666 return addToCache(QQmlFile::urlToLocalFileOrQrc(path), file);
1669#if defined(Q_OS_ANDROID)
1670 if (path.size() > 7 && path.at(6) == QLatin1Char(
':') && path.at(7) == QLatin1Char(
'/')
1671 && path.startsWith(QLatin1String(
"assets"), Qt::CaseInsensitive)) {
1673 return addToCache(QQmlFile::urlToLocalFileOrQrc(path), file);
1676 if (path.size() > 8 && path.at(7) == QLatin1Char(
':') && path.at(8) == QLatin1Char(
'/')
1677 && path.startsWith(QLatin1String(
"content"), Qt::CaseInsensitive)) {
1679 return addToCache(QQmlFile::urlToLocalFileOrQrc(path), file);
1683 return addToCache(path, file);
1688
1689
1690
1691bool QQmlTypeLoader::directoryExists(
const QString &path)
1698 bool isResource = path.at(0) == QLatin1Char(
':');
1699#if defined(Q_OS_ANDROID)
1700 isResource = isResource || path.startsWith(QLatin1String(
"assets:/")) || path.startsWith(QLatin1String(
"content:/"));
1705 QFileInfo fileInfo(path);
1706 return fileInfo.exists() && fileInfo.isDir();
1709 const QString dirPath = stripTrailingSlashes(path);
1711 QQmlTypeLoaderSharedDataPtr data(&m_data);
1712 if (!data->importDirCache.contains(dirPath)) {
1713 if (QDir(dirPath).exists()) {
1714 QCache<QString,
bool> *files =
new QCache<QString,
bool>;
1715 populateFileSet(files, dirPath, QString());
1716 data->importDirCache.insert(dirPath, files);
1720 data->importDirCache.insert(dirPath,
nullptr);
1724 return data->importDirCache.object(dirPath) !=
nullptr;
1729
1730
1731
1732
1733
1734
1735const QQmlTypeLoaderQmldirContent QQmlTypeLoader::qmldirContent(
const QString &filePathIn)
1747 QUrl url(filePathIn);
1749 QQmlTypeLoaderThreadDataPtr data(&m_data);
1751 if (url.scheme().size() < 2) {
1752 filePath = filePathIn;
1754 filePath = QQmlFile::urlToLocalFileOrQrc(url);
1755 if (filePath.isEmpty()) {
1756 if (
auto entry = data->importQmlDirCache.value(filePathIn))
1759 return QQmlTypeLoaderQmldirContent();
1763 QQmlTypeLoaderQmldirContent **val = data->importQmlDirCache.value(filePath);
1766 QQmlTypeLoaderQmldirContent *qmldir =
new QQmlTypeLoaderQmldirContent;
1768#define ERROR(description) { QQmlError e; e.setDescription(description); qmldir->setError(e); }
1769#define NOT_READABLE_ERROR QString(QLatin1String("module \"$$URI$$\" definition \"%1\" not readable"))
1770#define NOT_FOUND_ERROR QString(QLatin1String("cannot load module \"$$URI$$\": File \"%1\" not found"))
1772 QFile file(filePath);
1773 if (!fileExists(filePath)) {
1775 }
else if (file.open(QFile::ReadOnly)) {
1776 QByteArray data = file.readAll();
1777 qmldir->setContent(filePath, QString::fromUtf8(data));
1783#undef NOT_READABLE_ERROR
1784#undef CASE_MISMATCH_ERROR
1786 data->importQmlDirCache.insert(filePath, qmldir);
1790void QQmlTypeLoader::setQmldirContent(
const QString &url,
const QString &content)
1794 QQmlTypeLoaderThreadDataPtr data(&m_data);
1795 QQmlTypeLoaderQmldirContent *qmldir;
1796 QQmlTypeLoaderQmldirContent **val = data->importQmlDirCache.value(url);
1800 qmldir =
new QQmlTypeLoaderQmldirContent;
1801 data->importQmlDirCache.insert(url, qmldir);
1804 if (!qmldir->hasContent())
1805 qmldir->setContent(url, content);
1808template<
typename Blob>
1811 std::for_each(blobs->cbegin(), blobs->cend(), [](
const QQmlRefPointer<Blob> &blob) {
1812 blob->resetTypeLoader();
1818
1819
1820
1821void QQmlTypeLoader::clearCache()
1832 QQmlTypeLoaderThreadDataPtr threadData(&m_data);
1833 qDeleteAll(threadData->importQmlDirCache);
1834 threadData->checksumCache.clear();
1835 threadData->importQmlDirCache.clear();
1837 QQmlTypeLoaderSharedDataPtr data(&m_data);
1838 clearBlobs(&data->typeCache);
1839 clearBlobs(&data->scriptCache);
1840 clearBlobs(&data->qmldirCache);
1841 data->typeCacheTrimThreshold = QQmlTypeLoaderSharedData::MinimumTypeCacheTrimThreshold;
1842 data->importDirCache.clear();
1847void QQmlTypeLoader::trimCache()
1849 const QQmlTypeLoaderSharedDataPtr data(&m_data);
1853void QQmlTypeLoader::updateTypeCacheTrimThreshold(
const QQmlTypeLoaderSharedDataPtr &data)
1857 int size = data->typeCache.size();
1858 if (size > data->typeCacheTrimThreshold)
1859 data->typeCacheTrimThreshold = size * 2;
1860 if (size < data->typeCacheTrimThreshold / 2) {
1861 data->typeCacheTrimThreshold
1862 = qMax(size * 2,
int(QQmlTypeLoaderSharedData::MinimumTypeCacheTrimThreshold));
1866void QQmlTypeLoader::trimCache(
const QQmlTypeLoaderSharedDataPtr &data)
1873 bool deletedOneType =
false;
1874 for (
auto iter = data->typeCache.begin(), end = data->typeCache.end(); iter != end;) {
1875 const QQmlRefPointer<QQmlTypeData> &typeData = iter.value();
1880 if (typeData->count() != 1 || !typeData->isCompleteOrError()) {
1889 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit
1890 = typeData->m_compiledData;
1891 if (compilationUnit) {
1892 if (compilationUnit->count()
1893 > QQmlMetaType::countInternalCompositeTypeSelfReferences(
1894 compilationUnit) + 1) {
1899 QQmlMetaType::unregisterInternalCompositeType(compilationUnit);
1900 Q_ASSERT(compilationUnit->count() == 1);
1904 iter = data->typeCache.erase(iter);
1905 deletedOneType =
true;
1908 if (!deletedOneType)
1914 updateTypeCacheTrimThreshold(data);
1916 QQmlMetaType::freeUnusedTypesAndCaches();
1919bool QQmlTypeLoader::isTypeLoaded(
const QUrl &url)
const
1921 const QQmlTypeLoaderSharedDataConstPtr data(&m_data);
1922 return data->typeCache.contains(url);
1925bool QQmlTypeLoader::isScriptLoaded(
const QUrl &url)
const
1927 const QQmlTypeLoaderSharedDataConstPtr data(&m_data);
1928 return data->scriptCache.contains(url);
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944QQmlTypeLoader::LocalQmldirResult QQmlTypeLoader::locateLocalQmldir(
1945 QQmlTypeLoader::Blob *blob,
const QQmlTypeLoader::Blob::PendingImportPtr &import,
1946 QList<QQmlError> *errors)
1950 LocalQmldirResult result = QmldirNotFound;
1951 QQmlTypeLoaderThreadData::QmldirInfo *cacheTail =
nullptr;
1953 QQmlTypeLoaderThreadDataPtr threadData(&m_data);
1954 QQmlTypeLoaderThreadData::QmldirInfo **cachePtr = threadData->qmldirInfo.value(import->uri);
1955 QQmlTypeLoaderThreadData::QmldirInfo *cacheHead = cachePtr ? *cachePtr :
nullptr;
1957 cacheTail = cacheHead;
1959 if (cacheTail->version == import->version) {
1960 if (cacheTail->qmldirFilePath.isEmpty()) {
1961 return cacheTail->qmldirPathUrl.isEmpty()
1963 : QmldirInterceptedToRemote;
1965 if (blob->handleLocalQmldirForImport(
1966 import, cacheTail->qmldirFilePath, cacheTail->qmldirPathUrl, errors)) {
1969 result = QmldirRejected;
1971 }
while (cacheTail->next && (cacheTail = cacheTail->next));
1977 if (result != QmldirNotFound
1978 || QQmlMetaType::isStronglyLockedModule(import->uri, import->version)) {
1982 QQmlTypeLoaderConfiguredDataConstPtr configuredData(&m_data);
1983 const bool hasInterceptors = !configuredData->urlInterceptors.isEmpty();
1986 QStringList localImportPaths = importPathList(hasInterceptors ? LocalOrRemote : Local);
1989 const QStringList qmlDirPaths = QQmlImports::completeQmldirPaths(
1990 import->uri, localImportPaths, import->version);
1992 QString qmldirAbsoluteFilePath;
1993 for (QString qmldirPath : qmlDirPaths) {
1994 if (hasInterceptors) {
2005 const QUrl intercepted = doInterceptUrl(
2006 QQmlImports::urlFromLocalFileOrQrcOrUrl(qmldirPath),
2007 QQmlAbstractUrlInterceptor::QmldirFile,
2008 configuredData->urlInterceptors);
2009 qmldirPath = QQmlFile::urlToLocalFileOrQrc(intercepted);
2010 if (result != QmldirInterceptedToRemote
2011 && qmldirPath.isEmpty()
2012 && !QQmlFile::isLocalFile(intercepted)) {
2013 result = QmldirInterceptedToRemote;
2017 qmldirAbsoluteFilePath = absoluteFilePath(qmldirPath);
2018 if (!qmldirAbsoluteFilePath.isEmpty()) {
2020 const QString absolutePath = qmldirAbsoluteFilePath.left(
2021 qmldirAbsoluteFilePath.lastIndexOf(u'/') + 1);
2022 if (absolutePath.at(0) == u':') {
2023 url = QStringLiteral(
"qrc") + absolutePath;
2025 url = QUrl::fromLocalFile(absolutePath).toString();
2026 sanitizeUNCPath(&qmldirAbsoluteFilePath);
2029 QQmlTypeLoaderThreadData::QmldirInfo *cache =
new QQmlTypeLoaderThreadData::QmldirInfo;
2030 cache->version = import->version;
2031 cache->qmldirFilePath = qmldirAbsoluteFilePath;
2032 cache->qmldirPathUrl = url;
2033 cache->next =
nullptr;
2035 cacheTail->next = cache;
2037 threadData->qmldirInfo.insert(import->uri, cache);
2040 if (result != QmldirFound) {
2041 result = blob->handleLocalQmldirForImport(
2042 import, qmldirAbsoluteFilePath, url, errors)
2052 if (result == QmldirNotFound || result == QmldirInterceptedToRemote) {
2053 QQmlTypeLoaderThreadData::QmldirInfo *cache =
new QQmlTypeLoaderThreadData::QmldirInfo;
2054 cache->version = import->version;
2055 cache->next = cacheHead;
2056 if (result == QmldirInterceptedToRemote) {
2059 cache->qmldirPathUrl = QStringLiteral(
"intercepted");
2061 threadData->qmldirInfo.insert(import->uri, cache);
2063 if (result == QmldirNotFound) {
2064 qCDebug(lcQmlImport)
2065 <<
"locateLocalQmldir:" << qPrintable(import->uri)
2066 <<
"module's qmldir file not found";
2069 qCDebug(lcQmlImport)
2070 <<
"locateLocalQmldir:" << qPrintable(import->uri) <<
"module's qmldir found at"
2071 << qmldirAbsoluteFilePath;
#define ASSERT_ENGINETHREAD()
#define ASSERT_LOADTHREAD()
static bool isPathAbsolute(const QString &path)
static FileSetPopulateResult populateFileSet(QCache< QString, bool > *fileSet, const QString &path, const QString &file)
static constexpr QDirListing::IteratorFlags dirListingFlags()
static void initializeConfiguredData(const QQmlTypeLoaderConfiguredDataPtr &data, QV4::ExecutionEngine *engine)
static void addDependencyImportError(const QQmlTypeLoader::Blob::PendingImportPtr &import, QList< QQmlError > *errors)
static QQmlTypeLoaderConfiguredDataConstPtr configuredData(QQmlTypeLoaderLockedData *m_data)
#define ERROR(description)
#define NOT_READABLE_ERROR
static bool isModuleUrl(const QUrl &url)
void clearBlobs(QHash< QUrl, QQmlRefPointer< Blob > > *blobs)
void doInitializeEngine(Interface *iface, QQmlTypeLoaderThread *thread, QQmlTypeLoaderLockedData *data, const char *uri)
static QStringList parseEnvPath(const QString &envImportPath)
void postProcessQmldir(QQmlTypeLoader::Blob *self, const QQmlTypeLoader::Blob::PendingImportPtr &import, const QString &qmldirFilePath, const URL &qmldirUrl)
QQmlRefPointer< Blob > handleExisting(const QQmlTypeLoaderSharedDataPtr &data, QQmlRefPointer< Blob > &&blob, QQmlTypeLoader::Mode mode)
static QString stripTrailingSlashes(const QString &path)
const QQmlPrivate::CachedQmlUnit * unit
CachedLoader(const QQmlPrivate::CachedQmlUnit *unit)
void loadThread(QQmlTypeLoader *loader, const QQmlDataBlob::Ptr &blob) const
void loadAsync(QQmlTypeLoader *loader, const QQmlDataBlob::Ptr &blob) const
void load(QQmlTypeLoader *loader, const QQmlDataBlob::Ptr &blob) const
void load(QQmlTypeLoader *loader, const QQmlDataBlob::Ptr &blob) const
StaticLoader(const QByteArray &data)
void loadThread(QQmlTypeLoader *loader, const QQmlDataBlob::Ptr &blob) const
void loadAsync(QQmlTypeLoader *loader, const QQmlDataBlob::Ptr &blob) const