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;
111QUrl QQmlTypeLoader::interceptUrl(
const QUrl &url, QQmlAbstractUrlInterceptor::DataType type)
const
117 case QQmlAbstractUrlInterceptor::QmlFile:
118 case QQmlAbstractUrlInterceptor::JavaScriptFile: {
133 const QQmlTypeLoaderSharedDataConstPtr data(&m_data);
134 const auto it = data->typeCache.constFind(QQmlMetaType::normalizedUrl(url));
135 if (it != data->typeCache.constEnd())
139 case QQmlAbstractUrlInterceptor::QmldirFile:
145 return interceptUncachedUrl(url, type);
148static QUrl doInterceptUrl(
149 const QUrl &url, QQmlAbstractUrlInterceptor::DataType type,
150 const QList<QQmlAbstractUrlInterceptor *> &urlInterceptors)
153 for (QQmlAbstractUrlInterceptor *interceptor : urlInterceptors)
154 result = interceptor->intercept(result, type);
158QUrl QQmlTypeLoader::interceptUncachedUrl(
const QUrl &url,
159 QQmlAbstractUrlInterceptor::DataType type)
const
163 QQmlTypeLoaderConfiguredDataConstPtr data(&m_data);
164 return doInterceptUrl(url, type, data->urlInterceptors);
167bool QQmlTypeLoader::hasUrlInterceptors()
const
170 QQmlTypeLoaderConfiguredDataConstPtr data(&m_data);
171 return !data->urlInterceptors.isEmpty();
174#if QT_CONFIG(qml_debug)
175void QQmlTypeLoader::setProfiler(QQmlProfiler *profiler)
177 ASSERT_ENGINETHREAD();
179 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
180 Q_ASSERT(!data->profiler);
181 data->profiler.reset(profiler);
186 void loadThread(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
188 loader->loadThread(blob);
190 void load(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
192 loader->ensureThread()->load(blob);
194 void loadAsync(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
196 loader->ensureThread()->loadAsync(blob);
204 void loadThread(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
206 loader->loadWithStaticDataThread(blob, data);
208 void load(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
210 loader->ensureThread()->loadWithStaticData(blob, data);
212 void loadAsync(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
214 loader->ensureThread()->loadWithStaticDataAsync(blob, data);
222 void loadThread(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
224 loader->loadWithCachedUnitThread(blob, unit);
226 void load(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
228 loader->ensureThread()->loadWithCachedUnit(blob, unit);
230 void loadAsync(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
232 loader->ensureThread()->loadWithCachedUnitAsync(blob, unit);
236template<
typename Loader>
237void QQmlTypeLoader::doLoad(
const Loader &loader,
const QQmlDataBlob::Ptr &blob, Mode mode)
241 qWarning(
"QQmlTypeLoader::doLoad(%s): %s thread", qPrintable(blob->urlString()),
242 (m_thread && m_thread->isThisThread()) ?
"Compile" :
"Engine");
244 blob->startLoading();
246 if (QQmlTypeLoaderThread *t = thread(); t && t->isThisThread()) {
247 loader.loadThread(
this, blob);
251 if (mode == Asynchronous) {
252 blob->setIsAsync(
true);
253 loader.loadAsync(
this, blob);
257 loader.load(
this, blob);
258 if (blob->isCompleteOrError())
261 if (mode == PreferSynchronous) {
262 blob->setIsAsync(
true);
266 Q_ASSERT(mode == Synchronous);
269 QQmlTypeLoaderSharedDataConstPtr lock(&m_data);
271 m_data.thread()->waitForNextMessage();
272 }
while (!blob->isCompleteOrError());
276
277
278
279
280void QQmlTypeLoader::load(
const QQmlDataBlob::Ptr &blob, Mode mode)
283 doLoad(PlainLoader(), blob, mode);
287
288
289
290
291void QQmlTypeLoader::loadWithStaticData(
292 const QQmlDataBlob::Ptr &blob,
const QByteArray &data, Mode mode)
295 doLoad(StaticLoader(data), blob, mode);
298void QQmlTypeLoader::loadWithCachedUnit(
299 const QQmlDataBlob::Ptr &blob,
const QQmlPrivate::CachedQmlUnit *unit, Mode mode)
302 doLoad(CachedLoader(unit), blob, mode);
305void QQmlTypeLoader::drop(
const QQmlDataBlob::Ptr &blob)
313 if (QQmlTypeLoaderThread *t = thread(); t && !t->isThisThread())
317void QQmlTypeLoader::loadWithStaticDataThread(
const QQmlDataBlob::Ptr &blob,
const QByteArray &data)
321 setData(blob, data, DataOrigin::Static);
324void QQmlTypeLoader::loadWithCachedUnitThread(
const QQmlDataBlob::Ptr &blob,
const QQmlPrivate::CachedQmlUnit *unit)
328 setCachedUnit(blob, unit);
331void QQmlTypeLoader::loadThread(
const QQmlDataBlob::Ptr &blob)
335 if (blob->m_url.isEmpty()) {
337 error.setDescription(QLatin1String(
"Invalid null URL"));
338 blob->setError(error);
342 if (QQmlFile::isSynchronous(blob->m_url)) {
343 const QString fileName = QQmlFile::urlToLocalFileOrQrc(blob->m_url);
344 if (fileName.isEmpty()) {
345 blob->setError(QLatin1String(
"URL cannot be represented as a local file: "
346 "unexpected NUL or percent-encoded directory separator "
347 "(%2F, or %5C on Windows)."));
351 const bool exists = fileExists(fileName);
352 if (!exists && !isResource(fileName) && QFileInfo::exists(fileName)) {
356 blob->setError(QLatin1String(
"File name case mismatch"));
360 if (blob->setProgress(1.f) && blob->isAsync())
361 thread()->callDownloadProgressChanged(blob, 1.);
363 setData(blob, fileName, exists);
366#if QT_CONFIG(qml_network)
367 QNetworkReply *reply = thread()->networkAccessManager()->get(QNetworkRequest(blob->m_url));
368 QQmlTypeLoaderNetworkReplyProxy *nrp = thread()->networkReplyProxy();
370 QQmlTypeLoaderThreadDataPtr data(&m_data);
371 data->networkReplies.insert(reply, blob);
373 if (reply->isFinished()) {
374 nrp->manualFinished(reply);
376 QObject::connect(reply, &QNetworkReply::downloadProgress,
377 nrp, &QQmlTypeLoaderNetworkReplyProxy::downloadProgress);
378 QObject::connect(reply, &QNetworkReply::finished,
379 nrp, &QQmlTypeLoaderNetworkReplyProxy::finished);
383 qWarning(
"QQmlDataBlob: requested %s", qPrintable(blob->urlString()));
389#define DATALOADER_MAXIMUM_REDIRECT_RECURSION 16
391#if QT_CONFIG(qml_network)
392void QQmlTypeLoader::networkReplyFinished(QNetworkReply *reply)
396 reply->deleteLater();
398 QQmlTypeLoaderThreadDataPtr data(&m_data);
399 QQmlRefPointer<QQmlDataBlob> blob = data->networkReplies.take(reply);
403 blob->m_redirectCount++;
405 if (blob->m_redirectCount < DATALOADER_MAXIMUM_REDIRECT_RECURSION) {
406 QVariant redirect = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
407 if (redirect.isValid()) {
408 QUrl url = reply->url().resolved(redirect.toUrl());
409 blob->m_finalUrl = url;
410 blob->m_finalUrlString.clear();
412 QNetworkReply *reply = thread()->networkAccessManager()->get(QNetworkRequest(url));
413 QObject *nrp = thread()->networkReplyProxy();
414 QObject::connect(reply, SIGNAL(finished()), nrp, SLOT(finished()));
415 data->networkReplies.insert(reply, std::move(blob));
417 qWarning(
"QQmlDataBlob: redirected to %s", qPrintable(blob->finalUrlString()));
423 if (reply->error()) {
424 blob->networkError(reply->error());
426 QByteArray data = reply->readAll();
427 setData(blob, data, DataOrigin::Device);
431void QQmlTypeLoader::networkReplyProgress(QNetworkReply *reply,
432 qint64 bytesReceived, qint64 bytesTotal)
436 QQmlTypeLoaderThreadDataConstPtr data(&m_data);
437 const QQmlRefPointer<QQmlDataBlob> blob = data->networkReplies.value(reply);
441 if (bytesTotal != 0) {
442 const qreal progress = (qreal(bytesReceived) / qreal(bytesTotal));
443 if (blob->setProgress(progress) && blob->isAsync())
444 thread()->callDownloadProgressChanged(blob, blob->progress());
450
451
452
453template<
class Interface>
455 Interface *iface, QQmlTypeLoaderThread *thread, QQmlTypeLoaderLockedData *data,
461 if (thread && thread->isThisThread())
462 thread->initializeEngine(iface, uri);
464 iface->initializeEngine(data->engine()->qmlEngine(), uri);
467void QQmlTypeLoader::initializeEngine(QQmlEngineExtensionInterface *iface,
const char *uri)
470 doInitializeEngine(iface, thread(), &m_data, uri);
473void QQmlTypeLoader::initializeEngine(QQmlExtensionInterface *iface,
const char *uri)
476 doInitializeEngine(iface, thread(), &m_data, uri);
480
481
482
483
484
485
486
487
488void QQmlTypeLoader::setData(
const QQmlDataBlob::Ptr &blob,
const QByteArray &data, DataOrigin origin)
492 QQmlDataBlob::SourceCodeData d;
493 d.inlineSourceCode = QString::fromUtf8(data);
494 d.hasInlineSourceCode =
true;
495 d.hasStaticData = (origin == DataOrigin::Static);
499void QQmlTypeLoader::setData(
const QQmlDataBlob::Ptr &blob,
const QString &fileName,
bool exists)
503 QQmlDataBlob::SourceCodeData d;
504 d.fileInfo = QFileInfo(fileName);
505 d.fileExists = exists;
509void QQmlTypeLoader::setData(
const QQmlDataBlob::Ptr &blob,
const QQmlDataBlob::SourceCodeData &d)
513 Q_TRACE_SCOPE(QQmlCompiling, blob->url());
514 QQmlCompilingProfiler prof(profiler(), blob.data());
516 blob->m_inCallback =
true;
518 blob->dataReceived(d);
520 if (!blob->isError() && !blob->isWaiting())
521 blob->allDependenciesDone();
523 blob->m_inCallback =
false;
528void QQmlTypeLoader::setCachedUnit(
const QQmlDataBlob::Ptr &blob,
const QQmlPrivate::CachedQmlUnit *unit)
532 Q_TRACE_SCOPE(QQmlCompiling, blob->url());
533 QQmlCompilingProfiler prof(profiler(), blob.data());
535 blob->m_inCallback =
true;
537 blob->initializeFromCachedUnit(unit);
539 if (!blob->isError() && !blob->isWaiting())
540 blob->allDependenciesDone();
542 blob->m_inCallback =
false;
549#if defined(Q_OS_UNIX)
550 return (path.at(0) == QLatin1Char(
'/'));
553 return fi.isAbsolute();
559 return path.startsWith(u':') || isPathAbsolute(path);
569 if (urlOrPath.isEmpty() || isPathQrcOrAbsolute(urlOrPath))
571 return QQmlFile::urlToLocalFileOrQrc(urlOrPath);
575
576
577QStringList QQmlTypeLoader::importPathList(PathType type)
const
579 QQmlTypeLoaderConfiguredDataConstPtr data(&m_data);
580 if (type == LocalOrRemote)
581 return data->importPaths;
584 for (
const QString &path : data->importPaths) {
590 if ((isPathAbsolute(path) || QQmlFile::isLocalFile(path)) == (type == Local))
598
599
600void QQmlTypeLoader::addImportPath(
const QString &path, AddPathMode mode)
602 qCDebug(lcQmlImport) <<
"addImportPath:" << path;
607 QUrl url = QUrl(path);
610 if (url.scheme() == QLatin1String(
"file")) {
611 cPath = QQmlFile::urlToLocalFileOrQrc(url);
612 }
else if (path.startsWith(QLatin1Char(
':'))) {
615 cPath = QLatin1String(
"qrc") + path;
616 cPath.replace(QLatin1Char(
'\\'), QLatin1Char(
'/'));
617 }
else if (url.isRelative() ||
618 (url.scheme().size() == 1 && QFile::exists(path)) ) {
619 QDir dir = QDir(path);
620 cPath = dir.canonicalPath();
623 cPath.replace(QLatin1Char(
'\\'), QLatin1Char(
'/'));
626 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
627 if (!cPath.isEmpty()) {
628 if (mode == PrependPath) {
631 if (data->importPaths.contains(cPath))
632 data->importPaths.move(data->importPaths.indexOf(cPath), 0);
634 data->importPaths.prepend(cPath);
635 }
else if (!data->importPaths.contains(cPath)) {
638 data->importPaths.append(cPath);
644
645
646void QQmlTypeLoader::setImportPathList(
const QStringList &paths)
648 qCDebug(lcQmlImport) <<
"setImportPathList:" << paths;
650 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
651 data->importPaths.clear();
652 for (
const QString &path : paths)
653 addImportPath(path, AppendPath);
661
662
663void QQmlTypeLoader::setPluginPathList(
const QStringList &paths)
665 qCDebug(lcQmlImport) <<
"setPluginPathList:" << paths;
666 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
667 data->pluginPaths = paths;
671
672
673void QQmlTypeLoader::addPluginPath(
const QString& path, AddPathMode mode)
675 qCDebug(lcQmlImport) <<
"addPluginPath:" << path;
677 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
679 QUrl url = QUrl(path);
680 QString canonicalPath = path;
681 if (url.isRelative() || url.scheme() == QLatin1String(
"file")
682 || (url.scheme().size() == 1 && QFile::exists(path)) ) {
683 QDir dir = QDir(path);
684 canonicalPath = dir.canonicalPath();
687 if (mode == PrependPath)
688 data->pluginPaths.prepend(canonicalPath);
690 data->pluginPaths.append(canonicalPath);
693#if QT_CONFIG(qml_network)
694QQmlNetworkAccessManagerFactoryPtrConst QQmlTypeLoader::networkAccessManagerFactory()
const
696 ASSERT_ENGINETHREAD();
697 return QQmlNetworkAccessManagerFactoryPtrConst(&m_data);
700void QQmlTypeLoader::setNetworkAccessManagerFactory(QQmlNetworkAccessManagerFactory *factory)
702 ASSERT_ENGINETHREAD();
703 QQmlNetworkAccessManagerFactoryPtr(&m_data).reset(factory);
706QNetworkAccessManager *QQmlTypeLoader::createNetworkAccessManager(QObject *parent)
const
715 if (
const auto factory = QQmlNetworkAccessManagerFactoryPtrConst(&m_data))
716 return factory->create(parent);
718 return new QNetworkAccessManager(parent);
722void QQmlTypeLoader::clearQmldirInfo()
724 QQmlTypeLoaderThreadDataPtr data(&m_data);
726 auto itr = data->qmldirInfo.constBegin();
727 while (itr != data->qmldirInfo.constEnd()) {
728 const QQmlTypeLoaderThreadData::QmldirInfo *cache = *itr;
730 const QQmlTypeLoaderThreadData::QmldirInfo *nextCache = cache->next;
737 data->qmldirInfo.clear();
741 const QQmlTypeLoaderConfiguredDataPtr &data, QV4::ExecutionEngine *engine)
743 data->diskCacheOptions = engine->diskCacheOptions();
744 data->debugInstructionPass = engine->debugInstructionPass();
745 data->initialized =
true;
748void QQmlTypeLoader::startThread()
752 if (!m_data.thread()) {
757 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
758 initializeConfiguredData(data, m_data.engine());
759 m_data.createThread(
this);
763void QQmlTypeLoader::shutdownThread()
768 m_data.deleteThread();
771QQmlTypeLoader::Blob::PendingImport::PendingImport(
772 const QQmlRefPointer<Blob> &blob,
const QV4::CompiledData::Import *import,
773 QQmlImports::ImportFlags flags)
774 : uri(blob->stringAt(import->uriIndex))
775 , qualifier(blob->stringAt(import->qualifierIndex))
776 , type(
static_cast<QV4::CompiledData::Import::ImportType>(quint32(import->type)))
777 , location(import->location)
779 , version(import->version)
780 , searchVersion(import->version)
784QQmlTypeLoader::Blob::Blob(
const QUrl &url, QQmlDataBlob::Type type, QQmlTypeLoader *loader)
785 : QQmlDataBlob(url, type, loader)
786 , m_importCache(
new QQmlImports(), QQmlRefPointer<QQmlImports>::Adopt)
790QQmlTypeLoader::Blob::~Blob()
794bool QQmlTypeLoader::Blob::fetchQmldir(
795 const QUrl &url,
const QQmlTypeLoader::Blob::PendingImportPtr &import,
int priority,
796 QList<QQmlError> *errors)
798 assertTypeLoaderThread();
800 QQmlRefPointer<QQmlQmldirData> data = typeLoader()->getQmldir(url);
802 data->setPriority(
this, import, priority);
804 if (data->status() == Error) {
807 }
else if (data->status() == Complete) {
809 return qmldirDataAvailable(data, errors);
813 addDependency(data.data());
818
819
820
821
822void QQmlTypeLoader::Blob::importQmldirScripts(
823 const QQmlTypeLoader::Blob::PendingImportPtr &import,
824 const QQmlTypeLoaderQmldirContent &qmldir,
const QUrl &qmldirUrl)
826 assertTypeLoaderThread();
835 QQmlTypeLoaderQmldirContent redirectedQmldir = qmldir;
836 const QUrl scriptBaseUrl = redirectedQmldir.hasRedirection()
837 ? QUrl{QQmlImports::redirectQmldirContent(typeLoader(), &redirectedQmldir)}
839 const auto qmldirScripts = redirectedQmldir.scripts();
840 for (
const QQmlDirParser::Script &script : qmldirScripts) {
841 const QUrl plainUrl = QUrl(script.fileName);
842 const QUrl scriptUrl = scriptBaseUrl.resolved(plainUrl);
843 QQmlRefPointer<QQmlScriptBlob> blob = typeLoader()->getScript(scriptUrl, plainUrl);
846 if (blob.data() ==
this)
849 addDependency(blob.data());
850 scriptImported(blob, import->location, script.nameSpace, import->qualifier);
854template<
typename URL>
856 QQmlTypeLoader::Blob *self,
857 const QQmlTypeLoader::Blob::PendingImportPtr &import,
const QString &qmldirFilePath,
858 const URL &qmldirUrl)
860 self->assertTypeLoaderThread();
862 const QQmlTypeLoaderQmldirContent qmldir = self->typeLoader()->qmldirContent(qmldirFilePath);
863 if (!import->qualifier.isEmpty())
864 self->importQmldirScripts(import, qmldir, QUrl(qmldirUrl));
866 if (qmldir.plugins().isEmpty()) {
871 auto module = QQmlMetaType::typeModule(qmldir.typeNamespace(), import->version);
873 QQmlMetaType::qmlRegisterModuleTypes(qmldir.typeNamespace());
879 const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
882 QString reason = errors->front().description();
883 if (reason.size() > 512)
884 reason = reason.first(252) + QLatin1String(
"... ...") + reason.last(252);
885 if (import->version.hasMajorVersion()) {
886 error.setDescription(
887 QQmlImports::tr(
"module \"%1\" version %2.%3 cannot be imported because:\n%4")
888 .arg(import->uri, QString::number(import->version.majorVersion()),
889 import->version.hasMinorVersion()
890 ? QString::number(import->version.minorVersion())
891 : QLatin1String(
"x"),
894 error.setDescription(QQmlImports::tr(
"module \"%1\" cannot be imported because:\n%2")
895 .arg(import->uri, reason));
897 errors->prepend(error);
900bool QQmlTypeLoader::Blob::handleLocalQmldirForImport(
901 const PendingImportPtr &import,
const QString &qmldirFilePath,
902 const QString &qmldirUrl, QList<QQmlError> *errors)
905 const QTypeRevision actualVersion = m_importCache->addLibraryImport(
906 typeLoader(), import->uri, import->qualifier, import->version, qmldirFilePath,
907 qmldirUrl, import->flags, import->precedence, errors);
908 if (!actualVersion.isValid())
912 if (actualVersion.hasMajorVersion())
913 import->version = actualVersion;
915 if (!loadImportDependencies(import, qmldirFilePath, import->flags, errors)) {
916 addDependencyImportError(import, errors);
920 postProcessQmldir(
this, import, qmldirFilePath, qmldirUrl);
924bool QQmlTypeLoader::Blob::updateQmldir(
const QQmlRefPointer<QQmlQmldirData> &data,
const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
928 assertTypeLoaderThread();
930 QString qmldirIdentifier = data->urlString();
931 QString qmldirUrl = qmldirIdentifier.left(qmldirIdentifier.lastIndexOf(QLatin1Char(
'/')) + 1);
933 typeLoader()->setQmldirContent(qmldirIdentifier, data->content());
935 const QTypeRevision version = m_importCache->updateQmldirContent(
936 typeLoader(), import->uri, import->version, import->qualifier, qmldirIdentifier,
938 if (!version.isValid())
942 if (version.hasMajorVersion())
943 import->version = version;
945 if (!loadImportDependencies(import, qmldirIdentifier, import->flags, errors))
948 import->priority = 0;
953 postProcessQmldir(
this, import, qmldirIdentifier, qmldirUrl);
957bool QQmlTypeLoader::Blob::addScriptImport(
const QQmlTypeLoader::Blob::PendingImportPtr &import)
959 assertTypeLoaderThread();
960 const QUrl url(import->uri);
961 QQmlTypeLoader *loader = typeLoader();
962 QQmlRefPointer<QQmlScriptBlob> blob = loader->getScript(finalUrl().resolved(url), url);
963 addDependency(blob.data());
964 scriptImported(blob, import->location, import->qualifier, QString());
968bool QQmlTypeLoader::Blob::addFileImport(
const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
970 assertTypeLoaderThread();
971 QQmlImports::ImportFlags flags;
973 QUrl importUrl(import->uri);
974 QString path = importUrl.path();
975 path.append(QLatin1String(path.endsWith(QLatin1Char(
'/')) ?
"qmldir" :
"/qmldir"));
976 importUrl.setPath(path);
979 if (!finalUrl().isValid() && importUrl.isRelative()) {
981 error.setDescription(
982 QString::fromLatin1(
"Can't resolve relative qmldir URL %1 on invalid base URL")
983 .arg(importUrl.toString()));
984 errors->append(error);
988 QUrl qmldirUrl = finalUrl().resolved(importUrl);
989 if (!QQmlImports::isLocal(qmldirUrl)) {
991 flags = QQmlImports::ImportIncomplete;
994 const QTypeRevision version = m_importCache->addFileImport(
995 typeLoader(), import->uri, import->qualifier, import->version, flags,
996 import->precedence,
nullptr, errors);
997 if (!version.isValid())
1001 if (version.hasMajorVersion())
1002 import->version = version;
1004 if (flags & QQmlImports::ImportIncomplete) {
1005 if (!fetchQmldir(qmldirUrl, import, 1, errors))
1008 const QString qmldirFilePath = QQmlFile::urlToLocalFileOrQrc(qmldirUrl);
1009 if (!loadImportDependencies(import, qmldirFilePath, import->flags, errors))
1012 postProcessQmldir(
this, import, qmldirFilePath, qmldirUrl);
1018bool QQmlTypeLoader::Blob::addLibraryImport(
const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
1020 assertTypeLoaderThread();
1022 const LocalQmldirResult qmldirResult = typeLoader()->locateLocalQmldir(
this, import, errors);
1023 switch (qmldirResult) {
1026 case QmldirNotFound: {
1027 if (!loadImportDependencies(import, QString(), import->flags, errors)) {
1028 addDependencyImportError(import, errors);
1033 case QmldirInterceptedToRemote:
1035 case QmldirRejected:
1044 if (qmldirResult != QmldirInterceptedToRemote && registerPendingTypes(import)) {
1045 if (m_importCache->addLibraryImport(
1046 typeLoader(), import->uri, import->qualifier, import->version, QString(),
1047 QString(), import->flags, import->precedence, errors).isValid()) {
1055 m_unresolvedImports << import;
1058 const QTypeRevision version = m_importCache->addLibraryImport(
1059 typeLoader(), import->uri, import->qualifier, import->version, QString(),
1060 QString(), import->flags | QQmlImports::ImportIncomplete, import->precedence,
1063 if (!version.isValid())
1067 if (version.hasMajorVersion())
1068 import->version = version;
1070 const bool hasInterceptors = m_typeLoader->hasUrlInterceptors();
1074 QStringList remotePathList = typeLoader()->importPathList(
1075 hasInterceptors ? LocalOrRemote : Remote);
1076 if (!remotePathList.isEmpty()) {
1079 const QStringList qmlDirPaths = QQmlImports::completeQmldirPaths(
1080 import->uri, remotePathList, import->searchVersion);
1081 for (
const QString &qmldirPath : qmlDirPaths) {
1082 if (hasInterceptors) {
1083 QUrl url = m_typeLoader->interceptUrl(
1084 QQmlImports::urlFromLocalFileOrQrcOrUrl(qmldirPath),
1085 QQmlAbstractUrlInterceptor::QmldirFile);
1086 if (!QQmlFile::isLocalFile(url)
1087 && !fetchQmldir(url, import, ++priority, errors)) {
1090 }
else if (!fetchQmldir(QUrl(qmldirPath), import, ++priority, errors)) {
1099bool QQmlTypeLoader::Blob::registerPendingTypes(
const PendingImportPtr &import)
1101 assertTypeLoaderThread();
1106 QQmlMetaType::typeModule(import->uri, import->version)
1109 || QQmlMetaType::qmlRegisterModuleTypes(import->uri)
1113 || QQmlMetaType::latestModuleVersion(import->uri).isValid();
1116bool QQmlTypeLoader::Blob::addImport(
const QV4::CompiledData::Import *import,
1117 QQmlImports::ImportFlags flags, QList<QQmlError> *errors)
1119 assertTypeLoaderThread();
1120 return addImport(std::make_shared<PendingImport>(
this, import, flags), errors);
1123bool QQmlTypeLoader::Blob::addImport(
1124 const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
1126 assertTypeLoaderThread();
1130 switch (import->type)
1132 case QV4::CompiledData::Import::ImportLibrary:
1133 return addLibraryImport(import, errors);
1134 case QV4::CompiledData::Import::ImportFile:
1135 return addFileImport(import ,errors);
1136 case QV4::CompiledData::Import::ImportScript:
1137 return addScriptImport(import);
1138 case QV4::CompiledData::Import::ImportInlineComponent:
1139 Q_UNREACHABLE_RETURN(
false);
1142 Q_UNREACHABLE_RETURN(
false);
1145void QQmlTypeLoader::Blob::dependencyComplete(
const QQmlDataBlob::Ptr &blob)
1147 assertTypeLoaderThread();
1149 if (blob->type() == QQmlDataBlob::QmldirFile) {
1150 QQmlQmldirData *data =
static_cast<QQmlQmldirData *>(blob.data());
1151 QList<QQmlError> errors;
1152 if (!qmldirDataAvailable(data, &errors)) {
1153 Q_ASSERT(errors.size());
1154 QQmlError error(errors.takeFirst());
1155 error.setUrl(m_importCache->baseUrl());
1156 const QV4::CompiledData::Location importLocation = data->importLocation(
this);
1157 error.setLine(qmlConvertSourceCoordinate<quint32,
int>(importLocation.line()));
1158 error.setColumn(qmlConvertSourceCoordinate<quint32,
int>(importLocation.column()));
1159 errors.prepend(error);
1165bool QQmlTypeLoader::Blob::loadDependentImports(
1166 const QList<QQmlDirParser::Import> &imports,
const QString &qualifier,
1167 QTypeRevision version, QTypeRevision searchVersion, quint8 precedence,
1168 QQmlImports::ImportFlags flags, QList<QQmlError> *errors)
1170 assertTypeLoaderThread();
1172 for (
const auto &import : imports) {
1173 if (import.flags & QQmlDirParser::Import::Optional)
1175 auto dependencyImport = std::make_shared<PendingImport>();
1176 dependencyImport->uri = import.module;
1177 dependencyImport->qualifier = qualifier;
1178 if (import.flags & QQmlDirParser::Import::Auto) {
1179 dependencyImport->version = version;
1180 dependencyImport->searchVersion = searchVersion;
1182 dependencyImport->version = import.version;
1183 dependencyImport->searchVersion = import.version;
1185 dependencyImport->flags = flags;
1186 dependencyImport->precedence = precedence;
1188 qCDebug(lcQmlImport)
1189 <<
"loading dependent import" << dependencyImport->uri <<
"version"
1190 << dependencyImport->version <<
"as" << dependencyImport->qualifier;
1192 if (!addImport(dependencyImport, errors)) {
1194 error.setDescription(
1195 QString::fromLatin1(
1196 "Failed to load dependent import \"%1\" version %2.%3")
1197 .arg(dependencyImport->uri)
1198 .arg(dependencyImport->version.majorVersion())
1199 .arg(dependencyImport->version.minorVersion()));
1200 errors->append(error);
1208bool QQmlTypeLoader::Blob::loadImportDependencies(
1209 const QQmlTypeLoader::Blob::PendingImportPtr ¤tImport,
const QString &qmldirUri,
1210 QQmlImports::ImportFlags flags, QList<QQmlError> *errors)
1212 assertTypeLoaderThread();
1214 QList<QQmlDirParser::Import> implicitImports
1215 = QQmlMetaType::moduleImports(currentImport->uri, currentImport->version);
1216 if (!qmldirUri.isEmpty())
1217 implicitImports += typeLoader()->qmldirContent(qmldirUri).imports();
1220 switch (currentImport->precedence) {
1221 case QQmlImportInstance::Implicit - 1:
1222 case QQmlImportInstance::Lowest: {
1224 error.setDescription(
1225 QString::fromLatin1(
"Too many dependent imports for %1 %2.%3")
1226 .arg(currentImport->uri)
1227 .arg(currentImport->version.majorVersion())
1228 .arg(currentImport->version.minorVersion()));
1229 errors->append(error);
1236 if (!loadDependentImports(
1237 implicitImports, currentImport->qualifier, currentImport->version,
1238 currentImport->searchVersion, currentImport->precedence + 1, flags, errors)) {
1240 error.setDescription(
1241 QString::fromLatin1(
1242 "Failed to load dependencies for module \"%1\" version %2.%3")
1243 .arg(currentImport->uri)
1244 .arg(currentImport->version.majorVersion())
1245 .arg(currentImport->version.minorVersion()));
1246 errors->append(error);
1255 if (!QQmlTypeLoaderConfiguredDataConstPtr(m_data)->initialized)
1256 initializeConfiguredData(QQmlTypeLoaderConfiguredDataPtr(m_data), m_data->engine());
1258 return QQmlTypeLoaderConfiguredDataConstPtr(m_data);
1261QV4::Moth::DebugInstructionPass QQmlTypeLoader::debugInstructionPass()
1263 return configuredData(&m_data)->debugInstructionPass;
1266bool QQmlTypeLoader::readCacheFile()
1268 return configuredData(&m_data)->diskCacheOptions & QV4::ExecutionEngine::DiskCache::QmlcRead;
1271bool QQmlTypeLoader::writeCacheFile()
1273 return configuredData(&m_data)->diskCacheOptions & QV4::ExecutionEngine::DiskCache::QmlcWrite;
1276QQmlMetaType::CacheMode QQmlTypeLoader::aotCacheMode()
1278 const QV4::ExecutionEngine::DiskCacheOptions options
1279 = configuredData(&m_data)->diskCacheOptions;
1280 if (!(options & QV4::ExecutionEngine::DiskCache::Aot))
1281 return QQmlMetaType::RejectAll;
1282 if (options & QV4::ExecutionEngine::DiskCache::AotByteCode)
1283 return QQmlMetaType::AcceptUntyped;
1284 return QQmlMetaType::RequireFullyTyped;
1287bool QQmlTypeLoader::Blob::qmldirDataAvailable(
const QQmlRefPointer<QQmlQmldirData> &data, QList<QQmlError> *errors)
1289 assertTypeLoaderThread();
1290 return data->processImports(
this, [&](
const PendingImportPtr &import) {
1291 return updateQmldir(data, import, errors);
1297 if (QDir::listSeparator() == u':') {
1299 QStringList paths = envImportPath.split(u':');
1300 bool wasEmpty =
false;
1301 for (
auto it = paths.begin(); it != paths.end();) {
1302 if (it->isEmpty()) {
1304 it = paths.erase(it);
1315 return envImportPath.split(QDir::listSeparator(), Qt::SkipEmptyParts);
1320
1321
1322QQmlTypeLoader::QQmlTypeLoader(QV4::ExecutionEngine *engine)
1328 const bool isPluginApplication = QCoreApplication::testAttribute(Qt::AA_PluginApplication);
1330 auto addEnvPath = [
this, isPluginApplication](
const char *var,
auto addPath) {
1331 if (Q_UNLIKELY(!isPluginApplication && !qEnvironmentVariableIsEmpty(var))) {
1332 const QStringList paths = parseEnvPath(qEnvironmentVariable(var));
1333 for (
const QString &path : paths)
1334 (
this->*addPath)(path, AppendPath);
1340#if defined(Q_OS_ANDROID)
1341 addImportPath(QStringLiteral(
"qrc:/android_rcc_bundle/qml"), AppendPath);
1344 if (!isPluginApplication)
1345 addImportPath(QCoreApplication::applicationDirPath(), AppendPath);
1347 addImportPath(QStringLiteral(
"qrc:/qt-project.org/imports"), AppendPath);
1348 addImportPath(QStringLiteral(
"qrc:/qt/qml"), AppendPath);
1350 addEnvPath(
"QML2_IMPORT_PATH", &QQmlTypeLoader::addImportPath);
1351 addEnvPath(
"QML_IMPORT_PATH", &QQmlTypeLoader::addImportPath);
1353 const auto qmlImportPaths = QLibraryInfo::paths(QLibraryInfo::QmlImportsPath);
1354 for (
const auto &qmlImportPath : qmlImportPaths)
1355 addImportPath(qmlImportPath, AppendPath);
1359#if defined(Q_OS_ANDROID)
1360 addEnvPath(
"QT_BUNDLED_LIBS_PATH", &QQmlTypeLoader::addPluginPath);
1362 addEnvPath(
"QML_PLUGIN_PATH", &QQmlTypeLoader::addPluginPath);
1364 const auto pluginPaths = QLibraryInfo::paths(QLibraryInfo::PluginsPath);
1365 for (
const auto &pluginPath : pluginPaths)
1366 addPluginPath(pluginPath, AppendPath);
1371 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
1372 data->pluginPaths << QLatin1String(
".");
1376
1377
1378
1379QQmlTypeLoader::~QQmlTypeLoader()
1393template<
typename Blob>
1395 const QQmlTypeLoaderSharedDataPtr &data, QQmlRefPointer<Blob> &&blob,
1396 QQmlTypeLoader::Mode mode)
1398 if ((mode == QQmlTypeLoader::PreferSynchronous && QQmlFile::isSynchronous(blob->finalUrl()))
1399 || mode == QQmlTypeLoader::Synchronous) {
1410 QQmlTypeLoaderThread *thread = data.thread();
1411 if (thread && !thread->isThisThread()) {
1412 while (!blob->isCompleteOrError())
1413 thread->waitForNextMessage();
1420
1421
1422QQmlRefPointer<QQmlTypeData> QQmlTypeLoader::getType(
const QUrl &unNormalizedUrl, Mode mode)
1426 Q_ASSERT(!unNormalizedUrl.isRelative() &&
1427 (QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl).isEmpty() ||
1428 !QDir::isRelativePath(QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl))));
1430 QQmlRefPointer<QQmlTypeData> typeData;
1432 const QUrl url = QQmlMetaType::normalizedUrl(unNormalizedUrl);
1433 QQmlTypeLoaderSharedDataPtr data(&m_data);
1435 typeData = data->typeCache.value(url);
1437 return handleExisting(data, std::move(typeData), mode);
1440 if (data->typeCache.size() >= data->typeCacheTrimThreshold)
1443 typeData = QQml::makeRefPointer<QQmlTypeData>(url,
this);
1446 data->typeCache.insert(url, typeData);
1449 return finalizeBlob(std::move(typeData), mode);
1453
1454
1455
1456QQmlRefPointer<QQmlTypeData> QQmlTypeLoader::getType(
1457 const QByteArray &data,
const QUrl &url, Mode mode)
1461 QQmlRefPointer<QQmlTypeData> typeData = QQml::makeRefPointer<QQmlTypeData>(url,
this);
1462 QQmlTypeLoader::loadWithStaticData(QQmlDataBlob::Ptr(typeData.data()), data, mode);
1468 return url.fragment() == QLatin1String(
"module") || url.path().endsWith(QLatin1String(
".mjs"));
1471QQmlRefPointer<QQmlScriptBlob> QQmlTypeLoader::getScript(
const QUrl &unNormalizedUrl, Mode mode)
1475 Q_ASSERT(!unNormalizedUrl.isRelative() &&
1476 (QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl).isEmpty() ||
1477 !QDir::isRelativePath(QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl))));
1479 QQmlRefPointer<QQmlScriptBlob> scriptBlob;
1481 const QUrl url = QQmlMetaType::normalizedUrl(unNormalizedUrl);
1482 QQmlTypeLoaderSharedDataPtr data(&m_data);
1484 scriptBlob = data->scriptCache.value(url);
1486 return handleExisting(data, std::move(scriptBlob), mode);
1488 scriptBlob = QQml::makeRefPointer<QQmlScriptBlob>(url,
this, isModuleUrl(url)
1489 ? QQmlScriptBlob::IsESModule::Yes
1490 : QQmlScriptBlob::IsESModule::No);
1491 data->scriptCache.insert(url, scriptBlob);
1494 return finalizeBlob(std::move(scriptBlob), mode);
1497QQmlRefPointer<QV4::CompiledData::CompilationUnit> QQmlTypeLoader::injectModule(
1498 const QUrl &relativeUrl,
const QV4::CompiledData::Unit *unit)
1502 QQmlRefPointer<QQmlScriptBlob> blob = QQml::makeRefPointer<QQmlScriptBlob>(
1503 relativeUrl,
this, QQmlScriptBlob::IsESModule::Yes);
1504 QQmlPrivate::CachedQmlUnit cached { unit,
nullptr,
nullptr};
1507 QQmlTypeLoaderSharedDataPtr data(&m_data);
1508 data->scriptCache.insert(relativeUrl, blob);
1511 loadWithCachedUnit(blob.data(), &cached, Synchronous);
1512 Q_ASSERT(blob->isComplete());
1513 return blob->scriptData()->compilationUnit();
1517
1518
1519
1520QQmlRefPointer<QQmlScriptBlob> QQmlTypeLoader::getScript(
1521 const QUrl &unNormalizedUrl,
const QUrl &relativeUrl)
1525 Q_ASSERT(!unNormalizedUrl.isRelative() &&
1526 (QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl).isEmpty() ||
1527 !QDir::isRelativePath(QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl))));
1529 const QUrl url = QQmlMetaType::normalizedUrl(unNormalizedUrl);
1531 QQmlRefPointer<QQmlScriptBlob> scriptBlob;
1533 QQmlTypeLoaderSharedDataPtr data(&m_data);
1534 scriptBlob = data->scriptCache.value(url);
1538 if (!scriptBlob && unNormalizedUrl != relativeUrl)
1539 scriptBlob = data->scriptCache.value(relativeUrl);
1545 scriptBlob = QQml::makeRefPointer<QQmlScriptBlob>(url,
this, isModuleUrl(url)
1546 ? QQmlScriptBlob::IsESModule::Yes
1547 : QQmlScriptBlob::IsESModule::No);
1548 data->scriptCache.insert(url, scriptBlob);
1551 return finalizeBlob(std::move(scriptBlob), PreferSynchronous);
1555
1556
1557QQmlRefPointer<QQmlQmldirData> QQmlTypeLoader::getQmldir(
const QUrl &url)
1561 Q_ASSERT(!url.isRelative() &&
1562 (QQmlFile::urlToLocalFileOrQrc(url).isEmpty() ||
1563 !QDir::isRelativePath(QQmlFile::urlToLocalFileOrQrc(url))));
1565 QQmlRefPointer<QQmlQmldirData> qmldirData;
1567 QQmlTypeLoaderSharedDataPtr data(&m_data);
1568 qmldirData = data->qmldirCache.value(url);
1572 qmldirData = QQml::makeRefPointer<QQmlQmldirData>(url,
this);
1573 data->qmldirCache.insert(url, qmldirData);
1576 QQmlTypeLoader::load(QQmlDataBlob::Ptr(qmldirData.data()));
1580bool QQmlTypeLoader::isResource(
const QString &path)
1582 const bool startsWithColon = path.at(0) == QLatin1Char(
':');
1583#if defined(Q_OS_ANDROID)
1584 return startsWithColon || path.startsWith(QLatin1String(
"assets:/"), Qt::CaseInsensitive)
1585 || path.startsWith(QLatin1String(
"content:/"), Qt::CaseInsensitive);
1587 return startsWithColon;
1592
1593
1594
1595
1596
1597
1598
1599
1600QString QQmlTypeLoader::cleanFilePath(
const QString &path)
const
1607 const QString cleanPath = QDir::cleanPath(path);
1608 if (isResource(cleanPath)) {
1610 QFileInfo fileInfo(cleanPath);
1611 return fileInfo.isFile() ? cleanPath : QString();
1614 return fileExists(cleanPath)
1621 for (qsizetype length = path.size(); length > 0; --length) {
1622 if (path[length - 1] != QLatin1Char(
'/'))
1623 return path.left(length);
1629bool QQmlTypeLoader::fileExists(
const QString &dirPath,
const QString &file)
const
1639 const QChar nullChar(QChar::Null);
1640 if (dirPath.isEmpty() || dirPath.contains(nullChar) || file.isEmpty()
1641 || file.contains(nullChar)) {
1648 if (isResource(dirPath)) {
1651 const QFileInfo info(dirPath + u'/' + file);
1652 return info.exists();
1655 const QString path = stripTrailingSlashes(dirPath);
1657 QQmlTypeLoaderSharedDataConstPtr data(&m_data);
1658 return data->importDirCache.fileExists(path, file);
1663
1664
1665
1666bool QQmlTypeLoader::directoryExists(
const QString &path)
const
1673 if (isResource(path)) {
1675 QFileInfo fileInfo(path);
1676 return fileInfo.exists() && fileInfo.isDir();
1679 const QString dirPath = stripTrailingSlashes(path);
1681 QQmlTypeLoaderSharedDataConstPtr data(&m_data);
1682 return data->importDirCache.directoryExists(path);
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697const QQmlTypeLoaderQmldirContent QQmlTypeLoader::qmldirContent(
const QString &filePathIn)
1709 QUrl url(filePathIn);
1711 QQmlTypeLoaderThreadDataPtr data(&m_data);
1713 if (url.scheme().size() < 2) {
1714 filePath = filePathIn;
1716 filePath = QQmlFile::urlToLocalFileOrQrc(url);
1717 if (filePath.isEmpty()) {
1718 if (
auto entry = data->importQmlDirCache.value(filePathIn))
1721 return QQmlTypeLoaderQmldirContent();
1725 QQmlTypeLoaderQmldirContent **val = data->importQmlDirCache.value(filePath);
1728 QQmlTypeLoaderQmldirContent *qmldir =
new QQmlTypeLoaderQmldirContent;
1730#define ERROR(description) { QQmlError e; e.setDescription(description); qmldir->setError(e); }
1731#define NOT_READABLE_ERROR QString(QLatin1String("module \"$$URI$$\" definition \"%1\" not readable"))
1732#define NOT_FOUND_ERROR QString(QLatin1String("cannot load module \"$$URI$$\": File \"%1\" not found"))
1734 QFile file(filePath);
1735 if (!fileExists(filePath)) {
1737 }
else if (file.open(QFile::ReadOnly)) {
1738 QByteArray data = file.readAll();
1739 qmldir->setContent(filePath, QString::fromUtf8(data));
1745#undef NOT_READABLE_ERROR
1746#undef NOT_FOUND_ERROR
1748 data->importQmlDirCache.insert(filePath, qmldir);
1752void QQmlTypeLoader::setQmldirContent(
const QString &url,
const QString &content)
1756 QQmlTypeLoaderThreadDataPtr data(&m_data);
1757 QQmlTypeLoaderQmldirContent *qmldir;
1758 QQmlTypeLoaderQmldirContent **val = data->importQmlDirCache.value(url);
1762 qmldir =
new QQmlTypeLoaderQmldirContent;
1763 data->importQmlDirCache.insert(url, qmldir);
1766 if (!qmldir->hasContent())
1767 qmldir->setContent(url, content);
1770template<
typename Blob>
1773 std::for_each(blobs->cbegin(), blobs->cend(), [](
const QQmlRefPointer<Blob> &blob) {
1774 blob->resetTypeLoader();
1780
1781
1782
1783void QQmlTypeLoader::clearCache()
1794 QQmlTypeLoaderThreadDataPtr threadData(&m_data);
1795 qDeleteAll(threadData->importQmlDirCache);
1796 threadData->checksumCache.clear();
1797 threadData->importQmlDirCache.clear();
1799 QQmlTypeLoaderSharedDataPtr data(&m_data);
1800 clearBlobs(&data->typeCache);
1801 clearBlobs(&data->scriptCache);
1802 clearBlobs(&data->qmldirCache);
1803 data->typeCacheTrimThreshold = QQmlTypeLoaderSharedData::MinimumTypeCacheTrimThreshold;
1804 data->importDirCache.clear();
1810
1811
1812
1813
1814
1815bool QQmlTypeLoader::removeFromCache(
const QUrl &url)
1817 const QUrl normalized = QQmlMetaType::normalizedUrl(url);
1818 const QQmlTypeLoaderSharedDataPtr data(&m_data);
1819 return data->typeCache.remove(normalized) || data->scriptCache.remove(normalized)
1820 || data->qmldirCache.remove(normalized);
1823void QQmlTypeLoader::trimCache()
1825 const QQmlTypeLoaderSharedDataPtr data(&m_data);
1829void QQmlTypeLoader::updateTypeCacheTrimThreshold(
const QQmlTypeLoaderSharedDataPtr &data)
1833 int size = data->typeCache.size();
1834 if (size > data->typeCacheTrimThreshold)
1835 data->typeCacheTrimThreshold = size * 2;
1836 if (size < data->typeCacheTrimThreshold / 2) {
1837 data->typeCacheTrimThreshold
1838 = qMax(size * 2,
int(QQmlTypeLoaderSharedData::MinimumTypeCacheTrimThreshold));
1842void QQmlTypeLoader::trimCache(
const QQmlTypeLoaderSharedDataPtr &data)
1849 bool deletedOneType =
false;
1850 for (
auto iter = data->typeCache.begin(), end = data->typeCache.end(); iter != end;) {
1851 const QQmlRefPointer<QQmlTypeData> &typeData = iter.value();
1856 if (typeData->count() != 1 || !typeData->isCompleteOrError()) {
1865 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit
1866 = typeData->m_compiledData;
1867 if (compilationUnit) {
1868 if (compilationUnit->count()
1869 > QQmlMetaType::countInternalCompositeTypeSelfReferences(
1870 compilationUnit) + 1) {
1875 QQmlMetaType::unregisterInternalCompositeType(compilationUnit);
1876 Q_ASSERT(compilationUnit->count() == 1);
1880 iter = data->typeCache.erase(iter);
1881 deletedOneType =
true;
1884 if (!deletedOneType)
1890 updateTypeCacheTrimThreshold(data);
1892 QQmlMetaType::freeUnusedTypesAndCaches();
1895bool QQmlTypeLoader::isTypeLoaded(
const QUrl &url)
const
1897 const QQmlTypeLoaderSharedDataConstPtr data(&m_data);
1898 return data->typeCache.contains(url);
1901bool QQmlTypeLoader::isScriptLoaded(
const QUrl &url)
const
1903 const QQmlTypeLoaderSharedDataConstPtr data(&m_data);
1904 return data->scriptCache.contains(url);
1909 const auto dir = path.left(path.lastIndexOf(u"/") + 1);
1910 if (dir.at(0) == u':')
1911 return QStringLiteral(
"qrc") + dir;
1913 return QUrl::fromLocalFile(dir).toString();
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929QQmlTypeLoader::LocalQmldirResult QQmlTypeLoader::locateLocalQmldir(
1930 QQmlTypeLoader::Blob *blob,
const QQmlTypeLoader::Blob::PendingImportPtr &import,
1931 QList<QQmlError> *errors)
1935 LocalQmldirResult result = QmldirNotFound;
1936 QQmlTypeLoaderThreadData::QmldirInfo *cacheTail =
nullptr;
1938 QQmlTypeLoaderThreadDataPtr threadData(&m_data);
1939 QQmlTypeLoaderThreadData::QmldirInfo **cachePtr = threadData->qmldirInfo.value(import->uri);
1940 QQmlTypeLoaderThreadData::QmldirInfo *cacheHead = cachePtr ? *cachePtr :
nullptr;
1942 cacheTail = cacheHead;
1944 if (cacheTail->version == import->searchVersion) {
1945 if (cacheTail->qmldirFilePath.isEmpty()) {
1946 return cacheTail->qmldirPathUrl.isEmpty()
1948 : QmldirInterceptedToRemote;
1950 if (blob->handleLocalQmldirForImport(
1951 import, cacheTail->qmldirFilePath, cacheTail->qmldirPathUrl, errors)) {
1954 result = QmldirRejected;
1956 }
while (cacheTail->next && (cacheTail = cacheTail->next));
1962 if (result != QmldirNotFound
1963 || QQmlMetaType::isStronglyLockedModule(import->uri, import->version)) {
1967 QQmlTypeLoaderConfiguredDataConstPtr configuredData(&m_data);
1968 const bool hasInterceptors = !configuredData->urlInterceptors.isEmpty();
1971 QStringList localImportPaths = importPathList(hasInterceptors ? LocalOrRemote : Local);
1974 const QStringList qmlDirPaths = QQmlImports::completeQmldirPaths(
1975 import->uri, localImportPaths, import->searchVersion);
1977 QString qmldirCleanFilePath;
1978 for (QString qmldirPath : qmlDirPaths) {
1979 if (hasInterceptors) {
1990 const QUrl intercepted = doInterceptUrl(
1991 QQmlImports::urlFromLocalFileOrQrcOrUrl(qmldirPath),
1992 QQmlAbstractUrlInterceptor::QmldirFile,
1993 configuredData->urlInterceptors);
1994 qmldirPath = QQmlFile::urlToLocalFileOrQrc(intercepted);
1995 if (result != QmldirInterceptedToRemote
1996 && qmldirPath.isEmpty()
1997 && !QQmlFile::isLocalFile(intercepted)) {
1998 result = QmldirInterceptedToRemote;
2005 qmldirCleanFilePath = cleanFilePath(localPathForUrlOrPath(qmldirPath));
2007 if (!qmldirCleanFilePath.isEmpty()) {
2008 QString url = pathToUrl(qmldirCleanFilePath);
2009 if (url.startsWith(QStringLiteral(
"file:")))
2010 sanitizeUNCPath(&qmldirCleanFilePath);
2012 QQmlTypeLoaderThreadData::QmldirInfo *cache =
new QQmlTypeLoaderThreadData::QmldirInfo;
2013 cache->version = import->searchVersion;
2014 cache->qmldirFilePath = qmldirCleanFilePath;
2015 cache->qmldirPathUrl = url;
2016 cache->next =
nullptr;
2018 cacheTail->next = cache;
2020 threadData->qmldirInfo.insert(import->uri, cache);
2023 if (result != QmldirFound) {
2024 result = blob->handleLocalQmldirForImport(
2025 import, qmldirCleanFilePath, url, errors)
2035 if (result == QmldirNotFound || result == QmldirInterceptedToRemote) {
2036 QQmlTypeLoaderThreadData::QmldirInfo *cache =
new QQmlTypeLoaderThreadData::QmldirInfo;
2037 cache->version = import->searchVersion;
2038 cache->next = cacheHead;
2039 if (result == QmldirInterceptedToRemote) {
2042 cache->qmldirPathUrl = QStringLiteral(
"intercepted");
2044 threadData->qmldirInfo.insert(import->uri, cache);
2046 if (result == QmldirNotFound) {
2047 qCDebug(lcQmlImport)
2048 <<
"locateLocalQmldir:" << qPrintable(import->uri)
2049 <<
"module's qmldir file not found";
2052 qCDebug(lcQmlImport)
2053 <<
"locateLocalQmldir:" << qPrintable(import->uri) <<
"module's qmldir found at"
2054 << 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