1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include "qqmlvmemetaobject_p.h"
46 #include <private/qqmlrefcount_p.h>
47 #include "qqmlexpression.h"
48 #include "qqmlexpression_p.h"
49 #include "qqmlcontext_p.h"
50 #include "qqmlbinding_p.h"
51 #include "qqmlpropertyvalueinterceptor_p.h"
53 #include <private/qv8variantresource_p.h>
54 #include <private/qqmlglobal_p.h>
56 Q_DECLARE_METATYPE(QJSValue);
60 QQmlVMEVariantQObjectPtr::QQmlVMEVariantQObjectPtr(bool isVar)
61 : QQmlGuard<QObject>(0), m_target(0), m_isVar(isVar), m_index(-1)
65 QQmlVMEVariantQObjectPtr::~QQmlVMEVariantQObjectPtr()
69 void QQmlVMEVariantQObjectPtr::objectDestroyed(QObject *)
71 if (m_target && m_index >= 0) {
72 if (m_isVar && m_target->varPropertiesInitialized && !m_target->varProperties.IsEmpty()) {
73 // Set the var property to NULL
74 m_target->varProperties->Set(m_index - m_target->firstVarPropertyIndex, v8::Null());
77 m_target->activate(m_target->object, m_target->methodOffset() + m_index, 0);
81 void QQmlVMEVariantQObjectPtr::setGuardedValue(QObject *obj, QQmlVMEMetaObject *target, int index)
91 inline QQmlVMEVariant();
92 inline ~QQmlVMEVariant();
94 inline const void *dataPtr() const;
95 inline void *dataPtr();
96 inline int dataType() const;
97 inline size_t dataSize() const;
99 inline QObject *asQObject();
100 inline const QVariant &asQVariant();
102 inline bool asBool();
103 inline double asDouble();
104 inline const QString &asQString();
105 inline const QUrl &asQUrl();
106 inline const QTime &asQTime();
107 inline const QDate &asQDate();
108 inline const QDateTime &asQDateTime();
109 inline const QRectF &asQRectF();
110 inline const QPointF &asQPointF();
111 inline const QSizeF &asQSizeF();
112 inline const QJSValue &asQJSValue();
114 inline void setValue(QObject *v, QQmlVMEMetaObject *target, int index);
115 inline void setValue(const QVariant &);
116 inline void setValue(int);
117 inline void setValue(bool);
118 inline void setValue(double);
119 inline void setValue(const QString &);
120 inline void setValue(const QUrl &);
121 inline void setValue(const QTime &);
122 inline void setValue(const QDate &);
123 inline void setValue(const QDateTime &);
124 inline void setValue(const QRectF &);
125 inline void setValue(const QPointF &);
126 inline void setValue(const QSizeF &);
127 inline void setValue(const QJSValue &);
129 inline void setDataType(int t);
131 inline void ensureValueType(int);
135 void *data[8]; // Large enough to hold all types
137 inline void cleanup();
140 class QQmlVMEMetaObjectEndpoint : public QQmlNotifierEndpoint
143 QQmlVMEMetaObjectEndpoint();
144 static void vmecallback(QQmlNotifierEndpoint *, void **);
147 QFlagPointer<QQmlVMEMetaObject> metaObject;
151 QQmlVMEVariant::QQmlVMEVariant()
152 : type(QVariant::Invalid)
156 QQmlVMEVariant::~QQmlVMEVariant()
161 void QQmlVMEVariant::cleanup()
163 if (type == QVariant::Invalid) {
164 } else if (type == QMetaType::Int ||
165 type == QMetaType::Bool ||
166 type == QMetaType::Double) {
167 type = QVariant::Invalid;
168 } else if (type == QMetaType::QObjectStar) {
169 ((QQmlVMEVariantQObjectPtr*)dataPtr())->~QQmlVMEVariantQObjectPtr();
170 type = QVariant::Invalid;
171 } else if (type == QMetaType::QString) {
172 ((QString *)dataPtr())->~QString();
173 type = QVariant::Invalid;
174 } else if (type == QMetaType::QUrl) {
175 ((QUrl *)dataPtr())->~QUrl();
176 type = QVariant::Invalid;
177 } else if (type == QMetaType::QTime) {
178 ((QTime *)dataPtr())->~QTime();
179 type = QVariant::Invalid;
180 } else if (type == QMetaType::QDate) {
181 ((QDate *)dataPtr())->~QDate();
182 type = QVariant::Invalid;
183 } else if (type == QMetaType::QDateTime) {
184 ((QDateTime *)dataPtr())->~QDateTime();
185 type = QVariant::Invalid;
186 } else if (type == QMetaType::QRectF) {
187 ((QRectF *)dataPtr())->~QRectF();
188 type = QVariant::Invalid;
189 } else if (type == QMetaType::QPointF) {
190 ((QPointF *)dataPtr())->~QPointF();
191 type = QVariant::Invalid;
192 } else if (type == QMetaType::QSizeF) {
193 ((QSizeF *)dataPtr())->~QSizeF();
194 type = QVariant::Invalid;
195 } else if (type == qMetaTypeId<QVariant>()) {
196 ((QVariant *)dataPtr())->~QVariant();
197 type = QVariant::Invalid;
198 } else if (type == qMetaTypeId<QJSValue>()) {
199 ((QJSValue *)dataPtr())->~QJSValue();
200 type = QVariant::Invalid;
202 if (QQml_valueTypeProvider()->destroyValueType(type, dataPtr(), dataSize())) {
203 type = QVariant::Invalid;
208 int QQmlVMEVariant::dataType() const
213 const void *QQmlVMEVariant::dataPtr() const
218 void *QQmlVMEVariant::dataPtr()
223 size_t QQmlVMEVariant::dataSize() const
228 QObject *QQmlVMEVariant::asQObject()
230 if (type != QMetaType::QObjectStar)
231 setValue((QObject *)0, 0, -1);
233 return *(QQmlGuard<QObject> *)(dataPtr());
236 const QVariant &QQmlVMEVariant::asQVariant()
238 if (type != QMetaType::QVariant)
239 setValue(QVariant());
241 return *(QVariant *)(dataPtr());
244 int QQmlVMEVariant::asInt()
246 if (type != QMetaType::Int)
249 return *(int *)(dataPtr());
252 bool QQmlVMEVariant::asBool()
254 if (type != QMetaType::Bool)
255 setValue(bool(false));
257 return *(bool *)(dataPtr());
260 double QQmlVMEVariant::asDouble()
262 if (type != QMetaType::Double)
265 return *(double *)(dataPtr());
268 const QString &QQmlVMEVariant::asQString()
270 if (type != QMetaType::QString)
273 return *(QString *)(dataPtr());
276 const QUrl &QQmlVMEVariant::asQUrl()
278 if (type != QMetaType::QUrl)
281 return *(QUrl *)(dataPtr());
284 const QTime &QQmlVMEVariant::asQTime()
286 if (type != QMetaType::QTime)
289 return *(QTime *)(dataPtr());
292 const QDate &QQmlVMEVariant::asQDate()
294 if (type != QMetaType::QDate)
297 return *(QDate *)(dataPtr());
300 const QDateTime &QQmlVMEVariant::asQDateTime()
302 if (type != QMetaType::QDateTime)
303 setValue(QDateTime());
305 return *(QDateTime *)(dataPtr());
308 const QRectF &QQmlVMEVariant::asQRectF()
310 if (type != QMetaType::QRectF)
313 return *(QRectF *)(dataPtr());
316 const QSizeF &QQmlVMEVariant::asQSizeF()
318 if (type != QMetaType::QSizeF)
321 return *(QSizeF *)(dataPtr());
324 const QPointF &QQmlVMEVariant::asQPointF()
326 if (type != QMetaType::QPointF)
329 return *(QPointF *)(dataPtr());
332 const QJSValue &QQmlVMEVariant::asQJSValue()
334 if (type != qMetaTypeId<QJSValue>())
335 setValue(QJSValue());
337 return *(QJSValue *)(dataPtr());
340 void QQmlVMEVariant::setValue(QObject *v, QQmlVMEMetaObject *target, int index)
342 if (type != QMetaType::QObjectStar) {
344 type = QMetaType::QObjectStar;
345 new (dataPtr()) QQmlVMEVariantQObjectPtr(false);
347 reinterpret_cast<QQmlVMEVariantQObjectPtr*>(dataPtr())->setGuardedValue(v, target, index);
350 void QQmlVMEVariant::setValue(const QVariant &v)
352 if (type != qMetaTypeId<QVariant>()) {
354 type = qMetaTypeId<QVariant>();
355 new (dataPtr()) QVariant(v);
357 *(QVariant *)(dataPtr()) = v;
361 void QQmlVMEVariant::setValue(int v)
363 if (type != QMetaType::Int) {
365 type = QMetaType::Int;
367 *(int *)(dataPtr()) = v;
370 void QQmlVMEVariant::setValue(bool v)
372 if (type != QMetaType::Bool) {
374 type = QMetaType::Bool;
376 *(bool *)(dataPtr()) = v;
379 void QQmlVMEVariant::setValue(double v)
381 if (type != QMetaType::Double) {
383 type = QMetaType::Double;
385 *(double *)(dataPtr()) = v;
388 void QQmlVMEVariant::setValue(const QString &v)
390 if (type != QMetaType::QString) {
392 type = QMetaType::QString;
393 new (dataPtr()) QString(v);
395 *(QString *)(dataPtr()) = v;
399 void QQmlVMEVariant::setValue(const QUrl &v)
401 if (type != QMetaType::QUrl) {
403 type = QMetaType::QUrl;
404 new (dataPtr()) QUrl(v);
406 *(QUrl *)(dataPtr()) = v;
410 void QQmlVMEVariant::setValue(const QTime &v)
412 if (type != QMetaType::QTime) {
414 type = QMetaType::QTime;
415 new (dataPtr()) QTime(v);
417 *(QTime *)(dataPtr()) = v;
421 void QQmlVMEVariant::setValue(const QDate &v)
423 if (type != QMetaType::QDate) {
425 type = QMetaType::QDate;
426 new (dataPtr()) QDate(v);
428 *(QDate *)(dataPtr()) = v;
432 void QQmlVMEVariant::setValue(const QDateTime &v)
434 if (type != QMetaType::QDateTime) {
436 type = QMetaType::QDateTime;
437 new (dataPtr()) QDateTime(v);
439 *(QDateTime *)(dataPtr()) = v;
443 void QQmlVMEVariant::setValue(const QRectF &v)
445 if (type != QMetaType::QRectF) {
447 type = QMetaType::QRectF;
448 new (dataPtr()) QRectF(v);
450 *(QRectF *)(dataPtr()) = v;
454 void QQmlVMEVariant::setValue(const QPointF &v)
456 if (type != QMetaType::QPointF) {
458 type = QMetaType::QPointF;
459 new (dataPtr()) QPointF(v);
461 *(QPointF *)(dataPtr()) = v;
465 void QQmlVMEVariant::setValue(const QSizeF &v)
467 if (type != QMetaType::QSizeF) {
469 type = QMetaType::QSizeF;
470 new (dataPtr()) QSizeF(v);
472 *(QSizeF *)(dataPtr()) = v;
476 void QQmlVMEVariant::setValue(const QJSValue &v)
478 if (type != qMetaTypeId<QJSValue>()) {
480 type = qMetaTypeId<QJSValue>();
481 new (dataPtr()) QJSValue(v);
483 *(QJSValue *)(dataPtr()) = v;
487 void QQmlVMEVariant::setDataType(int t)
492 void QQmlVMEVariant::ensureValueType(int t)
497 QQml_valueTypeProvider()->initValueType(t, dataPtr(), dataSize());
501 QQmlVMEMetaObjectEndpoint::QQmlVMEMetaObjectEndpoint()
503 setCallback(QQmlNotifierEndpoint::QQmlVMEMetaObjectEndpoint);
506 void QQmlVMEMetaObjectEndpoint_callback(QQmlNotifierEndpoint *e, void **)
508 QQmlVMEMetaObjectEndpoint *vmee = static_cast<QQmlVMEMetaObjectEndpoint*>(e);
512 void QQmlVMEMetaObjectEndpoint::tryConnect()
514 int aliasId = this - metaObject->aliasEndpoints;
516 if (metaObject.flag()) {
517 // This is actually notify
518 int sigIdx = metaObject->methodOffset() + aliasId + metaObject->metaData->propertyCount;
519 metaObject->activate(metaObject->object, sigIdx, 0);
521 QQmlVMEMetaData::AliasData *d = metaObject->metaData->aliasData() + aliasId;
522 if (!d->isObjectAlias()) {
523 QQmlContextData *ctxt = metaObject->ctxt;
524 QObject *target = ctxt->idValues[d->contextIdx].data();
528 if (d->notifySignal != -1)
529 connect(target, d->notifySignal, ctxt->engine);
532 metaObject.setFlag();
536 QAbstractDynamicMetaObject *QQmlVMEMetaObject::toDynamicMetaObject(QObject *o)
538 if (!hasAssignedMetaObjectData) {
539 *static_cast<QMetaObject *>(this) = *cache->createMetaObject();
542 this->d.superdata = parent.asT1()->toDynamicMetaObject(o);
544 this->d.superdata = parent.asT2();
546 hasAssignedMetaObjectData = true;
552 QQmlVMEMetaObject::QQmlVMEMetaObject(QObject *obj,
553 QQmlPropertyCache *cache,
554 const QQmlVMEMetaData *meta)
555 : QV8GCCallback::Node(GcPrologueCallback), object(obj),
556 ctxt(QQmlData::get(obj, true)->outerContext), cache(cache), metaData(meta),
557 hasAssignedMetaObjectData(false), data(0), aliasEndpoints(0), firstVarPropertyIndex(-1),
558 varPropertiesInitialized(false), interceptors(0), v8methods(0)
560 QObjectPrivate *op = QObjectPrivate::get(obj);
562 if (op->metaObject) {
563 parent = op->metaObject;
564 // Use the extra flag in QBiPointer to know if we can safely cast parent.asT1() to QQmlVMEMetaObject*
565 parent.setFlagValue(QQmlData::get(obj)->hasVMEMetaObject);
567 parent = obj->metaObject();
569 op->metaObject = this;
570 QQmlData::get(obj)->hasVMEMetaObject = true;
572 data = new QQmlVMEVariant[metaData->propertyCount - metaData->varPropertyCount];
574 aConnected.resize(metaData->aliasCount);
575 int list_type = qMetaTypeId<QQmlListProperty<QObject> >();
576 int qobject_type = qMetaTypeId<QObject*>();
577 int variant_type = qMetaTypeId<QVariant>();
578 bool needsGcCallback = (metaData->varPropertyCount > 0);
581 for (int ii = 0; ii < metaData->propertyCount - metaData->varPropertyCount; ++ii) {
582 int t = (metaData->propertyData() + ii)->propertyType;
583 if (t == list_type) {
584 listProperties.append(List(methodOffset() + ii, this));
585 data[ii].setValue(listProperties.count() - 1);
586 } else if (!needsGcCallback && (t == qobject_type || t == variant_type)) {
587 needsGcCallback = true;
591 firstVarPropertyIndex = metaData->propertyCount - metaData->varPropertyCount;
593 // both var properties and variant properties can keep references to
594 // other QObjects, and var properties can also keep references to
595 // JavaScript objects. If we have any properties, we need to hook
596 // the gc() to ensure that references keep objects alive as needed.
597 if (needsGcCallback) {
598 QV8GCCallback::addGcCallbackNode(this);
602 QQmlVMEMetaObject::~QQmlVMEMetaObject()
604 if (parent.isT1()) parent.asT1()->objectDestroyed(object);
606 delete [] aliasEndpoints;
608 for (int ii = 0; v8methods && ii < metaData->methodCount; ++ii) {
609 qPersistentDispose(v8methods[ii]);
613 if (metaData->varPropertyCount)
614 qPersistentDispose(varProperties); // if not weak, will not have been cleaned up by the callback.
616 qDeleteAll(varObjectGuards);
619 int QQmlVMEMetaObject::metaCall(QMetaObject::Call c, int _id, void **a)
622 if (c == QMetaObject::WriteProperty && interceptors &&
623 !(*reinterpret_cast<int*>(a[3]) & QQmlPropertyPrivate::BypassInterceptor)) {
625 for (QQmlPropertyValueInterceptor *vi = interceptors; vi; vi = vi->m_next) {
626 if (vi->m_coreIndex != id)
629 int valueIndex = vi->m_valueTypeCoreIndex;
630 int type = QQmlData::get(object)->propertyCache->property(id)->propType;
632 if (type != QVariant::Invalid) {
633 if (valueIndex != -1) {
634 QQmlValueType *valueType = QQmlValueTypeFactory::valueType(type);
638 // Consider the following case:
639 // color c = { 0.1, 0.2, 0.3 }
640 // interceptor exists on c.r
641 // write { 0.2, 0.4, 0.6 }
643 // The interceptor may choose not to update the r component at this
644 // point (for example, a behavior that creates an animation). But we
645 // need to ensure that the g and b components are updated correctly.
647 // So we need to perform a full write where the value type is:
648 // r = old value, g = new value, b = new value
650 // And then call the interceptor which may or may not write the
651 // new value to the r component.
653 // This will ensure that the other components don't contain stale data
654 // and any relevant signals are emitted.
657 // (1) Store the new value type as a whole (needed due to
658 // aliasing between a[0] and static storage in value type).
659 // (2) Read the entire existing value type from object -> valueType temp.
660 // (3) Read the previous value of the component being changed
661 // from the valueType temp.
662 // (4) Write the entire new value type into the temp.
663 // (5) Overwrite the component being changed with the old value.
664 // (6) Perform a full write to the value type (which may emit signals etc).
665 // (7) Issue the interceptor call with the new component value.
668 QMetaProperty valueProp = valueType->metaObject()->property(valueIndex);
669 QVariant newValue(type, a[0]);
671 valueType->read(object, id);
672 QVariant prevComponentValue = valueProp.read(valueType);
674 valueType->setValue(newValue);
675 QVariant newComponentValue = valueProp.read(valueType);
677 // Don't apply the interceptor if the intercepted value has not changed
678 bool updated = false;
679 if (newComponentValue != prevComponentValue) {
680 valueProp.write(valueType, prevComponentValue);
681 valueType->write(object, id, QQmlPropertyPrivate::DontRemoveBinding | QQmlPropertyPrivate::BypassInterceptor);
683 vi->write(newComponentValue);
690 vi->write(QVariant(type, a[0]));
696 if (c == QMetaObject::ReadProperty || c == QMetaObject::WriteProperty || c == QMetaObject::ResetProperty) {
697 if (id >= propOffset()) {
700 if (id < metaData->propertyCount) {
701 int t = (metaData->propertyData() + id)->propertyType;
702 bool needActivate = false;
704 if (id >= firstVarPropertyIndex) {
705 Q_ASSERT(t == QMetaType::QVariant);
706 // the context can be null if accessing var properties from cpp after re-parenting an item.
707 QQmlEnginePrivate *ep = (ctxt == 0 || ctxt->engine == 0) ? 0 : QQmlEnginePrivate::get(ctxt->engine);
708 QV8Engine *v8e = (ep == 0) ? 0 : ep->v8engine();
710 v8::HandleScope handleScope;
711 v8::Context::Scope contextScope(v8e->context());
712 if (c == QMetaObject::ReadProperty) {
713 *reinterpret_cast<QVariant *>(a[0]) = readPropertyAsVariant(id);
714 } else if (c == QMetaObject::WriteProperty) {
715 writeProperty(id, *reinterpret_cast<QVariant *>(a[0]));
717 } else if (c == QMetaObject::ReadProperty) {
718 // if the context was disposed, we just return an invalid variant from read.
719 *reinterpret_cast<QVariant *>(a[0]) = QVariant();
724 if (c == QMetaObject::ReadProperty) {
727 *reinterpret_cast<int *>(a[0]) = data[id].asInt();
730 *reinterpret_cast<bool *>(a[0]) = data[id].asBool();
732 case QVariant::Double:
733 *reinterpret_cast<double *>(a[0]) = data[id].asDouble();
735 case QVariant::String:
736 *reinterpret_cast<QString *>(a[0]) = data[id].asQString();
739 *reinterpret_cast<QUrl *>(a[0]) = data[id].asQUrl();
742 *reinterpret_cast<QDate *>(a[0]) = data[id].asQDate();
744 case QVariant::DateTime:
745 *reinterpret_cast<QDateTime *>(a[0]) = data[id].asQDateTime();
747 case QVariant::RectF:
748 *reinterpret_cast<QRectF *>(a[0]) = data[id].asQRectF();
750 case QVariant::SizeF:
751 *reinterpret_cast<QSizeF *>(a[0]) = data[id].asQSizeF();
753 case QVariant::PointF:
754 *reinterpret_cast<QPointF *>(a[0]) = data[id].asQPointF();
756 case QMetaType::QObjectStar:
757 *reinterpret_cast<QObject **>(a[0]) = data[id].asQObject();
759 case QMetaType::QVariant:
760 *reinterpret_cast<QVariant *>(a[0]) = readPropertyAsVariant(id);
763 QQml_valueTypeProvider()->readValueType(data[id].dataType(), data[id].dataPtr(), data->dataSize(), t, a[0]);
766 if (t == qMetaTypeId<QQmlListProperty<QObject> >()) {
767 int listIndex = data[id].asInt();
768 const List *list = &listProperties.at(listIndex);
769 *reinterpret_cast<QQmlListProperty<QObject> *>(a[0]) =
770 QQmlListProperty<QObject>(object, (void *)list,
771 list_append, list_count, list_at,
775 } else if (c == QMetaObject::WriteProperty) {
779 needActivate = *reinterpret_cast<int *>(a[0]) != data[id].asInt();
780 data[id].setValue(*reinterpret_cast<int *>(a[0]));
783 needActivate = *reinterpret_cast<bool *>(a[0]) != data[id].asBool();
784 data[id].setValue(*reinterpret_cast<bool *>(a[0]));
786 case QVariant::Double:
787 needActivate = *reinterpret_cast<double *>(a[0]) != data[id].asDouble();
788 data[id].setValue(*reinterpret_cast<double *>(a[0]));
790 case QVariant::String:
791 needActivate = *reinterpret_cast<QString *>(a[0]) != data[id].asQString();
792 data[id].setValue(*reinterpret_cast<QString *>(a[0]));
795 needActivate = *reinterpret_cast<QUrl *>(a[0]) != data[id].asQUrl();
796 data[id].setValue(*reinterpret_cast<QUrl *>(a[0]));
799 needActivate = *reinterpret_cast<QDate *>(a[0]) != data[id].asQDate();
800 data[id].setValue(*reinterpret_cast<QDate *>(a[0]));
802 case QVariant::DateTime:
803 needActivate = *reinterpret_cast<QDateTime *>(a[0]) != data[id].asQDateTime();
804 data[id].setValue(*reinterpret_cast<QDateTime *>(a[0]));
806 case QVariant::RectF:
807 needActivate = *reinterpret_cast<QRectF *>(a[0]) != data[id].asQRectF();
808 data[id].setValue(*reinterpret_cast<QRectF *>(a[0]));
810 case QVariant::SizeF:
811 needActivate = *reinterpret_cast<QSizeF *>(a[0]) != data[id].asQSizeF();
812 data[id].setValue(*reinterpret_cast<QSizeF *>(a[0]));
814 case QVariant::PointF:
815 needActivate = *reinterpret_cast<QPointF *>(a[0]) != data[id].asQPointF();
816 data[id].setValue(*reinterpret_cast<QPointF *>(a[0]));
818 case QMetaType::QObjectStar:
819 needActivate = *reinterpret_cast<QObject **>(a[0]) != data[id].asQObject();
820 data[id].setValue(*reinterpret_cast<QObject **>(a[0]), this, id);
822 case QMetaType::QVariant:
823 writeProperty(id, *reinterpret_cast<QVariant *>(a[0]));
826 data[id].ensureValueType(t);
827 needActivate = !QQml_valueTypeProvider()->equalValueType(t, a[0], data[id].dataPtr(), data[id].dataSize());
828 QQml_valueTypeProvider()->writeValueType(t, a[0], data[id].dataPtr(), data[id].dataSize());
835 if (c == QMetaObject::WriteProperty && needActivate) {
836 activate(object, methodOffset() + id, 0);
842 id -= metaData->propertyCount;
844 if (id < metaData->aliasCount) {
846 QQmlVMEMetaData::AliasData *d = metaData->aliasData() + id;
848 if (d->flags & QML_ALIAS_FLAG_PTR && c == QMetaObject::ReadProperty)
849 *reinterpret_cast<void **>(a[0]) = 0;
851 if (!ctxt) return -1;
853 QQmlContext *context = ctxt->asQQmlContext();
854 QQmlContextPrivate *ctxtPriv = QQmlContextPrivate::get(context);
856 QObject *target = ctxtPriv->data->idValues[d->contextIdx].data();
862 if (d->isObjectAlias()) {
863 *reinterpret_cast<QObject **>(a[0]) = target;
867 // Remove binding (if any) on write
868 if(c == QMetaObject::WriteProperty) {
869 int flags = *reinterpret_cast<int*>(a[3]);
870 if (flags & QQmlPropertyPrivate::RemoveBindingOnAliasWrite) {
871 QQmlData *targetData = QQmlData::get(target);
872 if (targetData && targetData->hasBindingBit(d->propertyIndex())) {
873 QQmlAbstractBinding *binding = QQmlPropertyPrivate::setBinding(target, d->propertyIndex(), d->isValueTypeAlias()?d->valueTypeIndex():-1, 0);
874 if (binding) binding->destroy();
879 if (d->isValueTypeAlias()) {
880 // Value type property
881 QQmlValueType *valueType = QQmlValueTypeFactory::valueType(d->valueType());
884 valueType->read(target, d->propertyIndex());
885 int rv = QMetaObject::metacall(valueType, c, d->valueTypeIndex(), a);
887 if (c == QMetaObject::WriteProperty)
888 valueType->write(target, d->propertyIndex(), 0x00);
893 return QMetaObject::metacall(target, c, d->propertyIndex(), a);
901 } else if(c == QMetaObject::InvokeMetaMethod) {
903 if (id >= methodOffset()) {
905 id -= methodOffset();
906 int plainSignals = metaData->signalCount + metaData->propertyCount +
907 metaData->aliasCount;
908 if (id < plainSignals) {
909 activate(object, _id, a);
915 if (id < metaData->methodCount) {
917 return -1; // We can't run the method
919 QQmlEnginePrivate *ep = QQmlEnginePrivate::get(ctxt->engine);
920 ep->referenceScarceResources(); // "hold" scarce resources in memory during evaluation.
922 v8::Handle<v8::Function> function = method(id);
923 if (function.IsEmpty()) {
924 // The function was not compiled. There are some exceptional cases which the
925 // expression rewriter does not rewrite properly (e.g., \r-terminated lines
926 // are not rewritten correctly but this bug is deemed out-of-scope to fix for
927 // performance reasons; see QTBUG-24064) and thus compilation will have failed.
929 e.setDescription(QString(QLatin1String("Exception occurred during compilation of function: %1")).
930 arg(QLatin1String(QMetaObject::method(_id).methodSignature().constData())));
932 return -1; // The dynamic method with that id is not available.
935 QQmlVMEMetaData::MethodData *data = metaData->methodData() + id;
937 v8::HandleScope handle_scope;
938 v8::Context::Scope scope(ep->v8engine()->context());
939 v8::Handle<v8::Value> *args = 0;
941 if (data->parameterCount) {
942 args = new v8::Handle<v8::Value>[data->parameterCount];
943 for (int ii = 0; ii < data->parameterCount; ++ii)
944 args[ii] = ep->v8engine()->fromVariant(*(QVariant *)a[ii + 1]);
947 v8::TryCatch try_catch;
949 v8::Local<v8::Value> result = function->Call(ep->v8engine()->global(), data->parameterCount, args);
952 if (try_catch.HasCaught()) {
954 QQmlExpressionPrivate::exceptionToError(try_catch.Message(), error);
957 if (a[0]) *(QVariant *)a[0] = QVariant();
959 if (a[0]) *(QVariant *)a[0] = ep->v8engine()->toVariant(result, 0);
962 ep->dereferenceScarceResources(); // "release" scarce resources if top-level expression evaluation is complete.
970 return parent.asT1()->metaCall(object, c, _id, a);
972 return object->qt_metacall(c, _id, a);
975 v8::Handle<v8::Function> QQmlVMEMetaObject::method(int index)
977 if (!ctxt || !ctxt->isValid()) {
978 qWarning("QQmlVMEMetaObject: Internal error - attempted to evaluate a function in an invalid context");
979 return v8::Handle<v8::Function>();
983 v8methods = new v8::Persistent<v8::Function>[metaData->methodCount];
985 if (v8methods[index].IsEmpty()) {
986 QQmlVMEMetaData::MethodData *data = metaData->methodData() + index;
988 const char *body = ((const char*)metaData) + data->bodyOffset;
989 int bodyLength = data->bodyLength;
991 // XXX We should evaluate all methods in a single big script block to
992 // improve the call time between dynamic methods defined on the same
994 v8methods[index] = QQmlExpressionPrivate::evalFunction(ctxt, object, body,
1000 return v8methods[index];
1003 v8::Handle<v8::Value> QQmlVMEMetaObject::readVarProperty(int id)
1005 Q_ASSERT(id >= firstVarPropertyIndex);
1007 if (ensureVarPropertiesAllocated())
1008 return varProperties->Get(id - firstVarPropertyIndex);
1009 return v8::Handle<v8::Value>();
1012 QVariant QQmlVMEMetaObject::readPropertyAsVariant(int id)
1014 if (id >= firstVarPropertyIndex) {
1015 if (ensureVarPropertiesAllocated())
1016 return QQmlEnginePrivate::get(ctxt->engine)->v8engine()->toVariant(varProperties->Get(id - firstVarPropertyIndex), -1);
1019 if (data[id].dataType() == QMetaType::QObjectStar) {
1020 return QVariant::fromValue(data[id].asQObject());
1022 return data[id].asQVariant();
1027 void QQmlVMEMetaObject::writeVarProperty(int id, v8::Handle<v8::Value> value)
1029 Q_ASSERT(id >= firstVarPropertyIndex);
1030 if (!ensureVarPropertiesAllocated())
1033 // Importantly, if the current value is a scarce resource, we need to ensure that it
1034 // gets automatically released by the engine if no other references to it exist.
1035 v8::Local<v8::Value> oldv = varProperties->Get(id - firstVarPropertyIndex);
1036 if (oldv->IsObject()) {
1037 QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(oldv));
1039 r->removeVmePropertyReference();
1043 QObject *valueObject = 0;
1044 QQmlVMEVariantQObjectPtr *guard = getQObjectGuardForProperty(id);
1046 if (value->IsObject()) {
1047 // And, if the new value is a scarce resource, we need to ensure that it does not get
1048 // automatically released by the engine until no other references to it exist.
1049 if (QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(value))) {
1050 r->addVmePropertyReference();
1051 } else if (QV8QObjectResource *r = v8_resource_cast<QV8QObjectResource>(v8::Handle<v8::Object>::Cast(value))) {
1052 // We need to track this QObject to signal its deletion
1053 valueObject = r->object;
1055 // Do we already have a QObject guard for this property?
1056 if (valueObject && !guard) {
1057 guard = new QQmlVMEVariantQObjectPtr(true);
1058 varObjectGuards.append(guard);
1064 guard->setGuardedValue(valueObject, this, id);
1067 // Write the value and emit change signal as appropriate.
1068 varProperties->Set(id - firstVarPropertyIndex, value);
1069 activate(object, methodOffset() + id, 0);
1072 void QQmlVMEMetaObject::writeProperty(int id, const QVariant &value)
1074 if (id >= firstVarPropertyIndex) {
1075 if (!ensureVarPropertiesAllocated())
1078 // Importantly, if the current value is a scarce resource, we need to ensure that it
1079 // gets automatically released by the engine if no other references to it exist.
1080 v8::Local<v8::Value> oldv = varProperties->Get(id - firstVarPropertyIndex);
1081 if (oldv->IsObject()) {
1082 QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(oldv));
1084 r->removeVmePropertyReference();
1088 // And, if the new value is a scarce resource, we need to ensure that it does not get
1089 // automatically released by the engine until no other references to it exist.
1090 v8::Handle<v8::Value> newv = QQmlEnginePrivate::get(ctxt->engine)->v8engine()->fromVariant(value);
1091 if (newv->IsObject()) {
1092 QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(newv));
1094 r->addVmePropertyReference();
1098 // Write the value and emit change signal as appropriate.
1099 QVariant currentValue = readPropertyAsVariant(id);
1100 varProperties->Set(id - firstVarPropertyIndex, newv);
1101 if ((currentValue.userType() != value.userType() || currentValue != value))
1102 activate(object, methodOffset() + id, 0);
1104 bool needActivate = false;
1105 if (value.userType() == QMetaType::QObjectStar) {
1106 QObject *o = *(QObject **)value.data();
1107 needActivate = (data[id].dataType() != QMetaType::QObjectStar || data[id].asQObject() != o);
1108 data[id].setValue(o, this, id);
1110 needActivate = (data[id].dataType() != qMetaTypeId<QVariant>() ||
1111 data[id].asQVariant().userType() != value.userType() ||
1112 data[id].asQVariant() != value);
1113 data[id].setValue(value);
1117 activate(object, methodOffset() + id, 0);
1121 void QQmlVMEMetaObject::listChanged(int id)
1123 activate(object, methodOffset() + id, 0);
1126 void QQmlVMEMetaObject::list_append(QQmlListProperty<QObject> *prop, QObject *o)
1128 List *list = static_cast<List *>(prop->data);
1130 list->mo->activate(prop->object, list->notifyIndex, 0);
1133 int QQmlVMEMetaObject::list_count(QQmlListProperty<QObject> *prop)
1135 return static_cast<List *>(prop->data)->count();
1138 QObject *QQmlVMEMetaObject::list_at(QQmlListProperty<QObject> *prop, int index)
1140 return static_cast<List *>(prop->data)->at(index);
1143 void QQmlVMEMetaObject::list_clear(QQmlListProperty<QObject> *prop)
1145 List *list = static_cast<List *>(prop->data);
1147 list->mo->activate(prop->object, list->notifyIndex, 0);
1150 void QQmlVMEMetaObject::registerInterceptor(int index, int valueIndex, QQmlPropertyValueInterceptor *interceptor)
1152 interceptor->m_coreIndex = index;
1153 interceptor->m_valueTypeCoreIndex = valueIndex;
1154 interceptor->m_next = interceptors;
1155 interceptors = interceptor;
1158 quint16 QQmlVMEMetaObject::vmeMethodLineNumber(int index)
1160 if (index < methodOffset()) {
1161 Q_ASSERT(parentVMEMetaObject());
1162 return parentVMEMetaObject()->vmeMethodLineNumber(index);
1165 int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
1166 Q_ASSERT(index >= (methodOffset() + plainSignals) && index < (methodOffset() + plainSignals + metaData->methodCount));
1168 int rawIndex = index - methodOffset() - plainSignals;
1170 QQmlVMEMetaData::MethodData *data = metaData->methodData() + rawIndex;
1171 return data->lineNumber;
1174 v8::Handle<v8::Function> QQmlVMEMetaObject::vmeMethod(int index)
1176 if (index < methodOffset()) {
1177 Q_ASSERT(parentVMEMetaObject());
1178 return parentVMEMetaObject()->vmeMethod(index);
1180 int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
1181 Q_ASSERT(index >= (methodOffset() + plainSignals) && index < (methodOffset() + plainSignals + metaData->methodCount));
1182 return method(index - methodOffset() - plainSignals);
1186 void QQmlVMEMetaObject::setVmeMethod(int index, v8::Persistent<v8::Function> value)
1188 if (index < methodOffset()) {
1189 Q_ASSERT(parentVMEMetaObject());
1190 return parentVMEMetaObject()->setVmeMethod(index, value);
1192 int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
1193 Q_ASSERT(index >= (methodOffset() + plainSignals) && index < (methodOffset() + plainSignals + metaData->methodCount));
1196 v8methods = new v8::Persistent<v8::Function>[metaData->methodCount];
1198 int methodIndex = index - methodOffset() - plainSignals;
1199 if (!v8methods[methodIndex].IsEmpty())
1200 qPersistentDispose(v8methods[methodIndex]);
1201 v8methods[methodIndex] = value;
1204 v8::Handle<v8::Value> QQmlVMEMetaObject::vmeProperty(int index)
1206 if (index < propOffset()) {
1207 Q_ASSERT(parentVMEMetaObject());
1208 return parentVMEMetaObject()->vmeProperty(index);
1210 return readVarProperty(index - propOffset());
1213 void QQmlVMEMetaObject::setVMEProperty(int index, v8::Handle<v8::Value> v)
1215 if (index < propOffset()) {
1216 Q_ASSERT(parentVMEMetaObject());
1217 parentVMEMetaObject()->setVMEProperty(index, v);
1220 return writeVarProperty(index - propOffset(), v);
1223 bool QQmlVMEMetaObject::ensureVarPropertiesAllocated()
1225 if (!varPropertiesInitialized)
1226 allocateVarPropertiesArray();
1228 // in some situations, the QObject's v8object (and associated v8 data,
1229 // such as the varProperties array) will have been cleaned up, but the
1230 // QObject ptr will not yet have been deleted (eg, waiting on deleteLater).
1231 // In this situation, the varProperties handle will be (and should remain)
1233 return !varProperties.IsEmpty();
1236 // see also: QV8GCCallback::garbageCollectorPrologueCallback()
1237 void QQmlVMEMetaObject::allocateVarPropertiesArray()
1239 v8::HandleScope handleScope;
1240 v8::Context::Scope cs(QQmlEnginePrivate::get(ctxt->engine)->v8engine()->context());
1241 varProperties = qPersistentNew(v8::Array::New(metaData->varPropertyCount));
1242 varProperties.MakeWeak(static_cast<void*>(this), VarPropertiesWeakReferenceCallback);
1243 varPropertiesInitialized = true;
1247 The "var" properties are stored in a v8::Array which will be strong persistent if the object has cpp-ownership
1248 and the root QObject in the parent chain does not have JS-ownership. In the weak persistent handle case,
1249 this callback will dispose the handle when the v8object which owns the lifetime of the var properties array
1250 is cleared as a result of all other handles to that v8object being released.
1251 See QV8GCCallback::garbageCollectorPrologueCallback() for more information.
1253 void QQmlVMEMetaObject::VarPropertiesWeakReferenceCallback(v8::Persistent<v8::Value> object, void* parameter)
1255 QQmlVMEMetaObject *vmemo = static_cast<QQmlVMEMetaObject*>(parameter);
1257 qPersistentDispose(object);
1258 vmemo->varProperties.Clear();
1261 void QQmlVMEMetaObject::GcPrologueCallback(QV8GCCallback::Node *node)
1263 QQmlVMEMetaObject *vmemo = static_cast<QQmlVMEMetaObject*>(node);
1266 if (!vmemo->ctxt || !vmemo->ctxt->engine)
1268 QQmlEnginePrivate *ep = QQmlEnginePrivate::get(vmemo->ctxt->engine);
1270 // add references created by VMEVariant properties
1271 int maxDataIdx = vmemo->metaData->propertyCount - vmemo->metaData->varPropertyCount;
1272 for (int ii = 0; ii < maxDataIdx; ++ii) { // XXX TODO: optimize?
1273 if (vmemo->data[ii].dataType() == QMetaType::QObjectStar) {
1274 // possible QObject reference.
1275 QObject *ref = vmemo->data[ii].asQObject();
1277 ep->v8engine()->addRelationshipForGC(vmemo->object, ref);
1282 // add references created by var properties
1283 if (!vmemo->varPropertiesInitialized || vmemo->varProperties.IsEmpty())
1285 ep->v8engine()->addRelationshipForGC(vmemo->object, vmemo->varProperties);
1288 bool QQmlVMEMetaObject::aliasTarget(int index, QObject **target, int *coreIndex, int *valueTypeIndex) const
1290 Q_ASSERT(index >= propOffset() + metaData->propertyCount);
1294 *valueTypeIndex = -1;
1299 QQmlVMEMetaData::AliasData *d = metaData->aliasData() + (index - propOffset() - metaData->propertyCount);
1300 QQmlContext *context = ctxt->asQQmlContext();
1301 QQmlContextPrivate *ctxtPriv = QQmlContextPrivate::get(context);
1303 *target = ctxtPriv->data->idValues[d->contextIdx].data();
1307 if (d->isObjectAlias()) {
1308 } else if (d->isValueTypeAlias()) {
1309 *coreIndex = d->propertyIndex();
1310 *valueTypeIndex = d->valueTypeIndex();
1312 *coreIndex = d->propertyIndex();
1318 void QQmlVMEMetaObject::connectAlias(int aliasId)
1320 if (!aConnected.testBit(aliasId)) {
1322 if (!aliasEndpoints)
1323 aliasEndpoints = new QQmlVMEMetaObjectEndpoint[metaData->aliasCount];
1325 aConnected.setBit(aliasId);
1327 QQmlVMEMetaData::AliasData *d = metaData->aliasData() + aliasId;
1329 QQmlVMEMetaObjectEndpoint *endpoint = aliasEndpoints + aliasId;
1330 endpoint->metaObject = this;
1332 endpoint->connect(&ctxt->idValues[d->contextIdx].bindings);
1334 endpoint->tryConnect();
1338 void QQmlVMEMetaObject::connectAliasSignal(int index, bool indexInSignalRange)
1340 int aliasId = (index - (indexInSignalRange ? signalOffset() : methodOffset())) - metaData->propertyCount;
1341 if (aliasId < 0 || aliasId >= metaData->aliasCount)
1344 connectAlias(aliasId);
1348 \a index is in the method index range (QMetaMethod::methodIndex()).
1350 void QQmlVMEMetaObject::activate(QObject *object, int index, void **args)
1352 QMetaObject::activate(object, signalOffset(), index - methodOffset(), args);
1355 QQmlVMEMetaObject *QQmlVMEMetaObject::getForProperty(QObject *o, int coreIndex)
1357 QQmlVMEMetaObject *vme = QQmlVMEMetaObject::get(o);
1358 while (vme && vme->propOffset() > coreIndex)
1359 vme = vme->parentVMEMetaObject();
1365 QQmlVMEMetaObject *QQmlVMEMetaObject::getForMethod(QObject *o, int coreIndex)
1367 QQmlVMEMetaObject *vme = QQmlVMEMetaObject::get(o);
1368 while (vme && vme->methodOffset() > coreIndex)
1369 vme = vme->parentVMEMetaObject();
1376 \a coreIndex is in the signal index range (see QObjectPrivate::signalIndex()).
1377 This is different from QMetaMethod::methodIndex().
1379 QQmlVMEMetaObject *QQmlVMEMetaObject::getForSignal(QObject *o, int coreIndex)
1381 QQmlVMEMetaObject *vme = QQmlVMEMetaObject::get(o);
1382 while (vme && vme->signalOffset() > coreIndex)
1383 vme = vme->parentVMEMetaObject();
1389 QQmlVMEVariantQObjectPtr *QQmlVMEMetaObject::getQObjectGuardForProperty(int index) const
1391 QList<QQmlVMEVariantQObjectPtr *>::ConstIterator it = varObjectGuards.constBegin(), end = varObjectGuards.constEnd();
1392 for ( ; it != end; ++it) {
1393 if ((*it)->m_index == index) {