Implement strict mode for qmldir modules
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmlvmemetaobject_p.h
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 #ifndef QQMLVMEMETAOBJECT_P_H
43 #define QQMLVMEMETAOBJECT_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include "qqml.h"
57
58 #include <QtCore/QMetaObject>
59 #include <QtCore/QBitArray>
60 #include <QtCore/QPair>
61 #include <QtCore/QDate>
62 #include <QtCore/qlist.h>
63 #include <QtCore/qdebug.h>
64
65 #include <private/qobject_p.h>
66
67 #include "qqmlguard_p.h"
68 #include "qqmlcompiler_p.h"
69 #include "qqmlcontext_p.h"
70
71 #include <private/qv8engine_p.h>
72 #include <private/qflagpointer_p.h>
73
74 #include <private/qv8_p.h>
75
76 QT_BEGIN_NAMESPACE
77
78 #define QML_ALIAS_FLAG_PTR 0x00000001
79
80 struct QQmlVMEMetaData
81 {
82     short varPropertyCount;
83     short propertyCount;
84     short aliasCount;
85     short signalCount;
86     short methodCount;
87     short dummyForAlignment; // Add padding to ensure that the following
88                              // AliasData/PropertyData/MethodData is int aligned.
89
90     struct AliasData {
91         int contextIdx;
92         int propertyIdx;
93         int flags;
94         int notifySignal;
95
96         bool isObjectAlias() const {
97             return propertyIdx == -1;
98         }
99         bool isPropertyAlias() const {
100             return !isObjectAlias() && !(propertyIdx & 0xFF000000);
101         }
102         bool isValueTypeAlias() const {
103             return !isObjectAlias() && (propertyIdx & 0xFF000000);
104         }
105         int propertyIndex() const {
106             return propertyIdx & 0x0000FFFF;
107         }
108         int valueTypeIndex() const {
109             return (propertyIdx & 0x00FF0000) >> 16;
110         }
111         int valueType() const {
112             return ((unsigned int)propertyIdx) >> 24;
113         }
114     };
115     
116     struct PropertyData {
117         int propertyType;
118     };
119
120     struct MethodData {
121         int parameterCount;
122         int bodyOffset;
123         int bodyLength;
124         int lineNumber;
125     };
126
127     PropertyData *propertyData() const {
128         return (PropertyData *)(((const char *)this) + sizeof(QQmlVMEMetaData));
129     }
130
131     AliasData *aliasData() const {
132         return (AliasData *)(propertyData() + propertyCount);
133     }
134
135     MethodData *methodData() const {
136         return (MethodData *)(aliasData() + aliasCount);
137     }
138 };
139
140 class QQmlVMEMetaObject;
141 class QQmlVMEVariantQObjectPtr : public QQmlGuard<QObject>
142 {
143 public:
144     inline QQmlVMEVariantQObjectPtr(bool isVar);
145     inline ~QQmlVMEVariantQObjectPtr();
146
147     inline void objectDestroyed(QObject *);
148     inline void setGuardedValue(QObject *obj, QQmlVMEMetaObject *target, int index);
149
150     QQmlVMEMetaObject *m_target;
151     unsigned m_isVar : 1;
152     int m_index : 31;
153 };
154
155 class QV8QObjectWrapper;
156 class QQmlVMEVariant;
157 class QQmlRefCount;
158 class QQmlVMEMetaObjectEndpoint;
159 class Q_AUTOTEST_EXPORT QQmlVMEMetaObject : public QAbstractDynamicMetaObject,
160                                                     public QV8GCCallback::Node
161 {
162 public:
163     QQmlVMEMetaObject(QObject *obj, QQmlPropertyCache *cache, const QQmlVMEMetaData *data);
164     ~QQmlVMEMetaObject();
165
166     bool aliasTarget(int index, QObject **target, int *coreIndex, int *valueTypeIndex) const;
167     void registerInterceptor(int index, int valueIndex, QQmlPropertyValueInterceptor *interceptor);
168     v8::Handle<v8::Function> vmeMethod(int index);
169     int vmeMethodLineNumber(int index);
170     void setVmeMethod(int index, v8::Persistent<v8::Function>);
171     v8::Handle<v8::Value> vmeProperty(int index);
172     void setVMEProperty(int index, v8::Handle<v8::Value> v);
173
174     void connectAliasSignal(int index, bool indexInSignalRange);
175
176     virtual QAbstractDynamicMetaObject *toDynamicMetaObject(QObject *o);
177
178     static inline QQmlVMEMetaObject *get(QObject *o);
179     static QQmlVMEMetaObject *getForProperty(QObject *o, int coreIndex);
180     static QQmlVMEMetaObject *getForMethod(QObject *o, int coreIndex);
181     static QQmlVMEMetaObject *getForSignal(QObject *o, int coreIndex);
182
183 protected:
184     virtual int metaCall(QMetaObject::Call _c, int _id, void **_a);
185
186 public:
187     friend class QQmlVMEMetaObjectEndpoint;
188     friend class QQmlVMEVariantQObjectPtr;
189
190     QObject *object;
191     QQmlGuardedContextData ctxt;
192     QQmlPropertyCache *cache;
193
194     const QQmlVMEMetaData *metaData;
195     inline int propOffset() const;
196     inline int methodOffset() const;
197     inline int signalOffset() const;
198
199     bool hasAssignedMetaObjectData;
200     QQmlVMEVariant *data;
201     QQmlVMEMetaObjectEndpoint *aliasEndpoints;
202
203     v8::Persistent<v8::Array> varProperties;
204     int firstVarPropertyIndex;
205     bool varPropertiesInitialized;
206     static void VarPropertiesWeakReferenceCallback(v8::Persistent<v8::Value> object, void* parameter);
207     static void GcPrologueCallback(QV8GCCallback::Node *node);
208     inline void allocateVarPropertiesArray();
209     inline bool ensureVarPropertiesAllocated();
210
211     void connectAlias(int aliasId);
212     QBitArray aConnected;
213
214     QQmlPropertyValueInterceptor *interceptors;
215
216     v8::Persistent<v8::Function> *v8methods;
217     v8::Handle<v8::Function> method(int);
218
219     v8::Handle<v8::Value> readVarProperty(int);
220     void writeVarProperty(int, v8::Handle<v8::Value>);
221     QVariant readPropertyAsVariant(int);
222     void writeProperty(int, const QVariant &);
223
224     QBiPointer<QDynamicMetaObjectData, const QMetaObject> parent;
225
226     void listChanged(int);
227     class List : public QList<QObject*>
228     {
229     public:
230         List(int lpi, QQmlVMEMetaObject *mo) : notifyIndex(lpi), mo(mo) {}
231         int notifyIndex;
232         QQmlVMEMetaObject *mo;
233     };
234     QList<List> listProperties;
235
236     static void list_append(QQmlListProperty<QObject> *, QObject *);
237     static int list_count(QQmlListProperty<QObject> *);
238     static QObject *list_at(QQmlListProperty<QObject> *, int);
239     static void list_clear(QQmlListProperty<QObject> *);
240
241     void activate(QObject *, int, void **);
242
243     QList<QQmlVMEVariantQObjectPtr *> varObjectGuards;
244
245     QQmlVMEVariantQObjectPtr *getQObjectGuardForProperty(int) const;
246
247     friend class QV8GCCallback;
248     friend class QV8QObjectWrapper;
249 };
250
251 QQmlVMEMetaObject *QQmlVMEMetaObject::get(QObject *obj)
252 {
253     if (obj) {
254         if (QQmlData *data = QQmlData::get(obj)) {
255             if (data->hasVMEMetaObject)
256                 return static_cast<QQmlVMEMetaObject *>(QObjectPrivate::get(obj)->metaObject);
257         }
258     }
259
260     return 0;
261 }
262
263 int QQmlVMEMetaObject::propOffset() const
264 {
265     return cache->propertyOffset();
266 }
267
268 int QQmlVMEMetaObject::methodOffset() const
269 {
270     return cache->methodOffset();
271 }
272
273 int QQmlVMEMetaObject::signalOffset() const
274 {
275     return cache->signalOffset();
276 }
277
278 QT_END_NAMESPACE
279
280 #endif // QQMLVMEMETAOBJECT_P_H