1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include "qqmlpropertycache_p.h"
44 #include <private/qqmlengine_p.h>
45 #include <private/qqmlbinding_p.h>
46 #include <private/qqmlvmemetaobject_p.h>
47 #include <private/qv8engine_p.h>
49 #include <private/qmetaobject_p.h>
50 #include <private/qqmlaccessors_p.h>
51 #include <private/qmetaobjectbuilder_p.h>
52 #include <private/qqmlrewrite_p.h>
54 #include <QtCore/qdebug.h>
56 #include <ctype.h> // for toupper
60 // nonstandard extension used : zero-sized array in struct/union.
61 # pragma warning( disable : 4200 )
64 Q_DECLARE_METATYPE(QJSValue)
65 Q_DECLARE_METATYPE(QQmlV8Handle);
69 #define Q_INT16_MAX 32767
71 class QQmlPropertyCacheMethodArguments
74 QQmlPropertyCacheMethodArguments *next;
76 //for signal handler rewrites
77 QString *signalParameterStringForJS;
78 int signalParameterCountForJS:30;
82 QList<QByteArray> *names;
86 // Flags that do *NOT* depend on the property's QMetaProperty::userType() and thus are quick
88 static QQmlPropertyData::Flags fastFlagsForProperty(const QMetaProperty &p)
90 QQmlPropertyData::Flags flags;
93 flags |= QQmlPropertyData::IsConstant;
95 flags |= QQmlPropertyData::IsWritable;
97 flags |= QQmlPropertyData::IsResettable;
99 flags |= QQmlPropertyData::IsFinal;
101 flags |= QQmlPropertyData::IsEnumType;
106 // Flags that do depend on the property's QMetaProperty::userType() and thus are slow to
108 static QQmlPropertyData::Flags flagsForPropertyType(int propType, QQmlEngine *engine)
110 Q_ASSERT(propType != -1);
112 QQmlPropertyData::Flags flags;
114 if (propType == QMetaType::QObjectStar) {
115 flags |= QQmlPropertyData::IsQObjectDerived;
116 } else if (propType == QMetaType::QVariant) {
117 flags |= QQmlPropertyData::IsQVariant;
118 } else if (propType < (int)QVariant::UserType) {
119 } else if (propType == qMetaTypeId<QQmlBinding *>()) {
120 flags |= QQmlPropertyData::IsQmlBinding;
121 } else if (propType == qMetaTypeId<QJSValue>()) {
122 flags |= QQmlPropertyData::IsQJSValue;
123 } else if (propType == qMetaTypeId<QQmlV8Handle>()) {
124 flags |= QQmlPropertyData::IsV8Handle;
126 QQmlMetaType::TypeCategory cat =
127 engine ? QQmlEnginePrivate::get(engine)->typeCategory(propType)
128 : QQmlMetaType::typeCategory(propType);
130 if (cat == QQmlMetaType::Object)
131 flags |= QQmlPropertyData::IsQObjectDerived;
132 else if (cat == QQmlMetaType::List)
133 flags |= QQmlPropertyData::IsQList;
139 static int metaObjectSignalCount(const QMetaObject *metaObject)
142 for (const QMetaObject *obj = metaObject; obj; obj = obj->superClass())
143 signalCount += QMetaObjectPrivate::get(obj)->signalCount;
147 QQmlPropertyData::Flags
148 QQmlPropertyData::flagsForProperty(const QMetaProperty &p, QQmlEngine *engine)
150 return fastFlagsForProperty(p) | flagsForPropertyType(p.userType(), engine);
153 void QQmlPropertyData::lazyLoad(const QMetaProperty &p, QQmlEngine *engine)
157 coreIndex = p.propertyIndex();
158 notifyIndex = QMetaObjectPrivate::signalIndex(p.notifySignal());
159 Q_ASSERT(p.revision() <= Q_INT16_MAX);
160 revision = p.revision();
162 flags = fastFlagsForProperty(p);
165 if (type == QMetaType::QObjectStar) {
167 flags |= QQmlPropertyData::IsQObjectDerived;
168 } else if (type == QMetaType::QVariant) {
170 flags |= QQmlPropertyData::IsQVariant;
171 } else if (type == QVariant::UserType || type == -1) {
172 propTypeName = p.typeName();
173 flags |= QQmlPropertyData::NotFullyResolved;
179 void QQmlPropertyData::load(const QMetaProperty &p, QQmlEngine *engine)
181 propType = p.userType();
182 coreIndex = p.propertyIndex();
183 notifyIndex = QMetaObjectPrivate::signalIndex(p.notifySignal());
184 flags = fastFlagsForProperty(p) | flagsForPropertyType(propType, engine);
185 Q_ASSERT(p.revision() <= Q_INT16_MAX);
186 revision = p.revision();
189 void QQmlPropertyData::load(const QMetaMethod &m)
191 coreIndex = m.methodIndex();
194 if (m.methodType() == QMetaMethod::Signal)
196 propType = m.returnType();
198 if (m.parameterCount()) {
199 flags |= HasArguments;
200 if ((m.parameterCount() == 1) && (m.parameterTypes().first() == "QQmlV8Function*")) {
201 flags |= IsV8Function;
205 if (m.attributes() & QMetaMethod::Cloned)
208 Q_ASSERT(m.revision() <= Q_INT16_MAX);
209 revision = m.revision();
212 void QQmlPropertyData::lazyLoad(const QMetaMethod &m)
214 coreIndex = m.methodIndex();
217 if (m.methodType() == QMetaMethod::Signal)
219 propType = QMetaType::Void;
221 const char *returnType = m.typeName();
224 if ((*returnType != 'v') || (qstrcmp(returnType+1, "oid") != 0)) {
225 propTypeName = returnType;
226 flags |= NotFullyResolved;
229 if (m.parameterCount()) {
230 flags |= HasArguments;
231 if ((m.parameterCount() == 1) && (m.parameterTypes().first() == "QQmlV8Function*")) {
232 flags |= IsV8Function;
236 if (m.attributes() & QMetaMethod::Cloned)
239 Q_ASSERT(m.revision() <= Q_INT16_MAX);
240 revision = m.revision();
244 Creates a new empty QQmlPropertyCache.
246 QQmlPropertyCache::QQmlPropertyCache(QQmlEngine *e)
247 : engine(e), _parent(0), propertyIndexCacheStart(0), methodIndexCacheStart(0),
248 signalHandlerIndexCacheStart(0), _hasPropertyOverrides(false), _ownMetaObject(false),
249 _metaObject(0), argumentsCache(0)
255 Creates a new QQmlPropertyCache of \a metaObject.
257 QQmlPropertyCache::QQmlPropertyCache(QQmlEngine *e, const QMetaObject *metaObject)
258 : engine(e), _parent(0), propertyIndexCacheStart(0), methodIndexCacheStart(0),
259 signalHandlerIndexCacheStart(0), _hasPropertyOverrides(false), _ownMetaObject(false),
260 _metaObject(0), argumentsCache(0)
263 Q_ASSERT(metaObject);
265 update(engine, metaObject);
268 QQmlPropertyCache::~QQmlPropertyCache()
272 QQmlPropertyCacheMethodArguments *args = argumentsCache;
274 QQmlPropertyCacheMethodArguments *next = args->next;
275 if (args->signalParameterStringForJS) delete args->signalParameterStringForJS;
276 if (args->names) delete args->names;
281 // We must clear this prior to releasing the parent incase it is a
284 if (_parent) _parent->release();
286 if (_ownMetaObject) free((void *)_metaObject);
292 void QQmlPropertyCache::destroy()
294 Q_ASSERT(engine || constructor.IsEmpty());
295 if (constructor.IsEmpty())
298 QQmlEnginePrivate::deleteInEngineThread(engine, this);
301 // This is inherited from QQmlCleanup, so it should only clear the things
302 // that are tied to the specific QQmlEngine.
303 void QQmlPropertyCache::clear()
305 qPersistentDispose(constructor);
309 QQmlPropertyCache *QQmlPropertyCache::copy(int reserve)
311 QQmlPropertyCache *cache = new QQmlPropertyCache(engine);
312 cache->_parent = this;
313 cache->_parent->addref();
314 cache->propertyIndexCacheStart = propertyIndexCache.count() + propertyIndexCacheStart;
315 cache->methodIndexCacheStart = methodIndexCache.count() + methodIndexCacheStart;
316 cache->signalHandlerIndexCacheStart = signalHandlerIndexCache.count() + signalHandlerIndexCacheStart;
317 cache->stringCache.linkAndReserve(stringCache, reserve);
318 cache->allowedRevisionCache = allowedRevisionCache;
319 cache->_metaObject = _metaObject;
320 cache->_defaultPropertyName = _defaultPropertyName;
322 // We specifically do *NOT* copy the constructor
327 QQmlPropertyCache *QQmlPropertyCache::copy()
332 QQmlPropertyCache *QQmlPropertyCache::copyAndReserve(QQmlEngine *, int propertyCount, int methodCount,
335 QQmlPropertyCache *rv = copy(propertyCount + methodCount + signalCount);
336 rv->propertyIndexCache.reserve(propertyCount);
337 rv->methodIndexCache.reserve(methodCount);
338 rv->signalHandlerIndexCache.reserve(signalCount);
346 \a notifyIndex MUST be in the signal index range (see QObjectPrivate::signalIndex()).
347 This is different from QMetaMethod::methodIndex().
349 void QQmlPropertyCache::appendProperty(const QString &name,
350 quint32 flags, int coreIndex, int propType, int notifyIndex)
352 QQmlPropertyData data;
353 data.propType = propType;
354 data.coreIndex = coreIndex;
355 data.notifyIndex = notifyIndex;
358 QQmlPropertyData *old = findNamedProperty(name);
360 data.markAsOverrideOf(old);
362 int index = propertyIndexCache.count();
363 propertyIndexCache.append(data);
365 setNamedProperty(name, index + propertyOffset(), propertyIndexCache.data() + index, (old != 0));
368 void QQmlPropertyCache::appendProperty(const QHashedCStringRef &name,
369 quint32 flags, int coreIndex, int propType, int notifyIndex)
371 QQmlPropertyData data;
372 data.propType = propType;
373 data.coreIndex = coreIndex;
374 data.notifyIndex = notifyIndex;
377 QQmlPropertyData *old = findNamedProperty(name);
379 data.markAsOverrideOf(old);
381 int index = propertyIndexCache.count();
382 propertyIndexCache.append(data);
384 setNamedProperty(name, index + propertyOffset(), propertyIndexCache.data() + index, (old != 0));
387 void QQmlPropertyCache::appendSignal(const QString &name, quint32 flags, int coreIndex,
388 const int *types, const QList<QByteArray> &names)
390 QQmlPropertyData data;
391 data.propType = QVariant::Invalid;
392 data.coreIndex = coreIndex;
396 QQmlPropertyData handler = data;
397 handler.flags |= QQmlPropertyData::IsSignalHandler;
400 int argumentCount = *types;
401 QQmlPropertyCacheMethodArguments *args = createArgumentsObject(argumentCount, names);
402 ::memcpy(args->arguments, types, (argumentCount + 1) * sizeof(int));
403 args->argumentsValid = true;
404 data.arguments = args;
407 QQmlPropertyData *old = findNamedProperty(name);
409 data.markAsOverrideOf(old);
411 int methodIndex = methodIndexCache.count();
412 methodIndexCache.append(data);
414 int signalHandlerIndex = signalHandlerIndexCache.count();
415 signalHandlerIndexCache.append(handler);
417 QString handlerName = QLatin1String("on") + name;
418 handlerName[2] = handlerName[2].toUpper();
420 setNamedProperty(name, methodIndex + methodOffset(), methodIndexCache.data() + methodIndex, (old != 0));
421 setNamedProperty(handlerName, signalHandlerIndex + signalOffset(), signalHandlerIndexCache.data() + signalHandlerIndex, (old != 0));
424 void QQmlPropertyCache::appendSignal(const QHashedCStringRef &name, quint32 flags, int coreIndex,
425 const int *types, const QList<QByteArray> &names)
427 QQmlPropertyData data;
428 data.propType = QVariant::Invalid;
429 data.coreIndex = coreIndex;
433 QQmlPropertyData handler = data;
434 handler.flags |= QQmlPropertyData::IsSignalHandler;
437 int argumentCount = *types;
438 QQmlPropertyCacheMethodArguments *args = createArgumentsObject(argumentCount, names);
439 ::memcpy(args->arguments, types, (argumentCount + 1) * sizeof(int));
440 args->argumentsValid = true;
441 data.arguments = args;
444 QQmlPropertyData *old = findNamedProperty(name);
446 data.markAsOverrideOf(old);
448 int methodIndex = methodIndexCache.count();
449 methodIndexCache.append(data);
451 int signalHandlerIndex = signalHandlerIndexCache.count();
452 signalHandlerIndexCache.append(handler);
454 QString handlerName = QLatin1String("on") + name.toUtf16();
455 handlerName[2] = handlerName[2].toUpper();
457 setNamedProperty(name, methodIndex + methodOffset(), methodIndexCache.data() + methodIndex, (old != 0));
458 setNamedProperty(handlerName, signalHandlerIndex + signalOffset(), signalHandlerIndexCache.data() + signalHandlerIndex, (old != 0));
461 void QQmlPropertyCache::appendMethod(const QString &name, quint32 flags, int coreIndex,
462 const QList<QByteArray> &names)
464 int argumentCount = names.count();
466 QQmlPropertyData data;
467 data.propType = QMetaType::QVariant;
468 data.coreIndex = coreIndex;
470 QQmlPropertyCacheMethodArguments *args = createArgumentsObject(argumentCount, names);
471 for (int ii = 0; ii < argumentCount; ++ii)
472 args->arguments[ii + 1] = QMetaType::QVariant;
473 args->argumentsValid = true;
474 data.arguments = args;
478 QQmlPropertyData *old = findNamedProperty(name);
480 data.markAsOverrideOf(old);
482 int methodIndex = methodIndexCache.count();
483 methodIndexCache.append(data);
485 setNamedProperty(name, methodIndex + methodOffset(), methodIndexCache.data() + methodIndex, (old != 0));
488 void QQmlPropertyCache::appendMethod(const QHashedCStringRef &name, quint32 flags, int coreIndex,
489 const QList<QByteArray> &names)
491 int argumentCount = names.count();
493 QQmlPropertyData data;
494 data.propType = QMetaType::QVariant;
495 data.coreIndex = coreIndex;
497 QQmlPropertyCacheMethodArguments *args = createArgumentsObject(argumentCount, names);
498 for (int ii = 0; ii < argumentCount; ++ii)
499 args->arguments[ii + 1] = QMetaType::QVariant;
500 args->argumentsValid = true;
501 data.arguments = args;
505 QQmlPropertyData *old = findNamedProperty(name);
507 data.markAsOverrideOf(old);
509 int methodIndex = methodIndexCache.count();
510 methodIndexCache.append(data);
512 setNamedProperty(name, methodIndex + methodOffset(), methodIndexCache.data() + methodIndex, (old != 0));
515 // Returns this property cache's metaObject. May be null if it hasn't been created yet.
516 const QMetaObject *QQmlPropertyCache::metaObject() const
521 // Returns this property cache's metaObject, creating it if necessary.
522 const QMetaObject *QQmlPropertyCache::createMetaObject()
525 _ownMetaObject = true;
527 QMetaObjectBuilder builder;
528 toMetaObjectBuilder(builder);
529 builder.setSuperClass(_parent->createMetaObject());
530 _metaObject = builder.toMetaObject();
536 // Returns the name of the default property for this cache
537 QString QQmlPropertyCache::defaultPropertyName() const
539 return _defaultPropertyName;
542 QQmlPropertyData *QQmlPropertyCache::defaultProperty() const
544 return property(defaultPropertyName(), 0, 0);
547 QQmlPropertyCache *QQmlPropertyCache::parent() const
552 void QQmlPropertyCache::setParent(QQmlPropertyCache *newParent)
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 (StringCache::mapped_type *it = stringCache.value(methodName))
705 setNamedProperty(methodName, ii, data, (old != 0));
707 if (data->isSignal()) {
708 QHashedString on(QStringLiteral("on") % methodName.at(0).toUpper() % methodName.midRef(1));
709 setNamedProperty(on, ii, sigdata, (old != 0));
710 ++signalHandlerIndex;
713 QHashedCStringRef methodName(rawName, cptr - rawName);
714 if (StringCache::mapped_type *it = stringCache.value(methodName))
716 setNamedProperty(methodName, ii, data, (old != 0));
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 setNamedProperty(on, ii, data, (old != 0));
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;
740 data->markAsOverrideOf(old);
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 (StringCache::mapped_type *it = stringCache.value(propName))
780 setNamedProperty(propName, ii, data, (old != 0));
782 QHashedCStringRef propName(str, cptr - str);
783 if (StringCache::mapped_type *it = stringCache.value(propName))
785 setNamedProperty(propName, ii, data, (old != 0));
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->markAsOverrideOf(old);
803 QQmlPropertyData *QQmlPropertyCache::ensureResolved(QQmlPropertyData *p) const
805 if (p && p->notFullyResolved())
811 void QQmlPropertyCache::resolve(QQmlPropertyData *data) const
813 Q_ASSERT(data->notFullyResolved());
815 data->propType = QMetaType::type(data->propTypeName);
817 if (!data->isFunction())
818 data->flags |= flagsForPropertyType(data->propType, engine);
820 data->flags &= ~QQmlPropertyData::NotFullyResolved;
823 void QQmlPropertyCache::updateRecur(QQmlEngine *engine, const QMetaObject *metaObject)
828 updateRecur(engine, metaObject->superClass());
830 append(engine, metaObject, -1);
833 void QQmlPropertyCache::update(QQmlEngine *engine, const QMetaObject *metaObject)
836 Q_ASSERT(metaObject);
837 Q_ASSERT(stringCache.isEmpty());
839 // Preallocate enough space in the index caches for all the properties/methods/signals that
840 // are not cached in a parent cache so that the caches never need to be reallocated as this
841 // would invalidate pointers stored in the stringCache.
842 int pc = metaObject->propertyCount();
843 int mc = metaObject->methodCount();
844 int sc = metaObjectSignalCount(metaObject);
845 propertyIndexCache.reserve(pc - propertyIndexCacheStart);
846 methodIndexCache.reserve(mc - methodIndexCacheStart);
847 signalHandlerIndexCache.reserve(sc - signalHandlerIndexCacheStart);
849 // Reserve enough space in the stringCache for all properties/methods/signals including those
850 // cached in a parent cache.
851 stringCache.reserve(pc + mc + sc);
853 updateRecur(engine,metaObject);
857 invalidates and updates the PropertyCache if the QMetaObject has changed.
858 This function is used in the tooling to update dynamic properties.
860 void QQmlPropertyCache::invalidate(QQmlEngine *engine, const QMetaObject *metaObject)
863 propertyIndexCache.clear();
864 methodIndexCache.clear();
865 signalHandlerIndexCache.clear();
867 _hasPropertyOverrides = false;
870 int pc = metaObject->propertyCount();
871 int mc = metaObject->methodCount();
872 int sc = metaObjectSignalCount(metaObject);
873 int reserve = pc + mc + sc;
876 propertyIndexCacheStart = parent()->propertyIndexCache.count() + parent()->propertyIndexCacheStart;
877 methodIndexCacheStart = parent()->methodIndexCache.count() + parent()->methodIndexCacheStart;
878 signalHandlerIndexCacheStart = parent()->signalHandlerIndexCache.count() + parent()->signalHandlerIndexCacheStart;
879 stringCache.linkAndReserve(parent()->stringCache, reserve);
880 append(engine, metaObject, -1);
882 propertyIndexCacheStart = 0;
883 methodIndexCacheStart = 0;
884 signalHandlerIndexCacheStart = 0;
885 update(engine, metaObject);
890 \a index MUST be in the signal index range (see QObjectPrivate::signalIndex()).
891 This is different from QMetaMethod::methodIndex().
894 QQmlPropertyCache::signal(int index, QQmlPropertyCache **c) const
896 if (index < 0 || index >= (signalHandlerIndexCacheStart + signalHandlerIndexCache.count()))
899 if (index < signalHandlerIndexCacheStart)
900 return _parent->signal(index, c);
902 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&methodIndexCache.at(index - signalHandlerIndexCacheStart));
903 if (rv->notFullyResolved()) resolve(rv);
904 Q_ASSERT(rv->isSignal() || rv->coreIndex == -1);
905 if (c) *c = const_cast<QQmlPropertyCache *>(this);
909 int QQmlPropertyCache::methodIndexToSignalIndex(int index) const
911 if (index < 0 || index >= (methodIndexCacheStart + methodIndexCache.count()))
914 if (index < methodIndexCacheStart)
915 return _parent->methodIndexToSignalIndex(index);
917 return index - methodIndexCacheStart + signalHandlerIndexCacheStart;
921 QQmlPropertyCache::property(int index) const
923 if (index < 0 || index >= (propertyIndexCacheStart + propertyIndexCache.count()))
926 if (index < propertyIndexCacheStart)
927 return _parent->property(index);
929 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&propertyIndexCache.at(index - propertyIndexCacheStart));
930 return ensureResolved(rv);
934 QQmlPropertyCache::method(int index) const
936 if (index < 0 || index >= (methodIndexCacheStart + methodIndexCache.count()))
939 if (index < methodIndexCacheStart)
940 return _parent->method(index);
942 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&methodIndexCache.at(index - methodIndexCacheStart));
943 return ensureResolved(rv);
946 QQmlPropertyData *QQmlPropertyCache::findProperty(StringCache::ConstIterator it, QObject *object, QQmlContextData *context) const
948 QQmlData *data = (object ? QQmlData::get(object) : 0);
949 const QQmlVMEMetaObject *vmemo = (data && data->hasVMEMetaObject ? static_cast<const QQmlVMEMetaObject *>(object->metaObject()) : 0);
950 return findProperty(it, vmemo, context);
955 inline bool contextHasNoExtensions(QQmlContextData *context)
957 // This context has no extension if its parent is the engine's rootContext,
958 // which has children but no imports
959 return (!context->parent || !context->parent->imports);
962 inline int maximumIndexForProperty(QQmlPropertyData *prop, const QQmlVMEMetaObject *vmemo)
964 return (prop->isFunction() ? vmemo->methodCount()
965 : prop->isSignalHandler() ? vmemo->signalCount()
966 : vmemo->propertyCount());
971 QQmlPropertyData *QQmlPropertyCache::findProperty(StringCache::ConstIterator it, const QQmlVMEMetaObject *vmemo, QQmlContextData *context) const
973 StringCache::ConstIterator end = stringCache.end();
976 QQmlPropertyData *result = it.value().second;
978 // If there exists a typed property (not a function or signal handler), of the
979 // right name available to the specified context, we need to return that
980 // property rather than any subsequent override
982 if (vmemo && context && !contextHasNoExtensions(context)) {
983 // Find the meta-object that corresponds to the supplied context
985 if (vmemo->ctxt == context)
988 vmemo = vmemo->parentVMEMetaObject();
993 // Ensure that the property we resolve to is accessible from this meta-object
995 const StringCache::mapped_type &property(it.value());
997 if (property.first < maximumIndexForProperty(property.second, vmemo)) {
998 // This property is available in the specified context
999 if (property.second->isFunction() || property.second->isSignalHandler()) {
1000 // Prefer the earlier resolution
1002 // Prefer the typed property to any previous property found
1003 result = property.second;
1008 // See if there is a better candidate
1009 it = stringCache.findNext(it);
1010 } while (it != end);
1013 return ensureResolved(result);
1019 QString QQmlPropertyData::name(QObject *object)
1024 return name(object->metaObject());
1027 QString QQmlPropertyData::name(const QMetaObject *metaObject)
1029 if (!metaObject || coreIndex == -1)
1032 if (flags & IsFunction) {
1033 QMetaMethod m = metaObject->method(coreIndex);
1035 return QString::fromUtf8(m.name().constData());
1037 QMetaProperty p = metaObject->property(coreIndex);
1038 return QString::fromUtf8(p.name());
1042 void QQmlPropertyData::markAsOverrideOf(QQmlPropertyData *predecessor)
1044 overrideIndexIsProperty = !predecessor->isFunction();
1045 overrideIndex = predecessor->coreIndex;
1047 predecessor->flags |= QQmlPropertyData::IsOverridden;
1050 QStringList QQmlPropertyCache::propertyNames() const
1053 for (StringCache::ConstIterator iter = stringCache.begin(); iter != stringCache.end(); ++iter)
1054 keys.append(iter.key());
1058 struct StaticQtMetaObject : public QObject
1060 static const QMetaObject *get()
1061 { return &static_cast<StaticQtMetaObject*> (0)->staticQtMetaObject; }
1064 static int EnumType(const QMetaObject *metaobj, const QByteArray &str, int type)
1068 int scopeIdx = str.lastIndexOf("::");
1069 if (scopeIdx != -1) {
1070 scope = str.left(scopeIdx);
1071 name = str.mid(scopeIdx + 2);
1075 const QMetaObject *meta;
1077 meta = StaticQtMetaObject::get();
1080 for (int i = meta->enumeratorCount() - 1; i >= 0; --i) {
1081 QMetaEnum m = meta->enumerator(i);
1082 if ((m.name() == name) && (scope.isEmpty() || (m.scope() == scope)))
1083 return QVariant::Int;
1088 QQmlPropertyCacheMethodArguments *QQmlPropertyCache::createArgumentsObject(int argc, const QList<QByteArray> &names)
1090 typedef QQmlPropertyCacheMethodArguments A;
1091 A *args = static_cast<A *>(malloc(sizeof(A) + (argc + 1) * sizeof(int)));
1092 args->arguments[0] = argc;
1093 args->argumentsValid = false;
1094 args->signalParameterStringForJS = 0;
1095 args->signalParameterCountForJS = 0;
1096 args->parameterError = false;
1097 args->names = argc ? new QList<QByteArray>(names) : 0;
1098 args->next = argumentsCache;
1099 argumentsCache = args;
1104 \a index MUST be in the signal index range (see QObjectPrivate::signalIndex()).
1105 This is different from QMetaMethod::methodIndex().
1107 QString QQmlPropertyCache::signalParameterStringForJS(int index, int *count, QString *errorString)
1109 QQmlPropertyCache *c = 0;
1110 QQmlPropertyData *signalData = signal(index, &c);
1114 typedef QQmlPropertyCacheMethodArguments A;
1116 if (signalData->arguments) {
1117 A *arguments = static_cast<A *>(signalData->arguments);
1118 if (arguments->signalParameterStringForJS) {
1120 *count = arguments->signalParameterCountForJS;
1121 if (arguments->parameterError) {
1123 *errorString = *arguments->signalParameterStringForJS;
1126 return *arguments->signalParameterStringForJS;
1130 QList<QByteArray> parameterNameList = signalParameterNames(index);
1132 if (!signalData->arguments) {
1133 A *args = c->createArgumentsObject(parameterNameList.count(), parameterNameList);
1134 signalData->arguments = args;
1137 QQmlRewrite::RewriteSignalHandler rewriter;
1138 QQmlEnginePrivate *ep = QQmlEnginePrivate::get(engine);
1139 const QString ¶meters = rewriter.createParameterString(parameterNameList,
1140 ep->v8engine()->illegalNames());
1142 bool error = rewriter.hasParameterError();
1143 A *arguments = static_cast<A *>(signalData->arguments);
1144 arguments->signalParameterStringForJS = new QString(error ? rewriter.parameterError() : parameters);
1145 arguments->signalParameterCountForJS = rewriter.parameterCountForJS();
1147 *count = arguments->signalParameterCountForJS;
1149 arguments->parameterError = true;
1151 *errorString = *arguments->signalParameterStringForJS;
1154 return *arguments->signalParameterStringForJS;
1157 // Returns an array of the arguments for method \a index. The first entry in the array
1158 // is the number of arguments.
1159 int *QQmlPropertyCache::methodParameterTypes(QObject *object, int index,
1160 QVarLengthArray<int, 9> &dummy,
1161 QByteArray *unknownTypeError)
1163 Q_ASSERT(object && index >= 0);
1165 QQmlData *ddata = QQmlData::get(object, false);
1167 if (ddata && ddata->propertyCache) {
1168 typedef QQmlPropertyCacheMethodArguments A;
1170 QQmlPropertyCache *c = ddata->propertyCache;
1171 Q_ASSERT(index < c->methodIndexCacheStart + c->methodIndexCache.count());
1173 while (index < c->methodIndexCacheStart)
1176 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&c->methodIndexCache.at(index - c->methodIndexCacheStart));
1178 if (rv->arguments && static_cast<A *>(rv->arguments)->argumentsValid)
1179 return static_cast<A *>(rv->arguments)->arguments;
1181 const QMetaObject *metaObject = c->createMetaObject();
1182 Q_ASSERT(metaObject);
1183 QMetaMethod m = metaObject->method(index);
1185 int argc = m.parameterCount();
1186 if (!rv->arguments) {
1187 A *args = c->createArgumentsObject(argc);
1188 rv->arguments = args;
1190 A *args = static_cast<A *>(rv->arguments);
1192 QList<QByteArray> argTypeNames; // Only loaded if needed
1194 for (int ii = 0; ii < argc; ++ii) {
1195 int type = m.parameterType(ii);
1196 QMetaType::TypeFlags flags = QMetaType::typeFlags(type);
1197 if (flags & QMetaType::IsEnumeration)
1198 type = QVariant::Int;
1199 else if (type == QMetaType::UnknownType ||
1200 (type >= (int)QVariant::UserType && !(flags & QMetaType::PointerToQObject) &&
1201 type != qMetaTypeId<QJSValue>())) {
1202 //the UserType clause is to catch registered QFlags
1203 if (argTypeNames.isEmpty())
1204 argTypeNames = m.parameterTypes();
1205 type = EnumType(object->metaObject(), argTypeNames.at(ii), type);
1207 if (type == QMetaType::UnknownType) {
1208 if (unknownTypeError) *unknownTypeError = argTypeNames.at(ii);
1211 args->arguments[ii + 1] = type;
1213 args->argumentsValid = true;
1214 return static_cast<A *>(rv->arguments)->arguments;
1217 QMetaMethod m = object->metaObject()->method(index);
1218 int argc = m.parameterCount();
1219 dummy.resize(argc + 1);
1221 QList<QByteArray> argTypeNames; // Only loaded if needed
1223 for (int ii = 0; ii < argc; ++ii) {
1224 int type = m.parameterType(ii);
1225 QMetaType::TypeFlags flags = QMetaType::typeFlags(type);
1226 if (flags & QMetaType::IsEnumeration)
1227 type = QVariant::Int;
1228 else if (type == QMetaType::UnknownType ||
1229 (type >= (int)QVariant::UserType && !(flags & QMetaType::PointerToQObject) &&
1230 type != qMetaTypeId<QJSValue>())) {
1231 //the UserType clause is to catch registered QFlags)
1232 if (argTypeNames.isEmpty())
1233 argTypeNames = m.parameterTypes();
1234 type = EnumType(object->metaObject(), argTypeNames.at(ii), type);
1236 if (type == QMetaType::UnknownType) {
1237 if (unknownTypeError) *unknownTypeError = argTypeNames.at(ii);
1240 dummy[ii + 1] = type;
1243 return dummy.data();
1247 // Returns the return type of the method.
1248 int QQmlPropertyCache::methodReturnType(QObject *object, const QQmlPropertyData &data,
1249 QByteArray *unknownTypeError)
1251 Q_ASSERT(object && data.coreIndex >= 0);
1253 int type = data.propType;
1255 const char *propTypeName = 0;
1257 if (type == QMetaType::UnknownType) {
1258 // Find the return type name from the method info
1261 QQmlData *ddata = QQmlData::get(object, false);
1262 if (ddata && ddata->propertyCache) {
1263 QQmlPropertyCache *c = ddata->propertyCache;
1264 Q_ASSERT(data.coreIndex < c->methodIndexCacheStart + c->methodIndexCache.count());
1266 while (data.coreIndex < c->methodIndexCacheStart)
1269 const QMetaObject *metaObject = c->createMetaObject();
1270 Q_ASSERT(metaObject);
1271 m = metaObject->method(data.coreIndex);
1273 m = object->metaObject()->method(data.coreIndex);
1276 type = m.returnType();
1277 propTypeName = m.typeName();
1280 QMetaType::TypeFlags flags = QMetaType::typeFlags(type);
1281 if (flags & QMetaType::IsEnumeration) {
1282 type = QVariant::Int;
1283 } else if (type == QMetaType::UnknownType ||
1284 (type >= (int)QVariant::UserType && !(flags & QMetaType::PointerToQObject) &&
1285 type != qMetaTypeId<QJSValue>())) {
1286 //the UserType clause is to catch registered QFlags
1287 type = EnumType(object->metaObject(), propTypeName, type);
1290 if (type == QMetaType::UnknownType) {
1291 if (unknownTypeError) *unknownTypeError = propTypeName;
1297 int QQmlPropertyCache::originalClone(int index)
1299 while (signal(index)->isCloned())
1304 int QQmlPropertyCache::originalClone(QObject *object, int index)
1306 QQmlData *data = QQmlData::get(object, false);
1307 if (data && data->propertyCache) {
1308 QQmlPropertyCache *cache = data->propertyCache;
1309 while (cache->signal(index)->isCloned())
1312 while (QMetaObjectPrivate::signal(object->metaObject(), index).attributes() & QMetaMethod::Cloned)
1318 QQmlPropertyData qQmlPropertyCacheCreate(const QMetaObject *metaObject, const QString &property)
1320 Q_ASSERT(metaObject);
1322 QQmlPropertyData rv;
1324 const QMetaObject *cmo = metaObject;
1325 const QByteArray propertyName = property.toUtf8();
1327 int idx = cmo->indexOfProperty(propertyName);
1329 QMetaProperty p = cmo->property(idx);
1330 if (p.isScriptable()) {
1334 while (cmo && cmo->propertyOffset() >= idx)
1335 cmo = cmo->superClass();
1343 //Used to block access to QObject::destroyed() and QObject::deleteLater() from QML
1344 static const int destroyedIdx1 = QObject::staticMetaObject.indexOfSignal("destroyed(QObject*)");
1345 static const int destroyedIdx2 = QObject::staticMetaObject.indexOfSignal("destroyed()");
1346 static const int deleteLaterIdx = QObject::staticMetaObject.indexOfSlot("deleteLater()");
1348 int methodCount = metaObject->methodCount();
1349 for (int ii = methodCount - 1; ii >= 0; --ii) {
1350 if (ii == destroyedIdx1 || ii == destroyedIdx2 || ii == deleteLaterIdx)
1352 QMetaMethod m = metaObject->method(ii);
1353 if (m.access() == QMetaMethod::Private)
1355 QString methodName = QString::fromUtf8(m.name().constData());
1357 if (methodName == property) {
1366 inline const QString &qQmlPropertyCacheToString(const QString &string)
1371 inline QString qQmlPropertyCacheToString(const QHashedV8String &string)
1373 return QV8Engine::toStringStatic(string.string());
1376 template<typename T>
1378 qQmlPropertyCacheProperty(QQmlEngine *engine, QObject *obj, const T &name,
1379 QQmlContextData *context, QQmlPropertyData &local)
1381 QQmlPropertyCache *cache = 0;
1383 QQmlData *ddata = QQmlData::get(obj, false);
1385 if (ddata && ddata->propertyCache) {
1386 cache = ddata->propertyCache;
1387 } else if (engine) {
1388 QQmlEnginePrivate *ep = QQmlEnginePrivate::get(engine);
1389 cache = ep->cache(obj);
1391 ddata = QQmlData::get(obj, true);
1393 ddata->propertyCache = cache;
1397 QQmlPropertyData *rv = 0;
1400 rv = cache->property(name, obj, context);
1402 local = qQmlPropertyCacheCreate(obj->metaObject(), qQmlPropertyCacheToString(name));
1403 if (local.isValid())
1411 QQmlPropertyCache::property(QQmlEngine *engine, QObject *obj, const QHashedV8String &name,
1412 QQmlContextData *context, QQmlPropertyData &local)
1414 return qQmlPropertyCacheProperty<QHashedV8String>(engine, obj, name, context, local);
1418 QQmlPropertyCache::property(QQmlEngine *engine, QObject *obj,
1419 const QString &name, QQmlContextData *context, QQmlPropertyData &local)
1421 return qQmlPropertyCacheProperty<QString>(engine, obj, name, context, local);
1424 static inline const QMetaObjectPrivate *priv(const uint* data)
1425 { return reinterpret_cast<const QMetaObjectPrivate*>(data); }
1427 bool QQmlPropertyCache::isDynamicMetaObject(const QMetaObject *mo)
1429 return priv(mo->d.data)->revision >= 3 && priv(mo->d.data)->flags & DynamicMetaObject;
1432 const char *QQmlPropertyCache::className() const
1434 if (!_ownMetaObject && _metaObject)
1435 return _metaObject->className();
1437 return _dynamicClassName.constData();
1440 void QQmlPropertyCache::toMetaObjectBuilder(QMetaObjectBuilder &builder)
1442 struct Sort { static bool lt(const QPair<QString, QQmlPropertyData *> &lhs,
1443 const QPair<QString, QQmlPropertyData *> &rhs) {
1444 return lhs.second->coreIndex < rhs.second->coreIndex;
1447 struct Insert { static void in(QQmlPropertyCache *This,
1448 QList<QPair<QString, QQmlPropertyData *> > &properties,
1449 QList<QPair<QString, QQmlPropertyData *> > &methods,
1450 StringCache::ConstIterator iter, QQmlPropertyData *data) {
1451 if (data->isSignalHandler())
1454 if (data->isFunction()) {
1455 if (data->coreIndex < This->methodIndexCacheStart)
1458 QPair<QString, QQmlPropertyData *> entry = qMakePair((QString)iter.key(), data);
1459 // Overrides can cause the entry to already exist
1460 if (!methods.contains(entry)) methods.append(entry);
1462 data = This->overrideData(data);
1463 if (data && !data->isFunction()) Insert::in(This, properties, methods, iter, data);
1465 if (data->coreIndex < This->propertyIndexCacheStart)
1468 QPair<QString, QQmlPropertyData *> entry = qMakePair((QString)iter.key(), data);
1469 // Overrides can cause the entry to already exist
1470 if (!properties.contains(entry)) properties.append(entry);
1472 data = This->overrideData(data);
1473 if (data) Insert::in(This, properties, methods, iter, data);
1478 builder.setClassName(_dynamicClassName);
1480 QList<QPair<QString, QQmlPropertyData *> > properties;
1481 QList<QPair<QString, QQmlPropertyData *> > methods;
1483 for (StringCache::ConstIterator iter = stringCache.begin(); iter != stringCache.end(); ++iter)
1484 Insert::in(this, properties, methods, iter, iter.value().second);
1486 Q_ASSERT(properties.count() == propertyIndexCache.count());
1487 Q_ASSERT(methods.count() == methodIndexCache.count());
1489 qSort(properties.begin(), properties.end(), Sort::lt);
1490 qSort(methods.begin(), methods.end(), Sort::lt);
1492 for (int ii = 0; ii < properties.count(); ++ii) {
1493 QQmlPropertyData *data = properties.at(ii).second;
1495 int notifierId = -1;
1496 if (data->notifyIndex != -1)
1497 notifierId = data->notifyIndex - signalHandlerIndexCacheStart;
1499 QMetaPropertyBuilder property = builder.addProperty(properties.at(ii).first.toUtf8(),
1500 QMetaType::typeName(data->propType),
1503 property.setReadable(true);
1504 property.setWritable(data->isWritable());
1505 property.setResettable(data->isResettable());
1508 for (int ii = 0; ii < methods.count(); ++ii) {
1509 QQmlPropertyData *data = methods.at(ii).second;
1511 QByteArray returnType;
1512 if (data->propType != 0)
1513 returnType = QMetaType::typeName(data->propType);
1515 QByteArray signature = methods.at(ii).first.toUtf8() + "(";
1517 QQmlPropertyCacheMethodArguments *arguments = 0;
1518 if (data->hasArguments()) {
1519 arguments = (QQmlPropertyCacheMethodArguments *)data->arguments;
1520 Q_ASSERT(arguments->argumentsValid);
1521 for (int ii = 0; ii < arguments->arguments[0]; ++ii) {
1522 if (ii != 0) signature.append(",");
1523 signature.append(QMetaType::typeName(arguments->arguments[1 + ii]));
1527 signature.append(")");
1529 QMetaMethodBuilder method;
1530 if (data->isSignal()) {
1531 method = builder.addSignal(signature);
1533 method = builder.addSlot(signature);
1535 method.setAccess(QMetaMethod::Protected);
1537 if (arguments && arguments->names)
1538 method.setParameterNames(*arguments->names);
1540 if (!returnType.isEmpty())
1541 method.setReturnType(returnType);
1544 if (!_defaultPropertyName.isEmpty()) {
1545 QQmlPropertyData *dp = property(_defaultPropertyName, 0, 0);
1546 if (dp && dp->coreIndex >= propertyIndexCacheStart) {
1547 Q_ASSERT(!dp->isFunction());
1548 builder.addClassInfo("DefaultProperty", _defaultPropertyName.toUtf8());
1554 \a index MUST be in the signal index range (see QObjectPrivate::signalIndex()).
1555 This is different from QMetaMethod::methodIndex().
1557 QList<QByteArray> QQmlPropertyCache::signalParameterNames(int index) const
1559 QQmlPropertyData *signalData = signal(index);
1560 if (signalData && signalData->hasArguments()) {
1561 QQmlPropertyCacheMethodArguments *args = (QQmlPropertyCacheMethodArguments *)signalData->arguments;
1562 if (args && args->names)
1563 return *args->names;
1564 const QMetaMethod &method = QMetaObjectPrivate::signal(firstCppMetaObject(), index);
1565 return method.parameterNames();
1567 return QList<QByteArray>();
1570 // Returns true if \a from is assignable to a property of type \a to
1571 bool QQmlMetaObject::canConvert(const QQmlMetaObject &from, const QQmlMetaObject &to)
1573 Q_ASSERT(!from.isNull() && !to.isNull());
1575 struct I { static bool equal(const QMetaObject *lhs, const QMetaObject *rhs) {
1576 return lhs == rhs || (lhs && rhs && lhs->d.stringdata == rhs->d.stringdata);
1579 const QMetaObject *tom = to._m.isT1()?to._m.asT1()->metaObject():to._m.asT2();
1580 if (tom == &QObject::staticMetaObject) return true;
1582 if (from._m.isT1() && to._m.isT1()) { // QQmlPropertyCache -> QQmlPropertyCache
1583 QQmlPropertyCache *fromp = from._m.asT1();
1584 QQmlPropertyCache *top = to._m.asT1();
1587 if (fromp == top) return true;
1588 fromp = fromp->parent();
1590 } else if (from._m.isT1() && to._m.isT2()) { // QQmlPropertyCache -> QMetaObject
1591 QQmlPropertyCache *fromp = from._m.asT1();
1594 const QMetaObject *fromm = fromp->metaObject();
1595 if (fromm && I::equal(fromm, tom)) return true;
1596 fromp = fromp->parent();
1598 } else if (from._m.isT2() && to._m.isT1()) { // QMetaObject -> QQmlPropertyCache
1599 const QMetaObject *fromm = from._m.asT2();
1601 if (!tom) return false;
1604 if (I::equal(fromm, tom)) return true;
1605 fromm = fromm->superClass();
1607 } else { // QMetaObject -> QMetaObject
1608 const QMetaObject *fromm = from._m.asT2();
1611 if (I::equal(fromm, tom)) return true;
1612 fromm = fromm->superClass();
1619 QQmlPropertyCache *QQmlMetaObject::propertyCache(QQmlEnginePrivate *e) const
1621 if (_m.isNull()) return 0;
1622 if (_m.isT1()) return _m.asT1();
1623 else return e->cache(_m.asT2());