Adapt to Qt5 meta-object changes
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmlvmemetaobject.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qqmlvmemetaobject_p.h"
43
44
45 #include "qqml.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"
52
53 #include <private/qv8variantresource_p.h>
54
55 Q_DECLARE_METATYPE(QJSValue);
56
57 QT_BEGIN_NAMESPACE
58
59 class QQmlVMEVariant
60 {
61 public:
62     inline QQmlVMEVariant();
63     inline ~QQmlVMEVariant();
64
65     inline const void *dataPtr() const;
66     inline void *dataPtr();
67     inline int dataType() const;
68
69     inline QObject *asQObject();
70     inline const QVariant &asQVariant();
71     inline int asInt();
72     inline bool asBool();
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();
81
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 &);
94 private:
95     int type;
96     void *data[4]; // Large enough to hold all types
97
98     inline void cleanup();
99 };
100
101 class QQmlVMEMetaObjectEndpoint : public QQmlNotifierEndpoint
102 {
103 public:
104     QQmlVMEMetaObjectEndpoint();
105     static void vmecallback(QQmlNotifierEndpoint *);
106     void tryConnect();
107
108     QFlagPointer<QQmlVMEMetaObject> metaObject;
109 };
110
111
112 QQmlVMEVariant::QQmlVMEVariant()
113 : type(QVariant::Invalid)
114 {
115 }
116
117 QQmlVMEVariant::~QQmlVMEVariant()
118 {
119     cleanup();
120 }
121
122 void QQmlVMEVariant::cleanup()
123 {
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;
156     }
157
158 }
159
160 int QQmlVMEVariant::dataType() const
161 {
162     return type;
163 }
164
165 const void *QQmlVMEVariant::dataPtr() const
166 {
167     return &data;
168 }
169
170 void *QQmlVMEVariant::dataPtr() 
171 {
172     return &data;
173 }
174
175 QObject *QQmlVMEVariant::asQObject() 
176 {
177     if (type != QMetaType::QObjectStar) 
178         setValue((QObject *)0);
179
180     return *(QQmlGuard<QObject> *)(dataPtr());
181 }
182
183 const QVariant &QQmlVMEVariant::asQVariant() 
184 {
185     if (type != QMetaType::QVariant)
186         setValue(QVariant());
187
188     return *(QVariant *)(dataPtr());
189 }
190
191 int QQmlVMEVariant::asInt() 
192 {
193     if (type != QMetaType::Int)
194         setValue(int(0));
195
196     return *(int *)(dataPtr());
197 }
198
199 bool QQmlVMEVariant::asBool() 
200 {
201     if (type != QMetaType::Bool)
202         setValue(bool(false));
203
204     return *(bool *)(dataPtr());
205 }
206
207 double QQmlVMEVariant::asDouble() 
208 {
209     if (type != QMetaType::Double)
210         setValue(double(0));
211
212     return *(double *)(dataPtr());
213 }
214
215 const QString &QQmlVMEVariant::asQString() 
216 {
217     if (type != QMetaType::QString)
218         setValue(QString());
219
220     return *(QString *)(dataPtr());
221 }
222
223 const QUrl &QQmlVMEVariant::asQUrl() 
224 {
225     if (type != QMetaType::QUrl)
226         setValue(QUrl());
227
228     return *(QUrl *)(dataPtr());
229 }
230
231 const QColor &QQmlVMEVariant::asQColor() 
232 {
233     if (type != QMetaType::QColor)
234         setValue(QColor());
235
236     return *(QColor *)(dataPtr());
237 }
238
239 const QTime &QQmlVMEVariant::asQTime() 
240 {
241     if (type != QMetaType::QTime)
242         setValue(QTime());
243
244     return *(QTime *)(dataPtr());
245 }
246
247 const QDate &QQmlVMEVariant::asQDate() 
248 {
249     if (type != QMetaType::QDate)
250         setValue(QDate());
251
252     return *(QDate *)(dataPtr());
253 }
254
255 const QDateTime &QQmlVMEVariant::asQDateTime() 
256 {
257     if (type != QMetaType::QDateTime)
258         setValue(QDateTime());
259
260     return *(QDateTime *)(dataPtr());
261 }
262
263 const QJSValue &QQmlVMEVariant::asQJSValue()
264 {
265     if (type != qMetaTypeId<QJSValue>())
266         setValue(QJSValue());
267
268     return *(QJSValue *)(dataPtr());
269 }
270
271 void QQmlVMEVariant::setValue(QObject *v)
272 {
273     if (type != QMetaType::QObjectStar) {
274         cleanup();
275         type = QMetaType::QObjectStar;
276         new (dataPtr()) QQmlGuard<QObject>();
277     }
278     *(QQmlGuard<QObject>*)(dataPtr()) = v;
279 }
280
281 void QQmlVMEVariant::setValue(const QVariant &v)
282 {
283     if (type != qMetaTypeId<QVariant>()) {
284         cleanup();
285         type = qMetaTypeId<QVariant>();
286         new (dataPtr()) QVariant(v);
287     } else {
288         *(QVariant *)(dataPtr()) = v;
289     }
290 }
291
292 void QQmlVMEVariant::setValue(int v)
293 {
294     if (type != QMetaType::Int) {
295         cleanup();
296         type = QMetaType::Int;
297     }
298     *(int *)(dataPtr()) = v;
299 }
300
301 void QQmlVMEVariant::setValue(bool v)
302 {
303     if (type != QMetaType::Bool) {
304         cleanup();
305         type = QMetaType::Bool;
306     }
307     *(bool *)(dataPtr()) = v;
308 }
309
310 void QQmlVMEVariant::setValue(double v)
311 {
312     if (type != QMetaType::Double) {
313         cleanup();
314         type = QMetaType::Double;
315     }
316     *(double *)(dataPtr()) = v;
317 }
318
319 void QQmlVMEVariant::setValue(const QString &v)
320 {
321     if (type != QMetaType::QString) {
322         cleanup();
323         type = QMetaType::QString;
324         new (dataPtr()) QString(v);
325     } else {
326         *(QString *)(dataPtr()) = v;
327     }
328 }
329
330 void QQmlVMEVariant::setValue(const QUrl &v)
331 {
332     if (type != QMetaType::QUrl) {
333         cleanup();
334         type = QMetaType::QUrl;
335         new (dataPtr()) QUrl(v);
336     } else {
337         *(QUrl *)(dataPtr()) = v;
338     }
339 }
340
341 void QQmlVMEVariant::setValue(const QColor &v)
342 {
343     if (type != QMetaType::QColor) {
344         cleanup();
345         type = QMetaType::QColor;
346         new (dataPtr()) QColor(v);
347     } else {
348         *(QColor *)(dataPtr()) = v;
349     }
350 }
351
352 void QQmlVMEVariant::setValue(const QTime &v)
353 {
354     if (type != QMetaType::QTime) {
355         cleanup();
356         type = QMetaType::QTime;
357         new (dataPtr()) QTime(v);
358     } else {
359         *(QTime *)(dataPtr()) = v;
360     }
361 }
362
363 void QQmlVMEVariant::setValue(const QDate &v)
364 {
365     if (type != QMetaType::QDate) {
366         cleanup();
367         type = QMetaType::QDate;
368         new (dataPtr()) QDate(v);
369     } else {
370         *(QDate *)(dataPtr()) = v;
371     }
372 }
373
374 void QQmlVMEVariant::setValue(const QDateTime &v)
375 {
376     if (type != QMetaType::QDateTime) {
377         cleanup();
378         type = QMetaType::QDateTime;
379         new (dataPtr()) QDateTime(v);
380     } else {
381         *(QDateTime *)(dataPtr()) = v;
382     }
383 }
384
385 void QQmlVMEVariant::setValue(const QJSValue &v)
386 {
387     if (type != qMetaTypeId<QJSValue>()) {
388         cleanup();
389         type = qMetaTypeId<QJSValue>();
390         new (dataPtr()) QJSValue(v);
391     } else {
392         *(QJSValue *)(dataPtr()) = v;
393     }
394 }
395
396 QQmlVMEMetaObjectEndpoint::QQmlVMEMetaObjectEndpoint()
397 {
398     callback = &vmecallback;
399 }
400
401 void QQmlVMEMetaObjectEndpoint::vmecallback(QQmlNotifierEndpoint *e)
402 {
403     QQmlVMEMetaObjectEndpoint *vmee = static_cast<QQmlVMEMetaObjectEndpoint*>(e);
404     vmee->tryConnect();
405 }
406
407 void QQmlVMEMetaObjectEndpoint::tryConnect()
408 {
409     int aliasId = this - metaObject->aliasEndpoints;
410
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);
415     } else {
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();
420             if (!target)
421                 return;
422
423             QMetaProperty prop = target->metaObject()->property(d->propertyIndex());
424             if (prop.hasNotifySignal())
425                 connect(target, prop.notifySignalIndex());
426         }
427
428         metaObject.setFlag();
429     }
430 }
431
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)
440 {
441     compiledData->addref();
442
443     *static_cast<QMetaObject *>(this) = *other;
444     this->d.superdata = obj->metaObject();
445
446     QObjectPrivate *op = QObjectPrivate::get(obj);
447     if (op->metaObject)
448         parent = static_cast<QAbstractDynamicMetaObject*>(op->metaObject);
449     op->metaObject = this;
450
451     propOffset = QAbstractDynamicMetaObject::propertyOffset();
452     methodOffset = QAbstractDynamicMetaObject::methodOffset();
453
454     data = new QQmlVMEVariant[metaData->propertyCount - metaData->varPropertyCount];
455
456     aConnected.resize(metaData->aliasCount);
457     int list_type = qMetaTypeId<QQmlListProperty<QObject> >();
458
459     // ### Optimize
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);
465         } 
466     }
467
468     firstVarPropertyIndex = metaData->propertyCount - metaData->varPropertyCount;
469     if (metaData->varPropertyCount)
470         QV8GCCallback::addGcCallbackNode(this);
471 }
472
473 QQmlVMEMetaObject::~QQmlVMEMetaObject()
474 {
475     compiledData->release();
476     delete parent;
477     delete [] data;
478     delete [] aliasEndpoints;
479
480     for (int ii = 0; v8methods && ii < metaData->methodCount; ++ii) {
481         qPersistentDispose(v8methods[ii]);
482     }
483     delete [] v8methods;
484
485     if (metaData->varPropertyCount)
486         qPersistentDispose(varProperties); // if not weak, will not have been cleaned up by the callback.
487 }
488
489 int QQmlVMEMetaObject::metaCall(QMetaObject::Call c, int _id, void **a)
490 {
491     int id = _id;
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();
501
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);
508                     Q_ASSERT(valueType);
509
510                     valueType->setValue(QVariant(type, a[0]));
511                     QMetaProperty valueProp = valueType->metaObject()->property(valueIndex);
512                     vi->write(valueProp.read(valueType));
513
514                     if (!ep) delete valueType;
515                     return -1;
516                 } else {
517                     vi->write(QVariant(type, a[0]));
518                     return -1;
519                 }
520             }
521         }
522     }
523     if (c == QMetaObject::ReadProperty || c == QMetaObject::WriteProperty || c == QMetaObject::ResetProperty) {
524         if (id >= propOffset) {
525             id -= propOffset;
526
527             if (id < metaData->propertyCount) {
528                int t = (metaData->propertyData() + id)->propertyType;
529                 bool needActivate = false;
530
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();
536                     if (v8e) {
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]));
543                         }
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();
547                     }
548
549                 } else {
550
551                     if (c == QMetaObject::ReadProperty) {
552                         switch(t) {
553                         case QVariant::Int:
554                             *reinterpret_cast<int *>(a[0]) = data[id].asInt();
555                             break;
556                         case QVariant::Bool:
557                             *reinterpret_cast<bool *>(a[0]) = data[id].asBool();
558                             break;
559                         case QVariant::Double:
560                             *reinterpret_cast<double *>(a[0]) = data[id].asDouble();
561                             break;
562                         case QVariant::String:
563                             *reinterpret_cast<QString *>(a[0]) = data[id].asQString();
564                             break;
565                         case QVariant::Url:
566                             *reinterpret_cast<QUrl *>(a[0]) = data[id].asQUrl();
567                             break;
568                         case QVariant::Color:
569                             *reinterpret_cast<QColor *>(a[0]) = data[id].asQColor();
570                             break;
571                         case QVariant::Date:
572                             *reinterpret_cast<QDate *>(a[0]) = data[id].asQDate();
573                             break;
574                         case QVariant::DateTime:
575                             *reinterpret_cast<QDateTime *>(a[0]) = data[id].asQDateTime();
576                             break;
577                         case QMetaType::QObjectStar:
578                             *reinterpret_cast<QObject **>(a[0]) = data[id].asQObject();
579                             break;
580                         case QMetaType::QVariant:
581                             *reinterpret_cast<QVariant *>(a[0]) = readPropertyAsVariant(id);
582                             break;
583                         default:
584                             break;
585                         }
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, 
592                                                                   list_clear);
593                         }
594
595                     } else if (c == QMetaObject::WriteProperty) {
596
597                         switch(t) {
598                         case QVariant::Int:
599                             needActivate = *reinterpret_cast<int *>(a[0]) != data[id].asInt();
600                             data[id].setValue(*reinterpret_cast<int *>(a[0]));
601                             break;
602                         case QVariant::Bool:
603                             needActivate = *reinterpret_cast<bool *>(a[0]) != data[id].asBool();
604                             data[id].setValue(*reinterpret_cast<bool *>(a[0]));
605                             break;
606                         case QVariant::Double:
607                             needActivate = *reinterpret_cast<double *>(a[0]) != data[id].asDouble();
608                             data[id].setValue(*reinterpret_cast<double *>(a[0]));
609                             break;
610                         case QVariant::String:
611                             needActivate = *reinterpret_cast<QString *>(a[0]) != data[id].asQString();
612                             data[id].setValue(*reinterpret_cast<QString *>(a[0]));
613                             break;
614                         case QVariant::Url:
615                             needActivate = *reinterpret_cast<QUrl *>(a[0]) != data[id].asQUrl();
616                             data[id].setValue(*reinterpret_cast<QUrl *>(a[0]));
617                             break;
618                         case QVariant::Color:
619                             needActivate = *reinterpret_cast<QColor *>(a[0]) != data[id].asQColor();
620                             data[id].setValue(*reinterpret_cast<QColor *>(a[0]));
621                             break;
622                         case QVariant::Date:
623                             needActivate = *reinterpret_cast<QDate *>(a[0]) != data[id].asQDate();
624                             data[id].setValue(*reinterpret_cast<QDate *>(a[0]));
625                             break;
626                         case QVariant::DateTime:
627                             needActivate = *reinterpret_cast<QDateTime *>(a[0]) != data[id].asQDateTime();
628                             data[id].setValue(*reinterpret_cast<QDateTime *>(a[0]));
629                             break;
630                         case QMetaType::QObjectStar:
631                             needActivate = *reinterpret_cast<QObject **>(a[0]) != data[id].asQObject();
632                             data[id].setValue(*reinterpret_cast<QObject **>(a[0]));
633                             break;
634                         case QMetaType::QVariant:
635                             writeProperty(id, *reinterpret_cast<QVariant *>(a[0]));
636                             break;
637                         default:
638                             break;
639                         }
640                     }
641
642                 }
643
644                 if (c == QMetaObject::WriteProperty && needActivate) {
645                     activate(object, methodOffset + id, 0);
646                 }
647
648                 return -1;
649             }
650
651             id -= metaData->propertyCount;
652
653             if (id < metaData->aliasCount) {
654
655                 QQmlVMEMetaData::AliasData *d = metaData->aliasData() + id;
656
657                 if (d->flags & QML_ALIAS_FLAG_PTR && c == QMetaObject::ReadProperty) 
658                         *reinterpret_cast<void **>(a[0]) = 0;
659
660                 if (!ctxt) return -1;
661
662                 QQmlContext *context = ctxt->asQQmlContext();
663                 QQmlContextPrivate *ctxtPriv = QQmlContextPrivate::get(context);
664
665                 QObject *target = ctxtPriv->data->idValues[d->contextIdx].data();
666                 if (!target) 
667                     return -1;
668
669                 connectAlias(id);
670
671                 if (d->isObjectAlias()) {
672                     *reinterpret_cast<QObject **>(a[0]) = target;
673                     return -1;
674                 } 
675                 
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();
684                         }
685                     }
686                 }
687                 
688                 if (d->isValueTypeAlias()) {
689                     // Value type property
690                     QQmlEnginePrivate *ep = QQmlEnginePrivate::get(ctxt->engine);
691
692                     QQmlValueType *valueType = ep->valueTypes[d->valueType()];
693                     Q_ASSERT(valueType);
694
695                     valueType->read(target, d->propertyIndex());
696                     int rv = QMetaObject::metacall(valueType, c, d->valueTypeIndex(), a);
697                     
698                     if (c == QMetaObject::WriteProperty)
699                         valueType->write(target, d->propertyIndex(), 0x00);
700
701                     return rv;
702
703                 } else {
704                     return QMetaObject::metacall(target, c, d->propertyIndex(), a);
705                 }
706
707             }
708             return -1;
709
710         }
711
712     } else if(c == QMetaObject::InvokeMetaMethod) {
713
714         if (id >= methodOffset) {
715
716             id -= methodOffset;
717             int plainSignals = metaData->signalCount + metaData->propertyCount +
718                                metaData->aliasCount;
719             if (id < plainSignals) {
720                 QMetaObject::activate(object, _id, a);
721                 return -1;
722             }
723
724             id -= plainSignals;
725
726             if (id < metaData->methodCount) {
727                 if (!ctxt->engine)
728                     return -1; // We can't run the method
729
730                 QQmlEnginePrivate *ep = QQmlEnginePrivate::get(ctxt->engine);
731                 ep->referenceScarceResources(); // "hold" scarce resources in memory during evaluation.
732
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.
739                     QQmlError e;
740                     e.setDescription(QString(QLatin1String("Exception occurred during compilation of function: %1")).
741                                      arg(QLatin1String(QMetaObject::method(_id).methodSignature().constData())));
742                     ep->warning(e);
743                     return -1; // The dynamic method with that id is not available.
744                 }
745
746                 QQmlVMEMetaData::MethodData *data = metaData->methodData() + id;
747
748                 v8::HandleScope handle_scope;
749                 v8::Context::Scope scope(ep->v8engine()->context());
750                 v8::Handle<v8::Value> *args = 0;
751
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]);
756                 }
757
758                 v8::TryCatch try_catch;
759
760                 v8::Local<v8::Value> result = function->Call(ep->v8engine()->global(), data->parameterCount, args);
761
762                 QVariant rv;
763                 if (try_catch.HasCaught()) {
764                     QQmlError error;
765                     QQmlExpressionPrivate::exceptionToError(try_catch.Message(), error);
766                     if (error.isValid())
767                         ep->warning(error);
768                     if (a[0]) *(QVariant *)a[0] = QVariant();
769                 } else {
770                     if (a[0]) *(QVariant *)a[0] = ep->v8engine()->toVariant(result, 0);
771                 }
772
773                 ep->dereferenceScarceResources(); // "release" scarce resources if top-level expression evaluation is complete.
774                 return -1;
775             }
776             return -1;
777         }
778     }
779
780     if (parent)
781         return parent->metaCall(c, _id, a);
782     else
783         return object->qt_metacall(c, _id, a);
784 }
785
786 v8::Handle<v8::Function> QQmlVMEMetaObject::method(int index)
787 {
788     if (!v8methods) 
789         v8methods = new v8::Persistent<v8::Function>[metaData->methodCount];
790
791     if (v8methods[index].IsEmpty()) {
792         QQmlVMEMetaData::MethodData *data = metaData->methodData() + index;
793
794         const char *body = ((const char*)metaData) + data->bodyOffset;
795         int bodyLength = data->bodyLength;
796
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
799         // object
800         v8methods[index] = QQmlExpressionPrivate::evalFunction(ctxt, object, body,
801                                                                        bodyLength,
802                                                                        ctxt->urlString,
803                                                                        data->lineNumber);
804     }
805
806     return v8methods[index];
807 }
808
809 v8::Handle<v8::Value> QQmlVMEMetaObject::readVarProperty(int id)
810 {
811     Q_ASSERT(id >= firstVarPropertyIndex);
812
813     ensureVarPropertiesAllocated();
814     return varProperties->Get(id - firstVarPropertyIndex);
815 }
816
817 QVariant QQmlVMEMetaObject::readPropertyAsVariant(int id)
818 {
819     if (id >= firstVarPropertyIndex) {
820         ensureVarPropertiesAllocated();
821         return QQmlEnginePrivate::get(ctxt->engine)->v8engine()->toVariant(varProperties->Get(id - firstVarPropertyIndex), -1);
822     } else {
823         if (data[id].dataType() == QMetaType::QObjectStar) {
824             return QVariant::fromValue(data[id].asQObject());
825         } else {
826             return data[id].asQVariant();
827         }
828     }
829 }
830
831 void QQmlVMEMetaObject::writeVarProperty(int id, v8::Handle<v8::Value> value)
832 {
833     Q_ASSERT(id >= firstVarPropertyIndex);
834     ensureVarPropertiesAllocated();
835
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));
841         if (r) {
842             r->removeVmePropertyReference();
843         }
844     }
845
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));
850         if (r) {
851             r->addVmePropertyReference();
852         }
853     }
854
855     // Write the value and emit change signal as appropriate.
856     varProperties->Set(id - firstVarPropertyIndex, value);
857     activate(object, methodOffset + id, 0);
858 }
859
860 void QQmlVMEMetaObject::writeProperty(int id, const QVariant &value)
861 {
862     if (id >= firstVarPropertyIndex) {
863         ensureVarPropertiesAllocated();
864
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));
870             if (r) {
871                 r->removeVmePropertyReference();
872             }
873         }
874
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));
880             if (r) {
881                 r->addVmePropertyReference();
882             }
883         }
884
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);
890     } else {
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));
896         } else {
897             needActivate = (data[id].dataType() != qMetaTypeId<QVariant>() ||
898                             data[id].asQVariant().userType() != value.userType() ||
899                             data[id].asQVariant() != value);
900             data[id].setValue(value);
901         }
902
903         if (needActivate)
904             activate(object, methodOffset + id, 0);
905     }
906 }
907
908 void QQmlVMEMetaObject::listChanged(int id)
909 {
910     activate(object, methodOffset + id, 0);
911 }
912
913 void QQmlVMEMetaObject::list_append(QQmlListProperty<QObject> *prop, QObject *o)
914 {
915     List *list = static_cast<List *>(prop->data);
916     list->append(o);
917     QMetaObject::activate(prop->object, list->notifyIndex, 0);
918 }
919
920 int QQmlVMEMetaObject::list_count(QQmlListProperty<QObject> *prop)
921 {
922     return static_cast<List *>(prop->data)->count();
923 }
924
925 QObject *QQmlVMEMetaObject::list_at(QQmlListProperty<QObject> *prop, int index)
926 {
927     return static_cast<List *>(prop->data)->at(index);
928 }
929
930 void QQmlVMEMetaObject::list_clear(QQmlListProperty<QObject> *prop)
931 {
932     List *list = static_cast<List *>(prop->data);
933     list->clear();
934     QMetaObject::activate(prop->object, list->notifyIndex, 0);
935 }
936
937 void QQmlVMEMetaObject::registerInterceptor(int index, int valueIndex, QQmlPropertyValueInterceptor *interceptor)
938 {
939     if (aInterceptors.isEmpty())
940         aInterceptors.resize(propertyCount() + metaData->propertyCount);
941     aInterceptors.setBit(index);
942     interceptors.insert(index, qMakePair(valueIndex, interceptor));
943 }
944
945 int QQmlVMEMetaObject::vmeMethodLineNumber(int index)
946 {
947     if (index < methodOffset) {
948         Q_ASSERT(parent);
949         return static_cast<QQmlVMEMetaObject *>(parent)->vmeMethodLineNumber(index);
950     }
951
952     int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
953     Q_ASSERT(index >= (methodOffset + plainSignals) && index < (methodOffset + plainSignals + metaData->methodCount));
954
955     int rawIndex = index - methodOffset - plainSignals;
956
957     QQmlVMEMetaData::MethodData *data = metaData->methodData() + rawIndex;
958     return data->lineNumber;
959 }
960
961 v8::Handle<v8::Function> QQmlVMEMetaObject::vmeMethod(int index)
962 {
963     if (index < methodOffset) {
964         Q_ASSERT(parent);
965         return static_cast<QQmlVMEMetaObject *>(parent)->vmeMethod(index);
966     }
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);
970 }
971
972 // Used by debugger
973 void QQmlVMEMetaObject::setVmeMethod(int index, v8::Persistent<v8::Function> value)
974 {
975     if (index < methodOffset) {
976         Q_ASSERT(parent);
977         return static_cast<QQmlVMEMetaObject *>(parent)->setVmeMethod(index, value);
978     }
979     int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
980     Q_ASSERT(index >= (methodOffset + plainSignals) && index < (methodOffset + plainSignals + metaData->methodCount));
981
982     if (!v8methods) 
983         v8methods = new v8::Persistent<v8::Function>[metaData->methodCount];
984
985     int methodIndex = index - methodOffset - plainSignals;
986     if (!v8methods[methodIndex].IsEmpty()) 
987         qPersistentDispose(v8methods[methodIndex]);
988     v8methods[methodIndex] = value;
989 }
990
991 v8::Handle<v8::Value> QQmlVMEMetaObject::vmeProperty(int index)
992 {
993     if (index < propOffset) {
994         Q_ASSERT(parent);
995         return static_cast<QQmlVMEMetaObject *>(parent)->vmeProperty(index);
996     }
997     return readVarProperty(index - propOffset);
998 }
999
1000 void QQmlVMEMetaObject::setVMEProperty(int index, v8::Handle<v8::Value> v)
1001 {
1002     if (index < propOffset) {
1003         Q_ASSERT(parent);
1004         static_cast<QQmlVMEMetaObject *>(parent)->setVMEProperty(index, v);
1005         return;
1006     }
1007     return writeVarProperty(index - propOffset, v);
1008 }
1009
1010 void QQmlVMEMetaObject::ensureVarPropertiesAllocated()
1011 {
1012     if (!varPropertiesInitialized)
1013         allocateVarPropertiesArray();
1014 }
1015
1016 // see also: QV8GCCallback::garbageCollectorPrologueCallback()
1017 void QQmlVMEMetaObject::allocateVarPropertiesArray()
1018 {
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;
1024 }
1025
1026 /*
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.
1032  */
1033 void QQmlVMEMetaObject::VarPropertiesWeakReferenceCallback(v8::Persistent<v8::Value> object, void* parameter)
1034 {
1035     QQmlVMEMetaObject *vmemo = static_cast<QQmlVMEMetaObject*>(parameter);
1036     Q_ASSERT(vmemo);
1037     qPersistentDispose(object);
1038     vmemo->varProperties.Clear();
1039 }
1040
1041 void QQmlVMEMetaObject::GcPrologueCallback(QV8GCCallback::Node *node)
1042 {
1043     QQmlVMEMetaObject *vmemo = static_cast<QQmlVMEMetaObject*>(node);
1044     Q_ASSERT(vmemo);
1045     if (!vmemo->varPropertiesInitialized || vmemo->varProperties.IsEmpty() || !vmemo->ctxt || !vmemo->ctxt->engine)
1046         return;
1047     QQmlEnginePrivate *ep = QQmlEnginePrivate::get(vmemo->ctxt->engine);
1048     ep->v8engine()->addRelationshipForGC(vmemo->object, vmemo->varProperties);
1049 }
1050
1051 bool QQmlVMEMetaObject::aliasTarget(int index, QObject **target, int *coreIndex, int *valueTypeIndex) const
1052 {
1053     Q_ASSERT(index >= propOffset + metaData->propertyCount);
1054
1055     *target = 0;
1056     *coreIndex = -1;
1057     *valueTypeIndex = -1;
1058
1059     if (!ctxt)
1060         return false;
1061
1062     QQmlVMEMetaData::AliasData *d = metaData->aliasData() + (index - propOffset - metaData->propertyCount);
1063     QQmlContext *context = ctxt->asQQmlContext();
1064     QQmlContextPrivate *ctxtPriv = QQmlContextPrivate::get(context);
1065
1066     *target = ctxtPriv->data->idValues[d->contextIdx].data();
1067     if (!*target)
1068         return false;
1069
1070     if (d->isObjectAlias()) {
1071     } else if (d->isValueTypeAlias()) {
1072         *coreIndex = d->propertyIndex();
1073         *valueTypeIndex = d->valueTypeIndex();
1074     } else {
1075         *coreIndex = d->propertyIndex();
1076     }
1077
1078     return true;
1079 }
1080
1081 void QQmlVMEMetaObject::connectAlias(int aliasId)
1082 {
1083     if (!aConnected.testBit(aliasId)) {
1084
1085         if (!aliasEndpoints)
1086             aliasEndpoints = new QQmlVMEMetaObjectEndpoint[metaData->aliasCount];
1087
1088         aConnected.setBit(aliasId);
1089
1090         QQmlVMEMetaData::AliasData *d = metaData->aliasData() + aliasId;
1091
1092         QQmlVMEMetaObjectEndpoint *endpoint = aliasEndpoints + aliasId;
1093         endpoint->metaObject = this;
1094
1095         endpoint->connect(&ctxt->idValues[d->contextIdx].bindings);
1096
1097         endpoint->tryConnect();
1098     }
1099 }
1100
1101 void QQmlVMEMetaObject::connectAliasSignal(int index)
1102 {
1103     int aliasId = (index - methodOffset) - metaData->propertyCount;
1104     if (aliasId < 0 || aliasId >= metaData->aliasCount)
1105         return;
1106
1107     connectAlias(aliasId);
1108 }
1109
1110 QT_END_NAMESPACE