1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
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()
61 : QQmlGuard<QObject>(0), m_target(0), m_index(-1)
65 QQmlVMEVariantQObjectPtr::~QQmlVMEVariantQObjectPtr()
69 void QQmlVMEVariantQObjectPtr::objectDestroyed(QObject *)
71 if (m_target && m_index >= 0)
72 m_target->activate(m_target->object, m_target->methodOffset + m_index, 0);
75 void QQmlVMEVariantQObjectPtr::setGuardedValue(QObject *obj, QQmlVMEMetaObject *target, int index)
85 inline QQmlVMEVariant();
86 inline ~QQmlVMEVariant();
88 inline const void *dataPtr() const;
89 inline void *dataPtr();
90 inline int dataType() const;
91 inline size_t dataSize() const;
93 inline QObject *asQObject();
94 inline const QVariant &asQVariant();
97 inline double asDouble();
98 inline const QString &asQString();
99 inline const QUrl &asQUrl();
100 inline const QTime &asQTime();
101 inline const QDate &asQDate();
102 inline const QDateTime &asQDateTime();
103 inline const QJSValue &asQJSValue();
105 inline void setValue(QObject *v, QQmlVMEMetaObject *target, int index);
106 inline void setValue(const QVariant &);
107 inline void setValue(int);
108 inline void setValue(bool);
109 inline void setValue(double);
110 inline void setValue(const QString &);
111 inline void setValue(const QUrl &);
112 inline void setValue(const QTime &);
113 inline void setValue(const QDate &);
114 inline void setValue(const QDateTime &);
115 inline void setValue(const QJSValue &);
117 inline void setDataType(int t);
121 void *data[6]; // Large enough to hold all types
123 inline void cleanup();
126 class QQmlVMEMetaObjectEndpoint : public QQmlNotifierEndpoint
129 QQmlVMEMetaObjectEndpoint();
130 static void vmecallback(QQmlNotifierEndpoint *, void **);
133 QFlagPointer<QQmlVMEMetaObject> metaObject;
137 QQmlVMEVariant::QQmlVMEVariant()
138 : type(QVariant::Invalid)
142 QQmlVMEVariant::~QQmlVMEVariant()
147 void QQmlVMEVariant::cleanup()
149 if (type == QVariant::Invalid) {
150 } else if (type == QMetaType::Int ||
151 type == QMetaType::Bool ||
152 type == QMetaType::Double) {
153 type = QVariant::Invalid;
154 } else if (type == QMetaType::QObjectStar) {
155 ((QQmlVMEVariantQObjectPtr*)dataPtr())->~QQmlVMEVariantQObjectPtr();
156 type = QVariant::Invalid;
157 } else if (type == QMetaType::QString) {
158 ((QString *)dataPtr())->~QString();
159 type = QVariant::Invalid;
160 } else if (type == QMetaType::QUrl) {
161 ((QUrl *)dataPtr())->~QUrl();
162 type = QVariant::Invalid;
163 } else if (type == QMetaType::QTime) {
164 ((QTime *)dataPtr())->~QTime();
165 type = QVariant::Invalid;
166 } else if (type == QMetaType::QDate) {
167 ((QDate *)dataPtr())->~QDate();
168 type = QVariant::Invalid;
169 } else if (type == QMetaType::QDateTime) {
170 ((QDateTime *)dataPtr())->~QDateTime();
171 type = QVariant::Invalid;
172 } else if (type == qMetaTypeId<QVariant>()) {
173 ((QVariant *)dataPtr())->~QVariant();
174 type = QVariant::Invalid;
175 } else if (type == qMetaTypeId<QJSValue>()) {
176 ((QJSValue *)dataPtr())->~QJSValue();
177 type = QVariant::Invalid;
179 if (QQml_valueTypeProvider()->destroyValueType(type, dataPtr(), dataSize())) {
180 type = QVariant::Invalid;
185 int QQmlVMEVariant::dataType() const
190 const void *QQmlVMEVariant::dataPtr() const
195 void *QQmlVMEVariant::dataPtr()
200 size_t QQmlVMEVariant::dataSize() const
205 QObject *QQmlVMEVariant::asQObject()
207 if (type != QMetaType::QObjectStar)
208 setValue((QObject *)0, 0, -1);
210 return *(QQmlGuard<QObject> *)(dataPtr());
213 const QVariant &QQmlVMEVariant::asQVariant()
215 if (type != QMetaType::QVariant)
216 setValue(QVariant());
218 return *(QVariant *)(dataPtr());
221 int QQmlVMEVariant::asInt()
223 if (type != QMetaType::Int)
226 return *(int *)(dataPtr());
229 bool QQmlVMEVariant::asBool()
231 if (type != QMetaType::Bool)
232 setValue(bool(false));
234 return *(bool *)(dataPtr());
237 double QQmlVMEVariant::asDouble()
239 if (type != QMetaType::Double)
242 return *(double *)(dataPtr());
245 const QString &QQmlVMEVariant::asQString()
247 if (type != QMetaType::QString)
250 return *(QString *)(dataPtr());
253 const QUrl &QQmlVMEVariant::asQUrl()
255 if (type != QMetaType::QUrl)
258 return *(QUrl *)(dataPtr());
261 const QTime &QQmlVMEVariant::asQTime()
263 if (type != QMetaType::QTime)
266 return *(QTime *)(dataPtr());
269 const QDate &QQmlVMEVariant::asQDate()
271 if (type != QMetaType::QDate)
274 return *(QDate *)(dataPtr());
277 const QDateTime &QQmlVMEVariant::asQDateTime()
279 if (type != QMetaType::QDateTime)
280 setValue(QDateTime());
282 return *(QDateTime *)(dataPtr());
285 const QJSValue &QQmlVMEVariant::asQJSValue()
287 if (type != qMetaTypeId<QJSValue>())
288 setValue(QJSValue());
290 return *(QJSValue *)(dataPtr());
293 void QQmlVMEVariant::setValue(QObject *v, QQmlVMEMetaObject *target, int index)
295 if (type != QMetaType::QObjectStar) {
297 type = QMetaType::QObjectStar;
298 new (dataPtr()) QQmlVMEVariantQObjectPtr;
300 reinterpret_cast<QQmlVMEVariantQObjectPtr*>(dataPtr())->setGuardedValue(v, target, index);
303 void QQmlVMEVariant::setValue(const QVariant &v)
305 if (type != qMetaTypeId<QVariant>()) {
307 type = qMetaTypeId<QVariant>();
308 new (dataPtr()) QVariant(v);
310 *(QVariant *)(dataPtr()) = v;
314 void QQmlVMEVariant::setValue(int v)
316 if (type != QMetaType::Int) {
318 type = QMetaType::Int;
320 *(int *)(dataPtr()) = v;
323 void QQmlVMEVariant::setValue(bool v)
325 if (type != QMetaType::Bool) {
327 type = QMetaType::Bool;
329 *(bool *)(dataPtr()) = v;
332 void QQmlVMEVariant::setValue(double v)
334 if (type != QMetaType::Double) {
336 type = QMetaType::Double;
338 *(double *)(dataPtr()) = v;
341 void QQmlVMEVariant::setValue(const QString &v)
343 if (type != QMetaType::QString) {
345 type = QMetaType::QString;
346 new (dataPtr()) QString(v);
348 *(QString *)(dataPtr()) = v;
352 void QQmlVMEVariant::setValue(const QUrl &v)
354 if (type != QMetaType::QUrl) {
356 type = QMetaType::QUrl;
357 new (dataPtr()) QUrl(v);
359 *(QUrl *)(dataPtr()) = v;
363 void QQmlVMEVariant::setValue(const QTime &v)
365 if (type != QMetaType::QTime) {
367 type = QMetaType::QTime;
368 new (dataPtr()) QTime(v);
370 *(QTime *)(dataPtr()) = v;
374 void QQmlVMEVariant::setValue(const QDate &v)
376 if (type != QMetaType::QDate) {
378 type = QMetaType::QDate;
379 new (dataPtr()) QDate(v);
381 *(QDate *)(dataPtr()) = v;
385 void QQmlVMEVariant::setValue(const QDateTime &v)
387 if (type != QMetaType::QDateTime) {
389 type = QMetaType::QDateTime;
390 new (dataPtr()) QDateTime(v);
392 *(QDateTime *)(dataPtr()) = v;
396 void QQmlVMEVariant::setValue(const QJSValue &v)
398 if (type != qMetaTypeId<QJSValue>()) {
400 type = qMetaTypeId<QJSValue>();
401 new (dataPtr()) QJSValue(v);
403 *(QJSValue *)(dataPtr()) = v;
407 void QQmlVMEVariant::setDataType(int t)
412 QQmlVMEMetaObjectEndpoint::QQmlVMEMetaObjectEndpoint()
414 callback = &vmecallback;
417 void QQmlVMEMetaObjectEndpoint::vmecallback(QQmlNotifierEndpoint *e, void **)
419 QQmlVMEMetaObjectEndpoint *vmee = static_cast<QQmlVMEMetaObjectEndpoint*>(e);
423 void QQmlVMEMetaObjectEndpoint::tryConnect()
425 int aliasId = this - metaObject->aliasEndpoints;
427 if (metaObject.flag()) {
428 // This is actually notify
429 int sigIdx = metaObject->methodOffset + aliasId + metaObject->metaData->propertyCount;
430 QMetaObject::activate(metaObject->object, sigIdx, 0);
432 QQmlVMEMetaData::AliasData *d = metaObject->metaData->aliasData() + aliasId;
433 if (!d->isObjectAlias()) {
434 QQmlContextData *ctxt = metaObject->ctxt;
435 QObject *target = ctxt->idValues[d->contextIdx].data();
439 QMetaProperty prop = target->metaObject()->property(d->propertyIndex());
440 if (prop.hasNotifySignal())
441 connect(target, prop.notifySignalIndex());
444 metaObject.setFlag();
448 QQmlVMEMetaObject::QQmlVMEMetaObject(QObject *obj,
449 const QMetaObject *other,
450 const QQmlVMEMetaData *meta,
451 QQmlCompiledData *cdata)
452 : QV8GCCallback::Node(GcPrologueCallback), object(obj), compiledData(cdata),
453 ctxt(QQmlData::get(obj, true)->outerContext), metaData(meta), data(0),
454 aliasEndpoints(0), firstVarPropertyIndex(-1), varPropertiesInitialized(false),
455 v8methods(0), parent(0)
457 compiledData->addref();
459 *static_cast<QMetaObject *>(this) = *other;
460 this->d.superdata = obj->metaObject();
462 QObjectPrivate *op = QObjectPrivate::get(obj);
464 parent = static_cast<QAbstractDynamicMetaObject*>(op->metaObject);
465 op->metaObject = this;
467 propOffset = QAbstractDynamicMetaObject::propertyOffset();
468 methodOffset = QAbstractDynamicMetaObject::methodOffset();
470 data = new QQmlVMEVariant[metaData->propertyCount - metaData->varPropertyCount];
472 aConnected.resize(metaData->aliasCount);
473 int list_type = qMetaTypeId<QQmlListProperty<QObject> >();
476 for (int ii = 0; ii < metaData->propertyCount - metaData->varPropertyCount; ++ii) {
477 int t = (metaData->propertyData() + ii)->propertyType;
478 if (t == list_type) {
479 listProperties.append(List(methodOffset + ii));
480 data[ii].setValue(listProperties.count() - 1);
484 firstVarPropertyIndex = metaData->propertyCount - metaData->varPropertyCount;
485 if (metaData->varPropertyCount)
486 QV8GCCallback::addGcCallbackNode(this);
489 QQmlVMEMetaObject::~QQmlVMEMetaObject()
491 compiledData->release();
494 delete [] aliasEndpoints;
496 for (int ii = 0; v8methods && ii < metaData->methodCount; ++ii) {
497 qPersistentDispose(v8methods[ii]);
501 if (metaData->varPropertyCount)
502 qPersistentDispose(varProperties); // if not weak, will not have been cleaned up by the callback.
505 int QQmlVMEMetaObject::metaCall(QMetaObject::Call c, int _id, void **a)
508 if(c == QMetaObject::WriteProperty) {
509 int flags = *reinterpret_cast<int*>(a[3]);
510 if (!(flags & QQmlPropertyPrivate::BypassInterceptor)
511 && !aInterceptors.isEmpty()
512 && aInterceptors.testBit(id)) {
513 QPair<int, QQmlPropertyValueInterceptor*> pair = interceptors.value(id);
514 int valueIndex = pair.first;
515 QQmlPropertyValueInterceptor *vi = pair.second;
516 int type = property(id).userType();
518 if (type != QVariant::Invalid) {
519 if (valueIndex != -1) {
520 QQmlEnginePrivate *ep = ctxt?QQmlEnginePrivate::get(ctxt->engine):0;
521 QQmlValueType *valueType = 0;
522 if (ep) valueType = ep->valueTypes[type];
523 else valueType = QQmlValueTypeFactory::valueType(type);
526 valueType->setValue(QVariant(type, a[0]));
527 QMetaProperty valueProp = valueType->metaObject()->property(valueIndex);
528 vi->write(valueProp.read(valueType));
530 if (!ep) delete valueType;
533 vi->write(QVariant(type, a[0]));
539 if (c == QMetaObject::ReadProperty || c == QMetaObject::WriteProperty || c == QMetaObject::ResetProperty) {
540 if (id >= propOffset) {
543 if (id < metaData->propertyCount) {
544 int t = (metaData->propertyData() + id)->propertyType;
545 bool needActivate = false;
547 if (id >= firstVarPropertyIndex) {
548 Q_ASSERT(t == QMetaType::QVariant);
549 // the context can be null if accessing var properties from cpp after re-parenting an item.
550 QQmlEnginePrivate *ep = (ctxt == 0 || ctxt->engine == 0) ? 0 : QQmlEnginePrivate::get(ctxt->engine);
551 QV8Engine *v8e = (ep == 0) ? 0 : ep->v8engine();
553 v8::HandleScope handleScope;
554 v8::Context::Scope contextScope(v8e->context());
555 if (c == QMetaObject::ReadProperty) {
556 *reinterpret_cast<QVariant *>(a[0]) = readPropertyAsVariant(id);
557 } else if (c == QMetaObject::WriteProperty) {
558 writeProperty(id, *reinterpret_cast<QVariant *>(a[0]));
560 } else if (c == QMetaObject::ReadProperty) {
561 // if the context was disposed, we just return an invalid variant from read.
562 *reinterpret_cast<QVariant *>(a[0]) = QVariant();
567 if (c == QMetaObject::ReadProperty) {
570 *reinterpret_cast<int *>(a[0]) = data[id].asInt();
573 *reinterpret_cast<bool *>(a[0]) = data[id].asBool();
575 case QVariant::Double:
576 *reinterpret_cast<double *>(a[0]) = data[id].asDouble();
578 case QVariant::String:
579 *reinterpret_cast<QString *>(a[0]) = data[id].asQString();
582 *reinterpret_cast<QUrl *>(a[0]) = data[id].asQUrl();
585 *reinterpret_cast<QDate *>(a[0]) = data[id].asQDate();
587 case QVariant::DateTime:
588 *reinterpret_cast<QDateTime *>(a[0]) = data[id].asQDateTime();
590 case QMetaType::QObjectStar:
591 *reinterpret_cast<QObject **>(a[0]) = data[id].asQObject();
593 case QMetaType::QVariant:
594 *reinterpret_cast<QVariant *>(a[0]) = readPropertyAsVariant(id);
597 QQml_valueTypeProvider()->readValueType(data[id].dataType(), data[id].dataPtr(), t, a[0]);
600 if (t == qMetaTypeId<QQmlListProperty<QObject> >()) {
601 int listIndex = data[id].asInt();
602 const List *list = &listProperties.at(listIndex);
603 *reinterpret_cast<QQmlListProperty<QObject> *>(a[0]) =
604 QQmlListProperty<QObject>(object, (void *)list,
605 list_append, list_count, list_at,
609 } else if (c == QMetaObject::WriteProperty) {
613 needActivate = *reinterpret_cast<int *>(a[0]) != data[id].asInt();
614 data[id].setValue(*reinterpret_cast<int *>(a[0]));
617 needActivate = *reinterpret_cast<bool *>(a[0]) != data[id].asBool();
618 data[id].setValue(*reinterpret_cast<bool *>(a[0]));
620 case QVariant::Double:
621 needActivate = *reinterpret_cast<double *>(a[0]) != data[id].asDouble();
622 data[id].setValue(*reinterpret_cast<double *>(a[0]));
624 case QVariant::String:
625 needActivate = *reinterpret_cast<QString *>(a[0]) != data[id].asQString();
626 data[id].setValue(*reinterpret_cast<QString *>(a[0]));
629 needActivate = *reinterpret_cast<QUrl *>(a[0]) != data[id].asQUrl();
630 data[id].setValue(*reinterpret_cast<QUrl *>(a[0]));
633 needActivate = *reinterpret_cast<QDate *>(a[0]) != data[id].asQDate();
634 data[id].setValue(*reinterpret_cast<QDate *>(a[0]));
636 case QVariant::DateTime:
637 needActivate = *reinterpret_cast<QDateTime *>(a[0]) != data[id].asQDateTime();
638 data[id].setValue(*reinterpret_cast<QDateTime *>(a[0]));
640 case QMetaType::QObjectStar:
641 needActivate = *reinterpret_cast<QObject **>(a[0]) != data[id].asQObject();
642 data[id].setValue(*reinterpret_cast<QObject **>(a[0]), this, id);
644 case QMetaType::QVariant:
645 writeProperty(id, *reinterpret_cast<QVariant *>(a[0]));
648 needActivate = QQml_valueTypeProvider()->writeValueType(t, a[0], data[id].dataPtr(), data[id].dataSize());
650 data[id].setDataType(t);
658 if (c == QMetaObject::WriteProperty && needActivate) {
659 activate(object, methodOffset + id, 0);
665 id -= metaData->propertyCount;
667 if (id < metaData->aliasCount) {
669 QQmlVMEMetaData::AliasData *d = metaData->aliasData() + id;
671 if (d->flags & QML_ALIAS_FLAG_PTR && c == QMetaObject::ReadProperty)
672 *reinterpret_cast<void **>(a[0]) = 0;
674 if (!ctxt) return -1;
676 QQmlContext *context = ctxt->asQQmlContext();
677 QQmlContextPrivate *ctxtPriv = QQmlContextPrivate::get(context);
679 QObject *target = ctxtPriv->data->idValues[d->contextIdx].data();
685 if (d->isObjectAlias()) {
686 *reinterpret_cast<QObject **>(a[0]) = target;
690 // Remove binding (if any) on write
691 if(c == QMetaObject::WriteProperty) {
692 int flags = *reinterpret_cast<int*>(a[3]);
693 if (flags & QQmlPropertyPrivate::RemoveBindingOnAliasWrite) {
694 QQmlData *targetData = QQmlData::get(target);
695 if (targetData && targetData->hasBindingBit(d->propertyIndex())) {
696 QQmlAbstractBinding *binding = QQmlPropertyPrivate::setBinding(target, d->propertyIndex(), d->isValueTypeAlias()?d->valueTypeIndex():-1, 0);
697 if (binding) binding->destroy();
702 if (d->isValueTypeAlias()) {
703 // Value type property
704 QQmlEnginePrivate *ep = QQmlEnginePrivate::get(ctxt->engine);
706 QQmlValueType *valueType = ep->valueTypes[d->valueType()];
709 valueType->read(target, d->propertyIndex());
710 int rv = QMetaObject::metacall(valueType, c, d->valueTypeIndex(), a);
712 if (c == QMetaObject::WriteProperty)
713 valueType->write(target, d->propertyIndex(), 0x00);
718 return QMetaObject::metacall(target, c, d->propertyIndex(), a);
726 } else if(c == QMetaObject::InvokeMetaMethod) {
728 if (id >= methodOffset) {
731 int plainSignals = metaData->signalCount + metaData->propertyCount +
732 metaData->aliasCount;
733 if (id < plainSignals) {
734 QMetaObject::activate(object, _id, a);
740 if (id < metaData->methodCount) {
742 return -1; // We can't run the method
744 QQmlEnginePrivate *ep = QQmlEnginePrivate::get(ctxt->engine);
745 ep->referenceScarceResources(); // "hold" scarce resources in memory during evaluation.
747 v8::Handle<v8::Function> function = method(id);
748 if (function.IsEmpty()) {
749 // The function was not compiled. There are some exceptional cases which the
750 // expression rewriter does not rewrite properly (e.g., \r-terminated lines
751 // are not rewritten correctly but this bug is deemed out-of-scope to fix for
752 // performance reasons; see QTBUG-24064) and thus compilation will have failed.
754 e.setDescription(QString(QLatin1String("Exception occurred during compilation of function: %1")).
755 arg(QLatin1String(QMetaObject::method(_id).methodSignature().constData())));
757 return -1; // The dynamic method with that id is not available.
760 QQmlVMEMetaData::MethodData *data = metaData->methodData() + id;
762 v8::HandleScope handle_scope;
763 v8::Context::Scope scope(ep->v8engine()->context());
764 v8::Handle<v8::Value> *args = 0;
766 if (data->parameterCount) {
767 args = new v8::Handle<v8::Value>[data->parameterCount];
768 for (int ii = 0; ii < data->parameterCount; ++ii)
769 args[ii] = ep->v8engine()->fromVariant(*(QVariant *)a[ii + 1]);
772 v8::TryCatch try_catch;
774 v8::Local<v8::Value> result = function->Call(ep->v8engine()->global(), data->parameterCount, args);
777 if (try_catch.HasCaught()) {
779 QQmlExpressionPrivate::exceptionToError(try_catch.Message(), error);
782 if (a[0]) *(QVariant *)a[0] = QVariant();
784 if (a[0]) *(QVariant *)a[0] = ep->v8engine()->toVariant(result, 0);
787 ep->dereferenceScarceResources(); // "release" scarce resources if top-level expression evaluation is complete.
795 return parent->metaCall(c, _id, a);
797 return object->qt_metacall(c, _id, a);
800 v8::Handle<v8::Function> QQmlVMEMetaObject::method(int index)
803 v8methods = new v8::Persistent<v8::Function>[metaData->methodCount];
805 if (v8methods[index].IsEmpty()) {
806 QQmlVMEMetaData::MethodData *data = metaData->methodData() + index;
808 const char *body = ((const char*)metaData) + data->bodyOffset;
809 int bodyLength = data->bodyLength;
811 // XXX We should evaluate all methods in a single big script block to
812 // improve the call time between dynamic methods defined on the same
814 v8methods[index] = QQmlExpressionPrivate::evalFunction(ctxt, object, body,
820 return v8methods[index];
823 v8::Handle<v8::Value> QQmlVMEMetaObject::readVarProperty(int id)
825 Q_ASSERT(id >= firstVarPropertyIndex);
827 if (ensureVarPropertiesAllocated())
828 return varProperties->Get(id - firstVarPropertyIndex);
829 return v8::Handle<v8::Value>();
832 QVariant QQmlVMEMetaObject::readPropertyAsVariant(int id)
834 if (id >= firstVarPropertyIndex) {
835 if (ensureVarPropertiesAllocated())
836 return QQmlEnginePrivate::get(ctxt->engine)->v8engine()->toVariant(varProperties->Get(id - firstVarPropertyIndex), -1);
839 if (data[id].dataType() == QMetaType::QObjectStar) {
840 return QVariant::fromValue(data[id].asQObject());
842 return data[id].asQVariant();
847 void QQmlVMEMetaObject::writeVarProperty(int id, v8::Handle<v8::Value> value)
849 Q_ASSERT(id >= firstVarPropertyIndex);
850 if (!ensureVarPropertiesAllocated())
853 // Importantly, if the current value is a scarce resource, we need to ensure that it
854 // gets automatically released by the engine if no other references to it exist.
855 v8::Local<v8::Value> oldv = varProperties->Get(id - firstVarPropertyIndex);
856 if (oldv->IsObject()) {
857 QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(oldv));
859 r->removeVmePropertyReference();
863 // And, if the new value is a scarce resource, we need to ensure that it does not get
864 // automatically released by the engine until no other references to it exist.
865 if (value->IsObject()) {
866 QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(value));
868 r->addVmePropertyReference();
872 // Write the value and emit change signal as appropriate.
873 varProperties->Set(id - firstVarPropertyIndex, value);
874 activate(object, methodOffset + id, 0);
877 void QQmlVMEMetaObject::writeProperty(int id, const QVariant &value)
879 if (id >= firstVarPropertyIndex) {
880 if (!ensureVarPropertiesAllocated())
883 // Importantly, if the current value is a scarce resource, we need to ensure that it
884 // gets automatically released by the engine if no other references to it exist.
885 v8::Local<v8::Value> oldv = varProperties->Get(id - firstVarPropertyIndex);
886 if (oldv->IsObject()) {
887 QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(oldv));
889 r->removeVmePropertyReference();
893 // And, if the new value is a scarce resource, we need to ensure that it does not get
894 // automatically released by the engine until no other references to it exist.
895 v8::Handle<v8::Value> newv = QQmlEnginePrivate::get(ctxt->engine)->v8engine()->fromVariant(value);
896 if (newv->IsObject()) {
897 QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(newv));
899 r->addVmePropertyReference();
903 // Write the value and emit change signal as appropriate.
904 QVariant currentValue = readPropertyAsVariant(id);
905 varProperties->Set(id - firstVarPropertyIndex, newv);
906 if ((currentValue.userType() != value.userType() || currentValue != value))
907 activate(object, methodOffset + id, 0);
909 bool needActivate = false;
910 if (value.userType() == QMetaType::QObjectStar) {
911 QObject *o = qvariant_cast<QObject *>(value);
912 needActivate = (data[id].dataType() != QMetaType::QObjectStar || data[id].asQObject() != o);
913 data[id].setValue(qvariant_cast<QObject *>(value), this, id);
915 needActivate = (data[id].dataType() != qMetaTypeId<QVariant>() ||
916 data[id].asQVariant().userType() != value.userType() ||
917 data[id].asQVariant() != value);
918 data[id].setValue(value);
922 activate(object, methodOffset + id, 0);
926 void QQmlVMEMetaObject::listChanged(int id)
928 activate(object, methodOffset + id, 0);
931 void QQmlVMEMetaObject::list_append(QQmlListProperty<QObject> *prop, QObject *o)
933 List *list = static_cast<List *>(prop->data);
935 QMetaObject::activate(prop->object, list->notifyIndex, 0);
938 int QQmlVMEMetaObject::list_count(QQmlListProperty<QObject> *prop)
940 return static_cast<List *>(prop->data)->count();
943 QObject *QQmlVMEMetaObject::list_at(QQmlListProperty<QObject> *prop, int index)
945 return static_cast<List *>(prop->data)->at(index);
948 void QQmlVMEMetaObject::list_clear(QQmlListProperty<QObject> *prop)
950 List *list = static_cast<List *>(prop->data);
952 QMetaObject::activate(prop->object, list->notifyIndex, 0);
955 void QQmlVMEMetaObject::registerInterceptor(int index, int valueIndex, QQmlPropertyValueInterceptor *interceptor)
957 if (aInterceptors.isEmpty())
958 aInterceptors.resize(propertyCount() + metaData->propertyCount);
959 aInterceptors.setBit(index);
960 interceptors.insert(index, qMakePair(valueIndex, interceptor));
963 int QQmlVMEMetaObject::vmeMethodLineNumber(int index)
965 if (index < methodOffset) {
967 return static_cast<QQmlVMEMetaObject *>(parent)->vmeMethodLineNumber(index);
970 int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
971 Q_ASSERT(index >= (methodOffset + plainSignals) && index < (methodOffset + plainSignals + metaData->methodCount));
973 int rawIndex = index - methodOffset - plainSignals;
975 QQmlVMEMetaData::MethodData *data = metaData->methodData() + rawIndex;
976 return data->lineNumber;
979 v8::Handle<v8::Function> QQmlVMEMetaObject::vmeMethod(int index)
981 if (index < methodOffset) {
983 return static_cast<QQmlVMEMetaObject *>(parent)->vmeMethod(index);
985 int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
986 Q_ASSERT(index >= (methodOffset + plainSignals) && index < (methodOffset + plainSignals + metaData->methodCount));
987 return method(index - methodOffset - plainSignals);
991 void QQmlVMEMetaObject::setVmeMethod(int index, v8::Persistent<v8::Function> value)
993 if (index < methodOffset) {
995 return static_cast<QQmlVMEMetaObject *>(parent)->setVmeMethod(index, value);
997 int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
998 Q_ASSERT(index >= (methodOffset + plainSignals) && index < (methodOffset + plainSignals + metaData->methodCount));
1001 v8methods = new v8::Persistent<v8::Function>[metaData->methodCount];
1003 int methodIndex = index - methodOffset - plainSignals;
1004 if (!v8methods[methodIndex].IsEmpty())
1005 qPersistentDispose(v8methods[methodIndex]);
1006 v8methods[methodIndex] = value;
1009 v8::Handle<v8::Value> QQmlVMEMetaObject::vmeProperty(int index)
1011 if (index < propOffset) {
1013 return static_cast<QQmlVMEMetaObject *>(parent)->vmeProperty(index);
1015 return readVarProperty(index - propOffset);
1018 void QQmlVMEMetaObject::setVMEProperty(int index, v8::Handle<v8::Value> v)
1020 if (index < propOffset) {
1022 static_cast<QQmlVMEMetaObject *>(parent)->setVMEProperty(index, v);
1025 return writeVarProperty(index - propOffset, v);
1028 bool QQmlVMEMetaObject::ensureVarPropertiesAllocated()
1030 if (!varPropertiesInitialized)
1031 allocateVarPropertiesArray();
1033 // in some situations, the QObject's v8object (and associated v8 data,
1034 // such as the varProperties array) will have been cleaned up, but the
1035 // QObject ptr will not yet have been deleted (eg, waiting on deleteLater).
1036 // In this situation, the varProperties handle will be (and should remain)
1038 return !varProperties.IsEmpty();
1041 // see also: QV8GCCallback::garbageCollectorPrologueCallback()
1042 void QQmlVMEMetaObject::allocateVarPropertiesArray()
1044 v8::HandleScope handleScope;
1045 v8::Context::Scope cs(QQmlEnginePrivate::get(ctxt->engine)->v8engine()->context());
1046 varProperties = qPersistentNew(v8::Array::New(metaData->varPropertyCount));
1047 varProperties.MakeWeak(static_cast<void*>(this), VarPropertiesWeakReferenceCallback);
1048 varPropertiesInitialized = true;
1052 The "var" properties are stored in a v8::Array which will be strong persistent if the object has cpp-ownership
1053 and the root QObject in the parent chain does not have JS-ownership. In the weak persistent handle case,
1054 this callback will dispose the handle when the v8object which owns the lifetime of the var properties array
1055 is cleared as a result of all other handles to that v8object being released.
1056 See QV8GCCallback::garbageCollectorPrologueCallback() for more information.
1058 void QQmlVMEMetaObject::VarPropertiesWeakReferenceCallback(v8::Persistent<v8::Value> object, void* parameter)
1060 QQmlVMEMetaObject *vmemo = static_cast<QQmlVMEMetaObject*>(parameter);
1062 qPersistentDispose(object);
1063 vmemo->varProperties.Clear();
1066 void QQmlVMEMetaObject::GcPrologueCallback(QV8GCCallback::Node *node)
1068 QQmlVMEMetaObject *vmemo = static_cast<QQmlVMEMetaObject*>(node);
1070 if (!vmemo->varPropertiesInitialized || vmemo->varProperties.IsEmpty() || !vmemo->ctxt || !vmemo->ctxt->engine)
1072 QQmlEnginePrivate *ep = QQmlEnginePrivate::get(vmemo->ctxt->engine);
1073 ep->v8engine()->addRelationshipForGC(vmemo->object, vmemo->varProperties);
1076 bool QQmlVMEMetaObject::aliasTarget(int index, QObject **target, int *coreIndex, int *valueTypeIndex) const
1078 Q_ASSERT(index >= propOffset + metaData->propertyCount);
1082 *valueTypeIndex = -1;
1087 QQmlVMEMetaData::AliasData *d = metaData->aliasData() + (index - propOffset - metaData->propertyCount);
1088 QQmlContext *context = ctxt->asQQmlContext();
1089 QQmlContextPrivate *ctxtPriv = QQmlContextPrivate::get(context);
1091 *target = ctxtPriv->data->idValues[d->contextIdx].data();
1095 if (d->isObjectAlias()) {
1096 } else if (d->isValueTypeAlias()) {
1097 *coreIndex = d->propertyIndex();
1098 *valueTypeIndex = d->valueTypeIndex();
1100 *coreIndex = d->propertyIndex();
1106 void QQmlVMEMetaObject::connectAlias(int aliasId)
1108 if (!aConnected.testBit(aliasId)) {
1110 if (!aliasEndpoints)
1111 aliasEndpoints = new QQmlVMEMetaObjectEndpoint[metaData->aliasCount];
1113 aConnected.setBit(aliasId);
1115 QQmlVMEMetaData::AliasData *d = metaData->aliasData() + aliasId;
1117 QQmlVMEMetaObjectEndpoint *endpoint = aliasEndpoints + aliasId;
1118 endpoint->metaObject = this;
1120 endpoint->connect(&ctxt->idValues[d->contextIdx].bindings);
1122 endpoint->tryConnect();
1126 void QQmlVMEMetaObject::connectAliasSignal(int index)
1128 int aliasId = (index - methodOffset) - metaData->propertyCount;
1129 if (aliasId < 0 || aliasId >= metaData->aliasCount)
1132 connectAlias(aliasId);