Rename QDeclarative symbols to QQuick and QQml
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmlpropertycache.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qqmlpropertycache_p.h"
43
44 #include "qqmlengine_p.h"
45 #include "qqmlbinding_p.h"
46 #include <private/qv8engine_p.h>
47
48 #include <private/qmetaobject_p.h>
49 #include <private/qqmlaccessors_p.h>
50
51 #include <QtCore/qdebug.h>
52
53 Q_DECLARE_METATYPE(QJSValue)
54 Q_DECLARE_METATYPE(QQmlV8Handle);
55
56 QT_BEGIN_NAMESPACE
57
58 #define Q_INT16_MAX 32767
59
60 class QQmlPropertyCacheMethodArguments 
61 {
62 public:
63     QQmlPropertyCacheMethodArguments *next;
64     int arguments[0];
65 };
66
67 // Flags that do *NOT* depend on the property's QMetaProperty::userType() and thus are quick
68 // to load
69 static QQmlPropertyData::Flags fastFlagsForProperty(const QMetaProperty &p)
70 {
71     QQmlPropertyData::Flags flags;
72
73     if (p.isConstant())
74         flags |= QQmlPropertyData::IsConstant;
75     if (p.isWritable())
76         flags |= QQmlPropertyData::IsWritable;
77     if (p.isResettable())
78         flags |= QQmlPropertyData::IsResettable;
79     if (p.isFinal())
80         flags |= QQmlPropertyData::IsFinal;
81     if (p.isEnumType())
82         flags |= QQmlPropertyData::IsEnumType;
83
84     return flags;
85 }
86
87 // Flags that do depend on the property's QMetaProperty::userType() and thus are slow to 
88 // load
89 static QQmlPropertyData::Flags flagsForPropertyType(int propType, QQmlEngine *engine)
90 {
91     Q_ASSERT(propType != -1);
92
93     QQmlPropertyData::Flags flags;
94
95     if (propType == QMetaType::QObjectStar || propType == QMetaType::QWidgetStar) {
96         flags |= QQmlPropertyData::IsQObjectDerived;
97     } else if (propType == QMetaType::QVariant) {
98         flags |= QQmlPropertyData::IsQVariant;
99     } else if (propType < (int)QVariant::UserType) {
100     } else if (propType == qMetaTypeId<QQmlBinding *>()) {
101         flags |= QQmlPropertyData::IsQmlBinding;
102     } else if (propType == qMetaTypeId<QJSValue>()) {
103         flags |= QQmlPropertyData::IsQJSValue;
104     } else if (propType == qMetaTypeId<QQmlV8Handle>()) {
105         flags |= QQmlPropertyData::IsV8Handle;
106     } else {
107         QQmlMetaType::TypeCategory cat = 
108             engine ? QQmlEnginePrivate::get(engine)->typeCategory(propType)
109                    : QQmlMetaType::typeCategory(propType);
110
111         if (cat == QQmlMetaType::Object)
112             flags |= QQmlPropertyData::IsQObjectDerived;
113         else if (cat == QQmlMetaType::List)
114             flags |= QQmlPropertyData::IsQList;
115     }
116
117     return flags;
118 }
119
120 static int metaObjectSignalCount(const QMetaObject *metaObject)
121 {
122     int signalCount = 0;
123     for (const QMetaObject *obj = metaObject; obj; obj = obj->superClass())
124         signalCount += QMetaObjectPrivate::get(obj)->signalCount;
125     return signalCount;
126 }
127
128 QQmlPropertyData::Flags
129 QQmlPropertyData::flagsForProperty(const QMetaProperty &p, QQmlEngine *engine)
130 {
131     return fastFlagsForProperty(p) | flagsForPropertyType(p.userType(), engine);
132 }
133
134 void QQmlPropertyData::lazyLoad(const QMetaProperty &p, QQmlEngine *engine)
135 {
136     Q_UNUSED(engine);
137
138     coreIndex = p.propertyIndex();
139     notifyIndex = p.notifySignalIndex();
140     Q_ASSERT(p.revision() <= Q_INT16_MAX);
141     revision = p.revision();
142
143     flags = fastFlagsForProperty(p);
144
145     int type = p.type();
146     if (type == QMetaType::QObjectStar || type == QMetaType::QWidgetStar) {
147         propType = type;
148         flags |= QQmlPropertyData::IsQObjectDerived;
149     } else if (type == QMetaType::QVariant) {
150         propType = type;
151         flags |= QQmlPropertyData::IsQVariant;
152     } else if (type == QVariant::UserType || type == -1) {
153         propTypeName = p.typeName();
154         flags |= QQmlPropertyData::NotFullyResolved;
155     } else {
156         propType = type;
157     }
158 }
159
160 void QQmlPropertyData::load(const QMetaProperty &p, QQmlEngine *engine)
161 {
162     propType = p.userType();
163     coreIndex = p.propertyIndex();
164     notifyIndex = p.notifySignalIndex();
165     flags = fastFlagsForProperty(p) | flagsForPropertyType(propType, engine);
166     Q_ASSERT(p.revision() <= Q_INT16_MAX);
167     revision = p.revision();
168 }
169
170 void QQmlPropertyData::load(const QMetaMethod &m)
171 {
172     coreIndex = m.methodIndex();
173     arguments = 0;
174     flags |= IsFunction;
175     if (m.methodType() == QMetaMethod::Signal)
176         flags |= IsSignal;
177     propType = QVariant::Invalid;
178
179     const char *returnType = m.typeName();
180     if (returnType) 
181         propType = QMetaType::type(returnType);
182
183     const char *signature = m.signature();
184     while (*signature != '(') { Q_ASSERT(*signature != 0); ++signature; }
185
186     ++signature;
187     if (*signature != ')') {
188         flags |= HasArguments;
189         if (0 == ::strcmp(signature, "QQmlV8Function*)")) {
190             flags |= IsV8Function;
191         }
192     }
193
194     Q_ASSERT(m.revision() <= Q_INT16_MAX);
195     revision = m.revision();
196 }
197
198 void QQmlPropertyData::lazyLoad(const QMetaMethod &m)
199 {
200     coreIndex = m.methodIndex();
201     arguments = 0;
202     flags |= IsFunction;
203     if (m.methodType() == QMetaMethod::Signal)
204         flags |= IsSignal;
205     propType = QVariant::Invalid;
206
207     const char *returnType = m.typeName();
208     if (returnType && *returnType) {
209         propTypeName = returnType;
210         flags |= NotFullyResolved;
211     }
212
213     const char *signature = m.signature();
214     while (*signature != '(') { Q_ASSERT(*signature != 0); ++signature; }
215
216     ++signature;
217     if (*signature != ')') {
218         flags |= HasArguments;
219         if (0 == ::strcmp(signature, "QQmlV8Function*)")) {
220             flags |= IsV8Function;
221         }
222     }
223
224     Q_ASSERT(m.revision() <= Q_INT16_MAX);
225     revision = m.revision();
226 }
227
228 /*!
229 Creates a new empty QQmlPropertyCache.
230 */
231 QQmlPropertyCache::QQmlPropertyCache(QQmlEngine *e)
232 : engine(e), parent(0), propertyIndexCacheStart(0), methodIndexCacheStart(0),
233   signalHanderIndexCacheStart(0), metaObject(0), argumentsCache(0)
234 {
235     Q_ASSERT(engine);
236 }
237
238 /*!
239 Creates a new QQmlPropertyCache of \a metaObject.
240 */
241 QQmlPropertyCache::QQmlPropertyCache(QQmlEngine *e, const QMetaObject *metaObject)
242 : engine(e), parent(0), propertyIndexCacheStart(0), methodIndexCacheStart(0),
243   signalHanderIndexCacheStart(0), metaObject(0), argumentsCache(0)
244 {
245     Q_ASSERT(engine);
246     Q_ASSERT(metaObject);
247
248     update(engine, metaObject);
249 }
250
251 QQmlPropertyCache::~QQmlPropertyCache()
252 {
253     clear();
254
255     QQmlPropertyCacheMethodArguments *args = argumentsCache;
256     while (args) {
257         QQmlPropertyCacheMethodArguments *next = args->next;
258         free(args);
259         args = next;
260     }
261
262     // We must clear this prior to releasing the parent incase it is a
263     // linked hash
264     stringCache.clear();
265     if (parent) parent->release();
266     parent = 0;
267     engine = 0;
268 }
269
270 void QQmlPropertyCache::destroy()
271 {
272     Q_ASSERT(engine || constructor.IsEmpty());
273     if (constructor.IsEmpty())
274         delete this;
275     else
276         QQmlEnginePrivate::deleteInEngineThread(engine, this);
277 }
278
279 // This is inherited from QQmlCleanup, so it should only clear the things
280 // that are tied to the specific QQmlEngine.
281 void QQmlPropertyCache::clear()
282 {
283     qPersistentDispose(constructor);
284     engine = 0;
285 }
286
287 QQmlPropertyCache *QQmlPropertyCache::copy(int reserve)
288 {
289     QQmlPropertyCache *cache = new QQmlPropertyCache(engine);
290     cache->parent = this;
291     cache->parent->addref();
292     cache->propertyIndexCacheStart = propertyIndexCache.count() + propertyIndexCacheStart;
293     cache->methodIndexCacheStart = methodIndexCache.count() + methodIndexCacheStart;
294     cache->signalHanderIndexCacheStart = signalHandlerIndexCache.count() + signalHanderIndexCacheStart;
295     cache->stringCache.linkAndReserve(stringCache, reserve);
296     cache->allowedRevisionCache = allowedRevisionCache;
297     cache->metaObject = metaObject;
298
299     // We specifically do *NOT* copy the constructor
300
301     return cache;
302 }
303
304 QQmlPropertyCache *QQmlPropertyCache::copy()
305 {
306     return copy(0);
307 }
308
309 QQmlPropertyCache *
310 QQmlPropertyCache::copyAndAppend(QQmlEngine *engine, const QMetaObject *metaObject,
311                                          QQmlPropertyData::Flag propertyFlags,
312                                          QQmlPropertyData::Flag methodFlags,
313                                          QQmlPropertyData::Flag signalFlags)
314 {
315     return copyAndAppend(engine, metaObject, -1, propertyFlags, methodFlags, signalFlags);
316 }
317
318 QQmlPropertyCache *
319 QQmlPropertyCache::copyAndAppend(QQmlEngine *engine, const QMetaObject *metaObject,
320                                          int revision,
321                                          QQmlPropertyData::Flag propertyFlags,
322                                          QQmlPropertyData::Flag methodFlags,
323                                          QQmlPropertyData::Flag signalFlags)
324 {
325     Q_ASSERT(QMetaObjectPrivate::get(metaObject)->revision >= 4);
326
327     // Reserve enough space in the name hash for all the methods (including signals), all the
328     // signal handlers and all the properties.  This assumes no name clashes, but this is the
329     // common case.
330     QQmlPropertyCache *rv = copy(QMetaObjectPrivate::get(metaObject)->methodCount +
331                                          QMetaObjectPrivate::get(metaObject)->signalCount +
332                                          QMetaObjectPrivate::get(metaObject)->propertyCount);
333
334     rv->append(engine, metaObject, revision, propertyFlags, methodFlags, signalFlags);
335
336     return rv;
337 }
338
339 void QQmlPropertyCache::append(QQmlEngine *engine, const QMetaObject *metaObject, 
340                                        QQmlPropertyData::Flag propertyFlags,
341                                        QQmlPropertyData::Flag methodFlags,
342                                        QQmlPropertyData::Flag signalFlags)
343 {
344     append(engine, metaObject, -1, propertyFlags, methodFlags, signalFlags);
345 }
346
347 void QQmlPropertyCache::append(QQmlEngine *engine, const QMetaObject *metaObject, 
348                                        int revision, 
349                                        QQmlPropertyData::Flag propertyFlags,
350                                        QQmlPropertyData::Flag methodFlags,
351                                        QQmlPropertyData::Flag signalFlags)
352 {
353     Q_UNUSED(revision);
354     Q_ASSERT(constructor.IsEmpty()); // We should not be appending to an in-use property cache
355
356     this->metaObject = metaObject;
357
358     bool dynamicMetaObject = isDynamicMetaObject(metaObject);
359
360     allowedRevisionCache.append(0);
361
362     int methodCount = metaObject->methodCount();
363     Q_ASSERT(QMetaObjectPrivate::get(metaObject)->revision >= 4);
364     int signalCount = metaObjectSignalCount(metaObject);
365     int classInfoCount = QMetaObjectPrivate::get(metaObject)->classInfoCount;
366
367     QQmlAccessorProperties::Properties accessorProperties;
368
369     // Special case QObject as we don't want to add a qt_HasQmlAccessors classinfo to it
370     if (metaObject == &QObject::staticMetaObject) {
371         accessorProperties = QQmlAccessorProperties::properties(metaObject);
372     } else if (classInfoCount) {
373         int classInfoOffset = metaObject->classInfoOffset();
374         bool hasFastProperty = false;
375         for (int ii = 0; ii < classInfoCount; ++ii) {
376             int idx = ii + classInfoOffset;
377
378             if (0 == qstrcmp(metaObject->classInfo(idx).name(), "qt_HasQmlAccessors")) {
379                 hasFastProperty = true;
380                 break;
381             }
382         }
383
384         if (hasFastProperty) {
385             accessorProperties = QQmlAccessorProperties::properties(metaObject);
386             if (accessorProperties.count == 0)
387                 qFatal("QQmlPropertyCache: %s has FastProperty class info, but has not "
388                        "installed property accessors", metaObject->className());
389         } else {
390 #ifndef QT_NO_DEBUG
391             accessorProperties = QQmlAccessorProperties::properties(metaObject);
392             if (accessorProperties.count != 0)
393                 qFatal("QQmlPropertyCache: %s has fast property accessors, but is missing "
394                        "FastProperty class info", metaObject->className());
395 #endif
396         }
397     }
398
399     // qMax(defaultMethods, methodOffset) to block the signals and slots of QObject::staticMetaObject
400     // incl. destroyed signals, objectNameChanged signal, deleteLater slot, _q_reregisterTimers slot.
401     int methodOffset = qMax(QObject::staticMetaObject.methodCount(), metaObject->methodOffset());
402     int signalOffset = signalCount - QMetaObjectPrivate::get(metaObject)->signalCount;
403
404     // update() should have reserved enough space in the vector that this doesn't cause a realloc
405     // and invalidate the stringCache.
406     methodIndexCache.resize(methodCount - methodIndexCacheStart);
407     signalHandlerIndexCache.resize(signalCount - signalHanderIndexCacheStart);
408     int signalHandlerIndex = signalOffset;
409     for (int ii = methodOffset; ii < methodCount; ++ii) {
410         QMetaMethod m = metaObject->method(ii);
411         if (m.access() == QMetaMethod::Private) 
412             continue;
413
414         // Extract method name
415         const char *signature = m.signature();
416         const char *cptr = signature;
417         char utf8 = 0;
418         while (*cptr != '(') {
419             Q_ASSERT(*cptr != 0);
420             utf8 |= *cptr & 0x80;
421             ++cptr;
422         }
423
424         QQmlPropertyData *data = &methodIndexCache[ii - methodIndexCacheStart];
425         QQmlPropertyData *sigdata = 0;
426
427         data->lazyLoad(m);
428
429         if (data->isSignal())
430             data->flags |= signalFlags;
431         else
432             data->flags |= methodFlags;
433
434         if (!dynamicMetaObject)
435             data->flags |= QQmlPropertyData::IsDirect;
436
437         Q_ASSERT((allowedRevisionCache.count() - 1) < Q_INT16_MAX);
438         data->metaObjectOffset = allowedRevisionCache.count() - 1;
439
440         if (data->isSignal()) {
441             sigdata = &signalHandlerIndexCache[signalHandlerIndex - signalHanderIndexCacheStart];
442             *sigdata = *data;
443             sigdata->flags |= QQmlPropertyData::IsSignalHandler;
444         }
445
446         QQmlPropertyData *old = 0;
447
448         if (utf8) {
449             QHashedString methodName(QString::fromUtf8(signature, cptr - signature));
450             if (QQmlPropertyData **it = stringCache.value(methodName))
451                 old = *it;
452             stringCache.insert(methodName, data);
453
454             if (data->isSignal()) {
455                 QHashedString on(QStringLiteral("on") % methodName.at(0).toUpper() % methodName.midRef(1));
456                 stringCache.insert(on, sigdata);
457                 ++signalHandlerIndex;
458             }
459         } else {
460             QHashedCStringRef methodName(signature, cptr - signature);
461             if (QQmlPropertyData **it = stringCache.value(methodName))
462                 old = *it;
463             stringCache.insert(methodName, data);
464
465             if (data->isSignal()) {
466                 int length = methodName.length();
467
468                 QVarLengthArray<char, 128> str(length+3);
469                 str[0] = 'o';
470                 str[1] = 'n';
471                 str[2] = toupper(signature[0]);
472                 if (length > 1)
473                     memcpy(&str[3], &signature[1], length - 1);
474                 str[length + 2] = '\0';
475
476                 QHashedString on(QString::fromLatin1(str.data()));
477                 stringCache.insert(on, sigdata);
478                 ++signalHandlerIndex;
479             }
480         }
481
482         if (old) {
483             // We only overload methods in the same class, exactly like C++
484             if (old->isFunction() && old->coreIndex >= methodOffset)
485                 data->flags |= QQmlPropertyData::IsOverload;
486             data->overrideIndexIsProperty = !old->isFunction();
487             data->overrideIndex = old->coreIndex;
488         }
489     }
490
491     int propCount = metaObject->propertyCount();
492     int propOffset = metaObject->propertyOffset();
493
494     // update() should have reserved enough space in the vector that this doesn't cause a realloc
495     // and invalidate the stringCache.
496     propertyIndexCache.resize(propCount - propertyIndexCacheStart);
497     for (int ii = propOffset; ii < propCount; ++ii) {
498         QMetaProperty p = metaObject->property(ii);
499         if (!p.isScriptable())
500             continue;
501
502         const char *str = p.name();
503         char utf8 = 0;
504         const char *cptr = str;
505         while (*cptr != 0) {
506             utf8 |= *cptr & 0x80;
507             ++cptr;
508         }
509
510         QQmlPropertyData *data = &propertyIndexCache[ii - propertyIndexCacheStart];
511
512         data->lazyLoad(p, engine);
513         data->flags |= propertyFlags;
514
515         if (!dynamicMetaObject) 
516             data->flags |= QQmlPropertyData::IsDirect;
517
518         Q_ASSERT((allowedRevisionCache.count() - 1) < Q_INT16_MAX);
519         data->metaObjectOffset = allowedRevisionCache.count() - 1;
520
521         QQmlPropertyData *old = 0;
522
523         if (utf8) {
524             QHashedString propName(QString::fromUtf8(str, cptr - str));
525             if (QQmlPropertyData **it = stringCache.value(propName))
526                 old = *it;
527             stringCache.insert(propName, data);
528         } else {
529             QHashedCStringRef propName(str, cptr - str);
530             if (QQmlPropertyData **it = stringCache.value(propName))
531                 old = *it;
532             stringCache.insert(propName, data);
533         }
534
535         QQmlAccessorProperties::Property *accessorProperty = accessorProperties.property(str);
536
537         // Fast properties may not be overrides or revisioned
538         Q_ASSERT(accessorProperty == 0 || (old == 0 && data->revision == 0));
539
540         if (accessorProperty) {
541             data->flags |= QQmlPropertyData::HasAccessors;
542             data->accessors = accessorProperty->accessors;
543             data->accessorData = accessorProperty->data;
544         } else if (old) {
545             data->overrideIndexIsProperty = !old->isFunction();
546             data->overrideIndex = old->coreIndex;
547         }
548     }
549 }
550
551 void QQmlPropertyCache::resolve(QQmlPropertyData *data) const
552 {
553     Q_ASSERT(data->notFullyResolved());
554
555     data->propType = QMetaType::type(data->propTypeName);
556
557     if (!data->isFunction())
558         data->flags |= flagsForPropertyType(data->propType, engine);
559
560     data->flags &= ~QQmlPropertyData::NotFullyResolved;
561 }
562
563 void QQmlPropertyCache::updateRecur(QQmlEngine *engine, const QMetaObject *metaObject)
564 {
565     if (!metaObject)
566         return;
567
568     updateRecur(engine, metaObject->superClass());
569
570     append(engine, metaObject);
571 }
572
573 void QQmlPropertyCache::update(QQmlEngine *engine, const QMetaObject *metaObject)
574 {
575     Q_ASSERT(engine);
576     Q_ASSERT(metaObject);
577     Q_ASSERT(stringCache.isEmpty());
578
579     // Preallocate enough space in the index caches for all the properties/methods/signals that
580     // are not cached in a parent cache so that the caches never need to be reallocated as this
581     // would invalidate pointers stored in the stringCache.
582     int pc = metaObject->propertyCount();
583     int mc = metaObject->methodCount();
584     int sc = metaObjectSignalCount(metaObject);
585     propertyIndexCache.reserve(pc - propertyIndexCacheStart);
586     methodIndexCache.reserve(mc - methodIndexCacheStart);
587     signalHandlerIndexCache.reserve(sc - signalHanderIndexCacheStart);
588
589     // Reserve enough space in the stringCache for all properties/methods/signals including those
590     // cached in a parent cache.
591     stringCache.reserve(pc + mc + sc);
592
593     updateRecur(engine,metaObject);
594 }
595
596 QQmlPropertyData *
597 QQmlPropertyCache::property(int index) const
598 {
599     if (index < 0 || index >= (propertyIndexCacheStart + propertyIndexCache.count()))
600         return 0;
601     
602     if (index < propertyIndexCacheStart)
603         return parent->property(index);
604
605     QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&propertyIndexCache.at(index - propertyIndexCacheStart));
606     if (rv->notFullyResolved()) resolve(rv);
607     return rv;
608 }
609
610 QQmlPropertyData *
611 QQmlPropertyCache::method(int index) const
612 {
613     if (index < 0 || index >= (methodIndexCacheStart + methodIndexCache.count()))
614         return 0;
615
616     if (index < methodIndexCacheStart)
617         return parent->method(index);
618
619     QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&methodIndexCache.at(index - methodIndexCacheStart));
620     if (rv->notFullyResolved()) resolve(rv);
621     return rv;
622 }
623
624 QQmlPropertyData *
625 QQmlPropertyCache::property(const QHashedStringRef &str) const
626 {
627     QQmlPropertyData **rv = stringCache.value(str);
628     if (rv && (*rv)->notFullyResolved()) resolve(*rv);
629     return rv?*rv:0;
630 }
631
632 QQmlPropertyData *
633 QQmlPropertyCache::property(const QHashedCStringRef &str) const
634 {
635     QQmlPropertyData **rv = stringCache.value(str);
636     if (rv && (*rv)->notFullyResolved()) resolve(*rv);
637     return rv?*rv:0;
638 }
639
640 QQmlPropertyData *
641 QQmlPropertyCache::property(const QString &str) const
642 {
643     QQmlPropertyData **rv = stringCache.value(str);
644     if (rv && (*rv)->notFullyResolved()) resolve(*rv);
645     return rv?*rv:0;
646 }
647
648 QString QQmlPropertyData::name(QObject *object)
649 {
650     if (!object)
651         return QString();
652
653     return name(object->metaObject());
654 }
655
656 QString QQmlPropertyData::name(const QMetaObject *metaObject)
657 {
658     if (!metaObject || coreIndex == -1)
659         return QString();
660
661     if (flags & IsFunction) {
662         QMetaMethod m = metaObject->method(coreIndex);
663
664         QString name = QString::fromUtf8(m.signature());
665         int parenIdx = name.indexOf(QLatin1Char('('));
666         if (parenIdx != -1)
667             name = name.left(parenIdx);
668         return name;
669     } else {
670         QMetaProperty p = metaObject->property(coreIndex);
671         return QString::fromUtf8(p.name());
672     }
673 }
674
675 QStringList QQmlPropertyCache::propertyNames() const
676 {
677     QStringList keys;
678     for (StringCache::ConstIterator iter = stringCache.begin(); iter != stringCache.end(); ++iter) 
679         keys.append(iter.key());
680     return keys;
681 }
682
683 static int EnumType(const QMetaObject *meta, const QByteArray &str)
684 {
685     QByteArray scope;
686     QByteArray name;
687     int scopeIdx = str.lastIndexOf("::");
688     if (scopeIdx != -1) {
689         scope = str.left(scopeIdx);
690         name = str.mid(scopeIdx + 2);
691     } else { 
692         name = str;
693     }
694     for (int i = meta->enumeratorCount() - 1; i >= 0; --i) {
695         QMetaEnum m = meta->enumerator(i);
696         if ((m.name() == name) && (scope.isEmpty() || (m.scope() == scope)))
697             return QVariant::Int;
698     }
699     return QVariant::Invalid;
700 }
701
702 // Returns an array of the arguments for method \a index.  The first entry in the array
703 // is the number of arguments.
704 int *QQmlPropertyCache::methodParameterTypes(QObject *object, int index, 
705                                                      QVarLengthArray<int, 9> &dummy,
706                                                      QByteArray *unknownTypeError)
707 {
708     Q_ASSERT(object && index >= 0);
709
710     QQmlData *ddata = QQmlData::get(object, false);
711
712     if (ddata && ddata->propertyCache) {
713         typedef QQmlPropertyCacheMethodArguments A;
714
715         QQmlPropertyCache *c = ddata->propertyCache;
716         Q_ASSERT(index < c->methodIndexCacheStart + c->methodIndexCache.count());
717
718         while (index < c->methodIndexCacheStart)
719             c = c->parent;
720
721         QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&c->methodIndexCache.at(index - c->methodIndexCacheStart));
722
723         if (rv->arguments)  
724             return static_cast<A *>(rv->arguments)->arguments;
725
726         const QMetaObject *metaObject = object->metaObject();
727         QMetaMethod m = metaObject->method(index);
728         QList<QByteArray> argTypeNames = m.parameterTypes();
729
730         A *args = static_cast<A *>(malloc(sizeof(A) + (argTypeNames.count() + 1) * sizeof(int)));
731         args->arguments[0] = argTypeNames.count();
732
733         for (int ii = 0; ii < argTypeNames.count(); ++ii) {
734             int type = QMetaType::type(argTypeNames.at(ii));
735             if (type == QVariant::Invalid)
736                 type = EnumType(object->metaObject(), argTypeNames.at(ii));
737             if (type == QVariant::Invalid) {
738                 if (unknownTypeError) *unknownTypeError = argTypeNames.at(ii);
739                 free(args);
740                 return 0;
741             }
742             args->arguments[ii + 1] = type;
743         }
744
745         rv->arguments = args;
746         args->next = c->argumentsCache;
747         c->argumentsCache = args;
748         return static_cast<A *>(rv->arguments)->arguments;
749
750     } else {
751         QMetaMethod m = object->metaObject()->method(index);
752         QList<QByteArray> argTypeNames = m.parameterTypes();
753         dummy.resize(argTypeNames.count() + 1);
754         dummy[0] = argTypeNames.count();
755
756         for (int ii = 0; ii < argTypeNames.count(); ++ii) {
757             int type = QMetaType::type(argTypeNames.at(ii));
758             if (type == QVariant::Invalid)
759                 type = EnumType(object->metaObject(), argTypeNames.at(ii));
760             if (type == QVariant::Invalid) {
761                 if (unknownTypeError) *unknownTypeError = argTypeNames.at(ii);
762                 return 0;
763             }
764             dummy[ii + 1] = type;
765         }
766
767         return dummy.data();
768     }
769 }
770
771 QQmlPropertyData qQmlPropertyCacheCreate(const QMetaObject *metaObject,
772                                                          const QString &property)
773 {
774     Q_ASSERT(metaObject);
775
776     QQmlPropertyData rv;
777     {
778         const QMetaObject *cmo = metaObject;
779         const QByteArray propertyName = property.toUtf8();
780         while (cmo) {
781             int idx = cmo->indexOfProperty(propertyName);
782             if (idx != -1) {
783                 QMetaProperty p = cmo->property(idx);
784                 if (p.isScriptable()) {
785                     rv.load(p);
786                     return rv;
787                 } else {
788                     while (cmo && cmo->propertyOffset() >= idx)
789                         cmo = cmo->superClass();
790                 }
791             } else {
792                 cmo = 0;
793             }
794         }
795     }
796
797     int methodCount = metaObject->methodCount();
798     int defaultMethods = QObject::staticMetaObject.methodCount();
799     for (int ii = methodCount - 1; ii >= defaultMethods; --ii) {
800         // >=defaultMethods to block the signals and slots of QObject::staticMetaObject
801         // incl. destroyed signals, objectNameChanged signal, deleteLater slot, _q_reregisterTimers slot.
802         QMetaMethod m = metaObject->method(ii);
803         if (m.access() == QMetaMethod::Private)
804             continue;
805         QString methodName = QString::fromUtf8(m.signature());
806
807         int parenIdx = methodName.indexOf(QLatin1Char('('));
808         Q_ASSERT(parenIdx != -1);
809         QStringRef methodNameRef = methodName.leftRef(parenIdx);
810
811         if (methodNameRef == property) {
812             rv.load(m);
813             return rv;
814         }
815     }
816
817     return rv;
818 }
819
820 inline const QString &qQmlPropertyCacheToString(const QString &string)
821 {
822     return string;
823 }
824
825 inline QString qQmlPropertyCacheToString(const QHashedV8String &string)
826 {
827     return QV8Engine::toStringStatic(string.string());
828 }
829
830 template<typename T>
831 QQmlPropertyData *
832 qQmlPropertyCacheProperty(QQmlEngine *engine, QObject *obj,
833                                   const T &name, QQmlPropertyData &local)
834 {
835     QQmlPropertyCache *cache = 0;
836
837     if (engine) {
838         QQmlData *ddata = QQmlData::get(obj);
839
840         if (ddata && ddata->propertyCache) {
841             cache = ddata->propertyCache;
842         } else if (engine) {
843             QQmlEnginePrivate *ep = QQmlEnginePrivate::get(engine);
844             cache = ep->cache(obj);
845             if (cache) {
846                 ddata = QQmlData::get(obj, true);
847                 cache->addref();
848                 ddata->propertyCache = cache;
849             }
850         }
851     }
852
853     QQmlPropertyData *rv = 0;
854
855     if (cache) {
856         rv = cache->property(name);
857     } else {
858         local = qQmlPropertyCacheCreate(obj->metaObject(),
859                                                 qQmlPropertyCacheToString(name));
860         if (local.isValid())
861             rv = &local;
862     }
863
864     return rv;
865 }
866
867 QQmlPropertyData *
868 QQmlPropertyCache::property(QQmlEngine *engine, QObject *obj, 
869                                     const QHashedV8String &name, QQmlPropertyData &local)
870 {
871     return qQmlPropertyCacheProperty<QHashedV8String>(engine, obj, name, local);
872 }
873
874 QQmlPropertyData *
875 QQmlPropertyCache::property(QQmlEngine *engine, QObject *obj,
876                                     const QString &name, QQmlPropertyData &local)
877 {
878     return qQmlPropertyCacheProperty<QString>(engine, obj, name, local);
879 }
880
881 static inline const QMetaObjectPrivate *priv(const uint* data)
882 { return reinterpret_cast<const QMetaObjectPrivate*>(data); }
883
884 bool QQmlPropertyCache::isDynamicMetaObject(const QMetaObject *mo)
885 {
886     return priv(mo->d.data)->revision >= 3 && priv(mo->d.data)->flags & DynamicMetaObject;
887 }
888
889 QT_END_NAMESPACE