1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qqmlpropertycache_p.h"
44 #include "qqmlengine_p.h"
45 #include "qqmlbinding_p.h"
46 #include <private/qv8engine_p.h>
48 #include <private/qmetaobject_p.h>
49 #include <private/qqmlaccessors_p.h>
50 #include <private/qmetaobjectbuilder_p.h>
52 #include <QtCore/qdebug.h>
54 #include <ctype.h> // for toupper
57 // nonstandard extension used : zero-sized array in struct/union.
58 # pragma warning( disable : 4200 )
61 Q_DECLARE_METATYPE(QJSValue)
62 Q_DECLARE_METATYPE(QQmlV8Handle);
66 #define Q_INT16_MAX 32767
68 class QQmlPropertyCacheMethodArguments
71 QQmlPropertyCacheMethodArguments *next;
73 QList<QByteArray> *names;
77 // Flags that do *NOT* depend on the property's QMetaProperty::userType() and thus are quick
79 static QQmlPropertyData::Flags fastFlagsForProperty(const QMetaProperty &p)
81 QQmlPropertyData::Flags flags;
84 flags |= QQmlPropertyData::IsConstant;
86 flags |= QQmlPropertyData::IsWritable;
88 flags |= QQmlPropertyData::IsResettable;
90 flags |= QQmlPropertyData::IsFinal;
92 flags |= QQmlPropertyData::IsEnumType;
97 // Flags that do depend on the property's QMetaProperty::userType() and thus are slow to
99 static QQmlPropertyData::Flags flagsForPropertyType(int propType, QQmlEngine *engine)
101 Q_ASSERT(propType != -1);
103 QQmlPropertyData::Flags flags;
105 if (propType == QMetaType::QObjectStar || propType == QMetaType::QWidgetStar) {
106 flags |= QQmlPropertyData::IsQObjectDerived;
107 } else if (propType == QMetaType::QVariant) {
108 flags |= QQmlPropertyData::IsQVariant;
109 } else if (propType < (int)QVariant::UserType) {
110 } else if (propType == qMetaTypeId<QQmlBinding *>()) {
111 flags |= QQmlPropertyData::IsQmlBinding;
112 } else if (propType == qMetaTypeId<QJSValue>()) {
113 flags |= QQmlPropertyData::IsQJSValue;
114 } else if (propType == qMetaTypeId<QQmlV8Handle>()) {
115 flags |= QQmlPropertyData::IsV8Handle;
117 QQmlMetaType::TypeCategory cat =
118 engine ? QQmlEnginePrivate::get(engine)->typeCategory(propType)
119 : QQmlMetaType::typeCategory(propType);
121 if (cat == QQmlMetaType::Object)
122 flags |= QQmlPropertyData::IsQObjectDerived;
123 else if (cat == QQmlMetaType::List)
124 flags |= QQmlPropertyData::IsQList;
130 static int metaObjectSignalCount(const QMetaObject *metaObject)
133 for (const QMetaObject *obj = metaObject; obj; obj = obj->superClass())
134 signalCount += QMetaObjectPrivate::get(obj)->signalCount;
138 QQmlPropertyData::Flags
139 QQmlPropertyData::flagsForProperty(const QMetaProperty &p, QQmlEngine *engine)
141 return fastFlagsForProperty(p) | flagsForPropertyType(p.userType(), engine);
144 void QQmlPropertyData::lazyLoad(const QMetaProperty &p, QQmlEngine *engine)
148 coreIndex = p.propertyIndex();
149 notifyIndex = QMetaObjectPrivate::signalIndex(p.notifySignal());
150 Q_ASSERT(p.revision() <= Q_INT16_MAX);
151 revision = p.revision();
153 flags = fastFlagsForProperty(p);
156 if (type == QMetaType::QObjectStar || type == QMetaType::QWidgetStar) {
158 flags |= QQmlPropertyData::IsQObjectDerived;
159 } else if (type == QMetaType::QVariant) {
161 flags |= QQmlPropertyData::IsQVariant;
162 } else if (type == QVariant::UserType || type == -1) {
163 propTypeName = p.typeName();
164 flags |= QQmlPropertyData::NotFullyResolved;
170 void QQmlPropertyData::load(const QMetaProperty &p, QQmlEngine *engine)
172 propType = p.userType();
173 coreIndex = p.propertyIndex();
174 notifyIndex = QMetaObjectPrivate::signalIndex(p.notifySignal());
175 flags = fastFlagsForProperty(p) | flagsForPropertyType(propType, engine);
176 Q_ASSERT(p.revision() <= Q_INT16_MAX);
177 revision = p.revision();
180 void QQmlPropertyData::load(const QMetaMethod &m)
182 coreIndex = m.methodIndex();
185 if (m.methodType() == QMetaMethod::Signal)
187 propType = m.returnType();
189 if (m.parameterCount()) {
190 flags |= HasArguments;
191 if ((m.parameterCount() == 1) && (m.parameterTypes().first() == "QQmlV8Function*")) {
192 flags |= IsV8Function;
196 if (m.attributes() & QMetaMethod::Cloned)
199 Q_ASSERT(m.revision() <= Q_INT16_MAX);
200 revision = m.revision();
203 void QQmlPropertyData::lazyLoad(const QMetaMethod &m)
205 coreIndex = m.methodIndex();
208 if (m.methodType() == QMetaMethod::Signal)
210 propType = QMetaType::Void;
212 const char *returnType = m.typeName();
215 if ((*returnType != 'v') || (qstrcmp(returnType+1, "oid") != 0)) {
216 propTypeName = returnType;
217 flags |= NotFullyResolved;
220 if (m.parameterCount()) {
221 flags |= HasArguments;
222 if ((m.parameterCount() == 1) && (m.parameterTypes().first() == "QQmlV8Function*")) {
223 flags |= IsV8Function;
227 if (m.attributes() & QMetaMethod::Cloned)
230 Q_ASSERT(m.revision() <= Q_INT16_MAX);
231 revision = m.revision();
235 Creates a new empty QQmlPropertyCache.
237 QQmlPropertyCache::QQmlPropertyCache(QQmlEngine *e)
238 : engine(e), _parent(0), propertyIndexCacheStart(0), methodIndexCacheStart(0),
239 signalHandlerIndexCacheStart(0), _ownMetaObject(false), _metaObject(0), argumentsCache(0)
245 Creates a new QQmlPropertyCache of \a metaObject.
247 QQmlPropertyCache::QQmlPropertyCache(QQmlEngine *e, const QMetaObject *metaObject)
248 : engine(e), _parent(0), propertyIndexCacheStart(0), methodIndexCacheStart(0),
249 signalHandlerIndexCacheStart(0), _ownMetaObject(false), _metaObject(0), argumentsCache(0)
252 Q_ASSERT(metaObject);
254 update(engine, metaObject);
257 QQmlPropertyCache::~QQmlPropertyCache()
261 QQmlPropertyCacheMethodArguments *args = argumentsCache;
263 QQmlPropertyCacheMethodArguments *next = args->next;
264 if (args->names) delete args->names;
269 // We must clear this prior to releasing the parent incase it is a
272 if (_parent) _parent->release();
274 if (_ownMetaObject) free((void *)_metaObject);
280 void QQmlPropertyCache::destroy()
282 Q_ASSERT(engine || constructor.IsEmpty());
283 if (constructor.IsEmpty())
286 QQmlEnginePrivate::deleteInEngineThread(engine, this);
289 // This is inherited from QQmlCleanup, so it should only clear the things
290 // that are tied to the specific QQmlEngine.
291 void QQmlPropertyCache::clear()
293 qPersistentDispose(constructor);
297 QQmlPropertyCache *QQmlPropertyCache::copy(int reserve)
299 QQmlPropertyCache *cache = new QQmlPropertyCache(engine);
300 cache->_parent = this;
301 cache->_parent->addref();
302 cache->propertyIndexCacheStart = propertyIndexCache.count() + propertyIndexCacheStart;
303 cache->methodIndexCacheStart = methodIndexCache.count() + methodIndexCacheStart;
304 cache->signalHandlerIndexCacheStart = signalHandlerIndexCache.count() + signalHandlerIndexCacheStart;
305 cache->stringCache.linkAndReserve(stringCache, reserve);
306 cache->allowedRevisionCache = allowedRevisionCache;
307 cache->_metaObject = _metaObject;
308 cache->_defaultPropertyName = _defaultPropertyName;
310 // We specifically do *NOT* copy the constructor
315 QQmlPropertyCache *QQmlPropertyCache::copy()
320 QQmlPropertyCache *QQmlPropertyCache::copyAndReserve(QQmlEngine *, int propertyCount, int methodCount,
323 QQmlPropertyCache *rv = copy(propertyCount + methodCount + signalCount);
324 rv->propertyIndexCache.reserve(propertyCount);
325 rv->methodIndexCache.reserve(methodCount);
326 rv->signalHandlerIndexCache.reserve(signalCount);
334 \a notifyIndex MUST be in the signal index range (see QObjectPrivate::signalIndex()).
335 This is different from QMetaMethod::methodIndex().
337 void QQmlPropertyCache::appendProperty(const QString &name,
338 quint32 flags, int coreIndex, int propType, int notifyIndex)
340 QQmlPropertyData data;
341 data.propType = propType;
342 data.coreIndex = coreIndex;
343 data.notifyIndex = notifyIndex;
346 QHashedString string(name);
347 if (QQmlPropertyData **old = stringCache.value(string)) {
348 data.overrideIndexIsProperty = !(*old)->isFunction();
349 data.overrideIndex = (*old)->coreIndex;
350 (*old)->flags |= QQmlPropertyData::IsOverridden;
353 propertyIndexCache.append(data);
355 stringCache.insert(string, propertyIndexCache.data() + propertyIndexCache.count() - 1);
358 void QQmlPropertyCache::appendProperty(const QHashedCStringRef &name,
359 quint32 flags, int coreIndex, int propType, int notifyIndex)
361 QQmlPropertyData data;
362 data.propType = propType;
363 data.coreIndex = coreIndex;
364 data.notifyIndex = notifyIndex;
367 if (QQmlPropertyData **old = stringCache.value(name)) {
368 data.overrideIndexIsProperty = !(*old)->isFunction();
369 data.overrideIndex = (*old)->coreIndex;
370 (*old)->flags |= QQmlPropertyData::IsOverridden;
373 propertyIndexCache.append(data);
375 stringCache.insert(name, propertyIndexCache.data() + propertyIndexCache.count() - 1);
378 void QQmlPropertyCache::appendSignal(const QString &name, quint32 flags, int coreIndex,
379 const int *types, const QList<QByteArray> &names)
381 QQmlPropertyData data;
382 data.propType = QVariant::Invalid;
383 data.coreIndex = coreIndex;
387 QQmlPropertyData handler = data;
388 handler.flags |= QQmlPropertyData::IsSignalHandler;
391 int argumentCount = *types;
392 typedef QQmlPropertyCacheMethodArguments A;
393 A *args = static_cast<A *>(malloc(sizeof(A) + (argumentCount + 1) * sizeof(int)));
394 ::memcpy(args->arguments, types, (argumentCount + 1) * sizeof(int));
395 args->names = new QList<QByteArray>(names);
396 args->next = argumentsCache;
397 argumentsCache = args;
398 data.arguments = args;
401 QString handlerName = QLatin1String("on") + name;
402 handlerName[2] = handlerName[2].toUpper();
404 QHashedString string(name);
405 if (QQmlPropertyData **old = stringCache.value(string)) {
406 data.overrideIndexIsProperty = !(*old)->isFunction();
407 data.overrideIndex = (*old)->coreIndex;
408 (*old)->flags |= QQmlPropertyData::IsOverridden;
411 methodIndexCache.append(data);
412 signalHandlerIndexCache.append(handler);
414 stringCache.insert(string, methodIndexCache.data() + methodIndexCache.count() - 1);
415 stringCache.insert(handlerName, signalHandlerIndexCache.data() + signalHandlerIndexCache.count() - 1);
418 void QQmlPropertyCache::appendSignal(const QHashedCStringRef &name, quint32 flags, int coreIndex,
419 const int *types, const QList<QByteArray> &names)
421 QQmlPropertyData data;
422 data.propType = QVariant::Invalid;
423 data.coreIndex = coreIndex;
427 QQmlPropertyData handler = data;
428 handler.flags |= QQmlPropertyData::IsSignalHandler;
431 int argumentCount = *types;
432 typedef QQmlPropertyCacheMethodArguments A;
433 A *args = static_cast<A *>(malloc(sizeof(A) + (argumentCount + 1) * sizeof(int)));
434 ::memcpy(args->arguments, types, (argumentCount + 1) * sizeof(int));
435 args->names = new QList<QByteArray>(names);
436 args->next = argumentsCache;
437 argumentsCache = args;
438 data.arguments = args;
441 QString handlerName = QLatin1String("on") + name.toUtf16();
442 handlerName[2] = handlerName[2].toUpper();
444 if (QQmlPropertyData **old = stringCache.value(name)) {
445 data.overrideIndexIsProperty = !(*old)->isFunction();
446 data.overrideIndex = (*old)->coreIndex;
447 (*old)->flags |= QQmlPropertyData::IsOverridden;
450 methodIndexCache.append(data);
451 signalHandlerIndexCache.append(handler);
453 stringCache.insert(name, methodIndexCache.data() + methodIndexCache.count() - 1);
454 stringCache.insert(handlerName, signalHandlerIndexCache.data() + signalHandlerIndexCache.count() - 1);
457 void QQmlPropertyCache::appendMethod(const QString &name, quint32 flags, int coreIndex,
458 const QList<QByteArray> &names)
460 int argumentCount = names.count();
462 QQmlPropertyData data;
463 data.propType = QMetaType::QVariant;
464 data.coreIndex = coreIndex;
466 typedef QQmlPropertyCacheMethodArguments A;
467 A *args = static_cast<A *>(malloc(sizeof(A) + (argumentCount + 1) * sizeof(int)));
468 args->arguments[0] = argumentCount;
469 for (int ii = 0; ii < argumentCount; ++ii)
470 args->arguments[ii + 1] = QMetaType::QVariant;
473 args->names = new QList<QByteArray>(names);
474 args->next = argumentsCache;
475 argumentsCache = args;
476 data.arguments = args;
480 QHashedString string(name);
481 if (QQmlPropertyData **old = stringCache.value(string)) {
482 data.overrideIndexIsProperty = !(*old)->isFunction();
483 data.overrideIndex = (*old)->coreIndex;
484 (*old)->flags |= QQmlPropertyData::IsOverridden;
487 methodIndexCache.append(data);
489 stringCache.insert(string, methodIndexCache.data() + methodIndexCache.count() - 1);
492 void QQmlPropertyCache::appendMethod(const QHashedCStringRef &name, quint32 flags, int coreIndex,
493 const QList<QByteArray> &names)
495 int argumentCount = names.count();
497 QQmlPropertyData data;
498 data.propType = QMetaType::QVariant;
499 data.coreIndex = coreIndex;
501 typedef QQmlPropertyCacheMethodArguments A;
502 A *args = static_cast<A *>(malloc(sizeof(A) + (argumentCount + 1) * sizeof(int)));
503 args->arguments[0] = argumentCount;
504 for (int ii = 0; ii < argumentCount; ++ii)
505 args->arguments[ii + 1] = QMetaType::QVariant;
508 args->names = new QList<QByteArray>(names);
509 args->next = argumentsCache;
510 argumentsCache = args;
511 data.arguments = args;
515 if (QQmlPropertyData **old = stringCache.value(name)) {
516 data.overrideIndexIsProperty = !(*old)->isFunction();
517 data.overrideIndex = (*old)->coreIndex;
518 (*old)->flags |= QQmlPropertyData::IsOverridden;
521 methodIndexCache.append(data);
523 stringCache.insert(name, methodIndexCache.data() + methodIndexCache.count() - 1);
526 // Returns this property cache's metaObject. May be null if it hasn't been created yet.
527 const QMetaObject *QQmlPropertyCache::metaObject() const
532 // Returns this property cache's metaObject, creating it if necessary.
533 const QMetaObject *QQmlPropertyCache::createMetaObject()
536 _ownMetaObject = true;
538 QMetaObjectBuilder builder;
539 toMetaObjectBuilder(builder);
540 builder.setSuperClass(_parent->createMetaObject());
541 _metaObject = builder.toMetaObject();
547 // Returns the name of the default property for this cache
548 QString QQmlPropertyCache::defaultPropertyName() const
550 return _defaultPropertyName;
553 QQmlPropertyData *QQmlPropertyCache::defaultProperty() const
555 return property(defaultPropertyName());
558 QQmlPropertyCache *QQmlPropertyCache::parent() const
563 // Returns the first C++ type's QMetaObject - that is, the first QMetaObject not created by
565 const QMetaObject *QQmlPropertyCache::firstCppMetaObject() const
567 while (_parent && (_metaObject == 0 || _ownMetaObject))
568 return _parent->firstCppMetaObject();
573 QQmlPropertyCache::copyAndAppend(QQmlEngine *engine, const QMetaObject *metaObject,
574 QQmlPropertyData::Flag propertyFlags,
575 QQmlPropertyData::Flag methodFlags,
576 QQmlPropertyData::Flag signalFlags)
578 return copyAndAppend(engine, metaObject, -1, propertyFlags, methodFlags, signalFlags);
582 QQmlPropertyCache::copyAndAppend(QQmlEngine *engine, const QMetaObject *metaObject,
584 QQmlPropertyData::Flag propertyFlags,
585 QQmlPropertyData::Flag methodFlags,
586 QQmlPropertyData::Flag signalFlags)
588 Q_ASSERT(QMetaObjectPrivate::get(metaObject)->revision >= 4);
590 // Reserve enough space in the name hash for all the methods (including signals), all the
591 // signal handlers and all the properties. This assumes no name clashes, but this is the
593 QQmlPropertyCache *rv = copy(QMetaObjectPrivate::get(metaObject)->methodCount +
594 QMetaObjectPrivate::get(metaObject)->signalCount +
595 QMetaObjectPrivate::get(metaObject)->propertyCount);
597 rv->append(engine, metaObject, revision, propertyFlags, methodFlags, signalFlags);
602 void QQmlPropertyCache::append(QQmlEngine *engine, const QMetaObject *metaObject,
604 QQmlPropertyData::Flag propertyFlags,
605 QQmlPropertyData::Flag methodFlags,
606 QQmlPropertyData::Flag signalFlags)
609 Q_ASSERT(constructor.IsEmpty()); // We should not be appending to an in-use property cache
611 _metaObject = metaObject;
613 bool dynamicMetaObject = isDynamicMetaObject(metaObject);
615 allowedRevisionCache.append(0);
617 int methodCount = metaObject->methodCount();
618 Q_ASSERT(QMetaObjectPrivate::get(metaObject)->revision >= 4);
619 int signalCount = metaObjectSignalCount(metaObject);
620 int classInfoCount = QMetaObjectPrivate::get(metaObject)->classInfoCount;
622 QQmlAccessorProperties::Properties accessorProperties;
624 if (classInfoCount) {
625 int classInfoOffset = metaObject->classInfoOffset();
626 bool hasFastProperty = false;
627 for (int ii = 0; ii < classInfoCount; ++ii) {
628 int idx = ii + classInfoOffset;
630 if (0 == qstrcmp(metaObject->classInfo(idx).name(), "qt_HasQmlAccessors")) {
631 hasFastProperty = true;
632 } else if (0 == qstrcmp(metaObject->classInfo(idx).name(), "DefaultProperty")) {
633 _defaultPropertyName = QString::fromUtf8(metaObject->classInfo(idx).value());
637 if (hasFastProperty) {
638 accessorProperties = QQmlAccessorProperties::properties(metaObject);
639 if (accessorProperties.count == 0)
640 qFatal("QQmlPropertyCache: %s has FastProperty class info, but has not "
641 "installed property accessors", metaObject->className());
644 accessorProperties = QQmlAccessorProperties::properties(metaObject);
645 if (accessorProperties.count != 0)
646 qFatal("QQmlPropertyCache: %s has fast property accessors, but is missing "
647 "FastProperty class info", metaObject->className());
652 //Used to block access to QObject::destroyed() and QObject::deleteLater() from QML
653 static const int destroyedIdx1 = QObject::staticMetaObject.indexOfSignal("destroyed(QObject*)");
654 static const int destroyedIdx2 = QObject::staticMetaObject.indexOfSignal("destroyed()");
655 static const int deleteLaterIdx = QObject::staticMetaObject.indexOfSlot("deleteLater()");
657 int methodOffset = metaObject->methodOffset();
658 int signalOffset = signalCount - QMetaObjectPrivate::get(metaObject)->signalCount;
660 // update() should have reserved enough space in the vector that this doesn't cause a realloc
661 // and invalidate the stringCache.
662 methodIndexCache.resize(methodCount - methodIndexCacheStart);
663 signalHandlerIndexCache.resize(signalCount - signalHandlerIndexCacheStart);
664 int signalHandlerIndex = signalOffset;
665 for (int ii = methodOffset; ii < methodCount; ++ii) {
666 if (ii == destroyedIdx1 || ii == destroyedIdx2 || ii == deleteLaterIdx)
668 QMetaMethod m = metaObject->method(ii);
669 if (m.access() == QMetaMethod::Private)
672 // Extract method name
673 // It's safe to keep the raw name pointer
674 Q_ASSERT(QMetaObjectPrivate::get(metaObject)->revision >= 7);
675 const char *rawName = m.name().constData();
676 const char *cptr = rawName;
679 utf8 |= *cptr & 0x80;
683 QQmlPropertyData *data = &methodIndexCache[ii - methodIndexCacheStart];
684 QQmlPropertyData *sigdata = 0;
688 if (data->isSignal())
689 data->flags |= signalFlags;
691 data->flags |= methodFlags;
693 if (!dynamicMetaObject)
694 data->flags |= QQmlPropertyData::IsDirect;
696 Q_ASSERT((allowedRevisionCache.count() - 1) < Q_INT16_MAX);
697 data->metaObjectOffset = allowedRevisionCache.count() - 1;
699 if (data->isSignal()) {
700 sigdata = &signalHandlerIndexCache[signalHandlerIndex - signalHandlerIndexCacheStart];
702 sigdata->flags |= QQmlPropertyData::IsSignalHandler;
705 QQmlPropertyData *old = 0;
708 QHashedString methodName(QString::fromUtf8(rawName, cptr - rawName));
709 if (QQmlPropertyData **it = stringCache.value(methodName))
711 stringCache.insert(methodName, data);
713 if (data->isSignal()) {
714 QHashedString on(QStringLiteral("on") % methodName.at(0).toUpper() % methodName.midRef(1));
715 stringCache.insert(on, sigdata);
716 ++signalHandlerIndex;
719 QHashedCStringRef methodName(rawName, cptr - rawName);
720 if (QQmlPropertyData **it = stringCache.value(methodName))
722 stringCache.insert(methodName, data);
724 if (data->isSignal()) {
725 int length = methodName.length();
727 QVarLengthArray<char, 128> str(length+3);
730 str[2] = toupper(rawName[0]);
732 memcpy(&str[3], &rawName[1], length - 1);
733 str[length + 2] = '\0';
735 QHashedString on(QString::fromLatin1(str.data()));
736 stringCache.insert(on, sigdata);
737 ++signalHandlerIndex;
742 // We only overload methods in the same class, exactly like C++
743 if (old->isFunction() && old->coreIndex >= methodOffset)
744 data->flags |= QQmlPropertyData::IsOverload;
745 data->overrideIndexIsProperty = !old->isFunction();
746 data->overrideIndex = old->coreIndex;
747 old->flags |= QQmlPropertyData::IsOverridden;
751 int propCount = metaObject->propertyCount();
752 int propOffset = metaObject->propertyOffset();
754 // update() should have reserved enough space in the vector that this doesn't cause a realloc
755 // and invalidate the stringCache.
756 propertyIndexCache.resize(propCount - propertyIndexCacheStart);
757 for (int ii = propOffset; ii < propCount; ++ii) {
758 QMetaProperty p = metaObject->property(ii);
759 if (!p.isScriptable())
762 const char *str = p.name();
764 const char *cptr = str;
766 utf8 |= *cptr & 0x80;
770 QQmlPropertyData *data = &propertyIndexCache[ii - propertyIndexCacheStart];
772 data->lazyLoad(p, engine);
773 data->flags |= propertyFlags;
775 if (!dynamicMetaObject)
776 data->flags |= QQmlPropertyData::IsDirect;
778 Q_ASSERT((allowedRevisionCache.count() - 1) < Q_INT16_MAX);
779 data->metaObjectOffset = allowedRevisionCache.count() - 1;
781 QQmlPropertyData *old = 0;
784 QHashedString propName(QString::fromUtf8(str, cptr - str));
785 if (QQmlPropertyData **it = stringCache.value(propName))
787 stringCache.insert(propName, data);
789 QHashedCStringRef propName(str, cptr - str);
790 if (QQmlPropertyData **it = stringCache.value(propName))
792 stringCache.insert(propName, data);
795 QQmlAccessorProperties::Property *accessorProperty = accessorProperties.property(str);
797 // Fast properties may not be overrides or revisioned
798 Q_ASSERT(accessorProperty == 0 || (old == 0 && data->revision == 0));
800 if (accessorProperty) {
801 data->flags |= QQmlPropertyData::HasAccessors;
802 data->accessors = accessorProperty->accessors;
803 data->accessorData = accessorProperty->data;
805 data->overrideIndexIsProperty = !old->isFunction();
806 data->overrideIndex = old->coreIndex;
807 old->flags |= QQmlPropertyData::IsOverridden;
812 void QQmlPropertyCache::resolve(QQmlPropertyData *data) const
814 Q_ASSERT(data->notFullyResolved());
816 data->propType = QMetaType::type(data->propTypeName);
818 if (!data->isFunction())
819 data->flags |= flagsForPropertyType(data->propType, engine);
821 data->flags &= ~QQmlPropertyData::NotFullyResolved;
824 void QQmlPropertyCache::updateRecur(QQmlEngine *engine, const QMetaObject *metaObject)
829 updateRecur(engine, metaObject->superClass());
831 append(engine, metaObject, -1);
834 void QQmlPropertyCache::update(QQmlEngine *engine, const QMetaObject *metaObject)
837 Q_ASSERT(metaObject);
838 Q_ASSERT(stringCache.isEmpty());
840 // Preallocate enough space in the index caches for all the properties/methods/signals that
841 // are not cached in a parent cache so that the caches never need to be reallocated as this
842 // would invalidate pointers stored in the stringCache.
843 int pc = metaObject->propertyCount();
844 int mc = metaObject->methodCount();
845 int sc = metaObjectSignalCount(metaObject);
846 propertyIndexCache.reserve(pc - propertyIndexCacheStart);
847 methodIndexCache.reserve(mc - methodIndexCacheStart);
848 signalHandlerIndexCache.reserve(sc - signalHandlerIndexCacheStart);
850 // Reserve enough space in the stringCache for all properties/methods/signals including those
851 // cached in a parent cache.
852 stringCache.reserve(pc + mc + sc);
854 updateRecur(engine,metaObject);
858 \a index MUST be in the signal index range (see QObjectPrivate::signalIndex()).
859 This is different from QMetaMethod::methodIndex().
862 QQmlPropertyCache::signal(int index) const
864 if (index < 0 || index >= (signalHandlerIndexCacheStart + signalHandlerIndexCache.count()))
867 if (index < signalHandlerIndexCacheStart)
868 return _parent->signal(index);
870 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&methodIndexCache.at(index - signalHandlerIndexCacheStart));
871 if (rv->notFullyResolved()) resolve(rv);
872 Q_ASSERT(rv->isSignal() || rv->coreIndex == -1);
876 int QQmlPropertyCache::methodIndexToSignalIndex(int index) const
878 if (index < 0 || index >= (methodIndexCacheStart + methodIndexCache.count()))
881 if (index < methodIndexCacheStart)
882 return _parent->methodIndexToSignalIndex(index);
884 return index - methodIndexCacheStart + signalHandlerIndexCacheStart;
888 QQmlPropertyCache::property(int index) const
890 if (index < 0 || index >= (propertyIndexCacheStart + propertyIndexCache.count()))
893 if (index < propertyIndexCacheStart)
894 return _parent->property(index);
896 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&propertyIndexCache.at(index - propertyIndexCacheStart));
897 if (rv->notFullyResolved()) resolve(rv);
902 QQmlPropertyCache::method(int index) const
904 if (index < 0 || index >= (methodIndexCacheStart + methodIndexCache.count()))
907 if (index < methodIndexCacheStart)
908 return _parent->method(index);
910 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&methodIndexCache.at(index - methodIndexCacheStart));
911 if (rv->notFullyResolved()) resolve(rv);
916 QQmlPropertyCache::property(const QHashedStringRef &str) const
918 QQmlPropertyData **rv = stringCache.value(str);
919 if (rv && (*rv)->notFullyResolved()) resolve(*rv);
924 QQmlPropertyCache::property(const QHashedCStringRef &str) const
926 QQmlPropertyData **rv = stringCache.value(str);
927 if (rv && (*rv)->notFullyResolved()) resolve(*rv);
932 QQmlPropertyCache::property(const QString &str) const
934 QQmlPropertyData **rv = stringCache.value(str);
935 if (rv && (*rv)->notFullyResolved()) resolve(*rv);
939 QString QQmlPropertyData::name(QObject *object)
944 return name(object->metaObject());
947 QString QQmlPropertyData::name(const QMetaObject *metaObject)
949 if (!metaObject || coreIndex == -1)
952 if (flags & IsFunction) {
953 QMetaMethod m = metaObject->method(coreIndex);
955 return QString::fromUtf8(m.name().constData());
957 QMetaProperty p = metaObject->property(coreIndex);
958 return QString::fromUtf8(p.name());
962 QStringList QQmlPropertyCache::propertyNames() const
965 for (StringCache::ConstIterator iter = stringCache.begin(); iter != stringCache.end(); ++iter)
966 keys.append(iter.key());
970 struct StaticQtMetaObject : public QObject
972 static const QMetaObject *get()
973 { return &static_cast<StaticQtMetaObject*> (0)->staticQtMetaObject; }
976 static int EnumType(const QMetaObject *metaobj, const QByteArray &str, int type)
980 int scopeIdx = str.lastIndexOf("::");
981 if (scopeIdx != -1) {
982 scope = str.left(scopeIdx);
983 name = str.mid(scopeIdx + 2);
987 const QMetaObject *meta;
989 meta = StaticQtMetaObject::get();
992 for (int i = meta->enumeratorCount() - 1; i >= 0; --i) {
993 QMetaEnum m = meta->enumerator(i);
994 if ((m.name() == name) && (scope.isEmpty() || (m.scope() == scope)))
995 return QVariant::Int;
1001 \a index MUST be in the signal index range (see QObjectPrivate::signalIndex()).
1002 This is different from QMetaMethod::methodIndex().
1004 QList<QByteArray> QQmlPropertyCache::signalParameterNames(QObject *object, int index)
1006 QQmlData *data = QQmlData::get(object, false);
1007 if (data->propertyCache) {
1008 QQmlPropertyData *p = data->propertyCache->signal(index);
1009 if (!p->hasArguments())
1010 return QList<QByteArray>();
1013 return QMetaObjectPrivate::signal(object->metaObject(), index).parameterNames();
1016 // Returns an array of the arguments for method \a index. The first entry in the array
1017 // is the number of arguments.
1018 int *QQmlPropertyCache::methodParameterTypes(QObject *object, int index,
1019 QVarLengthArray<int, 9> &dummy,
1020 QByteArray *unknownTypeError)
1022 Q_ASSERT(object && index >= 0);
1024 QQmlData *ddata = QQmlData::get(object, false);
1026 if (ddata && ddata->propertyCache) {
1027 typedef QQmlPropertyCacheMethodArguments A;
1029 QQmlPropertyCache *c = ddata->propertyCache;
1030 Q_ASSERT(index < c->methodIndexCacheStart + c->methodIndexCache.count());
1032 while (index < c->methodIndexCacheStart)
1035 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&c->methodIndexCache.at(index - c->methodIndexCacheStart));
1038 return static_cast<A *>(rv->arguments)->arguments;
1040 const QMetaObject *metaObject = c->createMetaObject();
1041 Q_ASSERT(metaObject);
1042 QMetaMethod m = metaObject->method(index);
1044 int argc = m.parameterCount();
1045 A *args = static_cast<A *>(malloc(sizeof(A) + (argc + 1) * sizeof(int)));
1046 args->arguments[0] = argc;
1048 QList<QByteArray> argTypeNames; // Only loaded if needed
1050 for (int ii = 0; ii < argc; ++ii) {
1051 int type = m.parameterType(ii);
1052 QMetaType::TypeFlags flags = QMetaType::typeFlags(type);
1053 if (flags & QMetaType::IsEnumeration)
1054 type = QVariant::Int;
1055 else if (type == QMetaType::UnknownType ||
1056 (type >= (int)QVariant::UserType && !(flags & QMetaType::PointerToQObject) &&
1057 type != qMetaTypeId<QJSValue>())) {
1058 //the UserType clause is to catch registered QFlags
1059 if (argTypeNames.isEmpty())
1060 argTypeNames = m.parameterTypes();
1061 type = EnumType(object->metaObject(), argTypeNames.at(ii), type);
1063 if (type == QMetaType::UnknownType) {
1064 if (unknownTypeError) *unknownTypeError = argTypeNames.at(ii);
1068 args->arguments[ii + 1] = type;
1071 rv->arguments = args;
1072 args->next = c->argumentsCache;
1073 c->argumentsCache = args;
1074 return static_cast<A *>(rv->arguments)->arguments;
1077 QMetaMethod m = object->metaObject()->method(index);
1078 int argc = m.parameterCount();
1079 dummy.resize(argc + 1);
1081 QList<QByteArray> argTypeNames; // Only loaded if needed
1083 for (int ii = 0; ii < argc; ++ii) {
1084 int type = m.parameterType(ii);
1085 QMetaType::TypeFlags flags = QMetaType::typeFlags(type);
1086 if (flags & QMetaType::IsEnumeration)
1087 type = QVariant::Int;
1088 else if (type == QMetaType::UnknownType ||
1089 (type >= (int)QVariant::UserType && !(flags & QMetaType::PointerToQObject) &&
1090 type != qMetaTypeId<QJSValue>())) {
1091 //the UserType clause is to catch registered QFlags)
1092 if (argTypeNames.isEmpty())
1093 argTypeNames = m.parameterTypes();
1094 type = EnumType(object->metaObject(), argTypeNames.at(ii), type);
1096 if (type == QMetaType::UnknownType) {
1097 if (unknownTypeError) *unknownTypeError = argTypeNames.at(ii);
1100 dummy[ii + 1] = type;
1103 return dummy.data();
1107 // Returns the return type of the method.
1108 int QQmlPropertyCache::methodReturnType(QObject *object, const QQmlPropertyData &data,
1109 QByteArray *unknownTypeError)
1111 Q_ASSERT(object && data.coreIndex >= 0);
1113 int type = data.propType;
1115 const char *propTypeName = 0;
1117 if (type == QMetaType::UnknownType) {
1118 // Find the return type name from the method info
1121 QQmlData *ddata = QQmlData::get(object, false);
1122 if (ddata && ddata->propertyCache) {
1123 QQmlPropertyCache *c = ddata->propertyCache;
1124 Q_ASSERT(data.coreIndex < c->methodIndexCacheStart + c->methodIndexCache.count());
1126 while (data.coreIndex < c->methodIndexCacheStart)
1129 const QMetaObject *metaObject = c->createMetaObject();
1130 Q_ASSERT(metaObject);
1131 m = metaObject->method(data.coreIndex);
1133 m = object->metaObject()->method(data.coreIndex);
1136 type = m.returnType();
1137 propTypeName = m.typeName();
1140 QMetaType::TypeFlags flags = QMetaType::typeFlags(type);
1141 if (flags & QMetaType::IsEnumeration) {
1142 type = QVariant::Int;
1143 } else if (type == QMetaType::UnknownType ||
1144 (type >= (int)QVariant::UserType && !(flags & QMetaType::PointerToQObject) &&
1145 type != qMetaTypeId<QJSValue>())) {
1146 //the UserType clause is to catch registered QFlags
1147 type = EnumType(object->metaObject(), propTypeName, type);
1150 if (type == QMetaType::UnknownType) {
1151 if (unknownTypeError) *unknownTypeError = propTypeName;
1157 QQmlPropertyData qQmlPropertyCacheCreate(const QMetaObject *metaObject, const QString &property)
1159 Q_ASSERT(metaObject);
1161 QQmlPropertyData rv;
1163 const QMetaObject *cmo = metaObject;
1164 const QByteArray propertyName = property.toUtf8();
1166 int idx = cmo->indexOfProperty(propertyName);
1168 QMetaProperty p = cmo->property(idx);
1169 if (p.isScriptable()) {
1173 while (cmo && cmo->propertyOffset() >= idx)
1174 cmo = cmo->superClass();
1182 //Used to block access to QObject::destroyed() and QObject::deleteLater() from QML
1183 static const int destroyedIdx1 = QObject::staticMetaObject.indexOfSignal("destroyed(QObject*)");
1184 static const int destroyedIdx2 = QObject::staticMetaObject.indexOfSignal("destroyed()");
1185 static const int deleteLaterIdx = QObject::staticMetaObject.indexOfSlot("deleteLater()");
1187 int methodCount = metaObject->methodCount();
1188 for (int ii = methodCount - 1; ii >= 0; --ii) {
1189 if (ii == destroyedIdx1 || ii == destroyedIdx2 || ii == deleteLaterIdx)
1191 QMetaMethod m = metaObject->method(ii);
1192 if (m.access() == QMetaMethod::Private)
1194 QString methodName = QString::fromUtf8(m.name().constData());
1196 if (methodName == property) {
1205 inline const QString &qQmlPropertyCacheToString(const QString &string)
1210 inline QString qQmlPropertyCacheToString(const QHashedV8String &string)
1212 return QV8Engine::toStringStatic(string.string());
1215 template<typename T>
1217 qQmlPropertyCacheProperty(QQmlEngine *engine, QObject *obj, const T &name, QQmlPropertyData &local)
1219 QQmlPropertyCache *cache = 0;
1221 QQmlData *ddata = QQmlData::get(obj, false);
1223 if (ddata && ddata->propertyCache) {
1224 cache = ddata->propertyCache;
1225 } else if (engine) {
1226 QQmlEnginePrivate *ep = QQmlEnginePrivate::get(engine);
1227 cache = ep->cache(obj);
1229 ddata = QQmlData::get(obj, true);
1231 ddata->propertyCache = cache;
1235 QQmlPropertyData *rv = 0;
1238 rv = cache->property(name);
1240 local = qQmlPropertyCacheCreate(obj->metaObject(), qQmlPropertyCacheToString(name));
1241 if (local.isValid())
1249 QQmlPropertyCache::property(QQmlEngine *engine, QObject *obj,
1250 const QHashedV8String &name, QQmlPropertyData &local)
1252 return qQmlPropertyCacheProperty<QHashedV8String>(engine, obj, name, local);
1256 QQmlPropertyCache::property(QQmlEngine *engine, QObject *obj,
1257 const QString &name, QQmlPropertyData &local)
1259 return qQmlPropertyCacheProperty<QString>(engine, obj, name, local);
1262 static inline const QMetaObjectPrivate *priv(const uint* data)
1263 { return reinterpret_cast<const QMetaObjectPrivate*>(data); }
1265 bool QQmlPropertyCache::isDynamicMetaObject(const QMetaObject *mo)
1267 return priv(mo->d.data)->revision >= 3 && priv(mo->d.data)->flags & DynamicMetaObject;
1270 const char *QQmlPropertyCache::className() const
1272 if (!_ownMetaObject && _metaObject)
1273 return _metaObject->className();
1275 return _dynamicClassName.constData();
1278 void QQmlPropertyCache::toMetaObjectBuilder(QMetaObjectBuilder &builder)
1280 struct Sort { static bool lt(const QPair<QString, QQmlPropertyData *> &lhs,
1281 const QPair<QString, QQmlPropertyData *> &rhs) {
1282 return lhs.second->coreIndex < rhs.second->coreIndex;
1285 struct Insert { static void in(QQmlPropertyCache *This,
1286 QList<QPair<QString, QQmlPropertyData *> > &properties,
1287 QList<QPair<QString, QQmlPropertyData *> > &methods,
1288 StringCache::ConstIterator iter, QQmlPropertyData *data) {
1289 if (data->isSignalHandler())
1292 if (data->isFunction()) {
1293 if (data->coreIndex < This->methodIndexCacheStart)
1296 QPair<QString, QQmlPropertyData *> entry = qMakePair((QString)iter.key(), data);
1297 // Overrides can cause the entry to already exist
1298 if (!methods.contains(entry)) methods.append(entry);
1300 data = This->overrideData(data);
1301 if (data && !data->isFunction()) Insert::in(This, properties, methods, iter, data);
1303 if (data->coreIndex < This->propertyIndexCacheStart)
1306 QPair<QString, QQmlPropertyData *> entry = qMakePair((QString)iter.key(), data);
1307 // Overrides can cause the entry to already exist
1308 if (!properties.contains(entry)) properties.append(entry);
1310 data = This->overrideData(data);
1311 if (data) Insert::in(This, properties, methods, iter, data);
1316 builder.setClassName(_dynamicClassName);
1318 QList<QPair<QString, QQmlPropertyData *> > properties;
1319 QList<QPair<QString, QQmlPropertyData *> > methods;
1321 for (StringCache::ConstIterator iter = stringCache.begin(); iter != stringCache.end(); ++iter)
1322 Insert::in(this, properties, methods, iter, iter.value());
1324 Q_ASSERT(properties.count() == propertyIndexCache.count());
1325 Q_ASSERT(methods.count() == methodIndexCache.count());
1327 qSort(properties.begin(), properties.end(), Sort::lt);
1328 qSort(methods.begin(), methods.end(), Sort::lt);
1330 for (int ii = 0; ii < properties.count(); ++ii) {
1331 QQmlPropertyData *data = properties.at(ii).second;
1333 int notifierId = -1;
1334 if (data->notifyIndex != -1)
1335 notifierId = data->notifyIndex - signalHandlerIndexCacheStart;
1337 QMetaPropertyBuilder property = builder.addProperty(properties.at(ii).first.toUtf8(),
1338 QMetaType::typeName(data->propType),
1341 property.setReadable(true);
1342 property.setWritable(data->isWritable());
1343 property.setResettable(data->isResettable());
1346 for (int ii = 0; ii < methods.count(); ++ii) {
1347 QQmlPropertyData *data = methods.at(ii).second;
1349 QByteArray returnType;
1350 if (data->propType != 0)
1351 returnType = QMetaType::typeName(data->propType);
1353 QByteArray signature = methods.at(ii).first.toUtf8() + "(";
1355 QQmlPropertyCacheMethodArguments *arguments = 0;
1356 if (data->hasArguments()) {
1357 arguments = (QQmlPropertyCacheMethodArguments *)data->arguments;
1359 for (int ii = 0; ii < arguments->arguments[0]; ++ii) {
1360 if (ii != 0) signature.append(",");
1361 signature.append(QMetaType::typeName(arguments->arguments[1 + ii]));
1365 signature.append(")");
1367 QMetaMethodBuilder method;
1368 if (data->isSignal()) {
1369 method = builder.addSignal(signature);
1371 method = builder.addSlot(signature);
1373 method.setAccess(QMetaMethod::Protected);
1375 if (arguments && arguments->names)
1376 method.setParameterNames(*arguments->names);
1378 if (!returnType.isEmpty())
1379 method.setReturnType(returnType);
1382 if (!_defaultPropertyName.isEmpty()) {
1383 QQmlPropertyData *dp = property(_defaultPropertyName);
1384 if (dp && dp->coreIndex >= propertyIndexCacheStart) {
1385 Q_ASSERT(!dp->isFunction());
1386 builder.addClassInfo("DefaultProperty", _defaultPropertyName.toUtf8());
1391 // Returns true if \a from is assignable to a property of type \a to
1392 bool QQmlMetaObject::canConvert(const QQmlMetaObject &from, const QQmlMetaObject &to)
1394 Q_ASSERT(!from.isNull() && !to.isNull());
1396 struct I { static bool equal(const QMetaObject *lhs, const QMetaObject *rhs) {
1397 return lhs == rhs || (lhs && rhs && lhs->d.stringdata == rhs->d.stringdata);
1400 const QMetaObject *tom = to._m.isT1()?to._m.asT1()->metaObject():to._m.asT2();
1401 if (tom == &QObject::staticMetaObject) return true;
1403 if (from._m.isT1() && to._m.isT1()) { // QQmlPropertyCache -> QQmlPropertyCache
1404 QQmlPropertyCache *fromp = from._m.asT1();
1405 QQmlPropertyCache *top = to._m.asT1();
1408 if (fromp == top) return true;
1409 fromp = fromp->parent();
1411 } else if (from._m.isT1() && to._m.isT2()) { // QQmlPropertyCache -> QMetaObject
1412 QQmlPropertyCache *fromp = from._m.asT1();
1415 const QMetaObject *fromm = fromp->metaObject();
1416 if (fromm && I::equal(fromm, tom)) return true;
1417 fromp = fromp->parent();
1419 } else if (from._m.isT2() && to._m.isT1()) { // QMetaObject -> QQmlPropertyCache
1420 const QMetaObject *fromm = from._m.asT2();
1422 if (!tom) return false;
1425 if (I::equal(fromm, tom)) return true;
1426 fromm = fromm->superClass();
1428 } else { // QMetaObject -> QMetaObject
1429 const QMetaObject *fromm = from._m.asT2();
1432 if (I::equal(fromm, tom)) return true;
1433 fromm = fromm->superClass();
1440 QQmlPropertyCache *QQmlMetaObject::propertyCache(QQmlEnginePrivate *e) const
1442 if (_m.isNull()) return 0;
1443 if (_m.isT1()) return _m.asT1();
1444 else return e->cache(_m.asT2());