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>
35#define ASSERT_LOADTHREAD()
36 Q_ASSERT(thread() && thread()->isThisThread())
37#define ASSERT_ENGINETHREAD()
38 Q_ASSERT(!engine()->jsEngine() || engine()->jsEngine()->thread()->isCurrentThread())
42Q_TRACE_POINT(qtqml, QQmlCompiling_entry,
const QUrl &url)
43Q_TRACE_POINT(qtqml, QQmlCompiling_exit)
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
75void QQmlTypeLoader::invalidate()
81#if QT_CONFIG(qml_network)
85 QQmlTypeLoaderThreadDataPtr data(&m_data);
86 data->networkReplies.clear();
90void QQmlTypeLoader::addUrlInterceptor(QQmlAbstractUrlInterceptor *urlInterceptor)
93 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
94 data->urlInterceptors.append(urlInterceptor);
97void QQmlTypeLoader::removeUrlInterceptor(QQmlAbstractUrlInterceptor *urlInterceptor)
100 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
101 data->urlInterceptors.removeOne(urlInterceptor);
104QList<QQmlAbstractUrlInterceptor *> QQmlTypeLoader::urlInterceptors()
const
107 QQmlTypeLoaderConfiguredDataConstPtr data(&m_data);
108 return data->urlInterceptors;
111static QUrl doInterceptUrl(
112 const QUrl &url, QQmlAbstractUrlInterceptor::DataType type,
113 const QList<QQmlAbstractUrlInterceptor *> &urlInterceptors)
116 for (QQmlAbstractUrlInterceptor *interceptor : urlInterceptors)
117 result = interceptor->intercept(result, type);
121QUrl QQmlTypeLoader::interceptUrl(
const QUrl &url, QQmlAbstractUrlInterceptor::DataType type)
const
125 QQmlTypeLoaderConfiguredDataConstPtr data(&m_data);
126 return doInterceptUrl(url, type, data->urlInterceptors);
129bool QQmlTypeLoader::hasUrlInterceptors()
const
132 QQmlTypeLoaderConfiguredDataConstPtr data(&m_data);
133 return !data->urlInterceptors.isEmpty();
136#if QT_CONFIG(qml_debug)
137void QQmlTypeLoader::setProfiler(QQmlProfiler *profiler)
139 ASSERT_ENGINETHREAD();
141 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
142 Q_ASSERT(!data->profiler);
143 data->profiler.reset(profiler);
148 void loadThread(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
150 loader->loadThread(blob);
152 void load(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
154 loader->ensureThread()->load(blob);
156 void loadAsync(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
158 loader->ensureThread()->loadAsync(blob);
166 void loadThread(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
168 loader->loadWithStaticDataThread(blob, data);
170 void load(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
172 loader->ensureThread()->loadWithStaticData(blob, data);
174 void loadAsync(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
176 loader->ensureThread()->loadWithStaticDataAsync(blob, data);
184 void loadThread(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
186 loader->loadWithCachedUnitThread(blob, unit);
188 void load(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
190 loader->ensureThread()->loadWithCachedUnit(blob, unit);
192 void loadAsync(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
194 loader->ensureThread()->loadWithCachedUnitAsync(blob, unit);
198template<
typename Loader>
199void QQmlTypeLoader::doLoad(
const Loader &loader,
const QQmlDataBlob::Ptr &blob, Mode mode)
203 qWarning(
"QQmlTypeLoader::doLoad(%s): %s thread", qPrintable(blob->urlString()),
204 (m_thread && m_thread->isThisThread()) ?
"Compile" :
"Engine");
206 blob->startLoading();
208 if (QQmlTypeLoaderThread *t = thread(); t && t->isThisThread()) {
209 loader.loadThread(
this, blob);
213 if (mode == Asynchronous) {
214 blob->setIsAsync(
true);
215 loader.loadAsync(
this, blob);
219 loader.load(
this, blob);
220 if (blob->isCompleteOrError())
223 if (mode == PreferSynchronous) {
224 blob->setIsAsync(
true);
228 Q_ASSERT(mode == Synchronous);
231 QQmlTypeLoaderSharedDataConstPtr lock(&m_data);
233 m_data.thread()->waitForNextMessage();
234 }
while (!blob->isCompleteOrError());
238
239
240
241
242void QQmlTypeLoader::load(
const QQmlDataBlob::Ptr &blob, Mode mode)
245 doLoad(PlainLoader(), blob, mode);
249
250
251
252
253void QQmlTypeLoader::loadWithStaticData(
254 const QQmlDataBlob::Ptr &blob,
const QByteArray &data, Mode mode)
257 doLoad(StaticLoader(data), blob, mode);
260void QQmlTypeLoader::loadWithCachedUnit(
261 const QQmlDataBlob::Ptr &blob,
const QQmlPrivate::CachedQmlUnit *unit, Mode mode)
264 doLoad(CachedLoader(unit), blob, mode);
267void QQmlTypeLoader::drop(
const QQmlDataBlob::Ptr &blob)
275 if (QQmlTypeLoaderThread *t = thread(); t && !t->isThisThread())
279void QQmlTypeLoader::loadWithStaticDataThread(
const QQmlDataBlob::Ptr &blob,
const QByteArray &data)
283 setData(blob, data, DataOrigin::Static);
286void QQmlTypeLoader::loadWithCachedUnitThread(
const QQmlDataBlob::Ptr &blob,
const QQmlPrivate::CachedQmlUnit *unit)
290 setCachedUnit(blob, unit);
293void QQmlTypeLoader::loadThread(
const QQmlDataBlob::Ptr &blob)
297 if (blob->m_url.isEmpty()) {
299 error.setDescription(QLatin1String(
"Invalid null URL"));
300 blob->setError(error);
304 if (QQmlFile::isSynchronous(blob->m_url)) {
305 const QString fileName = QQmlFile::urlToLocalFileOrQrc(blob->m_url);
306 if (!fileExists(fileName) && QFileInfo::exists(fileName)) {
309 blob->setError(QLatin1String(
"File name case mismatch"));
313 if (blob->setProgress(1.f) && blob->isAsync())
314 thread()->callDownloadProgressChanged(blob, 1.);
316 setData(blob, fileName);
319#if QT_CONFIG(qml_network)
320 QNetworkReply *reply = thread()->networkAccessManager()->get(QNetworkRequest(blob->m_url));
321 QQmlTypeLoaderNetworkReplyProxy *nrp = thread()->networkReplyProxy();
323 QQmlTypeLoaderThreadDataPtr data(&m_data);
324 data->networkReplies.insert(reply, blob);
326 if (reply->isFinished()) {
327 nrp->manualFinished(reply);
329 QObject::connect(reply, &QNetworkReply::downloadProgress,
330 nrp, &QQmlTypeLoaderNetworkReplyProxy::downloadProgress);
331 QObject::connect(reply, &QNetworkReply::finished,
332 nrp, &QQmlTypeLoaderNetworkReplyProxy::finished);
336 qWarning(
"QQmlDataBlob: requested %s", qPrintable(blob->urlString()));
342#define DATALOADER_MAXIMUM_REDIRECT_RECURSION 16
344#if QT_CONFIG(qml_network)
345void QQmlTypeLoader::networkReplyFinished(QNetworkReply *reply)
349 reply->deleteLater();
351 QQmlTypeLoaderThreadDataPtr data(&m_data);
352 QQmlRefPointer<QQmlDataBlob> blob = data->networkReplies.take(reply);
356 blob->m_redirectCount++;
358 if (blob->m_redirectCount < DATALOADER_MAXIMUM_REDIRECT_RECURSION) {
359 QVariant redirect = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
360 if (redirect.isValid()) {
361 QUrl url = reply->url().resolved(redirect.toUrl());
362 blob->m_finalUrl = url;
363 blob->m_finalUrlString.clear();
365 QNetworkReply *reply = thread()->networkAccessManager()->get(QNetworkRequest(url));
366 QObject *nrp = thread()->networkReplyProxy();
367 QObject::connect(reply, SIGNAL(finished()), nrp, SLOT(finished()));
368 data->networkReplies.insert(reply, std::move(blob));
370 qWarning(
"QQmlDataBlob: redirected to %s", qPrintable(blob->finalUrlString()));
376 if (reply->error()) {
377 blob->networkError(reply->error());
379 QByteArray data = reply->readAll();
380 setData(blob, data, DataOrigin::Device);
384void QQmlTypeLoader::networkReplyProgress(QNetworkReply *reply,
385 qint64 bytesReceived, qint64 bytesTotal)
389 QQmlTypeLoaderThreadDataConstPtr data(&m_data);
390 const QQmlRefPointer<QQmlDataBlob> blob = data->networkReplies.value(reply);
394 if (bytesTotal != 0) {
395 const qreal progress = (qreal(bytesReceived) / qreal(bytesTotal));
396 if (blob->setProgress(progress) && blob->isAsync())
397 thread()->callDownloadProgressChanged(blob, blob->progress());
403
404
405
406template<
class Interface>
408 Interface *iface, QQmlTypeLoaderThread *thread, QQmlTypeLoaderLockedData *data,
414 if (thread && thread->isThisThread())
415 thread->initializeEngine(iface, uri);
417 iface->initializeEngine(data->engine()->qmlEngine(), uri);
420void QQmlTypeLoader::initializeEngine(QQmlEngineExtensionInterface *iface,
const char *uri)
423 doInitializeEngine(iface, thread(), &m_data, uri);
426void QQmlTypeLoader::initializeEngine(QQmlExtensionInterface *iface,
const char *uri)
429 doInitializeEngine(iface, thread(), &m_data, uri);
433
434
435
436
437
438
439
440
441void QQmlTypeLoader::setData(
const QQmlDataBlob::Ptr &blob,
const QByteArray &data, DataOrigin origin)
445 QQmlDataBlob::SourceCodeData d;
446 d.inlineSourceCode = QString::fromUtf8(data);
447 d.hasInlineSourceCode =
true;
448 d.hasStaticData = (origin == DataOrigin::Static);
452void QQmlTypeLoader::setData(
const QQmlDataBlob::Ptr &blob,
const QString &fileName)
456 QQmlDataBlob::SourceCodeData d;
457 d.fileInfo = QFileInfo(fileName);
461void QQmlTypeLoader::setData(
const QQmlDataBlob::Ptr &blob,
const QQmlDataBlob::SourceCodeData &d)
465 Q_TRACE_SCOPE(QQmlCompiling, blob->url());
466 QQmlCompilingProfiler prof(profiler(), blob.data());
468 blob->m_inCallback =
true;
470 blob->dataReceived(d);
472 if (!blob->isError() && !blob->isWaiting())
473 blob->allDependenciesDone();
475 blob->m_inCallback =
false;
480void QQmlTypeLoader::setCachedUnit(
const QQmlDataBlob::Ptr &blob,
const QQmlPrivate::CachedQmlUnit *unit)
484 Q_TRACE_SCOPE(QQmlCompiling, blob->url());
485 QQmlCompilingProfiler prof(profiler(), blob.data());
487 blob->m_inCallback =
true;
489 blob->initializeFromCachedUnit(unit);
491 if (!blob->isError() && !blob->isWaiting())
492 blob->allDependenciesDone();
494 blob->m_inCallback =
false;
501#if defined(Q_OS_UNIX)
502 return (path.at(0) == QLatin1Char(
'/'));
505 return fi.isAbsolute();
511 return path.startsWith(u':') || isPathAbsolute(path);
521 if (urlOrPath.isEmpty() || isPathQrcOrAbsolute(urlOrPath))
523 return QQmlFile::urlToLocalFileOrQrc(urlOrPath);
527
528
529QStringList QQmlTypeLoader::importPathList(PathType type)
const
531 QQmlTypeLoaderConfiguredDataConstPtr data(&m_data);
532 if (type == LocalOrRemote)
533 return data->importPaths;
536 for (
const QString &path : data->importPaths) {
542 if ((isPathAbsolute(path) || QQmlFile::isLocalFile(path)) == (type == Local))
550
551
552void QQmlTypeLoader::addImportPath(
const QString &path, AddPathMode mode)
554 qCDebug(lcQmlImport) <<
"addImportPath:" << path;
559 QUrl url = QUrl(path);
562 if (url.scheme() == QLatin1String(
"file")) {
563 cPath = QQmlFile::urlToLocalFileOrQrc(url);
564 }
else if (path.startsWith(QLatin1Char(
':'))) {
567 cPath = QLatin1String(
"qrc") + path;
568 cPath.replace(QLatin1Char(
'\\'), QLatin1Char(
'/'));
569 }
else if (url.isRelative() ||
570 (url.scheme().size() == 1 && QFile::exists(path)) ) {
571 QDir dir = QDir(path);
572 cPath = dir.canonicalPath();
575 cPath.replace(QLatin1Char(
'\\'), QLatin1Char(
'/'));
578 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
579 if (!cPath.isEmpty()) {
580 if (mode == PrependPath) {
583 if (data->importPaths.contains(cPath))
584 data->importPaths.move(data->importPaths.indexOf(cPath), 0);
586 data->importPaths.prepend(cPath);
587 }
else if (!data->importPaths.contains(cPath)) {
590 data->importPaths.append(cPath);
596
597
598void QQmlTypeLoader::setImportPathList(
const QStringList &paths)
600 qCDebug(lcQmlImport) <<
"setImportPathList:" << paths;
602 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
603 data->importPaths.clear();
604 for (
const QString &path : paths)
605 addImportPath(path, AppendPath);
613
614
615void QQmlTypeLoader::setPluginPathList(
const QStringList &paths)
617 qCDebug(lcQmlImport) <<
"setPluginPathList:" << paths;
618 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
619 data->pluginPaths = paths;
623
624
625void QQmlTypeLoader::addPluginPath(
const QString& path, AddPathMode mode)
627 qCDebug(lcQmlImport) <<
"addPluginPath:" << path;
629 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
631 QUrl url = QUrl(path);
632 QString canonicalPath = path;
633 if (url.isRelative() || url.scheme() == QLatin1String(
"file")
634 || (url.scheme().size() == 1 && QFile::exists(path)) ) {
635 QDir dir = QDir(path);
636 canonicalPath = dir.canonicalPath();
639 if (mode == PrependPath)
640 data->pluginPaths.prepend(canonicalPath);
642 data->pluginPaths.append(canonicalPath);
645#if QT_CONFIG(qml_network)
646QQmlNetworkAccessManagerFactoryPtrConst QQmlTypeLoader::networkAccessManagerFactory()
const
648 ASSERT_ENGINETHREAD();
649 return QQmlNetworkAccessManagerFactoryPtrConst(&m_data);
652void QQmlTypeLoader::setNetworkAccessManagerFactory(QQmlNetworkAccessManagerFactory *factory)
654 ASSERT_ENGINETHREAD();
655 QQmlNetworkAccessManagerFactoryPtr(&m_data).reset(factory);
658QNetworkAccessManager *QQmlTypeLoader::createNetworkAccessManager(QObject *parent)
const
667 if (
const auto factory = QQmlNetworkAccessManagerFactoryPtrConst(&m_data))
668 return factory->create(parent);
670 return new QNetworkAccessManager(parent);
674void QQmlTypeLoader::clearQmldirInfo()
676 QQmlTypeLoaderThreadDataPtr data(&m_data);
678 auto itr = data->qmldirInfo.constBegin();
679 while (itr != data->qmldirInfo.constEnd()) {
680 const QQmlTypeLoaderThreadData::QmldirInfo *cache = *itr;
682 const QQmlTypeLoaderThreadData::QmldirInfo *nextCache = cache->next;
689 data->qmldirInfo.clear();
693 const QQmlTypeLoaderConfiguredDataPtr &data, QV4::ExecutionEngine *engine)
695 data->diskCacheOptions = engine->diskCacheOptions();
696 data->isDebugging = engine->debugger() !=
nullptr;
697 data->initialized =
true;
700void QQmlTypeLoader::startThread()
704 if (!m_data.thread()) {
709 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
710 initializeConfiguredData(data, m_data.engine());
711 m_data.createThread(
this);
715void QQmlTypeLoader::shutdownThread()
720 m_data.deleteThread();
723QQmlTypeLoader::Blob::PendingImport::PendingImport(
724 const QQmlRefPointer<Blob> &blob,
const QV4::CompiledData::Import *import,
725 QQmlImports::ImportFlags flags)
726 : uri(blob->stringAt(import->uriIndex))
727 , qualifier(blob->stringAt(import->qualifierIndex))
728 , type(
static_cast<QV4::CompiledData::Import::ImportType>(quint32(import->type)))
729 , location(import->location)
731 , version(import->version)
735QQmlTypeLoader::Blob::Blob(
const QUrl &url, QQmlDataBlob::Type type, QQmlTypeLoader *loader)
736 : QQmlDataBlob(url, type, loader)
737 , m_importCache(
new QQmlImports(), QQmlRefPointer<QQmlImports>::Adopt)
741QQmlTypeLoader::Blob::~Blob()
745bool QQmlTypeLoader::Blob::fetchQmldir(
746 const QUrl &url,
const QQmlTypeLoader::Blob::PendingImportPtr &import,
int priority,
747 QList<QQmlError> *errors)
749 assertTypeLoaderThread();
751 QQmlRefPointer<QQmlQmldirData> data = typeLoader()->getQmldir(url);
753 data->setPriority(
this, import, priority);
755 if (data->status() == Error) {
758 }
else if (data->status() == Complete) {
760 return qmldirDataAvailable(data, errors);
764 addDependency(data.data());
769
770
771
772
773void QQmlTypeLoader::Blob::importQmldirScripts(
774 const QQmlTypeLoader::Blob::PendingImportPtr &import,
775 const QQmlTypeLoaderQmldirContent &qmldir,
const QUrl &qmldirUrl)
777 assertTypeLoaderThread();
786 QQmlTypeLoaderQmldirContent redirectedQmldir = qmldir;
787 const QUrl scriptBaseUrl = redirectedQmldir.hasRedirection()
788 ? QUrl{QQmlImports::redirectQmldirContent(typeLoader(), &redirectedQmldir)}
790 const auto qmldirScripts = redirectedQmldir.scripts();
791 for (
const QQmlDirParser::Script &script : qmldirScripts) {
792 const QUrl plainUrl = QUrl(script.fileName);
793 const QUrl scriptUrl = scriptBaseUrl.resolved(plainUrl);
794 QQmlRefPointer<QQmlScriptBlob> blob = typeLoader()->getScript(scriptUrl, plainUrl);
797 if (blob.data() ==
this)
800 addDependency(blob.data());
801 scriptImported(blob, import->location, script.nameSpace, import->qualifier);
805template<
typename URL>
807 QQmlTypeLoader::Blob *self,
808 const QQmlTypeLoader::Blob::PendingImportPtr &import,
const QString &qmldirFilePath,
809 const URL &qmldirUrl)
811 self->assertTypeLoaderThread();
813 const QQmlTypeLoaderQmldirContent qmldir = self->typeLoader()->qmldirContent(qmldirFilePath);
814 if (!import->qualifier.isEmpty())
815 self->importQmldirScripts(import, qmldir, QUrl(qmldirUrl));
817 if (qmldir.plugins().isEmpty()) {
822 auto module = QQmlMetaType::typeModule(qmldir.typeNamespace(), import->version);
824 QQmlMetaType::qmlRegisterModuleTypes(qmldir.typeNamespace());
830 const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
833 QString reason = errors->front().description();
834 if (reason.size() > 512)
835 reason = reason.first(252) + QLatin1String(
"... ...") + reason.last(252);
836 if (import->version.hasMajorVersion()) {
837 error.setDescription(
838 QQmlImports::tr(
"module \"%1\" version %2.%3 cannot be imported because:\n%4")
839 .arg(import->uri, QString::number(import->version.majorVersion()),
840 import->version.hasMinorVersion()
841 ? QString::number(import->version.minorVersion())
842 : QLatin1String(
"x"),
845 error.setDescription(QQmlImports::tr(
"module \"%1\" cannot be imported because:\n%2")
846 .arg(import->uri, reason));
848 errors->prepend(error);
851bool QQmlTypeLoader::Blob::handleLocalQmldirForImport(
852 const PendingImportPtr &import,
const QString &qmldirFilePath,
853 const QString &qmldirUrl, QList<QQmlError> *errors)
856 const QTypeRevision actualVersion = m_importCache->addLibraryImport(
857 typeLoader(), import->uri, import->qualifier, import->version, qmldirFilePath,
858 qmldirUrl, import->flags, import->precedence, errors);
859 if (!actualVersion.isValid())
863 if (actualVersion.hasMajorVersion())
864 import->version = actualVersion;
866 if (!loadImportDependencies(import, qmldirFilePath, import->flags, errors)) {
867 addDependencyImportError(import, errors);
871 postProcessQmldir(
this, import, qmldirFilePath, qmldirUrl);
875bool QQmlTypeLoader::Blob::updateQmldir(
const QQmlRefPointer<QQmlQmldirData> &data,
const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
879 assertTypeLoaderThread();
881 QString qmldirIdentifier = data->urlString();
882 QString qmldirUrl = qmldirIdentifier.left(qmldirIdentifier.lastIndexOf(QLatin1Char(
'/')) + 1);
884 typeLoader()->setQmldirContent(qmldirIdentifier, data->content());
886 const QTypeRevision version = m_importCache->updateQmldirContent(
887 typeLoader(), import->uri, import->version, import->qualifier, qmldirIdentifier,
889 if (!version.isValid())
893 if (version.hasMajorVersion())
894 import->version = version;
896 if (!loadImportDependencies(import, qmldirIdentifier, import->flags, errors))
899 import->priority = 0;
904 postProcessQmldir(
this, import, qmldirIdentifier, qmldirUrl);
908bool QQmlTypeLoader::Blob::addScriptImport(
const QQmlTypeLoader::Blob::PendingImportPtr &import)
910 assertTypeLoaderThread();
911 const QUrl url(import->uri);
912 QQmlTypeLoader *loader = typeLoader();
913 QQmlRefPointer<QQmlScriptBlob> blob = loader->getScript(finalUrl().resolved(url), url);
914 addDependency(blob.data());
915 scriptImported(blob, import->location, import->qualifier, QString());
919bool QQmlTypeLoader::Blob::addFileImport(
const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
921 assertTypeLoaderThread();
922 QQmlImports::ImportFlags flags;
924 QUrl importUrl(import->uri);
925 QString path = importUrl.path();
926 path.append(QLatin1String(path.endsWith(QLatin1Char(
'/')) ?
"qmldir" :
"/qmldir"));
927 importUrl.setPath(path);
930 if (!finalUrl().isValid() && importUrl.isRelative()) {
932 error.setDescription(
933 QString::fromLatin1(
"Can't resolve relative qmldir URL %1 on invalid base URL")
934 .arg(importUrl.toString()));
935 errors->append(error);
939 QUrl qmldirUrl = finalUrl().resolved(importUrl);
940 if (!QQmlImports::isLocal(qmldirUrl)) {
942 flags = QQmlImports::ImportIncomplete;
945 const QTypeRevision version = m_importCache->addFileImport(
946 typeLoader(), import->uri, import->qualifier, import->version, flags,
947 import->precedence,
nullptr, errors);
948 if (!version.isValid())
952 if (version.hasMajorVersion())
953 import->version = version;
955 if (flags & QQmlImports::ImportIncomplete) {
956 if (!fetchQmldir(qmldirUrl, import, 1, errors))
959 const QString qmldirFilePath = QQmlFile::urlToLocalFileOrQrc(qmldirUrl);
960 if (!loadImportDependencies(import, qmldirFilePath, import->flags, errors))
963 postProcessQmldir(
this, import, qmldirFilePath, qmldirUrl);
969bool QQmlTypeLoader::Blob::addLibraryImport(
const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
971 assertTypeLoaderThread();
973 const LocalQmldirResult qmldirResult = typeLoader()->locateLocalQmldir(
this, import, errors);
974 switch (qmldirResult) {
977 case QmldirNotFound: {
978 if (!loadImportDependencies(import, QString(), import->flags, errors)) {
979 addDependencyImportError(import, errors);
984 case QmldirInterceptedToRemote:
995 if (qmldirResult != QmldirInterceptedToRemote && registerPendingTypes(import)) {
996 if (m_importCache->addLibraryImport(
997 typeLoader(), import->uri, import->qualifier, import->version, QString(),
998 QString(), import->flags, import->precedence, errors).isValid()) {
1006 m_unresolvedImports << import;
1009 const QTypeRevision version = m_importCache->addLibraryImport(
1010 typeLoader(), import->uri, import->qualifier, import->version, QString(),
1011 QString(), import->flags | QQmlImports::ImportIncomplete, import->precedence,
1014 if (!version.isValid())
1018 if (version.hasMajorVersion())
1019 import->version = version;
1021 const bool hasInterceptors = m_typeLoader->hasUrlInterceptors();
1025 QStringList remotePathList = typeLoader()->importPathList(
1026 hasInterceptors ? LocalOrRemote : Remote);
1027 if (!remotePathList.isEmpty()) {
1030 const QStringList qmlDirPaths = QQmlImports::completeQmldirPaths(
1031 import->uri, remotePathList, import->version);
1032 for (
const QString &qmldirPath : qmlDirPaths) {
1033 if (hasInterceptors) {
1034 QUrl url = m_typeLoader->interceptUrl(
1035 QQmlImports::urlFromLocalFileOrQrcOrUrl(qmldirPath),
1036 QQmlAbstractUrlInterceptor::QmldirFile);
1037 if (!QQmlFile::isLocalFile(url)
1038 && !fetchQmldir(url, import, ++priority, errors)) {
1041 }
else if (!fetchQmldir(QUrl(qmldirPath), import, ++priority, errors)) {
1050bool QQmlTypeLoader::Blob::registerPendingTypes(
const PendingImportPtr &import)
1052 assertTypeLoaderThread();
1057 QQmlMetaType::typeModule(import->uri, import->version)
1060 || QQmlMetaType::qmlRegisterModuleTypes(import->uri)
1064 || QQmlMetaType::latestModuleVersion(import->uri).isValid();
1067bool QQmlTypeLoader::Blob::addImport(
const QV4::CompiledData::Import *import,
1068 QQmlImports::ImportFlags flags, QList<QQmlError> *errors)
1070 assertTypeLoaderThread();
1071 return addImport(std::make_shared<PendingImport>(
this, import, flags), errors);
1074bool QQmlTypeLoader::Blob::addImport(
1075 const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
1077 assertTypeLoaderThread();
1081 switch (import->type)
1083 case QV4::CompiledData::Import::ImportLibrary:
1084 return addLibraryImport(import, errors);
1085 case QV4::CompiledData::Import::ImportFile:
1086 return addFileImport(import ,errors);
1087 case QV4::CompiledData::Import::ImportScript:
1088 return addScriptImport(import);
1089 case QV4::CompiledData::Import::ImportInlineComponent:
1090 Q_UNREACHABLE_RETURN(
false);
1093 Q_UNREACHABLE_RETURN(
false);
1096void QQmlTypeLoader::Blob::dependencyComplete(
const QQmlDataBlob::Ptr &blob)
1098 assertTypeLoaderThread();
1100 if (blob->type() == QQmlDataBlob::QmldirFile) {
1101 QQmlQmldirData *data =
static_cast<QQmlQmldirData *>(blob.data());
1102 QList<QQmlError> errors;
1103 if (!qmldirDataAvailable(data, &errors)) {
1104 Q_ASSERT(errors.size());
1105 QQmlError error(errors.takeFirst());
1106 error.setUrl(m_importCache->baseUrl());
1107 const QV4::CompiledData::Location importLocation = data->importLocation(
this);
1108 error.setLine(qmlConvertSourceCoordinate<quint32,
int>(importLocation.line()));
1109 error.setColumn(qmlConvertSourceCoordinate<quint32,
int>(importLocation.column()));
1110 errors.prepend(error);
1116bool QQmlTypeLoader::Blob::loadDependentImports(
1117 const QList<QQmlDirParser::Import> &imports,
const QString &qualifier,
1118 QTypeRevision version, quint8 precedence, QQmlImports::ImportFlags flags,
1119 QList<QQmlError> *errors)
1121 assertTypeLoaderThread();
1123 for (
const auto &import : imports) {
1124 if (import.flags & QQmlDirParser::Import::Optional)
1126 auto dependencyImport = std::make_shared<PendingImport>();
1127 dependencyImport->uri = import.module;
1128 dependencyImport->qualifier = qualifier;
1129 dependencyImport->version = (import.flags & QQmlDirParser::Import::Auto)
1130 ? version : import.version;
1131 dependencyImport->flags = flags;
1132 dependencyImport->precedence = precedence;
1134 qCDebug(lcQmlImport)
1135 <<
"loading dependent import" << dependencyImport->uri <<
"version"
1136 << dependencyImport->version <<
"as" << dependencyImport->qualifier;
1138 if (!addImport(dependencyImport, errors)) {
1140 error.setDescription(
1141 QString::fromLatin1(
1142 "Failed to load dependent import \"%1\" version %2.%3")
1143 .arg(dependencyImport->uri)
1144 .arg(dependencyImport->version.majorVersion())
1145 .arg(dependencyImport->version.minorVersion()));
1146 errors->append(error);
1154bool QQmlTypeLoader::Blob::loadImportDependencies(
1155 const QQmlTypeLoader::Blob::PendingImportPtr ¤tImport,
const QString &qmldirUri,
1156 QQmlImports::ImportFlags flags, QList<QQmlError> *errors)
1158 assertTypeLoaderThread();
1160 QList<QQmlDirParser::Import> implicitImports
1161 = QQmlMetaType::moduleImports(currentImport->uri, currentImport->version);
1162 if (!qmldirUri.isEmpty())
1163 implicitImports += typeLoader()->qmldirContent(qmldirUri).imports();
1166 switch (currentImport->precedence) {
1167 case QQmlImportInstance::Implicit - 1:
1168 case QQmlImportInstance::Lowest: {
1170 error.setDescription(
1171 QString::fromLatin1(
"Too many dependent imports for %1 %2.%3")
1172 .arg(currentImport->uri)
1173 .arg(currentImport->version.majorVersion())
1174 .arg(currentImport->version.minorVersion()));
1175 errors->append(error);
1182 if (!loadDependentImports(
1183 implicitImports, currentImport->qualifier, currentImport->version,
1184 currentImport->precedence + 1, flags, errors)) {
1186 error.setDescription(
1187 QString::fromLatin1(
1188 "Failed to load dependencies for module \"%1\" version %2.%3")
1189 .arg(currentImport->uri)
1190 .arg(currentImport->version.majorVersion())
1191 .arg(currentImport->version.minorVersion()));
1192 errors->append(error);
1201 if (!QQmlTypeLoaderConfiguredDataConstPtr(m_data)->initialized)
1202 initializeConfiguredData(QQmlTypeLoaderConfiguredDataPtr(m_data), m_data->engine());
1204 return QQmlTypeLoaderConfiguredDataConstPtr(m_data);
1207bool QQmlTypeLoader::isDebugging()
1209 return configuredData(&m_data)->isDebugging;
1212bool QQmlTypeLoader::readCacheFile()
1214 return configuredData(&m_data)->diskCacheOptions & QV4::ExecutionEngine::DiskCache::QmlcRead;
1217bool QQmlTypeLoader::writeCacheFile()
1219 return configuredData(&m_data)->diskCacheOptions & QV4::ExecutionEngine::DiskCache::QmlcWrite;
1222QQmlMetaType::CacheMode QQmlTypeLoader::aotCacheMode()
1224 const QV4::ExecutionEngine::DiskCacheOptions options
1225 = configuredData(&m_data)->diskCacheOptions;
1226 if (!(options & QV4::ExecutionEngine::DiskCache::Aot))
1227 return QQmlMetaType::RejectAll;
1228 if (options & QV4::ExecutionEngine::DiskCache::AotByteCode)
1229 return QQmlMetaType::AcceptUntyped;
1230 return QQmlMetaType::RequireFullyTyped;
1233bool QQmlTypeLoader::Blob::qmldirDataAvailable(
const QQmlRefPointer<QQmlQmldirData> &data, QList<QQmlError> *errors)
1235 assertTypeLoaderThread();
1236 return data->processImports(
this, [&](
const PendingImportPtr &import) {
1237 return updateQmldir(data, import, errors);
1243 if (QDir::listSeparator() == u':') {
1245 QStringList paths = envImportPath.split(u':');
1246 bool wasEmpty =
false;
1247 for (
auto it = paths.begin(); it != paths.end();) {
1248 if (it->isEmpty()) {
1250 it = paths.erase(it);
1261 return envImportPath.split(QDir::listSeparator(), Qt::SkipEmptyParts);
1266
1267
1268QQmlTypeLoader::QQmlTypeLoader(QV4::ExecutionEngine *engine)
1274 const bool isPluginApplication = QCoreApplication::testAttribute(Qt::AA_PluginApplication);
1276 auto addEnvPath = [
this, isPluginApplication](
const char *var,
auto addPath) {
1277 if (Q_UNLIKELY(!isPluginApplication && !qEnvironmentVariableIsEmpty(var))) {
1278 const QStringList paths = parseEnvPath(qEnvironmentVariable(var));
1279 for (
const QString &path : paths)
1280 (
this->*addPath)(path, AppendPath);
1286#if defined(Q_OS_ANDROID)
1287 addImportPath(QStringLiteral(
"qrc:/android_rcc_bundle/qml"), AppendPath);
1290 if (!isPluginApplication)
1291 addImportPath(QCoreApplication::applicationDirPath(), AppendPath);
1293 addImportPath(QStringLiteral(
"qrc:/qt-project.org/imports"), AppendPath);
1294 addImportPath(QStringLiteral(
"qrc:/qt/qml"), AppendPath);
1296 addEnvPath(
"QML2_IMPORT_PATH", &QQmlTypeLoader::addImportPath);
1297 addEnvPath(
"QML_IMPORT_PATH", &QQmlTypeLoader::addImportPath);
1299 const auto qmlImportPaths = QLibraryInfo::paths(QLibraryInfo::QmlImportsPath);
1300 for (
const auto &qmlImportPath : qmlImportPaths)
1301 addImportPath(qmlImportPath, AppendPath);
1305#if defined(Q_OS_ANDROID)
1306 addEnvPath(
"QT_BUNDLED_LIBS_PATH", &QQmlTypeLoader::addPluginPath);
1308 addEnvPath(
"QML_PLUGIN_PATH", &QQmlTypeLoader::addPluginPath);
1310 const auto pluginPaths = QLibraryInfo::paths(QLibraryInfo::PluginsPath);
1311 for (
const auto &pluginPath : pluginPaths)
1312 addPluginPath(pluginPath, AppendPath);
1317 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
1318 data->pluginPaths << QLatin1String(
".");
1322
1323
1324
1325QQmlTypeLoader::~QQmlTypeLoader()
1339template<
typename Blob>
1341 const QQmlTypeLoaderSharedDataPtr &data, QQmlRefPointer<Blob> &&blob,
1342 QQmlTypeLoader::Mode mode)
1344 if ((mode == QQmlTypeLoader::PreferSynchronous && QQmlFile::isSynchronous(blob->finalUrl()))
1345 || mode == QQmlTypeLoader::Synchronous) {
1356 QQmlTypeLoaderThread *thread = data.thread();
1357 if (thread && !thread->isThisThread()) {
1358 while (!blob->isCompleteOrError())
1359 thread->waitForNextMessage();
1366
1367
1368QQmlRefPointer<QQmlTypeData> QQmlTypeLoader::getType(
const QUrl &unNormalizedUrl, Mode mode)
1372 Q_ASSERT(!unNormalizedUrl.isRelative() &&
1373 (QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl).isEmpty() ||
1374 !QDir::isRelativePath(QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl))));
1376 QQmlRefPointer<QQmlTypeData> typeData;
1378 const QUrl url = QQmlMetaType::normalizedUrl(unNormalizedUrl);
1379 QQmlTypeLoaderSharedDataPtr data(&m_data);
1381 typeData = data->typeCache.value(url);
1383 return handleExisting(data, std::move(typeData), mode);
1386 if (data->typeCache.size() >= data->typeCacheTrimThreshold)
1389 typeData = QQml::makeRefPointer<QQmlTypeData>(url,
this);
1392 data->typeCache.insert(url, typeData);
1395 return finalizeBlob(std::move(typeData), mode);
1399
1400
1401
1402QQmlRefPointer<QQmlTypeData> QQmlTypeLoader::getType(
1403 const QByteArray &data,
const QUrl &url, Mode mode)
1407 QQmlRefPointer<QQmlTypeData> typeData = QQml::makeRefPointer<QQmlTypeData>(url,
this);
1408 QQmlTypeLoader::loadWithStaticData(QQmlDataBlob::Ptr(typeData.data()), data, mode);
1414 return url.fragment() == QLatin1String(
"module") || url.path().endsWith(QLatin1String(
".mjs"));
1417QQmlRefPointer<QQmlScriptBlob> QQmlTypeLoader::getScript(
const QUrl &unNormalizedUrl, Mode mode)
1421 Q_ASSERT(!unNormalizedUrl.isRelative() &&
1422 (QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl).isEmpty() ||
1423 !QDir::isRelativePath(QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl))));
1425 QQmlRefPointer<QQmlScriptBlob> scriptBlob;
1427 const QUrl url = QQmlMetaType::normalizedUrl(unNormalizedUrl);
1428 QQmlTypeLoaderSharedDataPtr data(&m_data);
1430 scriptBlob = data->scriptCache.value(url);
1432 return handleExisting(data, std::move(scriptBlob), mode);
1434 scriptBlob = QQml::makeRefPointer<QQmlScriptBlob>(url,
this, isModuleUrl(url)
1435 ? QQmlScriptBlob::IsESModule::Yes
1436 : QQmlScriptBlob::IsESModule::No);
1437 data->scriptCache.insert(url, scriptBlob);
1440 return finalizeBlob(std::move(scriptBlob), mode);
1443QQmlRefPointer<QV4::CompiledData::CompilationUnit> QQmlTypeLoader::injectModule(
1444 const QUrl &relativeUrl,
const QV4::CompiledData::Unit *unit)
1448 QQmlRefPointer<QQmlScriptBlob> blob = QQml::makeRefPointer<QQmlScriptBlob>(
1449 relativeUrl,
this, QQmlScriptBlob::IsESModule::Yes);
1450 QQmlPrivate::CachedQmlUnit cached { unit,
nullptr,
nullptr};
1453 QQmlTypeLoaderSharedDataPtr data(&m_data);
1454 data->scriptCache.insert(relativeUrl, blob);
1457 loadWithCachedUnit(blob.data(), &cached, Synchronous);
1458 Q_ASSERT(blob->isComplete());
1459 return blob->scriptData()->compilationUnit();
1463
1464
1465
1466QQmlRefPointer<QQmlScriptBlob> QQmlTypeLoader::getScript(
1467 const QUrl &unNormalizedUrl,
const QUrl &relativeUrl)
1471 Q_ASSERT(!unNormalizedUrl.isRelative() &&
1472 (QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl).isEmpty() ||
1473 !QDir::isRelativePath(QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl))));
1475 const QUrl url = QQmlMetaType::normalizedUrl(unNormalizedUrl);
1477 QQmlRefPointer<QQmlScriptBlob> scriptBlob;
1479 QQmlTypeLoaderSharedDataPtr data(&m_data);
1480 scriptBlob = data->scriptCache.value(url);
1484 if (!scriptBlob && unNormalizedUrl != relativeUrl)
1485 scriptBlob = data->scriptCache.value(relativeUrl);
1491 scriptBlob = QQml::makeRefPointer<QQmlScriptBlob>(url,
this, isModuleUrl(url)
1492 ? QQmlScriptBlob::IsESModule::Yes
1493 : QQmlScriptBlob::IsESModule::No);
1494 data->scriptCache.insert(url, scriptBlob);
1497 return finalizeBlob(std::move(scriptBlob), PreferSynchronous);
1501
1502
1503QQmlRefPointer<QQmlQmldirData> QQmlTypeLoader::getQmldir(
const QUrl &url)
1507 Q_ASSERT(!url.isRelative() &&
1508 (QQmlFile::urlToLocalFileOrQrc(url).isEmpty() ||
1509 !QDir::isRelativePath(QQmlFile::urlToLocalFileOrQrc(url))));
1511 QQmlRefPointer<QQmlQmldirData> qmldirData;
1513 QQmlTypeLoaderSharedDataPtr data(&m_data);
1514 qmldirData = data->qmldirCache.value(url);
1518 qmldirData = QQml::makeRefPointer<QQmlQmldirData>(url,
this);
1519 data->qmldirCache.insert(url, qmldirData);
1522 QQmlTypeLoader::load(QQmlDataBlob::Ptr(qmldirData.data()));
1528 const bool startsWithColon = path.at(0) == QLatin1Char(
':');
1529#if defined(Q_OS_ANDROID)
1530 return startsWithColon || path.startsWith(QLatin1String(
"assets:/"), Qt::CaseInsensitive)
1531 || path.startsWith(QLatin1String(
"content:/"), Qt::CaseInsensitive);
1533 return startsWithColon;
1538
1539
1540
1541
1542
1543
1544
1545
1546QString QQmlTypeLoader::absoluteFilePath(
const QString &path)
const
1553 if (isResource(path)) {
1555 QFileInfo fileInfo(path);
1556 return fileInfo.isFile() ? fileInfo.absoluteFilePath() : QString();
1559 return fileExists(path)
1560 ? QFileInfo(path).absoluteFilePath()
1566 for (qsizetype length = path.size(); length > 0; --length) {
1567 if (path[length - 1] != QLatin1Char(
'/'))
1568 return path.left(length);
1574bool QQmlTypeLoader::fileExists(
const QString &dirPath,
const QString &file)
const
1584 const QChar nullChar(QChar::Null);
1585 if (dirPath.isEmpty() || dirPath.contains(nullChar) || file.isEmpty()
1586 || file.contains(nullChar)) {
1593 if (isResource(dirPath)) {
1596 const QFileInfo info(dirPath + u'/' + file);
1597 return info.exists();
1600 const QString path = stripTrailingSlashes(dirPath);
1602 QQmlTypeLoaderSharedDataConstPtr data(&m_data);
1603 return data->importDirCache.fileExists(path, file);
1608
1609
1610
1611bool QQmlTypeLoader::directoryExists(
const QString &path)
const
1618 if (isResource(path)) {
1620 QFileInfo fileInfo(path);
1621 return fileInfo.exists() && fileInfo.isDir();
1624 const QString dirPath = stripTrailingSlashes(path);
1626 QQmlTypeLoaderSharedDataConstPtr data(&m_data);
1627 return data->importDirCache.directoryExists(path);
1633
1634
1635
1636
1637
1638
1639const QQmlTypeLoaderQmldirContent QQmlTypeLoader::qmldirContent(
const QString &filePathIn)
1651 QUrl url(filePathIn);
1653 QQmlTypeLoaderThreadDataPtr data(&m_data);
1655 if (url.scheme().size() < 2) {
1656 filePath = filePathIn;
1658 filePath = QQmlFile::urlToLocalFileOrQrc(url);
1659 if (filePath.isEmpty()) {
1660 if (
auto entry = data->importQmlDirCache.value(filePathIn))
1663 return QQmlTypeLoaderQmldirContent();
1667 QQmlTypeLoaderQmldirContent **val = data->importQmlDirCache.value(filePath);
1670 QQmlTypeLoaderQmldirContent *qmldir =
new QQmlTypeLoaderQmldirContent;
1672#define ERROR(description) { QQmlError e; e.setDescription(description); qmldir->setError(e); }
1673#define NOT_READABLE_ERROR QString(QLatin1String("module \"$$URI$$\" definition \"%1\" not readable"))
1674#define NOT_FOUND_ERROR QString(QLatin1String("cannot load module \"$$URI$$\": File \"%1\" not found"))
1676 QFile file(filePath);
1677 if (!fileExists(filePath)) {
1679 }
else if (file.open(QFile::ReadOnly)) {
1680 QByteArray data = file.readAll();
1681 qmldir->setContent(filePath, QString::fromUtf8(data));
1687#undef NOT_READABLE_ERROR
1688#undef NOT_FOUND_ERROR
1690 data->importQmlDirCache.insert(filePath, qmldir);
1694void QQmlTypeLoader::setQmldirContent(
const QString &url,
const QString &content)
1698 QQmlTypeLoaderThreadDataPtr data(&m_data);
1699 QQmlTypeLoaderQmldirContent *qmldir;
1700 QQmlTypeLoaderQmldirContent **val = data->importQmlDirCache.value(url);
1704 qmldir =
new QQmlTypeLoaderQmldirContent;
1705 data->importQmlDirCache.insert(url, qmldir);
1708 if (!qmldir->hasContent())
1709 qmldir->setContent(url, content);
1712template<
typename Blob>
1715 std::for_each(blobs->cbegin(), blobs->cend(), [](
const QQmlRefPointer<Blob> &blob) {
1716 blob->resetTypeLoader();
1722
1723
1724
1725void QQmlTypeLoader::clearCache()
1736 QQmlTypeLoaderThreadDataPtr threadData(&m_data);
1737 qDeleteAll(threadData->importQmlDirCache);
1738 threadData->checksumCache.clear();
1739 threadData->importQmlDirCache.clear();
1741 QQmlTypeLoaderSharedDataPtr data(&m_data);
1742 clearBlobs(&data->typeCache);
1743 clearBlobs(&data->scriptCache);
1744 clearBlobs(&data->qmldirCache);
1745 data->typeCacheTrimThreshold = QQmlTypeLoaderSharedData::MinimumTypeCacheTrimThreshold;
1746 data->importDirCache.clear();
1752
1753
1754
1755
1756
1757bool QQmlTypeLoader::removeFromCache(
const QUrl &url)
1759 const QUrl normalized = QQmlMetaType::normalizedUrl(url);
1760 const QQmlTypeLoaderSharedDataPtr data(&m_data);
1761 return data->typeCache.remove(normalized) || data->scriptCache.remove(normalized)
1762 || data->qmldirCache.remove(normalized);
1765void QQmlTypeLoader::trimCache()
1767 const QQmlTypeLoaderSharedDataPtr data(&m_data);
1771void QQmlTypeLoader::updateTypeCacheTrimThreshold(
const QQmlTypeLoaderSharedDataPtr &data)
1775 int size = data->typeCache.size();
1776 if (size > data->typeCacheTrimThreshold)
1777 data->typeCacheTrimThreshold = size * 2;
1778 if (size < data->typeCacheTrimThreshold / 2) {
1779 data->typeCacheTrimThreshold
1780 = qMax(size * 2,
int(QQmlTypeLoaderSharedData::MinimumTypeCacheTrimThreshold));
1784void QQmlTypeLoader::trimCache(
const QQmlTypeLoaderSharedDataPtr &data)
1791 bool deletedOneType =
false;
1792 for (
auto iter = data->typeCache.begin(), end = data->typeCache.end(); iter != end;) {
1793 const QQmlRefPointer<QQmlTypeData> &typeData = iter.value();
1798 if (typeData->count() != 1 || !typeData->isCompleteOrError()) {
1807 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit
1808 = typeData->m_compiledData;
1809 if (compilationUnit) {
1810 if (compilationUnit->count()
1811 > QQmlMetaType::countInternalCompositeTypeSelfReferences(
1812 compilationUnit) + 1) {
1817 QQmlMetaType::unregisterInternalCompositeType(compilationUnit);
1818 Q_ASSERT(compilationUnit->count() == 1);
1822 iter = data->typeCache.erase(iter);
1823 deletedOneType =
true;
1826 if (!deletedOneType)
1832 updateTypeCacheTrimThreshold(data);
1834 QQmlMetaType::freeUnusedTypesAndCaches();
1837bool QQmlTypeLoader::isTypeLoaded(
const QUrl &url)
const
1839 const QQmlTypeLoaderSharedDataConstPtr data(&m_data);
1840 return data->typeCache.contains(url);
1843bool QQmlTypeLoader::isScriptLoaded(
const QUrl &url)
const
1845 const QQmlTypeLoaderSharedDataConstPtr data(&m_data);
1846 return data->scriptCache.contains(url);
1851 const auto dir = path.left(path.lastIndexOf(u"/") + 1);
1852 if (dir.at(0) == u':')
1853 return QStringLiteral(
"qrc") + dir;
1855 return QUrl::fromLocalFile(dir).toString();
1858QStringList QQmlTypeLoader::urlsForModule(
const QString &module)
const
1860 const auto &importPaths = importPathList();
1861 const auto completedImportPaths = QQmlImports::completeQmldirPaths(module, importPaths, {});
1863 for (
const auto &importPath : completedImportPaths) {
1864 const auto absolutePath = absoluteFilePath(localPathForUrlOrPath(importPath));
1865 if (absolutePath.isEmpty())
1867 if (
const std::optional<QString> url = pathToUrl(absolutePath))
1868 urls.append(url.value());
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887QQmlTypeLoader::LocalQmldirResult QQmlTypeLoader::locateLocalQmldir(
1888 QQmlTypeLoader::Blob *blob,
const QQmlTypeLoader::Blob::PendingImportPtr &import,
1889 QList<QQmlError> *errors)
1893 LocalQmldirResult result = QmldirNotFound;
1894 QQmlTypeLoaderThreadData::QmldirInfo *cacheTail =
nullptr;
1896 QQmlTypeLoaderThreadDataPtr threadData(&m_data);
1897 QQmlTypeLoaderThreadData::QmldirInfo **cachePtr = threadData->qmldirInfo.value(import->uri);
1898 QQmlTypeLoaderThreadData::QmldirInfo *cacheHead = cachePtr ? *cachePtr :
nullptr;
1900 cacheTail = cacheHead;
1902 if (cacheTail->version == import->version) {
1903 if (cacheTail->qmldirFilePath.isEmpty()) {
1904 return cacheTail->qmldirPathUrl.isEmpty()
1906 : QmldirInterceptedToRemote;
1908 if (blob->handleLocalQmldirForImport(
1909 import, cacheTail->qmldirFilePath, cacheTail->qmldirPathUrl, errors)) {
1912 result = QmldirRejected;
1914 }
while (cacheTail->next && (cacheTail = cacheTail->next));
1920 if (result != QmldirNotFound
1921 || QQmlMetaType::isStronglyLockedModule(import->uri, import->version)) {
1925 QQmlTypeLoaderConfiguredDataConstPtr configuredData(&m_data);
1926 const bool hasInterceptors = !configuredData->urlInterceptors.isEmpty();
1929 QStringList localImportPaths = importPathList(hasInterceptors ? LocalOrRemote : Local);
1932 const QStringList qmlDirPaths = QQmlImports::completeQmldirPaths(
1933 import->uri, localImportPaths, import->version);
1935 QString qmldirAbsoluteFilePath;
1936 for (QString qmldirPath : qmlDirPaths) {
1937 if (hasInterceptors) {
1948 const QUrl intercepted = doInterceptUrl(
1949 QQmlImports::urlFromLocalFileOrQrcOrUrl(qmldirPath),
1950 QQmlAbstractUrlInterceptor::QmldirFile,
1951 configuredData->urlInterceptors);
1952 qmldirPath = QQmlFile::urlToLocalFileOrQrc(intercepted);
1953 if (result != QmldirInterceptedToRemote
1954 && qmldirPath.isEmpty()
1955 && !QQmlFile::isLocalFile(intercepted)) {
1956 result = QmldirInterceptedToRemote;
1963 qmldirAbsoluteFilePath = absoluteFilePath(localPathForUrlOrPath(qmldirPath));
1965 if (!qmldirAbsoluteFilePath.isEmpty()) {
1966 QString url = pathToUrl(qmldirAbsoluteFilePath);
1967 if (url.startsWith(QStringLiteral(
"file:")))
1968 sanitizeUNCPath(&qmldirAbsoluteFilePath);
1970 QQmlTypeLoaderThreadData::QmldirInfo *cache =
new QQmlTypeLoaderThreadData::QmldirInfo;
1971 cache->version = import->version;
1972 cache->qmldirFilePath = qmldirAbsoluteFilePath;
1973 cache->qmldirPathUrl = url;
1974 cache->next =
nullptr;
1976 cacheTail->next = cache;
1978 threadData->qmldirInfo.insert(import->uri, cache);
1981 if (result != QmldirFound) {
1982 result = blob->handleLocalQmldirForImport(
1983 import, qmldirAbsoluteFilePath, url, errors)
1993 if (result == QmldirNotFound || result == QmldirInterceptedToRemote) {
1994 QQmlTypeLoaderThreadData::QmldirInfo *cache =
new QQmlTypeLoaderThreadData::QmldirInfo;
1995 cache->version = import->version;
1996 cache->next = cacheHead;
1997 if (result == QmldirInterceptedToRemote) {
2000 cache->qmldirPathUrl = QStringLiteral(
"intercepted");
2002 threadData->qmldirInfo.insert(import->uri, cache);
2004 if (result == QmldirNotFound) {
2005 qCDebug(lcQmlImport)
2006 <<
"locateLocalQmldir:" << qPrintable(import->uri)
2007 <<
"module's qmldir file not found";
2010 qCDebug(lcQmlImport)
2011 <<
"locateLocalQmldir:" << qPrintable(import->uri) <<
"module's qmldir found at"
2012 << qmldirAbsoluteFilePath;
#define ASSERT_ENGINETHREAD()
#define ASSERT_LOADTHREAD()
static QString pathToUrl(const QString &path)
static bool isPathAbsolute(const QString &path)
static QString localPathForUrlOrPath(const QString &urlOrPath)
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)
static bool isResource(const QString &path)
void doInitializeEngine(Interface *iface, QQmlTypeLoaderThread *thread, QQmlTypeLoaderLockedData *data, const char *uri)
static QStringList parseEnvPath(const QString &envImportPath)
static bool isPathQrcOrAbsolute(const QString &path)
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