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 = p.notifySignalIndex();
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 = p.notifySignalIndex();
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();
213 Q_ASSERT(returnType != 0);
214 if ((*returnType != 'v') || (qstrcmp(returnType+1, "oid") != 0)) {
215 propTypeName = returnType;
216 flags |= NotFullyResolved;
219 if (m.parameterCount()) {
220 flags |= HasArguments;
221 if ((m.parameterCount() == 1) && (m.parameterTypes().first() == "QQmlV8Function*")) {
222 flags |= IsV8Function;
226 if (m.attributes() & QMetaMethod::Cloned)
229 Q_ASSERT(m.revision() <= Q_INT16_MAX);
230 revision = m.revision();
234 Creates a new empty QQmlPropertyCache.
236 QQmlPropertyCache::QQmlPropertyCache(QQmlEngine *e)
237 : engine(e), _parent(0), propertyIndexCacheStart(0), methodIndexCacheStart(0),
238 signalHandlerIndexCacheStart(0), _ownMetaObject(false), _metaObject(0), argumentsCache(0)
244 Creates a new QQmlPropertyCache of \a metaObject.
246 QQmlPropertyCache::QQmlPropertyCache(QQmlEngine *e, const QMetaObject *metaObject)
247 : engine(e), _parent(0), propertyIndexCacheStart(0), methodIndexCacheStart(0),
248 signalHandlerIndexCacheStart(0), _ownMetaObject(false), _metaObject(0), argumentsCache(0)
251 Q_ASSERT(metaObject);
253 update(engine, metaObject);
256 QQmlPropertyCache::~QQmlPropertyCache()
260 QQmlPropertyCacheMethodArguments *args = argumentsCache;
262 QQmlPropertyCacheMethodArguments *next = args->next;
263 if (args->names) delete args->names;
268 // We must clear this prior to releasing the parent incase it is a
271 if (_parent) _parent->release();
273 if (_ownMetaObject) free((void *)_metaObject);
279 void QQmlPropertyCache::destroy()
281 Q_ASSERT(engine || constructor.IsEmpty());
282 if (constructor.IsEmpty())
285 QQmlEnginePrivate::deleteInEngineThread(engine, this);
288 // This is inherited from QQmlCleanup, so it should only clear the things
289 // that are tied to the specific QQmlEngine.
290 void QQmlPropertyCache::clear()
292 qPersistentDispose(constructor);
296 QQmlPropertyCache *QQmlPropertyCache::copy(int reserve)
298 QQmlPropertyCache *cache = new QQmlPropertyCache(engine);
299 cache->_parent = this;
300 cache->_parent->addref();
301 cache->propertyIndexCacheStart = propertyIndexCache.count() + propertyIndexCacheStart;
302 cache->methodIndexCacheStart = methodIndexCache.count() + methodIndexCacheStart;
303 cache->signalHandlerIndexCacheStart = signalHandlerIndexCache.count() + signalHandlerIndexCacheStart;
304 cache->stringCache.linkAndReserve(stringCache, reserve);
305 cache->allowedRevisionCache = allowedRevisionCache;
306 cache->_metaObject = _metaObject;
307 cache->_defaultPropertyName = _defaultPropertyName;
309 // We specifically do *NOT* copy the constructor
314 QQmlPropertyCache *QQmlPropertyCache::copy()
319 QQmlPropertyCache *QQmlPropertyCache::copyAndReserve(QQmlEngine *, int propertyCount, int methodCount,
322 QQmlPropertyCache *rv = copy(propertyCount + methodCount + signalCount);
323 rv->propertyIndexCache.reserve(propertyCount);
324 rv->methodIndexCache.reserve(methodCount);
325 rv->signalHandlerIndexCache.reserve(signalCount);
331 void QQmlPropertyCache::appendProperty(const QString &name,
332 quint32 flags, int coreIndex, int propType, int notifyIndex)
334 QQmlPropertyData data;
335 data.propType = propType;
336 data.coreIndex = coreIndex;
337 data.notifyIndex = notifyIndex;
340 QHashedString string(name);
341 if (QQmlPropertyData **old = stringCache.value(string)) {
342 data.overrideIndexIsProperty = !(*old)->isFunction();
343 data.overrideIndex = (*old)->coreIndex;
346 propertyIndexCache.append(data);
348 stringCache.insert(string, propertyIndexCache.data() + propertyIndexCache.count() - 1);
351 void QQmlPropertyCache::appendProperty(const QHashedCStringRef &name,
352 quint32 flags, int coreIndex, int propType, int notifyIndex)
354 QQmlPropertyData data;
355 data.propType = propType;
356 data.coreIndex = coreIndex;
357 data.notifyIndex = notifyIndex;
360 if (QQmlPropertyData **old = stringCache.value(name)) {
361 data.overrideIndexIsProperty = !(*old)->isFunction();
362 data.overrideIndex = (*old)->coreIndex;
365 propertyIndexCache.append(data);
367 stringCache.insert(name, propertyIndexCache.data() + propertyIndexCache.count() - 1);
370 void QQmlPropertyCache::appendSignal(const QString &name, quint32 flags, int coreIndex,
371 const int *types, const QList<QByteArray> &names)
373 QQmlPropertyData data;
374 data.propType = QVariant::Invalid;
375 data.coreIndex = coreIndex;
379 QQmlPropertyData handler = data;
380 handler.flags |= QQmlPropertyData::IsSignalHandler;
383 int argumentCount = *types;
384 typedef QQmlPropertyCacheMethodArguments A;
385 A *args = static_cast<A *>(malloc(sizeof(A) + (argumentCount + 1) * sizeof(int)));
386 ::memcpy(args->arguments, types, (argumentCount + 1) * sizeof(int));
387 args->names = new QList<QByteArray>(names);
388 args->next = argumentsCache;
389 argumentsCache = args;
390 data.arguments = args;
393 QString handlerName = QLatin1String("on") + name;
394 handlerName[2] = handlerName[2].toUpper();
396 QHashedString string(name);
397 if (QQmlPropertyData **old = stringCache.value(string)) {
398 data.overrideIndexIsProperty = !(*old)->isFunction();
399 data.overrideIndex = (*old)->coreIndex;
402 methodIndexCache.append(data);
403 signalHandlerIndexCache.append(handler);
405 stringCache.insert(string, methodIndexCache.data() + methodIndexCache.count() - 1);
406 stringCache.insert(handlerName, signalHandlerIndexCache.data() + signalHandlerIndexCache.count() - 1);
409 void QQmlPropertyCache::appendSignal(const QHashedCStringRef &name, quint32 flags, int coreIndex,
410 const int *types, const QList<QByteArray> &names)
412 QQmlPropertyData data;
413 data.propType = QVariant::Invalid;
414 data.coreIndex = coreIndex;
418 QQmlPropertyData handler = data;
419 handler.flags |= QQmlPropertyData::IsSignalHandler;
422 int argumentCount = *types;
423 typedef QQmlPropertyCacheMethodArguments A;
424 A *args = static_cast<A *>(malloc(sizeof(A) + (argumentCount + 1) * sizeof(int)));
425 ::memcpy(args->arguments, types, (argumentCount + 1) * sizeof(int));
426 args->names = new QList<QByteArray>(names);
427 args->next = argumentsCache;
428 argumentsCache = args;
429 data.arguments = args;
432 QString handlerName = QLatin1String("on") + name.toUtf16();
433 handlerName[2] = handlerName[2].toUpper();
435 if (QQmlPropertyData **old = stringCache.value(name)) {
436 data.overrideIndexIsProperty = !(*old)->isFunction();
437 data.overrideIndex = (*old)->coreIndex;
440 methodIndexCache.append(data);
441 signalHandlerIndexCache.append(handler);
443 stringCache.insert(name, methodIndexCache.data() + methodIndexCache.count() - 1);
444 stringCache.insert(handlerName, signalHandlerIndexCache.data() + signalHandlerIndexCache.count() - 1);
447 void QQmlPropertyCache::appendMethod(const QString &name, quint32 flags, int coreIndex,
448 const QList<QByteArray> &names)
450 int argumentCount = names.count();
452 QQmlPropertyData data;
453 data.propType = QMetaType::QVariant;
454 data.coreIndex = coreIndex;
456 typedef QQmlPropertyCacheMethodArguments A;
457 A *args = static_cast<A *>(malloc(sizeof(A) + (argumentCount + 1) * sizeof(int)));
458 args->arguments[0] = argumentCount;
459 for (int ii = 0; ii < argumentCount; ++ii)
460 args->arguments[ii + 1] = QMetaType::QVariant;
463 args->names = new QList<QByteArray>(names);
464 args->next = argumentsCache;
465 argumentsCache = args;
466 data.arguments = args;
470 QHashedString string(name);
471 if (QQmlPropertyData **old = stringCache.value(string)) {
472 data.overrideIndexIsProperty = !(*old)->isFunction();
473 data.overrideIndex = (*old)->coreIndex;
476 methodIndexCache.append(data);
478 stringCache.insert(string, methodIndexCache.data() + methodIndexCache.count() - 1);
481 void QQmlPropertyCache::appendMethod(const QHashedCStringRef &name, quint32 flags, int coreIndex,
482 const QList<QByteArray> &names)
484 int argumentCount = names.count();
486 QQmlPropertyData data;
487 data.propType = QMetaType::QVariant;
488 data.coreIndex = coreIndex;
490 typedef QQmlPropertyCacheMethodArguments A;
491 A *args = static_cast<A *>(malloc(sizeof(A) + (argumentCount + 1) * sizeof(int)));
492 args->arguments[0] = argumentCount;
493 for (int ii = 0; ii < argumentCount; ++ii)
494 args->arguments[ii + 1] = QMetaType::QVariant;
497 args->names = new QList<QByteArray>(names);
498 args->next = argumentsCache;
499 argumentsCache = args;
500 data.arguments = args;
504 if (QQmlPropertyData **old = stringCache.value(name)) {
505 data.overrideIndexIsProperty = !(*old)->isFunction();
506 data.overrideIndex = (*old)->coreIndex;
509 methodIndexCache.append(data);
511 stringCache.insert(name, methodIndexCache.data() + methodIndexCache.count() - 1);
514 // Returns this property cache's metaObject. May be null if it hasn't been created yet.
515 const QMetaObject *QQmlPropertyCache::metaObject() const
520 // Returns this property cache's metaObject, creating it if necessary.
521 const QMetaObject *QQmlPropertyCache::createMetaObject()
524 _ownMetaObject = true;
526 QMetaObjectBuilder builder;
527 toMetaObjectBuilder(builder);
528 builder.setSuperClass(_parent->createMetaObject());
529 _metaObject = builder.toMetaObject();
535 // Returns the name of the default property for this cache
536 QString QQmlPropertyCache::defaultPropertyName() const
538 return _defaultPropertyName;
541 QQmlPropertyData *QQmlPropertyCache::defaultProperty() const
543 return property(defaultPropertyName());
546 QQmlPropertyCache *QQmlPropertyCache::parent() const
551 // Returns the first C++ type's QMetaObject - that is, the first QMetaObject not created by
553 const QMetaObject *QQmlPropertyCache::firstCppMetaObject() const
555 while (_parent && (_metaObject == 0 || _ownMetaObject))
556 return _parent->firstCppMetaObject();
561 QQmlPropertyCache::copyAndAppend(QQmlEngine *engine, const QMetaObject *metaObject,
562 QQmlPropertyData::Flag propertyFlags,
563 QQmlPropertyData::Flag methodFlags,
564 QQmlPropertyData::Flag signalFlags)
566 return copyAndAppend(engine, metaObject, -1, propertyFlags, methodFlags, signalFlags);
570 QQmlPropertyCache::copyAndAppend(QQmlEngine *engine, const QMetaObject *metaObject,
572 QQmlPropertyData::Flag propertyFlags,
573 QQmlPropertyData::Flag methodFlags,
574 QQmlPropertyData::Flag signalFlags)
576 Q_ASSERT(QMetaObjectPrivate::get(metaObject)->revision >= 4);
578 // Reserve enough space in the name hash for all the methods (including signals), all the
579 // signal handlers and all the properties. This assumes no name clashes, but this is the
581 QQmlPropertyCache *rv = copy(QMetaObjectPrivate::get(metaObject)->methodCount +
582 QMetaObjectPrivate::get(metaObject)->signalCount +
583 QMetaObjectPrivate::get(metaObject)->propertyCount);
585 rv->append(engine, metaObject, revision, propertyFlags, methodFlags, signalFlags);
590 void QQmlPropertyCache::append(QQmlEngine *engine, const QMetaObject *metaObject,
592 QQmlPropertyData::Flag propertyFlags,
593 QQmlPropertyData::Flag methodFlags,
594 QQmlPropertyData::Flag signalFlags)
597 Q_ASSERT(constructor.IsEmpty()); // We should not be appending to an in-use property cache
599 _metaObject = metaObject;
601 bool dynamicMetaObject = isDynamicMetaObject(metaObject);
603 allowedRevisionCache.append(0);
605 int methodCount = metaObject->methodCount();
606 Q_ASSERT(QMetaObjectPrivate::get(metaObject)->revision >= 4);
607 int signalCount = metaObjectSignalCount(metaObject);
608 int classInfoCount = QMetaObjectPrivate::get(metaObject)->classInfoCount;
610 QQmlAccessorProperties::Properties accessorProperties;
612 if (classInfoCount) {
613 int classInfoOffset = metaObject->classInfoOffset();
614 bool hasFastProperty = false;
615 for (int ii = 0; ii < classInfoCount; ++ii) {
616 int idx = ii + classInfoOffset;
618 if (0 == qstrcmp(metaObject->classInfo(idx).name(), "qt_HasQmlAccessors")) {
619 hasFastProperty = true;
620 } else if (0 == qstrcmp(metaObject->classInfo(idx).name(), "DefaultProperty")) {
621 _defaultPropertyName = QString::fromUtf8(metaObject->classInfo(idx).value());
625 if (hasFastProperty) {
626 accessorProperties = QQmlAccessorProperties::properties(metaObject);
627 if (accessorProperties.count == 0)
628 qFatal("QQmlPropertyCache: %s has FastProperty class info, but has not "
629 "installed property accessors", metaObject->className());
632 accessorProperties = QQmlAccessorProperties::properties(metaObject);
633 if (accessorProperties.count != 0)
634 qFatal("QQmlPropertyCache: %s has fast property accessors, but is missing "
635 "FastProperty class info", metaObject->className());
640 //Used to block access to QObject::destroyed() and QObject::deleteLater() from QML
641 static const int destroyedIdx1 = QObject::staticMetaObject.indexOfSignal("destroyed(QObject*)");
642 static const int destroyedIdx2 = QObject::staticMetaObject.indexOfSignal("destroyed()");
643 static const int deleteLaterIdx = QObject::staticMetaObject.indexOfSlot("deleteLater()");
645 int methodOffset = metaObject->methodOffset();
646 int signalOffset = signalCount - QMetaObjectPrivate::get(metaObject)->signalCount;
648 // update() should have reserved enough space in the vector that this doesn't cause a realloc
649 // and invalidate the stringCache.
650 methodIndexCache.resize(methodCount - methodIndexCacheStart);
651 signalHandlerIndexCache.resize(signalCount - signalHandlerIndexCacheStart);
652 int signalHandlerIndex = signalOffset;
653 for (int ii = methodOffset; ii < methodCount; ++ii) {
654 if (ii == destroyedIdx1 || ii == destroyedIdx2 || ii == deleteLaterIdx)
656 QMetaMethod m = metaObject->method(ii);
657 if (m.access() == QMetaMethod::Private)
660 // Extract method name
661 // It's safe to keep the raw name pointer
662 Q_ASSERT(QMetaObjectPrivate::get(metaObject)->revision >= 7);
663 const char *rawName = m.name().constData();
664 const char *cptr = rawName;
667 utf8 |= *cptr & 0x80;
671 QQmlPropertyData *data = &methodIndexCache[ii - methodIndexCacheStart];
672 QQmlPropertyData *sigdata = 0;
676 if (data->isSignal())
677 data->flags |= signalFlags;
679 data->flags |= methodFlags;
681 if (!dynamicMetaObject)
682 data->flags |= QQmlPropertyData::IsDirect;
684 Q_ASSERT((allowedRevisionCache.count() - 1) < Q_INT16_MAX);
685 data->metaObjectOffset = allowedRevisionCache.count() - 1;
687 if (data->isSignal()) {
688 sigdata = &signalHandlerIndexCache[signalHandlerIndex - signalHandlerIndexCacheStart];
690 sigdata->flags |= QQmlPropertyData::IsSignalHandler;
693 QQmlPropertyData *old = 0;
696 QHashedString methodName(QString::fromUtf8(rawName, cptr - rawName));
697 if (QQmlPropertyData **it = stringCache.value(methodName))
699 stringCache.insert(methodName, data);
701 if (data->isSignal()) {
702 QHashedString on(QStringLiteral("on") % methodName.at(0).toUpper() % methodName.midRef(1));
703 stringCache.insert(on, sigdata);
704 ++signalHandlerIndex;
707 QHashedCStringRef methodName(rawName, cptr - rawName);
708 if (QQmlPropertyData **it = stringCache.value(methodName))
710 stringCache.insert(methodName, data);
712 if (data->isSignal()) {
713 int length = methodName.length();
715 QVarLengthArray<char, 128> str(length+3);
718 str[2] = toupper(rawName[0]);
720 memcpy(&str[3], &rawName[1], length - 1);
721 str[length + 2] = '\0';
723 QHashedString on(QString::fromLatin1(str.data()));
724 stringCache.insert(on, sigdata);
725 ++signalHandlerIndex;
730 // We only overload methods in the same class, exactly like C++
731 if (old->isFunction() && old->coreIndex >= methodOffset)
732 data->flags |= QQmlPropertyData::IsOverload;
733 data->overrideIndexIsProperty = !old->isFunction();
734 data->overrideIndex = old->coreIndex;
738 int propCount = metaObject->propertyCount();
739 int propOffset = metaObject->propertyOffset();
741 // update() should have reserved enough space in the vector that this doesn't cause a realloc
742 // and invalidate the stringCache.
743 propertyIndexCache.resize(propCount - propertyIndexCacheStart);
744 for (int ii = propOffset; ii < propCount; ++ii) {
745 QMetaProperty p = metaObject->property(ii);
746 if (!p.isScriptable())
749 const char *str = p.name();
751 const char *cptr = str;
753 utf8 |= *cptr & 0x80;
757 QQmlPropertyData *data = &propertyIndexCache[ii - propertyIndexCacheStart];
759 data->lazyLoad(p, engine);
760 data->flags |= propertyFlags;
762 if (!dynamicMetaObject)
763 data->flags |= QQmlPropertyData::IsDirect;
765 Q_ASSERT((allowedRevisionCache.count() - 1) < Q_INT16_MAX);
766 data->metaObjectOffset = allowedRevisionCache.count() - 1;
768 QQmlPropertyData *old = 0;
771 QHashedString propName(QString::fromUtf8(str, cptr - str));
772 if (QQmlPropertyData **it = stringCache.value(propName))
774 stringCache.insert(propName, data);
776 QHashedCStringRef propName(str, cptr - str);
777 if (QQmlPropertyData **it = stringCache.value(propName))
779 stringCache.insert(propName, data);
782 QQmlAccessorProperties::Property *accessorProperty = accessorProperties.property(str);
784 // Fast properties may not be overrides or revisioned
785 Q_ASSERT(accessorProperty == 0 || (old == 0 && data->revision == 0));
787 if (accessorProperty) {
788 data->flags |= QQmlPropertyData::HasAccessors;
789 data->accessors = accessorProperty->accessors;
790 data->accessorData = accessorProperty->data;
792 data->overrideIndexIsProperty = !old->isFunction();
793 data->overrideIndex = old->coreIndex;
798 void QQmlPropertyCache::resolve(QQmlPropertyData *data) const
800 Q_ASSERT(data->notFullyResolved());
802 data->propType = QMetaType::type(data->propTypeName);
804 if (!data->isFunction())
805 data->flags |= flagsForPropertyType(data->propType, engine);
807 data->flags &= ~QQmlPropertyData::NotFullyResolved;
810 void QQmlPropertyCache::updateRecur(QQmlEngine *engine, const QMetaObject *metaObject)
815 updateRecur(engine, metaObject->superClass());
817 append(engine, metaObject, -1);
820 void QQmlPropertyCache::update(QQmlEngine *engine, const QMetaObject *metaObject)
823 Q_ASSERT(metaObject);
824 Q_ASSERT(stringCache.isEmpty());
826 // Preallocate enough space in the index caches for all the properties/methods/signals that
827 // are not cached in a parent cache so that the caches never need to be reallocated as this
828 // would invalidate pointers stored in the stringCache.
829 int pc = metaObject->propertyCount();
830 int mc = metaObject->methodCount();
831 int sc = metaObjectSignalCount(metaObject);
832 propertyIndexCache.reserve(pc - propertyIndexCacheStart);
833 methodIndexCache.reserve(mc - methodIndexCacheStart);
834 signalHandlerIndexCache.reserve(sc - signalHandlerIndexCacheStart);
836 // Reserve enough space in the stringCache for all properties/methods/signals including those
837 // cached in a parent cache.
838 stringCache.reserve(pc + mc + sc);
840 updateRecur(engine,metaObject);
844 QQmlPropertyCache::property(int index) const
846 if (index < 0 || index >= (propertyIndexCacheStart + propertyIndexCache.count()))
849 if (index < propertyIndexCacheStart)
850 return _parent->property(index);
852 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&propertyIndexCache.at(index - propertyIndexCacheStart));
853 if (rv->notFullyResolved()) resolve(rv);
858 QQmlPropertyCache::method(int index) const
860 if (index < 0 || index >= (methodIndexCacheStart + methodIndexCache.count()))
863 if (index < methodIndexCacheStart)
864 return _parent->method(index);
866 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&methodIndexCache.at(index - methodIndexCacheStart));
867 if (rv->notFullyResolved()) resolve(rv);
872 QQmlPropertyCache::property(const QHashedStringRef &str) const
874 QQmlPropertyData **rv = stringCache.value(str);
875 if (rv && (*rv)->notFullyResolved()) resolve(*rv);
880 QQmlPropertyCache::property(const QHashedCStringRef &str) const
882 QQmlPropertyData **rv = stringCache.value(str);
883 if (rv && (*rv)->notFullyResolved()) resolve(*rv);
888 QQmlPropertyCache::property(const QString &str) const
890 QQmlPropertyData **rv = stringCache.value(str);
891 if (rv && (*rv)->notFullyResolved()) resolve(*rv);
895 QString QQmlPropertyData::name(QObject *object)
900 return name(object->metaObject());
903 QString QQmlPropertyData::name(const QMetaObject *metaObject)
905 if (!metaObject || coreIndex == -1)
908 if (flags & IsFunction) {
909 QMetaMethod m = metaObject->method(coreIndex);
911 return QString::fromUtf8(m.name().constData());
913 QMetaProperty p = metaObject->property(coreIndex);
914 return QString::fromUtf8(p.name());
918 QStringList QQmlPropertyCache::propertyNames() const
921 for (StringCache::ConstIterator iter = stringCache.begin(); iter != stringCache.end(); ++iter)
922 keys.append(iter.key());
926 struct StaticQtMetaObject : public QObject
928 static const QMetaObject *get()
929 { return &static_cast<StaticQtMetaObject*> (0)->staticQtMetaObject; }
932 static int EnumType(const QMetaObject *metaobj, const QByteArray &str, int type)
936 int scopeIdx = str.lastIndexOf("::");
937 if (scopeIdx != -1) {
938 scope = str.left(scopeIdx);
939 name = str.mid(scopeIdx + 2);
943 const QMetaObject *meta;
945 meta = StaticQtMetaObject::get();
948 for (int i = meta->enumeratorCount() - 1; i >= 0; --i) {
949 QMetaEnum m = meta->enumerator(i);
950 if ((m.name() == name) && (scope.isEmpty() || (m.scope() == scope)))
951 return QVariant::Int;
956 QList<QByteArray> QQmlPropertyCache::methodParameterNames(QObject *object, int index)
958 QQmlData *data = QQmlData::get(object, false);
959 if (data->propertyCache) {
960 QQmlPropertyData *p = data->propertyCache->method(index);
961 if (!p->hasArguments())
962 return QList<QByteArray>();
965 return object->metaObject()->method(index).parameterNames();
968 // Returns an array of the arguments for method \a index. The first entry in the array
969 // is the number of arguments.
970 int *QQmlPropertyCache::methodParameterTypes(QObject *object, int index,
971 QVarLengthArray<int, 9> &dummy,
972 QByteArray *unknownTypeError)
974 Q_ASSERT(object && index >= 0);
976 QQmlData *ddata = QQmlData::get(object, false);
978 if (ddata && ddata->propertyCache) {
979 typedef QQmlPropertyCacheMethodArguments A;
981 QQmlPropertyCache *c = ddata->propertyCache;
982 Q_ASSERT(index < c->methodIndexCacheStart + c->methodIndexCache.count());
984 while (index < c->methodIndexCacheStart)
987 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&c->methodIndexCache.at(index - c->methodIndexCacheStart));
990 return static_cast<A *>(rv->arguments)->arguments;
992 const QMetaObject *metaObject = c->createMetaObject();
993 Q_ASSERT(metaObject);
994 QMetaMethod m = metaObject->method(index);
996 int argc = m.parameterCount();
997 A *args = static_cast<A *>(malloc(sizeof(A) + (argc + 1) * sizeof(int)));
998 args->arguments[0] = argc;
1000 QList<QByteArray> argTypeNames; // Only loaded if needed
1002 for (int ii = 0; ii < argc; ++ii) {
1003 int type = m.parameterType(ii);
1004 QMetaType::TypeFlags flags = QMetaType::typeFlags(type);
1005 if (flags & QMetaType::IsEnumeration)
1006 type = QVariant::Int;
1007 else if (type == QMetaType::UnknownType ||
1008 (type >= (int)QVariant::UserType && !(flags & QMetaType::PointerToQObject) &&
1009 type != qMetaTypeId<QJSValue>())) {
1010 //the UserType clause is to catch registered QFlags
1011 if (argTypeNames.isEmpty())
1012 argTypeNames = m.parameterTypes();
1013 type = EnumType(object->metaObject(), argTypeNames.at(ii), type);
1015 if (type == QMetaType::UnknownType) {
1016 if (unknownTypeError) *unknownTypeError = argTypeNames.at(ii);
1020 args->arguments[ii + 1] = type;
1023 rv->arguments = args;
1024 args->next = c->argumentsCache;
1025 c->argumentsCache = args;
1026 return static_cast<A *>(rv->arguments)->arguments;
1029 QMetaMethod m = object->metaObject()->method(index);
1030 int argc = m.parameterCount();
1031 dummy.resize(argc + 1);
1033 QList<QByteArray> argTypeNames; // Only loaded if needed
1035 for (int ii = 0; ii < argc; ++ii) {
1036 int type = m.parameterType(ii);
1037 QMetaType::TypeFlags flags = QMetaType::typeFlags(type);
1038 if (flags & QMetaType::IsEnumeration)
1039 type = QVariant::Int;
1040 else if (type == QMetaType::UnknownType ||
1041 (type >= (int)QVariant::UserType && !(flags & QMetaType::PointerToQObject) &&
1042 type != qMetaTypeId<QJSValue>())) {
1043 //the UserType clause is to catch registered QFlags)
1044 if (argTypeNames.isEmpty())
1045 argTypeNames = m.parameterTypes();
1046 type = EnumType(object->metaObject(), argTypeNames.at(ii), type);
1048 if (type == QMetaType::UnknownType) {
1049 if (unknownTypeError) *unknownTypeError = argTypeNames.at(ii);
1052 dummy[ii + 1] = type;
1055 return dummy.data();
1059 QQmlPropertyData qQmlPropertyCacheCreate(const QMetaObject *metaObject, const QString &property)
1061 Q_ASSERT(metaObject);
1063 QQmlPropertyData rv;
1065 const QMetaObject *cmo = metaObject;
1066 const QByteArray propertyName = property.toUtf8();
1068 int idx = cmo->indexOfProperty(propertyName);
1070 QMetaProperty p = cmo->property(idx);
1071 if (p.isScriptable()) {
1075 while (cmo && cmo->propertyOffset() >= idx)
1076 cmo = cmo->superClass();
1084 //Used to block access to QObject::destroyed() and QObject::deleteLater() from QML
1085 static const int destroyedIdx1 = QObject::staticMetaObject.indexOfSignal("destroyed(QObject*)");
1086 static const int destroyedIdx2 = QObject::staticMetaObject.indexOfSignal("destroyed()");
1087 static const int deleteLaterIdx = QObject::staticMetaObject.indexOfSlot("deleteLater()");
1089 int methodCount = metaObject->methodCount();
1090 for (int ii = methodCount - 1; ii >= 0; --ii) {
1091 if (ii == destroyedIdx1 || ii == destroyedIdx2 || ii == deleteLaterIdx)
1093 QMetaMethod m = metaObject->method(ii);
1094 if (m.access() == QMetaMethod::Private)
1096 QString methodName = QString::fromUtf8(m.name().constData());
1098 if (methodName == property) {
1107 inline const QString &qQmlPropertyCacheToString(const QString &string)
1112 inline QString qQmlPropertyCacheToString(const QHashedV8String &string)
1114 return QV8Engine::toStringStatic(string.string());
1117 template<typename T>
1119 qQmlPropertyCacheProperty(QQmlEngine *engine, QObject *obj, const T &name, QQmlPropertyData &local)
1121 QQmlPropertyCache *cache = 0;
1123 QQmlData *ddata = QQmlData::get(obj, false);
1125 if (ddata && ddata->propertyCache) {
1126 cache = ddata->propertyCache;
1127 } else if (engine) {
1128 QQmlEnginePrivate *ep = QQmlEnginePrivate::get(engine);
1129 cache = ep->cache(obj);
1131 ddata = QQmlData::get(obj, true);
1133 ddata->propertyCache = cache;
1137 QQmlPropertyData *rv = 0;
1140 rv = cache->property(name);
1142 local = qQmlPropertyCacheCreate(obj->metaObject(), qQmlPropertyCacheToString(name));
1143 if (local.isValid())
1151 QQmlPropertyCache::property(QQmlEngine *engine, QObject *obj,
1152 const QHashedV8String &name, QQmlPropertyData &local)
1154 return qQmlPropertyCacheProperty<QHashedV8String>(engine, obj, name, local);
1158 QQmlPropertyCache::property(QQmlEngine *engine, QObject *obj,
1159 const QString &name, QQmlPropertyData &local)
1161 return qQmlPropertyCacheProperty<QString>(engine, obj, name, local);
1164 static inline const QMetaObjectPrivate *priv(const uint* data)
1165 { return reinterpret_cast<const QMetaObjectPrivate*>(data); }
1167 bool QQmlPropertyCache::isDynamicMetaObject(const QMetaObject *mo)
1169 return priv(mo->d.data)->revision >= 3 && priv(mo->d.data)->flags & DynamicMetaObject;
1172 const char *QQmlPropertyCache::className() const
1174 if (!_ownMetaObject && _metaObject)
1175 return _metaObject->className();
1177 return _dynamicClassName.constData();
1180 void QQmlPropertyCache::toMetaObjectBuilder(QMetaObjectBuilder &builder)
1182 struct Sort { static bool lt(const QPair<QString, QQmlPropertyData *> &lhs,
1183 const QPair<QString, QQmlPropertyData *> &rhs) {
1184 return lhs.second->coreIndex < rhs.second->coreIndex;
1187 struct Insert { static void in(QQmlPropertyCache *This,
1188 QList<QPair<QString, QQmlPropertyData *> > &properties,
1189 QList<QPair<QString, QQmlPropertyData *> > &methods,
1190 StringCache::ConstIterator iter, QQmlPropertyData *data) {
1191 if (data->isSignalHandler())
1194 if (data->isFunction()) {
1195 if (data->coreIndex < This->methodIndexCacheStart)
1198 QPair<QString, QQmlPropertyData *> entry = qMakePair((QString)iter.key(), data);
1199 // Overrides can cause the entry to already exist
1200 if (!methods.contains(entry)) methods.append(entry);
1202 QQmlPropertyData *olddata = data;
1203 data = This->overrideData(data);
1204 if (data) Insert::in(This, properties, methods, iter, data);
1206 if (data->coreIndex < This->propertyIndexCacheStart)
1209 QPair<QString, QQmlPropertyData *> entry = qMakePair((QString)iter.key(), data);
1210 // Overrides can cause the entry to already exist
1211 if (!properties.contains(entry)) properties.append(entry);
1213 QQmlPropertyData *olddata = data;
1214 data = This->overrideData(data);
1215 if (data) Insert::in(This, properties, methods, iter, data);
1220 builder.setClassName(_dynamicClassName);
1222 QList<QPair<QString, QQmlPropertyData *> > properties;
1223 QList<QPair<QString, QQmlPropertyData *> > methods;
1225 for (StringCache::ConstIterator iter = stringCache.begin(); iter != stringCache.end(); ++iter)
1226 Insert::in(this, properties, methods, iter, iter.value());
1228 Q_ASSERT(properties.count() == propertyIndexCache.count());
1229 Q_ASSERT(methods.count() == methodIndexCache.count());
1231 qSort(properties.begin(), properties.end(), Sort::lt);
1232 qSort(methods.begin(), methods.end(), Sort::lt);
1234 for (int ii = 0; ii < properties.count(); ++ii) {
1235 QQmlPropertyData *data = properties.at(ii).second;
1237 int notifierId = -1;
1238 if (data->notifyIndex != -1)
1239 notifierId = data->notifyIndex - methodIndexCacheStart;
1241 QMetaPropertyBuilder property = builder.addProperty(properties.at(ii).first.toUtf8(),
1242 QMetaType::typeName(data->propType),
1245 property.setReadable(true);
1246 property.setWritable(data->isWritable());
1247 property.setResettable(data->isResettable());
1250 for (int ii = 0; ii < methods.count(); ++ii) {
1251 QQmlPropertyData *data = methods.at(ii).second;
1253 QByteArray returnType;
1254 if (data->propType != 0)
1255 returnType = QMetaType::typeName(data->propType);
1257 QByteArray signature = methods.at(ii).first.toUtf8() + "(";
1259 QQmlPropertyCacheMethodArguments *arguments = 0;
1260 if (data->hasArguments()) {
1261 arguments = (QQmlPropertyCacheMethodArguments *)data->arguments;
1263 for (int ii = 0; ii < arguments->arguments[0]; ++ii) {
1264 if (ii != 0) signature.append(",");
1265 signature.append(QMetaType::typeName(arguments->arguments[1 + ii]));
1269 signature.append(")");
1271 QMetaMethodBuilder method;
1272 if (data->isSignal()) {
1273 method = builder.addSignal(signature);
1275 method = builder.addSlot(signature);
1277 method.setAccess(QMetaMethod::Protected);
1279 if (arguments && arguments->names)
1280 method.setParameterNames(*arguments->names);
1282 if (!returnType.isEmpty())
1283 method.setReturnType(returnType);
1286 if (!_defaultPropertyName.isEmpty()) {
1287 QQmlPropertyData *dp = property(_defaultPropertyName);
1288 if (dp && dp->coreIndex >= propertyIndexCacheStart) {
1289 Q_ASSERT(!dp->isFunction());
1290 builder.addClassInfo("DefaultProperty", _defaultPropertyName.toUtf8());
1295 // Returns true if \a from is assignable to a property of type \a to
1296 bool QQmlMetaObject::canConvert(const QQmlMetaObject &from, const QQmlMetaObject &to)
1298 Q_ASSERT(!from.isNull() && !to.isNull());
1300 struct I { static bool equal(const QMetaObject *lhs, const QMetaObject *rhs) {
1301 return lhs == rhs || (lhs && rhs && lhs->d.stringdata == rhs->d.stringdata);
1304 const QMetaObject *tom = to._m.isT1()?to._m.asT1()->metaObject():to._m.asT2();
1305 if (tom == &QObject::staticMetaObject) return true;
1307 if (from._m.isT1() && to._m.isT1()) { // QQmlPropertyCache -> QQmlPropertyCache
1308 QQmlPropertyCache *fromp = from._m.asT1();
1309 QQmlPropertyCache *top = to._m.asT1();
1312 if (fromp == top) return true;
1313 fromp = fromp->parent();
1315 } else if (from._m.isT1() && to._m.isT2()) { // QQmlPropertyCache -> QMetaObject
1316 QQmlPropertyCache *fromp = from._m.asT1();
1319 const QMetaObject *fromm = fromp->metaObject();
1320 if (fromm && I::equal(fromm, tom)) return true;
1321 fromp = fromp->parent();
1323 } else if (from._m.isT2() && to._m.isT1()) { // QMetaObject -> QQmlPropertyCache
1324 const QMetaObject *fromm = from._m.asT2();
1326 if (!tom) return false;
1329 if (I::equal(fromm, tom)) return true;
1330 fromm = fromm->superClass();
1332 } else { // QMetaObject -> QMetaObject
1333 const QMetaObject *fromm = from._m.asT2();
1336 if (I::equal(fromm, tom)) return true;
1337 fromm = fromm->superClass();
1344 QQmlPropertyCache *QQmlMetaObject::propertyCache(QQmlEnginePrivate *e) const
1346 if (_m.isNull()) return 0;
1347 if (_m.isT1()) return _m.asT1();
1348 else return e->cache(_m.asT2());