1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "private/qdeclarativepropertycache_p.h"
44 #include "private/qdeclarativeengine_p.h"
45 #include "private/qdeclarativebinding_p.h"
46 #include "private/qv8engine_p.h"
48 #include <private/qmetaobject_p.h>
50 #include <QtCore/qdebug.h>
52 Q_DECLARE_METATYPE(QJSValue)
53 Q_DECLARE_METATYPE(QDeclarativeV8Handle);
57 // Flags that do *NOT* depend on the property's QMetaProperty::userType() and thus are quick
59 static QDeclarativePropertyCache::Data::Flags fastFlagsForProperty(const QMetaProperty &p)
61 QDeclarativePropertyCache::Data::Flags flags;
64 flags |= QDeclarativePropertyCache::Data::IsConstant;
66 flags |= QDeclarativePropertyCache::Data::IsWritable;
68 flags |= QDeclarativePropertyCache::Data::IsResettable;
70 flags |= QDeclarativePropertyCache::Data::IsFinal;
72 flags |= QDeclarativePropertyCache::Data::IsEnumType;
77 // Flags that do depend on the property's QMetaProperty::userType() and thus are slow to
79 static QDeclarativePropertyCache::Data::Flags flagsForPropertyType(int propType, QDeclarativeEngine *engine)
81 QDeclarativePropertyCache::Data::Flags flags;
83 if (propType < QMetaType::User && propType != QMetaType::QObjectStar && propType != QMetaType::QWidgetStar) {
84 } else if (propType == qMetaTypeId<QDeclarativeBinding *>()) {
85 flags |= QDeclarativePropertyCache::Data::IsQmlBinding;
86 } else if (propType == qMetaTypeId<QJSValue>()) {
87 flags |= QDeclarativePropertyCache::Data::IsQJSValue;
88 } else if (propType == qMetaTypeId<QDeclarativeV8Handle>()) {
89 flags |= QDeclarativePropertyCache::Data::IsV8Handle;
91 QDeclarativeMetaType::TypeCategory cat =
92 engine ? QDeclarativeEnginePrivate::get(engine)->typeCategory(propType)
93 : QDeclarativeMetaType::typeCategory(propType);
95 if (cat == QDeclarativeMetaType::Object)
96 flags |= QDeclarativePropertyCache::Data::IsQObjectDerived;
97 else if (cat == QDeclarativeMetaType::List)
98 flags |= QDeclarativePropertyCache::Data::IsQList;
104 QDeclarativePropertyCache::Data::Flags
105 QDeclarativePropertyCache::Data::flagsForProperty(const QMetaProperty &p, QDeclarativeEngine *engine)
107 return fastFlagsForProperty(p) | flagsForPropertyType(p.userType(), engine);
110 void QDeclarativePropertyCache::Data::lazyLoad(const QMetaProperty &p, QDeclarativeEngine *engine)
114 coreIndex = p.propertyIndex();
115 notifyIndex = p.notifySignalIndex();
116 revision = p.revision();
118 flags = fastFlagsForProperty(p);
121 if (type == QMetaType::QObjectStar || type == QMetaType::QWidgetStar) {
123 flags |= QDeclarativePropertyCache::Data::IsQObjectDerived;
124 } else if (type == QVariant::UserType || type == -1) {
125 propTypeName = p.typeName();
126 flags |= QDeclarativePropertyCache::Data::NotFullyResolved;
132 void QDeclarativePropertyCache::Data::load(const QMetaProperty &p, QDeclarativeEngine *engine)
134 propType = p.userType();
135 if (QVariant::Type(propType) == QVariant::LastType)
136 propType = qMetaTypeId<QVariant>();
137 coreIndex = p.propertyIndex();
138 notifyIndex = p.notifySignalIndex();
139 flags = fastFlagsForProperty(p) | flagsForPropertyType(propType, engine);
140 revision = p.revision();
143 void QDeclarativePropertyCache::Data::load(const QMetaMethod &m)
145 coreIndex = m.methodIndex();
147 flags |= Data::IsFunction;
148 if (m.methodType() == QMetaMethod::Signal)
149 flags |= Data::IsSignal;
150 propType = QVariant::Invalid;
152 const char *returnType = m.typeName();
154 propType = QMetaType::type(returnType);
156 const char *signature = m.signature();
157 while (*signature != '(') { Q_ASSERT(*signature != 0); ++signature; }
160 if (*signature != ')') {
161 flags |= Data::HasArguments;
162 if (0 == ::strcmp(signature, "QDeclarativeV8Function*)")) {
163 flags |= Data::IsV8Function;
167 revision = m.revision();
170 void QDeclarativePropertyCache::Data::lazyLoad(const QMetaMethod &m)
172 coreIndex = m.methodIndex();
174 flags |= Data::IsFunction;
175 if (m.methodType() == QMetaMethod::Signal)
176 flags |= Data::IsSignal;
177 propType = QVariant::Invalid;
179 const char *returnType = m.typeName();
180 if (returnType && *returnType) {
181 propTypeName = returnType;
182 flags |= Data::NotFullyResolved;
185 const char *signature = m.signature();
186 while (*signature != '(') { Q_ASSERT(*signature != 0); ++signature; }
189 if (*signature != ')') {
190 flags |= Data::HasArguments;
191 if (0 == ::strcmp(signature, "QDeclarativeV8Function*)")) {
192 flags |= Data::IsV8Function;
196 revision = m.revision();
200 Creates a new empty QDeclarativePropertyCache.
202 QDeclarativePropertyCache::QDeclarativePropertyCache(QDeclarativeEngine *e)
203 : QDeclarativeCleanup(e), engine(e), parent(0), propertyIndexCacheStart(0), methodIndexCacheStart(0)
209 Creates a new QDeclarativePropertyCache of \a metaObject.
211 QDeclarativePropertyCache::QDeclarativePropertyCache(QDeclarativeEngine *e, const QMetaObject *metaObject)
212 : QDeclarativeCleanup(e), engine(e), parent(0), propertyIndexCacheStart(0), methodIndexCacheStart(0)
215 Q_ASSERT(metaObject);
217 update(engine, metaObject);
220 QDeclarativePropertyCache::~QDeclarativePropertyCache()
224 if (parent) parent->release();
228 // This is inherited from QDeclarativeCleanup, so it should only clear the things
229 // that are tied to the specific QDeclarativeEngine.
230 void QDeclarativePropertyCache::clear()
232 qPersistentDispose(constructor);
236 QDeclarativePropertyCache::Data QDeclarativePropertyCache::create(const QMetaObject *metaObject,
237 const QString &property)
239 Q_ASSERT(metaObject);
241 QDeclarativePropertyCache::Data rv;
243 const QMetaObject *cmo = metaObject;
245 int idx = metaObject->indexOfProperty(property.toUtf8());
247 QMetaProperty p = metaObject->property(idx);
248 if (p.isScriptable()) {
249 rv.load(metaObject->property(idx));
252 while (cmo && cmo->propertyOffset() >= idx)
253 cmo = cmo->superClass();
261 int methodCount = metaObject->methodCount();
262 for (int ii = methodCount - 1; ii >= 3; --ii) { // >=3 to block the destroyed signal and deleteLater() slot
263 QMetaMethod m = metaObject->method(ii);
264 if (m.access() == QMetaMethod::Private)
266 QString methodName = QString::fromUtf8(m.signature());
268 int parenIdx = methodName.indexOf(QLatin1Char('('));
269 Q_ASSERT(parenIdx != -1);
270 QStringRef methodNameRef = methodName.leftRef(parenIdx);
272 if (methodNameRef == property) {
281 QDeclarativePropertyCache *QDeclarativePropertyCache::copy()
283 QDeclarativePropertyCache *cache = new QDeclarativePropertyCache(engine);
284 cache->parent = this;
285 cache->parent->addref();
286 cache->propertyIndexCacheStart = propertyIndexCache.count() + propertyIndexCacheStart;
287 cache->methodIndexCacheStart = methodIndexCache.count() + methodIndexCacheStart;
288 cache->stringCache = stringCache;
289 cache->allowedRevisionCache = allowedRevisionCache;
291 // We specifically do *NOT* copy the constructor
296 void QDeclarativePropertyCache::append(QDeclarativeEngine *engine, const QMetaObject *metaObject,
297 Data::Flag propertyFlags, Data::Flag methodFlags, Data::Flag signalFlags)
299 append(engine, metaObject, -1, propertyFlags, methodFlags, signalFlags);
302 void QDeclarativePropertyCache::append(QDeclarativeEngine *engine, const QMetaObject *metaObject,
304 Data::Flag propertyFlags, Data::Flag methodFlags, Data::Flag signalFlags)
308 qPersistentDispose(constructor); // Now invalid
310 bool dynamicMetaObject = isDynamicMetaObject(metaObject);
312 allowedRevisionCache.append(0);
314 int methodCount = metaObject->methodCount();
315 // 3 to block the destroyed signal and the deleteLater() slot
316 int methodOffset = qMax(3, metaObject->methodOffset());
318 methodIndexCache.resize(methodCount - methodIndexCacheStart);
319 for (int ii = methodOffset; ii < methodCount; ++ii) {
320 QMetaMethod m = metaObject->method(ii);
321 if (m.access() == QMetaMethod::Private)
324 // Extract method name
325 const char *signature = m.signature();
326 const char *cptr = signature;
327 while (*cptr != '(') { Q_ASSERT(*cptr != 0); ++cptr; }
328 QString str = dynamicMetaObject?QString::fromUtf8(signature, cptr - signature):
329 QString::fromLatin1(signature, cptr - signature);
330 QHashedString methodName(str);
332 Data *data = &methodIndexCache[ii - methodIndexCacheStart];
335 if (data->isSignal())
336 data->flags |= signalFlags;
338 data->flags |= methodFlags;
340 if (!dynamicMetaObject)
341 data->flags |= Data::IsDirect;
343 data->metaObjectOffset = allowedRevisionCache.count() - 1;
345 if (Data **old = stringCache.value(methodName)) {
346 // We only overload methods in the same class, exactly like C++
347 if ((*old)->flags & Data::IsFunction && (*old)->coreIndex >= methodOffset)
348 data->relatedIndex = (*old)->coreIndex;
349 data->overrideIndexIsProperty = !bool((*old)->flags & Data::IsFunction);
350 data->overrideIndex = (*old)->coreIndex;
353 stringCache.insert(methodName, data);
356 int propCount = metaObject->propertyCount();
357 int propOffset = metaObject->propertyOffset();
359 propertyIndexCache.resize(propCount - propertyIndexCacheStart);
360 for (int ii = propOffset; ii < propCount; ++ii) {
361 QMetaProperty p = metaObject->property(ii);
362 if (!p.isScriptable())
365 QString str = dynamicMetaObject?QString::fromUtf8(p.name()):
366 QString::fromLatin1(p.name());
367 QHashedString propName(str);
369 Data *data = &propertyIndexCache[ii - propertyIndexCacheStart];
371 data->lazyLoad(p, engine);
372 data->flags |= propertyFlags;
374 if (!dynamicMetaObject)
375 data->flags |= Data::IsDirect;
377 data->metaObjectOffset = allowedRevisionCache.count() - 1;
379 if (Data **old = stringCache.value(propName)) {
380 data->overrideIndexIsProperty = !bool((*old)->flags & Data::IsFunction);
381 data->overrideIndex = (*old)->coreIndex;
384 stringCache.insert(propName, data);
388 void QDeclarativePropertyCache::resolve(Data *data) const
390 Q_ASSERT(data->notFullyResolved());
392 data->propType = QMetaType::type(data->propTypeName);
393 if (QVariant::Type(data->propType) == QVariant::LastType)
394 data->propType = qMetaTypeId<QVariant>();
397 if (!(data->flags & Data::IsFunction))
398 data->flags |= flagsForPropertyType(data->propType, engine);
400 data->flags &= ~Data::NotFullyResolved;
403 void QDeclarativePropertyCache::updateRecur(QDeclarativeEngine *engine, const QMetaObject *metaObject)
408 updateRecur(engine, metaObject->superClass());
410 append(engine, metaObject);
413 void QDeclarativePropertyCache::update(QDeclarativeEngine *engine, const QMetaObject *metaObject)
416 Q_ASSERT(metaObject);
417 Q_ASSERT(stringCache.isEmpty());
419 // Optimization to prevent unnecessary reallocation of lists
420 propertyIndexCache.reserve(metaObject->propertyCount());
421 methodIndexCache.reserve(metaObject->methodCount());
423 updateRecur(engine,metaObject);
426 QDeclarativePropertyCache::Data *
427 QDeclarativePropertyCache::property(int index) const
429 if (index < 0 || index >= (propertyIndexCacheStart + propertyIndexCache.count()))
432 if (index < propertyIndexCacheStart)
433 return parent->property(index);
435 Data *rv = const_cast<Data *>(&propertyIndexCache.at(index - propertyIndexCacheStart));
436 if (rv->notFullyResolved()) resolve(rv);
440 QDeclarativePropertyCache::Data *
441 QDeclarativePropertyCache::method(int index) const
443 if (index < 0 || index >= (methodIndexCacheStart + methodIndexCache.count()))
446 if (index < methodIndexCacheStart)
447 return parent->method(index);
449 Data *rv = const_cast<Data *>(&methodIndexCache.at(index - methodIndexCacheStart));
450 if (rv->notFullyResolved()) resolve(rv);
454 QDeclarativePropertyCache::Data *
455 QDeclarativePropertyCache::property(const QString &str) const
457 QDeclarativePropertyCache::Data **rv = stringCache.value(str);
458 if (rv && (*rv)->notFullyResolved()) resolve(*rv);
462 QString QDeclarativePropertyCache::Data::name(QObject *object)
467 return name(object->metaObject());
470 QString QDeclarativePropertyCache::Data::name(const QMetaObject *metaObject)
472 if (!metaObject || coreIndex == -1)
475 if (flags & IsFunction) {
476 QMetaMethod m = metaObject->method(coreIndex);
478 QString name = QString::fromUtf8(m.signature());
479 int parenIdx = name.indexOf(QLatin1Char('('));
481 name = name.left(parenIdx);
484 QMetaProperty p = metaObject->property(coreIndex);
485 return QString::fromUtf8(p.name());
489 QStringList QDeclarativePropertyCache::propertyNames() const
492 for (StringCache::ConstIterator iter = stringCache.begin(); iter != stringCache.end(); ++iter)
493 keys.append(iter.key());
497 QDeclarativePropertyCache::Data *
498 QDeclarativePropertyCache::property(QDeclarativeEngine *engine, QObject *obj,
499 const QHashedV8String &name, Data &local)
501 // XXX Optimize for worker script case where engine isn't available
502 QDeclarativePropertyCache *cache = 0;
504 QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine);
506 QDeclarativeData *ddata = QDeclarativeData::get(obj);
507 if (ddata && ddata->propertyCache)
508 cache = ddata->propertyCache;
510 cache = ep->cache(obj);
511 if (cache && ddata && !ddata->propertyCache) { cache->addref(); ddata->propertyCache = cache; }
515 QDeclarativePropertyCache::Data *rv = 0;
518 rv = cache->property(name);
520 QString strname = QV8Engine::toStringStatic(name.string());
521 // QString strname = ep->v8engine()->toString(name);
522 local = QDeclarativePropertyCache::create(obj->metaObject(), strname);
530 QDeclarativePropertyCache::Data *
531 QDeclarativePropertyCache::property(QDeclarativeEngine *engine, QObject *obj,
532 const QString &name, Data &local)
534 QDeclarativePropertyCache::Data *rv = 0;
537 local = QDeclarativePropertyCache::create(obj->metaObject(), name);
541 QDeclarativeEnginePrivate *enginePrivate = QDeclarativeEnginePrivate::get(engine);
543 QDeclarativePropertyCache *cache = 0;
544 QDeclarativeData *ddata = QDeclarativeData::get(obj);
545 if (ddata && ddata->propertyCache)
546 cache = ddata->propertyCache;
548 cache = enginePrivate->cache(obj);
549 if (cache && ddata && !ddata->propertyCache) { cache->addref(); ddata->propertyCache = cache; }
553 rv = cache->property(name);
555 local = QDeclarativePropertyCache::create(obj->metaObject(), name);
564 static inline const QMetaObjectPrivate *priv(const uint* data)
565 { return reinterpret_cast<const QMetaObjectPrivate*>(data); }
567 bool QDeclarativePropertyCache::isDynamicMetaObject(const QMetaObject *mo)
569 return priv(mo->d.data)->revision >= 3 && priv(mo->d.data)->flags & DynamicMetaObject;