Improve QJSValueIterator implementation.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativecompileddata.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 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "private/qdeclarativecompiler_p.h"
43 #include "qdeclarativeengine.h"
44 #include "qdeclarativecomponent.h"
45 #include "private/qdeclarativecomponent_p.h"
46 #include "qdeclarativecontext.h"
47 #include "private/qdeclarativecontext_p.h"
48
49 #include <QtCore/qdebug.h>
50
51 #include <private/qobject_p.h>
52
53 QT_BEGIN_NAMESPACE
54
55 int QDeclarativeCompiledData::pack(const char *data, size_t size)
56 {
57     const char *p = packData.constData();
58     unsigned int ps = packData.size();
59
60     for (unsigned int ii = 0; (ii + size) <= ps; ii += sizeof(int)) {
61         if (0 == ::memcmp(p + ii, data, size))
62             return ii;
63     }
64
65     int rv = packData.size();
66     packData.append(data, size);
67     return rv;
68 }
69
70 int QDeclarativeCompiledData::indexForString(const QString &data)
71 {
72     int idx = primitives.indexOf(data);
73     if (idx == -1) {
74         idx = primitives.count();
75         primitives << data;
76     }
77     return idx;
78 }
79
80 int QDeclarativeCompiledData::indexForByteArray(const QByteArray &data)
81 {
82     int idx = datas.indexOf(data);
83     if (idx == -1) {
84         idx = datas.count();
85         datas << data;
86     }
87     return idx;
88 }
89
90 int QDeclarativeCompiledData::indexForUrl(const QUrl &data)
91 {
92     int idx = urls.indexOf(data);
93     if (idx == -1) {
94         idx = urls.count();
95         urls << data;
96     }
97     return idx;
98 }
99
100 QDeclarativeCompiledData::QDeclarativeCompiledData(QDeclarativeEngine *engine)
101 : QDeclarativeCleanup(engine), importCache(0), root(0), rootPropertyCache(0)
102 {
103 }
104
105 QDeclarativeCompiledData::~QDeclarativeCompiledData()
106 {
107     for (int ii = 0; ii < types.count(); ++ii) {
108         if (types.at(ii).component)
109             types.at(ii).component->release();
110         if (types.at(ii).typePropertyCache)
111             types.at(ii).typePropertyCache->release();
112     }
113
114     for (int ii = 0; ii < propertyCaches.count(); ++ii) 
115         propertyCaches.at(ii)->release();
116
117     for (int ii = 0; ii < contextCaches.count(); ++ii)
118         contextCaches.at(ii)->release();
119
120     for (int ii = 0; ii < scripts.count(); ++ii)
121         scripts.at(ii)->release();
122
123     if (importCache)
124         importCache->release();
125
126     if (rootPropertyCache)
127         rootPropertyCache->release();
128
129     qDeleteAll(cachedClosures);
130
131     for (int ii = 0; ii < v8bindings.count(); ++ii)
132         qPersistentDispose(v8bindings[ii]);
133 }
134
135 void QDeclarativeCompiledData::clear()
136 {
137     qDeleteAll(cachedClosures);
138     for (int ii = 0; ii < cachedClosures.count(); ++ii)
139         cachedClosures[ii] = 0;
140 }
141
142 const QMetaObject *QDeclarativeCompiledData::TypeReference::metaObject() const
143 {
144     if (type) {
145         return type->metaObject();
146     } else {
147         Q_ASSERT(component);
148         return component->root;
149     }
150 }
151
152 /*!
153 Returns the property cache, if one alread exists.  The cache is not referenced.
154 */
155 QDeclarativePropertyCache *QDeclarativeCompiledData::TypeReference::propertyCache() const
156 {
157     if (type)
158         return typePropertyCache;
159     else
160         return component->rootPropertyCache;
161 }
162
163 /*!
164 Returns the property cache, creating one if it doesn't already exist.  The cache is not referenced.
165 */
166 QDeclarativePropertyCache *QDeclarativeCompiledData::TypeReference::createPropertyCache(QDeclarativeEngine *engine) 
167 {
168     if (typePropertyCache) {
169         return typePropertyCache;
170     } else if (type) {
171         typePropertyCache = QDeclarativeEnginePrivate::get(engine)->cache(type->metaObject());
172         typePropertyCache->addref();
173         return typePropertyCache;
174     } else {
175         return component->rootPropertyCache;
176     }
177 }
178
179
180 void QDeclarativeCompiledData::dumpInstructions()
181 {
182     if (!name.isEmpty())
183         qWarning() << name;
184     qWarning().nospace() << "Index\tOperation\t\tData1\tData2\tData3\tComments";
185     qWarning().nospace() << "-------------------------------------------------------------------------------";
186
187     const char *instructionStream = bytecode.constData();
188     const char *endInstructionStream = bytecode.constData() + bytecode.size();
189
190     int instructionCount = 0;
191     while (instructionStream < endInstructionStream) {
192         QDeclarativeInstruction *instr = (QDeclarativeInstruction *)instructionStream;
193         dump(instr, instructionCount);
194         instructionStream += instr->size();
195         instructionCount++;
196     }
197
198     qWarning().nospace() << "-------------------------------------------------------------------------------";
199 }
200
201 int QDeclarativeCompiledData::addInstruction(const QDeclarativeInstruction &instr) 
202
203     int ptrOffset = bytecode.size();
204     int size = instr.size();
205     bytecode.resize(bytecode.size() + size);
206     char *data = bytecode.data() + ptrOffset;
207     qMemCopy(data, &instr,  size);
208
209     return ptrOffset;
210 }
211
212 int QDeclarativeCompiledData::nextInstructionIndex() 
213
214     return bytecode.size();
215 }
216
217 QDeclarativeInstruction *QDeclarativeCompiledData::instruction(int index) 
218
219     return (QDeclarativeInstruction *)(bytecode.constData() + index);
220 }
221
222 QT_END_NAMESPACE