diff options
author | Simon Hausmann <simon.hausmann@qt.io> | 2017-03-15 16:26:02 +0100 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@qt.io> | 2017-03-21 20:10:23 +0000 |
commit | fc0254a48c775cdfea693402d0472b73c8b940e5 (patch) | |
tree | 9056e5d9d6f04e7a1b1db72ef5043d57584bcef2 | |
parent | 1bc9e17636f5fd0a4c86cba379f0060fe894b1bb (diff) |
Simplify internal source access API
All call sites of QQmlDataBlob::Data immediately convert the data to
UTf-8 - for .qml, .js and qmldir files. We can simplify the code by
reflecting that in the API and that also opens up the possibility for
future optimizations.
This means that the bi-pointer has to go, but at the moment the Data
object is stack-allocated anyway.
Since the "Data" class always represents source code, it is now called
SourceCodeData.
Change-Id: Icd262ed1e35f9edd64945ba6c16d80f9917eae72
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
-rw-r--r-- | src/qml/qml/qqmltypeloader.cpp | 40 | ||||
-rw-r--r-- | src/qml/qml/qqmltypeloader_p.h | 27 |
2 files changed, 30 insertions, 37 deletions
diff --git a/src/qml/qml/qqmltypeloader.cpp b/src/qml/qml/qqmltypeloader.cpp index 372b05eeef..193239e680 100644 --- a/src/qml/qml/qqmltypeloader.cpp +++ b/src/qml/qml/qqmltypeloader.cpp @@ -1245,20 +1245,22 @@ void QQmlTypeLoader::initializeEngine(QQmlExtensionInterface *iface, void QQmlTypeLoader::setData(QQmlDataBlob *blob, const QByteArray &data) { QML_MEMORY_SCOPE_URL(blob->url()); - QQmlDataBlob::Data d; - d.d = &data; + QQmlDataBlob::SourceCodeData d; + d.inlineSourceCodeOrFileName = QString::fromUtf8(data); + d.sourceCodeAvailable = true; setData(blob, d); } void QQmlTypeLoader::setData(QQmlDataBlob *blob, const QString &fileName) { QML_MEMORY_SCOPE_URL(blob->url()); - QQmlDataBlob::Data d; - d.d = &fileName; + QQmlDataBlob::SourceCodeData d; + d.inlineSourceCodeOrFileName = fileName; + d.sourceCodeAvailable = false; setData(blob, d); } -void QQmlTypeLoader::setData(QQmlDataBlob *blob, const QQmlDataBlob::Data &d) +void QQmlTypeLoader::setData(QQmlDataBlob *blob, const QQmlDataBlob::SourceCodeData &d) { QML_MEMORY_SCOPE_URL(blob->url()); QQmlCompilingProfiler prof(QQmlEnginePrivate::get(engine())->profiler, blob); @@ -2342,7 +2344,7 @@ bool QQmlTypeData::loadImplicitImport() return true; } -void QQmlTypeData::dataReceived(const Data &data) +void QQmlTypeData::dataReceived(const SourceCodeData &data) { QString error; m_backupSourceCode = data.readAll(&error, &m_sourceTimeStamp); @@ -2376,12 +2378,11 @@ void QQmlTypeData::initializeFromCachedUnit(const QQmlPrivate::CachedQmlUnit *un bool QQmlTypeData::loadFromSource() { - QString code = QString::fromUtf8(m_backupSourceCode); m_document.reset(new QmlIR::Document(isDebugging())); m_document->jsModule.sourceTimeStamp = m_sourceTimeStamp; QQmlEngine *qmlEngine = typeLoader()->engine(); QmlIR::IRBuilder compiler(QV8Engine::get(qmlEngine)->illegalNames()); - if (!compiler.generateFromQml(code, finalUrlString(), m_document.data())) { + if (!compiler.generateFromQml(m_backupSourceCode, finalUrlString(), m_document.data())) { QList<QQmlError> errors; errors.reserve(compiler.errors.count()); for (const QQmlJS::DiagnosticMessage &msg : qAsConst(compiler.errors)) { @@ -2872,7 +2873,7 @@ struct EmptyCompilationUnit : public QV4::CompiledData::CompilationUnit void linkBackendToEngine(QV4::ExecutionEngine *) override {} }; -void QQmlScriptBlob::dataReceived(const Data &data) +void QQmlScriptBlob::dataReceived(const SourceCodeData &data) { QV4::ExecutionEngine *v4 = QV8Engine::getV4(m_typeLoader->engine()); @@ -2891,7 +2892,7 @@ void QQmlScriptBlob::dataReceived(const Data &data) QmlIR::Document irUnit(isDebugging()); QString error; - QString source = QString::fromUtf8(data.readAll(&error, &irUnit.jsModule.sourceTimeStamp)); + QString source = data.readAll(&error, &irUnit.jsModule.sourceTimeStamp); if (!error.isEmpty()) { setError(error); return; @@ -3055,10 +3056,10 @@ void QQmlQmldirData::setPriority(int priority) m_priority = priority; } -void QQmlQmldirData::dataReceived(const Data &data) +void QQmlQmldirData::dataReceived(const SourceCodeData &data) { QString error; - m_content = QString::fromUtf8(data.readAll(&error)); + m_content = data.readAll(&error); if (!error.isEmpty()) { setError(error); return; @@ -3070,19 +3071,18 @@ void QQmlQmldirData::initializeFromCachedUnit(const QQmlPrivate::CachedQmlUnit * Q_UNIMPLEMENTED(); } -QByteArray QQmlDataBlob::Data::readAll(QString *error, qint64 *sourceTimeStamp) const +QString QQmlDataBlob::SourceCodeData::readAll(QString *error, qint64 *sourceTimeStamp) const { - Q_ASSERT(!d.isNull()); error->clear(); - if (d.isT1()) { + if (sourceCodeAvailable) { if (sourceTimeStamp) *sourceTimeStamp = 0; - return *d.asT1(); + return inlineSourceCodeOrFileName; } - QFile f(*d.asT2()); + QFile f(inlineSourceCodeOrFileName); if (!f.open(QIODevice::ReadOnly)) { *error = f.errorString(); - return QByteArray(); + return QString(); } if (sourceTimeStamp) { QDateTime timeStamp = QFileInfo(f).lastModified(); @@ -3095,9 +3095,9 @@ QByteArray QQmlDataBlob::Data::readAll(QString *error, qint64 *sourceTimeStamp) QByteArray data(f.size(), Qt::Uninitialized); if (f.read(data.data(), data.length()) != data.length()) { *error = f.errorString(); - return QByteArray(); + return QString(); } - return data; + return QString::fromUtf8(data); } QT_END_NAMESPACE diff --git a/src/qml/qml/qqmltypeloader_p.h b/src/qml/qml/qqmltypeloader_p.h index 915b1bcc4c..c1b3548fa6 100644 --- a/src/qml/qml/qqmltypeloader_p.h +++ b/src/qml/qml/qqmltypeloader_p.h @@ -130,16 +130,14 @@ public: QList<QQmlError> errors() const; - class Data { + class SourceCodeData { public: - QByteArray readAll(QString *error, qint64 *sourceTimeStamp = 0) const; + QString readAll(QString *error, qint64 *sourceTimeStamp = 0) const; private: friend class QQmlDataBlob; friend class QQmlTypeLoader; - inline Data(); - Data(const Data &); - Data &operator=(const Data &); - QBiPointer<const QByteArray, const QString> d; + QString inlineSourceCodeOrFileName; + bool sourceCodeAvailable = false; }; protected: @@ -152,7 +150,7 @@ protected: void addDependency(QQmlDataBlob *); // Callbacks made in load thread - virtual void dataReceived(const Data &) = 0; + virtual void dataReceived(const SourceCodeData &) = 0; virtual void initializeFromCachedUnit(const QQmlPrivate::CachedQmlUnit*) = 0; virtual void done(); #if QT_CONFIG(qml_network) @@ -339,7 +337,7 @@ private: void setData(QQmlDataBlob *, const QByteArray &); void setData(QQmlDataBlob *, const QString &fileName); - void setData(QQmlDataBlob *, const QQmlDataBlob::Data &); + void setData(QQmlDataBlob *, const QQmlDataBlob::SourceCodeData &); void setCachedUnit(QQmlDataBlob *blob, const QQmlPrivate::CachedQmlUnit *unit); template<typename T> @@ -436,7 +434,7 @@ public: protected: void done() override; void completed() override; - void dataReceived(const Data &) override; + void dataReceived(const SourceCodeData &) override; void initializeFromCachedUnit(const QQmlPrivate::CachedQmlUnit *unit) override; void allDependenciesDone() override; void downloadProgressChanged(qreal) override; @@ -463,7 +461,7 @@ private: qint64 m_sourceTimeStamp = 0; - QByteArray m_backupSourceCode; // used when cache verification fails. + QString m_backupSourceCode; // used when cache verification fails. QScopedPointer<QmlIR::Document> m_document; QV4::CompiledData::TypeReferenceMap m_typeReferences; @@ -547,7 +545,7 @@ public: QQmlScriptData *scriptData() const; protected: - void dataReceived(const Data &) override; + void dataReceived(const SourceCodeData &) override; void initializeFromCachedUnit(const QQmlPrivate::CachedQmlUnit *unit) override; void done() override; @@ -578,7 +576,7 @@ public: void setPriority(int); protected: - void dataReceived(const Data &) override; + void dataReceived(const SourceCodeData &) override; void initializeFromCachedUnit(const QQmlPrivate::CachedQmlUnit*) override; private: @@ -587,11 +585,6 @@ private: int m_priority; }; -QQmlDataBlob::Data::Data() -{ -} - - QT_END_NAMESPACE |