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;
352 propertyIndexCache.append(data);
354 stringCache.insert(string, propertyIndexCache.data() + propertyIndexCache.count() - 1);
357 void QQmlPropertyCache::appendProperty(const QHashedCStringRef &name,
358 quint32 flags, int coreIndex, int propType, int notifyIndex)
360 QQmlPropertyData data;
361 data.propType = propType;
362 data.coreIndex = coreIndex;
363 data.notifyIndex = notifyIndex;
366 if (QQmlPropertyData **old = stringCache.value(name)) {
367 data.overrideIndexIsProperty = !(*old)->isFunction();
368 data.overrideIndex = (*old)->coreIndex;
371 propertyIndexCache.append(data);
373 stringCache.insert(name, propertyIndexCache.data() + propertyIndexCache.count() - 1);
376 void QQmlPropertyCache::appendSignal(const QString &name, quint32 flags, int coreIndex,
377 const int *types, const QList<QByteArray> &names)
379 QQmlPropertyData data;
380 data.propType = QVariant::Invalid;
381 data.coreIndex = coreIndex;
385 QQmlPropertyData handler = data;
386 handler.flags |= QQmlPropertyData::IsSignalHandler;
389 int argumentCount = *types;
390 typedef QQmlPropertyCacheMethodArguments A;
391 A *args = static_cast<A *>(malloc(sizeof(A) + (argumentCount + 1) * sizeof(int)));
392 ::memcpy(args->arguments, types, (argumentCount + 1) * sizeof(int));
393 args->names = new QList<QByteArray>(names);
394 args->next = argumentsCache;
395 argumentsCache = args;
396 data.arguments = args;
399 QString handlerName = QLatin1String("on") + name;
400 handlerName[2] = handlerName[2].toUpper();
402 QHashedString string(name);
403 if (QQmlPropertyData **old = stringCache.value(string)) {
404 data.overrideIndexIsProperty = !(*old)->isFunction();
405 data.overrideIndex = (*old)->coreIndex;
408 methodIndexCache.append(data);
409 signalHandlerIndexCache.append(handler);
411 stringCache.insert(string, methodIndexCache.data() + methodIndexCache.count() - 1);
412 stringCache.insert(handlerName, signalHandlerIndexCache.data() + signalHandlerIndexCache.count() - 1);
415 void QQmlPropertyCache::appendSignal(const QHashedCStringRef &name, quint32 flags, int coreIndex,
416 const int *types, const QList<QByteArray> &names)
418 QQmlPropertyData data;
419 data.propType = QVariant::Invalid;
420 data.coreIndex = coreIndex;
424 QQmlPropertyData handler = data;
425 handler.flags |= QQmlPropertyData::IsSignalHandler;
428 int argumentCount = *types;
429 typedef QQmlPropertyCacheMethodArguments A;
430 A *args = static_cast<A *>(malloc(sizeof(A) + (argumentCount + 1) * sizeof(int)));
431 ::memcpy(args->arguments, types, (argumentCount + 1) * sizeof(int));
432 args->names = new QList<QByteArray>(names);
433 args->next = argumentsCache;
434 argumentsCache = args;
435 data.arguments = args;
438 QString handlerName = QLatin1String("on") + name.toUtf16();
439 handlerName[2] = handlerName[2].toUpper();
441 if (QQmlPropertyData **old = stringCache.value(name)) {
442 data.overrideIndexIsProperty = !(*old)->isFunction();
443 data.overrideIndex = (*old)->coreIndex;
446 methodIndexCache.append(data);
447 signalHandlerIndexCache.append(handler);
449 stringCache.insert(name, methodIndexCache.data() + methodIndexCache.count() - 1);
450 stringCache.insert(handlerName, signalHandlerIndexCache.data() + signalHandlerIndexCache.count() - 1);
453 void QQmlPropertyCache::appendMethod(const QString &name, quint32 flags, int coreIndex,
454 const QList<QByteArray> &names)
456 int argumentCount = names.count();
458 QQmlPropertyData data;
459 data.propType = QMetaType::QVariant;
460 data.coreIndex = coreIndex;
462 typedef QQmlPropertyCacheMethodArguments A;
463 A *args = static_cast<A *>(malloc(sizeof(A) + (argumentCount + 1) * sizeof(int)));
464 args->arguments[0] = argumentCount;
465 for (int ii = 0; ii < argumentCount; ++ii)
466 args->arguments[ii + 1] = QMetaType::QVariant;
469 args->names = new QList<QByteArray>(names);
470 args->next = argumentsCache;
471 argumentsCache = args;
472 data.arguments = args;
476 QHashedString string(name);
477 if (QQmlPropertyData **old = stringCache.value(string)) {
478 data.overrideIndexIsProperty = !(*old)->isFunction();
479 data.overrideIndex = (*old)->coreIndex;
482 methodIndexCache.append(data);
484 stringCache.insert(string, methodIndexCache.data() + methodIndexCache.count() - 1);
487 void QQmlPropertyCache::appendMethod(const QHashedCStringRef &name, quint32 flags, int coreIndex,
488 const QList<QByteArray> &names)
490 int argumentCount = names.count();
492 QQmlPropertyData data;
493 data.propType = QMetaType::QVariant;
494 data.coreIndex = coreIndex;
496 typedef QQmlPropertyCacheMethodArguments A;
497 A *args = static_cast<A *>(malloc(sizeof(A) + (argumentCount + 1) * sizeof(int)));
498 args->arguments[0] = argumentCount;
499 for (int ii = 0; ii < argumentCount; ++ii)
500 args->arguments[ii + 1] = QMetaType::QVariant;
503 args->names = new QList<QByteArray>(names);
504 args->next = argumentsCache;
505 argumentsCache = args;
506 data.arguments = args;
510 if (QQmlPropertyData **old = stringCache.value(name)) {
511 data.overrideIndexIsProperty = !(*old)->isFunction();
512 data.overrideIndex = (*old)->coreIndex;
515 methodIndexCache.append(data);
517 stringCache.insert(name, methodIndexCache.data() + methodIndexCache.count() - 1);
520 // Returns this property cache's metaObject. May be null if it hasn't been created yet.
521 const QMetaObject *QQmlPropertyCache::metaObject() const
526 // Returns this property cache's metaObject, creating it if necessary.
527 const QMetaObject *QQmlPropertyCache::createMetaObject()
530 _ownMetaObject = true;
532 QMetaObjectBuilder builder;
533 toMetaObjectBuilder(builder);
534 builder.setSuperClass(_parent->createMetaObject());
535 _metaObject = builder.toMetaObject();
541 // Returns the name of the default property for this cache
542 QString QQmlPropertyCache::defaultPropertyName() const
544 return _defaultPropertyName;
547 QQmlPropertyData *QQmlPropertyCache::defaultProperty() const
549 return property(defaultPropertyName());
552 QQmlPropertyCache *QQmlPropertyCache::parent() const
557 // Returns the first C++ type's QMetaObject - that is, the first QMetaObject not created by
559 const QMetaObject *QQmlPropertyCache::firstCppMetaObject() const
561 while (_parent && (_metaObject == 0 || _ownMetaObject))
562 return _parent->firstCppMetaObject();
567 QQmlPropertyCache::copyAndAppend(QQmlEngine *engine, const QMetaObject *metaObject,
568 QQmlPropertyData::Flag propertyFlags,
569 QQmlPropertyData::Flag methodFlags,
570 QQmlPropertyData::Flag signalFlags)
572 return copyAndAppend(engine, metaObject, -1, propertyFlags, methodFlags, signalFlags);
576 QQmlPropertyCache::copyAndAppend(QQmlEngine *engine, const QMetaObject *metaObject,
578 QQmlPropertyData::Flag propertyFlags,
579 QQmlPropertyData::Flag methodFlags,
580 QQmlPropertyData::Flag signalFlags)
582 Q_ASSERT(QMetaObjectPrivate::get(metaObject)->revision >= 4);
584 // Reserve enough space in the name hash for all the methods (including signals), all the
585 // signal handlers and all the properties. This assumes no name clashes, but this is the
587 QQmlPropertyCache *rv = copy(QMetaObjectPrivate::get(metaObject)->methodCount +
588 QMetaObjectPrivate::get(metaObject)->signalCount +
589 QMetaObjectPrivate::get(metaObject)->propertyCount);
591 rv->append(engine, metaObject, revision, propertyFlags, methodFlags, signalFlags);
596 void QQmlPropertyCache::append(QQmlEngine *engine, const QMetaObject *metaObject,
598 QQmlPropertyData::Flag propertyFlags,
599 QQmlPropertyData::Flag methodFlags,
600 QQmlPropertyData::Flag signalFlags)
603 Q_ASSERT(constructor.IsEmpty()); // We should not be appending to an in-use property cache
605 _metaObject = metaObject;
607 bool dynamicMetaObject = isDynamicMetaObject(metaObject);
609 allowedRevisionCache.append(0);
611 int methodCount = metaObject->methodCount();
612 Q_ASSERT(QMetaObjectPrivate::get(metaObject)->revision >= 4);
613 int signalCount = metaObjectSignalCount(metaObject);
614 int classInfoCount = QMetaObjectPrivate::get(metaObject)->classInfoCount;
616 QQmlAccessorProperties::Properties accessorProperties;
618 if (classInfoCount) {
619 int classInfoOffset = metaObject->classInfoOffset();
620 bool hasFastProperty = false;
621 for (int ii = 0; ii < classInfoCount; ++ii) {
622 int idx = ii + classInfoOffset;
624 if (0 == qstrcmp(metaObject->classInfo(idx).name(), "qt_HasQmlAccessors")) {
625 hasFastProperty = true;
626 } else if (0 == qstrcmp(metaObject->classInfo(idx).name(), "DefaultProperty")) {
627 _defaultPropertyName = QString::fromUtf8(metaObject->classInfo(idx).value());
631 if (hasFastProperty) {
632 accessorProperties = QQmlAccessorProperties::properties(metaObject);
633 if (accessorProperties.count == 0)
634 qFatal("QQmlPropertyCache: %s has FastProperty class info, but has not "
635 "installed property accessors", metaObject->className());
638 accessorProperties = QQmlAccessorProperties::properties(metaObject);
639 if (accessorProperties.count != 0)
640 qFatal("QQmlPropertyCache: %s has fast property accessors, but is missing "
641 "FastProperty class info", metaObject->className());
646 //Used to block access to QObject::destroyed() and QObject::deleteLater() from QML
647 static const int destroyedIdx1 = QObject::staticMetaObject.indexOfSignal("destroyed(QObject*)");
648 static const int destroyedIdx2 = QObject::staticMetaObject.indexOfSignal("destroyed()");
649 static const int deleteLaterIdx = QObject::staticMetaObject.indexOfSlot("deleteLater()");
651 int methodOffset = metaObject->methodOffset();
652 int signalOffset = signalCount - QMetaObjectPrivate::get(metaObject)->signalCount;
654 // update() should have reserved enough space in the vector that this doesn't cause a realloc
655 // and invalidate the stringCache.
656 methodIndexCache.resize(methodCount - methodIndexCacheStart);
657 signalHandlerIndexCache.resize(signalCount - signalHandlerIndexCacheStart);
658 int signalHandlerIndex = signalOffset;
659 for (int ii = methodOffset; ii < methodCount; ++ii) {
660 if (ii == destroyedIdx1 || ii == destroyedIdx2 || ii == deleteLaterIdx)
662 QMetaMethod m = metaObject->method(ii);
663 if (m.access() == QMetaMethod::Private)
666 // Extract method name
667 // It's safe to keep the raw name pointer
668 Q_ASSERT(QMetaObjectPrivate::get(metaObject)->revision >= 7);
669 const char *rawName = m.name().constData();
670 const char *cptr = rawName;
673 utf8 |= *cptr & 0x80;
677 QQmlPropertyData *data = &methodIndexCache[ii - methodIndexCacheStart];
678 QQmlPropertyData *sigdata = 0;
682 if (data->isSignal())
683 data->flags |= signalFlags;
685 data->flags |= methodFlags;
687 if (!dynamicMetaObject)
688 data->flags |= QQmlPropertyData::IsDirect;
690 Q_ASSERT((allowedRevisionCache.count() - 1) < Q_INT16_MAX);
691 data->metaObjectOffset = allowedRevisionCache.count() - 1;
693 if (data->isSignal()) {
694 sigdata = &signalHandlerIndexCache[signalHandlerIndex - signalHandlerIndexCacheStart];
696 sigdata->flags |= QQmlPropertyData::IsSignalHandler;
699 QQmlPropertyData *old = 0;
702 QHashedString methodName(QString::fromUtf8(rawName, cptr - rawName));
703 if (QQmlPropertyData **it = stringCache.value(methodName))
705 stringCache.insert(methodName, data);
707 if (data->isSignal()) {
708 QHashedString on(QStringLiteral("on") % methodName.at(0).toUpper() % methodName.midRef(1));
709 stringCache.insert(on, sigdata);
710 ++signalHandlerIndex;
713 QHashedCStringRef methodName(rawName, cptr - rawName);
714 if (QQmlPropertyData **it = stringCache.value(methodName))
716 stringCache.insert(methodName, data);
718 if (data->isSignal()) {
719 int length = methodName.length();
721 QVarLengthArray<char, 128> str(length+3);
724 str[2] = toupper(rawName[0]);
726 memcpy(&str[3], &rawName[1], length - 1);
727 str[length + 2] = '\0';
729 QHashedString on(QString::fromLatin1(str.data()));
730 stringCache.insert(on, sigdata);
731 ++signalHandlerIndex;
736 // We only overload methods in the same class, exactly like C++
737 if (old->isFunction() && old->coreIndex >= methodOffset)
738 data->flags |= QQmlPropertyData::IsOverload;
739 data->overrideIndexIsProperty = !old->isFunction();
740 data->overrideIndex = old->coreIndex;
744 int propCount = metaObject->propertyCount();
745 int propOffset = metaObject->propertyOffset();
747 // update() should have reserved enough space in the vector that this doesn't cause a realloc
748 // and invalidate the stringCache.
749 propertyIndexCache.resize(propCount - propertyIndexCacheStart);
750 for (int ii = propOffset; ii < propCount; ++ii) {
751 QMetaProperty p = metaObject->property(ii);
752 if (!p.isScriptable())
755 const char *str = p.name();
757 const char *cptr = str;
759 utf8 |= *cptr & 0x80;
763 QQmlPropertyData *data = &propertyIndexCache[ii - propertyIndexCacheStart];
765 data->lazyLoad(p, engine);
766 data->flags |= propertyFlags;
768 if (!dynamicMetaObject)
769 data->flags |= QQmlPropertyData::IsDirect;
771 Q_ASSERT((allowedRevisionCache.count() - 1) < Q_INT16_MAX);
772 data->metaObjectOffset = allowedRevisionCache.count() - 1;
774 QQmlPropertyData *old = 0;
777 QHashedString propName(QString::fromUtf8(str, cptr - str));
778 if (QQmlPropertyData **it = stringCache.value(propName))
780 stringCache.insert(propName, data);
782 QHashedCStringRef propName(str, cptr - str);
783 if (QQmlPropertyData **it = stringCache.value(propName))
785 stringCache.insert(propName, data);
788 QQmlAccessorProperties::Property *accessorProperty = accessorProperties.property(str);
790 // Fast properties may not be overrides or revisioned
791 Q_ASSERT(accessorProperty == 0 || (old == 0 && data->revision == 0));
793 if (accessorProperty) {
794 data->flags |= QQmlPropertyData::HasAccessors;
795 data->accessors = accessorProperty->accessors;
796 data->accessorData = accessorProperty->data;
798 data->overrideIndexIsProperty = !old->isFunction();
799 data->overrideIndex = old->coreIndex;
804 void QQmlPropertyCache::resolve(QQmlPropertyData *data) const
806 Q_ASSERT(data->notFullyResolved());
808 data->propType = QMetaType::type(data->propTypeName);
810 if (!data->isFunction())
811 data->flags |= flagsForPropertyType(data->propType, engine);
813 data->flags &= ~QQmlPropertyData::NotFullyResolved;
816 void QQmlPropertyCache::updateRecur(QQmlEngine *engine, const QMetaObject *metaObject)
821 updateRecur(engine, metaObject->superClass());
823 append(engine, metaObject, -1);
826 void QQmlPropertyCache::update(QQmlEngine *engine, const QMetaObject *metaObject)
829 Q_ASSERT(metaObject);
830 Q_ASSERT(stringCache.isEmpty());
832 // Preallocate enough space in the index caches for all the properties/methods/signals that
833 // are not cached in a parent cache so that the caches never need to be reallocated as this
834 // would invalidate pointers stored in the stringCache.
835 int pc = metaObject->propertyCount();
836 int mc = metaObject->methodCount();
837 int sc = metaObjectSignalCount(metaObject);
838 propertyIndexCache.reserve(pc - propertyIndexCacheStart);
839 methodIndexCache.reserve(mc - methodIndexCacheStart);
840 signalHandlerIndexCache.reserve(sc - signalHandlerIndexCacheStart);
842 // Reserve enough space in the stringCache for all properties/methods/signals including those
843 // cached in a parent cache.
844 stringCache.reserve(pc + mc + sc);
846 updateRecur(engine,metaObject);
850 \a index MUST be in the signal index range (see QObjectPrivate::signalIndex()).
851 This is different from QMetaMethod::methodIndex().
854 QQmlPropertyCache::signal(int index) const
856 if (index < 0 || index >= (signalHandlerIndexCacheStart + signalHandlerIndexCache.count()))
859 if (index < signalHandlerIndexCacheStart)
860 return _parent->signal(index);
862 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&methodIndexCache.at(index - signalHandlerIndexCacheStart));
863 if (rv->notFullyResolved()) resolve(rv);
864 Q_ASSERT(rv->isSignal() || rv->coreIndex == -1);
868 int QQmlPropertyCache::methodIndexToSignalIndex(int index) const
870 if (index < 0 || index >= (methodIndexCacheStart + methodIndexCache.count()))
873 if (index < methodIndexCacheStart)
874 return _parent->methodIndexToSignalIndex(index);
876 return index - methodIndexCacheStart + signalHandlerIndexCacheStart;
880 QQmlPropertyCache::property(int index) const
882 if (index < 0 || index >= (propertyIndexCacheStart + propertyIndexCache.count()))
885 if (index < propertyIndexCacheStart)
886 return _parent->property(index);
888 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&propertyIndexCache.at(index - propertyIndexCacheStart));
889 if (rv->notFullyResolved()) resolve(rv);
894 QQmlPropertyCache::method(int index) const
896 if (index < 0 || index >= (methodIndexCacheStart + methodIndexCache.count()))
899 if (index < methodIndexCacheStart)
900 return _parent->method(index);
902 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&methodIndexCache.at(index - methodIndexCacheStart));
903 if (rv->notFullyResolved()) resolve(rv);
908 QQmlPropertyCache::property(const QHashedStringRef &str) const
910 QQmlPropertyData **rv = stringCache.value(str);
911 if (rv && (*rv)->notFullyResolved()) resolve(*rv);
916 QQmlPropertyCache::property(const QHashedCStringRef &str) const
918 QQmlPropertyData **rv = stringCache.value(str);
919 if (rv && (*rv)->notFullyResolved()) resolve(*rv);
924 QQmlPropertyCache::property(const QString &str) const
926 QQmlPropertyData **rv = stringCache.value(str);
927 if (rv && (*rv)->notFullyResolved()) resolve(*rv);
931 QString QQmlPropertyData::name(QObject *object)
936 return name(object->metaObject());
939 QString QQmlPropertyData::name(const QMetaObject *metaObject)
941 if (!metaObject || coreIndex == -1)
944 if (flags & IsFunction) {
945 QMetaMethod m = metaObject->method(coreIndex);
947 return QString::fromUtf8(m.name().constData());
949 QMetaProperty p = metaObject->property(coreIndex);
950 return QString::fromUtf8(p.name());
954 QStringList QQmlPropertyCache::propertyNames() const
957 for (StringCache::ConstIterator iter = stringCache.begin(); iter != stringCache.end(); ++iter)
958 keys.append(iter.key());
962 struct StaticQtMetaObject : public QObject
964 static const QMetaObject *get()
965 { return &static_cast<StaticQtMetaObject*> (0)->staticQtMetaObject; }
968 static int EnumType(const QMetaObject *metaobj, const QByteArray &str, int type)
972 int scopeIdx = str.lastIndexOf("::");
973 if (scopeIdx != -1) {
974 scope = str.left(scopeIdx);
975 name = str.mid(scopeIdx + 2);
979 const QMetaObject *meta;
981 meta = StaticQtMetaObject::get();
984 for (int i = meta->enumeratorCount() - 1; i >= 0; --i) {
985 QMetaEnum m = meta->enumerator(i);
986 if ((m.name() == name) && (scope.isEmpty() || (m.scope() == scope)))
987 return QVariant::Int;
993 \a index MUST be in the signal index range (see QObjectPrivate::signalIndex()).
994 This is different from QMetaMethod::methodIndex().
996 QList<QByteArray> QQmlPropertyCache::signalParameterNames(QObject *object, int index)
998 QQmlData *data = QQmlData::get(object, false);
999 if (data->propertyCache) {
1000 QQmlPropertyData *p = data->propertyCache->signal(index);
1001 if (!p->hasArguments())
1002 return QList<QByteArray>();
1005 return QMetaObjectPrivate::signal(object->metaObject(), index).parameterNames();
1008 // Returns an array of the arguments for method \a index. The first entry in the array
1009 // is the number of arguments.
1010 int *QQmlPropertyCache::methodParameterTypes(QObject *object, int index,
1011 QVarLengthArray<int, 9> &dummy,
1012 QByteArray *unknownTypeError)
1014 Q_ASSERT(object && index >= 0);
1016 QQmlData *ddata = QQmlData::get(object, false);
1018 if (ddata && ddata->propertyCache) {
1019 typedef QQmlPropertyCacheMethodArguments A;
1021 QQmlPropertyCache *c = ddata->propertyCache;
1022 Q_ASSERT(index < c->methodIndexCacheStart + c->methodIndexCache.count());
1024 while (index < c->methodIndexCacheStart)
1027 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&c->methodIndexCache.at(index - c->methodIndexCacheStart));
1030 return static_cast<A *>(rv->arguments)->arguments;
1032 const QMetaObject *metaObject = c->createMetaObject();
1033 Q_ASSERT(metaObject);
1034 QMetaMethod m = metaObject->method(index);
1036 int argc = m.parameterCount();
1037 A *args = static_cast<A *>(malloc(sizeof(A) + (argc + 1) * sizeof(int)));
1038 args->arguments[0] = argc;
1040 QList<QByteArray> argTypeNames; // Only loaded if needed
1042 for (int ii = 0; ii < argc; ++ii) {
1043 int type = m.parameterType(ii);
1044 QMetaType::TypeFlags flags = QMetaType::typeFlags(type);
1045 if (flags & QMetaType::IsEnumeration)
1046 type = QVariant::Int;
1047 else if (type == QMetaType::UnknownType ||
1048 (type >= (int)QVariant::UserType && !(flags & QMetaType::PointerToQObject) &&
1049 type != qMetaTypeId<QJSValue>())) {
1050 //the UserType clause is to catch registered QFlags
1051 if (argTypeNames.isEmpty())
1052 argTypeNames = m.parameterTypes();
1053 type = EnumType(object->metaObject(), argTypeNames.at(ii), type);
1055 if (type == QMetaType::UnknownType) {
1056 if (unknownTypeError) *unknownTypeError = argTypeNames.at(ii);
1060 args->arguments[ii + 1] = type;
1063 rv->arguments = args;
1064 args->next = c->argumentsCache;
1065 c->argumentsCache = args;
1066 return static_cast<A *>(rv->arguments)->arguments;
1069 QMetaMethod m = object->metaObject()->method(index);
1070 int argc = m.parameterCount();
1071 dummy.resize(argc + 1);
1073 QList<QByteArray> argTypeNames; // Only loaded if needed
1075 for (int ii = 0; ii < argc; ++ii) {
1076 int type = m.parameterType(ii);
1077 QMetaType::TypeFlags flags = QMetaType::typeFlags(type);
1078 if (flags & QMetaType::IsEnumeration)
1079 type = QVariant::Int;
1080 else if (type == QMetaType::UnknownType ||
1081 (type >= (int)QVariant::UserType && !(flags & QMetaType::PointerToQObject) &&
1082 type != qMetaTypeId<QJSValue>())) {
1083 //the UserType clause is to catch registered QFlags)
1084 if (argTypeNames.isEmpty())
1085 argTypeNames = m.parameterTypes();
1086 type = EnumType(object->metaObject(), argTypeNames.at(ii), type);
1088 if (type == QMetaType::UnknownType) {
1089 if (unknownTypeError) *unknownTypeError = argTypeNames.at(ii);
1092 dummy[ii + 1] = type;
1095 return dummy.data();
1099 // Returns the return type of the method.
1100 int QQmlPropertyCache::methodReturnType(QObject *object, const QQmlPropertyData &data,
1101 QByteArray *unknownTypeError)
1103 Q_ASSERT(object && data.coreIndex >= 0);
1105 int type = data.propType;
1107 const char *propTypeName = 0;
1109 if (type == QMetaType::UnknownType) {
1110 // Find the return type name from the method info
1113 QQmlData *ddata = QQmlData::get(object, false);
1114 if (ddata && ddata->propertyCache) {
1115 QQmlPropertyCache *c = ddata->propertyCache;
1116 Q_ASSERT(data.coreIndex < c->methodIndexCacheStart + c->methodIndexCache.count());
1118 while (data.coreIndex < c->methodIndexCacheStart)
1121 const QMetaObject *metaObject = c->createMetaObject();
1122 Q_ASSERT(metaObject);
1123 m = metaObject->method(data.coreIndex);
1125 m = object->metaObject()->method(data.coreIndex);
1128 type = m.returnType();
1129 propTypeName = m.typeName();
1132 QMetaType::TypeFlags flags = QMetaType::typeFlags(type);
1133 if (flags & QMetaType::IsEnumeration) {
1134 type = QVariant::Int;
1135 } else if (type == QMetaType::UnknownType ||
1136 (type >= (int)QVariant::UserType && !(flags & QMetaType::PointerToQObject) &&
1137 type != qMetaTypeId<QJSValue>())) {
1138 //the UserType clause is to catch registered QFlags
1139 type = EnumType(object->metaObject(), propTypeName, type);
1142 if (type == QMetaType::UnknownType) {
1143 if (unknownTypeError) *unknownTypeError = propTypeName;
1149 QQmlPropertyData qQmlPropertyCacheCreate(const QMetaObject *metaObject, const QString &property)
1151 Q_ASSERT(metaObject);
1153 QQmlPropertyData rv;
1155 const QMetaObject *cmo = metaObject;
1156 const QByteArray propertyName = property.toUtf8();
1158 int idx = cmo->indexOfProperty(propertyName);
1160 QMetaProperty p = cmo->property(idx);
1161 if (p.isScriptable()) {
1165 while (cmo && cmo->propertyOffset() >= idx)
1166 cmo = cmo->superClass();
1174 //Used to block access to QObject::destroyed() and QObject::deleteLater() from QML
1175 static const int destroyedIdx1 = QObject::staticMetaObject.indexOfSignal("destroyed(QObject*)");
1176 static const int destroyedIdx2 = QObject::staticMetaObject.indexOfSignal("destroyed()");
1177 static const int deleteLaterIdx = QObject::staticMetaObject.indexOfSlot("deleteLater()");
1179 int methodCount = metaObject->methodCount();
1180 for (int ii = methodCount - 1; ii >= 0; --ii) {
1181 if (ii == destroyedIdx1 || ii == destroyedIdx2 || ii == deleteLaterIdx)
1183 QMetaMethod m = metaObject->method(ii);
1184 if (m.access() == QMetaMethod::Private)
1186 QString methodName = QString::fromUtf8(m.name().constData());
1188 if (methodName == property) {
1197 inline const QString &qQmlPropertyCacheToString(const QString &string)
1202 inline QString qQmlPropertyCacheToString(const QHashedV8String &string)
1204 return QV8Engine::toStringStatic(string.string());
1207 template<typename T>
1209 qQmlPropertyCacheProperty(QQmlEngine *engine, QObject *obj, const T &name, QQmlPropertyData &local)
1211 QQmlPropertyCache *cache = 0;
1213 QQmlData *ddata = QQmlData::get(obj, false);
1215 if (ddata && ddata->propertyCache) {
1216 cache = ddata->propertyCache;
1217 } else if (engine) {
1218 QQmlEnginePrivate *ep = QQmlEnginePrivate::get(engine);
1219 cache = ep->cache(obj);
1221 ddata = QQmlData::get(obj, true);
1223 ddata->propertyCache = cache;
1227 QQmlPropertyData *rv = 0;
1230 rv = cache->property(name);
1232 local = qQmlPropertyCacheCreate(obj->metaObject(), qQmlPropertyCacheToString(name));
1233 if (local.isValid())
1241 QQmlPropertyCache::property(QQmlEngine *engine, QObject *obj,
1242 const QHashedV8String &name, QQmlPropertyData &local)
1244 return qQmlPropertyCacheProperty<QHashedV8String>(engine, obj, name, local);
1248 QQmlPropertyCache::property(QQmlEngine *engine, QObject *obj,
1249 const QString &name, QQmlPropertyData &local)
1251 return qQmlPropertyCacheProperty<QString>(engine, obj, name, local);
1254 static inline const QMetaObjectPrivate *priv(const uint* data)
1255 { return reinterpret_cast<const QMetaObjectPrivate*>(data); }
1257 bool QQmlPropertyCache::isDynamicMetaObject(const QMetaObject *mo)
1259 return priv(mo->d.data)->revision >= 3 && priv(mo->d.data)->flags & DynamicMetaObject;
1262 const char *QQmlPropertyCache::className() const
1264 if (!_ownMetaObject && _metaObject)
1265 return _metaObject->className();
1267 return _dynamicClassName.constData();
1270 void QQmlPropertyCache::toMetaObjectBuilder(QMetaObjectBuilder &builder)
1272 struct Sort { static bool lt(const QPair<QString, QQmlPropertyData *> &lhs,
1273 const QPair<QString, QQmlPropertyData *> &rhs) {
1274 return lhs.second->coreIndex < rhs.second->coreIndex;
1277 struct Insert { static void in(QQmlPropertyCache *This,
1278 QList<QPair<QString, QQmlPropertyData *> > &properties,
1279 QList<QPair<QString, QQmlPropertyData *> > &methods,
1280 StringCache::ConstIterator iter, QQmlPropertyData *data) {
1281 if (data->isSignalHandler())
1284 if (data->isFunction()) {
1285 if (data->coreIndex < This->methodIndexCacheStart)
1288 QPair<QString, QQmlPropertyData *> entry = qMakePair((QString)iter.key(), data);
1289 // Overrides can cause the entry to already exist
1290 if (!methods.contains(entry)) methods.append(entry);
1292 data = This->overrideData(data);
1293 if (data && !data->isFunction()) Insert::in(This, properties, methods, iter, data);
1295 if (data->coreIndex < This->propertyIndexCacheStart)
1298 QPair<QString, QQmlPropertyData *> entry = qMakePair((QString)iter.key(), data);
1299 // Overrides can cause the entry to already exist
1300 if (!properties.contains(entry)) properties.append(entry);
1302 data = This->overrideData(data);
1303 if (data) Insert::in(This, properties, methods, iter, data);
1308 builder.setClassName(_dynamicClassName);
1310 QList<QPair<QString, QQmlPropertyData *> > properties;
1311 QList<QPair<QString, QQmlPropertyData *> > methods;
1313 for (StringCache::ConstIterator iter = stringCache.begin(); iter != stringCache.end(); ++iter)
1314 Insert::in(this, properties, methods, iter, iter.value());
1316 Q_ASSERT(properties.count() == propertyIndexCache.count());
1317 Q_ASSERT(methods.count() == methodIndexCache.count());
1319 qSort(properties.begin(), properties.end(), Sort::lt);
1320 qSort(methods.begin(), methods.end(), Sort::lt);
1322 for (int ii = 0; ii < properties.count(); ++ii) {
1323 QQmlPropertyData *data = properties.at(ii).second;
1325 int notifierId = -1;
1326 if (data->notifyIndex != -1)
1327 notifierId = data->notifyIndex - signalHandlerIndexCacheStart;
1329 QMetaPropertyBuilder property = builder.addProperty(properties.at(ii).first.toUtf8(),
1330 QMetaType::typeName(data->propType),
1333 property.setReadable(true);
1334 property.setWritable(data->isWritable());
1335 property.setResettable(data->isResettable());
1338 for (int ii = 0; ii < methods.count(); ++ii) {
1339 QQmlPropertyData *data = methods.at(ii).second;
1341 QByteArray returnType;
1342 if (data->propType != 0)
1343 returnType = QMetaType::typeName(data->propType);
1345 QByteArray signature = methods.at(ii).first.toUtf8() + "(";
1347 QQmlPropertyCacheMethodArguments *arguments = 0;
1348 if (data->hasArguments()) {
1349 arguments = (QQmlPropertyCacheMethodArguments *)data->arguments;
1351 for (int ii = 0; ii < arguments->arguments[0]; ++ii) {
1352 if (ii != 0) signature.append(",");
1353 signature.append(QMetaType::typeName(arguments->arguments[1 + ii]));
1357 signature.append(")");
1359 QMetaMethodBuilder method;
1360 if (data->isSignal()) {
1361 method = builder.addSignal(signature);
1363 method = builder.addSlot(signature);
1365 method.setAccess(QMetaMethod::Protected);
1367 if (arguments && arguments->names)
1368 method.setParameterNames(*arguments->names);
1370 if (!returnType.isEmpty())
1371 method.setReturnType(returnType);
1374 if (!_defaultPropertyName.isEmpty()) {
1375 QQmlPropertyData *dp = property(_defaultPropertyName);
1376 if (dp && dp->coreIndex >= propertyIndexCacheStart) {
1377 Q_ASSERT(!dp->isFunction());
1378 builder.addClassInfo("DefaultProperty", _defaultPropertyName.toUtf8());
1383 // Returns true if \a from is assignable to a property of type \a to
1384 bool QQmlMetaObject::canConvert(const QQmlMetaObject &from, const QQmlMetaObject &to)
1386 Q_ASSERT(!from.isNull() && !to.isNull());
1388 struct I { static bool equal(const QMetaObject *lhs, const QMetaObject *rhs) {
1389 return lhs == rhs || (lhs && rhs && lhs->d.stringdata == rhs->d.stringdata);
1392 const QMetaObject *tom = to._m.isT1()?to._m.asT1()->metaObject():to._m.asT2();
1393 if (tom == &QObject::staticMetaObject) return true;
1395 if (from._m.isT1() && to._m.isT1()) { // QQmlPropertyCache -> QQmlPropertyCache
1396 QQmlPropertyCache *fromp = from._m.asT1();
1397 QQmlPropertyCache *top = to._m.asT1();
1400 if (fromp == top) return true;
1401 fromp = fromp->parent();
1403 } else if (from._m.isT1() && to._m.isT2()) { // QQmlPropertyCache -> QMetaObject
1404 QQmlPropertyCache *fromp = from._m.asT1();
1407 const QMetaObject *fromm = fromp->metaObject();
1408 if (fromm && I::equal(fromm, tom)) return true;
1409 fromp = fromp->parent();
1411 } else if (from._m.isT2() && to._m.isT1()) { // QMetaObject -> QQmlPropertyCache
1412 const QMetaObject *fromm = from._m.asT2();
1414 if (!tom) return false;
1417 if (I::equal(fromm, tom)) return true;
1418 fromm = fromm->superClass();
1420 } else { // QMetaObject -> QMetaObject
1421 const QMetaObject *fromm = from._m.asT2();
1424 if (I::equal(fromm, tom)) return true;
1425 fromm = fromm->superClass();
1432 QQmlPropertyCache *QQmlMetaObject::propertyCache(QQmlEnginePrivate *e) const
1434 if (_m.isNull()) return 0;
1435 if (_m.isT1()) return _m.asT1();
1436 else return e->cache(_m.asT2());