Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativevmemetaobject.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "private/qdeclarativevmemetaobject_p.h"
43
44 #include "qdeclarative.h"
45 #include "private/qdeclarativerefcount_p.h"
46 #include "qdeclarativeexpression.h"
47 #include "private/qdeclarativeexpression_p.h"
48 #include "private/qdeclarativecontext_p.h"
49 #include "private/qdeclarativebinding_p.h"
50
51 Q_DECLARE_METATYPE(QScriptValue);
52
53 QT_BEGIN_NAMESPACE
54
55 class QDeclarativeVMEVariant
56 {
57 public:
58     inline QDeclarativeVMEVariant();
59     inline ~QDeclarativeVMEVariant();
60
61     inline const void *dataPtr() const;
62     inline void *dataPtr();
63     inline int dataType() const;
64
65     inline QObject *asQObject();
66     inline const QVariant &asQVariant();
67     inline int asInt();
68     inline bool asBool();
69     inline double asDouble();
70     inline const QString &asQString();
71     inline const QUrl &asQUrl();
72     inline const QColor &asQColor();
73     inline const QTime &asQTime();
74     inline const QDate &asQDate();
75     inline const QDateTime &asQDateTime();
76     inline const QScriptValue &asQScriptValue();
77
78     inline void setValue(QObject *);
79     inline void setValue(const QVariant &);
80     inline void setValue(int);
81     inline void setValue(bool);
82     inline void setValue(double);
83     inline void setValue(const QString &);
84     inline void setValue(const QUrl &);
85     inline void setValue(const QColor &);
86     inline void setValue(const QTime &);
87     inline void setValue(const QDate &);
88     inline void setValue(const QDateTime &);
89     inline void setValue(const QScriptValue &);
90 private:
91     int type;
92     void *data[4]; // Large enough to hold all types
93
94     inline void cleanup();
95 };
96
97 QDeclarativeVMEVariant::QDeclarativeVMEVariant()
98 : type(QVariant::Invalid)
99 {
100 }
101
102 QDeclarativeVMEVariant::~QDeclarativeVMEVariant()
103 {
104     cleanup();
105 }
106
107 void QDeclarativeVMEVariant::cleanup()
108 {
109     if (type == QVariant::Invalid) {
110     } else if (type == QMetaType::Int ||
111                type == QMetaType::Bool ||
112                type == QMetaType::Double) {
113         type = QVariant::Invalid;
114     } else if (type == QMetaType::QObjectStar) {
115         ((QDeclarativeGuard<QObject>*)dataPtr())->~QDeclarativeGuard<QObject>();
116         type = QVariant::Invalid;
117     } else if (type == QMetaType::QString) {
118         ((QString *)dataPtr())->~QString();
119         type = QVariant::Invalid;
120     } else if (type == QMetaType::QUrl) {
121         ((QUrl *)dataPtr())->~QUrl();
122         type = QVariant::Invalid;
123     } else if (type == QMetaType::QColor) {
124         ((QColor *)dataPtr())->~QColor();
125         type = QVariant::Invalid;
126     } else if (type == QMetaType::QTime) {
127         ((QTime *)dataPtr())->~QTime();
128         type = QVariant::Invalid;
129     } else if (type == QMetaType::QDate) {
130         ((QDate *)dataPtr())->~QDate();
131         type = QVariant::Invalid;
132     } else if (type == QMetaType::QDateTime) {
133         ((QDateTime *)dataPtr())->~QDateTime();
134         type = QVariant::Invalid;
135     } else if (type == qMetaTypeId<QVariant>()) {
136         ((QVariant *)dataPtr())->~QVariant();
137         type = QVariant::Invalid;
138     } else if (type == qMetaTypeId<QScriptValue>()) {
139         ((QScriptValue *)dataPtr())->~QScriptValue();
140         type = QVariant::Invalid;
141     }
142
143 }
144
145 int QDeclarativeVMEVariant::dataType() const
146 {
147     return type;
148 }
149
150 const void *QDeclarativeVMEVariant::dataPtr() const
151 {
152     return &data;
153 }
154
155 void *QDeclarativeVMEVariant::dataPtr() 
156 {
157     return &data;
158 }
159
160 QObject *QDeclarativeVMEVariant::asQObject() 
161 {
162     if (type != QMetaType::QObjectStar) 
163         setValue((QObject *)0);
164
165     return *(QDeclarativeGuard<QObject> *)(dataPtr());
166 }
167
168 const QVariant &QDeclarativeVMEVariant::asQVariant() 
169 {
170     if (type != QMetaType::QVariant)
171         setValue(QVariant());
172
173     return *(QVariant *)(dataPtr());
174 }
175
176 int QDeclarativeVMEVariant::asInt() 
177 {
178     if (type != QMetaType::Int)
179         setValue(int(0));
180
181     return *(int *)(dataPtr());
182 }
183
184 bool QDeclarativeVMEVariant::asBool() 
185 {
186     if (type != QMetaType::Bool)
187         setValue(bool(false));
188
189     return *(bool *)(dataPtr());
190 }
191
192 double QDeclarativeVMEVariant::asDouble() 
193 {
194     if (type != QMetaType::Double)
195         setValue(double(0));
196
197     return *(double *)(dataPtr());
198 }
199
200 const QString &QDeclarativeVMEVariant::asQString() 
201 {
202     if (type != QMetaType::QString)
203         setValue(QString());
204
205     return *(QString *)(dataPtr());
206 }
207
208 const QUrl &QDeclarativeVMEVariant::asQUrl() 
209 {
210     if (type != QMetaType::QUrl)
211         setValue(QUrl());
212
213     return *(QUrl *)(dataPtr());
214 }
215
216 const QColor &QDeclarativeVMEVariant::asQColor() 
217 {
218     if (type != QMetaType::QColor)
219         setValue(QColor());
220
221     return *(QColor *)(dataPtr());
222 }
223
224 const QTime &QDeclarativeVMEVariant::asQTime() 
225 {
226     if (type != QMetaType::QTime)
227         setValue(QTime());
228
229     return *(QTime *)(dataPtr());
230 }
231
232 const QDate &QDeclarativeVMEVariant::asQDate() 
233 {
234     if (type != QMetaType::QDate)
235         setValue(QDate());
236
237     return *(QDate *)(dataPtr());
238 }
239
240 const QDateTime &QDeclarativeVMEVariant::asQDateTime() 
241 {
242     if (type != QMetaType::QDateTime)
243         setValue(QDateTime());
244
245     return *(QDateTime *)(dataPtr());
246 }
247
248 const QScriptValue &QDeclarativeVMEVariant::asQScriptValue() 
249 {
250     if (type != qMetaTypeId<QScriptValue>())
251         setValue(QScriptValue());
252
253     return *(QScriptValue *)(dataPtr());
254 }
255
256 void QDeclarativeVMEVariant::setValue(QObject *v)
257 {
258     if (type != QMetaType::QObjectStar) {
259         cleanup();
260         type = QMetaType::QObjectStar;
261         new (dataPtr()) QDeclarativeGuard<QObject>();
262     }
263     *(QDeclarativeGuard<QObject>*)(dataPtr()) = v;
264 }
265
266 void QDeclarativeVMEVariant::setValue(const QVariant &v)
267 {
268     if (type != qMetaTypeId<QVariant>()) {
269         cleanup();
270         type = qMetaTypeId<QVariant>();
271         new (dataPtr()) QVariant(v);
272     } else {
273         *(QVariant *)(dataPtr()) = v;
274     }
275 }
276
277 void QDeclarativeVMEVariant::setValue(int v)
278 {
279     if (type != QMetaType::Int) {
280         cleanup();
281         type = QMetaType::Int;
282     }
283     *(int *)(dataPtr()) = v;
284 }
285
286 void QDeclarativeVMEVariant::setValue(bool v)
287 {
288     if (type != QMetaType::Bool) {
289         cleanup();
290         type = QMetaType::Bool;
291     }
292     *(bool *)(dataPtr()) = v;
293 }
294
295 void QDeclarativeVMEVariant::setValue(double v)
296 {
297     if (type != QMetaType::Double) {
298         cleanup();
299         type = QMetaType::Double;
300     }
301     *(double *)(dataPtr()) = v;
302 }
303
304 void QDeclarativeVMEVariant::setValue(const QString &v)
305 {
306     if (type != QMetaType::QString) {
307         cleanup();
308         type = QMetaType::QString;
309         new (dataPtr()) QString(v);
310     } else {
311         *(QString *)(dataPtr()) = v;
312     }
313 }
314
315 void QDeclarativeVMEVariant::setValue(const QUrl &v)
316 {
317     if (type != QMetaType::QUrl) {
318         cleanup();
319         type = QMetaType::QUrl;
320         new (dataPtr()) QUrl(v);
321     } else {
322         *(QUrl *)(dataPtr()) = v;
323     }
324 }
325
326 void QDeclarativeVMEVariant::setValue(const QColor &v)
327 {
328     if (type != QMetaType::QColor) {
329         cleanup();
330         type = QMetaType::QColor;
331         new (dataPtr()) QColor(v);
332     } else {
333         *(QColor *)(dataPtr()) = v;
334     }
335 }
336
337 void QDeclarativeVMEVariant::setValue(const QTime &v)
338 {
339     if (type != QMetaType::QTime) {
340         cleanup();
341         type = QMetaType::QTime;
342         new (dataPtr()) QTime(v);
343     } else {
344         *(QTime *)(dataPtr()) = v;
345     }
346 }
347
348 void QDeclarativeVMEVariant::setValue(const QDate &v)
349 {
350     if (type != QMetaType::QDate) {
351         cleanup();
352         type = QMetaType::QDate;
353         new (dataPtr()) QDate(v);
354     } else {
355         *(QDate *)(dataPtr()) = v;
356     }
357 }
358
359 void QDeclarativeVMEVariant::setValue(const QDateTime &v)
360 {
361     if (type != QMetaType::QDateTime) {
362         cleanup();
363         type = QMetaType::QDateTime;
364         new (dataPtr()) QDateTime(v);
365     } else {
366         *(QDateTime *)(dataPtr()) = v;
367     }
368 }
369
370 void QDeclarativeVMEVariant::setValue(const QScriptValue &v)
371 {
372     if (type != qMetaTypeId<QScriptValue>()) {
373         cleanup();
374         type = qMetaTypeId<QScriptValue>();
375         new (dataPtr()) QScriptValue(v);
376     } else {
377         *(QScriptValue *)(dataPtr()) = v;
378     }
379 }
380
381 QDeclarativeVMEMetaObject::QDeclarativeVMEMetaObject(QObject *obj,
382                                                      const QMetaObject *other, 
383                                                      const QDeclarativeVMEMetaData *meta,
384                                                      QDeclarativeCompiledData *cdata)
385 : object(obj), compiledData(cdata), ctxt(QDeclarativeData::get(obj, true)->outerContext),
386   metaData(meta), data(0), methods(0), parent(0)
387 {
388     compiledData->addref();
389
390     *static_cast<QMetaObject *>(this) = *other;
391     this->d.superdata = obj->metaObject();
392
393     QObjectPrivate *op = QObjectPrivate::get(obj);
394     if (op->metaObject)
395         parent = static_cast<QAbstractDynamicMetaObject*>(op->metaObject);
396     op->metaObject = this;
397
398     propOffset = QAbstractDynamicMetaObject::propertyOffset();
399     methodOffset = QAbstractDynamicMetaObject::methodOffset();
400
401     data = new QDeclarativeVMEVariant[metaData->propertyCount];
402
403     aConnected.resize(metaData->aliasCount);
404     int list_type = qMetaTypeId<QDeclarativeListProperty<QObject> >();
405
406     // ### Optimize
407     for (int ii = 0; ii < metaData->propertyCount; ++ii) {
408         int t = (metaData->propertyData() + ii)->propertyType;
409         if (t == list_type) {
410             listProperties.append(List(methodOffset + ii));
411             data[ii].setValue(listProperties.count() - 1);
412         } 
413     }
414 }
415
416 QDeclarativeVMEMetaObject::~QDeclarativeVMEMetaObject()
417 {
418     compiledData->release();
419     delete parent;
420     delete [] data;
421     delete [] methods;
422 }
423
424 int QDeclarativeVMEMetaObject::metaCall(QMetaObject::Call c, int _id, void **a)
425 {
426     int id = _id;
427     if(c == QMetaObject::WriteProperty) {
428         int flags = *reinterpret_cast<int*>(a[3]);
429         if (!(flags & QDeclarativePropertyPrivate::BypassInterceptor)
430             && !aInterceptors.isEmpty()
431             && aInterceptors.testBit(id)) {
432             QPair<int, QDeclarativePropertyValueInterceptor*> pair = interceptors.value(id);
433             int valueIndex = pair.first;
434             QDeclarativePropertyValueInterceptor *vi = pair.second;
435             int type = property(id).userType();
436
437             if (type != QVariant::Invalid) {
438                 if (valueIndex != -1) {
439                     QDeclarativeEnginePrivate *ep = ctxt?QDeclarativeEnginePrivate::get(ctxt->engine):0;
440                     QDeclarativeValueType *valueType = 0;
441                     if (ep) valueType = ep->valueTypes[type];
442                     else valueType = QDeclarativeValueTypeFactory::valueType(type);
443                     Q_ASSERT(valueType);
444
445                     valueType->setValue(QVariant(type, a[0]));
446                     QMetaProperty valueProp = valueType->metaObject()->property(valueIndex);
447                     vi->write(valueProp.read(valueType));
448
449                     if (!ep) delete valueType;
450                     return -1;
451                 } else {
452                     vi->write(QVariant(type, a[0]));
453                     return -1;
454                 }
455             }
456         }
457     }
458     if(c == QMetaObject::ReadProperty || c == QMetaObject::WriteProperty) {
459         if (id >= propOffset) {
460             id -= propOffset;
461
462             if (id < metaData->propertyCount) {
463                int t = (metaData->propertyData() + id)->propertyType;
464                 bool needActivate = false;
465
466                 if (t == -1) {
467
468                     if (c == QMetaObject::ReadProperty) {
469                         *reinterpret_cast<QVariant *>(a[0]) = readVarPropertyAsVariant(id);
470                     } else if (c == QMetaObject::WriteProperty) {
471                         writeVarProperty(id, *reinterpret_cast<QVariant *>(a[0]));
472                     }
473
474                 } else {
475
476                     if (c == QMetaObject::ReadProperty) {
477                         switch(t) {
478                         case QVariant::Int:
479                             *reinterpret_cast<int *>(a[0]) = data[id].asInt();
480                             break;
481                         case QVariant::Bool:
482                             *reinterpret_cast<bool *>(a[0]) = data[id].asBool();
483                             break;
484                         case QVariant::Double:
485                             *reinterpret_cast<double *>(a[0]) = data[id].asDouble();
486                             break;
487                         case QVariant::String:
488                             *reinterpret_cast<QString *>(a[0]) = data[id].asQString();
489                             break;
490                         case QVariant::Url:
491                             *reinterpret_cast<QUrl *>(a[0]) = data[id].asQUrl();
492                             break;
493                         case QVariant::Color:
494                             *reinterpret_cast<QColor *>(a[0]) = data[id].asQColor();
495                             break;
496                         case QVariant::Date:
497                             *reinterpret_cast<QDate *>(a[0]) = data[id].asQDate();
498                             break;
499                         case QVariant::DateTime:
500                             *reinterpret_cast<QDateTime *>(a[0]) = data[id].asQDateTime();
501                             break;
502                         case QMetaType::QObjectStar:
503                             *reinterpret_cast<QObject **>(a[0]) = data[id].asQObject();
504                             break;
505                         default:
506                             break;
507                         }
508                         if (t == qMetaTypeId<QDeclarativeListProperty<QObject> >()) {
509                             int listIndex = data[id].asInt();
510                             const List *list = &listProperties.at(listIndex);
511                             *reinterpret_cast<QDeclarativeListProperty<QObject> *>(a[0]) = 
512                                 QDeclarativeListProperty<QObject>(object, (void *)list,
513                                                                   list_append, list_count, list_at, 
514                                                                   list_clear);
515                         }
516
517                     } else if (c == QMetaObject::WriteProperty) {
518
519                         switch(t) {
520                         case QVariant::Int:
521                             needActivate = *reinterpret_cast<int *>(a[0]) != data[id].asInt();
522                             data[id].setValue(*reinterpret_cast<int *>(a[0]));
523                             break;
524                         case QVariant::Bool:
525                             needActivate = *reinterpret_cast<bool *>(a[0]) != data[id].asBool();
526                             data[id].setValue(*reinterpret_cast<bool *>(a[0]));
527                             break;
528                         case QVariant::Double:
529                             needActivate = *reinterpret_cast<double *>(a[0]) != data[id].asDouble();
530                             data[id].setValue(*reinterpret_cast<double *>(a[0]));
531                             break;
532                         case QVariant::String:
533                             needActivate = *reinterpret_cast<QString *>(a[0]) != data[id].asQString();
534                             data[id].setValue(*reinterpret_cast<QString *>(a[0]));
535                             break;
536                         case QVariant::Url:
537                             needActivate = *reinterpret_cast<QUrl *>(a[0]) != data[id].asQUrl();
538                             data[id].setValue(*reinterpret_cast<QUrl *>(a[0]));
539                             break;
540                         case QVariant::Color:
541                             needActivate = *reinterpret_cast<QColor *>(a[0]) != data[id].asQColor();
542                             data[id].setValue(*reinterpret_cast<QColor *>(a[0]));
543                             break;
544                         case QVariant::Date:
545                             needActivate = *reinterpret_cast<QDate *>(a[0]) != data[id].asQDate();
546                             data[id].setValue(*reinterpret_cast<QDate *>(a[0]));
547                             break;
548                         case QVariant::DateTime:
549                             needActivate = *reinterpret_cast<QDateTime *>(a[0]) != data[id].asQDateTime();
550                             data[id].setValue(*reinterpret_cast<QDateTime *>(a[0]));
551                             break;
552                         case QMetaType::QObjectStar:
553                             needActivate = *reinterpret_cast<QObject **>(a[0]) != data[id].asQObject();
554                             data[id].setValue(*reinterpret_cast<QObject **>(a[0]));
555                             break;
556                         default:
557                             break;
558                         }
559                     }
560
561                 }
562
563                 if (c == QMetaObject::WriteProperty && needActivate) {
564                     activate(object, methodOffset + id, 0);
565                 }
566
567                 return -1;
568             }
569
570             id -= metaData->propertyCount;
571
572             if (id < metaData->aliasCount) {
573
574                 QDeclarativeVMEMetaData::AliasData *d = metaData->aliasData() + id;
575
576                 if (d->flags & QML_ALIAS_FLAG_PTR && c == QMetaObject::ReadProperty) 
577                         *reinterpret_cast<void **>(a[0]) = 0;
578
579                 if (!ctxt) return -1;
580
581                 QDeclarativeContext *context = ctxt->asQDeclarativeContext();
582                 QDeclarativeContextPrivate *ctxtPriv = QDeclarativeContextPrivate::get(context);
583
584                 QObject *target = ctxtPriv->data->idValues[d->contextIdx].data();
585                 if (!target) 
586                     return -1;
587
588                 connectAlias(id);
589
590                 if (d->isObjectAlias()) {
591                     *reinterpret_cast<QObject **>(a[0]) = target;
592                     return -1;
593                 } 
594                 
595                 // Remove binding (if any) on write
596                 if(c == QMetaObject::WriteProperty) {
597                     int flags = *reinterpret_cast<int*>(a[3]);
598                     if (flags & QDeclarativePropertyPrivate::RemoveBindingOnAliasWrite) {
599                         QDeclarativeData *targetData = QDeclarativeData::get(target);
600                         if (targetData && targetData->hasBindingBit(d->propertyIndex())) {
601                             QDeclarativeAbstractBinding *binding = QDeclarativePropertyPrivate::setBinding(target, d->propertyIndex(), d->isValueTypeAlias()?d->valueTypeIndex():-1, 0);
602                             if (binding) binding->destroy();
603                         }
604                     }
605                 }
606                 
607                 if (d->isValueTypeAlias()) {
608                     // Value type property
609                     QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(ctxt->engine);
610
611                     QDeclarativeValueType *valueType = ep->valueTypes[d->valueType()];
612                     Q_ASSERT(valueType);
613
614                     valueType->read(target, d->propertyIndex());
615                     int rv = QMetaObject::metacall(valueType, c, d->valueTypeIndex(), a);
616                     
617                     if (c == QMetaObject::WriteProperty)
618                         valueType->write(target, d->propertyIndex(), 0x00);
619
620                     return rv;
621
622                 } else {
623                     return QMetaObject::metacall(target, c, d->propertyIndex(), a);
624                 }
625
626             }
627             return -1;
628
629         }
630
631     } else if(c == QMetaObject::InvokeMetaMethod) {
632
633         if (id >= methodOffset) {
634
635             id -= methodOffset;
636             int plainSignals = metaData->signalCount + metaData->propertyCount +
637                                metaData->aliasCount;
638             if (id < plainSignals) {
639                 QMetaObject::activate(object, _id, a);
640                 return -1;
641             }
642
643             id -= plainSignals;
644
645             if (id < metaData->methodCount) {
646                 if (!ctxt->engine)
647                     return -1; // We can't run the method
648
649                 QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(ctxt->engine);
650
651                 QScriptValue function = method(id);
652
653                 QScriptValueList args;
654                 QDeclarativeVMEMetaData::MethodData *data = metaData->methodData() + id;
655                 if (data->parameterCount) {
656                     for (int ii = 0; ii < data->parameterCount; ++ii) {
657                         args << ep->scriptValueFromVariant(*(QVariant *)a[ii + 1]);
658                     }
659                 }
660                 QScriptValue rv = function.call(ep->objectClass->newQObject(object), args);
661
662                 if (a[0]) *reinterpret_cast<QVariant *>(a[0]) = ep->scriptValueToVariant(rv);
663
664                 return -1;
665             }
666             return -1;
667         }
668     }
669
670     if (parent)
671         return parent->metaCall(c, _id, a);
672     else
673         return object->qt_metacall(c, _id, a);
674 }
675
676 QScriptValue QDeclarativeVMEMetaObject::method(int index)
677 {
678     if (!methods) 
679         methods = new QScriptValue[metaData->methodCount];
680
681     if (!methods[index].isValid()) {
682         QDeclarativeVMEMetaData::MethodData *data = metaData->methodData() + index;
683
684         const QChar *body = 
685             (const QChar *)(((const char*)metaData) + data->bodyOffset);
686
687         QString code = QString::fromRawData(body, data->bodyLength);
688
689         // XXX Use QScriptProgram
690         // XXX We should evaluate all methods in a single big script block to 
691         // improve the call time between dynamic methods defined on the same
692         // object
693         methods[index] = QDeclarativeExpressionPrivate::evalInObjectScope(ctxt, object, code, ctxt->url.toString(),
694                                                                           data->lineNumber, 0);
695     }
696
697     return methods[index];
698 }
699
700 QScriptValue QDeclarativeVMEMetaObject::readVarProperty(int id)
701 {
702     if (data[id].dataType() == qMetaTypeId<QScriptValue>())
703         return data[id].asQScriptValue();
704     else if (data[id].dataType() == QMetaType::QObjectStar) 
705         return QDeclarativeEnginePrivate::get(ctxt->engine)->objectClass->newQObject(data[id].asQObject());
706     else
707         return QDeclarativeEnginePrivate::get(ctxt->engine)->scriptValueFromVariant(data[id].asQVariant());
708 }
709
710 QVariant QDeclarativeVMEMetaObject::readVarPropertyAsVariant(int id)
711 {
712     if (data[id].dataType() == qMetaTypeId<QScriptValue>())
713         return QDeclarativeEnginePrivate::get(ctxt->engine)->scriptValueToVariant(data[id].asQScriptValue());
714     else if (data[id].dataType() == QMetaType::QObjectStar) 
715         return QVariant::fromValue(data[id].asQObject());
716     else 
717         return data[id].asQVariant();
718 }
719
720 void QDeclarativeVMEMetaObject::writeVarProperty(int id, const QScriptValue &value)
721 {
722     data[id].setValue(value);
723     activate(object, methodOffset + id, 0);
724 }
725
726 void QDeclarativeVMEMetaObject::writeVarProperty(int id, const QVariant &value)
727 {
728     bool needActivate = false;
729     if (value.userType() == QMetaType::QObjectStar) {
730         QObject *o = qvariant_cast<QObject *>(value);
731         needActivate = (data[id].dataType() != QMetaType::QObjectStar || data[id].asQObject() != o);
732         data[id].setValue(qvariant_cast<QObject *>(value));
733     } else {
734         needActivate = (data[id].dataType() != qMetaTypeId<QVariant>() || 
735                         data[id].asQVariant().userType() != value.userType() || 
736                         data[id].asQVariant() != value);
737         data[id].setValue(value);
738     }
739     if (needActivate)
740         activate(object, methodOffset + id, 0);
741 }
742
743 void QDeclarativeVMEMetaObject::listChanged(int id)
744 {
745     activate(object, methodOffset + id, 0);
746 }
747
748 void QDeclarativeVMEMetaObject::list_append(QDeclarativeListProperty<QObject> *prop, QObject *o)
749 {
750     List *list = static_cast<List *>(prop->data);
751     list->append(o);
752     QMetaObject::activate(prop->object, list->notifyIndex, 0);
753 }
754
755 int QDeclarativeVMEMetaObject::list_count(QDeclarativeListProperty<QObject> *prop)
756 {
757     return static_cast<List *>(prop->data)->count();
758 }
759
760 QObject *QDeclarativeVMEMetaObject::list_at(QDeclarativeListProperty<QObject> *prop, int index)
761 {
762     return static_cast<List *>(prop->data)->at(index);
763 }
764
765 void QDeclarativeVMEMetaObject::list_clear(QDeclarativeListProperty<QObject> *prop)
766 {
767     List *list = static_cast<List *>(prop->data);
768     list->clear();
769     QMetaObject::activate(prop->object, list->notifyIndex, 0);
770 }
771
772 void QDeclarativeVMEMetaObject::registerInterceptor(int index, int valueIndex, QDeclarativePropertyValueInterceptor *interceptor)
773 {
774     if (aInterceptors.isEmpty())
775         aInterceptors.resize(propertyCount() + metaData->propertyCount);
776     aInterceptors.setBit(index);
777     interceptors.insert(index, qMakePair(valueIndex, interceptor));
778 }
779
780 int QDeclarativeVMEMetaObject::vmeMethodLineNumber(int index)
781 {
782     if (index < methodOffset) {
783         Q_ASSERT(parent);
784         return static_cast<QDeclarativeVMEMetaObject *>(parent)->vmeMethodLineNumber(index);
785     }
786
787     int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
788     Q_ASSERT(index >= (methodOffset + plainSignals) && index < (methodOffset + plainSignals + metaData->methodCount));
789
790     int rawIndex = index - methodOffset - plainSignals;
791
792     QDeclarativeVMEMetaData::MethodData *data = metaData->methodData() + rawIndex;
793     return data->lineNumber;
794 }
795
796 QScriptValue QDeclarativeVMEMetaObject::vmeMethod(int index)
797 {
798     if (index < methodOffset) {
799         Q_ASSERT(parent);
800         return static_cast<QDeclarativeVMEMetaObject *>(parent)->vmeMethod(index);
801     }
802     int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
803     Q_ASSERT(index >= (methodOffset + plainSignals) && index < (methodOffset + plainSignals + metaData->methodCount));
804     return method(index - methodOffset - plainSignals);
805 }
806
807 void QDeclarativeVMEMetaObject::setVmeMethod(int index, const QScriptValue &value)
808 {
809     if (index < methodOffset) {
810         Q_ASSERT(parent);
811         return static_cast<QDeclarativeVMEMetaObject *>(parent)->setVmeMethod(index, value);
812     }
813     int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
814     Q_ASSERT(index >= (methodOffset + plainSignals) && index < (methodOffset + plainSignals + metaData->methodCount));
815
816     if (!methods) 
817         methods = new QScriptValue[metaData->methodCount];
818     methods[index - methodOffset - plainSignals] = value;
819 }
820
821 QScriptValue QDeclarativeVMEMetaObject::vmeProperty(int index)
822 {
823     if (index < propOffset) {
824         Q_ASSERT(parent);
825         return static_cast<QDeclarativeVMEMetaObject *>(parent)->vmeProperty(index);
826     }
827     return readVarProperty(index - propOffset);
828 }
829
830 void QDeclarativeVMEMetaObject::setVMEProperty(int index, const QScriptValue &v)
831 {
832     if (index < propOffset) {
833         Q_ASSERT(parent);
834         static_cast<QDeclarativeVMEMetaObject *>(parent)->setVMEProperty(index, v);
835     }
836     return writeVarProperty(index - propOffset, v);
837 }
838
839 bool QDeclarativeVMEMetaObject::aliasTarget(int index, QObject **target, int *coreIndex, int *valueTypeIndex) const
840 {
841     Q_ASSERT(index >= propOffset + metaData->propertyCount);
842
843     *target = 0;
844     *coreIndex = -1;
845     *valueTypeIndex = -1;
846
847     if (!ctxt)
848         return false;
849
850     QDeclarativeVMEMetaData::AliasData *d = metaData->aliasData() + (index - propOffset - metaData->propertyCount);
851     QDeclarativeContext *context = ctxt->asQDeclarativeContext();
852     QDeclarativeContextPrivate *ctxtPriv = QDeclarativeContextPrivate::get(context);
853
854     *target = ctxtPriv->data->idValues[d->contextIdx].data();
855     if (!*target)
856         return false;
857
858     if (d->isObjectAlias()) {
859     } else if (d->isValueTypeAlias()) {
860         *coreIndex = d->propertyIndex();
861         *valueTypeIndex = d->valueTypeIndex();
862     } else {
863         *coreIndex = d->propertyIndex();
864     }
865
866     return true;
867 }
868
869 void QDeclarativeVMEMetaObject::connectAlias(int aliasId)
870 {
871     if (!aConnected.testBit(aliasId)) {
872         aConnected.setBit(aliasId);
873
874         QDeclarativeContext *context = ctxt->asQDeclarativeContext();
875         QDeclarativeContextPrivate *ctxtPriv = QDeclarativeContextPrivate::get(context);
876
877         QDeclarativeVMEMetaData::AliasData *d = metaData->aliasData() + aliasId;
878
879         QObject *target = ctxtPriv->data->idValues[d->contextIdx].data();
880         if (!target) 
881             return;
882
883         int sigIdx = methodOffset + aliasId + metaData->propertyCount;
884         QMetaObject::connect(context, d->contextIdx + ctxtPriv->notifyIndex, object, sigIdx);
885
886         if (!d->isObjectAlias()) {
887             QMetaProperty prop = target->metaObject()->property(d->propertyIndex());
888             if (prop.hasNotifySignal())
889                 QDeclarativePropertyPrivate::connect(target, prop.notifySignalIndex(), object, sigIdx);
890         }
891     }
892 }
893
894 void QDeclarativeVMEMetaObject::connectAliasSignal(int index)
895 {
896     int aliasId = (index - methodOffset) - metaData->propertyCount;
897     if (aliasId < 0 || aliasId >= metaData->aliasCount)
898         return;
899
900     connectAlias(aliasId);
901 }
902
903 QT_END_NAMESPACE