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