| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | #include "qqmljsloadergenerator_p.h" |
| 5 | |
| 6 | #include <QByteArray> |
| 7 | #include <QRegularExpression> |
| 8 | #include <QString> |
| 9 | #include <QStringList> |
| 10 | #include <QTextStream> |
| 11 | #include <QVector> |
| 12 | #include <QtEndian> |
| 13 | #include <QStack> |
| 14 | #include <QFileInfo> |
| 15 | #include <QSaveFile> |
| 16 | |
| 17 | #include <algorithm> |
| 18 | |
| 19 | QT_BEGIN_NAMESPACE |
| 20 | |
| 21 | /*! |
| 22 | * \internal |
| 23 | * Mangles \a str to be a unique C++ identifier. Characters that are invalid for C++ identifiers |
| 24 | * are replaced by the pattern \c _0x<hex>_ where <hex> is the hexadecimal unicode |
| 25 | * representation of the character. As identifiers with leading underscores followed by either |
| 26 | * another underscore or a capital letter are reserved in C++, we also escape those, by escaping |
| 27 | * the first underscore, using the above method. |
| 28 | * |
| 29 | * \note |
| 30 | * Although C++11 allows for non-ascii (unicode) characters to be used in identifiers, |
| 31 | * many compilers forgot to read the spec and do not implement this. Some also do not |
| 32 | * implement C99 identifiers, because that is \e {at the implementation's discretion}. So, |
| 33 | * we are stuck with plain old boring identifiers. |
| 34 | */ |
| 35 | QString mangledIdentifier(const QString &str) |
| 36 | { |
| 37 | Q_ASSERT(!str.isEmpty()); |
| 38 | |
| 39 | QString mangled; |
| 40 | mangled.reserve(asize: str.size()); |
| 41 | |
| 42 | int i = 0; |
| 43 | if (str.startsWith(c: QLatin1Char('_')) && str.size() > 1) { |
| 44 | QChar ch = str.at(i: 1); |
| 45 | if (ch == QLatin1Char('_') |
| 46 | || (ch >= QLatin1Char('A') && ch <= QLatin1Char('Z'))) { |
| 47 | mangled += QLatin1String("_0x5f_" ); |
| 48 | ++i; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | for (int ei = str.size(); i != ei; ++i) { |
| 53 | auto c = str.at(i).unicode(); |
| 54 | if ((c >= QLatin1Char('0') && c <= QLatin1Char('9')) |
| 55 | || (c >= QLatin1Char('a') && c <= QLatin1Char('z')) |
| 56 | || (c >= QLatin1Char('A') && c <= QLatin1Char('Z')) |
| 57 | || c == QLatin1Char('_')) { |
| 58 | mangled += QChar(c); |
| 59 | } else { |
| 60 | mangled += QLatin1String("_0x" ) + QString::number(c, base: 16) + QLatin1Char('_'); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return mangled; |
| 65 | } |
| 66 | |
| 67 | QString qQmlJSSymbolNamespaceForPath(const QString &relativePath) |
| 68 | { |
| 69 | QFileInfo fi(relativePath); |
| 70 | QString symbol = fi.path(); |
| 71 | if (symbol.size() == 1 && symbol.startsWith(c: QLatin1Char('.'))) { |
| 72 | symbol.clear(); |
| 73 | } else { |
| 74 | symbol.replace(before: QLatin1Char('/'), after: QLatin1Char('_')); |
| 75 | symbol += QLatin1Char('_'); |
| 76 | } |
| 77 | symbol += fi.baseName(); |
| 78 | symbol += QLatin1Char('_'); |
| 79 | symbol += fi.completeSuffix(); |
| 80 | return mangledIdentifier(str: symbol); |
| 81 | } |
| 82 | |
| 83 | static QString qtResourceNameForFile(const QString &fileName) |
| 84 | { |
| 85 | QFileInfo fi(fileName); |
| 86 | QString name = fi.completeBaseName(); |
| 87 | if (name.isEmpty()) |
| 88 | name = fi.fileName(); |
| 89 | name.replace(re: QRegularExpression(QLatin1String("[^a-zA-Z0-9_]" )), after: QLatin1String("_" )); |
| 90 | return name; |
| 91 | } |
| 92 | |
| 93 | bool qQmlJSGenerateLoader(const QStringList &compiledFiles, const QString &outputFileName, |
| 94 | const QStringList &resourceFileMappings, QString *errorString) |
| 95 | { |
| 96 | QByteArray generatedLoaderCode; |
| 97 | |
| 98 | { |
| 99 | QTextStream stream(&generatedLoaderCode); |
| 100 | stream << "#include <QtQml/qqmlprivate.h>\n" ; |
| 101 | stream << "#include <QtCore/qdir.h>\n" ; |
| 102 | stream << "#include <QtCore/qurl.h>\n" ; |
| 103 | stream << "#include <QtCore/qhash.h>\n" ; |
| 104 | stream << "#include <QtCore/qstring.h>\n" ; |
| 105 | stream << "\n" ; |
| 106 | |
| 107 | stream << "namespace QmlCacheGeneratedCode {\n" ; |
| 108 | for (int i = 0; i < compiledFiles.size(); ++i) { |
| 109 | const QString compiledFile = compiledFiles.at(i); |
| 110 | const QString ns = qQmlJSSymbolNamespaceForPath(relativePath: compiledFile); |
| 111 | stream << "namespace " << ns << " { \n" ; |
| 112 | stream << " extern const unsigned char qmlData[];\n" ; |
| 113 | stream << " extern const QQmlPrivate::AOTCompiledFunction aotBuiltFunctions[];\n" ; |
| 114 | stream << " const QQmlPrivate::CachedQmlUnit unit = {\n" ; |
| 115 | stream << " reinterpret_cast<const QV4::CompiledData::Unit*>(&qmlData), &aotBuiltFunctions[0], nullptr\n" ; |
| 116 | stream << " };\n" ; |
| 117 | stream << "}\n" ; |
| 118 | } |
| 119 | |
| 120 | stream << "\n}\n" ; |
| 121 | stream << "namespace {\n" ; |
| 122 | |
| 123 | stream << "struct Registry {\n" ; |
| 124 | stream << " Registry();\n" ; |
| 125 | stream << " ~Registry();\n" ; |
| 126 | stream << " QHash<QString, const QQmlPrivate::CachedQmlUnit*> resourcePathToCachedUnit;\n" ; |
| 127 | stream << " static const QQmlPrivate::CachedQmlUnit *lookupCachedUnit(const QUrl &url);\n" ; |
| 128 | stream << "};\n\n" ; |
| 129 | stream << "Q_GLOBAL_STATIC(Registry, unitRegistry)\n" ; |
| 130 | stream << "\n\n" ; |
| 131 | |
| 132 | stream << "Registry::Registry() {\n" ; |
| 133 | |
| 134 | for (int i = 0; i < compiledFiles.size(); ++i) { |
| 135 | const QString qrcFile = compiledFiles.at(i); |
| 136 | const QString ns = qQmlJSSymbolNamespaceForPath(relativePath: qrcFile); |
| 137 | stream << " resourcePathToCachedUnit.insert(QStringLiteral(\"" << qrcFile << "\"), &QmlCacheGeneratedCode::" << ns << "::unit);\n" ; |
| 138 | } |
| 139 | |
| 140 | stream << " QQmlPrivate::RegisterQmlUnitCacheHook registration;\n" ; |
| 141 | stream << " registration.structVersion = 0;\n" ; |
| 142 | stream << " registration.lookupCachedQmlUnit = &lookupCachedUnit;\n" ; |
| 143 | stream << " QQmlPrivate::qmlregister(QQmlPrivate::QmlUnitCacheHookRegistration, ®istration);\n" ; |
| 144 | |
| 145 | stream << "}\n\n" ; |
| 146 | stream << "Registry::~Registry() {\n" ; |
| 147 | stream << " QQmlPrivate::qmlunregister(QQmlPrivate::QmlUnitCacheHookRegistration, quintptr(&lookupCachedUnit));\n" ; |
| 148 | stream << "}\n\n" ; |
| 149 | |
| 150 | stream << "const QQmlPrivate::CachedQmlUnit *Registry::lookupCachedUnit(const QUrl &url) {\n" ; |
| 151 | stream << " if (url.scheme() != QLatin1String(\"qrc\"))\n" ; |
| 152 | stream << " return nullptr;\n" ; |
| 153 | stream << " QString resourcePath = QDir::cleanPath(url.path());\n" ; |
| 154 | stream << " if (resourcePath.isEmpty())\n" ; |
| 155 | stream << " return nullptr;\n" ; |
| 156 | stream << " if (!resourcePath.startsWith(QLatin1Char('/')))\n" ; |
| 157 | stream << " resourcePath.prepend(QLatin1Char('/'));\n" ; |
| 158 | stream << " return unitRegistry()->resourcePathToCachedUnit.value(resourcePath, nullptr);\n" ; |
| 159 | stream << "}\n" ; |
| 160 | stream << "}\n" ; |
| 161 | |
| 162 | for (const QString &mapping: resourceFileMappings) { |
| 163 | QString originalResourceFile = mapping; |
| 164 | QString newResourceFile; |
| 165 | const int mappingSplit = originalResourceFile.indexOf(ch: QLatin1Char('=')); |
| 166 | if (mappingSplit != -1) { |
| 167 | newResourceFile = originalResourceFile.mid(position: mappingSplit + 1); |
| 168 | originalResourceFile.truncate(pos: mappingSplit); |
| 169 | } |
| 170 | |
| 171 | const QString suffix = qtResourceNameForFile(fileName: originalResourceFile); |
| 172 | const QString initFunction = QLatin1String("qInitResources_" ) + suffix; |
| 173 | |
| 174 | stream << QStringLiteral("int QT_MANGLE_NAMESPACE(%1)() {\n" ).arg(a: initFunction); |
| 175 | stream << " ::unitRegistry();\n" ; |
| 176 | if (!newResourceFile.isEmpty()) |
| 177 | stream << " Q_INIT_RESOURCE(" << qtResourceNameForFile(fileName: newResourceFile) << ");\n" ; |
| 178 | stream << " return 1;\n" ; |
| 179 | stream << "}\n" ; |
| 180 | stream << "Q_CONSTRUCTOR_FUNCTION(QT_MANGLE_NAMESPACE(" << initFunction << "))\n" ; |
| 181 | |
| 182 | const QString cleanupFunction = QLatin1String("qCleanupResources_" ) + suffix; |
| 183 | stream << QStringLiteral("int QT_MANGLE_NAMESPACE(%1)() {\n" ).arg(a: cleanupFunction); |
| 184 | if (!newResourceFile.isEmpty()) |
| 185 | stream << " Q_CLEANUP_RESOURCE(" << qtResourceNameForFile(fileName: newResourceFile) << ");\n" ; |
| 186 | stream << " return 1;\n" ; |
| 187 | stream << "}\n" ; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | #if QT_CONFIG(temporaryfile) |
| 192 | QSaveFile f(outputFileName); |
| 193 | #else |
| 194 | QFile f(outputFileName); |
| 195 | #endif |
| 196 | if (!f.open(flags: QIODevice::WriteOnly | QIODevice::Truncate)) { |
| 197 | *errorString = f.errorString(); |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | if (f.write(data: generatedLoaderCode) != generatedLoaderCode.size()) { |
| 202 | *errorString = f.errorString(); |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | #if QT_CONFIG(temporaryfile) |
| 207 | if (!f.commit()) { |
| 208 | *errorString = f.errorString(); |
| 209 | return false; |
| 210 | } |
| 211 | #endif |
| 212 | |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | QT_END_NAMESPACE |
| 217 | |