4#include <private/qqmltypeloader_p.h>
6#include <private/qqmldirdata_p.h>
7#include <private/qqmlprofiler_p.h>
8#include <private/qqmlscriptblob_p.h>
9#include <private/qqmlscriptdata_p.h>
10#include <private/qqmlsourcecoordinate_p.h>
11#include <private/qqmltypedata_p.h>
12#include <private/qqmltypeloaderqmldircontent_p.h>
13#include <private/qqmltypeloaderthread_p.h>
14#include <private/qv4compiler_p.h>
15#include <private/qv4compilercontext_p.h>
16#include <private/qv4runtimecodegen_p.h>
18#include <QtQml/qqmlabstracturlinterceptor.h>
19#include <QtQml/qqmlengine.h>
20#include <QtQml/qqmlextensioninterface.h>
21#include <QtQml/qqmlfile.h>
22#include <QtQml/qqmlnetworkaccessmanagerfactory.h>
24#include <qtqml_tracepoints_p.h>
26#include <QtCore/qdir.h>
27#include <QtCore/qdirlisting.h>
28#include <QtCore/qfile.h>
29#include <QtCore/qlibraryinfo.h>
30#include <QtCore/qthread.h>
33#include "private/qcore_mac_p.h"
38#define ASSERT_LOADTHREAD()
39 Q_ASSERT(thread() && thread()->isThisThread())
40#define ASSERT_ENGINETHREAD()
41 Q_ASSERT(!engine()->jsEngine() || engine()->jsEngine()->thread()->isCurrentThread())
45Q_TRACE_POINT(qtqml, QQmlCompiling_entry,
const QUrl &url)
46Q_TRACE_POINT(qtqml, QQmlCompiling_exit)
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
74
75
76
78void QQmlTypeLoader::invalidate()
84#if QT_CONFIG(qml_network)
88 QQmlTypeLoaderThreadDataPtr data(&m_data);
89 data->networkReplies.clear();
93void QQmlTypeLoader::addUrlInterceptor(QQmlAbstractUrlInterceptor *urlInterceptor)
96 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
97 data->urlInterceptors.append(urlInterceptor);
100void QQmlTypeLoader::removeUrlInterceptor(QQmlAbstractUrlInterceptor *urlInterceptor)
103 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
104 data->urlInterceptors.removeOne(urlInterceptor);
107QList<QQmlAbstractUrlInterceptor *> QQmlTypeLoader::urlInterceptors()
const
110 QQmlTypeLoaderConfiguredDataConstPtr data(&m_data);
111 return data->urlInterceptors;
114static QUrl doInterceptUrl(
115 const QUrl &url, QQmlAbstractUrlInterceptor::DataType type,
116 const QList<QQmlAbstractUrlInterceptor *> &urlInterceptors)
119 for (QQmlAbstractUrlInterceptor *interceptor : urlInterceptors)
120 result = interceptor->intercept(result, type);
124QUrl QQmlTypeLoader::interceptUrl(
const QUrl &url, QQmlAbstractUrlInterceptor::DataType type)
const
128 QQmlTypeLoaderConfiguredDataConstPtr data(&m_data);
129 return doInterceptUrl(url, type, data->urlInterceptors);
132bool QQmlTypeLoader::hasUrlInterceptors()
const
135 QQmlTypeLoaderConfiguredDataConstPtr data(&m_data);
136 return !data->urlInterceptors.isEmpty();
139#if QT_CONFIG(qml_debug)
140void QQmlTypeLoader::setProfiler(QQmlProfiler *profiler)
142 ASSERT_ENGINETHREAD();
144 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
145 Q_ASSERT(!data->profiler);
146 data->profiler.reset(profiler);
151 void loadThread(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
153 loader->loadThread(blob);
155 void load(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
157 loader->ensureThread()->load(blob);
159 void loadAsync(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
161 loader->ensureThread()->loadAsync(blob);
169 void loadThread(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
171 loader->loadWithStaticDataThread(blob, data);
173 void load(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
175 loader->ensureThread()->loadWithStaticData(blob, data);
177 void loadAsync(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
179 loader->ensureThread()->loadWithStaticDataAsync(blob, data);
187 void loadThread(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
189 loader->loadWithCachedUnitThread(blob, unit);
191 void load(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
193 loader->ensureThread()->loadWithCachedUnit(blob, unit);
195 void loadAsync(QQmlTypeLoader *loader,
const QQmlDataBlob::Ptr &blob)
const
197 loader->ensureThread()->loadWithCachedUnitAsync(blob, unit);
201template<
typename Loader>
202void QQmlTypeLoader::doLoad(
const Loader &loader,
const QQmlDataBlob::Ptr &blob, Mode mode)
206 qWarning(
"QQmlTypeLoader::doLoad(%s): %s thread", qPrintable(blob->urlString()),
207 (m_thread && m_thread->isThisThread()) ?
"Compile" :
"Engine");
209 blob->startLoading();
211 if (QQmlTypeLoaderThread *t = thread(); t && t->isThisThread()) {
212 loader.loadThread(
this, blob);
216 if (mode == Asynchronous) {
217 blob->setIsAsync(
true);
218 loader.loadAsync(
this, blob);
222 loader.load(
this, blob);
223 if (blob->isCompleteOrError())
226 if (mode == PreferSynchronous) {
227 blob->setIsAsync(
true);
231 Q_ASSERT(mode == Synchronous);
234 QQmlTypeLoaderSharedDataConstPtr lock(&m_data);
236 m_data.thread()->waitForNextMessage();
237 }
while (!blob->isCompleteOrError());
241
242
243
244
245void QQmlTypeLoader::load(
const QQmlDataBlob::Ptr &blob, Mode mode)
248 doLoad(PlainLoader(), blob, mode);
252
253
254
255
256void QQmlTypeLoader::loadWithStaticData(
257 const QQmlDataBlob::Ptr &blob,
const QByteArray &data, Mode mode)
260 doLoad(StaticLoader(data), blob, mode);
263void QQmlTypeLoader::loadWithCachedUnit(
264 const QQmlDataBlob::Ptr &blob,
const QQmlPrivate::CachedQmlUnit *unit, Mode mode)
267 doLoad(CachedLoader(unit), blob, mode);
270void QQmlTypeLoader::drop(
const QQmlDataBlob::Ptr &blob)
278 if (QQmlTypeLoaderThread *t = thread(); t && !t->isThisThread())
282void QQmlTypeLoader::loadWithStaticDataThread(
const QQmlDataBlob::Ptr &blob,
const QByteArray &data)
286 setData(blob, data, DataOrigin::Static);
289void QQmlTypeLoader::loadWithCachedUnitThread(
const QQmlDataBlob::Ptr &blob,
const QQmlPrivate::CachedQmlUnit *unit)
293 setCachedUnit(blob, unit);
296void QQmlTypeLoader::loadThread(
const QQmlDataBlob::Ptr &blob)
300 if (blob->m_url.isEmpty()) {
302 error.setDescription(QLatin1String(
"Invalid null URL"));
303 blob->setError(error);
307 if (QQmlFile::isSynchronous(blob->m_url)) {
308 const QString fileName = QQmlFile::urlToLocalFileOrQrc(blob->m_url);
309 if (!fileExists(fileName) && QFileInfo::exists(fileName)) {
312 blob->setError(QLatin1String(
"File name case mismatch"));
316 if (blob->setProgress(1.f) && blob->isAsync())
317 thread()->callDownloadProgressChanged(blob, 1.);
319 setData(blob, fileName);
322#if QT_CONFIG(qml_network)
323 QNetworkReply *reply = thread()->networkAccessManager()->get(QNetworkRequest(blob->m_url));
324 QQmlTypeLoaderNetworkReplyProxy *nrp = thread()->networkReplyProxy();
326 QQmlTypeLoaderThreadDataPtr data(&m_data);
327 data->networkReplies.insert(reply, blob);
329 if (reply->isFinished()) {
330 nrp->manualFinished(reply);
332 QObject::connect(reply, &QNetworkReply::downloadProgress,
333 nrp, &QQmlTypeLoaderNetworkReplyProxy::downloadProgress);
334 QObject::connect(reply, &QNetworkReply::finished,
335 nrp, &QQmlTypeLoaderNetworkReplyProxy::finished);
339 qWarning(
"QQmlDataBlob: requested %s", qPrintable(blob->urlString()));
345#define DATALOADER_MAXIMUM_REDIRECT_RECURSION 16
347#if QT_CONFIG(qml_network)
348void QQmlTypeLoader::networkReplyFinished(QNetworkReply *reply)
352 reply->deleteLater();
354 QQmlTypeLoaderThreadDataPtr data(&m_data);
355 QQmlRefPointer<QQmlDataBlob> blob = data->networkReplies.take(reply);
359 blob->m_redirectCount++;
361 if (blob->m_redirectCount < DATALOADER_MAXIMUM_REDIRECT_RECURSION) {
362 QVariant redirect = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
363 if (redirect.isValid()) {
364 QUrl url = reply->url().resolved(redirect.toUrl());
365 blob->m_finalUrl = url;
366 blob->m_finalUrlString.clear();
368 QNetworkReply *reply = thread()->networkAccessManager()->get(QNetworkRequest(url));
369 QObject *nrp = thread()->networkReplyProxy();
370 QObject::connect(reply, SIGNAL(finished()), nrp, SLOT(finished()));
371 data->networkReplies.insert(reply, std::move(blob));
373 qWarning(
"QQmlDataBlob: redirected to %s", qPrintable(blob->finalUrlString()));
379 if (reply->error()) {
380 blob->networkError(reply->error());
382 QByteArray data = reply->readAll();
383 setData(blob, data, DataOrigin::Device);
387void QQmlTypeLoader::networkReplyProgress(QNetworkReply *reply,
388 qint64 bytesReceived, qint64 bytesTotal)
392 QQmlTypeLoaderThreadDataConstPtr data(&m_data);
393 const QQmlRefPointer<QQmlDataBlob> blob = data->networkReplies.value(reply);
397 if (bytesTotal != 0) {
398 const qreal progress = (qreal(bytesReceived) / qreal(bytesTotal));
399 if (blob->setProgress(progress) && blob->isAsync())
400 thread()->callDownloadProgressChanged(blob, blob->progress());
406
407
408
409template<
class Interface>
411 Interface *iface, QQmlTypeLoaderThread *thread, QQmlTypeLoaderLockedData *data,
417 if (thread && thread->isThisThread())
418 thread->initializeEngine(iface, uri);
420 iface->initializeEngine(data->engine()->qmlEngine(), uri);
423void QQmlTypeLoader::initializeEngine(QQmlEngineExtensionInterface *iface,
const char *uri)
426 doInitializeEngine(iface, thread(), &m_data, uri);
429void QQmlTypeLoader::initializeEngine(QQmlExtensionInterface *iface,
const char *uri)
432 doInitializeEngine(iface, thread(), &m_data, uri);
436
437
438
439
440
441
442
443
444void QQmlTypeLoader::setData(
const QQmlDataBlob::Ptr &blob,
const QByteArray &data, DataOrigin origin)
448 QQmlDataBlob::SourceCodeData d;
449 d.inlineSourceCode = QString::fromUtf8(data);
450 d.hasInlineSourceCode =
true;
451 d.hasStaticData = (origin == DataOrigin::Static);
455void QQmlTypeLoader::setData(
const QQmlDataBlob::Ptr &blob,
const QString &fileName)
459 QQmlDataBlob::SourceCodeData d;
460 d.fileInfo = QFileInfo(fileName);
464void QQmlTypeLoader::setData(
const QQmlDataBlob::Ptr &blob,
const QQmlDataBlob::SourceCodeData &d)
468 Q_TRACE_SCOPE(QQmlCompiling, blob->url());
469 QQmlCompilingProfiler prof(profiler(), blob.data());
471 blob->m_inCallback =
true;
473 blob->dataReceived(d);
475 if (!blob->isError() && !blob->isWaiting())
476 blob->allDependenciesDone();
478 blob->m_inCallback =
false;
483void QQmlTypeLoader::setCachedUnit(
const QQmlDataBlob::Ptr &blob,
const QQmlPrivate::CachedQmlUnit *unit)
487 Q_TRACE_SCOPE(QQmlCompiling, blob->url());
488 QQmlCompilingProfiler prof(profiler(), blob.data());
490 blob->m_inCallback =
true;
492 blob->initializeFromCachedUnit(unit);
494 if (!blob->isError() && !blob->isWaiting())
495 blob->allDependenciesDone();
497 blob->m_inCallback =
false;
504#if defined(Q_OS_UNIX)
505 return (path.at(0) == QLatin1Char(
'/'));
508 return fi.isAbsolute();
514
515
516QStringList QQmlTypeLoader::importPathList(PathType type)
const
518 QQmlTypeLoaderConfiguredDataConstPtr data(&m_data);
519 if (type == LocalOrRemote)
520 return data->importPaths;
523 for (
const QString &path : data->importPaths) {
524 bool localPath = isPathAbsolute(path) || QQmlFile::isLocalFile(path);
525 if (localPath == (type == Local))
533
534
535void QQmlTypeLoader::addImportPath(
const QString &path)
537 qCDebug(lcQmlImport) <<
"addImportPath:" << path;
542 QUrl url = QUrl(path);
545 if (url.scheme() == QLatin1String(
"file")) {
546 cPath = QQmlFile::urlToLocalFileOrQrc(url);
547 }
else if (path.startsWith(QLatin1Char(
':'))) {
550 cPath = QLatin1String(
"qrc") + path;
551 cPath.replace(QLatin1Char(
'\\'), QLatin1Char(
'/'));
552 }
else if (url.isRelative() ||
553 (url.scheme().size() == 1 && QFile::exists(path)) ) {
554 QDir dir = QDir(path);
555 cPath = dir.canonicalPath();
558 cPath.replace(QLatin1Char(
'\\'), QLatin1Char(
'/'));
561 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
562 if (!cPath.isEmpty()) {
563 if (data->importPaths.contains(cPath))
564 data->importPaths.move(data->importPaths.indexOf(cPath), 0);
566 data->importPaths.prepend(cPath);
571
572
573void QQmlTypeLoader::setImportPathList(
const QStringList &paths)
575 qCDebug(lcQmlImport) <<
"setImportPathList:" << paths;
577 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
578 data->importPaths.clear();
579 for (
auto it = paths.crbegin(); it != paths.crend(); ++it)
588
589
590void QQmlTypeLoader::setPluginPathList(
const QStringList &paths)
592 qCDebug(lcQmlImport) <<
"setPluginPathList:" << paths;
593 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
594 data->pluginPaths = paths;
598
599
600void QQmlTypeLoader::addPluginPath(
const QString& path)
602 qCDebug(lcQmlImport) <<
"addPluginPath:" << path;
604 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
606 QUrl url = QUrl(path);
607 if (url.isRelative() || url.scheme() == QLatin1String(
"file")
608 || (url.scheme().size() == 1 && QFile::exists(path)) ) {
609 QDir dir = QDir(path);
610 data->pluginPaths.prepend(dir.canonicalPath());
612 data->pluginPaths.prepend(path);
616#if QT_CONFIG(qml_network)
617QQmlNetworkAccessManagerFactoryPtrConst QQmlTypeLoader::networkAccessManagerFactory()
const
619 ASSERT_ENGINETHREAD();
620 return QQmlNetworkAccessManagerFactoryPtrConst(&m_data);
623void QQmlTypeLoader::setNetworkAccessManagerFactory(QQmlNetworkAccessManagerFactory *factory)
625 ASSERT_ENGINETHREAD();
626 QQmlNetworkAccessManagerFactoryPtr(&m_data).reset(factory);
629QNetworkAccessManager *QQmlTypeLoader::createNetworkAccessManager(QObject *parent)
const
638 if (
const auto factory = QQmlNetworkAccessManagerFactoryPtrConst(&m_data))
639 return factory->create(parent);
641 return new QNetworkAccessManager(parent);
645void QQmlTypeLoader::clearQmldirInfo()
647 QQmlTypeLoaderThreadDataPtr data(&m_data);
649 auto itr = data->qmldirInfo.constBegin();
650 while (itr != data->qmldirInfo.constEnd()) {
651 const QQmlTypeLoaderThreadData::QmldirInfo *cache = *itr;
653 const QQmlTypeLoaderThreadData::QmldirInfo *nextCache = cache->next;
660 data->qmldirInfo.clear();
664 const QQmlTypeLoaderConfiguredDataPtr &data, QV4::ExecutionEngine *engine)
666 data->diskCacheOptions = engine->diskCacheOptions();
667 data->isDebugging = engine->debugger() !=
nullptr;
668 data->initialized =
true;
671void QQmlTypeLoader::startThread()
675 if (!m_data.thread()) {
680 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
681 initializeConfiguredData(data, m_data.engine());
682 m_data.createThread(
this);
686void QQmlTypeLoader::shutdownThread()
691 m_data.deleteThread();
694QQmlTypeLoader::Blob::PendingImport::PendingImport(
695 const QQmlRefPointer<Blob> &blob,
const QV4::CompiledData::Import *import,
696 QQmlImports::ImportFlags flags)
697 : uri(blob->stringAt(import->uriIndex))
698 , qualifier(blob->stringAt(import->qualifierIndex))
699 , type(
static_cast<QV4::CompiledData::Import::ImportType>(quint32(import->type)))
700 , location(import->location)
702 , version(import->version)
706QQmlTypeLoader::Blob::Blob(
const QUrl &url, QQmlDataBlob::Type type, QQmlTypeLoader *loader)
707 : QQmlDataBlob(url, type, loader)
708 , m_importCache(
new QQmlImports(), QQmlRefPointer<QQmlImports>::Adopt)
712QQmlTypeLoader::Blob::~Blob()
716bool QQmlTypeLoader::Blob::fetchQmldir(
717 const QUrl &url,
const QQmlTypeLoader::Blob::PendingImportPtr &import,
int priority,
718 QList<QQmlError> *errors)
720 assertTypeLoaderThread();
722 QQmlRefPointer<QQmlQmldirData> data = typeLoader()->getQmldir(url);
724 data->setPriority(
this, import, priority);
726 if (data->status() == Error) {
729 }
else if (data->status() == Complete) {
731 return qmldirDataAvailable(data, errors);
735 addDependency(data.data());
740
741
742
743
744void QQmlTypeLoader::Blob::importQmldirScripts(
745 const QQmlTypeLoader::Blob::PendingImportPtr &import,
746 const QQmlTypeLoaderQmldirContent &qmldir,
const QUrl &qmldirUrl)
748 assertTypeLoaderThread();
750 const auto qmldirScripts = qmldir.scripts();
751 for (
const QQmlDirParser::Script &script : qmldirScripts) {
752 const QUrl plainUrl = QUrl(script.fileName);
753 const QUrl scriptUrl = qmldirUrl.resolved(plainUrl);
754 QQmlRefPointer<QQmlScriptBlob> blob = typeLoader()->getScript(scriptUrl, plainUrl);
757 if (blob.data() ==
this)
760 addDependency(blob.data());
761 scriptImported(blob, import->location, script.nameSpace, import->qualifier);
765template<
typename URL>
767 QQmlTypeLoader::Blob *self,
768 const QQmlTypeLoader::Blob::PendingImportPtr &import,
const QString &qmldirFilePath,
769 const URL &qmldirUrl)
771 self->assertTypeLoaderThread();
773 const QQmlTypeLoaderQmldirContent qmldir = self->typeLoader()->qmldirContent(qmldirFilePath);
774 if (!import->qualifier.isEmpty())
775 self->importQmldirScripts(import, qmldir, QUrl(qmldirUrl));
777 if (qmldir.plugins().isEmpty()) {
782 auto module = QQmlMetaType::typeModule(qmldir.typeNamespace(), import->version);
784 QQmlMetaType::qmlRegisterModuleTypes(qmldir.typeNamespace());
790 const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
793 QString reason = errors->front().description();
794 if (reason.size() > 512)
795 reason = reason.first(252) + QLatin1String(
"... ...") + reason.last(252);
796 if (import->version.hasMajorVersion()) {
797 error.setDescription(QQmlImports::tr(
798 "module \"%1\" version %2.%3 cannot be imported because:\n%4")
799 .arg(import->uri).arg(import->version.majorVersion())
800 .arg(import->version.hasMinorVersion()
801 ? QString::number(import->version.minorVersion())
802 : QLatin1String(
"x"))
805 error.setDescription(QQmlImports::tr(
"module \"%1\" cannot be imported because:\n%2")
806 .arg(import->uri, reason));
808 errors->prepend(error);
811bool QQmlTypeLoader::Blob::handleLocalQmldirForImport(
812 const PendingImportPtr &import,
const QString &qmldirFilePath,
813 const QString &qmldirUrl, QList<QQmlError> *errors)
816 const QTypeRevision actualVersion = m_importCache->addLibraryImport(
817 typeLoader(), import->uri, import->qualifier, import->version, qmldirFilePath,
818 qmldirUrl, import->flags, import->precedence, errors);
819 if (!actualVersion.isValid())
823 if (actualVersion.hasMajorVersion())
824 import->version = actualVersion;
826 if (!loadImportDependencies(import, qmldirFilePath, import->flags, errors)) {
827 addDependencyImportError(import, errors);
831 postProcessQmldir(
this, import, qmldirFilePath, qmldirUrl);
835bool QQmlTypeLoader::Blob::updateQmldir(
const QQmlRefPointer<QQmlQmldirData> &data,
const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
839 assertTypeLoaderThread();
841 QString qmldirIdentifier = data->urlString();
842 QString qmldirUrl = qmldirIdentifier.left(qmldirIdentifier.lastIndexOf(QLatin1Char(
'/')) + 1);
844 typeLoader()->setQmldirContent(qmldirIdentifier, data->content());
846 const QTypeRevision version = m_importCache->updateQmldirContent(
847 typeLoader(), import->uri, import->version, import->qualifier, qmldirIdentifier,
849 if (!version.isValid())
853 if (version.hasMajorVersion())
854 import->version = version;
856 if (!loadImportDependencies(import, qmldirIdentifier, import->flags, errors))
859 import->priority = 0;
864 postProcessQmldir(
this, import, qmldirIdentifier, qmldirUrl);
868bool QQmlTypeLoader::Blob::addScriptImport(
const QQmlTypeLoader::Blob::PendingImportPtr &import)
870 assertTypeLoaderThread();
871 const QUrl url(import->uri);
872 QQmlTypeLoader *loader = typeLoader();
873 QQmlRefPointer<QQmlScriptBlob> blob = loader->getScript(finalUrl().resolved(url), url);
874 addDependency(blob.data());
875 scriptImported(blob, import->location, import->qualifier, QString());
879bool QQmlTypeLoader::Blob::addFileImport(
const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
881 assertTypeLoaderThread();
882 QQmlImports::ImportFlags flags;
884 QUrl importUrl(import->uri);
885 QString path = importUrl.path();
886 path.append(QLatin1String(path.endsWith(QLatin1Char(
'/')) ?
"qmldir" :
"/qmldir"));
887 importUrl.setPath(path);
890 if (!finalUrl().isValid() && importUrl.isRelative()) {
892 error.setDescription(
893 QString::fromLatin1(
"Can't resolve relative qmldir URL %1 on invalid base URL")
894 .arg(importUrl.toString()));
895 errors->append(error);
899 QUrl qmldirUrl = finalUrl().resolved(importUrl);
900 if (!QQmlImports::isLocal(qmldirUrl)) {
902 flags = QQmlImports::ImportIncomplete;
905 const QTypeRevision version = m_importCache->addFileImport(
906 typeLoader(), import->uri, import->qualifier, import->version, flags,
907 import->precedence,
nullptr, errors);
908 if (!version.isValid())
912 if (version.hasMajorVersion())
913 import->version = version;
915 if (flags & QQmlImports::ImportIncomplete) {
916 if (!fetchQmldir(qmldirUrl, import, 1, errors))
919 const QString qmldirFilePath = QQmlFile::urlToLocalFileOrQrc(qmldirUrl);
920 if (!loadImportDependencies(import, qmldirFilePath, import->flags, errors))
923 postProcessQmldir(
this, import, qmldirFilePath, qmldirUrl);
929bool QQmlTypeLoader::Blob::addLibraryImport(
const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
931 assertTypeLoaderThread();
933 const LocalQmldirResult qmldirResult = typeLoader()->locateLocalQmldir(
this, import, errors);
934 switch (qmldirResult) {
937 case QmldirNotFound: {
938 if (!loadImportDependencies(import, QString(), import->flags, errors)) {
939 addDependencyImportError(import, errors);
944 case QmldirInterceptedToRemote:
955 if (qmldirResult != QmldirInterceptedToRemote && registerPendingTypes(import)) {
956 if (m_importCache->addLibraryImport(
957 typeLoader(), import->uri, import->qualifier, import->version, QString(),
958 QString(), import->flags, import->precedence, errors).isValid()) {
966 m_unresolvedImports << import;
969 const QTypeRevision version = m_importCache->addLibraryImport(
970 typeLoader(), import->uri, import->qualifier, import->version, QString(),
971 QString(), import->flags | QQmlImports::ImportIncomplete, import->precedence,
974 if (!version.isValid())
978 if (version.hasMajorVersion())
979 import->version = version;
981 const bool hasInterceptors = m_typeLoader->hasUrlInterceptors();
985 QStringList remotePathList = typeLoader()->importPathList(
986 hasInterceptors ? LocalOrRemote : Remote);
987 if (!remotePathList.isEmpty()) {
990 const QStringList qmlDirPaths = QQmlImports::completeQmldirPaths(
991 import->uri, remotePathList, import->version);
992 for (
const QString &qmldirPath : qmlDirPaths) {
993 if (hasInterceptors) {
994 QUrl url = m_typeLoader->interceptUrl(
995 QQmlImports::urlFromLocalFileOrQrcOrUrl(qmldirPath),
996 QQmlAbstractUrlInterceptor::QmldirFile);
997 if (!QQmlFile::isLocalFile(url)
998 && !fetchQmldir(url, import, ++priority, errors)) {
1001 }
else if (!fetchQmldir(QUrl(qmldirPath), import, ++priority, errors)) {
1010bool QQmlTypeLoader::Blob::registerPendingTypes(
const PendingImportPtr &import)
1012 assertTypeLoaderThread();
1017 QQmlMetaType::typeModule(import->uri, import->version)
1020 || QQmlMetaType::qmlRegisterModuleTypes(import->uri)
1024 || QQmlMetaType::latestModuleVersion(import->uri).isValid();
1027bool QQmlTypeLoader::Blob::addImport(
const QV4::CompiledData::Import *import,
1028 QQmlImports::ImportFlags flags, QList<QQmlError> *errors)
1030 assertTypeLoaderThread();
1031 return addImport(std::make_shared<PendingImport>(
this, import, flags), errors);
1034bool QQmlTypeLoader::Blob::addImport(
1035 const QQmlTypeLoader::Blob::PendingImportPtr &import, QList<QQmlError> *errors)
1037 assertTypeLoaderThread();
1041 switch (import->type)
1043 case QV4::CompiledData::Import::ImportLibrary:
1044 return addLibraryImport(import, errors);
1045 case QV4::CompiledData::Import::ImportFile:
1046 return addFileImport(import ,errors);
1047 case QV4::CompiledData::Import::ImportScript:
1048 return addScriptImport(import);
1049 case QV4::CompiledData::Import::ImportInlineComponent:
1050 Q_UNREACHABLE_RETURN(
false);
1053 Q_UNREACHABLE_RETURN(
false);
1056void QQmlTypeLoader::Blob::dependencyComplete(
const QQmlDataBlob::Ptr &blob)
1058 assertTypeLoaderThread();
1060 if (blob->type() == QQmlDataBlob::QmldirFile) {
1061 QQmlQmldirData *data =
static_cast<QQmlQmldirData *>(blob.data());
1062 QList<QQmlError> errors;
1063 if (!qmldirDataAvailable(data, &errors)) {
1064 Q_ASSERT(errors.size());
1065 QQmlError error(errors.takeFirst());
1066 error.setUrl(m_importCache->baseUrl());
1067 const QV4::CompiledData::Location importLocation = data->importLocation(
this);
1068 error.setLine(qmlConvertSourceCoordinate<quint32,
int>(importLocation.line()));
1069 error.setColumn(qmlConvertSourceCoordinate<quint32,
int>(importLocation.column()));
1070 errors.prepend(error);
1076bool QQmlTypeLoader::Blob::loadDependentImports(
1077 const QList<QQmlDirParser::Import> &imports,
const QString &qualifier,
1078 QTypeRevision version, quint8 precedence, QQmlImports::ImportFlags flags,
1079 QList<QQmlError> *errors)
1081 assertTypeLoaderThread();
1083 for (
const auto &import : imports) {
1084 if (import.flags & QQmlDirParser::Import::Optional)
1086 auto dependencyImport = std::make_shared<PendingImport>();
1087 dependencyImport->uri = import.module;
1088 dependencyImport->qualifier = qualifier;
1089 dependencyImport->version = (import.flags & QQmlDirParser::Import::Auto)
1090 ? version : import.version;
1091 dependencyImport->flags = flags;
1092 dependencyImport->precedence = precedence;
1094 qCDebug(lcQmlImport)
1095 <<
"loading dependent import" << dependencyImport->uri <<
"version"
1096 << dependencyImport->version <<
"as" << dependencyImport->qualifier;
1098 if (!addImport(dependencyImport, errors)) {
1100 error.setDescription(
1101 QString::fromLatin1(
1102 "Failed to load dependent import \"%1\" version %2.%3")
1103 .arg(dependencyImport->uri)
1104 .arg(dependencyImport->version.majorVersion())
1105 .arg(dependencyImport->version.minorVersion()));
1106 errors->append(error);
1114bool QQmlTypeLoader::Blob::loadImportDependencies(
1115 const QQmlTypeLoader::Blob::PendingImportPtr ¤tImport,
const QString &qmldirUri,
1116 QQmlImports::ImportFlags flags, QList<QQmlError> *errors)
1118 assertTypeLoaderThread();
1120 QList<QQmlDirParser::Import> implicitImports
1121 = QQmlMetaType::moduleImports(currentImport->uri, currentImport->version);
1122 if (!qmldirUri.isEmpty())
1123 implicitImports += typeLoader()->qmldirContent(qmldirUri).imports();
1126 switch (currentImport->precedence) {
1127 case QQmlImportInstance::Implicit - 1:
1128 case QQmlImportInstance::Lowest: {
1130 error.setDescription(
1131 QString::fromLatin1(
"Too many dependent imports for %1 %2.%3")
1132 .arg(currentImport->uri)
1133 .arg(currentImport->version.majorVersion())
1134 .arg(currentImport->version.minorVersion()));
1135 errors->append(error);
1142 if (!loadDependentImports(
1143 implicitImports, currentImport->qualifier, currentImport->version,
1144 currentImport->precedence + 1, flags, errors)) {
1146 error.setDescription(
1147 QString::fromLatin1(
1148 "Failed to load dependencies for module \"%1\" version %2.%3")
1149 .arg(currentImport->uri)
1150 .arg(currentImport->version.majorVersion())
1151 .arg(currentImport->version.minorVersion()));
1152 errors->append(error);
1161 if (!QQmlTypeLoaderConfiguredDataConstPtr(m_data)->initialized)
1162 initializeConfiguredData(QQmlTypeLoaderConfiguredDataPtr(m_data), m_data->engine());
1164 return QQmlTypeLoaderConfiguredDataConstPtr(m_data);
1167bool QQmlTypeLoader::isDebugging()
1169 return configuredData(&m_data)->isDebugging;
1172bool QQmlTypeLoader::readCacheFile()
1174 return configuredData(&m_data)->diskCacheOptions & QV4::ExecutionEngine::DiskCache::QmlcRead;
1177bool QQmlTypeLoader::writeCacheFile()
1179 return configuredData(&m_data)->diskCacheOptions & QV4::ExecutionEngine::DiskCache::QmlcWrite;
1182QQmlMetaType::CacheMode QQmlTypeLoader::aotCacheMode()
1184 const QV4::ExecutionEngine::DiskCacheOptions options
1185 = configuredData(&m_data)->diskCacheOptions;
1186 if (!(options & QV4::ExecutionEngine::DiskCache::Aot))
1187 return QQmlMetaType::RejectAll;
1188 if (options & QV4::ExecutionEngine::DiskCache::AotByteCode)
1189 return QQmlMetaType::AcceptUntyped;
1190 return QQmlMetaType::RequireFullyTyped;
1193bool QQmlTypeLoader::Blob::qmldirDataAvailable(
const QQmlRefPointer<QQmlQmldirData> &data, QList<QQmlError> *errors)
1195 assertTypeLoaderThread();
1196 return data->processImports(
this, [&](
const PendingImportPtr &import) {
1197 return updateQmldir(data, import, errors);
1203 if (QDir::listSeparator() == u':') {
1205 QStringList paths = envImportPath.split(u':');
1206 bool wasEmpty =
false;
1207 for (
auto it = paths.begin(); it != paths.end();) {
1208 if (it->isEmpty()) {
1210 it = paths.erase(it);
1221 return envImportPath.split(QDir::listSeparator(), Qt::SkipEmptyParts);
1226
1227
1228QQmlTypeLoader::QQmlTypeLoader(QV4::ExecutionEngine *engine)
1231 QQmlTypeLoaderConfiguredDataPtr data(&m_data);
1232 data->pluginPaths << QLatin1String(
".");
1245 const auto paths = QLibraryInfo::paths(QLibraryInfo::QmlImportsPath);
1246 for (
const auto &installImportsPath: paths)
1247 addImportPath(installImportsPath);
1249 const bool isPluginApplication = QCoreApplication::testAttribute(Qt::AA_PluginApplication);
1251 auto addEnvImportPath = [
this, isPluginApplication](
const char *var) {
1252 if (Q_UNLIKELY(!isPluginApplication && !qEnvironmentVariableIsEmpty(var))) {
1253 const QStringList paths = parseEnvPath(qEnvironmentVariable(var));
1254 for (
int ii = paths.size() - 1; ii >= 0; --ii)
1255 addImportPath(paths.at(ii));
1260 addEnvImportPath(
"QML_IMPORT_PATH");
1261 addEnvImportPath(
"QML2_IMPORT_PATH");
1263 addImportPath(QStringLiteral(
"qrc:/qt/qml"));
1264 addImportPath(QStringLiteral(
"qrc:/qt-project.org/imports"));
1266 if (!isPluginApplication)
1267 addImportPath(QCoreApplication::applicationDirPath());
1269 auto addEnvPluginPath = [
this, isPluginApplication](
const char *var) {
1270 if (Q_UNLIKELY(!isPluginApplication && !qEnvironmentVariableIsEmpty(var))) {
1271 const QStringList paths = parseEnvPath(qEnvironmentVariable(var));
1272 for (
int ii = paths.size() - 1; ii >= 0; --ii)
1273 addPluginPath(paths.at(ii));
1277 addEnvPluginPath(
"QML_PLUGIN_PATH");
1278#if defined(Q_OS_ANDROID)
1279 addImportPath(QStringLiteral(
"qrc:/android_rcc_bundle/qml"));
1280 addEnvPluginPath(
"QT_BUNDLED_LIBS_PATH");
1281#elif defined(Q_OS_MACOS)
1285 if (!isPluginApplication) {
1286 if (CFBundleRef bundleRef = CFBundleGetMainBundle()) {
1287 if (QCFType<CFURLRef> urlRef = CFBundleCopyResourceURL(
1288 bundleRef, QCFString(QLatin1String(
"qml")), 0, 0)) {
1289 if (QCFType<CFURLRef> absoluteUrlRef = CFURLCopyAbsoluteURL(urlRef)) {
1290 if (QCFString path = CFURLCopyFileSystemPath(
1291 absoluteUrlRef, kCFURLPOSIXPathStyle)) {
1292 if (QFile::exists(path)) {
1293 addImportPath(QDir(path).canonicalPath());
1304
1305
1306
1307QQmlTypeLoader::~QQmlTypeLoader()
1321template<
typename Blob>
1323 const QQmlTypeLoaderSharedDataPtr &data, QQmlRefPointer<Blob> &&blob,
1324 QQmlTypeLoader::Mode mode)
1326 if ((mode == QQmlTypeLoader::PreferSynchronous && QQmlFile::isSynchronous(blob->finalUrl()))
1327 || mode == QQmlTypeLoader::Synchronous) {
1338 QQmlTypeLoaderThread *thread = data.thread();
1339 if (thread && !thread->isThisThread()) {
1340 while (!blob->isCompleteOrError())
1341 thread->waitForNextMessage();
1348
1349
1350QQmlRefPointer<QQmlTypeData> QQmlTypeLoader::getType(
const QUrl &unNormalizedUrl, Mode mode)
1354 Q_ASSERT(!unNormalizedUrl.isRelative() &&
1355 (QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl).isEmpty() ||
1356 !QDir::isRelativePath(QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl))));
1358 QQmlRefPointer<QQmlTypeData> typeData;
1360 const QUrl url = QQmlMetaType::normalizedUrl(unNormalizedUrl);
1361 QQmlTypeLoaderSharedDataPtr data(&m_data);
1363 typeData = data->typeCache.value(url);
1365 return handleExisting(data, std::move(typeData), mode);
1368 if (data->typeCache.size() >= data->typeCacheTrimThreshold)
1371 typeData = QQml::makeRefPointer<QQmlTypeData>(url,
this);
1374 data->typeCache.insert(url, typeData);
1377 return finalizeBlob(std::move(typeData), mode);
1381
1382
1383
1384QQmlRefPointer<QQmlTypeData> QQmlTypeLoader::getType(
1385 const QByteArray &data,
const QUrl &url, Mode mode)
1389 QQmlRefPointer<QQmlTypeData> typeData = QQml::makeRefPointer<QQmlTypeData>(url,
this);
1390 QQmlTypeLoader::loadWithStaticData(QQmlDataBlob::Ptr(typeData.data()), data, mode);
1396 return url.fragment() == QLatin1String(
"module") || url.path().endsWith(QLatin1String(
".mjs"));
1399QQmlRefPointer<QQmlScriptBlob> QQmlTypeLoader::getScript(
const QUrl &unNormalizedUrl, Mode mode)
1403 Q_ASSERT(!unNormalizedUrl.isRelative() &&
1404 (QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl).isEmpty() ||
1405 !QDir::isRelativePath(QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl))));
1407 QQmlRefPointer<QQmlScriptBlob> scriptBlob;
1409 const QUrl url = QQmlMetaType::normalizedUrl(unNormalizedUrl);
1410 QQmlTypeLoaderSharedDataPtr data(&m_data);
1412 scriptBlob = data->scriptCache.value(url);
1414 return handleExisting(data, std::move(scriptBlob), mode);
1416 scriptBlob = QQml::makeRefPointer<QQmlScriptBlob>(url,
this, isModuleUrl(url)
1417 ? QQmlScriptBlob::IsESModule::Yes
1418 : QQmlScriptBlob::IsESModule::No);
1419 data->scriptCache.insert(url, scriptBlob);
1422 return finalizeBlob(std::move(scriptBlob), mode);
1425QQmlRefPointer<QV4::CompiledData::CompilationUnit> QQmlTypeLoader::injectModule(
1426 const QUrl &relativeUrl,
const QV4::CompiledData::Unit *unit)
1430 QQmlRefPointer<QQmlScriptBlob> blob = QQml::makeRefPointer<QQmlScriptBlob>(
1431 relativeUrl,
this, QQmlScriptBlob::IsESModule::Yes);
1432 QQmlPrivate::CachedQmlUnit cached { unit,
nullptr,
nullptr};
1435 QQmlTypeLoaderSharedDataPtr data(&m_data);
1436 data->scriptCache.insert(relativeUrl, blob);
1439 loadWithCachedUnit(blob.data(), &cached, Synchronous);
1440 Q_ASSERT(blob->isComplete());
1441 return blob->scriptData()->compilationUnit();
1445
1446
1447
1448QQmlRefPointer<QQmlScriptBlob> QQmlTypeLoader::getScript(
1449 const QUrl &unNormalizedUrl,
const QUrl &relativeUrl)
1453 Q_ASSERT(!unNormalizedUrl.isRelative() &&
1454 (QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl).isEmpty() ||
1455 !QDir::isRelativePath(QQmlFile::urlToLocalFileOrQrc(unNormalizedUrl))));
1457 const QUrl url = QQmlMetaType::normalizedUrl(unNormalizedUrl);
1459 QQmlRefPointer<QQmlScriptBlob> scriptBlob;
1461 QQmlTypeLoaderSharedDataPtr data(&m_data);
1462 scriptBlob = data->scriptCache.value(url);
1466 if (!scriptBlob && unNormalizedUrl != relativeUrl)
1467 scriptBlob = data->scriptCache.value(relativeUrl);
1473 scriptBlob = QQml::makeRefPointer<QQmlScriptBlob>(url,
this, isModuleUrl(url)
1474 ? QQmlScriptBlob::IsESModule::Yes
1475 : QQmlScriptBlob::IsESModule::No);
1476 data->scriptCache.insert(url, scriptBlob);
1479 return finalizeBlob(std::move(scriptBlob), PreferSynchronous);
1483
1484
1485QQmlRefPointer<QQmlQmldirData> QQmlTypeLoader::getQmldir(
const QUrl &url)
1489 Q_ASSERT(!url.isRelative() &&
1490 (QQmlFile::urlToLocalFileOrQrc(url).isEmpty() ||
1491 !QDir::isRelativePath(QQmlFile::urlToLocalFileOrQrc(url))));
1493 QQmlRefPointer<QQmlQmldirData> qmldirData;
1495 QQmlTypeLoaderSharedDataPtr data(&m_data);
1496 qmldirData = data->qmldirCache.value(url);
1500 qmldirData = QQml::makeRefPointer<QQmlQmldirData>(url,
this);
1501 data->qmldirCache.insert(url, qmldirData);
1504 QQmlTypeLoader::load(QQmlDataBlob::Ptr(qmldirData.data()));
1509
1510
1511
1512
1513
1514
1515
1516
1517QString QQmlTypeLoader::absoluteFilePath(
const QString &path)
1523 if (path.at(0) == QLatin1Char(
':')) {
1525 QFileInfo fileInfo(path);
1526 return fileInfo.isFile() ? fileInfo.absoluteFilePath() : QString();
1527 }
else if (path.size() > 3 && path.at(3) == QLatin1Char(
':') &&
1528 path.startsWith(QLatin1String(
"qrc"), Qt::CaseInsensitive)) {
1530 QFileInfo fileInfo(QQmlFile::urlToLocalFileOrQrc(path));
1531 return fileInfo.isFile() ? fileInfo.absoluteFilePath() : QString();
1533#if defined(Q_OS_ANDROID)
1534 else if (path.size() > 7 && path.at(6) == QLatin1Char(
':') && path.at(7) == QLatin1Char(
'/') &&
1535 path.startsWith(QLatin1String(
"assets"), Qt::CaseInsensitive)) {
1537 QFileInfo fileInfo(QQmlFile::urlToLocalFileOrQrc(path));
1538 return fileInfo.isFile() ? fileInfo.absoluteFilePath() : QString();
1539 }
else if (path.size() > 8 && path.at(7) == QLatin1Char(
':') && path.at(8) == QLatin1Char(
'/') &&
1540 path.startsWith(QLatin1String(
"content"), Qt::CaseInsensitive)) {
1542 QFileInfo fileInfo(QQmlFile::urlToLocalFileOrQrc(path));
1543 return fileInfo.isFile() ? fileInfo.absoluteFilePath() : QString();
1547 return fileExists(path)
1548 ? QFileInfo(path).absoluteFilePath()
1554 return QDirListing::IteratorFlag::CaseSensitive | QDirListing::IteratorFlag::IncludeHidden;
1559 QCache<QString,
bool> *fileSet,
const QString &path,
const QString &file)
1561 const QDirListing listing(path, dirListingFlags());
1563 for (
const auto &entry : listing) {
1564 const QString next = entry.fileName();
1567 fileSet->insert(next,
new bool(
true));
1568 if (fileSet->totalCost() == fileSet->maxCost())
1574 if (fileSet->totalCost() < fileSet->maxCost())
1581 for (qsizetype length = path.size(); length > 0; --length) {
1582 if (path[length - 1] != QLatin1Char(
'/'))
1583 return path.left(length);
1589bool QQmlTypeLoader::fileExists(
const QString &dirPath,
const QString &file)
1599 const QString path = stripTrailingSlashes(dirPath);
1601 const QChar nullChar(QChar::Null);
1602 if (path.isEmpty() || path.contains(nullChar) || file.isEmpty() || file.contains(nullChar))
1606 QQmlTypeLoaderSharedDataPtr data(&m_data);
1607 QCache<QString,
bool> *fileSet = data->importDirCache.object(path);
1609 if (
const bool *exists = fileSet->object(file))
1614 if (fileSet->totalCost() < fileSet->maxCost())
1617 }
else if (data->importDirCache.contains(path)) {
1622 auto addToCache = [&](
const QString &path,
const QString &file) {
1623 const QDir dir(path);
1628 fileSet = dir.exists() ?
new QCache<QString,
bool> :
nullptr;
1629 const bool inserted = data->importDirCache.insert(path, fileSet);
1634 switch (populateFileSet(fileSet, dir.path(), file)) {
1635 case FileSetPopulateResult::NotFound:
1637 case FileSetPopulateResult::Found:
1639 case FileSetPopulateResult::Overflow:
1646 Q_ASSERT(fileSet->totalCost() == fileSet->maxCost());
1650 const QDirListing singleFile(dir.path(), {file}, dirListingFlags());
1651 const bool exists = singleFile.begin() != singleFile.end();
1652 fileSet->insert(file,
new bool(exists));
1653 Q_ASSERT(fileSet->totalCost() == fileSet->maxCost());
1657 if (path.at(0) == QLatin1Char(
':')) {
1659 return addToCache(path, file);
1662 if (path.size() > 3 && path.at(3) == QLatin1Char(
':')
1663 && path.startsWith(QLatin1String(
"qrc"), Qt::CaseInsensitive)) {
1665 return addToCache(QQmlFile::urlToLocalFileOrQrc(path), file);
1668#if defined(Q_OS_ANDROID)
1669 if (path.size() > 7 && path.at(6) == QLatin1Char(
':') && path.at(7) == QLatin1Char(
'/')
1670 && path.startsWith(QLatin1String(
"assets"), Qt::CaseInsensitive)) {
1672 return addToCache(QQmlFile::urlToLocalFileOrQrc(path), file);
1675 if (path.size() > 8 && path.at(7) == QLatin1Char(
':') && path.at(8) == QLatin1Char(
'/')
1676 && path.startsWith(QLatin1String(
"content"), Qt::CaseInsensitive)) {
1678 return addToCache(QQmlFile::urlToLocalFileOrQrc(path), file);
1682 return addToCache(path, file);
1687
1688
1689
1690bool QQmlTypeLoader::directoryExists(
const QString &path)
1697 bool isResource = path.at(0) == QLatin1Char(
':');
1698#if defined(Q_OS_ANDROID)
1699 isResource = isResource || path.startsWith(QLatin1String(
"assets:/")) || path.startsWith(QLatin1String(
"content:/"));
1704 QFileInfo fileInfo(path);
1705 return fileInfo.exists() && fileInfo.isDir();
1708 const QString dirPath = stripTrailingSlashes(path);
1710 QQmlTypeLoaderSharedDataPtr data(&m_data);
1711 if (!data->importDirCache.contains(dirPath)) {
1712 if (QDir(dirPath).exists()) {
1713 QCache<QString,
bool> *files =
new QCache<QString,
bool>;
1714 populateFileSet(files, dirPath, QString());
1715 data->importDirCache.insert(dirPath, files);
1719 data->importDirCache.insert(dirPath,
nullptr);
1723 return data->importDirCache.object(dirPath) !=
nullptr;
1728
1729
1730
1731
1732
1733
1734const QQmlTypeLoaderQmldirContent QQmlTypeLoader::qmldirContent(
const QString &filePathIn)
1746 QUrl url(filePathIn);
1748 QQmlTypeLoaderThreadDataPtr data(&m_data);
1750 if (url.scheme().size() < 2) {
1751 filePath = filePathIn;
1753 filePath = QQmlFile::urlToLocalFileOrQrc(url);
1754 if (filePath.isEmpty()) {
1755 if (
auto entry = data->importQmlDirCache.value(filePathIn))
1758 return QQmlTypeLoaderQmldirContent();
1762 QQmlTypeLoaderQmldirContent **val = data->importQmlDirCache.value(filePath);
1765 QQmlTypeLoaderQmldirContent *qmldir =
new QQmlTypeLoaderQmldirContent;
1767#define ERROR(description) { QQmlError e; e.setDescription(description); qmldir->setError(e); }
1768#define NOT_READABLE_ERROR QString(QLatin1String("module \"$$URI$$\" definition \"%1\" not readable"))
1769#define NOT_FOUND_ERROR QString(QLatin1String("cannot load module \"$$URI$$\": File \"%1\" not found"))
1771 QFile file(filePath);
1772 if (!fileExists(filePath)) {
1774 }
else if (file.open(QFile::ReadOnly)) {
1775 QByteArray data = file.readAll();
1776 qmldir->setContent(filePath, QString::fromUtf8(data));
1782#undef NOT_READABLE_ERROR
1783#undef CASE_MISMATCH_ERROR
1785 data->importQmlDirCache.insert(filePath, qmldir);
1789void QQmlTypeLoader::setQmldirContent(
const QString &url,
const QString &content)
1793 QQmlTypeLoaderThreadDataPtr data(&m_data);
1794 QQmlTypeLoaderQmldirContent *qmldir;
1795 QQmlTypeLoaderQmldirContent **val = data->importQmlDirCache.value(url);
1799 qmldir =
new QQmlTypeLoaderQmldirContent;
1800 data->importQmlDirCache.insert(url, qmldir);
1803 if (!qmldir->hasContent())
1804 qmldir->setContent(url, content);
1807template<
typename Blob>
1810 std::for_each(blobs->cbegin(), blobs->cend(), [](
const QQmlRefPointer<Blob> &blob) {
1811 blob->resetTypeLoader();
1817
1818
1819
1820void QQmlTypeLoader::clearCache()
1831 QQmlTypeLoaderThreadDataPtr threadData(&m_data);
1832 qDeleteAll(threadData->importQmlDirCache);
1833 threadData->checksumCache.clear();
1834 threadData->importQmlDirCache.clear();
1836 QQmlTypeLoaderSharedDataPtr data(&m_data);
1837 clearBlobs(&data->typeCache);
1838 clearBlobs(&data->scriptCache);
1839 clearBlobs(&data->qmldirCache);
1840 data->typeCacheTrimThreshold = QQmlTypeLoaderSharedData::MinimumTypeCacheTrimThreshold;
1841 data->importDirCache.clear();
1846void QQmlTypeLoader::trimCache()
1848 const QQmlTypeLoaderSharedDataPtr data(&m_data);
1852void QQmlTypeLoader::updateTypeCacheTrimThreshold(
const QQmlTypeLoaderSharedDataPtr &data)
1856 int size = data->typeCache.size();
1857 if (size > data->typeCacheTrimThreshold)
1858 data->typeCacheTrimThreshold = size * 2;
1859 if (size < data->typeCacheTrimThreshold / 2) {
1860 data->typeCacheTrimThreshold
1861 = qMax(size * 2,
int(QQmlTypeLoaderSharedData::MinimumTypeCacheTrimThreshold));
1865void QQmlTypeLoader::trimCache(
const QQmlTypeLoaderSharedDataPtr &data)
1872 bool deletedOneType =
false;
1873 for (
auto iter = data->typeCache.begin(), end = data->typeCache.end(); iter != end;) {
1874 const QQmlRefPointer<QQmlTypeData> &typeData = iter.value();
1879 if (typeData->count() != 1 || !typeData->isCompleteOrError()) {
1888 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit
1889 = typeData->m_compiledData;
1890 if (compilationUnit) {
1891 if (compilationUnit->count()
1892 > QQmlMetaType::countInternalCompositeTypeSelfReferences(
1893 compilationUnit) + 1) {
1898 QQmlMetaType::unregisterInternalCompositeType(compilationUnit);
1899 Q_ASSERT(compilationUnit->count() == 1);
1903 iter = data->typeCache.erase(iter);
1904 deletedOneType =
true;
1907 if (!deletedOneType)
1913 updateTypeCacheTrimThreshold(data);
1915 QQmlMetaType::freeUnusedTypesAndCaches();
1918bool QQmlTypeLoader::isTypeLoaded(
const QUrl &url)
const
1920 const QQmlTypeLoaderSharedDataConstPtr data(&m_data);
1921 return data->typeCache.contains(url);
1924bool QQmlTypeLoader::isScriptLoaded(
const QUrl &url)
const
1926 const QQmlTypeLoaderSharedDataConstPtr data(&m_data);
1927 return data->scriptCache.contains(url);
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943QQmlTypeLoader::LocalQmldirResult QQmlTypeLoader::locateLocalQmldir(
1944 QQmlTypeLoader::Blob *blob,
const QQmlTypeLoader::Blob::PendingImportPtr &import,
1945 QList<QQmlError> *errors)
1949 LocalQmldirResult result = QmldirNotFound;
1950 QQmlTypeLoaderThreadData::QmldirInfo *cacheTail =
nullptr;
1952 QQmlTypeLoaderThreadDataPtr threadData(&m_data);
1953 QQmlTypeLoaderThreadData::QmldirInfo **cachePtr = threadData->qmldirInfo.value(import->uri);
1954 QQmlTypeLoaderThreadData::QmldirInfo *cacheHead = cachePtr ? *cachePtr :
nullptr;
1956 cacheTail = cacheHead;
1958 if (cacheTail->version == import->version) {
1959 if (cacheTail->qmldirFilePath.isEmpty()) {
1960 return cacheTail->qmldirPathUrl.isEmpty()
1962 : QmldirInterceptedToRemote;
1964 if (blob->handleLocalQmldirForImport(
1965 import, cacheTail->qmldirFilePath, cacheTail->qmldirPathUrl, errors)) {
1968 result = QmldirRejected;
1970 }
while (cacheTail->next && (cacheTail = cacheTail->next));
1976 if (result != QmldirNotFound
1977 || QQmlMetaType::isStronglyLockedModule(import->uri, import->version)) {
1981 QQmlTypeLoaderConfiguredDataConstPtr configuredData(&m_data);
1982 const bool hasInterceptors = !configuredData->urlInterceptors.isEmpty();
1985 QStringList localImportPaths = importPathList(hasInterceptors ? LocalOrRemote : Local);
1988 const QStringList qmlDirPaths = QQmlImports::completeQmldirPaths(
1989 import->uri, localImportPaths, import->version);
1991 QString qmldirAbsoluteFilePath;
1992 for (QString qmldirPath : qmlDirPaths) {
1993 if (hasInterceptors) {
2004 const QUrl intercepted = doInterceptUrl(
2005 QQmlImports::urlFromLocalFileOrQrcOrUrl(qmldirPath),
2006 QQmlAbstractUrlInterceptor::QmldirFile,
2007 configuredData->urlInterceptors);
2008 qmldirPath = QQmlFile::urlToLocalFileOrQrc(intercepted);
2009 if (result != QmldirInterceptedToRemote
2010 && qmldirPath.isEmpty()
2011 && !QQmlFile::isLocalFile(intercepted)) {
2012 result = QmldirInterceptedToRemote;
2016 qmldirAbsoluteFilePath = absoluteFilePath(qmldirPath);
2017 if (!qmldirAbsoluteFilePath.isEmpty()) {
2019 const QString absolutePath = qmldirAbsoluteFilePath.left(
2020 qmldirAbsoluteFilePath.lastIndexOf(u'/') + 1);
2021 if (absolutePath.at(0) == u':') {
2022 url = QStringLiteral(
"qrc") + absolutePath;
2024 url = QUrl::fromLocalFile(absolutePath).toString();
2025 sanitizeUNCPath(&qmldirAbsoluteFilePath);
2028 QQmlTypeLoaderThreadData::QmldirInfo *cache =
new QQmlTypeLoaderThreadData::QmldirInfo;
2029 cache->version = import->version;
2030 cache->qmldirFilePath = qmldirAbsoluteFilePath;
2031 cache->qmldirPathUrl = url;
2032 cache->next =
nullptr;
2034 cacheTail->next = cache;
2036 threadData->qmldirInfo.insert(import->uri, cache);
2039 if (result != QmldirFound) {
2040 result = blob->handleLocalQmldirForImport(
2041 import, qmldirAbsoluteFilePath, url, errors)
2051 if (result == QmldirNotFound || result == QmldirInterceptedToRemote) {
2052 QQmlTypeLoaderThreadData::QmldirInfo *cache =
new QQmlTypeLoaderThreadData::QmldirInfo;
2053 cache->version = import->version;
2054 cache->next = cacheHead;
2055 if (result == QmldirInterceptedToRemote) {
2058 cache->qmldirPathUrl = QStringLiteral(
"intercepted");
2060 threadData->qmldirInfo.insert(import->uri, cache);
2062 if (result == QmldirNotFound) {
2063 qCDebug(lcQmlImport)
2064 <<
"locateLocalQmldir:" << qPrintable(import->uri)
2065 <<
"module's qmldir file not found";
2068 qCDebug(lcQmlImport)
2069 <<
"locateLocalQmldir:" << qPrintable(import->uri) <<
"module's qmldir found at"
2070 << qmldirAbsoluteFilePath;
#define ASSERT_ENGINETHREAD()
#define ASSERT_LOADTHREAD()
static bool isPathAbsolute(const QString &path)
static FileSetPopulateResult populateFileSet(QCache< QString, bool > *fileSet, const QString &path, const QString &file)
static constexpr QDirListing::IteratorFlags dirListingFlags()
static void initializeConfiguredData(const QQmlTypeLoaderConfiguredDataPtr &data, QV4::ExecutionEngine *engine)
static void addDependencyImportError(const QQmlTypeLoader::Blob::PendingImportPtr &import, QList< QQmlError > *errors)
static QQmlTypeLoaderConfiguredDataConstPtr configuredData(QQmlTypeLoaderLockedData *m_data)
#define ERROR(description)
#define NOT_READABLE_ERROR
static bool isModuleUrl(const QUrl &url)
void clearBlobs(QHash< QUrl, QQmlRefPointer< Blob > > *blobs)
void doInitializeEngine(Interface *iface, QQmlTypeLoaderThread *thread, QQmlTypeLoaderLockedData *data, const char *uri)
static QStringList parseEnvPath(const QString &envImportPath)
void postProcessQmldir(QQmlTypeLoader::Blob *self, const QQmlTypeLoader::Blob::PendingImportPtr &import, const QString &qmldirFilePath, const URL &qmldirUrl)
QQmlRefPointer< Blob > handleExisting(const QQmlTypeLoaderSharedDataPtr &data, QQmlRefPointer< Blob > &&blob, QQmlTypeLoader::Mode mode)
static QString stripTrailingSlashes(const QString &path)
const QQmlPrivate::CachedQmlUnit * unit
CachedLoader(const QQmlPrivate::CachedQmlUnit *unit)
void loadThread(QQmlTypeLoader *loader, const QQmlDataBlob::Ptr &blob) const
void loadAsync(QQmlTypeLoader *loader, const QQmlDataBlob::Ptr &blob) const
void load(QQmlTypeLoader *loader, const QQmlDataBlob::Ptr &blob) const
void load(QQmlTypeLoader *loader, const QQmlDataBlob::Ptr &blob) const
StaticLoader(const QByteArray &data)
void loadThread(QQmlTypeLoader *loader, const QQmlDataBlob::Ptr &blob) const
void loadAsync(QQmlTypeLoader *loader, const QQmlDataBlob::Ptr &blob) const