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 QtDeclarative 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 "qdeclarativevmemetaobject_p.h"
45 #include "qdeclarative.h"
46 #include <private/qdeclarativerefcount_p.h>
47 #include "qdeclarativeexpression.h"
48 #include "qdeclarativeexpression_p.h"
49 #include "qdeclarativecontext_p.h"
50 #include "qdeclarativebinding_p.h"
51 #include "qdeclarativepropertyvalueinterceptor_p.h"
53 #include <private/qv8variantresource_p.h>
55 Q_DECLARE_METATYPE(QJSValue);
59 class QDeclarativeVMEVariant
62 inline QDeclarativeVMEVariant();
63 inline ~QDeclarativeVMEVariant();
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 QDeclarativeVMEMetaObjectEndpoint : public QDeclarativeNotifierEndpoint
104 QDeclarativeVMEMetaObjectEndpoint();
105 static void vmecallback(QDeclarativeNotifierEndpoint *);
108 QFlagPointer<QDeclarativeVMEMetaObject> metaObject;
112 QDeclarativeVMEVariant::QDeclarativeVMEVariant()
113 : type(QVariant::Invalid)
117 QDeclarativeVMEVariant::~QDeclarativeVMEVariant()
122 void QDeclarativeVMEVariant::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 ((QDeclarativeGuard<QObject>*)dataPtr())->~QDeclarativeGuard<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 QDeclarativeVMEVariant::dataType() const
165 const void *QDeclarativeVMEVariant::dataPtr() const
170 void *QDeclarativeVMEVariant::dataPtr()
175 QObject *QDeclarativeVMEVariant::asQObject()
177 if (type != QMetaType::QObjectStar)
178 setValue((QObject *)0);
180 return *(QDeclarativeGuard<QObject> *)(dataPtr());
183 const QVariant &QDeclarativeVMEVariant::asQVariant()
185 if (type != QMetaType::QVariant)
186 setValue(QVariant());
188 return *(QVariant *)(dataPtr());
191 int QDeclarativeVMEVariant::asInt()
193 if (type != QMetaType::Int)
196 return *(int *)(dataPtr());
199 bool QDeclarativeVMEVariant::asBool()
201 if (type != QMetaType::Bool)
202 setValue(bool(false));
204 return *(bool *)(dataPtr());
207 double QDeclarativeVMEVariant::asDouble()
209 if (type != QMetaType::Double)
212 return *(double *)(dataPtr());
215 const QString &QDeclarativeVMEVariant::asQString()
217 if (type != QMetaType::QString)
220 return *(QString *)(dataPtr());
223 const QUrl &QDeclarativeVMEVariant::asQUrl()
225 if (type != QMetaType::QUrl)
228 return *(QUrl *)(dataPtr());
231 const QColor &QDeclarativeVMEVariant::asQColor()
233 if (type != QMetaType::QColor)
236 return *(QColor *)(dataPtr());
239 const QTime &QDeclarativeVMEVariant::asQTime()
241 if (type != QMetaType::QTime)
244 return *(QTime *)(dataPtr());
247 const QDate &QDeclarativeVMEVariant::asQDate()
249 if (type != QMetaType::QDate)
252 return *(QDate *)(dataPtr());
255 const QDateTime &QDeclarativeVMEVariant::asQDateTime()
257 if (type != QMetaType::QDateTime)
258 setValue(QDateTime());
260 return *(QDateTime *)(dataPtr());
263 const QJSValue &QDeclarativeVMEVariant::asQJSValue()
265 if (type != qMetaTypeId<QJSValue>())
266 setValue(QJSValue());
268 return *(QJSValue *)(dataPtr());
271 void QDeclarativeVMEVariant::setValue(QObject *v)
273 if (type != QMetaType::QObjectStar) {
275 type = QMetaType::QObjectStar;
276 new (dataPtr()) QDeclarativeGuard<QObject>();
278 *(QDeclarativeGuard<QObject>*)(dataPtr()) = v;
281 void QDeclarativeVMEVariant::setValue(const QVariant &v)
283 if (type != qMetaTypeId<QVariant>()) {
285 type = qMetaTypeId<QVariant>();
286 new (dataPtr()) QVariant(v);
288 *(QVariant *)(dataPtr()) = v;
292 void QDeclarativeVMEVariant::setValue(int v)
294 if (type != QMetaType::Int) {
296 type = QMetaType::Int;
298 *(int *)(dataPtr()) = v;
301 void QDeclarativeVMEVariant::setValue(bool v)
303 if (type != QMetaType::Bool) {
305 type = QMetaType::Bool;
307 *(bool *)(dataPtr()) = v;
310 void QDeclarativeVMEVariant::setValue(double v)
312 if (type != QMetaType::Double) {
314 type = QMetaType::Double;
316 *(double *)(dataPtr()) = v;
319 void QDeclarativeVMEVariant::setValue(const QString &v)
321 if (type != QMetaType::QString) {
323 type = QMetaType::QString;
324 new (dataPtr()) QString(v);
326 *(QString *)(dataPtr()) = v;
330 void QDeclarativeVMEVariant::setValue(const QUrl &v)
332 if (type != QMetaType::QUrl) {
334 type = QMetaType::QUrl;
335 new (dataPtr()) QUrl(v);
337 *(QUrl *)(dataPtr()) = v;
341 void QDeclarativeVMEVariant::setValue(const QColor &v)
343 if (type != QMetaType::QColor) {
345 type = QMetaType::QColor;
346 new (dataPtr()) QColor(v);
348 *(QColor *)(dataPtr()) = v;
352 void QDeclarativeVMEVariant::setValue(const QTime &v)
354 if (type != QMetaType::QTime) {
356 type = QMetaType::QTime;
357 new (dataPtr()) QTime(v);
359 *(QTime *)(dataPtr()) = v;
363 void QDeclarativeVMEVariant::setValue(const QDate &v)
365 if (type != QMetaType::QDate) {
367 type = QMetaType::QDate;
368 new (dataPtr()) QDate(v);
370 *(QDate *)(dataPtr()) = v;
374 void QDeclarativeVMEVariant::setValue(const QDateTime &v)
376 if (type != QMetaType::QDateTime) {
378 type = QMetaType::QDateTime;
379 new (dataPtr()) QDateTime(v);
381 *(QDateTime *)(dataPtr()) = v;
385 void QDeclarativeVMEVariant::setValue(const QJSValue &v)
387 if (type != qMetaTypeId<QJSValue>()) {
389 type = qMetaTypeId<QJSValue>();
390 new (dataPtr()) QJSValue(v);
392 *(QJSValue *)(dataPtr()) = v;
396 QDeclarativeVMEMetaObjectEndpoint::QDeclarativeVMEMetaObjectEndpoint()
398 callback = &vmecallback;
401 void QDeclarativeVMEMetaObjectEndpoint::vmecallback(QDeclarativeNotifierEndpoint *e)
403 QDeclarativeVMEMetaObjectEndpoint *vmee = static_cast<QDeclarativeVMEMetaObjectEndpoint*>(e);
407 void QDeclarativeVMEMetaObjectEndpoint::tryConnect()
409 if (metaObject.flag())
412 int aliasId = this - metaObject->aliasEndpoints;
414 QDeclarativeVMEMetaData::AliasData *d = metaObject->metaData->aliasData() + aliasId;
415 if (!d->isObjectAlias()) {
416 QDeclarativeContextData *ctxt = metaObject->ctxt;
417 QObject *target = ctxt->idValues[d->contextIdx].data();
421 QMetaProperty prop = target->metaObject()->property(d->propertyIndex());
422 if (prop.hasNotifySignal()) {
423 int sigIdx = metaObject->methodOffset + aliasId + metaObject->metaData->propertyCount;
424 QDeclarativePropertyPrivate::connect(target, prop.notifySignalIndex(),
425 metaObject->object, sigIdx);
429 metaObject.setFlag();
432 QDeclarativeVMEMetaObject::QDeclarativeVMEMetaObject(QObject *obj,
433 const QMetaObject *other,
434 const QDeclarativeVMEMetaData *meta,
435 QDeclarativeCompiledData *cdata)
436 : QV8GCCallback::Node(GcPrologueCallback), object(obj), compiledData(cdata),
437 ctxt(QDeclarativeData::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 QDeclarativeVMEVariant[metaData->propertyCount - metaData->varPropertyCount];
456 aConnected.resize(metaData->aliasCount);
457 int list_type = qMetaTypeId<QDeclarativeListProperty<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 QDeclarativeVMEMetaObject::~QDeclarativeVMEMetaObject()
475 compiledData->release();
478 delete [] aliasEndpoints;
480 for (int ii = 0; v8methods && ii < metaData->methodCount; ++ii) {
481 qPersistentDispose(v8methods[ii]);
484 if (metaData->varPropertyCount)
485 qPersistentDispose(varProperties); // if not weak, will not have been cleaned up by the callback.
488 int QDeclarativeVMEMetaObject::metaCall(QMetaObject::Call c, int _id, void **a)
491 if(c == QMetaObject::WriteProperty) {
492 int flags = *reinterpret_cast<int*>(a[3]);
493 if (!(flags & QDeclarativePropertyPrivate::BypassInterceptor)
494 && !aInterceptors.isEmpty()
495 && aInterceptors.testBit(id)) {
496 QPair<int, QDeclarativePropertyValueInterceptor*> pair = interceptors.value(id);
497 int valueIndex = pair.first;
498 QDeclarativePropertyValueInterceptor *vi = pair.second;
499 int type = property(id).userType();
501 if (type != QVariant::Invalid) {
502 if (valueIndex != -1) {
503 QDeclarativeEnginePrivate *ep = ctxt?QDeclarativeEnginePrivate::get(ctxt->engine):0;
504 QDeclarativeValueType *valueType = 0;
505 if (ep) valueType = ep->valueTypes[type];
506 else valueType = QDeclarativeValueTypeFactory::valueType(type);
509 valueType->setValue(QVariant(type, a[0]));
510 QMetaProperty valueProp = valueType->metaObject()->property(valueIndex);
511 vi->write(valueProp.read(valueType));
513 if (!ep) delete valueType;
516 vi->write(QVariant(type, a[0]));
522 if (c == QMetaObject::ReadProperty || c == QMetaObject::WriteProperty || c == QMetaObject::ResetProperty) {
523 if (id >= propOffset) {
526 if (id < metaData->propertyCount) {
527 int t = (metaData->propertyData() + id)->propertyType;
528 bool needActivate = false;
530 if (id >= firstVarPropertyIndex) {
531 Q_ASSERT(t == QMetaType::QVariant);
532 // the context can be null if accessing var properties from cpp after re-parenting an item.
533 QDeclarativeEnginePrivate *ep = (ctxt == 0 || ctxt->engine == 0) ? 0 : QDeclarativeEnginePrivate::get(ctxt->engine);
534 QV8Engine *v8e = (ep == 0) ? 0 : ep->v8engine();
536 v8::HandleScope handleScope;
537 v8::Context::Scope contextScope(v8e->context());
538 if (c == QMetaObject::ReadProperty) {
539 *reinterpret_cast<QVariant *>(a[0]) = readPropertyAsVariant(id);
540 } else if (c == QMetaObject::WriteProperty) {
541 writeProperty(id, *reinterpret_cast<QVariant *>(a[0]));
543 } else if (c == QMetaObject::ReadProperty) {
544 // if the context was disposed, we just return an invalid variant from read.
545 *reinterpret_cast<QVariant *>(a[0]) = QVariant();
550 if (c == QMetaObject::ReadProperty) {
553 *reinterpret_cast<int *>(a[0]) = data[id].asInt();
556 *reinterpret_cast<bool *>(a[0]) = data[id].asBool();
558 case QVariant::Double:
559 *reinterpret_cast<double *>(a[0]) = data[id].asDouble();
561 case QVariant::String:
562 *reinterpret_cast<QString *>(a[0]) = data[id].asQString();
565 *reinterpret_cast<QUrl *>(a[0]) = data[id].asQUrl();
567 case QVariant::Color:
568 *reinterpret_cast<QColor *>(a[0]) = data[id].asQColor();
571 *reinterpret_cast<QDate *>(a[0]) = data[id].asQDate();
573 case QVariant::DateTime:
574 *reinterpret_cast<QDateTime *>(a[0]) = data[id].asQDateTime();
576 case QMetaType::QObjectStar:
577 *reinterpret_cast<QObject **>(a[0]) = data[id].asQObject();
579 case QMetaType::QVariant:
580 *reinterpret_cast<QVariant *>(a[0]) = readPropertyAsVariant(id);
585 if (t == qMetaTypeId<QDeclarativeListProperty<QObject> >()) {
586 int listIndex = data[id].asInt();
587 const List *list = &listProperties.at(listIndex);
588 *reinterpret_cast<QDeclarativeListProperty<QObject> *>(a[0]) =
589 QDeclarativeListProperty<QObject>(object, (void *)list,
590 list_append, list_count, list_at,
594 } else if (c == QMetaObject::WriteProperty) {
598 needActivate = *reinterpret_cast<int *>(a[0]) != data[id].asInt();
599 data[id].setValue(*reinterpret_cast<int *>(a[0]));
602 needActivate = *reinterpret_cast<bool *>(a[0]) != data[id].asBool();
603 data[id].setValue(*reinterpret_cast<bool *>(a[0]));
605 case QVariant::Double:
606 needActivate = *reinterpret_cast<double *>(a[0]) != data[id].asDouble();
607 data[id].setValue(*reinterpret_cast<double *>(a[0]));
609 case QVariant::String:
610 needActivate = *reinterpret_cast<QString *>(a[0]) != data[id].asQString();
611 data[id].setValue(*reinterpret_cast<QString *>(a[0]));
614 needActivate = *reinterpret_cast<QUrl *>(a[0]) != data[id].asQUrl();
615 data[id].setValue(*reinterpret_cast<QUrl *>(a[0]));
617 case QVariant::Color:
618 needActivate = *reinterpret_cast<QColor *>(a[0]) != data[id].asQColor();
619 data[id].setValue(*reinterpret_cast<QColor *>(a[0]));
622 needActivate = *reinterpret_cast<QDate *>(a[0]) != data[id].asQDate();
623 data[id].setValue(*reinterpret_cast<QDate *>(a[0]));
625 case QVariant::DateTime:
626 needActivate = *reinterpret_cast<QDateTime *>(a[0]) != data[id].asQDateTime();
627 data[id].setValue(*reinterpret_cast<QDateTime *>(a[0]));
629 case QMetaType::QObjectStar:
630 needActivate = *reinterpret_cast<QObject **>(a[0]) != data[id].asQObject();
631 data[id].setValue(*reinterpret_cast<QObject **>(a[0]));
633 case QMetaType::QVariant:
634 writeProperty(id, *reinterpret_cast<QVariant *>(a[0]));
643 if (c == QMetaObject::WriteProperty && needActivate) {
644 activate(object, methodOffset + id, 0);
650 id -= metaData->propertyCount;
652 if (id < metaData->aliasCount) {
654 QDeclarativeVMEMetaData::AliasData *d = metaData->aliasData() + id;
656 if (d->flags & QML_ALIAS_FLAG_PTR && c == QMetaObject::ReadProperty)
657 *reinterpret_cast<void **>(a[0]) = 0;
659 if (!ctxt) return -1;
661 QDeclarativeContext *context = ctxt->asQDeclarativeContext();
662 QDeclarativeContextPrivate *ctxtPriv = QDeclarativeContextPrivate::get(context);
664 QObject *target = ctxtPriv->data->idValues[d->contextIdx].data();
670 if (d->isObjectAlias()) {
671 *reinterpret_cast<QObject **>(a[0]) = target;
675 // Remove binding (if any) on write
676 if(c == QMetaObject::WriteProperty) {
677 int flags = *reinterpret_cast<int*>(a[3]);
678 if (flags & QDeclarativePropertyPrivate::RemoveBindingOnAliasWrite) {
679 QDeclarativeData *targetData = QDeclarativeData::get(target);
680 if (targetData && targetData->hasBindingBit(d->propertyIndex())) {
681 QDeclarativeAbstractBinding *binding = QDeclarativePropertyPrivate::setBinding(target, d->propertyIndex(), d->isValueTypeAlias()?d->valueTypeIndex():-1, 0);
682 if (binding) binding->destroy();
687 if (d->isValueTypeAlias()) {
688 // Value type property
689 QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(ctxt->engine);
691 QDeclarativeValueType *valueType = ep->valueTypes[d->valueType()];
694 valueType->read(target, d->propertyIndex());
695 int rv = QMetaObject::metacall(valueType, c, d->valueTypeIndex(), a);
697 if (c == QMetaObject::WriteProperty)
698 valueType->write(target, d->propertyIndex(), 0x00);
703 return QMetaObject::metacall(target, c, d->propertyIndex(), a);
711 } else if(c == QMetaObject::InvokeMetaMethod) {
713 if (id >= methodOffset) {
716 int plainSignals = metaData->signalCount + metaData->propertyCount +
717 metaData->aliasCount;
718 if (id < plainSignals) {
719 QMetaObject::activate(object, _id, a);
725 if (id < metaData->methodCount) {
727 return -1; // We can't run the method
729 QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(ctxt->engine);
730 ep->referenceScarceResources(); // "hold" scarce resources in memory during evaluation.
732 v8::Handle<v8::Function> function = method(id);
733 if (function.IsEmpty()) {
734 // The function was not compiled. There are some exceptional cases which the
735 // expression rewriter does not rewrite properly (e.g., \r-terminated lines
736 // are not rewritten correctly but this bug is deemed out-of-scope to fix for
737 // performance reasons; see QTBUG-24064) and thus compilation will have failed.
739 e.setDescription(QString(QLatin1String("Exception occurred during compilation of function: %1")).arg(QMetaObject::method(_id).signature()));
741 return -1; // The dynamic method with that id is not available.
744 QDeclarativeVMEMetaData::MethodData *data = metaData->methodData() + id;
746 v8::HandleScope handle_scope;
747 v8::Context::Scope scope(ep->v8engine()->context());
748 v8::Handle<v8::Value> *args = 0;
750 if (data->parameterCount) {
751 args = new v8::Handle<v8::Value>[data->parameterCount];
752 for (int ii = 0; ii < data->parameterCount; ++ii)
753 args[ii] = ep->v8engine()->fromVariant(*(QVariant *)a[ii + 1]);
756 v8::TryCatch try_catch;
758 v8::Local<v8::Value> result = function->Call(ep->v8engine()->global(), data->parameterCount, args);
761 if (try_catch.HasCaught()) {
762 QDeclarativeError error;
763 QDeclarativeExpressionPrivate::exceptionToError(try_catch.Message(), error);
766 if (a[0]) *(QVariant *)a[0] = QVariant();
768 if (a[0]) *(QVariant *)a[0] = ep->v8engine()->toVariant(result, 0);
771 ep->dereferenceScarceResources(); // "release" scarce resources if top-level expression evaluation is complete.
779 return parent->metaCall(c, _id, a);
781 return object->qt_metacall(c, _id, a);
784 v8::Handle<v8::Function> QDeclarativeVMEMetaObject::method(int index)
787 v8methods = new v8::Persistent<v8::Function>[metaData->methodCount];
789 if (v8methods[index].IsEmpty()) {
790 QDeclarativeVMEMetaData::MethodData *data = metaData->methodData() + index;
793 (const QChar *)(((const char*)metaData) + data->bodyOffset);
795 QString code = QString::fromRawData(body, 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] = QDeclarativeExpressionPrivate::evalFunction(ctxt, object, code, ctxt->url.toString(),
804 return v8methods[index];
807 v8::Handle<v8::Value> QDeclarativeVMEMetaObject::readVarProperty(int id)
809 Q_ASSERT(id >= firstVarPropertyIndex);
811 ensureVarPropertiesAllocated();
812 return varProperties->Get(id - firstVarPropertyIndex);
815 QVariant QDeclarativeVMEMetaObject::readPropertyAsVariant(int id)
817 if (id >= firstVarPropertyIndex) {
818 ensureVarPropertiesAllocated();
819 return QDeclarativeEnginePrivate::get(ctxt->engine)->v8engine()->toVariant(varProperties->Get(id - firstVarPropertyIndex), -1);
821 if (data[id].dataType() == QMetaType::QObjectStar) {
822 return QVariant::fromValue(data[id].asQObject());
824 return data[id].asQVariant();
829 void QDeclarativeVMEMetaObject::writeVarProperty(int id, v8::Handle<v8::Value> value)
831 Q_ASSERT(id >= firstVarPropertyIndex);
832 ensureVarPropertiesAllocated();
834 // Importantly, if the current value is a scarce resource, we need to ensure that it
835 // gets automatically released by the engine if no other references to it exist.
836 v8::Local<v8::Value> oldv = varProperties->Get(id - firstVarPropertyIndex);
837 if (oldv->IsObject()) {
838 QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(oldv));
840 r->removeVmePropertyReference();
844 // And, if the new value is a scarce resource, we need to ensure that it does not get
845 // automatically released by the engine until no other references to it exist.
846 if (value->IsObject()) {
847 QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(value));
849 r->addVmePropertyReference();
853 // Write the value and emit change signal as appropriate.
854 varProperties->Set(id - firstVarPropertyIndex, value);
855 activate(object, methodOffset + id, 0);
858 void QDeclarativeVMEMetaObject::writeProperty(int id, const QVariant &value)
860 if (id >= firstVarPropertyIndex) {
861 ensureVarPropertiesAllocated();
863 // Importantly, if the current value is a scarce resource, we need to ensure that it
864 // gets automatically released by the engine if no other references to it exist.
865 v8::Local<v8::Value> oldv = varProperties->Get(id - firstVarPropertyIndex);
866 if (oldv->IsObject()) {
867 QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(oldv));
869 r->removeVmePropertyReference();
873 // And, if the new value is a scarce resource, we need to ensure that it does not get
874 // automatically released by the engine until no other references to it exist.
875 v8::Handle<v8::Value> newv = QDeclarativeEnginePrivate::get(ctxt->engine)->v8engine()->fromVariant(value);
876 if (newv->IsObject()) {
877 QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(newv));
879 r->addVmePropertyReference();
883 // Write the value and emit change signal as appropriate.
884 QVariant currentValue = readPropertyAsVariant(id);
885 varProperties->Set(id - firstVarPropertyIndex, newv);
886 if ((currentValue.userType() != value.userType() || currentValue != value))
887 activate(object, methodOffset + id, 0);
889 bool needActivate = false;
890 if (value.userType() == QMetaType::QObjectStar) {
891 QObject *o = qvariant_cast<QObject *>(value);
892 needActivate = (data[id].dataType() != QMetaType::QObjectStar || data[id].asQObject() != o);
893 data[id].setValue(qvariant_cast<QObject *>(value));
895 needActivate = (data[id].dataType() != qMetaTypeId<QVariant>() ||
896 data[id].asQVariant().userType() != value.userType() ||
897 data[id].asQVariant() != value);
898 data[id].setValue(value);
902 activate(object, methodOffset + id, 0);
906 void QDeclarativeVMEMetaObject::listChanged(int id)
908 activate(object, methodOffset + id, 0);
911 void QDeclarativeVMEMetaObject::list_append(QDeclarativeListProperty<QObject> *prop, QObject *o)
913 List *list = static_cast<List *>(prop->data);
915 QMetaObject::activate(prop->object, list->notifyIndex, 0);
918 int QDeclarativeVMEMetaObject::list_count(QDeclarativeListProperty<QObject> *prop)
920 return static_cast<List *>(prop->data)->count();
923 QObject *QDeclarativeVMEMetaObject::list_at(QDeclarativeListProperty<QObject> *prop, int index)
925 return static_cast<List *>(prop->data)->at(index);
928 void QDeclarativeVMEMetaObject::list_clear(QDeclarativeListProperty<QObject> *prop)
930 List *list = static_cast<List *>(prop->data);
932 QMetaObject::activate(prop->object, list->notifyIndex, 0);
935 void QDeclarativeVMEMetaObject::registerInterceptor(int index, int valueIndex, QDeclarativePropertyValueInterceptor *interceptor)
937 if (aInterceptors.isEmpty())
938 aInterceptors.resize(propertyCount() + metaData->propertyCount);
939 aInterceptors.setBit(index);
940 interceptors.insert(index, qMakePair(valueIndex, interceptor));
943 int QDeclarativeVMEMetaObject::vmeMethodLineNumber(int index)
945 if (index < methodOffset) {
947 return static_cast<QDeclarativeVMEMetaObject *>(parent)->vmeMethodLineNumber(index);
950 int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
951 Q_ASSERT(index >= (methodOffset + plainSignals) && index < (methodOffset + plainSignals + metaData->methodCount));
953 int rawIndex = index - methodOffset - plainSignals;
955 QDeclarativeVMEMetaData::MethodData *data = metaData->methodData() + rawIndex;
956 return data->lineNumber;
959 v8::Handle<v8::Function> QDeclarativeVMEMetaObject::vmeMethod(int index)
961 if (index < methodOffset) {
963 return static_cast<QDeclarativeVMEMetaObject *>(parent)->vmeMethod(index);
965 int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
966 Q_ASSERT(index >= (methodOffset + plainSignals) && index < (methodOffset + plainSignals + metaData->methodCount));
967 return method(index - methodOffset - plainSignals);
971 void QDeclarativeVMEMetaObject::setVmeMethod(int index, v8::Persistent<v8::Function> value)
973 if (index < methodOffset) {
975 return static_cast<QDeclarativeVMEMetaObject *>(parent)->setVmeMethod(index, value);
977 int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
978 Q_ASSERT(index >= (methodOffset + plainSignals) && index < (methodOffset + plainSignals + metaData->methodCount));
981 v8methods = new v8::Persistent<v8::Function>[metaData->methodCount];
983 int methodIndex = index - methodOffset - plainSignals;
984 if (!v8methods[methodIndex].IsEmpty())
985 qPersistentDispose(v8methods[methodIndex]);
986 v8methods[methodIndex] = value;
989 v8::Handle<v8::Value> QDeclarativeVMEMetaObject::vmeProperty(int index)
991 if (index < propOffset) {
993 return static_cast<QDeclarativeVMEMetaObject *>(parent)->vmeProperty(index);
995 return readVarProperty(index - propOffset);
998 void QDeclarativeVMEMetaObject::setVMEProperty(int index, v8::Handle<v8::Value> v)
1000 if (index < propOffset) {
1002 static_cast<QDeclarativeVMEMetaObject *>(parent)->setVMEProperty(index, v);
1005 return writeVarProperty(index - propOffset, v);
1008 void QDeclarativeVMEMetaObject::ensureVarPropertiesAllocated()
1010 if (!varPropertiesInitialized)
1011 allocateVarPropertiesArray();
1014 // see also: QV8GCCallback::garbageCollectorPrologueCallback()
1015 void QDeclarativeVMEMetaObject::allocateVarPropertiesArray()
1017 v8::HandleScope handleScope;
1018 v8::Context::Scope cs(QDeclarativeEnginePrivate::get(ctxt->engine)->v8engine()->context());
1019 varProperties = qPersistentNew(v8::Array::New(metaData->varPropertyCount));
1020 varProperties.MakeWeak(static_cast<void*>(this), VarPropertiesWeakReferenceCallback);
1021 varPropertiesInitialized = true;
1025 The "var" properties are stored in a v8::Array which will be strong persistent if the object has cpp-ownership
1026 and the root QObject in the parent chain does not have JS-ownership. In the weak persistent handle case,
1027 this callback will dispose the handle when the v8object which owns the lifetime of the var properties array
1028 is cleared as a result of all other handles to that v8object being released.
1029 See QV8GCCallback::garbageCollectorPrologueCallback() for more information.
1031 void QDeclarativeVMEMetaObject::VarPropertiesWeakReferenceCallback(v8::Persistent<v8::Value> object, void* parameter)
1033 QDeclarativeVMEMetaObject *vmemo = static_cast<QDeclarativeVMEMetaObject*>(parameter);
1035 qPersistentDispose(object);
1036 vmemo->varProperties.Clear();
1039 void QDeclarativeVMEMetaObject::GcPrologueCallback(QV8GCCallback::Node *node)
1041 QDeclarativeVMEMetaObject *vmemo = static_cast<QDeclarativeVMEMetaObject*>(node);
1043 if (!vmemo->varPropertiesInitialized || vmemo->varProperties.IsEmpty() || !vmemo->ctxt || !vmemo->ctxt->engine)
1045 QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(vmemo->ctxt->engine);
1046 ep->v8engine()->addRelationshipForGC(vmemo->object, vmemo->varProperties);
1049 bool QDeclarativeVMEMetaObject::aliasTarget(int index, QObject **target, int *coreIndex, int *valueTypeIndex) const
1051 Q_ASSERT(index >= propOffset + metaData->propertyCount);
1055 *valueTypeIndex = -1;
1060 QDeclarativeVMEMetaData::AliasData *d = metaData->aliasData() + (index - propOffset - metaData->propertyCount);
1061 QDeclarativeContext *context = ctxt->asQDeclarativeContext();
1062 QDeclarativeContextPrivate *ctxtPriv = QDeclarativeContextPrivate::get(context);
1064 *target = ctxtPriv->data->idValues[d->contextIdx].data();
1068 if (d->isObjectAlias()) {
1069 } else if (d->isValueTypeAlias()) {
1070 *coreIndex = d->propertyIndex();
1071 *valueTypeIndex = d->valueTypeIndex();
1073 *coreIndex = d->propertyIndex();
1079 void QDeclarativeVMEMetaObject::connectAlias(int aliasId)
1081 if (!aConnected.testBit(aliasId)) {
1083 if (!aliasEndpoints)
1084 aliasEndpoints = new QDeclarativeVMEMetaObjectEndpoint[metaData->aliasCount];
1086 aConnected.setBit(aliasId);
1088 QDeclarativeVMEMetaData::AliasData *d = metaData->aliasData() + aliasId;
1090 QDeclarativeVMEMetaObjectEndpoint *endpoint = aliasEndpoints + aliasId;
1091 endpoint->metaObject = this;
1093 endpoint->connect(&ctxt->idValues[d->contextIdx].bindings);
1095 endpoint->tryConnect();
1099 void QDeclarativeVMEMetaObject::connectAliasSignal(int index)
1101 int aliasId = (index - methodOffset) - metaData->propertyCount;
1102 if (aliasId < 0 || aliasId >= metaData->aliasCount)
1105 connectAlias(aliasId);