Implement strict mode for qmldir modules
[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(bool isVar)
61     : QQmlGuard<QObject>(0), m_target(0), m_isVar(isVar), m_index(-1)
62 {
63 }
64
65 QQmlVMEVariantQObjectPtr::~QQmlVMEVariantQObjectPtr()
66 {
67 }
68
69 void QQmlVMEVariantQObjectPtr::objectDestroyed(QObject *o)
70 {
71     if (m_target && m_index >= 0) {
72         if (m_isVar && m_target->varPropertiesInitialized && !m_target->varProperties.IsEmpty()) {
73             // Set the var property to NULL
74             m_target->varProperties->Set(m_index - m_target->firstVarPropertyIndex, v8::Null());
75         }
76
77         m_target->activate(m_target->object, m_target->methodOffset() + m_index, 0);
78     }
79 }
80
81 void QQmlVMEVariantQObjectPtr::setGuardedValue(QObject *obj, QQmlVMEMetaObject *target, int index)
82 {
83     m_target = target;
84     m_index = index;
85     setObject(obj);
86 }
87
88 class QQmlVMEVariant
89 {
90 public:
91     inline QQmlVMEVariant();
92     inline ~QQmlVMEVariant();
93
94     inline const void *dataPtr() const;
95     inline void *dataPtr();
96     inline int dataType() const;
97     inline size_t dataSize() const;
98
99     inline QObject *asQObject();
100     inline const QVariant &asQVariant();
101     inline int asInt();
102     inline bool asBool();
103     inline double asDouble();
104     inline const QString &asQString();
105     inline const QUrl &asQUrl();
106     inline const QTime &asQTime();
107     inline const QDate &asQDate();
108     inline const QDateTime &asQDateTime();
109     inline const QRectF &asQRectF();
110     inline const QPointF &asQPointF();
111     inline const QSizeF &asQSizeF();
112     inline const QJSValue &asQJSValue();
113
114     inline void setValue(QObject *v, QQmlVMEMetaObject *target, int index);
115     inline void setValue(const QVariant &);
116     inline void setValue(int);
117     inline void setValue(bool);
118     inline void setValue(double);
119     inline void setValue(const QString &);
120     inline void setValue(const QUrl &);
121     inline void setValue(const QTime &);
122     inline void setValue(const QDate &);
123     inline void setValue(const QDateTime &);
124     inline void setValue(const QRectF &);
125     inline void setValue(const QPointF &);
126     inline void setValue(const QSizeF &);
127     inline void setValue(const QJSValue &);
128
129     inline void setDataType(int t);
130
131     inline void ensureValueType(int);
132
133 private:
134     int type;
135     void *data[8]; // Large enough to hold all types
136
137     inline void cleanup();
138 };
139
140 class QQmlVMEMetaObjectEndpoint : public QQmlNotifierEndpoint
141 {
142 public:
143     QQmlVMEMetaObjectEndpoint();
144     static void vmecallback(QQmlNotifierEndpoint *, void **);
145     void tryConnect();
146
147     QFlagPointer<QQmlVMEMetaObject> metaObject;
148 };
149
150
151 QQmlVMEVariant::QQmlVMEVariant()
152 : type(QVariant::Invalid)
153 {
154 }
155
156 QQmlVMEVariant::~QQmlVMEVariant()
157 {
158     cleanup();
159 }
160
161 void QQmlVMEVariant::cleanup()
162 {
163     if (type == QVariant::Invalid) {
164     } else if (type == QMetaType::Int ||
165                type == QMetaType::Bool ||
166                type == QMetaType::Double) {
167         type = QVariant::Invalid;
168     } else if (type == QMetaType::QObjectStar) {
169         ((QQmlVMEVariantQObjectPtr*)dataPtr())->~QQmlVMEVariantQObjectPtr();
170         type = QVariant::Invalid;
171     } else if (type == QMetaType::QString) {
172         ((QString *)dataPtr())->~QString();
173         type = QVariant::Invalid;
174     } else if (type == QMetaType::QUrl) {
175         ((QUrl *)dataPtr())->~QUrl();
176         type = QVariant::Invalid;
177     } else if (type == QMetaType::QTime) {
178         ((QTime *)dataPtr())->~QTime();
179         type = QVariant::Invalid;
180     } else if (type == QMetaType::QDate) {
181         ((QDate *)dataPtr())->~QDate();
182         type = QVariant::Invalid;
183     } else if (type == QMetaType::QDateTime) {
184         ((QDateTime *)dataPtr())->~QDateTime();
185         type = QVariant::Invalid;
186     } else if (type == QMetaType::QRectF) {
187         ((QRectF *)dataPtr())->~QRectF();
188         type = QVariant::Invalid;
189     } else if (type == QMetaType::QPointF) {
190         ((QPointF *)dataPtr())->~QPointF();
191         type = QVariant::Invalid;
192     } else if (type == QMetaType::QSizeF) {
193         ((QSizeF *)dataPtr())->~QSizeF();
194         type = QVariant::Invalid;
195     } else if (type == qMetaTypeId<QVariant>()) {
196         ((QVariant *)dataPtr())->~QVariant();
197         type = QVariant::Invalid;
198     } else if (type == qMetaTypeId<QJSValue>()) {
199         ((QJSValue *)dataPtr())->~QJSValue();
200         type = QVariant::Invalid;
201     } else {
202         if (QQml_valueTypeProvider()->destroyValueType(type, dataPtr(), dataSize())) {
203             type = QVariant::Invalid;
204         }
205     }
206 }
207
208 int QQmlVMEVariant::dataType() const
209 {
210     return type;
211 }
212
213 const void *QQmlVMEVariant::dataPtr() const
214 {
215     return &data;
216 }
217
218 void *QQmlVMEVariant::dataPtr() 
219 {
220     return &data;
221 }
222
223 size_t QQmlVMEVariant::dataSize() const
224 {
225     return sizeof(data);
226 }
227
228 QObject *QQmlVMEVariant::asQObject() 
229 {
230     if (type != QMetaType::QObjectStar)
231         setValue((QObject *)0, 0, -1);
232
233     return *(QQmlGuard<QObject> *)(dataPtr());
234 }
235
236 const QVariant &QQmlVMEVariant::asQVariant() 
237 {
238     if (type != QMetaType::QVariant)
239         setValue(QVariant());
240
241     return *(QVariant *)(dataPtr());
242 }
243
244 int QQmlVMEVariant::asInt() 
245 {
246     if (type != QMetaType::Int)
247         setValue(int(0));
248
249     return *(int *)(dataPtr());
250 }
251
252 bool QQmlVMEVariant::asBool() 
253 {
254     if (type != QMetaType::Bool)
255         setValue(bool(false));
256
257     return *(bool *)(dataPtr());
258 }
259
260 double QQmlVMEVariant::asDouble() 
261 {
262     if (type != QMetaType::Double)
263         setValue(double(0));
264
265     return *(double *)(dataPtr());
266 }
267
268 const QString &QQmlVMEVariant::asQString() 
269 {
270     if (type != QMetaType::QString)
271         setValue(QString());
272
273     return *(QString *)(dataPtr());
274 }
275
276 const QUrl &QQmlVMEVariant::asQUrl() 
277 {
278     if (type != QMetaType::QUrl)
279         setValue(QUrl());
280
281     return *(QUrl *)(dataPtr());
282 }
283
284 const QTime &QQmlVMEVariant::asQTime() 
285 {
286     if (type != QMetaType::QTime)
287         setValue(QTime());
288
289     return *(QTime *)(dataPtr());
290 }
291
292 const QDate &QQmlVMEVariant::asQDate() 
293 {
294     if (type != QMetaType::QDate)
295         setValue(QDate());
296
297     return *(QDate *)(dataPtr());
298 }
299
300 const QDateTime &QQmlVMEVariant::asQDateTime() 
301 {
302     if (type != QMetaType::QDateTime)
303         setValue(QDateTime());
304
305     return *(QDateTime *)(dataPtr());
306 }
307
308 const QRectF &QQmlVMEVariant::asQRectF()
309 {
310     if (type != QMetaType::QRectF)
311         setValue(QRectF());
312
313     return *(QRectF *)(dataPtr());
314 }
315
316 const QSizeF &QQmlVMEVariant::asQSizeF()
317 {
318     if (type != QMetaType::QSizeF)
319         setValue(QSizeF());
320
321     return *(QSizeF *)(dataPtr());
322 }
323
324 const QPointF &QQmlVMEVariant::asQPointF()
325 {
326     if (type != QMetaType::QPointF)
327         setValue(QPointF());
328
329     return *(QPointF *)(dataPtr());
330 }
331
332 const QJSValue &QQmlVMEVariant::asQJSValue()
333 {
334     if (type != qMetaTypeId<QJSValue>())
335         setValue(QJSValue());
336
337     return *(QJSValue *)(dataPtr());
338 }
339
340 void QQmlVMEVariant::setValue(QObject *v, QQmlVMEMetaObject *target, int index)
341 {
342     if (type != QMetaType::QObjectStar) {
343         cleanup();
344         type = QMetaType::QObjectStar;
345         new (dataPtr()) QQmlVMEVariantQObjectPtr(false);
346     }
347     reinterpret_cast<QQmlVMEVariantQObjectPtr*>(dataPtr())->setGuardedValue(v, target, index);
348 }
349
350 void QQmlVMEVariant::setValue(const QVariant &v)
351 {
352     if (type != qMetaTypeId<QVariant>()) {
353         cleanup();
354         type = qMetaTypeId<QVariant>();
355         new (dataPtr()) QVariant(v);
356     } else {
357         *(QVariant *)(dataPtr()) = v;
358     }
359 }
360
361 void QQmlVMEVariant::setValue(int v)
362 {
363     if (type != QMetaType::Int) {
364         cleanup();
365         type = QMetaType::Int;
366     }
367     *(int *)(dataPtr()) = v;
368 }
369
370 void QQmlVMEVariant::setValue(bool v)
371 {
372     if (type != QMetaType::Bool) {
373         cleanup();
374         type = QMetaType::Bool;
375     }
376     *(bool *)(dataPtr()) = v;
377 }
378
379 void QQmlVMEVariant::setValue(double v)
380 {
381     if (type != QMetaType::Double) {
382         cleanup();
383         type = QMetaType::Double;
384     }
385     *(double *)(dataPtr()) = v;
386 }
387
388 void QQmlVMEVariant::setValue(const QString &v)
389 {
390     if (type != QMetaType::QString) {
391         cleanup();
392         type = QMetaType::QString;
393         new (dataPtr()) QString(v);
394     } else {
395         *(QString *)(dataPtr()) = v;
396     }
397 }
398
399 void QQmlVMEVariant::setValue(const QUrl &v)
400 {
401     if (type != QMetaType::QUrl) {
402         cleanup();
403         type = QMetaType::QUrl;
404         new (dataPtr()) QUrl(v);
405     } else {
406         *(QUrl *)(dataPtr()) = v;
407     }
408 }
409
410 void QQmlVMEVariant::setValue(const QTime &v)
411 {
412     if (type != QMetaType::QTime) {
413         cleanup();
414         type = QMetaType::QTime;
415         new (dataPtr()) QTime(v);
416     } else {
417         *(QTime *)(dataPtr()) = v;
418     }
419 }
420
421 void QQmlVMEVariant::setValue(const QDate &v)
422 {
423     if (type != QMetaType::QDate) {
424         cleanup();
425         type = QMetaType::QDate;
426         new (dataPtr()) QDate(v);
427     } else {
428         *(QDate *)(dataPtr()) = v;
429     }
430 }
431
432 void QQmlVMEVariant::setValue(const QDateTime &v)
433 {
434     if (type != QMetaType::QDateTime) {
435         cleanup();
436         type = QMetaType::QDateTime;
437         new (dataPtr()) QDateTime(v);
438     } else {
439         *(QDateTime *)(dataPtr()) = v;
440     }
441 }
442
443 void QQmlVMEVariant::setValue(const QRectF &v)
444 {
445     if (type != QMetaType::QRectF) {
446         cleanup();
447         type = QMetaType::QRectF;
448         new (dataPtr()) QRectF(v);
449     } else {
450         *(QRectF *)(dataPtr()) = v;
451     }
452 }
453
454 void QQmlVMEVariant::setValue(const QPointF &v)
455 {
456     if (type != QMetaType::QPointF) {
457         cleanup();
458         type = QMetaType::QPointF;
459         new (dataPtr()) QPointF(v);
460     } else {
461         *(QPointF *)(dataPtr()) = v;
462     }
463 }
464
465 void QQmlVMEVariant::setValue(const QSizeF &v)
466 {
467     if (type != QMetaType::QSizeF) {
468         cleanup();
469         type = QMetaType::QSizeF;
470         new (dataPtr()) QSizeF(v);
471     } else {
472         *(QSizeF *)(dataPtr()) = v;
473     }
474 }
475
476 void QQmlVMEVariant::setValue(const QJSValue &v)
477 {
478     if (type != qMetaTypeId<QJSValue>()) {
479         cleanup();
480         type = qMetaTypeId<QJSValue>();
481         new (dataPtr()) QJSValue(v);
482     } else {
483         *(QJSValue *)(dataPtr()) = v;
484     }
485 }
486
487 void QQmlVMEVariant::setDataType(int t)
488 {
489     type = t;
490 }
491
492 void QQmlVMEVariant::ensureValueType(int t)
493 {
494     if (type != t) {
495         cleanup();
496         type = t;
497         QQml_valueTypeProvider()->initValueType(t, dataPtr(), dataSize());
498     }
499 }
500
501 QQmlVMEMetaObjectEndpoint::QQmlVMEMetaObjectEndpoint()
502 {
503     setCallback(QQmlNotifierEndpoint::QQmlVMEMetaObjectEndpoint);
504 }
505
506 void QQmlVMEMetaObjectEndpoint_callback(QQmlNotifierEndpoint *e, void **)
507 {
508     QQmlVMEMetaObjectEndpoint *vmee = static_cast<QQmlVMEMetaObjectEndpoint*>(e);
509     vmee->tryConnect();
510 }
511
512 void QQmlVMEMetaObjectEndpoint::tryConnect()
513 {
514     int aliasId = this - metaObject->aliasEndpoints;
515
516     if (metaObject.flag()) {
517         // This is actually notify
518         int sigIdx = metaObject->methodOffset() + aliasId + metaObject->metaData->propertyCount;
519         metaObject->activate(metaObject->object, sigIdx, 0);
520     } else {
521         QQmlVMEMetaData::AliasData *d = metaObject->metaData->aliasData() + aliasId;
522         if (!d->isObjectAlias()) {
523             QQmlContextData *ctxt = metaObject->ctxt;
524             QObject *target = ctxt->idValues[d->contextIdx].data();
525             if (!target)
526                 return;
527
528             if (d->notifySignal != -1)
529                 connect(target, d->notifySignal, ctxt->engine);
530         }
531
532         metaObject.setFlag();
533     }
534 }
535
536 QAbstractDynamicMetaObject *QQmlVMEMetaObject::toDynamicMetaObject(QObject *o)
537 {
538     if (!hasAssignedMetaObjectData) {
539         *static_cast<QMetaObject *>(this) = *cache->createMetaObject();
540
541         if (parent.isT1())
542             this->d.superdata = parent.asT1()->toDynamicMetaObject(o);
543         else
544             this->d.superdata = parent.asT2();
545
546         hasAssignedMetaObjectData = true;
547     }
548
549     return this;
550 }
551
552 QQmlVMEMetaObject::QQmlVMEMetaObject(QObject *obj,
553                                      QQmlPropertyCache *cache,
554                                      const QQmlVMEMetaData *meta)
555 : QV8GCCallback::Node(GcPrologueCallback), object(obj),
556   ctxt(QQmlData::get(obj, true)->outerContext), cache(cache), metaData(meta),
557   hasAssignedMetaObjectData(false), data(0), aliasEndpoints(0), firstVarPropertyIndex(-1),
558   varPropertiesInitialized(false), interceptors(0), v8methods(0)
559 {
560     QObjectPrivate *op = QObjectPrivate::get(obj);
561
562     if (op->metaObject) parent = op->metaObject;
563     else parent = obj->metaObject();
564
565     op->metaObject = this;
566     QQmlData::get(obj)->hasVMEMetaObject = true;
567
568     data = new QQmlVMEVariant[metaData->propertyCount - metaData->varPropertyCount];
569
570     aConnected.resize(metaData->aliasCount);
571     int list_type = qMetaTypeId<QQmlListProperty<QObject> >();
572     int qobject_type = qMetaTypeId<QObject*>();
573     int variant_type = qMetaTypeId<QVariant>();
574     bool needsGcCallback = (metaData->varPropertyCount > 0);
575
576     // ### Optimize
577     for (int ii = 0; ii < metaData->propertyCount - metaData->varPropertyCount; ++ii) {
578         int t = (metaData->propertyData() + ii)->propertyType;
579         if (t == list_type) {
580             listProperties.append(List(methodOffset() + ii, this));
581             data[ii].setValue(listProperties.count() - 1);
582         } else if (!needsGcCallback && (t == qobject_type || t == variant_type)) {
583             needsGcCallback = true;
584         }
585     }
586
587     firstVarPropertyIndex = metaData->propertyCount - metaData->varPropertyCount;
588
589     // both var properties and variant properties can keep references to
590     // other QObjects, and var properties can also keep references to
591     // JavaScript objects.  If we have any properties, we need to hook
592     // the gc() to ensure that references keep objects alive as needed.
593     if (needsGcCallback) {
594         QV8GCCallback::addGcCallbackNode(this);
595     }
596 }
597
598 QQmlVMEMetaObject::~QQmlVMEMetaObject()
599 {
600     if (parent.isT1()) parent.asT1()->objectDestroyed(object);
601     delete [] data;
602     delete [] aliasEndpoints;
603
604     for (int ii = 0; v8methods && ii < metaData->methodCount; ++ii) {
605         qPersistentDispose(v8methods[ii]);
606     }
607     delete [] v8methods;
608
609     if (metaData->varPropertyCount)
610         qPersistentDispose(varProperties); // if not weak, will not have been cleaned up by the callback.
611
612     qDeleteAll(varObjectGuards);
613 }
614
615 int QQmlVMEMetaObject::metaCall(QMetaObject::Call c, int _id, void **a)
616 {
617     int id = _id;
618     if (c == QMetaObject::WriteProperty && interceptors &&
619        !(*reinterpret_cast<int*>(a[3]) & QQmlPropertyPrivate::BypassInterceptor)) {
620
621         for (QQmlPropertyValueInterceptor *vi = interceptors; vi; vi = vi->m_next) {
622             if (vi->m_coreIndex != id)
623                 continue;
624
625             int valueIndex = vi->m_valueTypeCoreIndex;
626             int type = QQmlData::get(object)->propertyCache->property(id)->propType;
627
628             if (type != QVariant::Invalid) {
629                 if (valueIndex != -1) {
630                     QQmlEnginePrivate *ep = ctxt?QQmlEnginePrivate::get(ctxt->engine):0;
631                     QQmlValueType *valueType = 0;
632                     if (ep) valueType = ep->valueTypes[type];
633                     else valueType = QQmlValueTypeFactory::valueType(type);
634                     Q_ASSERT(valueType);
635
636                     //
637                     // Consider the following case:
638                     //  color c = { 0.1, 0.2, 0.3 }
639                     //  interceptor exists on c.r
640                     //  write { 0.2, 0.4, 0.6 }
641                     //
642                     // The interceptor may choose not to update the r component at this
643                     // point (for example, a behavior that creates an animation). But we
644                     // need to ensure that the g and b components are updated correctly.
645                     //
646                     // So we need to perform a full write where the value type is:
647                     //    r = old value, g = new value, b = new value
648                     //
649                     // And then call the interceptor which may or may not write the
650                     // new value to the r component.
651                     //
652                     // This will ensure that the other components don't contain stale data
653                     // and any relevant signals are emitted.
654                     //
655                     // To achieve this:
656                     //   (1) Store the new value type as a whole (needed due to
657                     //       aliasing between a[0] and static storage in value type).
658                     //   (2) Read the entire existing value type from object -> valueType temp.
659                     //   (3) Read the previous value of the component being changed
660                     //       from the valueType temp.
661                     //   (4) Write the entire new value type into the temp.
662                     //   (5) Overwrite the component being changed with the old value.
663                     //   (6) Perform a full write to the value type (which may emit signals etc).
664                     //   (7) Issue the interceptor call with the new component value.
665                     //
666
667                     QMetaProperty valueProp = valueType->metaObject()->property(valueIndex);
668                     QVariant newValue(type, a[0]);
669
670                     valueType->read(object, id);
671                     QVariant prevComponentValue = valueProp.read(valueType);
672
673                     valueType->setValue(newValue);
674                     QVariant newComponentValue = valueProp.read(valueType);
675
676                     // Don't apply the interceptor if the intercepted value has not changed
677                     bool updated = false;
678                     if (newComponentValue != prevComponentValue) {
679                         valueProp.write(valueType, prevComponentValue);
680                         valueType->write(object, id, QQmlPropertyPrivate::DontRemoveBinding | QQmlPropertyPrivate::BypassInterceptor);
681
682                         vi->write(newComponentValue);
683                         updated = true;
684                     }
685
686                     if (!ep)
687                         delete valueType;
688
689                     if (updated)
690                         return -1;
691                 } else {
692                     vi->write(QVariant(type, a[0]));
693                     return -1;
694                 }
695             }
696         }
697     }
698     if (c == QMetaObject::ReadProperty || c == QMetaObject::WriteProperty || c == QMetaObject::ResetProperty) {
699         if (id >= propOffset()) {
700             id -= propOffset();
701
702             if (id < metaData->propertyCount) {
703                int t = (metaData->propertyData() + id)->propertyType;
704                 bool needActivate = false;
705
706                 if (id >= firstVarPropertyIndex) {
707                     Q_ASSERT(t == QMetaType::QVariant);
708                     // the context can be null if accessing var properties from cpp after re-parenting an item.
709                     QQmlEnginePrivate *ep = (ctxt == 0 || ctxt->engine == 0) ? 0 : QQmlEnginePrivate::get(ctxt->engine);
710                     QV8Engine *v8e = (ep == 0) ? 0 : ep->v8engine();
711                     if (v8e) {
712                         v8::HandleScope handleScope;
713                         v8::Context::Scope contextScope(v8e->context());
714                         if (c == QMetaObject::ReadProperty) {
715                             *reinterpret_cast<QVariant *>(a[0]) = readPropertyAsVariant(id);
716                         } else if (c == QMetaObject::WriteProperty) {
717                             writeProperty(id, *reinterpret_cast<QVariant *>(a[0]));
718                         }
719                     } else if (c == QMetaObject::ReadProperty) {
720                         // if the context was disposed, we just return an invalid variant from read.
721                         *reinterpret_cast<QVariant *>(a[0]) = QVariant();
722                     }
723
724                 } else {
725
726                     if (c == QMetaObject::ReadProperty) {
727                         switch(t) {
728                         case QVariant::Int:
729                             *reinterpret_cast<int *>(a[0]) = data[id].asInt();
730                             break;
731                         case QVariant::Bool:
732                             *reinterpret_cast<bool *>(a[0]) = data[id].asBool();
733                             break;
734                         case QVariant::Double:
735                             *reinterpret_cast<double *>(a[0]) = data[id].asDouble();
736                             break;
737                         case QVariant::String:
738                             *reinterpret_cast<QString *>(a[0]) = data[id].asQString();
739                             break;
740                         case QVariant::Url:
741                             *reinterpret_cast<QUrl *>(a[0]) = data[id].asQUrl();
742                             break;
743                         case QVariant::Date:
744                             *reinterpret_cast<QDate *>(a[0]) = data[id].asQDate();
745                             break;
746                         case QVariant::DateTime:
747                             *reinterpret_cast<QDateTime *>(a[0]) = data[id].asQDateTime();
748                             break;
749                         case QVariant::RectF:
750                             *reinterpret_cast<QRectF *>(a[0]) = data[id].asQRectF();
751                             break;
752                         case QVariant::SizeF:
753                             *reinterpret_cast<QSizeF *>(a[0]) = data[id].asQSizeF();
754                             break;
755                         case QVariant::PointF:
756                             *reinterpret_cast<QPointF *>(a[0]) = data[id].asQPointF();
757                             break;
758                         case QMetaType::QObjectStar:
759                             *reinterpret_cast<QObject **>(a[0]) = data[id].asQObject();
760                             break;
761                         case QMetaType::QVariant:
762                             *reinterpret_cast<QVariant *>(a[0]) = readPropertyAsVariant(id);
763                             break;
764                         default:
765                             QQml_valueTypeProvider()->readValueType(data[id].dataType(), data[id].dataPtr(), data->dataSize(), t, a[0]);
766                             break;
767                         }
768                         if (t == qMetaTypeId<QQmlListProperty<QObject> >()) {
769                             int listIndex = data[id].asInt();
770                             const List *list = &listProperties.at(listIndex);
771                             *reinterpret_cast<QQmlListProperty<QObject> *>(a[0]) = 
772                                 QQmlListProperty<QObject>(object, (void *)list,
773                                                                   list_append, list_count, list_at, 
774                                                                   list_clear);
775                         }
776
777                     } else if (c == QMetaObject::WriteProperty) {
778
779                         switch(t) {
780                         case QVariant::Int:
781                             needActivate = *reinterpret_cast<int *>(a[0]) != data[id].asInt();
782                             data[id].setValue(*reinterpret_cast<int *>(a[0]));
783                             break;
784                         case QVariant::Bool:
785                             needActivate = *reinterpret_cast<bool *>(a[0]) != data[id].asBool();
786                             data[id].setValue(*reinterpret_cast<bool *>(a[0]));
787                             break;
788                         case QVariant::Double:
789                             needActivate = *reinterpret_cast<double *>(a[0]) != data[id].asDouble();
790                             data[id].setValue(*reinterpret_cast<double *>(a[0]));
791                             break;
792                         case QVariant::String:
793                             needActivate = *reinterpret_cast<QString *>(a[0]) != data[id].asQString();
794                             data[id].setValue(*reinterpret_cast<QString *>(a[0]));
795                             break;
796                         case QVariant::Url:
797                             needActivate = *reinterpret_cast<QUrl *>(a[0]) != data[id].asQUrl();
798                             data[id].setValue(*reinterpret_cast<QUrl *>(a[0]));
799                             break;
800                         case QVariant::Date:
801                             needActivate = *reinterpret_cast<QDate *>(a[0]) != data[id].asQDate();
802                             data[id].setValue(*reinterpret_cast<QDate *>(a[0]));
803                             break;
804                         case QVariant::DateTime:
805                             needActivate = *reinterpret_cast<QDateTime *>(a[0]) != data[id].asQDateTime();
806                             data[id].setValue(*reinterpret_cast<QDateTime *>(a[0]));
807                             break;
808                         case QVariant::RectF:
809                             needActivate = *reinterpret_cast<QRectF *>(a[0]) != data[id].asQRectF();
810                             data[id].setValue(*reinterpret_cast<QRectF *>(a[0]));
811                             break;
812                         case QVariant::SizeF:
813                             needActivate = *reinterpret_cast<QSizeF *>(a[0]) != data[id].asQSizeF();
814                             data[id].setValue(*reinterpret_cast<QSizeF *>(a[0]));
815                             break;
816                         case QVariant::PointF:
817                             needActivate = *reinterpret_cast<QPointF *>(a[0]) != data[id].asQPointF();
818                             data[id].setValue(*reinterpret_cast<QPointF *>(a[0]));
819                             break;
820                         case QMetaType::QObjectStar:
821                             needActivate = *reinterpret_cast<QObject **>(a[0]) != data[id].asQObject();
822                             data[id].setValue(*reinterpret_cast<QObject **>(a[0]), this, id);
823                             break;
824                         case QMetaType::QVariant:
825                             writeProperty(id, *reinterpret_cast<QVariant *>(a[0]));
826                             break;
827                         default:
828                             data[id].ensureValueType(t);
829                             needActivate = !QQml_valueTypeProvider()->equalValueType(t, a[0], data[id].dataPtr(), data[id].dataSize());
830                             QQml_valueTypeProvider()->writeValueType(t, a[0], data[id].dataPtr(), data[id].dataSize());
831                             break;
832                         }
833                     }
834
835                 }
836
837                 if (c == QMetaObject::WriteProperty && needActivate) {
838                     activate(object, methodOffset() + id, 0);
839                 }
840
841                 return -1;
842             }
843
844             id -= metaData->propertyCount;
845
846             if (id < metaData->aliasCount) {
847
848                 QQmlVMEMetaData::AliasData *d = metaData->aliasData() + id;
849
850                 if (d->flags & QML_ALIAS_FLAG_PTR && c == QMetaObject::ReadProperty) 
851                         *reinterpret_cast<void **>(a[0]) = 0;
852
853                 if (!ctxt) return -1;
854
855                 QQmlContext *context = ctxt->asQQmlContext();
856                 QQmlContextPrivate *ctxtPriv = QQmlContextPrivate::get(context);
857
858                 QObject *target = ctxtPriv->data->idValues[d->contextIdx].data();
859                 if (!target) 
860                     return -1;
861
862                 connectAlias(id);
863
864                 if (d->isObjectAlias()) {
865                     *reinterpret_cast<QObject **>(a[0]) = target;
866                     return -1;
867                 } 
868                 
869                 // Remove binding (if any) on write
870                 if(c == QMetaObject::WriteProperty) {
871                     int flags = *reinterpret_cast<int*>(a[3]);
872                     if (flags & QQmlPropertyPrivate::RemoveBindingOnAliasWrite) {
873                         QQmlData *targetData = QQmlData::get(target);
874                         if (targetData && targetData->hasBindingBit(d->propertyIndex())) {
875                             QQmlAbstractBinding *binding = QQmlPropertyPrivate::setBinding(target, d->propertyIndex(), d->isValueTypeAlias()?d->valueTypeIndex():-1, 0);
876                             if (binding) binding->destroy();
877                         }
878                     }
879                 }
880                 
881                 if (d->isValueTypeAlias()) {
882                     // Value type property
883                     QQmlEnginePrivate *ep = QQmlEnginePrivate::get(ctxt->engine);
884
885                     QQmlValueType *valueType = ep->valueTypes[d->valueType()];
886                     Q_ASSERT(valueType);
887
888                     valueType->read(target, d->propertyIndex());
889                     int rv = QMetaObject::metacall(valueType, c, d->valueTypeIndex(), a);
890                     
891                     if (c == QMetaObject::WriteProperty)
892                         valueType->write(target, d->propertyIndex(), 0x00);
893
894                     return rv;
895
896                 } else {
897                     return QMetaObject::metacall(target, c, d->propertyIndex(), a);
898                 }
899
900             }
901             return -1;
902
903         }
904
905     } else if(c == QMetaObject::InvokeMetaMethod) {
906
907         if (id >= methodOffset()) {
908
909             id -= methodOffset();
910             int plainSignals = metaData->signalCount + metaData->propertyCount +
911                                metaData->aliasCount;
912             if (id < plainSignals) {
913                 activate(object, _id, a);
914                 return -1;
915             }
916
917             id -= plainSignals;
918
919             if (id < metaData->methodCount) {
920                 if (!ctxt->engine)
921                     return -1; // We can't run the method
922
923                 QQmlEnginePrivate *ep = QQmlEnginePrivate::get(ctxt->engine);
924                 ep->referenceScarceResources(); // "hold" scarce resources in memory during evaluation.
925
926                 v8::Handle<v8::Function> function = method(id);
927                 if (function.IsEmpty()) {
928                     // The function was not compiled.  There are some exceptional cases which the
929                     // expression rewriter does not rewrite properly (e.g., \r-terminated lines
930                     // are not rewritten correctly but this bug is deemed out-of-scope to fix for
931                     // performance reasons; see QTBUG-24064) and thus compilation will have failed.
932                     QQmlError e;
933                     e.setDescription(QString(QLatin1String("Exception occurred during compilation of function: %1")).
934                                      arg(QLatin1String(QMetaObject::method(_id).methodSignature().constData())));
935                     ep->warning(e);
936                     return -1; // The dynamic method with that id is not available.
937                 }
938
939                 QQmlVMEMetaData::MethodData *data = metaData->methodData() + id;
940
941                 v8::HandleScope handle_scope;
942                 v8::Context::Scope scope(ep->v8engine()->context());
943                 v8::Handle<v8::Value> *args = 0;
944
945                 if (data->parameterCount) {
946                     args = new v8::Handle<v8::Value>[data->parameterCount];
947                     for (int ii = 0; ii < data->parameterCount; ++ii) 
948                         args[ii] = ep->v8engine()->fromVariant(*(QVariant *)a[ii + 1]);
949                 }
950
951                 v8::TryCatch try_catch;
952
953                 v8::Local<v8::Value> result = function->Call(ep->v8engine()->global(), data->parameterCount, args);
954
955                 QVariant rv;
956                 if (try_catch.HasCaught()) {
957                     QQmlError error;
958                     QQmlExpressionPrivate::exceptionToError(try_catch.Message(), error);
959                     if (error.isValid())
960                         ep->warning(error);
961                     if (a[0]) *(QVariant *)a[0] = QVariant();
962                 } else {
963                     if (a[0]) *(QVariant *)a[0] = ep->v8engine()->toVariant(result, 0);
964                 }
965
966                 ep->dereferenceScarceResources(); // "release" scarce resources if top-level expression evaluation is complete.
967                 return -1;
968             }
969             return -1;
970         }
971     }
972
973     if (parent.isT1())
974         return parent.asT1()->metaCall(object, c, _id, a);
975     else
976         return object->qt_metacall(c, _id, a);
977 }
978
979 v8::Handle<v8::Function> QQmlVMEMetaObject::method(int index)
980 {
981     if (!ctxt || !ctxt->isValid()) {
982         qWarning("QQmlVMEMetaObject: Internal error - attempted to evaluate a function in an invalid context");
983         return v8::Handle<v8::Function>();
984     }
985
986     if (!v8methods) 
987         v8methods = new v8::Persistent<v8::Function>[metaData->methodCount];
988
989     if (v8methods[index].IsEmpty()) {
990         QQmlVMEMetaData::MethodData *data = metaData->methodData() + index;
991
992         const char *body = ((const char*)metaData) + data->bodyOffset;
993         int bodyLength = data->bodyLength;
994
995         // XXX We should evaluate all methods in a single big script block to 
996         // improve the call time between dynamic methods defined on the same
997         // object
998         v8methods[index] = QQmlExpressionPrivate::evalFunction(ctxt, object, body,
999                                                                        bodyLength,
1000                                                                        ctxt->urlString,
1001                                                                        data->lineNumber);
1002     }
1003
1004     return v8methods[index];
1005 }
1006
1007 v8::Handle<v8::Value> QQmlVMEMetaObject::readVarProperty(int id)
1008 {
1009     Q_ASSERT(id >= firstVarPropertyIndex);
1010
1011     if (ensureVarPropertiesAllocated())
1012         return varProperties->Get(id - firstVarPropertyIndex);
1013     return v8::Handle<v8::Value>();
1014 }
1015
1016 QVariant QQmlVMEMetaObject::readPropertyAsVariant(int id)
1017 {
1018     if (id >= firstVarPropertyIndex) {
1019         if (ensureVarPropertiesAllocated())
1020             return QQmlEnginePrivate::get(ctxt->engine)->v8engine()->toVariant(varProperties->Get(id - firstVarPropertyIndex), -1);
1021         return QVariant();
1022     } else {
1023         if (data[id].dataType() == QMetaType::QObjectStar) {
1024             return QVariant::fromValue(data[id].asQObject());
1025         } else {
1026             return data[id].asQVariant();
1027         }
1028     }
1029 }
1030
1031 void QQmlVMEMetaObject::writeVarProperty(int id, v8::Handle<v8::Value> value)
1032 {
1033     Q_ASSERT(id >= firstVarPropertyIndex);
1034     if (!ensureVarPropertiesAllocated())
1035         return;
1036
1037     // Importantly, if the current value is a scarce resource, we need to ensure that it
1038     // gets automatically released by the engine if no other references to it exist.
1039     v8::Local<v8::Value> oldv = varProperties->Get(id - firstVarPropertyIndex);
1040     if (oldv->IsObject()) {
1041         QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(oldv));
1042         if (r) {
1043             r->removeVmePropertyReference();
1044         }
1045     }
1046
1047     QObject *valueObject = 0;
1048     QQmlVMEVariantQObjectPtr *guard = getQObjectGuardForProperty(id);
1049
1050     if (value->IsObject()) {
1051         // And, if the new value is a scarce resource, we need to ensure that it does not get
1052         // automatically released by the engine until no other references to it exist.
1053         if (QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(value))) {
1054             r->addVmePropertyReference();
1055         } else if (QV8QObjectResource *r = v8_resource_cast<QV8QObjectResource>(v8::Handle<v8::Object>::Cast(value))) {
1056             // We need to track this QObject to signal its deletion
1057             valueObject = r->object;
1058
1059             // Do we already have a QObject guard for this property?
1060             if (valueObject && !guard) {
1061                 guard = new QQmlVMEVariantQObjectPtr(true);
1062                 varObjectGuards.append(guard);
1063             }
1064         }
1065     }
1066
1067     if (guard) {
1068         guard->setGuardedValue(valueObject, this, id);
1069     }
1070
1071     // Write the value and emit change signal as appropriate.
1072     varProperties->Set(id - firstVarPropertyIndex, value);
1073     activate(object, methodOffset() + id, 0);
1074 }
1075
1076 void QQmlVMEMetaObject::writeProperty(int id, const QVariant &value)
1077 {
1078     if (id >= firstVarPropertyIndex) {
1079         if (!ensureVarPropertiesAllocated())
1080             return;
1081
1082         // Importantly, if the current value is a scarce resource, we need to ensure that it
1083         // gets automatically released by the engine if no other references to it exist.
1084         v8::Local<v8::Value> oldv = varProperties->Get(id - firstVarPropertyIndex);
1085         if (oldv->IsObject()) {
1086             QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(oldv));
1087             if (r) {
1088                 r->removeVmePropertyReference();
1089             }
1090         }
1091
1092         // And, if the new value is a scarce resource, we need to ensure that it does not get
1093         // automatically released by the engine until no other references to it exist.
1094         v8::Handle<v8::Value> newv = QQmlEnginePrivate::get(ctxt->engine)->v8engine()->fromVariant(value);
1095         if (newv->IsObject()) {
1096             QV8VariantResource *r = v8_resource_cast<QV8VariantResource>(v8::Handle<v8::Object>::Cast(newv));
1097             if (r) {
1098                 r->addVmePropertyReference();
1099             }
1100         }
1101
1102         // Write the value and emit change signal as appropriate.
1103         QVariant currentValue = readPropertyAsVariant(id);
1104         varProperties->Set(id - firstVarPropertyIndex, newv);
1105         if ((currentValue.userType() != value.userType() || currentValue != value))
1106             activate(object, methodOffset() + id, 0);
1107     } else {
1108         bool needActivate = false;
1109         if (value.userType() == QMetaType::QObjectStar) {
1110             QObject *o = *(QObject **)value.data();
1111             needActivate = (data[id].dataType() != QMetaType::QObjectStar || data[id].asQObject() != o);
1112             data[id].setValue(o, this, id);
1113         } else {
1114             needActivate = (data[id].dataType() != qMetaTypeId<QVariant>() ||
1115                             data[id].asQVariant().userType() != value.userType() ||
1116                             data[id].asQVariant() != value);
1117             data[id].setValue(value);
1118         }
1119
1120         if (needActivate)
1121             activate(object, methodOffset() + id, 0);
1122     }
1123 }
1124
1125 void QQmlVMEMetaObject::listChanged(int id)
1126 {
1127     activate(object, methodOffset() + id, 0);
1128 }
1129
1130 void QQmlVMEMetaObject::list_append(QQmlListProperty<QObject> *prop, QObject *o)
1131 {
1132     List *list = static_cast<List *>(prop->data);
1133     list->append(o);
1134     list->mo->activate(prop->object, list->notifyIndex, 0);
1135 }
1136
1137 int QQmlVMEMetaObject::list_count(QQmlListProperty<QObject> *prop)
1138 {
1139     return static_cast<List *>(prop->data)->count();
1140 }
1141
1142 QObject *QQmlVMEMetaObject::list_at(QQmlListProperty<QObject> *prop, int index)
1143 {
1144     return static_cast<List *>(prop->data)->at(index);
1145 }
1146
1147 void QQmlVMEMetaObject::list_clear(QQmlListProperty<QObject> *prop)
1148 {
1149     List *list = static_cast<List *>(prop->data);
1150     list->clear();
1151     list->mo->activate(prop->object, list->notifyIndex, 0);
1152 }
1153
1154 void QQmlVMEMetaObject::registerInterceptor(int index, int valueIndex, QQmlPropertyValueInterceptor *interceptor)
1155 {
1156     interceptor->m_coreIndex = index;
1157     interceptor->m_valueTypeCoreIndex = valueIndex;
1158     interceptor->m_next = interceptors;
1159     interceptors = interceptor;
1160 }
1161
1162 int QQmlVMEMetaObject::vmeMethodLineNumber(int index)
1163 {
1164     if (index < methodOffset()) {
1165         Q_ASSERT(parent.isT1());
1166         return static_cast<QQmlVMEMetaObject *>(parent.asT1())->vmeMethodLineNumber(index);
1167     }
1168
1169     int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
1170     Q_ASSERT(index >= (methodOffset() + plainSignals) && index < (methodOffset() + plainSignals + metaData->methodCount));
1171
1172     int rawIndex = index - methodOffset() - plainSignals;
1173
1174     QQmlVMEMetaData::MethodData *data = metaData->methodData() + rawIndex;
1175     return data->lineNumber;
1176 }
1177
1178 v8::Handle<v8::Function> QQmlVMEMetaObject::vmeMethod(int index)
1179 {
1180     if (index < methodOffset()) {
1181         Q_ASSERT(parent.isT1());
1182         return static_cast<QQmlVMEMetaObject *>(parent.asT1())->vmeMethod(index);
1183     }
1184     int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
1185     Q_ASSERT(index >= (methodOffset() + plainSignals) && index < (methodOffset() + plainSignals + metaData->methodCount));
1186     return method(index - methodOffset() - plainSignals);
1187 }
1188
1189 // Used by debugger
1190 void QQmlVMEMetaObject::setVmeMethod(int index, v8::Persistent<v8::Function> value)
1191 {
1192     if (index < methodOffset()) {
1193         Q_ASSERT(parent.isT1());
1194         return static_cast<QQmlVMEMetaObject *>(parent.asT1())->setVmeMethod(index, value);
1195     }
1196     int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
1197     Q_ASSERT(index >= (methodOffset() + plainSignals) && index < (methodOffset() + plainSignals + metaData->methodCount));
1198
1199     if (!v8methods) 
1200         v8methods = new v8::Persistent<v8::Function>[metaData->methodCount];
1201
1202     int methodIndex = index - methodOffset() - plainSignals;
1203     if (!v8methods[methodIndex].IsEmpty()) 
1204         qPersistentDispose(v8methods[methodIndex]);
1205     v8methods[methodIndex] = value;
1206 }
1207
1208 v8::Handle<v8::Value> QQmlVMEMetaObject::vmeProperty(int index)
1209 {
1210     if (index < propOffset()) {
1211         Q_ASSERT(parent.isT1());
1212         return static_cast<QQmlVMEMetaObject *>(parent.asT1())->vmeProperty(index);
1213     }
1214     return readVarProperty(index - propOffset());
1215 }
1216
1217 void QQmlVMEMetaObject::setVMEProperty(int index, v8::Handle<v8::Value> v)
1218 {
1219     if (index < propOffset()) {
1220         Q_ASSERT(parent.isT1());
1221         static_cast<QQmlVMEMetaObject *>(parent.asT1())->setVMEProperty(index, v);
1222         return;
1223     }
1224     return writeVarProperty(index - propOffset(), v);
1225 }
1226
1227 bool QQmlVMEMetaObject::ensureVarPropertiesAllocated()
1228 {
1229     if (!varPropertiesInitialized)
1230         allocateVarPropertiesArray();
1231
1232     // in some situations, the QObject's v8object (and associated v8 data,
1233     // such as the varProperties array) will have been cleaned up, but the
1234     // QObject ptr will not yet have been deleted (eg, waiting on deleteLater).
1235     // In this situation, the varProperties handle will be (and should remain)
1236     // empty.
1237     return !varProperties.IsEmpty();
1238 }
1239
1240 // see also: QV8GCCallback::garbageCollectorPrologueCallback()
1241 void QQmlVMEMetaObject::allocateVarPropertiesArray()
1242 {
1243     v8::HandleScope handleScope;
1244     v8::Context::Scope cs(QQmlEnginePrivate::get(ctxt->engine)->v8engine()->context());
1245     varProperties = qPersistentNew(v8::Array::New(metaData->varPropertyCount));
1246     varProperties.MakeWeak(static_cast<void*>(this), VarPropertiesWeakReferenceCallback);
1247     varPropertiesInitialized = true;
1248 }
1249
1250 /*
1251    The "var" properties are stored in a v8::Array which will be strong persistent if the object has cpp-ownership
1252    and the root QObject in the parent chain does not have JS-ownership.  In the weak persistent handle case,
1253    this callback will dispose the handle when the v8object which owns the lifetime of the var properties array
1254    is cleared as a result of all other handles to that v8object being released.
1255    See QV8GCCallback::garbageCollectorPrologueCallback() for more information.
1256  */
1257 void QQmlVMEMetaObject::VarPropertiesWeakReferenceCallback(v8::Persistent<v8::Value> object, void* parameter)
1258 {
1259     QQmlVMEMetaObject *vmemo = static_cast<QQmlVMEMetaObject*>(parameter);
1260     Q_ASSERT(vmemo);
1261     qPersistentDispose(object);
1262     vmemo->varProperties.Clear();
1263 }
1264
1265 void QQmlVMEMetaObject::GcPrologueCallback(QV8GCCallback::Node *node)
1266 {
1267     QQmlVMEMetaObject *vmemo = static_cast<QQmlVMEMetaObject*>(node);
1268     Q_ASSERT(vmemo);
1269
1270     if (!vmemo->ctxt || !vmemo->ctxt->engine)
1271         return;
1272     QQmlEnginePrivate *ep = QQmlEnginePrivate::get(vmemo->ctxt->engine);
1273
1274     // add references created by VMEVariant properties
1275     int maxDataIdx = vmemo->metaData->propertyCount - vmemo->metaData->varPropertyCount;
1276     for (int ii = 0; ii < maxDataIdx; ++ii) { // XXX TODO: optimise?
1277         if (vmemo->data[ii].dataType() == QMetaType::QObjectStar) {
1278             // possible QObject reference.
1279             QObject *ref = vmemo->data[ii].asQObject();
1280             if (ref) {
1281                 ep->v8engine()->addRelationshipForGC(vmemo->object, ref);
1282             }
1283         }
1284     }
1285
1286     // add references created by var properties
1287     if (!vmemo->varPropertiesInitialized || vmemo->varProperties.IsEmpty())
1288         return;
1289     ep->v8engine()->addRelationshipForGC(vmemo->object, vmemo->varProperties);
1290 }
1291
1292 bool QQmlVMEMetaObject::aliasTarget(int index, QObject **target, int *coreIndex, int *valueTypeIndex) const
1293 {
1294     Q_ASSERT(index >= propOffset() + metaData->propertyCount);
1295
1296     *target = 0;
1297     *coreIndex = -1;
1298     *valueTypeIndex = -1;
1299
1300     if (!ctxt)
1301         return false;
1302
1303     QQmlVMEMetaData::AliasData *d = metaData->aliasData() + (index - propOffset() - metaData->propertyCount);
1304     QQmlContext *context = ctxt->asQQmlContext();
1305     QQmlContextPrivate *ctxtPriv = QQmlContextPrivate::get(context);
1306
1307     *target = ctxtPriv->data->idValues[d->contextIdx].data();
1308     if (!*target)
1309         return false;
1310
1311     if (d->isObjectAlias()) {
1312     } else if (d->isValueTypeAlias()) {
1313         *coreIndex = d->propertyIndex();
1314         *valueTypeIndex = d->valueTypeIndex();
1315     } else {
1316         *coreIndex = d->propertyIndex();
1317     }
1318
1319     return true;
1320 }
1321
1322 void QQmlVMEMetaObject::connectAlias(int aliasId)
1323 {
1324     if (!aConnected.testBit(aliasId)) {
1325
1326         if (!aliasEndpoints)
1327             aliasEndpoints = new QQmlVMEMetaObjectEndpoint[metaData->aliasCount];
1328
1329         aConnected.setBit(aliasId);
1330
1331         QQmlVMEMetaData::AliasData *d = metaData->aliasData() + aliasId;
1332
1333         QQmlVMEMetaObjectEndpoint *endpoint = aliasEndpoints + aliasId;
1334         endpoint->metaObject = this;
1335
1336         endpoint->connect(&ctxt->idValues[d->contextIdx].bindings);
1337
1338         endpoint->tryConnect();
1339     }
1340 }
1341
1342 void QQmlVMEMetaObject::connectAliasSignal(int index, bool indexInSignalRange)
1343 {
1344     int aliasId = (index - (indexInSignalRange ? signalOffset() : methodOffset())) - metaData->propertyCount;
1345     if (aliasId < 0 || aliasId >= metaData->aliasCount)
1346         return;
1347
1348     connectAlias(aliasId);
1349 }
1350
1351 /*! \internal
1352     \a index is in the method index range (QMetaMethod::methodIndex()).
1353 */
1354 void QQmlVMEMetaObject::activate(QObject *object, int index, void **args)
1355 {
1356     QMetaObject::activate(object, signalOffset(), index - methodOffset(), args);
1357 }
1358
1359 QQmlVMEMetaObject *QQmlVMEMetaObject::getForProperty(QObject *o, int coreIndex)
1360 {
1361     QQmlVMEMetaObject *vme = QQmlVMEMetaObject::get(o);
1362     while (vme->propOffset() > coreIndex) {
1363         Q_ASSERT(vme->parent.isT1());
1364         vme = static_cast<QQmlVMEMetaObject *>(vme->parent.asT1());
1365     }
1366     return vme;
1367 }
1368
1369 QQmlVMEMetaObject *QQmlVMEMetaObject::getForMethod(QObject *o, int coreIndex)
1370 {
1371     QQmlVMEMetaObject *vme = QQmlVMEMetaObject::get(o);
1372     while (vme->methodOffset() > coreIndex) {
1373         Q_ASSERT(vme->parent.isT1());
1374         vme = static_cast<QQmlVMEMetaObject *>(vme->parent.asT1());
1375     }
1376     return vme;
1377 }
1378
1379 /*! \internal
1380     \a coreIndex is in the signal index range (see QObjectPrivate::signalIndex()).
1381     This is different from QMetaMethod::methodIndex().
1382 */
1383 QQmlVMEMetaObject *QQmlVMEMetaObject::getForSignal(QObject *o, int coreIndex)
1384 {
1385     QQmlVMEMetaObject *vme = QQmlVMEMetaObject::get(o);
1386     while (vme->signalOffset() > coreIndex) {
1387         Q_ASSERT(vme->parent.isT1());
1388         vme = static_cast<QQmlVMEMetaObject *>(vme->parent.asT1());
1389     }
1390     return vme;
1391 }
1392
1393 QQmlVMEVariantQObjectPtr *QQmlVMEMetaObject::getQObjectGuardForProperty(int index) const
1394 {
1395     QList<QQmlVMEVariantQObjectPtr *>::ConstIterator it = varObjectGuards.constBegin(), end = varObjectGuards.constEnd();
1396     for ( ; it != end; ++it) {
1397         if ((*it)->m_index == index) {
1398             return *it;
1399         }
1400     }
1401
1402     return 0;
1403 }
1404
1405 QT_END_NAMESPACE