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 (fileName.isEmpty()) {
307 blob->setError(QLatin1String(
"URL cannot be represented as a local file: "
308 "unexpected NUL or percent-encoded directory separator "
309 "(%2F, or %5C on Windows)."));
313 const bool exists = fileExists(fileName);
314 if (!exists && !isResource(fileName) && QFileInfo::exists(fileName)) {
318 blob->setError(QLatin1String(
"File name case mismatch"));
322 if (blob->setProgress(1.f) && blob->isAsync())
323 thread()->callDownloadProgressChanged(blob, 1.);
325 setData(blob, fileName, exists);
328#if QT_CONFIG(qml_network)
329 QNetworkReply *reply = thread()->networkAccessManager()->get(QNetworkRequest(blob->m_url));
330 QQmlTypeLoaderNetworkReplyProxy *nrp = thread()->networkReplyProxy();
332 QQmlTypeLoaderThreadDataPtr data(&m_data);
333 data->networkReplies.insert(reply, blob);
335 if (reply->isFinished()) {
336 nrp->manualFinished(reply);
338 QObject::connect(reply, &QNetworkReply::downloadProgress,
339 nrp, &QQmlTypeLoaderNetworkReplyProxy::downloadProgress);
340 QObject::connect(reply, &QNetworkReply::finished,
341 nrp, &QQmlTypeLoaderNetworkReplyProxy::finished);
345 qWarning(
"QQmlDataBlob: requested %s", qPrintable(blob->urlString()));
351#define DATALOADER_MAXIMUM_REDIRECT_RECURSION 16
353#if QT_CONFIG(qml_network)
354void QQmlTypeLoader::networkReplyFinished(QNetworkReply *reply)
358 reply->deleteLater();
360 QQmlTypeLoaderThreadDataPtr data(&m_data);
361 QQmlRefPointer<QQmlDataBlob> blob = data->networkReplies.take(reply);
365 blob->m_redirectCount++;
367 if (blob->m_redirectCount < DATALOADER_MAXIMUM_REDIRECT_RECURSION) {
368 QVariant redirect = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
369 if (redirect.isValid()) {
370 QUrl url = reply->url().resolved(redirect.toUrl());
371 blob->m_finalUrl = url;
372 blob->m_finalUrlString.clear();
374 QNetworkReply *reply = thread()->networkAccessManager()->get(QNetworkRequest(url));
375 QObject *nrp = thread()->networkReplyProxy();
376 QObject::connect(reply, SIGNAL(finished()), nrp, SLOT(finished()));
377 data->networkReplies.insert(reply, std::move(blob));
379 qWarning(
"QQmlDataBlob: redirected to %s", qPrintable(blob->finalUrlString()));
385 if (reply->error()) {
386 blob->networkError(reply->error());
388 QByteArray data = reply->readAll();
389 setData(blob, data, DataOrigin::Device);
393void QQmlTypeLoader::networkReplyProgress(QNetworkReply *reply,
394 qint64 bytesReceived, qint64 bytesTotal)
398 QQmlTypeLoaderThreadDataConstPtr data(&m_data);
399 const QQmlRefPointer<QQmlDataBlob> blob = data->networkReplies.value(reply);
403 if (bytesTotal != 0) {
404 const qreal progress = (qreal(bytesReceived) / qreal(bytesTotal));
405 if (blob->setProgress(progress) && blob->isAsync())
406 thread()->callDownloadProgressChanged(blob, blob->progress());
412
413
414
415template<
class Interface>
417 Interface *iface, QQmlTypeLoaderThread *thread, QQmlTypeLoaderLockedData *data,
423 if (thread && thread->isThisThread())
424 thread->initializeEngine(iface, uri);
426 iface->initializeEngine(data->engine()->qmlEngine(), uri);
429void QQmlTypeLoader::initializeEngine(QQmlEngineExtensionInterface *iface,
const char *uri)
432 doInitializeEngine(iface, thread(), &m_data, uri);
435void QQmlTypeLoader::initializeEngine(QQmlExtensionInterface *iface,
const char *uri)
438 doInitializeEngine(iface, thread(), &m_data, uri);
442
443
444
445
446
447
448
449
450void QQmlTypeLoader::setData(
const QQmlDataBlob::Ptr &blob,
const QByteArray &data, DataOrigin origin)
454 QQmlDataBlob::SourceCodeData d;
455 d.inlineSourceCode = QString::fromUtf8(data);
456 d.hasInlineSourceCode =
true;
457 d.hasStaticData = (origin == DataOrigin::Static);
461void QQmlTypeLoader::setData(
const QQmlDataBlob::Ptr &blob,
const QString &fileName,
bool exists)
465 QQmlDataBlob::SourceCodeData d;
466 d.fileInfo = QFileInfo(fileName);
467 d.fileExists = exists;
471void QQmlTypeLoader::setData(
const QQmlDataBlob::Ptr &blob,
const QQmlDataBlob::SourceCodeData &d)
475 Q_TRACE_SCOPE(QQmlCompiling, blob->url());
476 QQmlCompilingProfiler prof(profiler(), blob.data());
478 blob->m_inCallback =
true;
480 blob->dataReceived(d);
482 if (!blob->isError() && !blob->isWaiting())
483 blob->allDependenciesDone();
485 blob->m_inCallback =
false;
490void QQmlTypeLoader::setCachedUnit(
const QQmlDataBlob::Ptr &blob,
const QQmlPrivate::CachedQmlUnit *unit)
494 Q_TRACE_SCOPE(QQmlCompiling, blob->url());
495 QQmlCompilingProfiler prof(profiler(), blob.data());
497 blob->m_inCallback =
true;
499 blob->initializeFromCachedUnit(unit);
501 if (!blob->isError() && !blob->isWaiting())
502 blob->allDependenciesDone();
504 blob->m_inCallback =
false;
511#if defined(Q_OS_UNIX)
512 return (path.at(0) == QLatin1Char(
'/'));
515 return fi.isAbsolute();
521 return path.startsWith(u':') || isPathAbsolute(path);
531 if (urlOrPath.isEmpty() || isPathQrcOrAbsolute(urlOrPath))
533 return QQmlFile::urlToLocalFileOrQrc(urlOrPath);
537
538
539QStringList QQmlTypeLoader::importPathList(PathType type)
const
541 QQmlTypeLoaderConfiguredDataConstPtr data(&m_data);
542 if (type == LocalOrRemote)
543 return data->importPaths;
546 for (
const QString &path : data->importPaths) {
552 if ((isPathAbsolute(path) || QQmlFile::isLocalFile(path)) == (type == Local))
560
561
562void QQmlTypeLoader::addImportPath(
const QString &path, AddPathMode mode)
564 qCDebug(lcQmlImport) <<
"addImportPath:" << path;
569 QUrl url = QUrl(path);
572 if (url.scheme() == QLatin1String(
"file")) {
573 cPath = QQmlFile::urlToLocalFileOrQrc(url);
574 }
else if (path.startsWith(QLatin1Char(
':'))) {
577 cPath = QLatin1String(
"qrc") + path;
578 cPath.replace(QLatin1Char(
'\\'), QLatin1Char(
'/'));
579 }
else if (url.isRelative() ||
580 (url.scheme().size() == 1 && QFile::exists(path)) ) {
581 QDir dir = QDir(path);
582 cPath = dir.canonicalPath();
585 cPath.replace(QLatin1Char(
'\\'), QLatin1Char(
'/'));
588 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
589 if (!cPath.isEmpty()) {
590 if (mode == PrependPath) {
593 if (data->importPaths.contains(cPath))
594 data->importPaths.move(data->importPaths.indexOf(cPath), 0);
596 data->importPaths.prepend(cPath);
597 }
else if (!data->importPaths.contains(cPath)) {
600 data->importPaths.append(cPath);
606
607
608void QQmlTypeLoader::setImportPathList(
const QStringList &paths)
610 qCDebug(lcQmlImport) <<
"setImportPathList:" << paths;
612 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
613 data->importPaths.clear();
614 for (
const QString &path : paths)
615 addImportPath(path, AppendPath);
623
624
625void QQmlTypeLoader::setPluginPathList(
const QStringList &paths)
627 qCDebug(lcQmlImport) <<
"setPluginPathList:" << paths;
628 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
629 data->pluginPaths = paths;
633
634
635void QQmlTypeLoader::addPluginPath(
const QString& path, AddPathMode mode)
637 qCDebug(lcQmlImport) <<
"addPluginPath:" << path;
639 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
641 QUrl url = QUrl(path);
642 QString canonicalPath = path;
643 if (url.isRelative() || url.scheme() == QLatin1String(
"file")
644 || (url.scheme().size() == 1 && QFile::exists(path)) ) {
645 QDir dir = QDir(path);
646 canonicalPath = dir.canonicalPath();
649 if (mode == PrependPath)
650 data->pluginPaths.prepend(canonicalPath);
652 data->pluginPaths.append(canonicalPath);
655#if QT_CONFIG(qml_network)
656QQmlNetworkAccessManagerFactoryPtrConst QQmlTypeLoader::networkAccessManagerFactory()
const
658 ASSERT_ENGINETHREAD();
659 return QQmlNetworkAccessManagerFactoryPtrConst(&m_data);
662void QQmlTypeLoader::setNetworkAccessManagerFactory(QQmlNetworkAccessManagerFactory *factory)
664 ASSERT_ENGINETHREAD();
665 QQmlNetworkAccessManagerFactoryPtr(&m_data).reset(factory);
668QNetworkAccessManager *QQmlTypeLoader::createNetworkAccessManager(QObject *parent)
const
677 if (
const auto factory = QQmlNetworkAccessManagerFactoryPtrConst(&m_data))
678 return factory->create(parent);
680 return new QNetworkAccessManager(parent);
684void QQmlTypeLoader::clearQmldirInfo()
686 QQmlTypeLoaderThreadDataPtr data(&m_data);
688 auto itr = data->qmldirInfo.constBegin();
689 while (itr != data->qmldirInfo.constEnd()) {
690 const QQmlTypeLoaderThreadData::QmldirInfo *cache = *itr;
692 const QQmlTypeLoaderThreadData::QmldirInfo *nextCache = cache->next;
699 data->qmldirInfo.clear();
703 const QQmlTypeLoaderConfiguredDataPtr &data, QV4::ExecutionEngine *engine)
705 data->diskCacheOptions = engine->diskCacheOptions();
706 data->isDebugging = engine->debugger() !=
nullptr;
707 data->initialized =
true;
710void QQmlTypeLoader::startThread()
714 if (!m_data.thread()) {
719 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
720 initializeConfiguredData(data, m_data.engine());
721 m_data.createThread(
this);
725void QQmlTypeLoader::shutdownThread()
730 m_data.deleteThread();
733QQmlTypeLoader::Blob::PendingImport::PendingImport(
734 const QQmlRefPointer<Blob> &blob,
const QV4::CompiledData::Import *import,
735 QQmlImports::ImportFlags flags)
736 : uri(blob->stringAt(import->uriIndex))
737 , qualifier(blob->stringAt(import->qualifierIndex))
738 , type(
static_cast<QV4::CompiledData::Import::ImportType>(quint32(import->type)))
739 , location(import->location)
741 , version(import->version)
742 , searchVersion(import->version)
746QQmlTypeLoader::Blob::Blob(
const QUrl &url, QQmlDataBlob::Type type, QQmlTypeLoader *loader)
747 : QQmlDataBlob(url, type, loader)
748 , m_importCache(
new QQmlImports(), QQmlRefPointer<QQmlImports>::Adopt)
752QQmlTypeLoader::Blob::~Blob()
756bool QQmlTypeLoader::Blob::fetchQmldir(
757 const QUrl &url,
const QQmlTypeLoader::Blob::PendingImportPtr &import,
int priority,
758 QList<QQmlError> *errors)
760 assertTypeLoaderThread();
762 QQmlRefPointer<QQmlQmldirData> data = typeLoader()->getQmldir(url);
764 data->setPriority(
this, import, priority);
766 if (data->status() == Error) {
769 }
else if (data->status() == Complete) {
771 return qmldirDataAvailable(data, errors);
775 addDependency(data.data());
780
781
782
783
784void QQmlTypeLoader::Blob::importQmldirScripts(
785 const QQmlTypeLoader::Blob::PendingImportPtr &import,
786 const QQmlTypeLoaderQmldirContent &qmldir,
const QUrl &qmldirUrl)
788 assertTypeLoaderThread();
797 QQmlTypeLoaderQmldirContent redirectedQmldir = qmldir;
798 const QUrl scriptBaseUrl = redirectedQmldir.hasRedirection()
799 ? QUrl{QQmlImports::redirectQmldirContent(typeLoader(), &redirectedQmldir)}
801 const auto qmldirScripts = redirectedQmldir.scripts();
802 for (
const QQmlDirParser::Script &script : qmldirScripts) {
803 const QUrl plainUrl = QUrl(script.fileName);
804 const QUrl scriptUrl = scriptBaseUrl.resolved(plainUrl);
805 QQmlRefPointer<QQmlScriptBlob> blob = typeLoader()->getScript(scriptUrl, plainUrl);
808 if (blob.data() ==
this)
811 addDependency(blob.data());
812 scriptImported(blob, import->location, script.nameSpace, import->qualifier);
816template<
typename URL>
818 QQmlTypeLoader::Blob *self,
819 const QQmlTypeLoader::Blob::PendingImportPtr &import,
const QString &qmldirFilePath,
820 const URL &qmldirUrl)
822 self->assertTypeLoaderThread();
824 const QQmlTypeLoaderQmldirContent qmldir = self->typeLoader()->qmldirContent(qmldirFilePath);
825 if (!import->qualifier.isEmpty())
826 self->importQmldirScripts(import, qmldir, QUrl(qmldirUrl));
828 if (qmldir.plugins().isEmpty()) {
833 auto module = QQmlMetaType::typeModule(qmldir.typeNamespace(), import->version);
835 QQmlMetaType::qmlRegisterModuleTypes(qmldir.typeNamespace());
841 const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
844 QString reason = errors->front().description();
845 if (reason.size() > 512)
846 reason = reason.first(252) + QLatin1String(
"... ...") + reason.last(252);
847 if (import->version.hasMajorVersion()) {
848 error.setDescription(
849 QQmlImports::tr(
"module \"%1\" version %2.%3 cannot be imported because:\n%4")
850 .arg(import->uri, QString::number(import->version.majorVersion()),
851 import->version.hasMinorVersion()
852 ? QString::number(import->version.minorVersion())
853 : QLatin1String(
"x"),
856 error.setDescription(QQmlImports::tr(
"module \"%1\" cannot be imported because:\n%2")
857 .arg(import->uri, reason));
859 errors->prepend(error);
862bool QQmlTypeLoader::Blob::handleLocalQmldirForImport(
863 const PendingImportPtr &import,
const QString &qmldirFilePath,
864 const QString &qmldirUrl, QList<QQmlError> *errors)
867 const QTypeRevision actualVersion = m_importCache->addLibraryImport(
868 typeLoader(), import->uri, import->qualifier, import->version, qmldirFilePath,
869 qmldirUrl, import->flags, import->precedence, errors);
870 if (!actualVersion.isValid())
874 if (actualVersion.hasMajorVersion())
875 import->version = actualVersion;
877 if (!loadImportDependencies(import, qmldirFilePath, import->flags, errors)) {
878 addDependencyImportError(import, errors);
882 postProcessQmldir(
this, import, qmldirFilePath, qmldirUrl);
886bool QQmlTypeLoader::Blob::updateQmldir(
const QQmlRefPointer<QQmlQmldirData> &data,
const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
890 assertTypeLoaderThread();
892 QString qmldirIdentifier = data->urlString();
893 QString qmldirUrl = qmldirIdentifier.left(qmldirIdentifier.lastIndexOf(QLatin1Char(
'/')) + 1);
895 typeLoader()->setQmldirContent(qmldirIdentifier, data->content());
897 const QTypeRevision version = m_importCache->updateQmldirContent(
898 typeLoader(), import->uri, import->version, import->qualifier, qmldirIdentifier,
900 if (!version.isValid())
904 if (version.hasMajorVersion())
905 import->version = version;
907 if (!loadImportDependencies(import, qmldirIdentifier, import->flags, errors))
910 import->priority = 0;
915 postProcessQmldir(
this, import, qmldirIdentifier, qmldirUrl);
919bool QQmlTypeLoader::Blob::addScriptImport(
const QQmlTypeLoader::Blob::PendingImportPtr &import)
921 assertTypeLoaderThread();
922 const QUrl url(import->uri);
923 QQmlTypeLoader *loader = typeLoader();
924 QQmlRefPointer<QQmlScriptBlob> blob = loader->getScript(finalUrl().resolved(url), url);
925 addDependency(blob.data());
926 scriptImported(blob, import->location, import->qualifier, QString());
930bool QQmlTypeLoader::Blob::addFileImport(
const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
932 assertTypeLoaderThread();
933 QQmlImports::ImportFlags flags;
935 QUrl importUrl(import->uri);
936 QString path = importUrl.path();
937 path.append(QLatin1String(path.endsWith(QLatin1Char(
'/')) ?
"qmldir" :
"/qmldir"));
938 importUrl.setPath(path);
941 if (!finalUrl().isValid() && importUrl.isRelative()) {
943 error.setDescription(
944 QString::fromLatin1(
"Can't resolve relative qmldir URL %1 on invalid base URL")
945 .arg(importUrl.toString()));
946 errors->append(error);
950 QUrl qmldirUrl = finalUrl().resolved(importUrl);
951 if (!QQmlImports::isLocal(qmldirUrl)) {
953 flags = QQmlImports::ImportIncomplete;
956 const QTypeRevision version = m_importCache->addFileImport(
957 typeLoader(), import->uri, import->qualifier, import->version, flags,
958 import->precedence,
nullptr, errors);
959 if (!version.isValid())
963 if (version.hasMajorVersion())
964 import->version = version;
966 if (flags & QQmlImports::ImportIncomplete) {
967 if (!fetchQmldir(qmldirUrl, import, 1, errors))
970 const QString qmldirFilePath = QQmlFile::urlToLocalFileOrQrc(qmldirUrl);
971 if (!loadImportDependencies(import, qmldirFilePath, import->flags, errors))
974 postProcessQmldir(
this, import, qmldirFilePath, qmldirUrl);
980bool QQmlTypeLoader::Blob::addLibraryImport(
const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
982 assertTypeLoaderThread();
984 const LocalQmldirResult qmldirResult = typeLoader()->locateLocalQmldir(
this, import, errors);
985 switch (qmldirResult) {
988 case QmldirNotFound: {
989 if (!loadImportDependencies(import, QString(), import->flags, errors)) {
990 addDependencyImportError(import, errors);
995 case QmldirInterceptedToRemote:
1006 if (qmldirResult != QmldirInterceptedToRemote && registerPendingTypes(import)) {
1007 if (m_importCache->addLibraryImport(
1008 typeLoader(), import->uri, import->qualifier, import->version, QString(),
1009 QString(), import->flags, import->precedence, errors).isValid()) {
1017 m_unresolvedImports << import;
1020 const QTypeRevision version = m_importCache->addLibraryImport(
1021 typeLoader(), import->uri, import->qualifier, import->version, QString(),
1022 QString(), import->flags | QQmlImports::ImportIncomplete, import->precedence,
1025 if (!version.isValid())
1029 if (version.hasMajorVersion())
1030 import->version = version;
1032 const bool hasInterceptors = m_typeLoader->hasUrlInterceptors();
1036 QStringList remotePathList = typeLoader()->importPathList(
1037 hasInterceptors ? LocalOrRemote : Remote);
1038 if (!remotePathList.isEmpty()) {
1041 const QStringList qmlDirPaths = QQmlImports::completeQmldirPaths(
1042 import->uri, remotePathList, import->searchVersion);
1043 for (
const QString &qmldirPath : qmlDirPaths) {
1044 if (hasInterceptors) {
1045 QUrl url = m_typeLoader->interceptUrl(
1046 QQmlImports::urlFromLocalFileOrQrcOrUrl(qmldirPath),
1047 QQmlAbstractUrlInterceptor::QmldirFile);
1048 if (!QQmlFile::isLocalFile(url)
1049 && !fetchQmldir(url, import, ++priority, errors)) {
1052 }
else if (!fetchQmldir(QUrl(qmldirPath), import, ++priority, errors)) {
1061bool QQmlTypeLoader::Blob::registerPendingTypes(
const PendingImportPtr &import)
1063 assertTypeLoaderThread();
1068 QQmlMetaType::typeModule(import->uri, import->version)
1071 || QQmlMetaType::qmlRegisterModuleTypes(import->uri)
1075 || QQmlMetaType::latestModuleVersion(import->uri).isValid();
1078bool QQmlTypeLoader::Blob::addImport(
const QV4::CompiledData::Import *import,
1079 QQmlImports::ImportFlags flags, QList<QQmlError> *errors)
1081 assertTypeLoaderThread();
1082 return addImport(std::make_shared<PendingImport>(
this, import, flags), errors);
1085bool QQmlTypeLoader::Blob::addImport(
1086 const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
1088 assertTypeLoaderThread();
1092 switch (import->type)
1094 case QV4::CompiledData::Import::ImportLibrary:
1095 return addLibraryImport(import, errors);
1096 case QV4::CompiledData::Import::ImportFile:
1097 return addFileImport(import ,errors);
1098 case QV4::CompiledData::Import::ImportScript:
1099 return addScriptImport(import);
1100 case QV4::CompiledData::Import::ImportInlineComponent:
1101 Q_UNREACHABLE_RETURN(
false);
1104 Q_UNREACHABLE_RETURN(
false);
1107void QQmlTypeLoader::Blob::dependencyComplete(
const QQmlDataBlob::Ptr &blob)
1109 assertTypeLoaderThread();
1111 if (blob->type() == QQmlDataBlob::QmldirFile) {
1112 QQmlQmldirData *data =
static_cast<QQmlQmldirData *>(blob.data());
1113 QList<QQmlError> errors;
1114 if (!qmldirDataAvailable(data, &errors)) {
1115 Q_ASSERT(errors.size());
1116 QQmlError error(errors.takeFirst());
1117 error.setUrl(m_importCache->baseUrl());
1118 const QV4::CompiledData::Location importLocation = data->importLocation(
this);
1119 error.setLine(qmlConvertSourceCoordinate<quint32,
int>(importLocation.line()));
1120 error.setColumn(qmlConvertSourceCoordinate<quint32,
int>(importLocation.column()));
1121 errors.prepend(error);
1127bool QQmlTypeLoader::Blob::loadDependentImports(
1128 const QList<QQmlDirParser::Import> &imports,
const QString &qualifier,
1129 QTypeRevision version, QTypeRevision searchVersion, quint8 precedence,
1130 QQmlImports::ImportFlags flags, QList<QQmlError> *errors)
1132 assertTypeLoaderThread();
1134 for (
const auto &import : imports) {
1135 if (import.flags & QQmlDirParser::Import::Optional)
1137 auto dependencyImport = std::make_shared<PendingImport>();
1138 dependencyImport->uri = import.module;
1139 dependencyImport->qualifier = qualifier;
1140 if (import.flags & QQmlDirParser::Import::Auto) {
1141 dependencyImport->version = version;
1142 dependencyImport->searchVersion = searchVersion;
1144 dependencyImport->version = import.version;
1145 dependencyImport->searchVersion = import.version;
1147 dependencyImport->flags = flags;
1148 dependencyImport->precedence = precedence;
1150 qCDebug(lcQmlImport)
1151 <<
"loading dependent import" << dependencyImport->uri <<
"version"
1152 << dependencyImport->version <<
"as" << dependencyImport->qualifier;
1154 if (!addImport(dependencyImport, errors)) {
1156 error.setDescription(
1157 QString::fromLatin1(
1158 "Failed to load dependent import \"%1\" version %2.%3")
1159 .arg(dependencyImport->uri)
1160 .arg(dependencyImport->version.majorVersion())
1161 .arg(dependencyImport->version.minorVersion()));
1162 errors->append(error);
1170bool QQmlTypeLoader::Blob::loadImportDependencies(
1171 const QQmlTypeLoader::Blob::PendingImportPtr ¤tImport,
const QString &qmldirUri,
1172 QQmlImports::ImportFlags flags, QList<QQmlError> *errors)
1174 assertTypeLoaderThread();
1176 QList<QQmlDirParser::Import> implicitImports
1177 = QQmlMetaType::moduleImports(currentImport->uri, currentImport->version);
1178 if (!qmldirUri.isEmpty())
1179 implicitImports += typeLoader()->qmldirContent(qmldirUri).imports();
1182 switch (currentImport->precedence) {
1183 case QQmlImportInstance::Implicit - 1:
1184 case QQmlImportInstance::Lowest: {
1186 error.setDescription(
1187 QString::fromLatin1(
"Too many dependent imports for %1 %2.%3")
1188 .arg(currentImport->uri)
1189 .arg(currentImport->version.majorVersion())
1190 .arg(currentImport->version.minorVersion()));
1191 errors->append(error);
1198 if (!loadDependentImports(
1199 implicitImports, currentImport->qualifier, currentImport->version,
1200 currentImport->searchVersion, currentImport->precedence + 1, flags, errors)) {
1202 error.setDescription(
1203 QString::fromLatin1(
1204 "Failed to load dependencies for module \"%1\" version %2.%3")
1205 .arg(currentImport->uri)
1206 .arg(currentImport->version.majorVersion())
1207 .arg(currentImport->version.minorVersion()));
1208 errors->append(error);
1217 if (!QQmlTypeLoaderConfiguredDataConstPtr(m_data)->initialized)
1218 initializeConfiguredData(QQmlTypeLoaderConfiguredDataPtr(m_data), m_data->engine());
1220 return QQmlTypeLoaderConfiguredDataConstPtr(m_data);
1223bool QQmlTypeLoader::isDebugging()
1225 return configuredData(&m_data)->isDebugging;
1228bool QQmlTypeLoader::readCacheFile()
1230 return configuredData(&m_data)->diskCacheOptions & QV4::ExecutionEngine::DiskCache::QmlcRead;
1233bool QQmlTypeLoader::writeCacheFile()
1235 return configuredData(&m_data)->diskCacheOptions & QV4::ExecutionEngine::DiskCache::QmlcWrite;
1238QQmlMetaType::CacheMode QQmlTypeLoader::aotCacheMode()
1240 const QV4::ExecutionEngine::DiskCacheOptions options
1241 = configuredData(&m_data)->diskCacheOptions;
1242 if (!(options & QV4::ExecutionEngine::DiskCache::Aot))
1243 return QQmlMetaType::RejectAll;
1244 if (options & QV4::ExecutionEngine::DiskCache::AotByteCode)
1245 return QQmlMetaType::AcceptUntyped;
1246 return QQmlMetaType::RequireFullyTyped;
1249bool QQmlTypeLoader::Blob::qmldirDataAvailable(
const QQmlRefPointer<QQmlQmldirData> &data, QList<QQmlError> *errors)
1251 assertTypeLoaderThread();
1252 return data->processImports(
this, [&](
const PendingImportPtr &import) {
1253 return updateQmldir(data, import, errors);
1259 if (QDir::listSeparator() == u':') {
1261 QStringList paths = envImportPath.split(u':');
1262 bool wasEmpty =
false;
1263 for (
auto it = paths.begin(); it != paths.end();) {
1264 if (it->isEmpty()) {
1266 it = paths.erase(it);
1277 return envImportPath.split(QDir::listSeparator(), Qt::SkipEmptyParts);
1282
1283
1284QQmlTypeLoader::QQmlTypeLoader(QV4::ExecutionEngine *engine)
1290 const bool isPluginApplication = QCoreApplication::testAttribute(Qt::AA_PluginApplication);
1292 auto addEnvPath = [
this, isPluginApplication](
const char *var,
auto addPath) {
1293 if (Q_UNLIKELY(!isPluginApplication && !qEnvironmentVariableIsEmpty(var))) {
1294 const QStringList paths = parseEnvPath(qEnvironmentVariable(var));
1295 for (
const QString &path : paths)
1296 (
this->*addPath)(path, AppendPath);
1302#if defined(Q_OS_ANDROID)
1303 addImportPath(QStringLiteral(
"qrc:/android_rcc_bundle/qml"), AppendPath);
1306 if (!isPluginApplication)
1307 addImportPath(QCoreApplication::applicationDirPath(), AppendPath);
1309 addImportPath(QStringLiteral(
"qrc:/qt-project.org/imports"), AppendPath);
1310 addImportPath(QStringLiteral(
"qrc:/qt/qml"), AppendPath);
1312 addEnvPath(
"QML2_IMPORT_PATH", &QQmlTypeLoader::addImportPath);
1313 addEnvPath(
"QML_IMPORT_PATH", &QQmlTypeLoader::addImportPath);
1315 const auto qmlImportPaths = QLibraryInfo::paths(QLibraryInfo::QmlImportsPath);
1316 for (
const auto &qmlImportPath : qmlImportPaths)
1317 addImportPath(qmlImportPath, AppendPath);
1321#if defined(Q_OS_ANDROID)
1322 addEnvPath(
"QT_BUNDLED_LIBS_PATH", &QQmlTypeLoader::addPluginPath);
1324 addEnvPath(
"QML_PLUGIN_PATH", &QQmlTypeLoader::addPluginPath);
1326 const auto pluginPaths = QLibraryInfo::paths(QLibraryInfo::PluginsPath);
1327 for (
const auto &pluginPath : pluginPaths)
1328 addPluginPath(pluginPath, AppendPath);
1333 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
1334 data->pluginPaths << QLatin1String(
".");
1338
1339
1340
1341QQmlTypeLoader::~QQmlTypeLoader()
1355template<
typename Blob>
1357 const QQmlTypeLoaderSharedDataPtr &data, QQmlRefPointer<Blob> &&blob,
1358 QQmlTypeLoader::Mode mode)
1360 if ((mode == QQmlTypeLoader::PreferSynchronous && QQmlFile::isSynchronous(blob->finalUrl()))
1361 || mode == QQmlTypeLoader::Synchronous) {
1372 QQmlTypeLoaderThread *thread = data.thread();
1373 if (thread && !thread->isThisThread()) {
1374 while (!blob->isCompleteOrError())
1375 thread->waitForNextMessage();
1382
1383
1384QQmlRefPointer<QQmlTypeData> QQmlTypeLoader::getType(
const QUrl &unNormalizedUrl, Mode mode)
1388 Q_ASSERT(!unNormalizedUrl.isRelative() &&
1389 (QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl).isEmpty() ||
1390 !QDir::isRelativePath(QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl))));
1392 QQmlRefPointer<QQmlTypeData> typeData;
1394 const QUrl url = QQmlMetaType::normalizedUrl(unNormalizedUrl);
1395 QQmlTypeLoaderSharedDataPtr data(&m_data);
1397 typeData = data->typeCache.value(url);
1399 return handleExisting(data, std::move(typeData), mode);
1402 if (data->typeCache.size() >= data->typeCacheTrimThreshold)
1405 typeData = QQml::makeRefPointer<QQmlTypeData>(url,
this);
1408 data->typeCache.insert(url, typeData);
1411 return finalizeBlob(std::move(typeData), mode);
1415
1416
1417
1418QQmlRefPointer<QQmlTypeData> QQmlTypeLoader::getType(
1419 const QByteArray &data,
const QUrl &url, Mode mode)
1423 QQmlRefPointer<QQmlTypeData> typeData = QQml::makeRefPointer<QQmlTypeData>(url,
this);
1424 QQmlTypeLoader::loadWithStaticData(QQmlDataBlob::Ptr(typeData.data()), data, mode);
1430 return url.fragment() == QLatin1String(
"module") || url.path().endsWith(QLatin1String(
".mjs"));
1433QQmlRefPointer<QQmlScriptBlob> QQmlTypeLoader::getScript(
const QUrl &unNormalizedUrl, Mode mode)
1437 Q_ASSERT(!unNormalizedUrl.isRelative() &&
1438 (QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl).isEmpty() ||
1439 !QDir::isRelativePath(QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl))));
1441 QQmlRefPointer<QQmlScriptBlob> scriptBlob;
1443 const QUrl url = QQmlMetaType::normalizedUrl(unNormalizedUrl);
1444 QQmlTypeLoaderSharedDataPtr data(&m_data);
1446 scriptBlob = data->scriptCache.value(url);
1448 return handleExisting(data, std::move(scriptBlob), mode);
1450 scriptBlob = QQml::makeRefPointer<QQmlScriptBlob>(url,
this, isModuleUrl(url)
1451 ? QQmlScriptBlob::IsESModule::Yes
1452 : QQmlScriptBlob::IsESModule::No);
1453 data->scriptCache.insert(url, scriptBlob);
1456 return finalizeBlob(std::move(scriptBlob), mode);
1459QQmlRefPointer<QV4::CompiledData::CompilationUnit> QQmlTypeLoader::injectModule(
1460 const QUrl &relativeUrl,
const QV4::CompiledData::Unit *unit)
1464 QQmlRefPointer<QQmlScriptBlob> blob = QQml::makeRefPointer<QQmlScriptBlob>(
1465 relativeUrl,
this, QQmlScriptBlob::IsESModule::Yes);
1466 QQmlPrivate::CachedQmlUnit cached { unit,
nullptr,
nullptr};
1469 QQmlTypeLoaderSharedDataPtr data(&m_data);
1470 data->scriptCache.insert(relativeUrl, blob);
1473 loadWithCachedUnit(blob.data(), &cached, Synchronous);
1474 Q_ASSERT(blob->isComplete());
1475 return blob->scriptData()->compilationUnit();
1479
1480
1481
1482QQmlRefPointer<QQmlScriptBlob> QQmlTypeLoader::getScript(
1483 const QUrl &unNormalizedUrl,
const QUrl &relativeUrl)
1487 Q_ASSERT(!unNormalizedUrl.isRelative() &&
1488 (QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl).isEmpty() ||
1489 !QDir::isRelativePath(QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl))));
1491 const QUrl url = QQmlMetaType::normalizedUrl(unNormalizedUrl);
1493 QQmlRefPointer<QQmlScriptBlob> scriptBlob;
1495 QQmlTypeLoaderSharedDataPtr data(&m_data);
1496 scriptBlob = data->scriptCache.value(url);
1500 if (!scriptBlob && unNormalizedUrl != relativeUrl)
1501 scriptBlob = data->scriptCache.value(relativeUrl);
1507 scriptBlob = QQml::makeRefPointer<QQmlScriptBlob>(url,
this, isModuleUrl(url)
1508 ? QQmlScriptBlob::IsESModule::Yes
1509 : QQmlScriptBlob::IsESModule::No);
1510 data->scriptCache.insert(url, scriptBlob);
1513 return finalizeBlob(std::move(scriptBlob), PreferSynchronous);
1517
1518
1519QQmlRefPointer<QQmlQmldirData> QQmlTypeLoader::getQmldir(
const QUrl &url)
1523 Q_ASSERT(!url.isRelative() &&
1524 (QQmlFile::urlToLocalFileOrQrc(url).isEmpty() ||
1525 !QDir::isRelativePath(QQmlFile::urlToLocalFileOrQrc(url))));
1527 QQmlRefPointer<QQmlQmldirData> qmldirData;
1529 QQmlTypeLoaderSharedDataPtr data(&m_data);
1530 qmldirData = data->qmldirCache.value(url);
1534 qmldirData = QQml::makeRefPointer<QQmlQmldirData>(url,
this);
1535 data->qmldirCache.insert(url, qmldirData);
1538 QQmlTypeLoader::load(QQmlDataBlob::Ptr(qmldirData.data()));
1542bool QQmlTypeLoader::isResource(
const QString &path)
1544 const bool startsWithColon = path.at(0) == QLatin1Char(
':');
1545#if defined(Q_OS_ANDROID)
1546 return startsWithColon || path.startsWith(QLatin1String(
"assets:/"), Qt::CaseInsensitive)
1547 || path.startsWith(QLatin1String(
"content:/"), Qt::CaseInsensitive);
1549 return startsWithColon;
1554
1555
1556
1557
1558
1559
1560
1561
1562QString QQmlTypeLoader::cleanFilePath(
const QString &path)
const
1569 const QString cleanPath = QDir::cleanPath(path);
1570 if (isResource(cleanPath)) {
1572 QFileInfo fileInfo(cleanPath);
1573 return fileInfo.isFile() ? cleanPath : QString();
1576 return fileExists(cleanPath)
1583 for (qsizetype length = path.size(); length > 0; --length) {
1584 if (path[length - 1] != QLatin1Char(
'/'))
1585 return path.left(length);
1591bool QQmlTypeLoader::fileExists(
const QString &dirPath,
const QString &file)
const
1601 const QChar nullChar(QChar::Null);
1602 if (dirPath.isEmpty() || dirPath.contains(nullChar) || file.isEmpty()
1603 || file.contains(nullChar)) {
1610 if (isResource(dirPath)) {
1613 const QFileInfo info(dirPath + u'/' + file);
1614 return info.exists();
1617 const QString path = stripTrailingSlashes(dirPath);
1619 QQmlTypeLoaderSharedDataConstPtr data(&m_data);
1620 return data->importDirCache.fileExists(path, file);
1625
1626
1627
1628bool QQmlTypeLoader::directoryExists(
const QString &path)
const
1635 if (isResource(path)) {
1637 QFileInfo fileInfo(path);
1638 return fileInfo.exists() && fileInfo.isDir();
1641 const QString dirPath = stripTrailingSlashes(path);
1643 QQmlTypeLoaderSharedDataConstPtr data(&m_data);
1644 return data->importDirCache.directoryExists(path);
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659const QQmlTypeLoaderQmldirContent QQmlTypeLoader::qmldirContent(
const QString &filePathIn)
1671 QUrl url(filePathIn);
1673 QQmlTypeLoaderThreadDataPtr data(&m_data);
1675 if (url.scheme().size() < 2) {
1676 filePath = filePathIn;
1678 filePath = QQmlFile::urlToLocalFileOrQrc(url);
1679 if (filePath.isEmpty()) {
1680 if (
auto entry = data->importQmlDirCache.value(filePathIn))
1683 return QQmlTypeLoaderQmldirContent();
1687 QQmlTypeLoaderQmldirContent **val = data->importQmlDirCache.value(filePath);
1690 QQmlTypeLoaderQmldirContent *qmldir =
new QQmlTypeLoaderQmldirContent;
1692#define ERROR(description) { QQmlError e; e.setDescription(description); qmldir->setError(e); }
1693#define NOT_READABLE_ERROR QString(QLatin1String("module \"$$URI$$\" definition \"%1\" not readable"))
1694#define NOT_FOUND_ERROR QString(QLatin1String("cannot load module \"$$URI$$\": File \"%1\" not found"))
1696 QFile file(filePath);
1697 if (!fileExists(filePath)) {
1699 }
else if (file.open(QFile::ReadOnly)) {
1700 QByteArray data = file.readAll();
1701 qmldir->setContent(filePath, QString::fromUtf8(data));
1707#undef NOT_READABLE_ERROR
1708#undef NOT_FOUND_ERROR
1710 data->importQmlDirCache.insert(filePath, qmldir);
1714void QQmlTypeLoader::setQmldirContent(
const QString &url,
const QString &content)
1718 QQmlTypeLoaderThreadDataPtr data(&m_data);
1719 QQmlTypeLoaderQmldirContent *qmldir;
1720 QQmlTypeLoaderQmldirContent **val = data->importQmlDirCache.value(url);
1724 qmldir =
new QQmlTypeLoaderQmldirContent;
1725 data->importQmlDirCache.insert(url, qmldir);
1728 if (!qmldir->hasContent())
1729 qmldir->setContent(url, content);
1732template<
typename Blob>
1735 std::for_each(blobs->cbegin(), blobs->cend(), [](
const QQmlRefPointer<Blob> &blob) {
1736 blob->resetTypeLoader();
1742
1743
1744
1745void QQmlTypeLoader::clearCache()
1756 QQmlTypeLoaderThreadDataPtr threadData(&m_data);
1757 qDeleteAll(threadData->importQmlDirCache);
1758 threadData->checksumCache.clear();
1759 threadData->importQmlDirCache.clear();
1761 QQmlTypeLoaderSharedDataPtr data(&m_data);
1762 clearBlobs(&data->typeCache);
1763 clearBlobs(&data->scriptCache);
1764 clearBlobs(&data->qmldirCache);
1765 data->typeCacheTrimThreshold = QQmlTypeLoaderSharedData::MinimumTypeCacheTrimThreshold;
1766 data->importDirCache.clear();
1772
1773
1774
1775
1776
1777bool QQmlTypeLoader::removeFromCache(
const QUrl &url)
1779 const QUrl normalized = QQmlMetaType::normalizedUrl(url);
1780 const QQmlTypeLoaderSharedDataPtr data(&m_data);
1781 return data->typeCache.remove(normalized) || data->scriptCache.remove(normalized)
1782 || data->qmldirCache.remove(normalized);
1785void QQmlTypeLoader::trimCache()
1787 const QQmlTypeLoaderSharedDataPtr data(&m_data);
1791void QQmlTypeLoader::updateTypeCacheTrimThreshold(
const QQmlTypeLoaderSharedDataPtr &data)
1795 int size = data->typeCache.size();
1796 if (size > data->typeCacheTrimThreshold)
1797 data->typeCacheTrimThreshold = size * 2;
1798 if (size < data->typeCacheTrimThreshold / 2) {
1799 data->typeCacheTrimThreshold
1800 = qMax(size * 2,
int(QQmlTypeLoaderSharedData::MinimumTypeCacheTrimThreshold));
1804void QQmlTypeLoader::trimCache(
const QQmlTypeLoaderSharedDataPtr &data)
1811 bool deletedOneType =
false;
1812 for (
auto iter = data->typeCache.begin(), end = data->typeCache.end(); iter != end;) {
1813 const QQmlRefPointer<QQmlTypeData> &typeData = iter.value();
1818 if (typeData->count() != 1 || !typeData->isCompleteOrError()) {
1827 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit
1828 = typeData->m_compiledData;
1829 if (compilationUnit) {
1830 if (compilationUnit->count()
1831 > QQmlMetaType::countInternalCompositeTypeSelfReferences(
1832 compilationUnit) + 1) {
1837 QQmlMetaType::unregisterInternalCompositeType(compilationUnit);
1838 Q_ASSERT(compilationUnit->count() == 1);
1842 iter = data->typeCache.erase(iter);
1843 deletedOneType =
true;
1846 if (!deletedOneType)
1852 updateTypeCacheTrimThreshold(data);
1854 QQmlMetaType::freeUnusedTypesAndCaches();
1857bool QQmlTypeLoader::isTypeLoaded(
const QUrl &url)
const
1859 const QQmlTypeLoaderSharedDataConstPtr data(&m_data);
1860 return data->typeCache.contains(url);
1863bool QQmlTypeLoader::isScriptLoaded(
const QUrl &url)
const
1865 const QQmlTypeLoaderSharedDataConstPtr data(&m_data);
1866 return data->scriptCache.contains(url);
1871 const auto dir = path.left(path.lastIndexOf(u"/") + 1);
1872 if (dir.at(0) == u':')
1873 return QStringLiteral(
"qrc") + dir;
1875 return QUrl::fromLocalFile(dir).toString();
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891QQmlTypeLoader::LocalQmldirResult QQmlTypeLoader::locateLocalQmldir(
1892 QQmlTypeLoader::Blob *blob,
const QQmlTypeLoader::Blob::PendingImportPtr &import,
1893 QList<QQmlError> *errors)
1897 LocalQmldirResult result = QmldirNotFound;
1898 QQmlTypeLoaderThreadData::QmldirInfo *cacheTail =
nullptr;
1900 QQmlTypeLoaderThreadDataPtr threadData(&m_data);
1901 QQmlTypeLoaderThreadData::QmldirInfo **cachePtr = threadData->qmldirInfo.value(import->uri);
1902 QQmlTypeLoaderThreadData::QmldirInfo *cacheHead = cachePtr ? *cachePtr :
nullptr;
1904 cacheTail = cacheHead;
1906 if (cacheTail->version == import->searchVersion) {
1907 if (cacheTail->qmldirFilePath.isEmpty()) {
1908 return cacheTail->qmldirPathUrl.isEmpty()
1910 : QmldirInterceptedToRemote;
1912 if (blob->handleLocalQmldirForImport(
1913 import, cacheTail->qmldirFilePath, cacheTail->qmldirPathUrl, errors)) {
1916 result = QmldirRejected;
1918 }
while (cacheTail->next && (cacheTail = cacheTail->next));
1924 if (result != QmldirNotFound
1925 || QQmlMetaType::isStronglyLockedModule(import->uri, import->version)) {
1929 QQmlTypeLoaderConfiguredDataConstPtr configuredData(&m_data);
1930 const bool hasInterceptors = !configuredData->urlInterceptors.isEmpty();
1933 QStringList localImportPaths = importPathList(hasInterceptors ? LocalOrRemote : Local);
1936 const QStringList qmlDirPaths = QQmlImports::completeQmldirPaths(
1937 import->uri, localImportPaths, import->searchVersion);
1939 QString qmldirCleanFilePath;
1940 for (QString qmldirPath : qmlDirPaths) {
1941 if (hasInterceptors) {
1952 const QUrl intercepted = doInterceptUrl(
1953 QQmlImports::urlFromLocalFileOrQrcOrUrl(qmldirPath),
1954 QQmlAbstractUrlInterceptor::QmldirFile,
1955 configuredData->urlInterceptors);
1956 qmldirPath = QQmlFile::urlToLocalFileOrQrc(intercepted);
1957 if (result != QmldirInterceptedToRemote
1958 && qmldirPath.isEmpty()
1959 && !QQmlFile::isLocalFile(intercepted)) {
1960 result = QmldirInterceptedToRemote;
1967 qmldirCleanFilePath = cleanFilePath(localPathForUrlOrPath(qmldirPath));
1969 if (!qmldirCleanFilePath.isEmpty()) {
1970 QString url = pathToUrl(qmldirCleanFilePath);
1971 if (url.startsWith(QStringLiteral(
"file:")))
1972 sanitizeUNCPath(&qmldirCleanFilePath);
1974 QQmlTypeLoaderThreadData::QmldirInfo *cache =
new QQmlTypeLoaderThreadData::QmldirInfo;
1975 cache->version = import->searchVersion;
1976 cache->qmldirFilePath = qmldirCleanFilePath;
1977 cache->qmldirPathUrl = url;
1978 cache->next =
nullptr;
1980 cacheTail->next = cache;
1982 threadData->qmldirInfo.insert(import->uri, cache);
1985 if (result != QmldirFound) {
1986 result = blob->handleLocalQmldirForImport(
1987 import, qmldirCleanFilePath, url, errors)
1997 if (result == QmldirNotFound || result == QmldirInterceptedToRemote) {
1998 QQmlTypeLoaderThreadData::QmldirInfo *cache =
new QQmlTypeLoaderThreadData::QmldirInfo;
1999 cache->version = import->searchVersion;
2000 cache->next = cacheHead;
2001 if (result == QmldirInterceptedToRemote) {
2004 cache->qmldirPathUrl = QStringLiteral(
"intercepted");
2006 threadData->qmldirInfo.insert(import->uri, cache);
2008 if (result == QmldirNotFound) {
2009 qCDebug(lcQmlImport)
2010 <<
"locateLocalQmldir:" << qPrintable(import->uri)
2011 <<
"module's qmldir file not found";
2014 qCDebug(lcQmlImport)
2015 <<
"locateLocalQmldir:" << qPrintable(import->uri) <<
"module's qmldir found at"
2016 << qmldirCleanFilePath;
#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)
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