Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlpropertycache.cpp
Go to the documentation of this file.
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// Qt-Security score:significant
4
6
7#include <private/qqmlengine_p.h>
8#include <private/qqmlbinding_p.h>
9#include <private/qqmlvmemetaobject_p.h>
10
11#include <private/qmetaobject_p.h>
12#include <private/qmetaobjectbuilder_p.h>
13#include <private/qqmlpropertycachemethodarguments_p.h>
14#include <private/qqmlsignalnames_p.h>
15
16#include <private/qv4codegen_p.h>
17#include <private/qv4value_p.h>
18
19#include <QtCore/qdebug.h>
20#include <QtCore/QCryptographicHash>
21#include <QtCore/private/qtools_p.h>
22
23#include <limits.h>
24#include <algorithm>
25
26#ifdef Q_CC_MSVC
27// nonstandard extension used : zero-sized array in struct/union.
28# pragma warning( disable : 4200 )
29#endif
30
31QT_BEGIN_NAMESPACE
32
33#define Q_INT16_MAX 32767
34
36namespace detail {
37
38static inline bool hasInvalidModifierCombintation(const QQmlPropertyData &overridingProperty)
39{
40 return (overridingProperty.isVirtual() && overridingProperty.isFinal())
41 || (overridingProperty.doesOverride() && overridingProperty.isFinal())
42 || (overridingProperty.isVirtual() && overridingProperty.doesOverride());
43}
44
45/*
46 * Performs minimal validation of property override semantics.
47 *
48 * This function checks whether an existing property can be overridden.
49 * It distinguishes between the following cases:
50 * - No base property exists → Status::NoOverride
51 * - Base property is marked final → Status::OverridingFinal
52 * - Otherwise → Status::Valid
53 *
54 * The minimal check is used in contexts where only basic inheritance
55 * constraints (existence and finality) must be verified.
56 */
57static inline Status checkMinimal(const QQmlPropertyData *const existingProperty)
58{
59 if (!existingProperty)
60 return Status::NoOverride;
61
62 if (existingProperty->isFinal()) {
64 }
65
66 return Status::Valid;
67}
68
69/*
70 * Performs full validation of property override semantics.
71 *
72 * This function enforces the full set of rules for `virtual`, `override`,
73 * and `final` keyword combinations when resolving property overrides.
74 * It verifies:
75 *
76 * - If `override` is specified but no base property exists,
77 * the override is invalid (Status::MissingBase).
78 *
79 * - If `override` is NOT specified and no base property exists,
80 * then there is no override (Status::NoOverride).
81 *
82 * - If the base property is final, overriding is not allowed
83 * (Status::OverridingFinal).
84 *
85 * - If the base property is invokable and overriding is not (and vice-versa),
86 * override is invalid (Status::InvokabilityMismatch).
87 *
88 * - If the base property is not virtual, but 'override' is present
89 * overriding is not allowed (Status::OverridingNonVirtualError),
90 * otherwise it returns Status::OverridingNonVirtual
91 *
92 * - If no `override` or `final` keyword is specified for an existing virtual base,
93 * the override specifier is missing (Status::MissingOverrideOrFinalSpecifier).
94 *
95 * Returns Status::Valid if the combination is semantically correct.
96 */
97static inline Status checkFull(const QQmlPropertyData &overridingProperty,
98 const QQmlPropertyData *const existingProperty)
99{
100 const auto overrideKeyword = overridingProperty.doesOverride();
101 if (overrideKeyword && !existingProperty) {
102 return Status::MissingBase;
103 }
104
105 const auto minimalCheckRes = checkMinimal(existingProperty);
106 if (minimalCheckRes != Status::Valid) {
107 return minimalCheckRes;
108 }
109
110 // if the property doesn't exist we should have returned MissingBase or NoOverride already
111 Q_ASSERT(existingProperty);
112 if (overridingProperty.isFunction() != existingProperty->isFunction()) {
114 }
115
116 if (!existingProperty->isVirtual()) {
117 return overrideKeyword ? Status::OverridingNonVirtualError
119 }
120
121 const auto overrideOrFinal = overrideKeyword || overridingProperty.isFinal();
122 if (!overrideOrFinal) {
124 }
125
126 return Status::Valid;
127}
128
129static inline Status check(const QQmlPropertyData &overridingProperty,
130 const QQmlPropertyData *const existingProperty, CheckMode mode)
131{
132 Q_ASSERT(!hasInvalidModifierCombintation(overridingProperty));
133
134 switch (mode) {
136 return detail::checkMinimal(existingProperty);
137 case CheckMode::Full:
138 return detail::checkFull(overridingProperty, existingProperty);
139 default:
140 Q_UNREACHABLE_RETURN(Status::Unknown);
141 }
142}
143} // namespace detail
144
145Status handleOverride(QQmlPropertyData &overridingProperty, QQmlPropertyData *existingProperty,
146 CheckMode mode)
147{
148 const auto status = detail::check(overridingProperty, existingProperty, mode);
149
150 if (isValidOverride(status)) {
151 overridingProperty.markAsOverrideOf(existingProperty);
152 }
153 return status;
154}
155
156} // namespace OverrideSemantics
157
158static int metaObjectSignalCount(const QMetaObject *metaObject)
159{
160 int signalCount = 0;
161 for (const QMetaObject *obj = metaObject; obj; obj = obj->superClass())
162 signalCount += QMetaObjectPrivate::get(obj)->signalCount;
163 return signalCount;
164}
165
166QQmlPropertyData::Flags
167QQmlPropertyData::flagsForProperty(const QMetaProperty &p)
168{
169 QQmlPropertyData::Flags flags;
170
171 flags.setIsConstant(p.isConstant());
172 flags.setIsWritable(p.isWritable());
173 flags.setIsResettable(p.isResettable());
174 flags.setIsFinal(p.isFinal());
175 flags.setIsVirtual(p.isVirtual());
176 flags.setDoesOverride(p.isOverride());
177 flags.setIsRequired(p.isRequired());
178 flags.setIsBindable(p.isBindable());
179
180
181 const QMetaType metaType = p.metaType();
182 int propType = metaType.id();
183 if (p.isEnumType()) {
184 flags.setType(QQmlPropertyData::Flags::EnumType);
185 } else if (metaType.flags() & QMetaType::PointerToQObject) {
186 flags.setType(QQmlPropertyData::Flags::QObjectDerivedType);
187 } else if (propType == QMetaType::QVariant) {
188 flags.setType(QQmlPropertyData::Flags::QVariantType);
189 } else if (metaType.flags() & QMetaType::IsQmlList) {
190 flags.setType(QQmlPropertyData::Flags::QListType);
191 }
192
193 return flags;
194}
195
196void QQmlPropertyData::load(const QMetaProperty &p)
197{
198 Q_ASSERT(p.revision() <= std::numeric_limits<quint16>::max());
199 setCoreIndex(p.propertyIndex());
200 setNotifyIndex(QMetaObjectPrivate::signalIndex(p.notifySignal()));
201 setFlags(flagsForProperty(p));
202 setRevision(QTypeRevision::fromEncodedVersion(p.revision()));
203 QMetaType type = p.metaType();
204 setPropType(type);
205}
206
207void QQmlPropertyData::load(const QMetaMethod &m)
208{
209 setCoreIndex(m.methodIndex());
210 m_flags.setType(Flags::FunctionType);
211
212 // We need to set the constructor, signal, constant, arguments, V4Function, cloned flags.
213 // These are specific to methods and change with each method.
214 // The same QQmlPropertyData may be loaded with multiple methods in sequence.
215
216 switch (m.methodType()) {
217 case QMetaMethod::Signal:
218 m_flags.setIsSignal(true);
219 m_flags.setIsConstructor(false);
220 setPropType(m.returnMetaType());
221 break;
222 case QMetaMethod::Constructor:
223 m_flags.setIsSignal(false);
224 m_flags.setIsConstructor(true);
225 break;
226 default:
227 m_flags.setIsSignal(false);
228 m_flags.setIsConstructor(false);
229 setPropType(m.returnMetaType());
230 break;
231 }
232
233 m_flags.setIsConstant(m.isConst());
234
235 const int paramCount = m.parameterCount();
236 if (paramCount) {
237 m_flags.setHasArguments(true);
238 m_flags.setIsV4Function(
239 paramCount == 1 &&
240 m.parameterMetaType(0) == QMetaType::fromType<QQmlV4FunctionPtr>());
241 } else {
242 m_flags.setHasArguments(false);
243 m_flags.setIsV4Function(false);
244 }
245
246 m_flags.setIsCloned(m.attributes() & QMetaMethod::Cloned);
247
248 Q_ASSERT(m.revision() <= std::numeric_limits<quint16>::max());
249 setRevision(QTypeRevision::fromEncodedVersion(m.revision()));
250}
251
252Q_LOGGING_CATEGORY(qqmlPropertyCacheAppend, "qt.qml.propertyCache.append", QtWarningMsg)
253
254/*!
255 \internal
256 Creates a standalone QQmlPropertyCache of \a metaObject. It is separate from the usual
257 QQmlPropertyCache hierarchy. It's parent is not equal to any other QQmlPropertyCache
258 created from QObject::staticMetaObject, for example.
259*/
260QQmlPropertyCache::Ptr QQmlPropertyCache::createStandalone(
261 const QMetaObject *metaObject, QTypeRevision metaObjectRevision)
262{
263 Q_ASSERT(metaObject);
264
265 Ptr result;
266 if (const QMetaObject *super = metaObject->superClass()) {
267 result = createStandalone(
268 super, metaObjectRevision)->copyAndAppend(metaObject, metaObjectRevision);
269 } else {
270 result.adopt(new QQmlPropertyCache(metaObject));
271 result->update(metaObject);
272 }
273
274 if (metaObjectRevision.isValid() && metaObjectRevision != QTypeRevision::zero()) {
275 // Set the revision of the meta object that this cache describes to be
276 // 'metaObjectRevision'. This is useful when constructing a property cache
277 // from a type that was created directly in C++, and not through QML. For such
278 // types, the revision for each recorded QMetaObject would normally be zero, which
279 // would exclude any revisioned properties.
280 for (int metaObjectOffset = 0; metaObjectOffset < result->allowedRevisionCache.size();
281 ++metaObjectOffset) {
282 result->allowedRevisionCache[metaObjectOffset] = metaObjectRevision;
283 }
284 }
285
286 return result;
287}
288
289QQmlPropertyCache::~QQmlPropertyCache()
290{
291 QQmlPropertyCacheMethodArguments *args = argumentsCache;
292 while (args) {
293 QQmlPropertyCacheMethodArguments *next = args->next;
294 delete args->names;
295 free(args);
296 args = next;
297 }
298
299 // We must clear this prior to releasing the parent incase it is a
300 // linked hash
301 stringCache.clear();
302}
303
304QQmlPropertyCache::Ptr QQmlPropertyCache::copy(const QQmlMetaObjectPointer &mo, int reserve) const
305{
306 QQmlPropertyCache::Ptr cache = QQmlPropertyCache::Ptr(
307 new QQmlPropertyCache(mo, _handleOverride), QQmlPropertyCache::Ptr::Adopt);
308 cache->_parent.reset(this);
309 cache->propertyIndexCacheStart = propertyIndexCache.size() + propertyIndexCacheStart;
310 cache->methodIndexCacheStart = methodIndexCache.size() + methodIndexCacheStart;
311 cache->signalHandlerIndexCacheStart = signalHandlerIndexCache.size() + signalHandlerIndexCacheStart;
312 cache->stringCache.linkAndReserve(stringCache, reserve);
313 cache->allowedRevisionCache = allowedRevisionCache;
314 cache->_defaultPropertyName = _defaultPropertyName;
315 cache->_listPropertyAssignBehavior = _listPropertyAssignBehavior;
316
317 return cache;
318}
319
320QQmlPropertyCache::Ptr QQmlPropertyCache::copy() const
321{
322 return copy(_metaObject, 0);
323}
324
325QQmlPropertyCache::Ptr QQmlPropertyCache::rebased(const ConstPtr &parent) const
326{
327 QQmlPropertyCache::Ptr cache = QQmlPropertyCache::Ptr(
328 new QQmlPropertyCache(parent->_metaObject, _handleOverride),
329 QQmlPropertyCache::Ptr::Adopt);
330
331 cache->_parent = parent;
332
333 cache->propertyIndexCacheStart = propertyIndexCacheStart;
334 cache->propertyIndexCache = propertyIndexCache;
335
336 cache->methodIndexCacheStart = methodIndexCacheStart;
337 cache->methodIndexCache = methodIndexCache;
338
339 cache->signalHandlerIndexCacheStart = signalHandlerIndexCacheStart;
340 cache->signalHandlerIndexCache = signalHandlerIndexCache;
341
342 cache->enumCache = enumCache;
343
344 cache->stringCache.linkAndReserve(
345 parent->stringCache, ownPropertyCount() + ownMethodCount() + ownSignalCount());
346
347 for (auto it = stringCache.begin(), end = stringCache.end(); it != end; ++it) {
348 const QQmlPropertyData *myData = it.value().second;
349 const int index = it.value().first;
350
351 QQmlPropertyData *copyData = nullptr;
352 if (myData->isSignalHandler())
353 copyData = cache->signalHandlerIndexCache.data() + (index - signalOffset());
354 else if (myData->isFunction())
355 copyData = cache->methodIndexCache.data() + (index - methodOffset());
356 else
357 copyData = cache->propertyIndexCache.data() + (index - propertyOffset());
358
359 cache->stringCache.insert(it.key(), std::make_pair(index, copyData));
360 }
361
362 cache->allowedRevisionCache = allowedRevisionCache;
363 cache->_dynamicStringData = _dynamicStringData;
364 cache->_dynamicClassName = _dynamicClassName;
365 cache->_defaultPropertyName = _defaultPropertyName;
366 cache->_listPropertyAssignBehavior = _listPropertyAssignBehavior;
367 return cache;
368}
369
370QQmlPropertyCache::Ptr QQmlPropertyCache::copyAndReserve(
371 int propertyCount, int methodCount, int signalCount, int enumCount) const
372{
373 QQmlPropertyCache::Ptr rv = copy(
374 QQmlMetaObjectPointer(), propertyCount + methodCount + signalCount);
375 rv->propertyIndexCache.reserve(propertyCount);
376 rv->methodIndexCache.reserve(methodCount);
377 rv->signalHandlerIndexCache.reserve(signalCount);
378 rv->enumCache.reserve(enumCount);
379 return rv;
380}
381
382QQmlPropertyCache::AppendResult
383QQmlPropertyCache::appendAlias(const QString &name, QQmlPropertyData::Flags flags, int coreIndex,
384 QMetaType propType, QTypeRevision version, int notifyIndex,
385 int encodedTargetIndex, int targetObjectId)
386{
387 QQmlPropertyData data;
388 data.setPropType(propType);
389 data.setCoreIndex(coreIndex);
390 data.setNotifyIndex(notifyIndex);
391 flags.setIsAlias(true);
392 data.setFlags(flags);
393 data.setAliasTarget(encodedTargetIndex);
394 data.setAliasTargetObjectId(targetObjectId);
395 data.setTypeVersion(version);
396
397 return appendPropertyAttr(name, std::move(data));
398}
399
400QQmlPropertyCache::AppendResult
401QQmlPropertyCache::appendComponentWrapper(int coreIndex, int wrappedObjectIndex)
402{
403 QQmlPropertyData data;
404 data.setCoreIndex(coreIndex);
405 QQmlPropertyData::Flags flags;
406 flags.setType(QQmlPropertyData::Flags::ComponentWrapperType);
407 data.setFlags(flags);
408 data.setWrappedObjectIndex(wrappedObjectIndex);
409
410 // Use a sentinel name so that defaultProperty() can find the wrapper.
411 // NB: We're not actually using the default property as default property. We only
412 // need some property to hold the wrapped object index.
413 _defaultPropertyName = QStringLiteral(".qt_component_wrapper__");
414 return appendPropertyAttr(_defaultPropertyName, std::move(data));
415}
416
417void QQmlPropertyCache::appendSignal(const QString &name, QQmlPropertyData::Flags flags,
418 int coreIndex, const QMetaType *types,
419 const QList<QByteArray> &names)
420{
421 QQmlPropertyData data;
422 data.setPropType(QMetaType());
423 data.setCoreIndex(coreIndex);
424 data.setFlags(flags);
425 data.setArguments(nullptr);
426
427 QQmlPropertyData handler = data;
428 handler.m_flags.setIsSignalHandler(true);
429
430 if (types) {
431 const auto argumentCount = names.size();
432 QQmlPropertyCacheMethodArguments *args = createArgumentsObject(argumentCount, names);
433 new (args->types) QMetaType; // Invalid return type
434 ::memcpy(args->types + 1, types, argumentCount * sizeof(QMetaType));
435 data.setArguments(args);
436 }
437
438 QQmlPropertyData *old = findNamedProperty(name);
439 const auto overrideStatus = _handleOverride(data, old, OverrideSemantics::CheckMode::Minimal);
440 maybeLog(overrideStatus, name);
441 // remove assert when checkMode is expanded and adjust handling correspondingly. For now it
442 // verifies that some code-path work in the same way as before introduction of virtual and
443 // override keywords
444 Q_ASSERT(overrideStatus == OverrideSemantics::Status::NoOverride
445 || overrideStatus == OverrideSemantics::Status::Valid
446 || overrideStatus == OverrideSemantics::Status::OverridingFinal);
447 if (overrideStatus == OverrideSemantics::Status::OverridingFinal) {
448 // TODO QTBUG-141728
449 // Insert the overridden member and its signal once more, to keep the counts in sync
450 methodIndexCache.append(*old);
451 handler = *old;
452 handler.m_flags.setIsSignalHandler(true);
453 signalHandlerIndexCache.append(handler);
454 return;
455 }
456
457 int methodIndex = methodIndexCache.size();
458 methodIndexCache.append(data);
459
460 int signalHandlerIndex = signalHandlerIndexCache.size();
461 signalHandlerIndexCache.append(handler);
462
463 const QString handlerName = QQmlSignalNames::signalNameToHandlerName(name);
464
465 setNamedProperty(name, methodIndex + methodOffset(), methodIndexCache.data() + methodIndex);
466 setNamedProperty(handlerName, signalHandlerIndex + signalOffset(),
467 signalHandlerIndexCache.data() + signalHandlerIndex);
468}
469
470void QQmlPropertyCache::appendMethod(const QString &name, QQmlPropertyData::Flags flags,
471 int coreIndex, QMetaType returnType,
472 const QList<QByteArray> &names,
473 const QList<QMetaType> &parameterTypes)
474{
475 int argumentCount = names.size();
476
477 QQmlPropertyData data;
478 data.setPropType(returnType);
479 data.setCoreIndex(coreIndex);
480 data.setFlags(flags);
481 QQmlPropertyData *old = findNamedProperty(name);
482 const auto overrideStatus = _handleOverride(data, old, OverrideSemantics::CheckMode::Minimal);
483 maybeLog(overrideStatus, name);
484 // remove assert when checkMode is expanded and adjust handling correspondingly. For now it
485 // verifies that some code-path work in the same way as before introduction of virtual and
486 // override keywords
487 Q_ASSERT(overrideStatus == OverrideSemantics::Status::NoOverride
488 || overrideStatus == OverrideSemantics::Status::Valid
489 || overrideStatus == OverrideSemantics::Status::OverridingFinal);
490 if (overrideStatus == OverrideSemantics::Status::OverridingFinal) {
491 // TODO QTBUG-141728
492 // Insert the overridden member once more, to keep the counts in sync
493 methodIndexCache.append(*old);
494 return;
495 }
496
497 QQmlPropertyCacheMethodArguments *args = createArgumentsObject(argumentCount, names);
498 new (args->types) QMetaType(returnType);
499 for (int ii = 0; ii < argumentCount; ++ii)
500 new (args->types + ii + 1) QMetaType(parameterTypes.at(ii));
501 data.setArguments(args);
502
503 int methodIndex = methodIndexCache.size();
504 methodIndexCache.append(data);
505
506 setNamedProperty(name, methodIndex + methodOffset(), methodIndexCache.data() + methodIndex);
507}
508
509void QQmlPropertyCache::appendEnum(const QString &name, const QList<QQmlEnumValue> &values)
510{
511 QQmlEnumData data;
512 data.name = name;
513 data.values = values;
514 enumCache.append(data);
515}
516
517// Returns this property cache's metaObject, creating it if necessary.
518const QMetaObject *QQmlPropertyCache::createMetaObject() const
519{
520 if (_metaObject.isNull()) {
521 QMetaObjectBuilder builder;
522 toMetaObjectBuilder(builder);
523 builder.setSuperClass(_parent->createMetaObject());
524 _metaObject.setSharedOnce(builder.toMetaObject());
525 }
526
527 return _metaObject.metaObject();
528}
529
530const QQmlPropertyData *QQmlPropertyCache::maybeUnresolvedProperty(int index) const
531{
532 if (index < 0 || index >= propertyCount())
533 return nullptr;
534
535 const QQmlPropertyData *rv = nullptr;
536 if (index < propertyIndexCacheStart)
537 return _parent->maybeUnresolvedProperty(index);
538 else
539 rv = const_cast<const QQmlPropertyData *>(&propertyIndexCache.at(index - propertyIndexCacheStart));
540 return rv;
541}
542
543const QQmlPropertyData *QQmlPropertyCache::defaultProperty() const
544{
545 return property(defaultPropertyName(), nullptr, nullptr);
546}
547
548void QQmlPropertyCache::setParent(QQmlPropertyCache::ConstPtr newParent)
549{
550 if (_parent != newParent)
551 _parent = std::move(newParent);
552}
553
554QQmlPropertyCache::Ptr
555QQmlPropertyCache::copyAndAppend(const QMetaObject *metaObject,
556 QTypeRevision typeVersion,
557 QQmlPropertyData::Flags propertyFlags,
558 QQmlPropertyData::Flags methodFlags,
559 QQmlPropertyData::Flags signalFlags) const
560{
561 Q_ASSERT(QMetaObjectPrivate::get(metaObject)->revision >= 4);
562
563 // Reserve enough space in the name hash for all the methods (including signals), all the
564 // signal handlers and all the properties. This assumes no name clashes, but this is the
565 // common case.
566 QQmlPropertyCache::Ptr rv = copy(
567 metaObject,
568 QMetaObjectPrivate::get(metaObject)->methodCount
569 + QMetaObjectPrivate::get(metaObject)->signalCount
570 + QMetaObjectPrivate::get(metaObject)->propertyCount);
571
572 rv->append(metaObject, typeVersion, propertyFlags, methodFlags, signalFlags);
573
574 return rv;
575}
576
577static QHashedString signalNameToHandlerName(const QHashedString &methodName)
578{
579 return QQmlSignalNames::signalNameToHandlerName(methodName);
580}
581
582static QHashedString signalNameToHandlerName(const QHashedCStringRef &methodName)
583{
584 return QQmlSignalNames::signalNameToHandlerName(
585 QLatin1StringView{ methodName.constData(), methodName.length() });
586}
587
588static inline std::pair<bool, int> deriveEncodingAndLength(const char *str)
589{
590 char utf8 = 0;
591 const char *cptr = str;
592 while (*cptr != 0) {
593 utf8 |= *cptr & 0x80;
594 ++cptr;
595 }
596 return std::make_pair(utf8, cptr - str);
597}
598
599void QQmlPropertyCache::append(const QMetaObject *metaObject,
600 QTypeRevision typeVersion,
601 QQmlPropertyData::Flags propertyFlags,
602 QQmlPropertyData::Flags methodFlags,
603 QQmlPropertyData::Flags signalFlags)
604{
605 allowedRevisionCache.append(QTypeRevision::zero());
606
607 int methodCount = metaObject->methodCount();
608 Q_ASSERT(QMetaObjectPrivate::get(metaObject)->revision >= 4);
609 int signalCount = metaObjectSignalCount(metaObject);
610 int classInfoCount = QMetaObjectPrivate::get(metaObject)->classInfoCount;
611
612 if (classInfoCount) {
613 int classInfoOffset = metaObject->classInfoOffset();
614 for (int ii = 0; ii < classInfoCount; ++ii) {
615 int idx = ii + classInfoOffset;
616 QMetaClassInfo mci = metaObject->classInfo(idx);
617 const char *name = mci.name();
618 if (0 == qstrcmp(name, "DefaultProperty")) {
619 _defaultPropertyName = QString::fromUtf8(mci.value());
620 } else if (0 == qstrcmp(name, "qt_QmlJSWrapperFactoryMethod")) {
621 const char * const factoryMethod = mci.value();
622 _jsFactoryMethodIndex = metaObject->indexOfSlot(factoryMethod);
623 if (_jsFactoryMethodIndex != -1)
624 _jsFactoryMethodIndex -= metaObject->methodOffset();
625 } else if (0 == qstrcmp(name, "QML.ListPropertyAssignBehavior")) {
626 _listPropertyAssignBehavior = mci.value();
627 }
628 }
629 }
630
631 //Used to block access to QObject::destroyed() and QObject::deleteLater() from QML
632 static const int destroyedIdx1 = QObject::staticMetaObject.indexOfSignal("destroyed(QObject*)");
633 static const int destroyedIdx2 = QObject::staticMetaObject.indexOfSignal("destroyed()");
634 static const int deleteLaterIdx = QObject::staticMetaObject.indexOfSlot("deleteLater()");
635 // These indices don't apply to gadgets, so don't block them.
636 // It is enough to check for QObject::staticMetaObject here because the loop below excludes
637 // methods of parent classes: It starts at metaObject->methodOffset()
638 const bool preventDestruction = (metaObject == &QObject::staticMetaObject);
639
640 int methodOffset = metaObject->methodOffset();
641 int signalOffset = signalCount - QMetaObjectPrivate::get(metaObject)->signalCount;
642
643 // update() should have reserved enough space in the vector that this doesn't cause a realloc
644 // and invalidate the stringCache.
645 methodIndexCache.resize(methodCount - methodIndexCacheStart);
646 signalHandlerIndexCache.resize(signalCount - signalHandlerIndexCacheStart);
647 int signalHandlerIndex = signalOffset;
648 for (int ii = methodOffset; ii < methodCount; ++ii) {
649 if (preventDestruction && (ii == destroyedIdx1 || ii == destroyedIdx2 || ii == deleteLaterIdx))
650 continue;
651 const QMetaMethod &m = metaObject->method(ii);
652 if (m.access() == QMetaMethod::Private)
653 continue;
654
655 // Extract method name
656 // It's safe to keep the raw name pointer
657 Q_ASSERT(QMetaObjectPrivate::get(metaObject)->revision >= 7);
658
659 QQmlPropertyData *data = &methodIndexCache[ii - methodIndexCacheStart];
660 QQmlPropertyData *sigdata = nullptr;
661
662 if (m.methodType() == QMetaMethod::Signal)
663 data->setFlags(signalFlags);
664 else
665 data->setFlags(methodFlags);
666
667 data->load(m);
668
669 Q_ASSERT((allowedRevisionCache.size() - 1) < Q_INT16_MAX);
670 data->setMetaObjectOffset(allowedRevisionCache.size() - 1);
671
672 if (data->isSignal()) {
673 sigdata = &signalHandlerIndexCache[signalHandlerIndex - signalHandlerIndexCacheStart];
674 *sigdata = *data;
675 sigdata->m_flags.setIsSignalHandler(true);
676 }
677
678 const auto doSetNamedProperty = [&](const auto &methodName) {
679 QQmlPropertyData *old = nullptr;
680 if (StringCache::mapped_type *it = stringCache.value(methodName)) {
681 const auto overrideStatus = _handleOverride(*data, (old = it->second),
682 OverrideSemantics::CheckMode::Minimal);
683 maybeLog(overrideStatus, methodName);
684 // remove assert when checkMode is expanded and adjust handling correspondingly. For
685 // now it verifies that some code-path work in the same way as before introduction
686 // of virtual and override keywords
687 Q_ASSERT(overrideStatus == OverrideSemantics::Status::NoOverride
688 || overrideStatus == OverrideSemantics::Status::Valid
689 || overrideStatus == OverrideSemantics::Status::OverridingFinal);
690 if (overrideStatus == OverrideSemantics::Status::OverridingFinal) {
691 // TODO QTBUG-141728
692 *data = *old;
693 if (sigdata) {
694 // Keep the signal counts in sync,
695 // even if the "old" data has no real signal.
696 *sigdata = *old;
697 sigdata->m_flags.setIsSignalHandler(true);
698 ++signalHandlerIndex;
699 }
700 return;
701 }
702 }
703
704 setNamedProperty(methodName, ii, data);
705
706 if (data->isSignal()) {
707
708 // TODO: Remove this once we can. Signals should not be overridable.
709 if constexpr (std::is_same_v<std::decay_t<decltype(methodName)>, QHashedCStringRef>)
710 data->m_flags.setIsOverridableSignal(true);
711
712 setNamedProperty(signalNameToHandlerName(methodName), ii, sigdata);
713 ++signalHandlerIndex;
714 }
715 };
716
717 const char *str = m.nameView().constData();
718 const auto [isUtf8, len] = deriveEncodingAndLength(str);
719 if (isUtf8)
720 doSetNamedProperty(QHashedString(QString::fromUtf8(str, len)));
721 else
722 doSetNamedProperty(QHashedCStringRef(str, len));
723 }
724
725 int propCount = metaObject->propertyCount();
726 int propOffset = metaObject->propertyOffset();
727
728 // update() should have reserved enough space in the vector that this doesn't cause a realloc
729 // and invalidate the stringCache.
730 propertyIndexCache.resize(propCount - propertyIndexCacheStart);
731 for (int ii = propOffset; ii < propCount; ++ii) {
732 QMetaProperty p = metaObject->property(ii);
733 if (!p.isScriptable())
734 continue;
735
736 // TODO QTBUG-141728
737 QQmlPropertyData *data = &propertyIndexCache[ii - propertyIndexCacheStart];
738
739 data->setFlags(propertyFlags);
740 data->load(p);
741 data->setTypeVersion(typeVersion);
742
743 Q_ASSERT((allowedRevisionCache.size() - 1) < Q_INT16_MAX);
744 data->setMetaObjectOffset(allowedRevisionCache.size() - 1);
745
746 const auto doSetNamedProperty = [this](const auto &propName, int index, auto *propData) {
747 QQmlPropertyData *existingPropData = findNamedProperty(propName);
748 const auto overrideStatus = _handleOverride(*propData, existingPropData,
749 OverrideSemantics::CheckMode::Full);
750 maybeLog(overrideStatus, propName);
751 if (!OverrideSemantics::isValidOverride(overrideStatus)) {
752 if (existingPropData) {
753 // TODO QTBUG-141728
754 *propData = *existingPropData;
755 }
756 return;
757 }
758
759 setNamedProperty(propName, index, propData);
760 };
761
762 const char *str = p.name();
763 const auto [isUtf8, len] = deriveEncodingAndLength(str);
764 if (isUtf8)
765 doSetNamedProperty(QHashedString(QString::fromUtf8(str, len)), ii, data);
766 else
767 doSetNamedProperty(QHashedCStringRef(str, len), ii, data);
768
769 bool isGadget = true;
770 for (const QMetaObject *it = metaObject; it != nullptr; it = it->superClass()) {
771 if (it == &QObject::staticMetaObject)
772 isGadget = false;
773 }
774
775 // otherwise always dispatch over a 'normal' meta-call so the QQmlValueType can intercept
776 if (!isGadget && !data->isAlias())
777 data->trySetStaticMetaCallFunction(metaObject->d.static_metacall, ii - propOffset);
778 }
779}
780
781void QQmlPropertyCache::update(const QMetaObject *metaObject)
782{
783 Q_ASSERT(metaObject);
784 stringCache.clear();
785
786 // Preallocate enough space in the index caches for all the properties/methods/signals that
787 // are not cached in a parent cache so that the caches never need to be reallocated as this
788 // would invalidate pointers stored in the stringCache.
789 int pc = metaObject->propertyCount();
790 int mc = metaObject->methodCount();
791 int sc = metaObjectSignalCount(metaObject);
792 propertyIndexCache.reserve(pc - propertyIndexCacheStart);
793 methodIndexCache.reserve(mc - methodIndexCacheStart);
794 signalHandlerIndexCache.reserve(sc - signalHandlerIndexCacheStart);
795
796 // Reserve enough space in the stringCache for all properties/methods/signals including those
797 // cached in a parent cache.
798 stringCache.reserve(pc + mc + sc);
799
800 if (metaObject)
801 append(metaObject, QTypeRevision());
802}
803
804/*! \internal
805 invalidates and updates the PropertyCache if the QMetaObject has changed.
806 This function is used in the tooling to update dynamic properties.
807*/
808void QQmlPropertyCache::invalidate(const QMetaObject *metaObject)
809{
810 propertyIndexCache.clear();
811 methodIndexCache.clear();
812 signalHandlerIndexCache.clear();
813
814 argumentsCache = nullptr;
815
816 int pc = metaObject->propertyCount();
817 int mc = metaObject->methodCount();
818 int sc = metaObjectSignalCount(metaObject);
819 int reserve = pc + mc + sc;
820
821 if (parent()) {
822 propertyIndexCacheStart = parent()->propertyIndexCache.size() + parent()->propertyIndexCacheStart;
823 methodIndexCacheStart = parent()->methodIndexCache.size() + parent()->methodIndexCacheStart;
824 signalHandlerIndexCacheStart = parent()->signalHandlerIndexCache.size() + parent()->signalHandlerIndexCacheStart;
825 stringCache.linkAndReserve(parent()->stringCache, reserve);
826 append(metaObject, QTypeRevision());
827 } else {
828 propertyIndexCacheStart = 0;
829 methodIndexCacheStart = 0;
830 signalHandlerIndexCacheStart = 0;
831 update(metaObject);
832 }
833}
834
835const QQmlPropertyData *QQmlPropertyCache::findProperty(
836 StringCache::ConstIterator it, QObject *object,
837 const QQmlRefPointer<QQmlContextData> &context) const
838{
839 QQmlData *data = (object ? QQmlData::get(object) : nullptr);
840 const QQmlVMEMetaObject *vmemo = nullptr;
841 if (data && data->hasVMEMetaObject) {
842 QObjectPrivate *op = QObjectPrivate::get(object);
843 vmemo = static_cast<const QQmlVMEMetaObject *>(op->metaObject);
844 }
845 return findProperty(it, vmemo, context);
846}
847
848namespace {
849
850inline bool contextHasNoExtensions(const QQmlRefPointer<QQmlContextData> &context)
851{
852 // This context has no extension if its parent is the engine's rootContext,
853 // which has children but no imports
854 const QQmlRefPointer<QQmlContextData> parent = context->parent();
855 return (!parent || !parent->imports());
856}
857
858inline int maximumIndexForProperty(const QQmlPropertyData *prop, const int methodCount, const int signalCount, const int propertyCount)
859{
860 return prop->isFunction() ? methodCount
861 : prop->isSignalHandler() ? signalCount
862 : propertyCount;
863}
864
865}
866
867const QQmlPropertyData *QQmlPropertyCache::findProperty(
868 StringCache::ConstIterator it, const QQmlVMEMetaObject *vmemo,
869 const QQmlRefPointer<QQmlContextData> &context) const
870{
871 StringCache::ConstIterator end = stringCache.end();
872
873 if (it != end) {
874 const QQmlPropertyData *result = it.value().second;
875
876 // If there exists a typed property (not a function or signal handler), of the
877 // right name available to the specified context, we need to return that
878 // property rather than any subsequent override
879
880 if (vmemo && context && !contextHasNoExtensions(context)) {
881 // Find the meta-object that corresponds to the supplied context
882 do {
883 if (vmemo->contextData() == context)
884 break;
885
886 vmemo = vmemo->parentVMEMetaObject();
887 } while (vmemo);
888 }
889
890 if (vmemo) {
891 const int methodCount = vmemo->cache->methodCount();
892 const int signalCount = vmemo->cache->signalCount();
893 const int propertyCount = vmemo->cache->propertyCount();
894
895 // Ensure that the property we resolve to is accessible from this meta-object
896 do {
897 const StringCache::mapped_type &property(it.value());
898
899 if (property.first < maximumIndexForProperty(property.second, methodCount, signalCount, propertyCount)) {
900 // This property is available in the specified context
901 if (property.second->isFunction() || property.second->isSignalHandler()) {
902 // Prefer the earlier resolution
903 } else {
904 // Prefer the typed property to any previous property found
905 result = property.second;
906 }
907 break;
908 }
909
910 // See if there is a better candidate
911 it = stringCache.findNext(it);
912 } while (it != end);
913 }
914
915 return result;
916 }
917
918 return nullptr;
919}
920
921// Note, this function is called when adding aliases, hence data.isEnum() can possibly be true
922QQmlPropertyCache::AppendResult QQmlPropertyCache::appendPropertyAttr(const QString &name,
923 QQmlPropertyData &&data)
924{
925 QQmlPropertyData *old = findNamedProperty(name);
926 const auto overrideStatus = _handleOverride(data, old, OverrideSemantics::CheckMode::Full);
927 maybeLog(overrideStatus, name);
928 if (!OverrideSemantics::isValidOverride(overrideStatus)) {
929 // TODO QTBUG-141728
930 // Insert the overridden member once more, to keep the counts in sync
931 propertyIndexCache.append(old ? *old : data);
932 return q23::make_unexpected(overrideStatus);
933 }
934
935 const int index = propertyIndexCache.size();
936 propertyIndexCache.append(std::move(data));
937
938 setNamedProperty(name, index + propertyOffset(), propertyIndexCache.data() + index);
939 return {};
940}
941
942void QQmlPropertyData::markAsOverrideOf(QQmlPropertyData *predecessor)
943{
944 Q_ASSERT(predecessor != this);
945
946 if (!predecessor) {
947 return;
948 }
949
950 setOverrideIndexIsProperty(!predecessor->isFunction());
951 setOverrideIndex(predecessor->coreIndex());
952 // propagate "virtuality"
953 m_flags.setIsVirtual(predecessor->isVirtual());
954 predecessor->m_flags.setIsOverridden(true);
955 Q_ASSERT(predecessor->isOverridden());
956 return;
957}
958
959QQmlPropertyCacheMethodArguments *QQmlPropertyCache::createArgumentsObject(
960 int argc, const QList<QByteArray> &names)
961{
962 typedef QQmlPropertyCacheMethodArguments A;
963 A *args = static_cast<A *>(malloc(sizeof(A) + argc * sizeof(QMetaType)));
964 args->names = argc ? new QList<QByteArray>(names) : nullptr;
965 args->next = argumentsCache;
966 argumentsCache = args;
967 return args;
968}
969
970QString QQmlPropertyCache::signalParameterStringForJS(
971 const QList<QByteArray> &parameterNameList, QString *errorString)
972{
973 bool unnamedParameter = false;
974 QString parameters;
975
976 const qsizetype count = parameterNameList.size();
977 if (count > std::numeric_limits<quint16>::max())
978 *errorString = QCoreApplication::translate("QQmlRewrite", "Signal has an excessive number of parameters: %1").arg(count);
979
980 for (qsizetype i = 0; i < count; ++i) {
981 if (i > 0)
982 parameters += QLatin1Char(',');
983 const QByteArray &param = parameterNameList.at(i);
984 if (param.isEmpty()) {
985 unnamedParameter = true;
986 } else if (unnamedParameter) {
987 if (errorString)
988 *errorString = QCoreApplication::translate("QQmlRewrite", "Signal uses unnamed parameter followed by named parameter.");
989 return QString();
990 } else if (QV4::Compiler::Codegen::isNameGlobal(param)) {
991 if (errorString)
992 *errorString = QCoreApplication::translate("QQmlRewrite", "Signal parameter \"%1\" hides global variable.").arg(QString::fromUtf8(param));
993 return QString();
994 }
995 parameters += QString::fromUtf8(param);
996 }
997
998 return parameters;
999}
1000
1001int QQmlPropertyCache::originalClone(int index) const
1002{
1003 while (signal(index)->isCloned())
1004 --index;
1005 return index;
1006}
1007
1008int QQmlPropertyCache::originalClone(const QObject *object, int index)
1009{
1010 QQmlData *data = QQmlData::get(object);
1011 if (data && data->propertyCache) {
1012 const QQmlPropertyCache *cache = data->propertyCache.data();
1013 const QQmlPropertyData *sig = cache->signal(index);
1014 while (sig && sig->isCloned()) {
1015 --index;
1016 sig = cache->signal(index);
1017 }
1018 } else {
1019 while (QMetaObjectPrivate::signal(object->metaObject(), index).attributes() & QMetaMethod::Cloned)
1020 --index;
1021 }
1022 return index;
1023}
1024
1025template<typename T>
1026static QQmlPropertyData qQmlPropertyCacheCreate(const QMetaObject *metaObject, const T& propertyName)
1027{
1028 Q_ASSERT(metaObject);
1029 static_assert(std::is_same_v<T, QByteArray>);
1030
1031 QQmlPropertyData rv;
1032
1033 /* It's important to check the method list before checking for properties;
1034 * otherwise, if the meta object is dynamic, a property will be created even
1035 * if not found and it might obscure a method having the same name. */
1036
1037 //Used to block access to QObject::destroyed() and QObject::deleteLater() from QML
1038 static const int destroyedIdx1 = QObject::staticMetaObject.indexOfSignal("destroyed(QObject*)");
1039 static const int destroyedIdx2 = QObject::staticMetaObject.indexOfSignal("destroyed()");
1040 static const int deleteLaterIdx = QObject::staticMetaObject.indexOfSlot("deleteLater()");
1041 // These indices don't apply to gadgets, so don't block them.
1042 const bool preventDestruction = metaObject->superClass() || metaObject == &QObject::staticMetaObject;
1043
1044 int methodCount = metaObject->methodCount();
1045 for (int ii = methodCount - 1; ii >= 0; --ii) {
1046 if (preventDestruction && (ii == destroyedIdx1 || ii == destroyedIdx2 || ii == deleteLaterIdx))
1047 continue;
1048 QMetaMethod m = metaObject->method(ii);
1049 if (m.access() == QMetaMethod::Private)
1050 continue;
1051
1052 if (m.name() == propertyName) {
1053 rv.load(m);
1054 return rv;
1055 }
1056 }
1057
1058 {
1059 const QMetaObject *cmo = metaObject;
1060 while (cmo) {
1061 int idx = cmo->indexOfProperty(propertyName.data());
1062 if (idx != -1) {
1063 QMetaProperty p = cmo->property(idx);
1064 if (p.isScriptable()) {
1065 rv.load(p);
1066 return rv;
1067 } else {
1068 bool changed = false;
1069 while (cmo && cmo->propertyOffset() >= idx) {
1070 cmo = cmo->superClass();
1071 changed = true;
1072 }
1073 /* If the "cmo" variable didn't change, set it to 0 to
1074 * avoid running into an infinite loop */
1075 if (!changed) cmo = nullptr;
1076 }
1077 } else {
1078 cmo = nullptr;
1079 }
1080 }
1081 }
1082 return rv;
1083}
1084
1085static inline QByteArray qQmlPropertyCacheToString(QLatin1String string)
1086{
1087 return string.toUtf8();
1088}
1089
1090static inline QByteArray qQmlPropertyCacheToString(QStringView string)
1091{
1092 return string.toUtf8();
1093}
1094
1095static inline QByteArray qQmlPropertyCacheToString(const QV4::String *string)
1096{
1097 return string->toQString().toUtf8();
1098}
1099
1100template<typename T>
1101const QQmlPropertyData *
1102qQmlPropertyCacheProperty(QObject *obj, T name, const QQmlRefPointer<QQmlContextData> &context,
1103 QQmlPropertyData *local)
1104{
1105 const QQmlPropertyCache *cache = nullptr;
1106
1107 QQmlData *ddata = QQmlData::get(obj, false);
1108
1109 if (ddata && ddata->propertyCache) {
1110 cache = ddata->propertyCache.data();
1111 } else if (auto newCache = QQmlMetaType::propertyCache(obj)) {
1112 cache = newCache.data();
1113 ddata = QQmlData::get(obj, true);
1114 ddata->propertyCache = std::move(newCache);
1115 }
1116
1117 const QQmlPropertyData *rv = nullptr;
1118
1119 if (cache) {
1120 rv = cache->property(name, obj, context);
1121 } else if (local) {
1122 *local = qQmlPropertyCacheCreate(obj->metaObject(), qQmlPropertyCacheToString(name));
1123 if (local->isValid())
1124 rv = local;
1125 }
1126
1127 return rv;
1128}
1129
1130const QQmlPropertyData *QQmlPropertyCache::property(
1131 QObject *obj, const QV4::String *name, const QQmlRefPointer<QQmlContextData> &context,
1132 QQmlPropertyData *local)
1133{
1134 return qQmlPropertyCacheProperty<const QV4::String *>(obj, name, context, local);
1135}
1136
1137const QQmlPropertyData *QQmlPropertyCache::property(
1138 QObject *obj, QStringView name, const QQmlRefPointer<QQmlContextData> &context,
1139 QQmlPropertyData *local)
1140{
1141 return qQmlPropertyCacheProperty<const QStringView &>(obj, name, context, local);
1142}
1143
1144const QQmlPropertyData *QQmlPropertyCache::property(
1145 QObject *obj, const QLatin1String &name, const QQmlRefPointer<QQmlContextData> &context,
1146 QQmlPropertyData *local)
1147{
1148 return qQmlPropertyCacheProperty<const QLatin1String &>(obj, name, context, local);
1149}
1150
1151// this function is copied from qmetaobject.cpp
1152static inline const QByteArray stringData(const QMetaObject *mo, int index)
1153{
1154 uint offset = mo->d.stringdata[2*index];
1155 uint length = mo->d.stringdata[2*index + 1];
1156 const char *string = reinterpret_cast<const char *>(mo->d.stringdata) + offset;
1157 return QByteArray::fromRawData(string, length);
1158}
1159
1160const char *QQmlPropertyCache::className() const
1161{
1162 if (const QMetaObject *mo = _metaObject.metaObject())
1163 return mo->className();
1164 else
1165 return _dynamicClassName.constData();
1166}
1167
1168void QQmlPropertyCache::toMetaObjectBuilder(QMetaObjectBuilder &builder) const
1169{
1170 struct Sort { static bool lt(const std::pair<QString, const QQmlPropertyData *> &lhs,
1171 const std::pair<QString, const QQmlPropertyData *> &rhs) {
1172 return lhs.second->coreIndex() < rhs.second->coreIndex();
1173 } };
1174
1175 struct Insert { static void in(const QQmlPropertyCache *This,
1176 QList<std::pair<QString, const QQmlPropertyData *> > &properties,
1177 QList<std::pair<QString, const QQmlPropertyData *> > &methods,
1178 StringCache::ConstIterator iter, const QQmlPropertyData *data) {
1179 if (data->isSignalHandler())
1180 return;
1181
1182 if (data->isFunction()) {
1183 if (data->coreIndex() < This->methodIndexCacheStart)
1184 return;
1185
1186 std::pair<QString, const QQmlPropertyData *> entry = std::make_pair((QString)iter.key(), data);
1187 // Overrides can cause the entry to already exist
1188 if (!methods.contains(entry)) methods.append(entry);
1189
1190 data = This->overrideData(data);
1191 if (data && !data->isFunction()) Insert::in(This, properties, methods, iter, data);
1192 } else {
1193 if (data->coreIndex() < This->propertyIndexCacheStart)
1194 return;
1195
1196 std::pair<QString, const QQmlPropertyData *> entry = std::make_pair((QString)iter.key(), data);
1197 // Overrides can cause the entry to already exist
1198 if (!properties.contains(entry)) properties.append(entry);
1199
1200 data = This->overrideData(data);
1201 if (data) Insert::in(This, properties, methods, iter, data);
1202 }
1203
1204 } };
1205
1206 builder.setClassName(_dynamicClassName);
1207
1208 QList<std::pair<QString, const QQmlPropertyData *> > properties;
1209 QList<std::pair<QString, const QQmlPropertyData *> > methods;
1210
1211 for (StringCache::ConstIterator iter = stringCache.begin(), cend = stringCache.end(); iter != cend; ++iter)
1212 Insert::in(this, properties, methods, iter, iter.value().second);
1213
1214 // Any invalid overrides are not linked by name into the properties and methods hashes.
1215 // Therefore there can be more properties and methods than present in the hashes.
1216 Q_ASSERT(properties.size() <= propertyIndexCache.size());
1217 Q_ASSERT(methods.size() <= methodIndexCache.size());
1218
1219 std::sort(properties.begin(), properties.end(), Sort::lt);
1220 std::sort(methods.begin(), methods.end(), Sort::lt);
1221
1222 for (int ii = 0; ii < properties.size(); ++ii) {
1223 const QQmlPropertyData *data = properties.at(ii).second;
1224
1225 int notifierId = -1;
1226 if (data->notifyIndex() != -1)
1227 notifierId = data->notifyIndex() - signalHandlerIndexCacheStart;
1228
1229 QMetaPropertyBuilder property = builder.addProperty(properties.at(ii).first.toUtf8(),
1230 data->propType().name(),
1231 data->propType(),
1232 notifierId);
1233
1234 property.setReadable(true);
1235 property.setWritable(data->isWritable());
1236 property.setResettable(data->isResettable());
1237 property.setBindable(data->notifiesViaBindable());
1238 property.setAlias(data->isAlias());
1239 }
1240
1241 for (int ii = 0; ii < methods.size(); ++ii) {
1242 const QQmlPropertyData *data = methods.at(ii).second;
1243
1244 QByteArray returnType;
1245 if (data->propType().isValid())
1246 returnType = data->propType().name();
1247
1248 QByteArray signature;
1249 // '+=' reserves extra capacity. Follow-up appending will be probably free.
1250 signature += methods.at(ii).first.toUtf8() + '(';
1251
1252 QQmlPropertyCacheMethodArguments *arguments = nullptr;
1253 if (data->hasArguments()) {
1254 arguments = data->arguments();
1255 for (int ii = 0, end = arguments->names ? arguments->names->size() : 0;
1256 ii < end; ++ii) {
1257 if (ii != 0)
1258 signature.append(',');
1259 signature.append(arguments->types[1 + ii].name());
1260 }
1261 }
1262
1263 signature.append(')');
1264
1265 QMetaMethodBuilder method;
1266 if (data->isSignal()) {
1267 method = builder.addSignal(signature);
1268 } else {
1269 method = builder.addSlot(signature);
1270 }
1271 method.setAccess(QMetaMethod::Public);
1272
1273 if (arguments && arguments->names)
1274 method.setParameterNames(*arguments->names);
1275
1276 if (!returnType.isEmpty())
1277 method.setReturnType(returnType);
1278 }
1279
1280 for (int ii = 0; ii < enumCache.size(); ++ii) {
1281 const QQmlEnumData &enumData = enumCache.at(ii);
1282 QMetaEnumBuilder enumeration = builder.addEnumerator(enumData.name.toUtf8());
1283 enumeration.setIsScoped(true);
1284 for (int jj = 0; jj < enumData.values.size(); ++jj) {
1285 const QQmlEnumValue &value = enumData.values.at(jj);
1286 enumeration.addKey(value.namedValue.toUtf8(), value.value);
1287 }
1288 }
1289
1290 if (!_defaultPropertyName.isEmpty()) {
1291 const QQmlPropertyData *dp = property(_defaultPropertyName, nullptr, nullptr);
1292 if (dp && dp->coreIndex() >= propertyIndexCacheStart) {
1293 Q_ASSERT(!dp->isFunction());
1294 builder.addClassInfo("DefaultProperty", _defaultPropertyName.toUtf8());
1295 }
1296 }
1297
1298 if (!_listPropertyAssignBehavior.isEmpty())
1299 builder.addClassInfo("QML.ListPropertyAssignBehavior", _listPropertyAssignBehavior);
1300}
1301
1302namespace {
1303template <typename StringVisitor, typename TypeInfoVisitor>
1304int visitMethods(const QMetaObject &mo, int methodOffset, int methodCount,
1305 StringVisitor visitString, TypeInfoVisitor visitTypeInfo)
1306{
1307 int fieldsForParameterData = 0;
1308
1309 bool hasOldStyleRevisionedMethods = false;
1310
1311 for (int i = 0; i < methodCount; ++i) {
1312 const int handle = methodOffset + i * QMetaObjectPrivate::IntsPerMethod;
1313
1314 const uint flags = mo.d.data[handle + 4];
1315 if (flags & MethodRevisioned) {
1316 if (mo.d.data[0] < 13)
1317 hasOldStyleRevisionedMethods = true;
1318 else
1319 fieldsForParameterData += 1; // revision
1320 }
1321
1322 visitString(mo.d.data[handle + 0]); // name
1323 visitString(mo.d.data[handle + 3]); // tag
1324
1325 const int argc = mo.d.data[handle + 1];
1326 const int paramIndex = mo.d.data[handle + 2];
1327
1328 fieldsForParameterData += argc * 2; // type and name
1329 fieldsForParameterData += 1; // + return type
1330
1331 // return type + args
1332 for (int i = 0; i < 1 + argc; ++i) {
1333 // type name (maybe)
1334 visitTypeInfo(mo.d.data[paramIndex + i]);
1335
1336 // parameter name
1337 if (i > 0)
1338 visitString(mo.d.data[paramIndex + argc + i]);
1339 }
1340 }
1341
1342 int fieldsForRevisions = 0;
1343 if (hasOldStyleRevisionedMethods)
1344 fieldsForRevisions = methodCount;
1345
1346 return methodCount * QMetaObjectPrivate::IntsPerMethod
1347 + fieldsForRevisions + fieldsForParameterData;
1348}
1349
1350template <typename StringVisitor, typename TypeInfoVisitor>
1351int visitProperties(const QMetaObject &mo, StringVisitor visitString, TypeInfoVisitor visitTypeInfo)
1352{
1353 const QMetaObjectPrivate *const priv = reinterpret_cast<const QMetaObjectPrivate*>(mo.d.data);
1354
1355 for (int i = 0; i < priv->propertyCount; ++i) {
1356 const int handle = priv->propertyData + i * QMetaObjectPrivate::IntsPerProperty;
1357
1358 visitString(mo.d.data[handle]); // name
1359 visitTypeInfo(mo.d.data[handle + 1]);
1360 }
1361
1362 return priv->propertyCount * QMetaObjectPrivate::IntsPerProperty;
1363}
1364
1365template <typename StringVisitor>
1366int visitClassInfo(const QMetaObject &mo, StringVisitor visitString)
1367{
1368 const QMetaObjectPrivate *const priv = reinterpret_cast<const QMetaObjectPrivate*>(mo.d.data);
1369 const int intsPerClassInfo = 2;
1370
1371 for (int i = 0; i < priv->classInfoCount; ++i) {
1372 const int handle = priv->classInfoData + i * intsPerClassInfo;
1373
1374 visitString(mo.d.data[handle]); // key
1375 visitString(mo.d.data[handle + 1]); // value
1376 }
1377
1378 return priv->classInfoCount * intsPerClassInfo;
1379}
1380
1381template <typename StringVisitor>
1382int visitEnumerations(const QMetaObject &mo, StringVisitor visitString)
1383{
1384 const QMetaObjectPrivate *const priv = reinterpret_cast<const QMetaObjectPrivate*>(mo.d.data);
1385
1386 int fieldCount = priv->enumeratorCount * QMetaObjectPrivate::IntsPerEnum;
1387
1388 for (int i = 0; i < priv->enumeratorCount; ++i) {
1389 const uint *enumeratorData = mo.d.data + priv->enumeratorData + i * QMetaObjectPrivate::IntsPerEnum;
1390
1391 const uint keyCount = enumeratorData[3];
1392 fieldCount += keyCount * 2;
1393
1394 visitString(enumeratorData[0]); // name
1395 visitString(enumeratorData[1]); // enum name
1396
1397 const uint keyOffset = enumeratorData[4];
1398
1399 for (uint j = 0; j < keyCount; ++j) {
1400 visitString(mo.d.data[keyOffset + 2 * j]);
1401 }
1402 }
1403
1404 return fieldCount;
1405}
1406
1407template <typename StringVisitor>
1408int countMetaObjectFields(const QMetaObject &mo, StringVisitor stringVisitor)
1409{
1410 const QMetaObjectPrivate *const priv = reinterpret_cast<const QMetaObjectPrivate*>(mo.d.data);
1411
1412 const auto typeInfoVisitor = [&stringVisitor](uint typeInfo) {
1413 if (typeInfo & IsUnresolvedType)
1414 stringVisitor(typeInfo & TypeNameIndexMask);
1415 };
1416
1417 int fieldCount = MetaObjectPrivateFieldCount;
1418
1419 fieldCount += visitMethods(mo, priv->methodData, priv->methodCount, stringVisitor,
1420 typeInfoVisitor);
1421 fieldCount += visitMethods(mo, priv->constructorData, priv->constructorCount, stringVisitor,
1422 typeInfoVisitor);
1423
1424 fieldCount += visitProperties(mo, stringVisitor, typeInfoVisitor);
1425 fieldCount += visitClassInfo(mo, stringVisitor);
1426 fieldCount += visitEnumerations(mo, stringVisitor);
1427
1428 return fieldCount;
1429}
1430
1431} // anonymous namespace
1432
1433static_assert(QMetaObjectPrivate::OutputRevision == 13, "Check and adjust determineMetaObjectSizes");
1434
1435bool QQmlPropertyCache::determineMetaObjectSizes(const QMetaObject &mo, int *fieldCount,
1436 int *stringCount)
1437{
1438 const QMetaObjectPrivate *priv = reinterpret_cast<const QMetaObjectPrivate*>(mo.d.data);
1439 if (priv->revision != QMetaObjectPrivate::OutputRevision)
1440 return false;
1441
1442 uint highestStringIndex = 0;
1443 const auto stringIndexVisitor = [&highestStringIndex](uint index) {
1444 highestStringIndex = qMax(highestStringIndex, index);
1445 };
1446
1447 *fieldCount = countMetaObjectFields(mo, stringIndexVisitor);
1448 *stringCount = highestStringIndex + 1;
1449
1450 return true;
1451}
1452
1453bool QQmlPropertyCache::addToHash(QCryptographicHash &hash, const QMetaObject &mo)
1454{
1455 int fieldCount = 0;
1456 int stringCount = 0;
1457 if (!determineMetaObjectSizes(mo, &fieldCount, &stringCount)) {
1458 return false;
1459 }
1460
1461 hash.addData({reinterpret_cast<const char *>(mo.d.data), qsizetype(fieldCount * sizeof(uint))});
1462 for (int i = 0; i < stringCount; ++i) {
1463 hash.addData(stringData(&mo, i));
1464 }
1465
1466 return true;
1467}
1468
1469QByteArray QQmlPropertyCache::checksum(QHash<quintptr, QByteArray> *checksums, bool *ok) const
1470{
1471 auto it = checksums->constFind(quintptr(this));
1472 if (it != checksums->constEnd()) {
1473 *ok = true;
1474 return *it;
1475 }
1476
1477 // Generate a checksum on the meta-object data only on C++ types.
1478 if (isComposite()) {
1479 *ok = false;
1480 return QByteArray();
1481 }
1482
1483 QCryptographicHash hash(QCryptographicHash::Md5);
1484
1485 if (_parent) {
1486 hash.addData(_parent->checksum(checksums, ok));
1487 if (!*ok)
1488 return QByteArray();
1489 }
1490
1491 if (!addToHash(hash, *_metaObject.metaObject())) {
1492 *ok = false;
1493 return QByteArray();
1494 }
1495
1496 const QByteArray result = hash.result();
1497 if (result.isEmpty()) {
1498 *ok = false;
1499 } else {
1500 *ok = true;
1501 checksums->insert(quintptr(this), result);
1502 }
1503 return result;
1504}
1505
1506/*! \internal
1507 \a index MUST be in the signal index range (see QObjectPrivate::signalIndex()).
1508 This is different from QMetaMethod::methodIndex().
1509*/
1510QList<QByteArray> QQmlPropertyCache::signalParameterNames(int index) const
1511{
1512 const QQmlPropertyData *signalData = signal(index);
1513 if (signalData && signalData->hasArguments()) {
1514 QQmlPropertyCacheMethodArguments *args = (QQmlPropertyCacheMethodArguments *)signalData->arguments();
1515 if (args && args->names)
1516 return *args->names;
1517 const QMetaMethod &method = QMetaObjectPrivate::signal(firstCppMetaObject(), index);
1518 return method.parameterNames();
1519 }
1520 return QList<QByteArray>();
1521}
1522
1523QT_END_NAMESPACE
static Status checkMinimal(const QQmlPropertyData *const existingProperty)
static Status check(const QQmlPropertyData &overridingProperty, const QQmlPropertyData *const existingProperty, CheckMode mode)
static bool hasInvalidModifierCombintation(const QQmlPropertyData &overridingProperty)
static Status checkFull(const QQmlPropertyData &overridingProperty, const QQmlPropertyData *const existingProperty)
Status handleOverride(QQmlPropertyData &overridingProperty, QQmlPropertyData *existingProperty, CheckMode mode)
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")
const QQmlPropertyData * qQmlPropertyCacheProperty(QObject *obj, T name, const QQmlRefPointer< QQmlContextData > &context, QQmlPropertyData *local)
static const QByteArray stringData(const QMetaObject *mo, int index)
static QQmlPropertyData qQmlPropertyCacheCreate(const QMetaObject *metaObject, const T &propertyName)
#define Q_INT16_MAX
static QByteArray qQmlPropertyCacheToString(QLatin1String string)
static QByteArray qQmlPropertyCacheToString(const QV4::String *string)
static std::pair< bool, int > deriveEncodingAndLength(const char *str)
static QHashedString signalNameToHandlerName(const QHashedString &methodName)
static int metaObjectSignalCount(const QMetaObject *metaObject)