Support and use parameters in QQmlNotifierEndpoint.
[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 #include <private/qqmlglobal_p.h>
55
56 Q_DECLARE_METATYPE(QJSValue);
57
58 QT_BEGIN_NAMESPACE
59
60 QQmlVMEVariantQObjectPtr::QQmlVMEVariantQObjectPtr()
61     : QQmlGuard<QObject>(0), m_target(0), m_index(-1)
62 {
63 }
64
65 QQmlVMEVariantQObjectPtr::~QQmlVMEVariantQObjectPtr()
66 {
67 }
68
69 void QQmlVMEVariantQObjectPtr::objectDestroyed(QObject *)
70 {
71     if (m_target && m_index >= 0)
72         m_target->activate(m_target->object, m_target->methodOffset + m_index, 0);
73 }
74
75 void QQmlVMEVariantQObjectPtr::setGuardedValue(QObject *obj, QQmlVMEMetaObject *target, int index)
76 {
77     m_target = target;
78     m_index = index;
79     setObject(obj);
80 }
81
82 class QQmlVMEVariant
83 {
84 public:
85     inline QQmlVMEVariant();
86     inline ~QQmlVMEVariant();
87
88     inline const void *dataPtr() const;
89     inline void *dataPtr();
90     inline int dataType() const;
91     inline size_t dataSize() const;
92
93     inline QObject *asQObject();
94     inline const QVariant &asQVariant();
95     inline int asInt();
96     inline bool asBool();
97     inline double asDouble();
98     inline const QString &asQString();
99     inline const QUrl &asQUrl();
100     inline const QTime &asQTime();
101     inline const QDate &asQDate();
102     inline const QDateTime &asQDateTime();
103     inline const QJSValue &asQJSValue();
104
105     inline void setValue(QObject *v, QQmlVMEMetaObject *target, int index);
106     inline void setValue(const QVariant &);
107     inline void setValue(int);
108     inline void setValue(bool);
109     inline void setValue(double);
110     inline void setValue(const QString &);
111     inline void setValue(const QUrl &);
112     inline void setValue(const QTime &);
113     inline void setValue(const QDate &);
114     inline void setValue(const QDateTime &);
115     inline void setValue(const QJSValue &);
116
117     inline void setDataType(int t);
118
119 private:
120     int type;
121     void *data[6]; // Large enough to hold all types
122
123     inline void cleanup();
124 };
125
126 class QQmlVMEMetaObjectEndpoint : public QQmlNotifierEndpoint
127 {
128 public:
129     QQmlVMEMetaObjectEndpoint();
130     static void vmecallback(QQmlNotifierEndpoint *, void **);
131     void tryConnect();
132
133     QFlagPointer<QQmlVMEMetaObject> metaObject;
134 };
135
136
137 QQmlVMEVariant::QQmlVMEVariant()
138 : type(QVariant::Invalid)
139 {
140 }
141
142 QQmlVMEVariant::~QQmlVMEVariant()
143 {
144     cleanup();
145 }
146
147 void QQmlVMEVariant::cleanup()
148 {
149     if (type == QVariant::Invalid) {
150     } else if (type == QMetaType::Int ||
151                type == QMetaType::Bool ||
152                type == QMetaType::Double) {
153         type = QVariant::Invalid;
154     } else if (type == QMetaType::QObjectStar) {
155         ((QQmlVMEVariantQObjectPtr*)dataPtr())->~QQmlVMEVariantQObjectPtr();
156         type = QVariant::Invalid;
157     } else if (type == QMetaType::QString) {
158         ((QString *)dataPtr())->~QString();
159         type = QVariant::Invalid;
160     } else if (type == QMetaType::QUrl) {
161         ((QUrl *)dataPtr())->~QUrl();
162         type = QVariant::Invalid;
163     } else if (type == QMetaType::QTime) {
164         ((QTime *)dataPtr())->~QTime();
165         type = QVariant::Invalid;
166     } else if (type == QMetaType::QDate) {
167         ((QDate *)dataPtr())->~QDate();
168         type = QVariant::Invalid;
169     } else if (type == QMetaType::QDateTime) {
170         ((QDateTime *)dataPtr())->~QDateTime();
171         type = QVariant::Invalid;
172     } else if (type == qMetaTypeId<QVariant>()) {
173         ((QVariant *)dataPtr())->~QVariant();
174         type = QVariant::Invalid;
175     } else if (type == qMetaTypeId<QJSValue>()) {
176         ((QJSValue *)dataPtr())->~QJSValue();
177         type = QVariant::Invalid;
178     } else {
179         if (QQml_valueTypeProvider()->destroyValueType(type, dataPtr(), dataSize())) {
180             type = QVariant::Invalid;
181         }
182     }
183 }
184
185 int QQmlVMEVariant::dataType() const
186 {
187     return type;
188 }
189
190 const void *QQmlVMEVariant::dataPtr() const
191 {
192     return &data;
193 }
194
195 void *QQmlVMEVariant::dataPtr() 
196 {
197     return &data;
198 }
199
200 size_t QQmlVMEVariant::dataSize() const
201 {
202     return sizeof(data);
203 }
204
205 QObject *QQmlVMEVariant::asQObject() 
206 {
207     if (type != QMetaType::QObjectStar)
208         setValue((QObject *)0, 0, -1);
209
210     return *(QQmlGuard<QObject> *)(dataPtr());
211 }
212
213 const QVariant &QQmlVMEVariant::asQVariant() 
214 {
215     if (type != QMetaType::QVariant)
216         setValue(QVariant());
217
218     return *(QVariant *)(dataPtr());
219 }
220
221 int QQmlVMEVariant::asInt() 
222 {
223     if (type != QMetaType::Int)
224         setValue(int(0));
225
226     return *(int *)(dataPtr());
227 }
228
229 bool QQmlVMEVariant::asBool() 
230 {
231     if (type != QMetaType::Bool)
232         setValue(bool(false));
233
234     return *(bool *)(dataPtr());
235 }
236
237 double QQmlVMEVariant::asDouble() 
238 {
239     if (type != QMetaType::Double)
240         setValue(double(0));
241
242     return *(double *)(dataPtr());
243 }
244
245 const QString &QQmlVMEVariant::asQString() 
246 {
247     if (type != QMetaType::QString)
248         setValue(QString());
249
250     return *(QString *)(dataPtr());
251 }
252
253 const QUrl &QQmlVMEVariant::asQUrl() 
254 {
255     if (type != QMetaType::QUrl)
256         setValue(QUrl());
257
258     return *(QUrl *)(dataPtr());
259 }
260
261 const QTime &QQmlVMEVariant::asQTime() 
262 {
263     if (type != QMetaType::QTime)
264         setValue(QTime());
265
266     return *(QTime *)(dataPtr());
267 }
268
269 const QDate &QQmlVMEVariant::asQDate() 
270 {
271     if (type != QMetaType::QDate)
272         setValue(QDate());
273
274     return *(QDate *)(dataPtr());
275 }
276
277 const QDateTime &QQmlVMEVariant::asQDateTime() 
278 {
279     if (type != QMetaType::QDateTime)
280         setValue(QDateTime());
281
282     return *(QDateTime *)(dataPtr());
283 }
284
285 const QJSValue &QQmlVMEVariant::asQJSValue()
286 {
287     if (type != qMetaTypeId<QJSValue>())
288         setValue(QJSValue());
289
290     return *(QJSValue *)(dataPtr());
291 }
292
293 void QQmlVMEVariant::setValue(QObject *v, QQmlVMEMetaObject *target, int index)
294 {
295     if (type != QMetaType::QObjectStar) {
296         cleanup();
297         type = QMetaType::QObjectStar;
298         new (dataPtr()) QQmlVMEVariantQObjectPtr;
299     }
300     reinterpret_cast<QQmlVMEVariantQObjectPtr*>(dataPtr())->setGuardedValue(v, target, index);
301 }
302
303 void QQmlVMEVariant::setValue(const QVariant &v)
304 {
305     if (type != qMetaTypeId<QVariant>()) {
306         cleanup();
307         type = qMetaTypeId<QVariant>();
308         new (dataPtr()) QVariant(v);
309     } else {
310         *(QVariant *)(dataPtr()) = v;
311     }
312 }
313
314 void QQmlVMEVariant::setValue(int v)
315 {
316     if (type != QMetaType::Int) {
317         cleanup();
318         type = QMetaType::Int;
319     }
320     *(int *)(dataPtr()) = v;
321 }
322
323 void QQmlVMEVariant::setValue(bool v)
324 {
325     if (type != QMetaType::Bool) {
326         cleanup();
327         type = QMetaType::Bool;
328     }
329     *(bool *)(dataPtr()) = v;
330 }
331
332 void QQmlVMEVariant::setValue(double v)
333 {
334     if (type != QMetaType::Double) {
335         cleanup();
336         type = QMetaType::Double;
337     }
338     *(double *)(dataPtr()) = v;
339 }
340
341 void QQmlVMEVariant::setValue(const QString &v)
342 {
343     if (type != QMetaType::QString) {
344         cleanup();
345         type = QMetaType::QString;
346         new (dataPtr()) QString(v);
347     } else {
348         *(QString *)(dataPtr()) = v;
349     }
350 }
351
352 void QQmlVMEVariant::setValue(const QUrl &v)
353 {
354     if (type != QMetaType::QUrl) {
355         cleanup();
356         type = QMetaType::QUrl;
357         new (dataPtr()) QUrl(v);
358     } else {
359         *(QUrl *)(dataPtr()) = v;
360     }
361 }
362
363 void QQmlVMEVariant::setValue(const QTime &v)
364 {
365     if (type != QMetaType::QTime) {
366         cleanup();
367         type = QMetaType::QTime;
368         new (dataPtr()) QTime(v);
369     } else {
370         *(QTime *)(dataPtr()) = v;
371     }
372 }
373
374 void QQmlVMEVariant::setValue(const QDate &v)
375 {
376     if (type != QMetaType::QDate) {
377         cleanup();
378         type = QMetaType::QDate;
379         new (dataPtr()) QDate(v);
380     } else {
381         *(QDate *)(dataPtr()) = v;
382     }
383 }
384
385 void QQmlVMEVariant::setValue(const QDateTime &v)
386 {
387     if (type != QMetaType::QDateTime) {
388         cleanup();
389         type = QMetaType::QDateTime;
390         new (dataPtr()) QDateTime(v);
391     } else {
392         *(QDateTime *)(dataPtr()) = v;
393     }
394 }
395
396 void QQmlVMEVariant::setValue(const QJSValue &v)
397 {
398     if (type != qMetaTypeId<QJSValue>()) {
399         cleanup();
400         type = qMetaTypeId<QJSValue>();
401         new (dataPtr()) QJSValue(v);
402     } else {
403         *(QJSValue *)(dataPtr()) = v;
404     }
405 }
406
407 void QQmlVMEVariant::setDataType(int t)
408 {
409     type = t;
410 }
411
412 QQmlVMEMetaObjectEndpoint::QQmlVMEMetaObjectEndpoint()
413 {
414     callback = &vmecallback;
415 }
416
417 void QQmlVMEMetaObjectEndpoint::vmecallback(QQmlNotifierEndpoint *e, void **)
418 {
419     QQmlVMEMetaObjectEndpoint *vmee = static_cast<QQmlVMEMetaObjectEndpoint*>(e);
420     vmee->tryConnect();
421 }
422
423 void QQmlVMEMetaObjectEndpoint::tryConnect()
424 {
425     int aliasId = this - metaObject->aliasEndpoints;
426
427     if (metaObject.flag()) {
428         // This is actually notify
429         int sigIdx = metaObject->methodOffset + aliasId + metaObject->metaData->propertyCount;
430         QMetaObject::activate(metaObject->object, sigIdx, 0);
431     } else {
432         QQmlVMEMetaData::AliasData *d = metaObject->metaData->aliasData() + aliasId;
433         if (!d->isObjectAlias()) {
434             QQmlContextData *ctxt = metaObject->ctxt;
435             QObject *target = ctxt->idValues[d->contextIdx].data();
436             if (!target)
437                 return;
438
439             QMetaProperty prop = target->metaObject()->property(d->propertyIndex());
440             if (prop.hasNotifySignal())
441                 connect(target, prop.notifySignalIndex());
442         }
443
444         metaObject.setFlag();
445     }
446 }
447
448 QQmlVMEMetaObject::QQmlVMEMetaObject(QObject *obj,
449                                                      const QMetaObject *other, 
450                                                      const QQmlVMEMetaData *meta,
451                                                      QQmlCompiledData *cdata)
452 : QV8GCCallback::Node(GcPrologueCallback), object(obj), compiledData(cdata),
453   ctxt(QQmlData::get(obj, true)->outerContext), metaData(meta), data(0),
454   aliasEndpoints(0), firstVarPropertyIndex(-1), varPropertiesInitialized(false),
455   v8methods(0), parent(0)
456 {
457     compiledData->addref();
458
459     *static_cast<QMetaObject *>(this) = *other;
460     this->d.superdata = obj->metaObject();
461
462     QObjectPrivate *op = QObjectPrivate::get(obj);
463     if (op->metaObject)
464         parent = static_cast<QAbstractDynamicMetaObject*>(op->metaObject);
465     op->metaObject = this;
466
467     propOffset = QAbstractDynamicMetaObject::propertyOffset();
468     methodOffset = QAbstractDynamicMetaObject::methodOffset();
469
470     data = new QQmlVMEVariant[metaData->propertyCount - metaData->varPropertyCount];
471
472     aConnected.resize(metaData->aliasCount);
473     int list_type = qMetaTypeId<QQmlListProperty<QObject> >();
474
475     // ### Optimize
476     for (int ii = 0; ii < metaData->propertyCount - metaData->varPropertyCount; ++ii) {
477         int t = (metaData->propertyData() + ii)->propertyType;
478         if (t == list_type) {
479             listProperties.append(List(methodOffset + ii));
480             data[ii].setValue(listProperties.count() - 1);
481         } 
482     }
483
484     firstVarPropertyIndex = metaData->propertyCount - metaData->varPropertyCount;
485     if (metaData->varPropertyCount)
486         QV8GCCallback::addGcCallbackNode(this);
487 }
488
489 QQmlVMEMetaObject::~QQmlVMEMetaObject()
490 {
491     compiledData->release();
492     delete parent;
493     delete [] data;
494     delete [] aliasEndpoints;
495
496     for (int ii = 0; v8methods && ii < metaData->methodCount; ++ii) {
497         qPersistentDispose(v8methods[ii]);
498     }
499     delete [] v8methods;
500
501     if (metaData->varPropertyCount)
502         qPersistentDispose(varProperties); // if not weak, will not have been cleaned up by the callback.
503 }
504
505 int QQmlVMEMetaObject::metaCall(QMetaObject::Call c, int _id, void **a)
506 {
507     int id = _id;
508     if(c == QMetaObject::WriteProperty) {
509         int flags = *reinterpret_cast<int*>(a[3]);
510         if (!(flags & QQmlPropertyPrivate::BypassInterceptor)
511             && !aInterceptors.isEmpty()
512             && aInterceptors.testBit(id)) {
513             QPair<int, QQmlPropertyValueInterceptor*> pair = interceptors.value(id);
514             int valueIndex = pair.first;
515             QQmlPropertyValueInterceptor *vi = pair.second;
516             int type = property(id).userType();
517
518             if (type != QVariant::Invalid) {
519                 if (valueIndex != -1) {
520                     QQmlEnginePrivate *ep = ctxt?QQmlEnginePrivate::get(ctxt->engine):0;
521                     QQmlValueType *valueType = 0;
522                     if (ep) valueType = ep->valueTypes[type];
523                     else valueType = QQmlValueTypeFactory::valueType(type);
524                     Q_ASSERT(valueType);
525
526                     valueType->setValue(QVariant(type, a[0]));
527                     QMetaProperty valueProp = valueType->metaObject()->property(valueIndex);
528                     vi->write(valueProp.read(valueType));
529
530                     if (!ep) delete valueType;
531                     return -1;
532                 } else {
533                     vi->write(QVariant(type, a[0]));
534                     return -1;
535                 }
536             }
537         }
538     }
539     if (c == QMetaObject::ReadProperty || c == QMetaObject::WriteProperty || c == QMetaObject::ResetProperty) {
540         if (id >= propOffset) {
541             id -= propOffset;
542
543             if (id < metaData->propertyCount) {
544                int t = (metaData->propertyData() + id)->propertyType;
545                 bool needActivate = false;
546
547                 if (id >= firstVarPropertyIndex) {
548                     Q_ASSERT(t == QMetaType::QVariant);
549                     // the context can be null if accessing var properties from cpp after re-parenting an item.
550                     QQmlEnginePrivate *ep = (ctxt == 0 || ctxt->engine == 0) ? 0 : QQmlEnginePrivate::get(ctxt->engine);
551                     QV8Engine *v8e = (ep == 0) ? 0 : ep->v8engine();
552                     if (v8e) {
553                         v8::HandleScope handleScope;
554                         v8::Context::Scope contextScope(v8e->context());
555                         if (c == QMetaObject::ReadProperty) {
556                             *reinterpret_cast<QVariant *>(a[0]) = readPropertyAsVariant(id);
557                         } else if (c == QMetaObject::WriteProperty) {
558                             writeProperty(id, *reinterpret_cast<QVariant *>(a[0]));
559                         }
560                     } else if (c == QMetaObject::ReadProperty) {
561                         // if the context was disposed, we just return an invalid variant from read.
562                         *reinterpret_cast<QVariant *>(a[0]) = QVariant();
563                     }
564
565                 } else {
566
567                     if (c == QMetaObject::ReadProperty) {
568                         switch(t) {
569                         case QVariant::Int:
570                             *reinterpret_cast<int *>(a[0]) = data[id].asInt();
571                             break;
572                         case QVariant::Bool:
573                             *reinterpret_cast<bool *>(a[0]) = data[id].asBool();
574                             break;
575                         case QVariant::Double:
576                             *reinterpret_cast<double *>(a[0]) = data[id].asDouble();
577                             break;
578                         case QVariant::String:
579                             *reinterpret_cast<QString *>(a[0]) = data[id].asQString();
580                             break;
581                         case QVariant::Url:
582                             *reinterpret_cast<QUrl *>(a[0]) = data[id].asQUrl();
583                             break;
584                         case QVariant::Date:
585                             *reinterpret_cast<QDate *>(a[0]) = data[id].asQDate();
586                             break;
587                         case QVariant::DateTime:
588                             *reinterpret_cast<QDateTime *>(a[0]) = data[id].asQDateTime();
589                             break;
590                         case QMetaType::QObjectStar:
591                             *reinterpret_cast<QObject **>(a[0]) = data[id].asQObject();
592                             break;
593                         case QMetaType::QVariant:
594                             *reinterpret_cast<QVariant *>(a[0]) = readPropertyAsVariant(id);
595                             break;
596                         default:
597                             QQml_valueTypeProvider()->readValueType(data[id].dataType(), data[id].dataPtr(), t, a[0]);
598                             break;
599                         }
600                         if (t == qMetaTypeId<QQmlListProperty<QObject> >()) {
601                             int listIndex = data[id].asInt();
602                             const List *list = &listProperties.at(listIndex);
603                             *reinterpret_cast<QQmlListProperty<QObject> *>(a[0]) = 
604                                 QQmlListProperty<QObject>(object, (void *)list,
605                                                                   list_append, list_count, list_at, 
606                                                                   list_clear);
607                         }
608
609                     } else if (c == QMetaObject::WriteProperty) {
610
611                         switch(t) {
612                         case QVariant::Int:
613                             needActivate = *reinterpret_cast<int *>(a[0]) != data[id].asInt();
614                             data[id].setValue(*reinterpret_cast<int *>(a[0]));
615                             break;
616                         case QVariant::Bool:
617                             needActivate = *reinterpret_cast<bool *>(a[0]) != data[id].asBool();
618                             data[id].setValue(*reinterpret_cast<bool *>(a[0]));
619                             break;
620                         case QVariant::Double:
621                             needActivate = *reinterpret_cast<double *>(a[0]) != data[id].asDouble();
622                             data[id].setValue(*reinterpret_cast<double *>(a[0]));
623                             break;
624                         case QVariant::String:
625                             needActivate = *reinterpret_cast<QString *>(a[0]) != data[id].asQString();
626                             data[id].setValue(*reinterpret_cast<QString *>(a[0]));
627                             break;
628                         case QVariant::Url:
629                             needActivate = *reinterpret_cast<QUrl *>(a[0]) != data[id].asQUrl();
630                             data[id].setValue(*reinterpret_cast<QUrl *>(a[0]));
631                             break;
632                         case QVariant::Date:
633                             needActivate = *reinterpret_cast<QDate *>(a[0]) != data[id].asQDate();
634                             data[id].setValue(*reinterpret_cast<QDate *>(a[0]));
635                             break;
636                         case QVariant::DateTime:
637                             needActivate = *reinterpret_cast<QDateTime *>(a[0]) != data[id].asQDateTime();
638                             data[id].setValue(*reinterpret_cast<QDateTime *>(a[0]));
639                             break;
640                         case QMetaType::QObjectStar:
641                             needActivate = *reinterpret_cast<QObject **>(a[0]) != data[id].asQObject();
642                             data[id].setValue(*reinterpret_cast<QObject **>(a[0]), this, id);
643                             break;
644                         case QMetaType::QVariant:
645                             writeProperty(id, *reinterpret_cast<QVariant *>(a[0]));
646                             break;
647                         default:
648                             needActivate = QQml_valueTypeProvider()->writeValueType(t, a[0], data[id].dataPtr(), data[id].dataSize());
649                             if (needActivate) {
650                                 data[id].setDataType(t);
651                             }
652                             break;
653                         }
654                     }
655
656                 }
657
658                 if (c == QMetaObject::WriteProperty && needActivate) {
659                     activate(object, methodOffset + id, 0);
660                 }
661
662                 return -1;
663             }
664
665             id -= metaData->propertyCount;
666
667             if (id < metaData->aliasCount) {
668
669                 QQmlVMEMetaData::AliasData *d = metaData->aliasData() + id;
670
671                 if (d->flags & QML_ALIAS_FLAG_PTR && c == QMetaObject::ReadProperty) 
672                         *reinterpret_cast<void **>(a[0]) = 0;
673
674                 if (!ctxt) return -1;
675
676                 QQmlContext *context = ctxt->asQQmlContext();
677                 QQmlContextPrivate *ctxtPriv = QQmlContextPrivate::get(context);
678
679                 QObject *target = ctxtPriv->data->idValues[d->contextIdx].data();
680                 if (!target) 
681                     return -1;
682
683                 connectAlias(id);
684
685                 if (d->isObjectAlias()) {
686                     *reinterpret_cast<QObject **>(a[0]) = target;
687                     return -1;
688                 } 
689                 
690                 // Remove binding (if any) on write
691                 if(c == QMetaObject::WriteProperty) {
692                     int flags = *reinterpret_cast<int*>(a[3]);
693                     if (flags & QQmlPropertyPrivate::RemoveBindingOnAliasWrite) {
694                         QQmlData *targetData = QQmlData::get(target);
695                         if (targetData && targetData->hasBindingBit(d->propertyIndex())) {
696                             QQmlAbstractBinding *binding = QQmlPropertyPrivate::setBinding(target, d->propertyIndex(), d->isValueTypeAlias()?d->valueTypeIndex():-1, 0);
697                             if (binding) binding->destroy();
698                         }
699                     }
700                 }
701                 
702                 if (d->isValueTypeAlias()) {
703                     // Value type property
704                     QQmlEnginePrivate *ep = QQmlEnginePrivate::get(ctxt->engine);
705
706                     QQmlValueType *valueType = ep->valueTypes[d->valueType()];
707                     Q_ASSERT(valueType);
708
709                     valueType->read(target, d->propertyIndex());
710                     int rv = QMetaObject::metacall(valueType, c, d->valueTypeIndex(), a);
711                     
712                     if (c == QMetaObject::WriteProperty)
713                         valueType->write(target, d->propertyIndex(), 0x00);
714
715                     return rv;
716
717                 } else {
718                     return QMetaObject::metacall(target, c, d->propertyIndex(), a);
719                 }
720
721             }
722             return -1;
723
724         }
725
726     } else if(c == QMetaObject::InvokeMetaMethod) {
727
728         if (id >= methodOffset) {
729
730             id -= methodOffset;
731             int plainSignals = metaData->signalCount + metaData->propertyCount +
732                                metaData->aliasCount;
733             if (id < plainSignals) {
734                 QMetaObject::activate(object, _id, a);
735                 return -1;
736             }
737
738             id -= plainSignals;
739
740             if (id < metaData->methodCount) {
741                 if (!ctxt->engine)
742                     return -1; // We can't run the method
743
744                 QQmlEnginePrivate *ep = QQmlEnginePrivate::get(ctxt->engine);
745                 ep->referenceScarceResources(); // "hold" scarce resources in memory during evaluation.
746
747                 v8::Handle<v8::Function> function = method(id);
748                 if (function.IsEmpty()) {
749                     // The function was not compiled.  There are some exceptional cases which the
750                     // expression rewriter does not rewrite properly (e.g., \r-terminated lines
751                     // are not rewritten correctly but this bug is deemed out-of-scope to fix for
752                     // performance reasons; see QTBUG-24064) and thus compilation will have failed.
753                     QQmlError e;
754                     e.setDescription(QString(QLatin1String("Exception occurred during compilation of function: %1")).
755                                      arg(QLatin1String(QMetaObject::method(_id).methodSignature().constData())));
756                     ep->warning(e);
757                     return -1; // The dynamic method with that id is not available.
758                 }
759
760                 QQmlVMEMetaData::MethodData *data = metaData->methodData() + id;
761
762                 v8::HandleScope handle_scope;
763                 v8::Context::Scope scope(ep->v8engine()->context());
764                 v8::Handle<v8::Value> *args = 0;
765
766                 if (data->parameterCount) {
767                     args = new v8::Handle<v8::Value>[data->parameterCount];
768                     for (int ii = 0; ii < data->parameterCount; ++ii) 
769                         args[ii] = ep->v8engine()->fromVariant(*(QVariant *)a[ii + 1]);
770                 }
771
772                 v8::TryCatch try_catch;
773
774                 v8::Local<v8::Value> result = function->Call(ep->v8engine()->global(), data->parameterCount, args);
775
776                 QVariant rv;
777                 if (try_catch.HasCaught()) {
778                     QQmlError error;
779                     QQmlExpressionPrivate::exceptionToError(try_catch.Message(), error);
780                     if (error.isValid())
781                         ep->warning(error);
782                     if (a[0]) *(QVariant *)a[0] = QVariant();
783                 } else {
784                     if (a[0]) *(QVariant *)a[0] = ep->v8engine()->toVariant(result, 0);
785                 }
786
787                 ep->dereferenceScarceResources(); // "release" scarce resources if top-level expression evaluation is complete.
788                 return -1;
789             }
790             return -1;
791         }
792     }
793
794     if (parent)
795         return parent->metaCall(c, _id, a);
796     else
797         return object->qt_metacall(c, _id, a);
798 }
799
800 v8::Handle<v8::Function> QQmlVMEMetaObject::method(int index)
801 {
802     if (!v8methods) 
803         v8methods = new v8::Persistent<v8::Function>[metaData->methodCount];
804
805     if (v8methods[index].IsEmpty()) {
806         QQmlVMEMetaData::MethodData *data = metaData->methodData() + index;
807
808         const char *body = ((const char*)metaData) + data->bodyOffset;
809         int bodyLength = data->bodyLength;
810
811         // XXX We should evaluate all methods in a single big script block to 
812         // improve the call time between dynamic methods defined on the same
813         // object
814         v8methods[index] = QQmlExpressionPrivate::evalFunction(ctxt, object, body,
815                                                                        bodyLength,
816                                                                        ctxt->urlString,
817                                                                        data->lineNumber);
818     }
819
820     return v8methods[index];
821 }
822
823 v8::Handle<v8::Value> QQmlVMEMetaObject::readVarProperty(int id)
824 {
825     Q_ASSERT(id >= firstVarPropertyIndex);
826
827     if (ensureVarPropertiesAllocated())
828         return varProperties->Get(id - firstVarPropertyIndex);
829     return v8::Handle<v8::Value>();
830 }
831
832 QVariant QQmlVMEMetaObject::readPropertyAsVariant(int id)
833 {
834     if (id >= firstVarPropertyIndex) {
835         if (ensureVarPropertiesAllocated())
836             return QQmlEnginePrivate::get(ctxt->engine)->v8engine()->toVariant(varProperties->Get(id - firstVarPropertyIndex), -1);
837         return QVariant();
838     } else {
839         if (data[id].dataType() == QMetaType::QObjectStar) {
840             return QVariant::fromValue(data[id].asQObject());
841         } else {
842             return data[id].asQVariant();
843         }
844     }
845 }
846
847 void QQmlVMEMetaObject::writeVarProperty(int id, v8::Handle<v8::Value> value)
848 {
849     Q_ASSERT(id >= firstVarPropertyIndex);
850     if (!ensureVarPropertiesAllocated())
851         return;
852
853     // Importantly, if the current value is a scarce resource, we need to ensure that it
854     // gets automatically released by the engine if no other references to it exist.
855     v8::Local<v8::Value> oldv = varProperties->Get(id - firstVarPropertyIndex);
856     if (oldv->IsObject()) {
857         QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(oldv));
858         if (r) {
859             r->removeVmePropertyReference();
860         }
861     }
862
863     // And, if the new value is a scarce resource, we need to ensure that it does not get
864     // automatically released by the engine until no other references to it exist.
865     if (value->IsObject()) {
866         QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(value));
867         if (r) {
868             r->addVmePropertyReference();
869         }
870     }
871
872     // Write the value and emit change signal as appropriate.
873     varProperties->Set(id - firstVarPropertyIndex, value);
874     activate(object, methodOffset + id, 0);
875 }
876
877 void QQmlVMEMetaObject::writeProperty(int id, const QVariant &value)
878 {
879     if (id >= firstVarPropertyIndex) {
880         if (!ensureVarPropertiesAllocated())
881             return;
882
883         // Importantly, if the current value is a scarce resource, we need to ensure that it
884         // gets automatically released by the engine if no other references to it exist.
885         v8::Local<v8::Value> oldv = varProperties->Get(id - firstVarPropertyIndex);
886         if (oldv->IsObject()) {
887             QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(oldv));
888             if (r) {
889                 r->removeVmePropertyReference();
890             }
891         }
892
893         // And, if the new value is a scarce resource, we need to ensure that it does not get
894         // automatically released by the engine until no other references to it exist.
895         v8::Handle<v8::Value> newv = QQmlEnginePrivate::get(ctxt->engine)->v8engine()->fromVariant(value);
896         if (newv->IsObject()) {
897             QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(newv));
898             if (r) {
899                 r->addVmePropertyReference();
900             }
901         }
902
903         // Write the value and emit change signal as appropriate.
904         QVariant currentValue = readPropertyAsVariant(id);
905         varProperties->Set(id - firstVarPropertyIndex, newv);
906         if ((currentValue.userType() != value.userType() || currentValue != value))
907             activate(object, methodOffset + id, 0);
908     } else {
909         bool needActivate = false;
910         if (value.userType() == QMetaType::QObjectStar) {
911             QObject *o = qvariant_cast<QObject *>(value);
912             needActivate = (data[id].dataType() != QMetaType::QObjectStar || data[id].asQObject() != o);
913             data[id].setValue(qvariant_cast<QObject *>(value), this, id);
914         } else {
915             needActivate = (data[id].dataType() != qMetaTypeId<QVariant>() ||
916                             data[id].asQVariant().userType() != value.userType() ||
917                             data[id].asQVariant() != value);
918             data[id].setValue(value);
919         }
920
921         if (needActivate)
922             activate(object, methodOffset + id, 0);
923     }
924 }
925
926 void QQmlVMEMetaObject::listChanged(int id)
927 {
928     activate(object, methodOffset + id, 0);
929 }
930
931 void QQmlVMEMetaObject::list_append(QQmlListProperty<QObject> *prop, QObject *o)
932 {
933     List *list = static_cast<List *>(prop->data);
934     list->append(o);
935     QMetaObject::activate(prop->object, list->notifyIndex, 0);
936 }
937
938 int QQmlVMEMetaObject::list_count(QQmlListProperty<QObject> *prop)
939 {
940     return static_cast<List *>(prop->data)->count();
941 }
942
943 QObject *QQmlVMEMetaObject::list_at(QQmlListProperty<QObject> *prop, int index)
944 {
945     return static_cast<List *>(prop->data)->at(index);
946 }
947
948 void QQmlVMEMetaObject::list_clear(QQmlListProperty<QObject> *prop)
949 {
950     List *list = static_cast<List *>(prop->data);
951     list->clear();
952     QMetaObject::activate(prop->object, list->notifyIndex, 0);
953 }
954
955 void QQmlVMEMetaObject::registerInterceptor(int index, int valueIndex, QQmlPropertyValueInterceptor *interceptor)
956 {
957     if (aInterceptors.isEmpty())
958         aInterceptors.resize(propertyCount() + metaData->propertyCount);
959     aInterceptors.setBit(index);
960     interceptors.insert(index, qMakePair(valueIndex, interceptor));
961 }
962
963 int QQmlVMEMetaObject::vmeMethodLineNumber(int index)
964 {
965     if (index < methodOffset) {
966         Q_ASSERT(parent);
967         return static_cast<QQmlVMEMetaObject *>(parent)->vmeMethodLineNumber(index);
968     }
969
970     int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
971     Q_ASSERT(index >= (methodOffset + plainSignals) && index < (methodOffset + plainSignals + metaData->methodCount));
972
973     int rawIndex = index - methodOffset - plainSignals;
974
975     QQmlVMEMetaData::MethodData *data = metaData->methodData() + rawIndex;
976     return data->lineNumber;
977 }
978
979 v8::Handle<v8::Function> QQmlVMEMetaObject::vmeMethod(int index)
980 {
981     if (index < methodOffset) {
982         Q_ASSERT(parent);
983         return static_cast<QQmlVMEMetaObject *>(parent)->vmeMethod(index);
984     }
985     int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
986     Q_ASSERT(index >= (methodOffset + plainSignals) && index < (methodOffset + plainSignals + metaData->methodCount));
987     return method(index - methodOffset - plainSignals);
988 }
989
990 // Used by debugger
991 void QQmlVMEMetaObject::setVmeMethod(int index, v8::Persistent<v8::Function> value)
992 {
993     if (index < methodOffset) {
994         Q_ASSERT(parent);
995         return static_cast<QQmlVMEMetaObject *>(parent)->setVmeMethod(index, value);
996     }
997     int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
998     Q_ASSERT(index >= (methodOffset + plainSignals) && index < (methodOffset + plainSignals + metaData->methodCount));
999
1000     if (!v8methods) 
1001         v8methods = new v8::Persistent<v8::Function>[metaData->methodCount];
1002
1003     int methodIndex = index - methodOffset - plainSignals;
1004     if (!v8methods[methodIndex].IsEmpty()) 
1005         qPersistentDispose(v8methods[methodIndex]);
1006     v8methods[methodIndex] = value;
1007 }
1008
1009 v8::Handle<v8::Value> QQmlVMEMetaObject::vmeProperty(int index)
1010 {
1011     if (index < propOffset) {
1012         Q_ASSERT(parent);
1013         return static_cast<QQmlVMEMetaObject *>(parent)->vmeProperty(index);
1014     }
1015     return readVarProperty(index - propOffset);
1016 }
1017
1018 void QQmlVMEMetaObject::setVMEProperty(int index, v8::Handle<v8::Value> v)
1019 {
1020     if (index < propOffset) {
1021         Q_ASSERT(parent);
1022         static_cast<QQmlVMEMetaObject *>(parent)->setVMEProperty(index, v);
1023         return;
1024     }
1025     return writeVarProperty(index - propOffset, v);
1026 }
1027
1028 bool QQmlVMEMetaObject::ensureVarPropertiesAllocated()
1029 {
1030     if (!varPropertiesInitialized)
1031         allocateVarPropertiesArray();
1032
1033     // in some situations, the QObject's v8object (and associated v8 data,
1034     // such as the varProperties array) will have been cleaned up, but the
1035     // QObject ptr will not yet have been deleted (eg, waiting on deleteLater).
1036     // In this situation, the varProperties handle will be (and should remain)
1037     // empty.
1038     return !varProperties.IsEmpty();
1039 }
1040
1041 // see also: QV8GCCallback::garbageCollectorPrologueCallback()
1042 void QQmlVMEMetaObject::allocateVarPropertiesArray()
1043 {
1044     v8::HandleScope handleScope;
1045     v8::Context::Scope cs(QQmlEnginePrivate::get(ctxt->engine)->v8engine()->context());
1046     varProperties = qPersistentNew(v8::Array::New(metaData->varPropertyCount));
1047     varProperties.MakeWeak(static_cast<void*>(this), VarPropertiesWeakReferenceCallback);
1048     varPropertiesInitialized = true;
1049 }
1050
1051 /*
1052    The "var" properties are stored in a v8::Array which will be strong persistent if the object has cpp-ownership
1053    and the root QObject in the parent chain does not have JS-ownership.  In the weak persistent handle case,
1054    this callback will dispose the handle when the v8object which owns the lifetime of the var properties array
1055    is cleared as a result of all other handles to that v8object being released.
1056    See QV8GCCallback::garbageCollectorPrologueCallback() for more information.
1057  */
1058 void QQmlVMEMetaObject::VarPropertiesWeakReferenceCallback(v8::Persistent<v8::Value> object, void* parameter)
1059 {
1060     QQmlVMEMetaObject *vmemo = static_cast<QQmlVMEMetaObject*>(parameter);
1061     Q_ASSERT(vmemo);
1062     qPersistentDispose(object);
1063     vmemo->varProperties.Clear();
1064 }
1065
1066 void QQmlVMEMetaObject::GcPrologueCallback(QV8GCCallback::Node *node)
1067 {
1068     QQmlVMEMetaObject *vmemo = static_cast<QQmlVMEMetaObject*>(node);
1069     Q_ASSERT(vmemo);
1070     if (!vmemo->varPropertiesInitialized || vmemo->varProperties.IsEmpty() || !vmemo->ctxt || !vmemo->ctxt->engine)
1071         return;
1072     QQmlEnginePrivate *ep = QQmlEnginePrivate::get(vmemo->ctxt->engine);
1073     ep->v8engine()->addRelationshipForGC(vmemo->object, vmemo->varProperties);
1074 }
1075
1076 bool QQmlVMEMetaObject::aliasTarget(int index, QObject **target, int *coreIndex, int *valueTypeIndex) const
1077 {
1078     Q_ASSERT(index >= propOffset + metaData->propertyCount);
1079
1080     *target = 0;
1081     *coreIndex = -1;
1082     *valueTypeIndex = -1;
1083
1084     if (!ctxt)
1085         return false;
1086
1087     QQmlVMEMetaData::AliasData *d = metaData->aliasData() + (index - propOffset - metaData->propertyCount);
1088     QQmlContext *context = ctxt->asQQmlContext();
1089     QQmlContextPrivate *ctxtPriv = QQmlContextPrivate::get(context);
1090
1091     *target = ctxtPriv->data->idValues[d->contextIdx].data();
1092     if (!*target)
1093         return false;
1094
1095     if (d->isObjectAlias()) {
1096     } else if (d->isValueTypeAlias()) {
1097         *coreIndex = d->propertyIndex();
1098         *valueTypeIndex = d->valueTypeIndex();
1099     } else {
1100         *coreIndex = d->propertyIndex();
1101     }
1102
1103     return true;
1104 }
1105
1106 void QQmlVMEMetaObject::connectAlias(int aliasId)
1107 {
1108     if (!aConnected.testBit(aliasId)) {
1109
1110         if (!aliasEndpoints)
1111             aliasEndpoints = new QQmlVMEMetaObjectEndpoint[metaData->aliasCount];
1112
1113         aConnected.setBit(aliasId);
1114
1115         QQmlVMEMetaData::AliasData *d = metaData->aliasData() + aliasId;
1116
1117         QQmlVMEMetaObjectEndpoint *endpoint = aliasEndpoints + aliasId;
1118         endpoint->metaObject = this;
1119
1120         endpoint->connect(&ctxt->idValues[d->contextIdx].bindings);
1121
1122         endpoint->tryConnect();
1123     }
1124 }
1125
1126 void QQmlVMEMetaObject::connectAliasSignal(int index)
1127 {
1128     int aliasId = (index - methodOffset) - metaData->propertyCount;
1129     if (aliasId < 0 || aliasId >= metaData->aliasCount)
1130         return;
1131
1132     connectAlias(aliasId);
1133 }
1134
1135 QT_END_NAMESPACE