| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qqmlpropertycache_p.h" |
| 5 | |
| 6 | #include <private/qqmlengine_p.h> |
| 7 | #include <private/qqmlbinding_p.h> |
| 8 | #include <private/qqmlvmemetaobject_p.h> |
| 9 | |
| 10 | #include <private/qmetaobject_p.h> |
| 11 | #include <private/qmetaobjectbuilder_p.h> |
| 12 | #include <private/qqmlpropertycachemethodarguments_p.h> |
| 13 | #include <private/qqmlsignalnames_p.h> |
| 14 | |
| 15 | #include <private/qv4value_p.h> |
| 16 | |
| 17 | #include <QtCore/qdebug.h> |
| 18 | #include <QtCore/QCryptographicHash> |
| 19 | #include <QtCore/private/qtools_p.h> |
| 20 | |
| 21 | #include <limits.h> |
| 22 | #include <algorithm> |
| 23 | |
| 24 | #ifdef Q_CC_MSVC |
| 25 | // nonstandard extension used : zero-sized array in struct/union. |
| 26 | # pragma warning( disable : 4200 ) |
| 27 | #endif |
| 28 | |
| 29 | QT_BEGIN_NAMESPACE |
| 30 | |
| 31 | #define Q_INT16_MAX 32767 |
| 32 | |
| 33 | static int metaObjectSignalCount(const QMetaObject *metaObject) |
| 34 | { |
| 35 | int signalCount = 0; |
| 36 | for (const QMetaObject *obj = metaObject; obj; obj = obj->superClass()) |
| 37 | signalCount += QMetaObjectPrivate::get(metaobject: obj)->signalCount; |
| 38 | return signalCount; |
| 39 | } |
| 40 | |
| 41 | QQmlPropertyData::Flags |
| 42 | QQmlPropertyData::flagsForProperty(const QMetaProperty &p) |
| 43 | { |
| 44 | QQmlPropertyData::Flags flags; |
| 45 | |
| 46 | flags.setIsConstant(p.isConstant()); |
| 47 | flags.setIsWritable(p.isWritable()); |
| 48 | flags.setIsResettable(p.isResettable()); |
| 49 | flags.setIsFinal(p.isFinal()); |
| 50 | flags.setIsRequired(p.isRequired()); |
| 51 | flags.setIsBindable(p.isBindable()); |
| 52 | |
| 53 | |
| 54 | const QMetaType metaType = p.metaType(); |
| 55 | int propType = metaType.id(); |
| 56 | if (p.isEnumType()) { |
| 57 | flags.setType(QQmlPropertyData::Flags::EnumType); |
| 58 | } else if (metaType.flags() & QMetaType::PointerToQObject) { |
| 59 | flags.setType(QQmlPropertyData::Flags::QObjectDerivedType); |
| 60 | } else if (propType == QMetaType::QVariant) { |
| 61 | flags.setType(QQmlPropertyData::Flags::QVariantType); |
| 62 | } else if (metaType.flags() & QMetaType::IsQmlList) { |
| 63 | flags.setType(QQmlPropertyData::Flags::QListType); |
| 64 | } |
| 65 | |
| 66 | return flags; |
| 67 | } |
| 68 | |
| 69 | void QQmlPropertyData::load(const QMetaProperty &p) |
| 70 | { |
| 71 | Q_ASSERT(p.revision() <= std::numeric_limits<quint16>::max()); |
| 72 | setCoreIndex(p.propertyIndex()); |
| 73 | setNotifyIndex(QMetaObjectPrivate::signalIndex(m: p.notifySignal())); |
| 74 | setFlags(flagsForProperty(p)); |
| 75 | setRevision(QTypeRevision::fromEncodedVersion(value: p.revision())); |
| 76 | QMetaType type = p.metaType(); |
| 77 | setPropType(type); |
| 78 | } |
| 79 | |
| 80 | void QQmlPropertyData::load(const QMetaMethod &m) |
| 81 | { |
| 82 | setCoreIndex(m.methodIndex()); |
| 83 | m_flags.setType(Flags::FunctionType); |
| 84 | |
| 85 | // We need to set the constructor, signal, constant, arguments, V4Function, cloned flags. |
| 86 | // These are specific to methods and change with each method. |
| 87 | // The same QQmlPropertyData may be loaded with multiple methods in sequence. |
| 88 | |
| 89 | switch (m.methodType()) { |
| 90 | case QMetaMethod::Signal: |
| 91 | m_flags.setIsSignal(true); |
| 92 | m_flags.setIsConstructor(false); |
| 93 | setPropType(m.returnMetaType()); |
| 94 | break; |
| 95 | case QMetaMethod::Constructor: |
| 96 | m_flags.setIsSignal(false); |
| 97 | m_flags.setIsConstructor(true); |
| 98 | break; |
| 99 | default: |
| 100 | m_flags.setIsSignal(false); |
| 101 | m_flags.setIsConstructor(false); |
| 102 | setPropType(m.returnMetaType()); |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | m_flags.setIsConstant(m.isConst()); |
| 107 | |
| 108 | const int paramCount = m.parameterCount(); |
| 109 | if (paramCount) { |
| 110 | m_flags.setHasArguments(true); |
| 111 | m_flags.setIsV4Function( |
| 112 | paramCount == 1 && |
| 113 | m.parameterMetaType(index: 0) == QMetaType::fromType<QQmlV4FunctionPtr>()); |
| 114 | } else { |
| 115 | m_flags.setHasArguments(false); |
| 116 | m_flags.setIsV4Function(false); |
| 117 | } |
| 118 | |
| 119 | m_flags.setIsCloned(m.attributes() & QMetaMethod::Cloned); |
| 120 | |
| 121 | Q_ASSERT(m.revision() <= std::numeric_limits<quint16>::max()); |
| 122 | setRevision(QTypeRevision::fromEncodedVersion(value: m.revision())); |
| 123 | } |
| 124 | |
| 125 | /*! |
| 126 | Creates a standalone QQmlPropertyCache of \a metaObject. It is separate from the usual |
| 127 | QQmlPropertyCache hierarchy. It's parent is not equal to any other QQmlPropertyCache |
| 128 | created from QObject::staticMetaObject, for example. |
| 129 | */ |
| 130 | QQmlPropertyCache::Ptr QQmlPropertyCache::createStandalone( |
| 131 | const QMetaObject *metaObject, QTypeRevision metaObjectRevision) |
| 132 | { |
| 133 | Q_ASSERT(metaObject); |
| 134 | |
| 135 | Ptr result; |
| 136 | if (const QMetaObject *super = metaObject->superClass()) { |
| 137 | result = createStandalone( |
| 138 | metaObject: super, metaObjectRevision)->copyAndAppend(metaObject, typeVersion: metaObjectRevision); |
| 139 | } else { |
| 140 | result.adopt(other: new QQmlPropertyCache(metaObject)); |
| 141 | result->update(metaObject); |
| 142 | } |
| 143 | |
| 144 | if (metaObjectRevision.isValid() && metaObjectRevision != QTypeRevision::zero()) { |
| 145 | // Set the revision of the meta object that this cache describes to be |
| 146 | // 'metaObjectRevision'. This is useful when constructing a property cache |
| 147 | // from a type that was created directly in C++, and not through QML. For such |
| 148 | // types, the revision for each recorded QMetaObject would normally be zero, which |
| 149 | // would exclude any revisioned properties. |
| 150 | for (int metaObjectOffset = 0; metaObjectOffset < result->allowedRevisionCache.size(); |
| 151 | ++metaObjectOffset) { |
| 152 | result->allowedRevisionCache[metaObjectOffset] = metaObjectRevision; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return result; |
| 157 | } |
| 158 | |
| 159 | QQmlPropertyCache::~QQmlPropertyCache() |
| 160 | { |
| 161 | QQmlPropertyCacheMethodArguments *args = argumentsCache; |
| 162 | while (args) { |
| 163 | QQmlPropertyCacheMethodArguments *next = args->next; |
| 164 | delete args->names; |
| 165 | free(ptr: args); |
| 166 | args = next; |
| 167 | } |
| 168 | |
| 169 | // We must clear this prior to releasing the parent incase it is a |
| 170 | // linked hash |
| 171 | stringCache.clear(); |
| 172 | } |
| 173 | |
| 174 | QQmlPropertyCache::Ptr QQmlPropertyCache::copy(const QQmlMetaObjectPointer &mo, int reserve) const |
| 175 | { |
| 176 | QQmlPropertyCache::Ptr cache = QQmlPropertyCache::Ptr( |
| 177 | new QQmlPropertyCache(mo), QQmlPropertyCache::Ptr::Adopt); |
| 178 | cache->_parent.reset(t: this); |
| 179 | cache->propertyIndexCacheStart = propertyIndexCache.size() + propertyIndexCacheStart; |
| 180 | cache->methodIndexCacheStart = methodIndexCache.size() + methodIndexCacheStart; |
| 181 | cache->signalHandlerIndexCacheStart = signalHandlerIndexCache.size() + signalHandlerIndexCacheStart; |
| 182 | cache->stringCache.linkAndReserve(other: stringCache, additionalReserve: reserve); |
| 183 | cache->allowedRevisionCache = allowedRevisionCache; |
| 184 | cache->_defaultPropertyName = _defaultPropertyName; |
| 185 | cache->_listPropertyAssignBehavior = _listPropertyAssignBehavior; |
| 186 | |
| 187 | return cache; |
| 188 | } |
| 189 | |
| 190 | QQmlPropertyCache::Ptr QQmlPropertyCache::copy() const |
| 191 | { |
| 192 | return copy(mo: _metaObject, reserve: 0); |
| 193 | } |
| 194 | |
| 195 | QQmlPropertyCache::Ptr QQmlPropertyCache::copyAndReserve( |
| 196 | int propertyCount, int methodCount, int signalCount, int enumCount) const |
| 197 | { |
| 198 | QQmlPropertyCache::Ptr rv = copy( |
| 199 | mo: QQmlMetaObjectPointer(), reserve: propertyCount + methodCount + signalCount); |
| 200 | rv->propertyIndexCache.reserve(asize: propertyCount); |
| 201 | rv->methodIndexCache.reserve(asize: methodCount); |
| 202 | rv->signalHandlerIndexCache.reserve(asize: signalCount); |
| 203 | rv->enumCache.reserve(asize: enumCount); |
| 204 | return rv; |
| 205 | } |
| 206 | |
| 207 | /*! \internal |
| 208 | |
| 209 | \a notifyIndex MUST be in the signal index range (see QObjectPrivate::signalIndex()). |
| 210 | This is different from QMetaMethod::methodIndex(). |
| 211 | */ |
| 212 | void QQmlPropertyCache::appendProperty(const QString &name, QQmlPropertyData::Flags flags, |
| 213 | int coreIndex, QMetaType propType, QTypeRevision version, |
| 214 | int notifyIndex) |
| 215 | { |
| 216 | QQmlPropertyData data; |
| 217 | data.setPropType(propType); |
| 218 | data.setCoreIndex(coreIndex); |
| 219 | data.setNotifyIndex(notifyIndex); |
| 220 | data.setFlags(flags); |
| 221 | data.setTypeVersion(version); |
| 222 | |
| 223 | const OverrideResult overrideResult = handleOverride(name, data: &data); |
| 224 | if (overrideResult == InvalidOverride) |
| 225 | return; |
| 226 | |
| 227 | int index = propertyIndexCache.size(); |
| 228 | propertyIndexCache.append(t: data); |
| 229 | |
| 230 | setNamedProperty(key: name, index: index + propertyOffset(), data: propertyIndexCache.data() + index); |
| 231 | } |
| 232 | |
| 233 | void QQmlPropertyCache::appendSignal(const QString &name, QQmlPropertyData::Flags flags, |
| 234 | int coreIndex, const QMetaType *types, |
| 235 | const QList<QByteArray> &names) |
| 236 | { |
| 237 | QQmlPropertyData data; |
| 238 | data.setPropType(QMetaType()); |
| 239 | data.setCoreIndex(coreIndex); |
| 240 | data.setFlags(flags); |
| 241 | data.setArguments(nullptr); |
| 242 | |
| 243 | QQmlPropertyData handler = data; |
| 244 | handler.m_flags.setIsSignalHandler(true); |
| 245 | |
| 246 | if (types) { |
| 247 | const auto argumentCount = names.size(); |
| 248 | QQmlPropertyCacheMethodArguments *args = createArgumentsObject(count: argumentCount, names); |
| 249 | new (args->types) QMetaType; // Invalid return type |
| 250 | ::memcpy(dest: args->types + 1, src: types, n: argumentCount * sizeof(QMetaType)); |
| 251 | data.setArguments(args); |
| 252 | } |
| 253 | |
| 254 | const OverrideResult overrideResult = handleOverride(name, data: &data); |
| 255 | if (overrideResult == InvalidOverride) |
| 256 | return; |
| 257 | |
| 258 | int methodIndex = methodIndexCache.size(); |
| 259 | methodIndexCache.append(t: data); |
| 260 | |
| 261 | int signalHandlerIndex = signalHandlerIndexCache.size(); |
| 262 | signalHandlerIndexCache.append(t: handler); |
| 263 | |
| 264 | const QString handlerName = QQmlSignalNames::signalNameToHandlerName(signal: name); |
| 265 | |
| 266 | setNamedProperty(key: name, index: methodIndex + methodOffset(), data: methodIndexCache.data() + methodIndex); |
| 267 | setNamedProperty(key: handlerName, index: signalHandlerIndex + signalOffset(), |
| 268 | data: signalHandlerIndexCache.data() + signalHandlerIndex); |
| 269 | } |
| 270 | |
| 271 | void QQmlPropertyCache::appendMethod(const QString &name, QQmlPropertyData::Flags flags, |
| 272 | int coreIndex, QMetaType returnType, |
| 273 | const QList<QByteArray> &names, |
| 274 | const QVector<QMetaType> ¶meterTypes) |
| 275 | { |
| 276 | int argumentCount = names.size(); |
| 277 | |
| 278 | QQmlPropertyData data; |
| 279 | data.setPropType(returnType); |
| 280 | data.setCoreIndex(coreIndex); |
| 281 | data.setFlags(flags); |
| 282 | const OverrideResult overrideResult = handleOverride(name, data: &data); |
| 283 | if (overrideResult == InvalidOverride) |
| 284 | return; |
| 285 | |
| 286 | QQmlPropertyCacheMethodArguments *args = createArgumentsObject(count: argumentCount, names); |
| 287 | new (args->types) QMetaType(returnType); |
| 288 | for (int ii = 0; ii < argumentCount; ++ii) |
| 289 | new (args->types + ii + 1) QMetaType(parameterTypes.at(i: ii)); |
| 290 | data.setArguments(args); |
| 291 | |
| 292 | int methodIndex = methodIndexCache.size(); |
| 293 | methodIndexCache.append(t: data); |
| 294 | |
| 295 | setNamedProperty(key: name, index: methodIndex + methodOffset(), data: methodIndexCache.data() + methodIndex); |
| 296 | } |
| 297 | |
| 298 | void QQmlPropertyCache::appendEnum(const QString &name, const QVector<QQmlEnumValue> &values) |
| 299 | { |
| 300 | QQmlEnumData data; |
| 301 | data.name = name; |
| 302 | data.values = values; |
| 303 | enumCache.append(t: data); |
| 304 | } |
| 305 | |
| 306 | // Returns this property cache's metaObject, creating it if necessary. |
| 307 | const QMetaObject *QQmlPropertyCache::createMetaObject() const |
| 308 | { |
| 309 | if (_metaObject.isNull()) { |
| 310 | QMetaObjectBuilder builder; |
| 311 | toMetaObjectBuilder(builder); |
| 312 | builder.setSuperClass(_parent->createMetaObject()); |
| 313 | _metaObject.setSharedOnce(builder.toMetaObject()); |
| 314 | } |
| 315 | |
| 316 | return _metaObject.metaObject(); |
| 317 | } |
| 318 | |
| 319 | const QQmlPropertyData *QQmlPropertyCache::maybeUnresolvedProperty(int index) const |
| 320 | { |
| 321 | if (index < 0 || index >= propertyCount()) |
| 322 | return nullptr; |
| 323 | |
| 324 | const QQmlPropertyData *rv = nullptr; |
| 325 | if (index < propertyIndexCacheStart) |
| 326 | return _parent->maybeUnresolvedProperty(index); |
| 327 | else |
| 328 | rv = const_cast<const QQmlPropertyData *>(&propertyIndexCache.at(i: index - propertyIndexCacheStart)); |
| 329 | return rv; |
| 330 | } |
| 331 | |
| 332 | const QQmlPropertyData *QQmlPropertyCache::defaultProperty() const |
| 333 | { |
| 334 | return property(key: defaultPropertyName(), object: nullptr, context: nullptr); |
| 335 | } |
| 336 | |
| 337 | void QQmlPropertyCache::setParent(QQmlPropertyCache::ConstPtr newParent) |
| 338 | { |
| 339 | if (_parent != newParent) |
| 340 | _parent = std::move(newParent); |
| 341 | } |
| 342 | |
| 343 | QQmlPropertyCache::Ptr |
| 344 | QQmlPropertyCache::copyAndAppend(const QMetaObject *metaObject, |
| 345 | QTypeRevision typeVersion, |
| 346 | QQmlPropertyData::Flags propertyFlags, |
| 347 | QQmlPropertyData::Flags methodFlags, |
| 348 | QQmlPropertyData::Flags signalFlags) const |
| 349 | { |
| 350 | Q_ASSERT(QMetaObjectPrivate::get(metaObject)->revision >= 4); |
| 351 | |
| 352 | // Reserve enough space in the name hash for all the methods (including signals), all the |
| 353 | // signal handlers and all the properties. This assumes no name clashes, but this is the |
| 354 | // common case. |
| 355 | QQmlPropertyCache::Ptr rv = copy( |
| 356 | mo: metaObject, |
| 357 | reserve: QMetaObjectPrivate::get(metaobject: metaObject)->methodCount |
| 358 | + QMetaObjectPrivate::get(metaobject: metaObject)->signalCount |
| 359 | + QMetaObjectPrivate::get(metaobject: metaObject)->propertyCount); |
| 360 | |
| 361 | rv->append(metaObject, typeVersion, propertyFlags, methodFlags, signalFlags); |
| 362 | |
| 363 | return rv; |
| 364 | } |
| 365 | |
| 366 | static QHashedString signalNameToHandlerName(const QHashedString &methodName) |
| 367 | { |
| 368 | return QQmlSignalNames::signalNameToHandlerName(signal: methodName); |
| 369 | } |
| 370 | |
| 371 | static QHashedString signalNameToHandlerName(const QHashedCStringRef &methodName) |
| 372 | { |
| 373 | return QQmlSignalNames::signalNameToHandlerName( |
| 374 | signal: QLatin1StringView{ methodName.constData(), methodName.length() }); |
| 375 | } |
| 376 | |
| 377 | |
| 378 | void QQmlPropertyCache::append(const QMetaObject *metaObject, |
| 379 | QTypeRevision typeVersion, |
| 380 | QQmlPropertyData::Flags propertyFlags, |
| 381 | QQmlPropertyData::Flags methodFlags, |
| 382 | QQmlPropertyData::Flags signalFlags) |
| 383 | { |
| 384 | allowedRevisionCache.append(t: QTypeRevision::zero()); |
| 385 | |
| 386 | int methodCount = metaObject->methodCount(); |
| 387 | Q_ASSERT(QMetaObjectPrivate::get(metaObject)->revision >= 4); |
| 388 | int signalCount = metaObjectSignalCount(metaObject); |
| 389 | int classInfoCount = QMetaObjectPrivate::get(metaobject: metaObject)->classInfoCount; |
| 390 | |
| 391 | if (classInfoCount) { |
| 392 | int classInfoOffset = metaObject->classInfoOffset(); |
| 393 | for (int ii = 0; ii < classInfoCount; ++ii) { |
| 394 | int idx = ii + classInfoOffset; |
| 395 | QMetaClassInfo mci = metaObject->classInfo(index: idx); |
| 396 | const char *name = mci.name(); |
| 397 | if (0 == qstrcmp(str1: name, str2: "DefaultProperty" )) { |
| 398 | _defaultPropertyName = QString::fromUtf8(utf8: mci.value()); |
| 399 | } else if (0 == qstrcmp(str1: name, str2: "qt_QmlJSWrapperFactoryMethod" )) { |
| 400 | const char * const factoryMethod = mci.value(); |
| 401 | _jsFactoryMethodIndex = metaObject->indexOfSlot(slot: factoryMethod); |
| 402 | if (_jsFactoryMethodIndex != -1) |
| 403 | _jsFactoryMethodIndex -= metaObject->methodOffset(); |
| 404 | } else if (0 == qstrcmp(str1: name, str2: "QML.ListPropertyAssignBehavior" )) { |
| 405 | _listPropertyAssignBehavior = mci.value(); |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | //Used to block access to QObject::destroyed() and QObject::deleteLater() from QML |
| 411 | static const int destroyedIdx1 = QObject::staticMetaObject.indexOfSignal(signal: "destroyed(QObject*)" ); |
| 412 | static const int destroyedIdx2 = QObject::staticMetaObject.indexOfSignal(signal: "destroyed()" ); |
| 413 | static const int deleteLaterIdx = QObject::staticMetaObject.indexOfSlot(slot: "deleteLater()" ); |
| 414 | // These indices don't apply to gadgets, so don't block them. |
| 415 | // It is enough to check for QObject::staticMetaObject here because the loop below excludes |
| 416 | // methods of parent classes: It starts at metaObject->methodOffset() |
| 417 | const bool preventDestruction = (metaObject == &QObject::staticMetaObject); |
| 418 | |
| 419 | int methodOffset = metaObject->methodOffset(); |
| 420 | int signalOffset = signalCount - QMetaObjectPrivate::get(metaobject: metaObject)->signalCount; |
| 421 | |
| 422 | // update() should have reserved enough space in the vector that this doesn't cause a realloc |
| 423 | // and invalidate the stringCache. |
| 424 | methodIndexCache.resize(size: methodCount - methodIndexCacheStart); |
| 425 | signalHandlerIndexCache.resize(size: signalCount - signalHandlerIndexCacheStart); |
| 426 | int signalHandlerIndex = signalOffset; |
| 427 | for (int ii = methodOffset; ii < methodCount; ++ii) { |
| 428 | if (preventDestruction && (ii == destroyedIdx1 || ii == destroyedIdx2 || ii == deleteLaterIdx)) |
| 429 | continue; |
| 430 | QMetaMethod m = metaObject->method(index: ii); |
| 431 | if (m.access() == QMetaMethod::Private) |
| 432 | continue; |
| 433 | |
| 434 | // Extract method name |
| 435 | // It's safe to keep the raw name pointer |
| 436 | Q_ASSERT(QMetaObjectPrivate::get(metaObject)->revision >= 7); |
| 437 | const char *rawName = m.name().constData(); |
| 438 | const char *cptr = rawName; |
| 439 | char utf8 = 0; |
| 440 | while (*cptr) { |
| 441 | utf8 |= *cptr & 0x80; |
| 442 | ++cptr; |
| 443 | } |
| 444 | |
| 445 | QQmlPropertyData *data = &methodIndexCache[ii - methodIndexCacheStart]; |
| 446 | QQmlPropertyData *sigdata = nullptr; |
| 447 | |
| 448 | if (m.methodType() == QMetaMethod::Signal) |
| 449 | data->setFlags(signalFlags); |
| 450 | else |
| 451 | data->setFlags(methodFlags); |
| 452 | |
| 453 | data->load(m); |
| 454 | |
| 455 | Q_ASSERT((allowedRevisionCache.size() - 1) < Q_INT16_MAX); |
| 456 | data->setMetaObjectOffset(allowedRevisionCache.size() - 1); |
| 457 | |
| 458 | if (data->isSignal()) { |
| 459 | sigdata = &signalHandlerIndexCache[signalHandlerIndex - signalHandlerIndexCacheStart]; |
| 460 | *sigdata = *data; |
| 461 | sigdata->m_flags.setIsSignalHandler(true); |
| 462 | } |
| 463 | |
| 464 | QQmlPropertyData *old = nullptr; |
| 465 | |
| 466 | const auto doSetNamedProperty = [&](const auto &methodName) { |
| 467 | if (StringCache::mapped_type *it = stringCache.value(methodName)) { |
| 468 | if (handleOverride(methodName, data, (old = it->second)) == InvalidOverride) |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | setNamedProperty(methodName, ii, data); |
| 473 | |
| 474 | if (data->isSignal()) { |
| 475 | |
| 476 | // TODO: Remove this once we can. Signals should not be overridable. |
| 477 | if (!utf8) |
| 478 | data->m_flags.setIsOverridableSignal(true); |
| 479 | |
| 480 | setNamedProperty(signalNameToHandlerName(methodName), ii, sigdata); |
| 481 | ++signalHandlerIndex; |
| 482 | } |
| 483 | }; |
| 484 | |
| 485 | if (utf8) |
| 486 | doSetNamedProperty(QHashedString(QString::fromUtf8(utf8: rawName, size: cptr - rawName))); |
| 487 | else |
| 488 | doSetNamedProperty(QHashedCStringRef(rawName, cptr - rawName)); |
| 489 | } |
| 490 | |
| 491 | int propCount = metaObject->propertyCount(); |
| 492 | int propOffset = metaObject->propertyOffset(); |
| 493 | |
| 494 | // update() should have reserved enough space in the vector that this doesn't cause a realloc |
| 495 | // and invalidate the stringCache. |
| 496 | propertyIndexCache.resize(size: propCount - propertyIndexCacheStart); |
| 497 | for (int ii = propOffset; ii < propCount; ++ii) { |
| 498 | QMetaProperty p = metaObject->property(index: ii); |
| 499 | if (!p.isScriptable()) |
| 500 | continue; |
| 501 | |
| 502 | const char *str = p.name(); |
| 503 | char utf8 = 0; |
| 504 | const char *cptr = str; |
| 505 | while (*cptr != 0) { |
| 506 | utf8 |= *cptr & 0x80; |
| 507 | ++cptr; |
| 508 | } |
| 509 | |
| 510 | QQmlPropertyData *data = &propertyIndexCache[ii - propertyIndexCacheStart]; |
| 511 | |
| 512 | data->setFlags(propertyFlags); |
| 513 | data->load(p); |
| 514 | data->setTypeVersion(typeVersion); |
| 515 | |
| 516 | Q_ASSERT((allowedRevisionCache.size() - 1) < Q_INT16_MAX); |
| 517 | data->setMetaObjectOffset(allowedRevisionCache.size() - 1); |
| 518 | |
| 519 | QQmlPropertyData *old = nullptr; |
| 520 | |
| 521 | if (utf8) { |
| 522 | QHashedString propName(QString::fromUtf8(utf8: str, size: cptr - str)); |
| 523 | if (StringCache::mapped_type *it = stringCache.value(key: propName)) { |
| 524 | if (handleOverride(name: propName, data, old: (old = it->second)) == InvalidOverride) |
| 525 | continue; |
| 526 | } |
| 527 | setNamedProperty(key: propName, index: ii, data); |
| 528 | } else { |
| 529 | QHashedCStringRef propName(str, cptr - str); |
| 530 | if (StringCache::mapped_type *it = stringCache.value(key: propName)) { |
| 531 | if (handleOverride(name: propName, data, old: (old = it->second)) == InvalidOverride) |
| 532 | continue; |
| 533 | } |
| 534 | setNamedProperty(key: propName, index: ii, data); |
| 535 | } |
| 536 | |
| 537 | bool isGadget = true; |
| 538 | for (const QMetaObject *it = metaObject; it != nullptr; it = it->superClass()) { |
| 539 | if (it == &QObject::staticMetaObject) |
| 540 | isGadget = false; |
| 541 | } |
| 542 | |
| 543 | // otherwise always dispatch over a 'normal' meta-call so the QQmlValueType can intercept |
| 544 | if (!isGadget) |
| 545 | data->trySetStaticMetaCallFunction(f: metaObject->d.static_metacall, relativePropertyIndex: ii - propOffset); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | void QQmlPropertyCache::update(const QMetaObject *metaObject) |
| 550 | { |
| 551 | Q_ASSERT(metaObject); |
| 552 | stringCache.clear(); |
| 553 | |
| 554 | // Preallocate enough space in the index caches for all the properties/methods/signals that |
| 555 | // are not cached in a parent cache so that the caches never need to be reallocated as this |
| 556 | // would invalidate pointers stored in the stringCache. |
| 557 | int pc = metaObject->propertyCount(); |
| 558 | int mc = metaObject->methodCount(); |
| 559 | int sc = metaObjectSignalCount(metaObject); |
| 560 | propertyIndexCache.reserve(asize: pc - propertyIndexCacheStart); |
| 561 | methodIndexCache.reserve(asize: mc - methodIndexCacheStart); |
| 562 | signalHandlerIndexCache.reserve(asize: sc - signalHandlerIndexCacheStart); |
| 563 | |
| 564 | // Reserve enough space in the stringCache for all properties/methods/signals including those |
| 565 | // cached in a parent cache. |
| 566 | stringCache.reserve(n: pc + mc + sc); |
| 567 | |
| 568 | if (metaObject) |
| 569 | append(metaObject, typeVersion: QTypeRevision()); |
| 570 | } |
| 571 | |
| 572 | /*! \internal |
| 573 | invalidates and updates the PropertyCache if the QMetaObject has changed. |
| 574 | This function is used in the tooling to update dynamic properties. |
| 575 | */ |
| 576 | void QQmlPropertyCache::invalidate(const QMetaObject *metaObject) |
| 577 | { |
| 578 | propertyIndexCache.clear(); |
| 579 | methodIndexCache.clear(); |
| 580 | signalHandlerIndexCache.clear(); |
| 581 | |
| 582 | argumentsCache = nullptr; |
| 583 | |
| 584 | int pc = metaObject->propertyCount(); |
| 585 | int mc = metaObject->methodCount(); |
| 586 | int sc = metaObjectSignalCount(metaObject); |
| 587 | int reserve = pc + mc + sc; |
| 588 | |
| 589 | if (parent()) { |
| 590 | propertyIndexCacheStart = parent()->propertyIndexCache.size() + parent()->propertyIndexCacheStart; |
| 591 | methodIndexCacheStart = parent()->methodIndexCache.size() + parent()->methodIndexCacheStart; |
| 592 | signalHandlerIndexCacheStart = parent()->signalHandlerIndexCache.size() + parent()->signalHandlerIndexCacheStart; |
| 593 | stringCache.linkAndReserve(other: parent()->stringCache, additionalReserve: reserve); |
| 594 | append(metaObject, typeVersion: QTypeRevision()); |
| 595 | } else { |
| 596 | propertyIndexCacheStart = 0; |
| 597 | methodIndexCacheStart = 0; |
| 598 | signalHandlerIndexCacheStart = 0; |
| 599 | update(metaObject); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | const QQmlPropertyData *QQmlPropertyCache::findProperty( |
| 604 | StringCache::ConstIterator it, QObject *object, |
| 605 | const QQmlRefPointer<QQmlContextData> &context) const |
| 606 | { |
| 607 | QQmlData *data = (object ? QQmlData::get(object) : nullptr); |
| 608 | const QQmlVMEMetaObject *vmemo = nullptr; |
| 609 | if (data && data->hasVMEMetaObject) { |
| 610 | QObjectPrivate *op = QObjectPrivate::get(o: object); |
| 611 | vmemo = static_cast<const QQmlVMEMetaObject *>(op->metaObject); |
| 612 | } |
| 613 | return findProperty(it, vmemo, context); |
| 614 | } |
| 615 | |
| 616 | namespace { |
| 617 | |
| 618 | inline bool contextHasNoExtensions(const QQmlRefPointer<QQmlContextData> &context) |
| 619 | { |
| 620 | // This context has no extension if its parent is the engine's rootContext, |
| 621 | // which has children but no imports |
| 622 | const QQmlRefPointer<QQmlContextData> parent = context->parent(); |
| 623 | return (!parent || !parent->imports()); |
| 624 | } |
| 625 | |
| 626 | inline int maximumIndexForProperty(const QQmlPropertyData *prop, const int methodCount, const int signalCount, const int propertyCount) |
| 627 | { |
| 628 | return prop->isFunction() ? methodCount |
| 629 | : prop->isSignalHandler() ? signalCount |
| 630 | : propertyCount; |
| 631 | } |
| 632 | |
| 633 | } |
| 634 | |
| 635 | const QQmlPropertyData *QQmlPropertyCache::findProperty( |
| 636 | StringCache::ConstIterator it, const QQmlVMEMetaObject *vmemo, |
| 637 | const QQmlRefPointer<QQmlContextData> &context) const |
| 638 | { |
| 639 | StringCache::ConstIterator end = stringCache.end(); |
| 640 | |
| 641 | if (it != end) { |
| 642 | const QQmlPropertyData *result = it.value().second; |
| 643 | |
| 644 | // If there exists a typed property (not a function or signal handler), of the |
| 645 | // right name available to the specified context, we need to return that |
| 646 | // property rather than any subsequent override |
| 647 | |
| 648 | if (vmemo && context && !contextHasNoExtensions(context)) { |
| 649 | // Find the meta-object that corresponds to the supplied context |
| 650 | do { |
| 651 | if (vmemo->ctxt.contextData().data() == context.data()) |
| 652 | break; |
| 653 | |
| 654 | vmemo = vmemo->parentVMEMetaObject(); |
| 655 | } while (vmemo); |
| 656 | } |
| 657 | |
| 658 | if (vmemo) { |
| 659 | const int methodCount = vmemo->cache->methodCount(); |
| 660 | const int signalCount = vmemo->cache->signalCount(); |
| 661 | const int propertyCount = vmemo->cache->propertyCount(); |
| 662 | |
| 663 | // Ensure that the property we resolve to is accessible from this meta-object |
| 664 | do { |
| 665 | const StringCache::mapped_type &property(it.value()); |
| 666 | |
| 667 | if (property.first < maximumIndexForProperty(prop: property.second, methodCount, signalCount, propertyCount)) { |
| 668 | // This property is available in the specified context |
| 669 | if (property.second->isFunction() || property.second->isSignalHandler()) { |
| 670 | // Prefer the earlier resolution |
| 671 | } else { |
| 672 | // Prefer the typed property to any previous property found |
| 673 | result = property.second; |
| 674 | } |
| 675 | break; |
| 676 | } |
| 677 | |
| 678 | // See if there is a better candidate |
| 679 | it = stringCache.findNext(iter: it); |
| 680 | } while (it != end); |
| 681 | } |
| 682 | |
| 683 | return result; |
| 684 | } |
| 685 | |
| 686 | return nullptr; |
| 687 | } |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | bool QQmlPropertyData::markAsOverrideOf(QQmlPropertyData *predecessor) |
| 694 | { |
| 695 | Q_ASSERT(predecessor != this); |
| 696 | if (predecessor->isFinal()) |
| 697 | return false; |
| 698 | |
| 699 | setOverrideIndexIsProperty(!predecessor->isFunction()); |
| 700 | setOverrideIndex(predecessor->coreIndex()); |
| 701 | predecessor->m_flags.setIsOverridden(true); |
| 702 | Q_ASSERT(predecessor->isOverridden()); |
| 703 | return true; |
| 704 | } |
| 705 | |
| 706 | QQmlPropertyCacheMethodArguments *QQmlPropertyCache::createArgumentsObject( |
| 707 | int argc, const QList<QByteArray> &names) |
| 708 | { |
| 709 | typedef QQmlPropertyCacheMethodArguments A; |
| 710 | A *args = static_cast<A *>(malloc(size: sizeof(A) + argc * sizeof(QMetaType))); |
| 711 | args->names = argc ? new QList<QByteArray>(names) : nullptr; |
| 712 | args->next = argumentsCache; |
| 713 | argumentsCache = args; |
| 714 | return args; |
| 715 | } |
| 716 | |
| 717 | QString QQmlPropertyCache::signalParameterStringForJS(QV4::ExecutionEngine *engine, const QList<QByteArray> ¶meterNameList, QString *errorString) |
| 718 | { |
| 719 | bool unnamedParameter = false; |
| 720 | const QSet<QString> &illegalNames = engine->illegalNames(); |
| 721 | QString parameters; |
| 722 | |
| 723 | const qsizetype count = parameterNameList.size(); |
| 724 | if (count > std::numeric_limits<quint16>::max()) |
| 725 | *errorString = QCoreApplication::translate(context: "QQmlRewrite" , key: "Signal has an excessive number of parameters: %1" ).arg(a: count); |
| 726 | |
| 727 | for (qsizetype i = 0; i < count; ++i) { |
| 728 | if (i > 0) |
| 729 | parameters += QLatin1Char(','); |
| 730 | const QByteArray ¶m = parameterNameList.at(i); |
| 731 | if (param.isEmpty()) { |
| 732 | unnamedParameter = true; |
| 733 | } else if (unnamedParameter) { |
| 734 | if (errorString) |
| 735 | *errorString = QCoreApplication::translate(context: "QQmlRewrite" , key: "Signal uses unnamed parameter followed by named parameter." ); |
| 736 | return QString(); |
| 737 | } else if (illegalNames.contains(value: QString::fromUtf8(ba: param))) { |
| 738 | if (errorString) |
| 739 | *errorString = QCoreApplication::translate(context: "QQmlRewrite" , key: "Signal parameter \"%1\" hides global variable." ).arg(a: QString::fromUtf8(ba: param)); |
| 740 | return QString(); |
| 741 | } |
| 742 | parameters += QString::fromUtf8(ba: param); |
| 743 | } |
| 744 | |
| 745 | return parameters; |
| 746 | } |
| 747 | |
| 748 | int QQmlPropertyCache::originalClone(int index) const |
| 749 | { |
| 750 | while (signal(index)->isCloned()) |
| 751 | --index; |
| 752 | return index; |
| 753 | } |
| 754 | |
| 755 | int QQmlPropertyCache::originalClone(const QObject *object, int index) |
| 756 | { |
| 757 | QQmlData *data = QQmlData::get(object); |
| 758 | if (data && data->propertyCache) { |
| 759 | const QQmlPropertyCache *cache = data->propertyCache.data(); |
| 760 | const QQmlPropertyData *sig = cache->signal(index); |
| 761 | while (sig && sig->isCloned()) { |
| 762 | --index; |
| 763 | sig = cache->signal(index); |
| 764 | } |
| 765 | } else { |
| 766 | while (QMetaObjectPrivate::signal(m: object->metaObject(), signal_index: index).attributes() & QMetaMethod::Cloned) |
| 767 | --index; |
| 768 | } |
| 769 | return index; |
| 770 | } |
| 771 | |
| 772 | template<typename T> |
| 773 | static QQmlPropertyData qQmlPropertyCacheCreate(const QMetaObject *metaObject, const T& propertyName) |
| 774 | { |
| 775 | Q_ASSERT(metaObject); |
| 776 | |
| 777 | QQmlPropertyData rv; |
| 778 | |
| 779 | /* It's important to check the method list before checking for properties; |
| 780 | * otherwise, if the meta object is dynamic, a property will be created even |
| 781 | * if not found and it might obscure a method having the same name. */ |
| 782 | |
| 783 | //Used to block access to QObject::destroyed() and QObject::deleteLater() from QML |
| 784 | static const int destroyedIdx1 = QObject::staticMetaObject.indexOfSignal(signal: "destroyed(QObject*)" ); |
| 785 | static const int destroyedIdx2 = QObject::staticMetaObject.indexOfSignal(signal: "destroyed()" ); |
| 786 | static const int deleteLaterIdx = QObject::staticMetaObject.indexOfSlot(slot: "deleteLater()" ); |
| 787 | // These indices don't apply to gadgets, so don't block them. |
| 788 | const bool preventDestruction = metaObject->superClass() || metaObject == &QObject::staticMetaObject; |
| 789 | |
| 790 | int methodCount = metaObject->methodCount(); |
| 791 | for (int ii = methodCount - 1; ii >= 0; --ii) { |
| 792 | if (preventDestruction && (ii == destroyedIdx1 || ii == destroyedIdx2 || ii == deleteLaterIdx)) |
| 793 | continue; |
| 794 | QMetaMethod m = metaObject->method(index: ii); |
| 795 | if (m.access() == QMetaMethod::Private) |
| 796 | continue; |
| 797 | |
| 798 | if (m.name() == propertyName) { |
| 799 | rv.load(m); |
| 800 | return rv; |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | { |
| 805 | const QMetaObject *cmo = metaObject; |
| 806 | while (cmo) { |
| 807 | int idx = cmo->indexOfProperty(name: propertyName); |
| 808 | if (idx != -1) { |
| 809 | QMetaProperty p = cmo->property(index: idx); |
| 810 | if (p.isScriptable()) { |
| 811 | rv.load(p); |
| 812 | return rv; |
| 813 | } else { |
| 814 | bool changed = false; |
| 815 | while (cmo && cmo->propertyOffset() >= idx) { |
| 816 | cmo = cmo->superClass(); |
| 817 | changed = true; |
| 818 | } |
| 819 | /* If the "cmo" variable didn't change, set it to 0 to |
| 820 | * avoid running into an infinite loop */ |
| 821 | if (!changed) cmo = nullptr; |
| 822 | } |
| 823 | } else { |
| 824 | cmo = nullptr; |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | return rv; |
| 829 | } |
| 830 | |
| 831 | static inline const char *qQmlPropertyCacheToString(QLatin1String string) |
| 832 | { |
| 833 | return string.data(); |
| 834 | } |
| 835 | |
| 836 | static inline QByteArray qQmlPropertyCacheToString(QStringView string) |
| 837 | { |
| 838 | return string.toUtf8(); |
| 839 | } |
| 840 | |
| 841 | static inline QByteArray qQmlPropertyCacheToString(const QV4::String *string) |
| 842 | { |
| 843 | return string->toQString().toUtf8(); |
| 844 | } |
| 845 | |
| 846 | template<typename T> |
| 847 | const QQmlPropertyData * |
| 848 | qQmlPropertyCacheProperty(QObject *obj, T name, const QQmlRefPointer<QQmlContextData> &context, |
| 849 | QQmlPropertyData *local) |
| 850 | { |
| 851 | const QQmlPropertyCache *cache = nullptr; |
| 852 | |
| 853 | QQmlData *ddata = QQmlData::get(object: obj, create: false); |
| 854 | |
| 855 | if (ddata && ddata->propertyCache) { |
| 856 | cache = ddata->propertyCache.data(); |
| 857 | } else if (auto newCache = QQmlMetaType::propertyCache(object: obj)) { |
| 858 | cache = newCache.data(); |
| 859 | ddata = QQmlData::get(object: obj, create: true); |
| 860 | ddata->propertyCache = std::move(newCache); |
| 861 | } |
| 862 | |
| 863 | const QQmlPropertyData *rv = nullptr; |
| 864 | |
| 865 | if (cache) { |
| 866 | rv = cache->property(name, obj, context); |
| 867 | } else if (local) { |
| 868 | *local = qQmlPropertyCacheCreate(obj->metaObject(), qQmlPropertyCacheToString(name)); |
| 869 | if (local->isValid()) |
| 870 | rv = local; |
| 871 | } |
| 872 | |
| 873 | return rv; |
| 874 | } |
| 875 | |
| 876 | const QQmlPropertyData *QQmlPropertyCache::property( |
| 877 | QObject *obj, const QV4::String *name, const QQmlRefPointer<QQmlContextData> &context, |
| 878 | QQmlPropertyData *local) |
| 879 | { |
| 880 | return qQmlPropertyCacheProperty<const QV4::String *>(obj, name, context, local); |
| 881 | } |
| 882 | |
| 883 | const QQmlPropertyData *QQmlPropertyCache::property( |
| 884 | QObject *obj, QStringView name, const QQmlRefPointer<QQmlContextData> &context, |
| 885 | QQmlPropertyData *local) |
| 886 | { |
| 887 | return qQmlPropertyCacheProperty<const QStringView &>(obj, name, context, local); |
| 888 | } |
| 889 | |
| 890 | const QQmlPropertyData *QQmlPropertyCache::property( |
| 891 | QObject *obj, const QLatin1String &name, const QQmlRefPointer<QQmlContextData> &context, |
| 892 | QQmlPropertyData *local) |
| 893 | { |
| 894 | return qQmlPropertyCacheProperty<const QLatin1String &>(obj, name, context, local); |
| 895 | } |
| 896 | |
| 897 | // this function is copied from qmetaobject.cpp |
| 898 | static inline const QByteArray stringData(const QMetaObject *mo, int index) |
| 899 | { |
| 900 | uint offset = mo->d.stringdata[2*index]; |
| 901 | uint length = mo->d.stringdata[2*index + 1]; |
| 902 | const char *string = reinterpret_cast<const char *>(mo->d.stringdata) + offset; |
| 903 | return QByteArray::fromRawData(data: string, size: length); |
| 904 | } |
| 905 | |
| 906 | const char *QQmlPropertyCache::className() const |
| 907 | { |
| 908 | if (const QMetaObject *mo = _metaObject.metaObject()) |
| 909 | return mo->className(); |
| 910 | else |
| 911 | return _dynamicClassName.constData(); |
| 912 | } |
| 913 | |
| 914 | void QQmlPropertyCache::toMetaObjectBuilder(QMetaObjectBuilder &builder) const |
| 915 | { |
| 916 | struct Sort { static bool lt(const QPair<QString, const QQmlPropertyData *> &lhs, |
| 917 | const QPair<QString, const QQmlPropertyData *> &rhs) { |
| 918 | return lhs.second->coreIndex() < rhs.second->coreIndex(); |
| 919 | } }; |
| 920 | |
| 921 | struct Insert { static void in(const QQmlPropertyCache *This, |
| 922 | QList<QPair<QString, const QQmlPropertyData *> > &properties, |
| 923 | QList<QPair<QString, const QQmlPropertyData *> > &methods, |
| 924 | StringCache::ConstIterator iter, const QQmlPropertyData *data) { |
| 925 | if (data->isSignalHandler()) |
| 926 | return; |
| 927 | |
| 928 | if (data->isFunction()) { |
| 929 | if (data->coreIndex() < This->methodIndexCacheStart) |
| 930 | return; |
| 931 | |
| 932 | QPair<QString, const QQmlPropertyData *> entry = qMakePair(value1: (QString)iter.key(), value2&: data); |
| 933 | // Overrides can cause the entry to already exist |
| 934 | if (!methods.contains(t: entry)) methods.append(t: entry); |
| 935 | |
| 936 | data = This->overrideData(data); |
| 937 | if (data && !data->isFunction()) Insert::in(This, properties, methods, iter, data); |
| 938 | } else { |
| 939 | if (data->coreIndex() < This->propertyIndexCacheStart) |
| 940 | return; |
| 941 | |
| 942 | QPair<QString, const QQmlPropertyData *> entry = qMakePair(value1: (QString)iter.key(), value2&: data); |
| 943 | // Overrides can cause the entry to already exist |
| 944 | if (!properties.contains(t: entry)) properties.append(t: entry); |
| 945 | |
| 946 | data = This->overrideData(data); |
| 947 | if (data) Insert::in(This, properties, methods, iter, data); |
| 948 | } |
| 949 | |
| 950 | } }; |
| 951 | |
| 952 | builder.setClassName(_dynamicClassName); |
| 953 | |
| 954 | QList<QPair<QString, const QQmlPropertyData *> > properties; |
| 955 | QList<QPair<QString, const QQmlPropertyData *> > methods; |
| 956 | |
| 957 | for (StringCache::ConstIterator iter = stringCache.begin(), cend = stringCache.end(); iter != cend; ++iter) |
| 958 | Insert::in(This: this, properties, methods, iter, data: iter.value().second); |
| 959 | |
| 960 | Q_ASSERT(properties.size() == propertyIndexCache.size()); |
| 961 | Q_ASSERT(methods.size() == methodIndexCache.size()); |
| 962 | |
| 963 | std::sort(first: properties.begin(), last: properties.end(), comp: Sort::lt); |
| 964 | std::sort(first: methods.begin(), last: methods.end(), comp: Sort::lt); |
| 965 | |
| 966 | for (int ii = 0; ii < properties.size(); ++ii) { |
| 967 | const QQmlPropertyData *data = properties.at(i: ii).second; |
| 968 | |
| 969 | int notifierId = -1; |
| 970 | if (data->notifyIndex() != -1) |
| 971 | notifierId = data->notifyIndex() - signalHandlerIndexCacheStart; |
| 972 | |
| 973 | QMetaPropertyBuilder property = builder.addProperty(name: properties.at(i: ii).first.toUtf8(), |
| 974 | type: data->propType().name(), |
| 975 | metaType: data->propType(), |
| 976 | notifierId); |
| 977 | |
| 978 | property.setReadable(true); |
| 979 | property.setWritable(data->isWritable()); |
| 980 | property.setResettable(data->isResettable()); |
| 981 | property.setBindable(data->isBindable()); |
| 982 | property.setAlias(data->isAlias()); |
| 983 | } |
| 984 | |
| 985 | for (int ii = 0; ii < methods.size(); ++ii) { |
| 986 | const QQmlPropertyData *data = methods.at(i: ii).second; |
| 987 | |
| 988 | QByteArray returnType; |
| 989 | if (data->propType().isValid()) |
| 990 | returnType = data->propType().name(); |
| 991 | |
| 992 | QByteArray signature; |
| 993 | // '+=' reserves extra capacity. Follow-up appending will be probably free. |
| 994 | signature += methods.at(i: ii).first.toUtf8() + '('; |
| 995 | |
| 996 | QQmlPropertyCacheMethodArguments *arguments = nullptr; |
| 997 | if (data->hasArguments()) { |
| 998 | arguments = data->arguments(); |
| 999 | for (int ii = 0, end = arguments->names ? arguments->names->size() : 0; |
| 1000 | ii < end; ++ii) { |
| 1001 | if (ii != 0) |
| 1002 | signature.append(c: ','); |
| 1003 | signature.append(s: arguments->types[1 + ii].name()); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | signature.append(c: ')'); |
| 1008 | |
| 1009 | QMetaMethodBuilder method; |
| 1010 | if (data->isSignal()) { |
| 1011 | method = builder.addSignal(signature); |
| 1012 | } else { |
| 1013 | method = builder.addSlot(signature); |
| 1014 | } |
| 1015 | method.setAccess(QMetaMethod::Public); |
| 1016 | |
| 1017 | if (arguments && arguments->names) |
| 1018 | method.setParameterNames(*arguments->names); |
| 1019 | |
| 1020 | if (!returnType.isEmpty()) |
| 1021 | method.setReturnType(returnType); |
| 1022 | } |
| 1023 | |
| 1024 | for (int ii = 0; ii < enumCache.size(); ++ii) { |
| 1025 | const QQmlEnumData &enumData = enumCache.at(i: ii); |
| 1026 | QMetaEnumBuilder enumeration = builder.addEnumerator(name: enumData.name.toUtf8()); |
| 1027 | enumeration.setIsScoped(true); |
| 1028 | for (int jj = 0; jj < enumData.values.size(); ++jj) { |
| 1029 | const QQmlEnumValue &value = enumData.values.at(i: jj); |
| 1030 | enumeration.addKey(name: value.namedValue.toUtf8(), value: value.value); |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | if (!_defaultPropertyName.isEmpty()) { |
| 1035 | const QQmlPropertyData *dp = property(key: _defaultPropertyName, object: nullptr, context: nullptr); |
| 1036 | if (dp && dp->coreIndex() >= propertyIndexCacheStart) { |
| 1037 | Q_ASSERT(!dp->isFunction()); |
| 1038 | builder.addClassInfo(name: "DefaultProperty" , value: _defaultPropertyName.toUtf8()); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | if (!_listPropertyAssignBehavior.isEmpty()) |
| 1043 | builder.addClassInfo(name: "QML.ListPropertyAssignBehavior" , value: _listPropertyAssignBehavior); |
| 1044 | } |
| 1045 | |
| 1046 | namespace { |
| 1047 | template <typename StringVisitor, typename TypeInfoVisitor> |
| 1048 | int visitMethods(const QMetaObject &mo, int methodOffset, int methodCount, |
| 1049 | StringVisitor visitString, TypeInfoVisitor visitTypeInfo) |
| 1050 | { |
| 1051 | int fieldsForParameterData = 0; |
| 1052 | |
| 1053 | bool hasRevisionedMethods = false; |
| 1054 | |
| 1055 | for (int i = 0; i < methodCount; ++i) { |
| 1056 | const int handle = methodOffset + i * QMetaObjectPrivate::IntsPerMethod; |
| 1057 | |
| 1058 | const uint flags = mo.d.data[handle + 4]; |
| 1059 | if (flags & MethodRevisioned) |
| 1060 | hasRevisionedMethods = true; |
| 1061 | |
| 1062 | visitString(mo.d.data[handle + 0]); // name |
| 1063 | visitString(mo.d.data[handle + 3]); // tag |
| 1064 | |
| 1065 | const int argc = mo.d.data[handle + 1]; |
| 1066 | const int paramIndex = mo.d.data[handle + 2]; |
| 1067 | |
| 1068 | fieldsForParameterData += argc * 2; // type and name |
| 1069 | fieldsForParameterData += 1; // + return type |
| 1070 | |
| 1071 | // return type + args |
| 1072 | for (int i = 0; i < 1 + argc; ++i) { |
| 1073 | // type name (maybe) |
| 1074 | visitTypeInfo(mo.d.data[paramIndex + i]); |
| 1075 | |
| 1076 | // parameter name |
| 1077 | if (i > 0) |
| 1078 | visitString(mo.d.data[paramIndex + argc + i]); |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | int fieldsForRevisions = 0; |
| 1083 | if (hasRevisionedMethods) |
| 1084 | fieldsForRevisions = methodCount; |
| 1085 | |
| 1086 | return methodCount * QMetaObjectPrivate::IntsPerMethod |
| 1087 | + fieldsForRevisions + fieldsForParameterData; |
| 1088 | } |
| 1089 | |
| 1090 | template <typename StringVisitor, typename TypeInfoVisitor> |
| 1091 | int visitProperties(const QMetaObject &mo, StringVisitor visitString, TypeInfoVisitor visitTypeInfo) |
| 1092 | { |
| 1093 | const QMetaObjectPrivate *const priv = reinterpret_cast<const QMetaObjectPrivate*>(mo.d.data); |
| 1094 | |
| 1095 | for (int i = 0; i < priv->propertyCount; ++i) { |
| 1096 | const int handle = priv->propertyData + i * QMetaObjectPrivate::IntsPerProperty; |
| 1097 | |
| 1098 | visitString(mo.d.data[handle]); // name |
| 1099 | visitTypeInfo(mo.d.data[handle + 1]); |
| 1100 | } |
| 1101 | |
| 1102 | return priv->propertyCount * QMetaObjectPrivate::IntsPerProperty; |
| 1103 | } |
| 1104 | |
| 1105 | template <typename StringVisitor> |
| 1106 | int visitClassInfo(const QMetaObject &mo, StringVisitor visitString) |
| 1107 | { |
| 1108 | const QMetaObjectPrivate *const priv = reinterpret_cast<const QMetaObjectPrivate*>(mo.d.data); |
| 1109 | const int intsPerClassInfo = 2; |
| 1110 | |
| 1111 | for (int i = 0; i < priv->classInfoCount; ++i) { |
| 1112 | const int handle = priv->classInfoData + i * intsPerClassInfo; |
| 1113 | |
| 1114 | visitString(mo.d.data[handle]); // key |
| 1115 | visitString(mo.d.data[handle + 1]); // value |
| 1116 | } |
| 1117 | |
| 1118 | return priv->classInfoCount * intsPerClassInfo; |
| 1119 | } |
| 1120 | |
| 1121 | template <typename StringVisitor> |
| 1122 | int visitEnumerations(const QMetaObject &mo, StringVisitor visitString) |
| 1123 | { |
| 1124 | const QMetaObjectPrivate *const priv = reinterpret_cast<const QMetaObjectPrivate*>(mo.d.data); |
| 1125 | |
| 1126 | int fieldCount = priv->enumeratorCount * QMetaObjectPrivate::IntsPerEnum; |
| 1127 | |
| 1128 | for (int i = 0; i < priv->enumeratorCount; ++i) { |
| 1129 | const uint *enumeratorData = mo.d.data + priv->enumeratorData + i * QMetaObjectPrivate::IntsPerEnum; |
| 1130 | |
| 1131 | const uint keyCount = enumeratorData[3]; |
| 1132 | fieldCount += keyCount * 2; |
| 1133 | |
| 1134 | visitString(enumeratorData[0]); // name |
| 1135 | visitString(enumeratorData[1]); // enum name |
| 1136 | |
| 1137 | const uint keyOffset = enumeratorData[4]; |
| 1138 | |
| 1139 | for (uint j = 0; j < keyCount; ++j) { |
| 1140 | visitString(mo.d.data[keyOffset + 2 * j]); |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | return fieldCount; |
| 1145 | } |
| 1146 | |
| 1147 | template <typename StringVisitor> |
| 1148 | int countMetaObjectFields(const QMetaObject &mo, StringVisitor stringVisitor) |
| 1149 | { |
| 1150 | const QMetaObjectPrivate *const priv = reinterpret_cast<const QMetaObjectPrivate*>(mo.d.data); |
| 1151 | |
| 1152 | const auto typeInfoVisitor = [&stringVisitor](uint typeInfo) { |
| 1153 | if (typeInfo & IsUnresolvedType) |
| 1154 | stringVisitor(typeInfo & TypeNameIndexMask); |
| 1155 | }; |
| 1156 | |
| 1157 | int fieldCount = MetaObjectPrivateFieldCount; |
| 1158 | |
| 1159 | fieldCount += visitMethods(mo, priv->methodData, priv->methodCount, stringVisitor, |
| 1160 | typeInfoVisitor); |
| 1161 | fieldCount += visitMethods(mo, priv->constructorData, priv->constructorCount, stringVisitor, |
| 1162 | typeInfoVisitor); |
| 1163 | |
| 1164 | fieldCount += visitProperties(mo, stringVisitor, typeInfoVisitor); |
| 1165 | fieldCount += visitClassInfo(mo, stringVisitor); |
| 1166 | fieldCount += visitEnumerations(mo, stringVisitor); |
| 1167 | |
| 1168 | return fieldCount; |
| 1169 | } |
| 1170 | |
| 1171 | } // anonymous namespace |
| 1172 | |
| 1173 | static_assert(QMetaObjectPrivate::OutputRevision == 12, "Check and adjust determineMetaObjectSizes" ); |
| 1174 | |
| 1175 | bool QQmlPropertyCache::determineMetaObjectSizes(const QMetaObject &mo, int *fieldCount, |
| 1176 | int *stringCount) |
| 1177 | { |
| 1178 | const QMetaObjectPrivate *priv = reinterpret_cast<const QMetaObjectPrivate*>(mo.d.data); |
| 1179 | if (priv->revision != QMetaObjectPrivate::OutputRevision) |
| 1180 | return false; |
| 1181 | |
| 1182 | uint highestStringIndex = 0; |
| 1183 | const auto stringIndexVisitor = [&highestStringIndex](uint index) { |
| 1184 | highestStringIndex = qMax(a: highestStringIndex, b: index); |
| 1185 | }; |
| 1186 | |
| 1187 | *fieldCount = countMetaObjectFields(mo, stringVisitor: stringIndexVisitor); |
| 1188 | *stringCount = highestStringIndex + 1; |
| 1189 | |
| 1190 | return true; |
| 1191 | } |
| 1192 | |
| 1193 | bool QQmlPropertyCache::addToHash(QCryptographicHash &hash, const QMetaObject &mo) |
| 1194 | { |
| 1195 | int fieldCount = 0; |
| 1196 | int stringCount = 0; |
| 1197 | if (!determineMetaObjectSizes(mo, fieldCount: &fieldCount, stringCount: &stringCount)) { |
| 1198 | return false; |
| 1199 | } |
| 1200 | |
| 1201 | hash.addData(data: {reinterpret_cast<const char *>(mo.d.data), qsizetype(fieldCount * sizeof(uint))}); |
| 1202 | for (int i = 0; i < stringCount; ++i) { |
| 1203 | hash.addData(data: stringData(mo: &mo, index: i)); |
| 1204 | } |
| 1205 | |
| 1206 | return true; |
| 1207 | } |
| 1208 | |
| 1209 | QByteArray QQmlPropertyCache::checksum(QHash<quintptr, QByteArray> *checksums, bool *ok) const |
| 1210 | { |
| 1211 | auto it = checksums->constFind(key: quintptr(this)); |
| 1212 | if (it != checksums->constEnd()) { |
| 1213 | *ok = true; |
| 1214 | return *it; |
| 1215 | } |
| 1216 | |
| 1217 | // Generate a checksum on the meta-object data only on C++ types. |
| 1218 | if (_metaObject.isShared()) { |
| 1219 | *ok = false; |
| 1220 | return QByteArray(); |
| 1221 | } |
| 1222 | |
| 1223 | QCryptographicHash hash(QCryptographicHash::Md5); |
| 1224 | |
| 1225 | if (_parent) { |
| 1226 | hash.addData(data: _parent->checksum(checksums, ok)); |
| 1227 | if (!*ok) |
| 1228 | return QByteArray(); |
| 1229 | } |
| 1230 | |
| 1231 | if (!addToHash(hash, mo: *_metaObject.metaObject())) { |
| 1232 | *ok = false; |
| 1233 | return QByteArray(); |
| 1234 | } |
| 1235 | |
| 1236 | const QByteArray result = hash.result(); |
| 1237 | if (result.isEmpty()) { |
| 1238 | *ok = false; |
| 1239 | } else { |
| 1240 | *ok = true; |
| 1241 | checksums->insert(key: quintptr(this), value: result); |
| 1242 | } |
| 1243 | return result; |
| 1244 | } |
| 1245 | |
| 1246 | /*! \internal |
| 1247 | \a index MUST be in the signal index range (see QObjectPrivate::signalIndex()). |
| 1248 | This is different from QMetaMethod::methodIndex(). |
| 1249 | */ |
| 1250 | QList<QByteArray> QQmlPropertyCache::signalParameterNames(int index) const |
| 1251 | { |
| 1252 | const QQmlPropertyData *signalData = signal(index); |
| 1253 | if (signalData && signalData->hasArguments()) { |
| 1254 | QQmlPropertyCacheMethodArguments *args = (QQmlPropertyCacheMethodArguments *)signalData->arguments(); |
| 1255 | if (args && args->names) |
| 1256 | return *args->names; |
| 1257 | const QMetaMethod &method = QMetaObjectPrivate::signal(m: firstCppMetaObject(), signal_index: index); |
| 1258 | return method.parameterNames(); |
| 1259 | } |
| 1260 | return QList<QByteArray>(); |
| 1261 | } |
| 1262 | |
| 1263 | QT_END_NAMESPACE |
| 1264 | |