Add missing QT_{BEGIN,END}_NAMESPACE
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmlcompileddata.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 "qqmlcompiler_p.h"
43 #include "qqmlengine.h"
44 #include "qqmlcomponent.h"
45 #include "qqmlcomponent_p.h"
46 #include "qqmlcontext.h"
47 #include "qqmlcontext_p.h"
48 #ifdef QML_THREADED_VME_INTERPRETER
49 #include "qqmlvme_p.h"
50 #endif
51
52 #include <QtCore/qdebug.h>
53
54 #include <private/qobject_p.h>
55
56 QT_BEGIN_NAMESPACE
57
58 int QQmlCompiledData::indexForString(const QString &data)
59 {
60     int idx = primitives.indexOf(data);
61     if (idx == -1) {
62         idx = primitives.count();
63         primitives << data;
64     }
65     return idx;
66 }
67
68 int QQmlCompiledData::indexForByteArray(const QByteArray &data)
69 {
70     int idx = datas.indexOf(data);
71     if (idx == -1) {
72         idx = datas.count();
73         datas << data;
74     }
75     return idx;
76 }
77
78 int QQmlCompiledData::indexForUrl(const QUrl &data)
79 {
80     int idx = urls.indexOf(data);
81     if (idx == -1) {
82         idx = urls.count();
83         urls << data;
84     }
85     return idx;
86 }
87
88 QQmlCompiledData::QQmlCompiledData(QQmlEngine *engine)
89 : engine(engine), importCache(0), metaTypeId(-1), listMetaTypeId(-1), isRegisteredWithEngine(false),
90   rootPropertyCache(0)
91 {
92     Q_ASSERT(engine);
93
94     bytecode.reserve(1024);
95 }
96
97 void QQmlCompiledData::destroy()
98 {
99     if (engine && hasEngine())
100         QQmlEnginePrivate::deleteInEngineThread(engine, this);
101     else
102         delete this;
103 }
104
105 QQmlCompiledData::~QQmlCompiledData()
106 {
107     if (isRegisteredWithEngine)
108         QQmlEnginePrivate::get(engine)->unregisterCompositeType(this);
109
110     clear();
111
112     for (int ii = 0; ii < types.count(); ++ii) {
113         if (types.at(ii).component)
114             types.at(ii).component->release();
115         if (types.at(ii).typePropertyCache)
116             types.at(ii).typePropertyCache->release();
117     }
118
119     for (int ii = 0; ii < propertyCaches.count(); ++ii) 
120         propertyCaches.at(ii)->release();
121
122     for (int ii = 0; ii < contextCaches.count(); ++ii)
123         contextCaches.at(ii)->release();
124
125     for (int ii = 0; ii < scripts.count(); ++ii)
126         scripts.at(ii)->release();
127
128     if (importCache)
129         importCache->release();
130
131     if (rootPropertyCache)
132         rootPropertyCache->release();
133 }
134
135 void QQmlCompiledData::clear()
136 {
137     for (int ii = 0; ii < programs.count(); ++ii)
138         qPersistentDispose(programs[ii].bindings);
139 }
140
141 /*!
142 Returns the property cache, if one alread exists.  The cache is not referenced.
143 */
144 QQmlPropertyCache *QQmlCompiledData::TypeReference::propertyCache() const
145 {
146     if (type)
147         return typePropertyCache;
148     else
149         return component->rootPropertyCache;
150 }
151
152 /*!
153 Returns the property cache, creating one if it doesn't already exist.  The cache is not referenced.
154 */
155 QQmlPropertyCache *QQmlCompiledData::TypeReference::createPropertyCache(QQmlEngine *engine) 
156 {
157     if (typePropertyCache) {
158         return typePropertyCache;
159     } else if (type) {
160         typePropertyCache = QQmlEnginePrivate::get(engine)->cache(type->metaObject());
161         typePropertyCache->addref();
162         return typePropertyCache;
163     } else {
164         return component->rootPropertyCache;
165     }
166 }
167
168
169 void QQmlCompiledData::dumpInstructions()
170 {
171     if (!name.isEmpty())
172         qWarning() << name;
173     qWarning().nospace() << "Index\tOperation\t\tData1\tData2\tData3\tComments";
174     qWarning().nospace() << "-------------------------------------------------------------------------------";
175
176     const char *instructionStream = bytecode.constData();
177     const char *endInstructionStream = bytecode.constData() + bytecode.size();
178
179     int instructionCount = 0;
180     while (instructionStream < endInstructionStream) {
181         QQmlInstruction *instr = (QQmlInstruction *)instructionStream;
182         dump(instr, instructionCount);
183         instructionStream += QQmlInstruction::size(instructionType(instr));
184         instructionCount++;
185     }
186
187     qWarning().nospace() << "-------------------------------------------------------------------------------";
188 }
189
190 int QQmlCompiledData::addInstructionHelper(QQmlInstruction::Type type, QQmlInstruction &instr)
191 {
192 #ifdef QML_THREADED_VME_INTERPRETER
193     instr.common.code = QQmlVME::instructionJumpTable()[static_cast<int>(type)];
194 #else
195     instr.common.instructionType = type;
196 #endif
197     int ptrOffset = bytecode.size();
198     int size = QQmlInstruction::size(type);
199     if (bytecode.capacity() <= bytecode.size() + size)
200         bytecode.reserve(bytecode.size() + size + 512);
201     bytecode.append(reinterpret_cast<const char *>(&instr), size);
202     return ptrOffset;
203 }
204
205 int QQmlCompiledData::nextInstructionIndex() 
206
207     return bytecode.size();
208 }
209
210 QQmlInstruction *QQmlCompiledData::instruction(int index) 
211
212     return (QQmlInstruction *)(bytecode.constData() + index);
213 }
214
215 QQmlInstruction::Type QQmlCompiledData::instructionType(const QQmlInstruction *instr)
216 {
217 #ifdef QML_THREADED_VME_INTERPRETER
218     void **jumpTable = QQmlVME::instructionJumpTable();
219     void *code = instr->common.code;
220
221 #  define QML_CHECK_INSTR_CODE(I, FMT) \
222     if (jumpTable[static_cast<int>(QQmlInstruction::I)] == code) \
223         return QQmlInstruction::I;
224
225     FOR_EACH_QML_INSTR(QML_CHECK_INSTR_CODE)
226     Q_ASSERT_X(false, Q_FUNC_INFO, "Invalid instruction address");
227     return static_cast<QQmlInstruction::Type>(0);
228 #  undef QML_CHECK_INSTR_CODE
229 #else
230     return static_cast<QQmlInstruction::Type>(instr->common.instructionType);
231 #endif
232 }
233
234 void QQmlCompiledData::initialize(QQmlEngine *engine)
235 {
236     Q_ASSERT(!hasEngine());
237     QQmlCleanup::addToEngine(engine);
238 }
239
240 QT_END_NAMESPACE