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(QScriptValue)
53 Q_DECLARE_METATYPE(QDeclarativeV8Handle);
57 QDeclarativePropertyCache::Data::Flags QDeclarativePropertyCache::Data::flagsForProperty(const QMetaProperty &p, QDeclarativeEngine *engine)
59 int propType = p.userType();
64 flags |= Data::IsConstant;
66 flags |= Data::IsWritable;
68 flags |= Data::IsResettable;
70 flags |= Data::IsFinal;
72 if (propType == qMetaTypeId<QDeclarativeBinding *>()) {
73 flags |= Data::IsQmlBinding;
74 } else if (propType == qMetaTypeId<QScriptValue>()) {
75 flags |= Data::IsQScriptValue;
76 } else if (propType == qMetaTypeId<QDeclarativeV8Handle>()) {
77 flags |= Data::IsV8Handle;
78 } else if (p.isEnumType()) {
79 flags |= Data::IsEnumType;
81 QDeclarativeMetaType::TypeCategory cat = engine ? QDeclarativeEnginePrivate::get(engine)->typeCategory(propType)
82 : QDeclarativeMetaType::typeCategory(propType);
83 if (cat == QDeclarativeMetaType::Object)
84 flags |= Data::IsQObjectDerived;
85 else if (cat == QDeclarativeMetaType::List)
86 flags |= Data::IsQList;
92 void QDeclarativePropertyCache::Data::load(const QMetaProperty &p, QDeclarativeEngine *engine)
94 propType = p.userType();
95 if (QVariant::Type(propType) == QVariant::LastType)
96 propType = qMetaTypeId<QVariant>();
97 coreIndex = p.propertyIndex();
98 notifyIndex = p.notifySignalIndex();
99 flags = flagsForProperty(p, engine);
100 revision = p.revision();
103 void QDeclarativePropertyCache::Data::load(const QMetaMethod &m)
105 coreIndex = m.methodIndex();
107 flags |= Data::IsFunction;
108 if (m.methodType() == QMetaMethod::Signal)
109 flags |= Data::IsSignal;
110 propType = QVariant::Invalid;
112 const char *returnType = m.typeName();
114 propType = QMetaType::type(returnType);
116 QList<QByteArray> params = m.parameterTypes();
117 if (!params.isEmpty()) {
118 flags |= Data::HasArguments;
119 if (params.at(0).length() == 23 &&
120 0 == qstrcmp(params.at(0).constData(), "QDeclarativeV8Function*")) {
121 flags |= Data::IsV8Function;
124 revision = m.revision();
129 Creates a new empty QDeclarativePropertyCache.
131 QDeclarativePropertyCache::QDeclarativePropertyCache(QDeclarativeEngine *e)
132 : QDeclarativeCleanup(e), engine(e)
138 Creates a new QDeclarativePropertyCache of \a metaObject.
140 QDeclarativePropertyCache::QDeclarativePropertyCache(QDeclarativeEngine *e, const QMetaObject *metaObject)
141 : QDeclarativeCleanup(e), engine(e)
144 Q_ASSERT(metaObject);
146 update(engine, metaObject);
149 QDeclarativePropertyCache::~QDeclarativePropertyCache()
154 void QDeclarativePropertyCache::clear()
156 for (int ii = 0; ii < indexCache.count(); ++ii) {
157 if (indexCache.at(ii)) indexCache.at(ii)->release();
160 for (int ii = 0; ii < methodIndexCache.count(); ++ii) {
161 RData *data = methodIndexCache.at(ii);
162 if (data) data->release();
165 for (StringCache::ConstIterator iter = stringCache.begin();
166 iter != stringCache.end(); ++iter) {
167 RData *data = (*iter);
172 methodIndexCache.clear();
174 qPersistentDispose(constructor);
177 QDeclarativePropertyCache::Data QDeclarativePropertyCache::create(const QMetaObject *metaObject,
178 const QString &property)
180 Q_ASSERT(metaObject);
182 QDeclarativePropertyCache::Data rv;
184 const QMetaObject *cmo = metaObject;
186 int idx = metaObject->indexOfProperty(property.toUtf8());
188 QMetaProperty p = metaObject->property(idx);
189 if (p.isScriptable()) {
190 rv.load(metaObject->property(idx));
193 while (cmo && cmo->propertyOffset() >= idx)
194 cmo = cmo->superClass();
202 int methodCount = metaObject->methodCount();
203 for (int ii = methodCount - 1; ii >= 3; --ii) { // >=3 to block the destroyed signal and deleteLater() slot
204 QMetaMethod m = metaObject->method(ii);
205 if (m.access() == QMetaMethod::Private)
207 QString methodName = QString::fromUtf8(m.signature());
209 int parenIdx = methodName.indexOf(QLatin1Char('('));
210 Q_ASSERT(parenIdx != -1);
211 QStringRef methodNameRef = methodName.leftRef(parenIdx);
213 if (methodNameRef == property) {
222 QDeclarativePropertyCache *QDeclarativePropertyCache::copy() const
224 QDeclarativePropertyCache *cache = new QDeclarativePropertyCache(engine);
225 cache->indexCache = indexCache;
226 cache->methodIndexCache = methodIndexCache;
227 cache->stringCache = stringCache;
228 cache->allowedRevisionCache = allowedRevisionCache;
230 for (int ii = 0; ii < indexCache.count(); ++ii) {
231 if (indexCache.at(ii)) indexCache.at(ii)->addref();
233 for (int ii = 0; ii < methodIndexCache.count(); ++ii) {
234 if (methodIndexCache.at(ii)) methodIndexCache.at(ii)->addref();
236 for (StringCache::ConstIterator iter = stringCache.begin(); iter != stringCache.end(); ++iter)
239 // We specifically do *NOT* copy the constructor
244 void QDeclarativePropertyCache::append(QDeclarativeEngine *engine, const QMetaObject *metaObject,
245 Data::Flag propertyFlags, Data::Flag methodFlags, Data::Flag signalFlags)
247 append(engine, metaObject, -1, propertyFlags, methodFlags, signalFlags);
250 void QDeclarativePropertyCache::append(QDeclarativeEngine *engine, const QMetaObject *metaObject,
252 Data::Flag propertyFlags, Data::Flag methodFlags, Data::Flag signalFlags)
256 qPersistentDispose(constructor); // Now invalid
258 bool dynamicMetaObject = isDynamicMetaObject(metaObject);
260 allowedRevisionCache.append(0);
262 QDeclarativeEnginePrivate *enginePriv = QDeclarativeEnginePrivate::get(engine);
263 int methodCount = metaObject->methodCount();
264 // 3 to block the destroyed signal and the deleteLater() slot
265 int methodOffset = qMax(3, metaObject->methodOffset());
267 methodIndexCache.resize(methodCount);
268 for (int ii = methodOffset; ii < methodCount; ++ii) {
269 QMetaMethod m = metaObject->method(ii);
270 if (m.access() == QMetaMethod::Private)
272 QString methodName = QString::fromUtf8(m.signature());
274 int parenIdx = methodName.indexOf(QLatin1Char('('));
275 Q_ASSERT(parenIdx != -1);
276 methodName = methodName.left(parenIdx);
278 RData *data = new RData;
279 methodIndexCache[ii] = data;
282 if (m.methodType() == QMetaMethod::Slot || m.methodType() == QMetaMethod::Method)
283 data->flags |= methodFlags;
284 else if (m.methodType() == QMetaMethod::Signal)
285 data->flags |= signalFlags;
287 if (!dynamicMetaObject)
288 data->flags |= Data::IsDirect;
290 data->metaObjectOffset = allowedRevisionCache.count() - 1;
292 if (stringCache.contains(methodName)) {
293 RData *old = stringCache[methodName];
294 // We only overload methods in the same class, exactly like C++
295 if (old->flags & Data::IsFunction && old->coreIndex >= methodOffset)
296 data->relatedIndex = old->coreIndex;
297 data->overrideIndexIsProperty = !bool(old->flags & Data::IsFunction);
298 data->overrideIndex = old->coreIndex;
299 stringCache[methodName]->release();
302 stringCache.insert(methodName, data);
306 int propCount = metaObject->propertyCount();
307 int propOffset = metaObject->propertyOffset();
309 indexCache.resize(propCount);
310 for (int ii = propOffset; ii < propCount; ++ii) {
311 QMetaProperty p = metaObject->property(ii);
312 if (!p.isScriptable())
315 QString propName = QString::fromUtf8(p.name());
317 RData *data = new RData;
318 indexCache[ii] = data;
320 data->load(p, engine);
321 data->flags |= propertyFlags;
323 if (!dynamicMetaObject)
324 data->flags |= Data::IsDirect;
326 data->metaObjectOffset = allowedRevisionCache.count() - 1;
328 if (stringCache.contains(propName)) {
329 RData *old = stringCache[propName];
330 data->overrideIndexIsProperty = !bool(old->flags & Data::IsFunction);
331 data->overrideIndex = old->coreIndex;
332 stringCache[propName]->release();
335 stringCache.insert(propName, data);
340 void QDeclarativePropertyCache::updateRecur(QDeclarativeEngine *engine, const QMetaObject *metaObject)
345 updateRecur(engine, metaObject->superClass());
347 append(engine, metaObject);
350 void QDeclarativePropertyCache::update(QDeclarativeEngine *engine, const QMetaObject *metaObject)
353 Q_ASSERT(metaObject);
357 // Optimization to prevent unnecessary reallocation of lists
358 indexCache.reserve(metaObject->propertyCount());
359 methodIndexCache.reserve(metaObject->methodCount());
361 updateRecur(engine,metaObject);
364 QDeclarativePropertyCache::Data *
365 QDeclarativePropertyCache::property(int index) const
367 if (index < 0 || index >= indexCache.count())
370 return indexCache.at(index);
373 QDeclarativePropertyCache::Data *
374 QDeclarativePropertyCache::method(int index) const
376 if (index < 0 || index >= methodIndexCache.count())
379 return methodIndexCache.at(index);
382 QDeclarativePropertyCache::Data *
383 QDeclarativePropertyCache::property(const QString &str) const
385 QDeclarativePropertyCache::RData **rv = stringCache.value(str);
389 QString QDeclarativePropertyCache::Data::name(QObject *object)
394 return name(object->metaObject());
397 QString QDeclarativePropertyCache::Data::name(const QMetaObject *metaObject)
399 if (!metaObject || coreIndex == -1)
402 if (flags & IsFunction) {
403 QMetaMethod m = metaObject->method(coreIndex);
405 QString name = QString::fromUtf8(m.signature());
406 int parenIdx = name.indexOf(QLatin1Char('('));
408 name = name.left(parenIdx);
411 QMetaProperty p = metaObject->property(coreIndex);
412 return QString::fromUtf8(p.name());
416 QStringList QDeclarativePropertyCache::propertyNames() const
419 for (StringCache::ConstIterator iter = stringCache.begin(); iter != stringCache.end(); ++iter)
420 keys.append(iter.key());
424 QDeclarativePropertyCache::Data *
425 QDeclarativePropertyCache::property(QDeclarativeEngine *engine, QObject *obj,
426 const QHashedV8String &name, Data &local)
428 // XXX Optimize for worker script case where engine isn't available
429 QDeclarativePropertyCache *cache = 0;
431 QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine);
433 QDeclarativeData *ddata = QDeclarativeData::get(obj);
434 if (ddata && ddata->propertyCache && ddata->propertyCache->qmlEngine() == engine) // XXX aakenend
435 cache = ddata->propertyCache;
437 cache = ep->cache(obj);
438 if (cache && ddata && !ddata->propertyCache) { cache->addref(); ddata->propertyCache = cache; }
442 QDeclarativePropertyCache::Data *rv = 0;
445 rv = cache->property(name);
447 QString strname = QV8Engine::toStringStatic(name.string());
448 // QString strname = ep->v8engine.toString(name);
449 local = QDeclarativePropertyCache::create(obj->metaObject(), strname);
457 QDeclarativePropertyCache::Data *
458 QDeclarativePropertyCache::property(QDeclarativeEngine *engine, QObject *obj,
459 const QString &name, Data &local)
461 QDeclarativePropertyCache::Data *rv = 0;
464 local = QDeclarativePropertyCache::create(obj->metaObject(), name);
468 QDeclarativeEnginePrivate *enginePrivate = QDeclarativeEnginePrivate::get(engine);
470 QDeclarativePropertyCache *cache = 0;
471 QDeclarativeData *ddata = QDeclarativeData::get(obj);
472 if (ddata && ddata->propertyCache && ddata->propertyCache->qmlEngine() == engine)
473 cache = ddata->propertyCache;
475 cache = enginePrivate->cache(obj);
476 if (cache && ddata && !ddata->propertyCache) { cache->addref(); ddata->propertyCache = cache; }
480 rv = cache->property(name);
482 local = QDeclarativePropertyCache::create(obj->metaObject(), name);
491 static inline const QMetaObjectPrivate *priv(const uint* data)
492 { return reinterpret_cast<const QMetaObjectPrivate*>(data); }
494 bool QDeclarativePropertyCache::isDynamicMetaObject(const QMetaObject *mo)
496 return priv(mo->d.data)->revision >= 3 && priv(mo->d.data)->flags & DynamicMetaObject;