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>
55 Q_DECLARE_METATYPE(QJSValue);
62 inline QQmlVMEVariant();
63 inline ~QQmlVMEVariant();
65 inline const void *dataPtr() const;
66 inline void *dataPtr();
67 inline int dataType() const;
69 inline QObject *asQObject();
70 inline const QVariant &asQVariant();
73 inline double asDouble();
74 inline const QString &asQString();
75 inline const QUrl &asQUrl();
76 inline const QColor &asQColor();
77 inline const QTime &asQTime();
78 inline const QDate &asQDate();
79 inline const QDateTime &asQDateTime();
80 inline const QJSValue &asQJSValue();
82 inline void setValue(QObject *);
83 inline void setValue(const QVariant &);
84 inline void setValue(int);
85 inline void setValue(bool);
86 inline void setValue(double);
87 inline void setValue(const QString &);
88 inline void setValue(const QUrl &);
89 inline void setValue(const QColor &);
90 inline void setValue(const QTime &);
91 inline void setValue(const QDate &);
92 inline void setValue(const QDateTime &);
93 inline void setValue(const QJSValue &);
96 void *data[4]; // Large enough to hold all types
98 inline void cleanup();
101 class QQmlVMEMetaObjectEndpoint : public QQmlNotifierEndpoint
104 QQmlVMEMetaObjectEndpoint();
105 static void vmecallback(QQmlNotifierEndpoint *);
108 QFlagPointer<QQmlVMEMetaObject> metaObject;
112 QQmlVMEVariant::QQmlVMEVariant()
113 : type(QVariant::Invalid)
117 QQmlVMEVariant::~QQmlVMEVariant()
122 void QQmlVMEVariant::cleanup()
124 if (type == QVariant::Invalid) {
125 } else if (type == QMetaType::Int ||
126 type == QMetaType::Bool ||
127 type == QMetaType::Double) {
128 type = QVariant::Invalid;
129 } else if (type == QMetaType::QObjectStar) {
130 ((QQmlGuard<QObject>*)dataPtr())->~QQmlGuard<QObject>();
131 type = QVariant::Invalid;
132 } else if (type == QMetaType::QString) {
133 ((QString *)dataPtr())->~QString();
134 type = QVariant::Invalid;
135 } else if (type == QMetaType::QUrl) {
136 ((QUrl *)dataPtr())->~QUrl();
137 type = QVariant::Invalid;
138 } else if (type == QMetaType::QColor) {
139 ((QColor *)dataPtr())->~QColor();
140 type = QVariant::Invalid;
141 } else if (type == QMetaType::QTime) {
142 ((QTime *)dataPtr())->~QTime();
143 type = QVariant::Invalid;
144 } else if (type == QMetaType::QDate) {
145 ((QDate *)dataPtr())->~QDate();
146 type = QVariant::Invalid;
147 } else if (type == QMetaType::QDateTime) {
148 ((QDateTime *)dataPtr())->~QDateTime();
149 type = QVariant::Invalid;
150 } else if (type == qMetaTypeId<QVariant>()) {
151 ((QVariant *)dataPtr())->~QVariant();
152 type = QVariant::Invalid;
153 } else if (type == qMetaTypeId<QJSValue>()) {
154 ((QJSValue *)dataPtr())->~QJSValue();
155 type = QVariant::Invalid;
160 int QQmlVMEVariant::dataType() const
165 const void *QQmlVMEVariant::dataPtr() const
170 void *QQmlVMEVariant::dataPtr()
175 QObject *QQmlVMEVariant::asQObject()
177 if (type != QMetaType::QObjectStar)
178 setValue((QObject *)0);
180 return *(QQmlGuard<QObject> *)(dataPtr());
183 const QVariant &QQmlVMEVariant::asQVariant()
185 if (type != QMetaType::QVariant)
186 setValue(QVariant());
188 return *(QVariant *)(dataPtr());
191 int QQmlVMEVariant::asInt()
193 if (type != QMetaType::Int)
196 return *(int *)(dataPtr());
199 bool QQmlVMEVariant::asBool()
201 if (type != QMetaType::Bool)
202 setValue(bool(false));
204 return *(bool *)(dataPtr());
207 double QQmlVMEVariant::asDouble()
209 if (type != QMetaType::Double)
212 return *(double *)(dataPtr());
215 const QString &QQmlVMEVariant::asQString()
217 if (type != QMetaType::QString)
220 return *(QString *)(dataPtr());
223 const QUrl &QQmlVMEVariant::asQUrl()
225 if (type != QMetaType::QUrl)
228 return *(QUrl *)(dataPtr());
231 const QColor &QQmlVMEVariant::asQColor()
233 if (type != QMetaType::QColor)
236 return *(QColor *)(dataPtr());
239 const QTime &QQmlVMEVariant::asQTime()
241 if (type != QMetaType::QTime)
244 return *(QTime *)(dataPtr());
247 const QDate &QQmlVMEVariant::asQDate()
249 if (type != QMetaType::QDate)
252 return *(QDate *)(dataPtr());
255 const QDateTime &QQmlVMEVariant::asQDateTime()
257 if (type != QMetaType::QDateTime)
258 setValue(QDateTime());
260 return *(QDateTime *)(dataPtr());
263 const QJSValue &QQmlVMEVariant::asQJSValue()
265 if (type != qMetaTypeId<QJSValue>())
266 setValue(QJSValue());
268 return *(QJSValue *)(dataPtr());
271 void QQmlVMEVariant::setValue(QObject *v)
273 if (type != QMetaType::QObjectStar) {
275 type = QMetaType::QObjectStar;
276 new (dataPtr()) QQmlGuard<QObject>();
278 *(QQmlGuard<QObject>*)(dataPtr()) = v;
281 void QQmlVMEVariant::setValue(const QVariant &v)
283 if (type != qMetaTypeId<QVariant>()) {
285 type = qMetaTypeId<QVariant>();
286 new (dataPtr()) QVariant(v);
288 *(QVariant *)(dataPtr()) = v;
292 void QQmlVMEVariant::setValue(int v)
294 if (type != QMetaType::Int) {
296 type = QMetaType::Int;
298 *(int *)(dataPtr()) = v;
301 void QQmlVMEVariant::setValue(bool v)
303 if (type != QMetaType::Bool) {
305 type = QMetaType::Bool;
307 *(bool *)(dataPtr()) = v;
310 void QQmlVMEVariant::setValue(double v)
312 if (type != QMetaType::Double) {
314 type = QMetaType::Double;
316 *(double *)(dataPtr()) = v;
319 void QQmlVMEVariant::setValue(const QString &v)
321 if (type != QMetaType::QString) {
323 type = QMetaType::QString;
324 new (dataPtr()) QString(v);
326 *(QString *)(dataPtr()) = v;
330 void QQmlVMEVariant::setValue(const QUrl &v)
332 if (type != QMetaType::QUrl) {
334 type = QMetaType::QUrl;
335 new (dataPtr()) QUrl(v);
337 *(QUrl *)(dataPtr()) = v;
341 void QQmlVMEVariant::setValue(const QColor &v)
343 if (type != QMetaType::QColor) {
345 type = QMetaType::QColor;
346 new (dataPtr()) QColor(v);
348 *(QColor *)(dataPtr()) = v;
352 void QQmlVMEVariant::setValue(const QTime &v)
354 if (type != QMetaType::QTime) {
356 type = QMetaType::QTime;
357 new (dataPtr()) QTime(v);
359 *(QTime *)(dataPtr()) = v;
363 void QQmlVMEVariant::setValue(const QDate &v)
365 if (type != QMetaType::QDate) {
367 type = QMetaType::QDate;
368 new (dataPtr()) QDate(v);
370 *(QDate *)(dataPtr()) = v;
374 void QQmlVMEVariant::setValue(const QDateTime &v)
376 if (type != QMetaType::QDateTime) {
378 type = QMetaType::QDateTime;
379 new (dataPtr()) QDateTime(v);
381 *(QDateTime *)(dataPtr()) = v;
385 void QQmlVMEVariant::setValue(const QJSValue &v)
387 if (type != qMetaTypeId<QJSValue>()) {
389 type = qMetaTypeId<QJSValue>();
390 new (dataPtr()) QJSValue(v);
392 *(QJSValue *)(dataPtr()) = v;
396 QQmlVMEMetaObjectEndpoint::QQmlVMEMetaObjectEndpoint()
398 callback = &vmecallback;
401 void QQmlVMEMetaObjectEndpoint::vmecallback(QQmlNotifierEndpoint *e)
403 QQmlVMEMetaObjectEndpoint *vmee = static_cast<QQmlVMEMetaObjectEndpoint*>(e);
407 void QQmlVMEMetaObjectEndpoint::tryConnect()
409 int aliasId = this - metaObject->aliasEndpoints;
411 if (metaObject.flag()) {
412 // This is actually notify
413 int sigIdx = metaObject->methodOffset + aliasId + metaObject->metaData->propertyCount;
414 QMetaObject::activate(metaObject->object, sigIdx, 0);
416 QQmlVMEMetaData::AliasData *d = metaObject->metaData->aliasData() + aliasId;
417 if (!d->isObjectAlias()) {
418 QQmlContextData *ctxt = metaObject->ctxt;
419 QObject *target = ctxt->idValues[d->contextIdx].data();
423 QMetaProperty prop = target->metaObject()->property(d->propertyIndex());
424 if (prop.hasNotifySignal())
425 connect(target, prop.notifySignalIndex());
428 metaObject.setFlag();
432 QQmlVMEMetaObject::QQmlVMEMetaObject(QObject *obj,
433 const QMetaObject *other,
434 const QQmlVMEMetaData *meta,
435 QQmlCompiledData *cdata)
436 : QV8GCCallback::Node(GcPrologueCallback), object(obj), compiledData(cdata),
437 ctxt(QQmlData::get(obj, true)->outerContext), metaData(meta), data(0),
438 aliasEndpoints(0), firstVarPropertyIndex(-1), varPropertiesInitialized(false),
439 v8methods(0), parent(0)
441 compiledData->addref();
443 *static_cast<QMetaObject *>(this) = *other;
444 this->d.superdata = obj->metaObject();
446 QObjectPrivate *op = QObjectPrivate::get(obj);
448 parent = static_cast<QAbstractDynamicMetaObject*>(op->metaObject);
449 op->metaObject = this;
451 propOffset = QAbstractDynamicMetaObject::propertyOffset();
452 methodOffset = QAbstractDynamicMetaObject::methodOffset();
454 data = new QQmlVMEVariant[metaData->propertyCount - metaData->varPropertyCount];
456 aConnected.resize(metaData->aliasCount);
457 int list_type = qMetaTypeId<QQmlListProperty<QObject> >();
460 for (int ii = 0; ii < metaData->propertyCount - metaData->varPropertyCount; ++ii) {
461 int t = (metaData->propertyData() + ii)->propertyType;
462 if (t == list_type) {
463 listProperties.append(List(methodOffset + ii));
464 data[ii].setValue(listProperties.count() - 1);
468 firstVarPropertyIndex = metaData->propertyCount - metaData->varPropertyCount;
469 if (metaData->varPropertyCount)
470 QV8GCCallback::addGcCallbackNode(this);
473 QQmlVMEMetaObject::~QQmlVMEMetaObject()
475 compiledData->release();
478 delete [] aliasEndpoints;
480 for (int ii = 0; v8methods && ii < metaData->methodCount; ++ii) {
481 qPersistentDispose(v8methods[ii]);
485 if (metaData->varPropertyCount)
486 qPersistentDispose(varProperties); // if not weak, will not have been cleaned up by the callback.
489 int QQmlVMEMetaObject::metaCall(QMetaObject::Call c, int _id, void **a)
492 if(c == QMetaObject::WriteProperty) {
493 int flags = *reinterpret_cast<int*>(a[3]);
494 if (!(flags & QQmlPropertyPrivate::BypassInterceptor)
495 && !aInterceptors.isEmpty()
496 && aInterceptors.testBit(id)) {
497 QPair<int, QQmlPropertyValueInterceptor*> pair = interceptors.value(id);
498 int valueIndex = pair.first;
499 QQmlPropertyValueInterceptor *vi = pair.second;
500 int type = property(id).userType();
502 if (type != QVariant::Invalid) {
503 if (valueIndex != -1) {
504 QQmlEnginePrivate *ep = ctxt?QQmlEnginePrivate::get(ctxt->engine):0;
505 QQmlValueType *valueType = 0;
506 if (ep) valueType = ep->valueTypes[type];
507 else valueType = QQmlValueTypeFactory::valueType(type);
510 valueType->setValue(QVariant(type, a[0]));
511 QMetaProperty valueProp = valueType->metaObject()->property(valueIndex);
512 vi->write(valueProp.read(valueType));
514 if (!ep) delete valueType;
517 vi->write(QVariant(type, a[0]));
523 if (c == QMetaObject::ReadProperty || c == QMetaObject::WriteProperty || c == QMetaObject::ResetProperty) {
524 if (id >= propOffset) {
527 if (id < metaData->propertyCount) {
528 int t = (metaData->propertyData() + id)->propertyType;
529 bool needActivate = false;
531 if (id >= firstVarPropertyIndex) {
532 Q_ASSERT(t == QMetaType::QVariant);
533 // the context can be null if accessing var properties from cpp after re-parenting an item.
534 QQmlEnginePrivate *ep = (ctxt == 0 || ctxt->engine == 0) ? 0 : QQmlEnginePrivate::get(ctxt->engine);
535 QV8Engine *v8e = (ep == 0) ? 0 : ep->v8engine();
537 v8::HandleScope handleScope;
538 v8::Context::Scope contextScope(v8e->context());
539 if (c == QMetaObject::ReadProperty) {
540 *reinterpret_cast<QVariant *>(a[0]) = readPropertyAsVariant(id);
541 } else if (c == QMetaObject::WriteProperty) {
542 writeProperty(id, *reinterpret_cast<QVariant *>(a[0]));
544 } else if (c == QMetaObject::ReadProperty) {
545 // if the context was disposed, we just return an invalid variant from read.
546 *reinterpret_cast<QVariant *>(a[0]) = QVariant();
551 if (c == QMetaObject::ReadProperty) {
554 *reinterpret_cast<int *>(a[0]) = data[id].asInt();
557 *reinterpret_cast<bool *>(a[0]) = data[id].asBool();
559 case QVariant::Double:
560 *reinterpret_cast<double *>(a[0]) = data[id].asDouble();
562 case QVariant::String:
563 *reinterpret_cast<QString *>(a[0]) = data[id].asQString();
566 *reinterpret_cast<QUrl *>(a[0]) = data[id].asQUrl();
568 case QVariant::Color:
569 *reinterpret_cast<QColor *>(a[0]) = data[id].asQColor();
572 *reinterpret_cast<QDate *>(a[0]) = data[id].asQDate();
574 case QVariant::DateTime:
575 *reinterpret_cast<QDateTime *>(a[0]) = data[id].asQDateTime();
577 case QMetaType::QObjectStar:
578 *reinterpret_cast<QObject **>(a[0]) = data[id].asQObject();
580 case QMetaType::QVariant:
581 *reinterpret_cast<QVariant *>(a[0]) = readPropertyAsVariant(id);
586 if (t == qMetaTypeId<QQmlListProperty<QObject> >()) {
587 int listIndex = data[id].asInt();
588 const List *list = &listProperties.at(listIndex);
589 *reinterpret_cast<QQmlListProperty<QObject> *>(a[0]) =
590 QQmlListProperty<QObject>(object, (void *)list,
591 list_append, list_count, list_at,
595 } else if (c == QMetaObject::WriteProperty) {
599 needActivate = *reinterpret_cast<int *>(a[0]) != data[id].asInt();
600 data[id].setValue(*reinterpret_cast<int *>(a[0]));
603 needActivate = *reinterpret_cast<bool *>(a[0]) != data[id].asBool();
604 data[id].setValue(*reinterpret_cast<bool *>(a[0]));
606 case QVariant::Double:
607 needActivate = *reinterpret_cast<double *>(a[0]) != data[id].asDouble();
608 data[id].setValue(*reinterpret_cast<double *>(a[0]));
610 case QVariant::String:
611 needActivate = *reinterpret_cast<QString *>(a[0]) != data[id].asQString();
612 data[id].setValue(*reinterpret_cast<QString *>(a[0]));
615 needActivate = *reinterpret_cast<QUrl *>(a[0]) != data[id].asQUrl();
616 data[id].setValue(*reinterpret_cast<QUrl *>(a[0]));
618 case QVariant::Color:
619 needActivate = *reinterpret_cast<QColor *>(a[0]) != data[id].asQColor();
620 data[id].setValue(*reinterpret_cast<QColor *>(a[0]));
623 needActivate = *reinterpret_cast<QDate *>(a[0]) != data[id].asQDate();
624 data[id].setValue(*reinterpret_cast<QDate *>(a[0]));
626 case QVariant::DateTime:
627 needActivate = *reinterpret_cast<QDateTime *>(a[0]) != data[id].asQDateTime();
628 data[id].setValue(*reinterpret_cast<QDateTime *>(a[0]));
630 case QMetaType::QObjectStar:
631 needActivate = *reinterpret_cast<QObject **>(a[0]) != data[id].asQObject();
632 data[id].setValue(*reinterpret_cast<QObject **>(a[0]));
634 case QMetaType::QVariant:
635 writeProperty(id, *reinterpret_cast<QVariant *>(a[0]));
644 if (c == QMetaObject::WriteProperty && needActivate) {
645 activate(object, methodOffset + id, 0);
651 id -= metaData->propertyCount;
653 if (id < metaData->aliasCount) {
655 QQmlVMEMetaData::AliasData *d = metaData->aliasData() + id;
657 if (d->flags & QML_ALIAS_FLAG_PTR && c == QMetaObject::ReadProperty)
658 *reinterpret_cast<void **>(a[0]) = 0;
660 if (!ctxt) return -1;
662 QQmlContext *context = ctxt->asQQmlContext();
663 QQmlContextPrivate *ctxtPriv = QQmlContextPrivate::get(context);
665 QObject *target = ctxtPriv->data->idValues[d->contextIdx].data();
671 if (d->isObjectAlias()) {
672 *reinterpret_cast<QObject **>(a[0]) = target;
676 // Remove binding (if any) on write
677 if(c == QMetaObject::WriteProperty) {
678 int flags = *reinterpret_cast<int*>(a[3]);
679 if (flags & QQmlPropertyPrivate::RemoveBindingOnAliasWrite) {
680 QQmlData *targetData = QQmlData::get(target);
681 if (targetData && targetData->hasBindingBit(d->propertyIndex())) {
682 QQmlAbstractBinding *binding = QQmlPropertyPrivate::setBinding(target, d->propertyIndex(), d->isValueTypeAlias()?d->valueTypeIndex():-1, 0);
683 if (binding) binding->destroy();
688 if (d->isValueTypeAlias()) {
689 // Value type property
690 QQmlEnginePrivate *ep = QQmlEnginePrivate::get(ctxt->engine);
692 QQmlValueType *valueType = ep->valueTypes[d->valueType()];
695 valueType->read(target, d->propertyIndex());
696 int rv = QMetaObject::metacall(valueType, c, d->valueTypeIndex(), a);
698 if (c == QMetaObject::WriteProperty)
699 valueType->write(target, d->propertyIndex(), 0x00);
704 return QMetaObject::metacall(target, c, d->propertyIndex(), a);
712 } else if(c == QMetaObject::InvokeMetaMethod) {
714 if (id >= methodOffset) {
717 int plainSignals = metaData->signalCount + metaData->propertyCount +
718 metaData->aliasCount;
719 if (id < plainSignals) {
720 QMetaObject::activate(object, _id, a);
726 if (id < metaData->methodCount) {
728 return -1; // We can't run the method
730 QQmlEnginePrivate *ep = QQmlEnginePrivate::get(ctxt->engine);
731 ep->referenceScarceResources(); // "hold" scarce resources in memory during evaluation.
733 v8::Handle<v8::Function> function = method(id);
734 if (function.IsEmpty()) {
735 // The function was not compiled. There are some exceptional cases which the
736 // expression rewriter does not rewrite properly (e.g., \r-terminated lines
737 // are not rewritten correctly but this bug is deemed out-of-scope to fix for
738 // performance reasons; see QTBUG-24064) and thus compilation will have failed.
740 e.setDescription(QString(QLatin1String("Exception occurred during compilation of function: %1")).
741 arg(QLatin1String(QMetaObject::method(_id).methodSignature().constData())));
743 return -1; // The dynamic method with that id is not available.
746 QQmlVMEMetaData::MethodData *data = metaData->methodData() + id;
748 v8::HandleScope handle_scope;
749 v8::Context::Scope scope(ep->v8engine()->context());
750 v8::Handle<v8::Value> *args = 0;
752 if (data->parameterCount) {
753 args = new v8::Handle<v8::Value>[data->parameterCount];
754 for (int ii = 0; ii < data->parameterCount; ++ii)
755 args[ii] = ep->v8engine()->fromVariant(*(QVariant *)a[ii + 1]);
758 v8::TryCatch try_catch;
760 v8::Local<v8::Value> result = function->Call(ep->v8engine()->global(), data->parameterCount, args);
763 if (try_catch.HasCaught()) {
765 QQmlExpressionPrivate::exceptionToError(try_catch.Message(), error);
768 if (a[0]) *(QVariant *)a[0] = QVariant();
770 if (a[0]) *(QVariant *)a[0] = ep->v8engine()->toVariant(result, 0);
773 ep->dereferenceScarceResources(); // "release" scarce resources if top-level expression evaluation is complete.
781 return parent->metaCall(c, _id, a);
783 return object->qt_metacall(c, _id, a);
786 v8::Handle<v8::Function> QQmlVMEMetaObject::method(int index)
789 v8methods = new v8::Persistent<v8::Function>[metaData->methodCount];
791 if (v8methods[index].IsEmpty()) {
792 QQmlVMEMetaData::MethodData *data = metaData->methodData() + index;
794 const char *body = ((const char*)metaData) + data->bodyOffset;
795 int bodyLength = data->bodyLength;
797 // XXX We should evaluate all methods in a single big script block to
798 // improve the call time between dynamic methods defined on the same
800 v8methods[index] = QQmlExpressionPrivate::evalFunction(ctxt, object, body,
806 return v8methods[index];
809 v8::Handle<v8::Value> QQmlVMEMetaObject::readVarProperty(int id)
811 Q_ASSERT(id >= firstVarPropertyIndex);
813 ensureVarPropertiesAllocated();
814 return varProperties->Get(id - firstVarPropertyIndex);
817 QVariant QQmlVMEMetaObject::readPropertyAsVariant(int id)
819 if (id >= firstVarPropertyIndex) {
820 ensureVarPropertiesAllocated();
821 return QQmlEnginePrivate::get(ctxt->engine)->v8engine()->toVariant(varProperties->Get(id - firstVarPropertyIndex), -1);
823 if (data[id].dataType() == QMetaType::QObjectStar) {
824 return QVariant::fromValue(data[id].asQObject());
826 return data[id].asQVariant();
831 void QQmlVMEMetaObject::writeVarProperty(int id, v8::Handle<v8::Value> value)
833 Q_ASSERT(id >= firstVarPropertyIndex);
834 ensureVarPropertiesAllocated();
836 // Importantly, if the current value is a scarce resource, we need to ensure that it
837 // gets automatically released by the engine if no other references to it exist.
838 v8::Local<v8::Value> oldv = varProperties->Get(id - firstVarPropertyIndex);
839 if (oldv->IsObject()) {
840 QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(oldv));
842 r->removeVmePropertyReference();
846 // And, if the new value is a scarce resource, we need to ensure that it does not get
847 // automatically released by the engine until no other references to it exist.
848 if (value->IsObject()) {
849 QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(value));
851 r->addVmePropertyReference();
855 // Write the value and emit change signal as appropriate.
856 varProperties->Set(id - firstVarPropertyIndex, value);
857 activate(object, methodOffset + id, 0);
860 void QQmlVMEMetaObject::writeProperty(int id, const QVariant &value)
862 if (id >= firstVarPropertyIndex) {
863 ensureVarPropertiesAllocated();
865 // Importantly, if the current value is a scarce resource, we need to ensure that it
866 // gets automatically released by the engine if no other references to it exist.
867 v8::Local<v8::Value> oldv = varProperties->Get(id - firstVarPropertyIndex);
868 if (oldv->IsObject()) {
869 QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(oldv));
871 r->removeVmePropertyReference();
875 // And, if the new value is a scarce resource, we need to ensure that it does not get
876 // automatically released by the engine until no other references to it exist.
877 v8::Handle<v8::Value> newv = QQmlEnginePrivate::get(ctxt->engine)->v8engine()->fromVariant(value);
878 if (newv->IsObject()) {
879 QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(newv));
881 r->addVmePropertyReference();
885 // Write the value and emit change signal as appropriate.
886 QVariant currentValue = readPropertyAsVariant(id);
887 varProperties->Set(id - firstVarPropertyIndex, newv);
888 if ((currentValue.userType() != value.userType() || currentValue != value))
889 activate(object, methodOffset + id, 0);
891 bool needActivate = false;
892 if (value.userType() == QMetaType::QObjectStar) {
893 QObject *o = qvariant_cast<QObject *>(value);
894 needActivate = (data[id].dataType() != QMetaType::QObjectStar || data[id].asQObject() != o);
895 data[id].setValue(qvariant_cast<QObject *>(value));
897 needActivate = (data[id].dataType() != qMetaTypeId<QVariant>() ||
898 data[id].asQVariant().userType() != value.userType() ||
899 data[id].asQVariant() != value);
900 data[id].setValue(value);
904 activate(object, methodOffset + id, 0);
908 void QQmlVMEMetaObject::listChanged(int id)
910 activate(object, methodOffset + id, 0);
913 void QQmlVMEMetaObject::list_append(QQmlListProperty<QObject> *prop, QObject *o)
915 List *list = static_cast<List *>(prop->data);
917 QMetaObject::activate(prop->object, list->notifyIndex, 0);
920 int QQmlVMEMetaObject::list_count(QQmlListProperty<QObject> *prop)
922 return static_cast<List *>(prop->data)->count();
925 QObject *QQmlVMEMetaObject::list_at(QQmlListProperty<QObject> *prop, int index)
927 return static_cast<List *>(prop->data)->at(index);
930 void QQmlVMEMetaObject::list_clear(QQmlListProperty<QObject> *prop)
932 List *list = static_cast<List *>(prop->data);
934 QMetaObject::activate(prop->object, list->notifyIndex, 0);
937 void QQmlVMEMetaObject::registerInterceptor(int index, int valueIndex, QQmlPropertyValueInterceptor *interceptor)
939 if (aInterceptors.isEmpty())
940 aInterceptors.resize(propertyCount() + metaData->propertyCount);
941 aInterceptors.setBit(index);
942 interceptors.insert(index, qMakePair(valueIndex, interceptor));
945 int QQmlVMEMetaObject::vmeMethodLineNumber(int index)
947 if (index < methodOffset) {
949 return static_cast<QQmlVMEMetaObject *>(parent)->vmeMethodLineNumber(index);
952 int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
953 Q_ASSERT(index >= (methodOffset + plainSignals) && index < (methodOffset + plainSignals + metaData->methodCount));
955 int rawIndex = index - methodOffset - plainSignals;
957 QQmlVMEMetaData::MethodData *data = metaData->methodData() + rawIndex;
958 return data->lineNumber;
961 v8::Handle<v8::Function> QQmlVMEMetaObject::vmeMethod(int index)
963 if (index < methodOffset) {
965 return static_cast<QQmlVMEMetaObject *>(parent)->vmeMethod(index);
967 int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
968 Q_ASSERT(index >= (methodOffset + plainSignals) && index < (methodOffset + plainSignals + metaData->methodCount));
969 return method(index - methodOffset - plainSignals);
973 void QQmlVMEMetaObject::setVmeMethod(int index, v8::Persistent<v8::Function> value)
975 if (index < methodOffset) {
977 return static_cast<QQmlVMEMetaObject *>(parent)->setVmeMethod(index, value);
979 int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
980 Q_ASSERT(index >= (methodOffset + plainSignals) && index < (methodOffset + plainSignals + metaData->methodCount));
983 v8methods = new v8::Persistent<v8::Function>[metaData->methodCount];
985 int methodIndex = index - methodOffset - plainSignals;
986 if (!v8methods[methodIndex].IsEmpty())
987 qPersistentDispose(v8methods[methodIndex]);
988 v8methods[methodIndex] = value;
991 v8::Handle<v8::Value> QQmlVMEMetaObject::vmeProperty(int index)
993 if (index < propOffset) {
995 return static_cast<QQmlVMEMetaObject *>(parent)->vmeProperty(index);
997 return readVarProperty(index - propOffset);
1000 void QQmlVMEMetaObject::setVMEProperty(int index, v8::Handle<v8::Value> v)
1002 if (index < propOffset) {
1004 static_cast<QQmlVMEMetaObject *>(parent)->setVMEProperty(index, v);
1007 return writeVarProperty(index - propOffset, v);
1010 void QQmlVMEMetaObject::ensureVarPropertiesAllocated()
1012 if (!varPropertiesInitialized)
1013 allocateVarPropertiesArray();
1016 // see also: QV8GCCallback::garbageCollectorPrologueCallback()
1017 void QQmlVMEMetaObject::allocateVarPropertiesArray()
1019 v8::HandleScope handleScope;
1020 v8::Context::Scope cs(QQmlEnginePrivate::get(ctxt->engine)->v8engine()->context());
1021 varProperties = qPersistentNew(v8::Array::New(metaData->varPropertyCount));
1022 varProperties.MakeWeak(static_cast<void*>(this), VarPropertiesWeakReferenceCallback);
1023 varPropertiesInitialized = true;
1027 The "var" properties are stored in a v8::Array which will be strong persistent if the object has cpp-ownership
1028 and the root QObject in the parent chain does not have JS-ownership. In the weak persistent handle case,
1029 this callback will dispose the handle when the v8object which owns the lifetime of the var properties array
1030 is cleared as a result of all other handles to that v8object being released.
1031 See QV8GCCallback::garbageCollectorPrologueCallback() for more information.
1033 void QQmlVMEMetaObject::VarPropertiesWeakReferenceCallback(v8::Persistent<v8::Value> object, void* parameter)
1035 QQmlVMEMetaObject *vmemo = static_cast<QQmlVMEMetaObject*>(parameter);
1037 qPersistentDispose(object);
1038 vmemo->varProperties.Clear();
1041 void QQmlVMEMetaObject::GcPrologueCallback(QV8GCCallback::Node *node)
1043 QQmlVMEMetaObject *vmemo = static_cast<QQmlVMEMetaObject*>(node);
1045 if (!vmemo->varPropertiesInitialized || vmemo->varProperties.IsEmpty() || !vmemo->ctxt || !vmemo->ctxt->engine)
1047 QQmlEnginePrivate *ep = QQmlEnginePrivate::get(vmemo->ctxt->engine);
1048 ep->v8engine()->addRelationshipForGC(vmemo->object, vmemo->varProperties);
1051 bool QQmlVMEMetaObject::aliasTarget(int index, QObject **target, int *coreIndex, int *valueTypeIndex) const
1053 Q_ASSERT(index >= propOffset + metaData->propertyCount);
1057 *valueTypeIndex = -1;
1062 QQmlVMEMetaData::AliasData *d = metaData->aliasData() + (index - propOffset - metaData->propertyCount);
1063 QQmlContext *context = ctxt->asQQmlContext();
1064 QQmlContextPrivate *ctxtPriv = QQmlContextPrivate::get(context);
1066 *target = ctxtPriv->data->idValues[d->contextIdx].data();
1070 if (d->isObjectAlias()) {
1071 } else if (d->isValueTypeAlias()) {
1072 *coreIndex = d->propertyIndex();
1073 *valueTypeIndex = d->valueTypeIndex();
1075 *coreIndex = d->propertyIndex();
1081 void QQmlVMEMetaObject::connectAlias(int aliasId)
1083 if (!aConnected.testBit(aliasId)) {
1085 if (!aliasEndpoints)
1086 aliasEndpoints = new QQmlVMEMetaObjectEndpoint[metaData->aliasCount];
1088 aConnected.setBit(aliasId);
1090 QQmlVMEMetaData::AliasData *d = metaData->aliasData() + aliasId;
1092 QQmlVMEMetaObjectEndpoint *endpoint = aliasEndpoints + aliasId;
1093 endpoint->metaObject = this;
1095 endpoint->connect(&ctxt->idValues[d->contextIdx].bindings);
1097 endpoint->tryConnect();
1101 void QQmlVMEMetaObject::connectAliasSignal(int index)
1103 int aliasId = (index - methodOffset) - metaData->propertyCount;
1104 if (aliasId < 0 || aliasId >= metaData->aliasCount)
1107 connectAlias(aliasId);