Optimizations to imports.
[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     bytecode.reserve(1024);
104 }
105
106 QDeclarativeCompiledData::~QDeclarativeCompiledData()
107 {
108     for (int ii = 0; ii < types.count(); ++ii) {
109         if (types.at(ii).component)
110             types.at(ii).component->release();
111         if (types.at(ii).typePropertyCache)
112             types.at(ii).typePropertyCache->release();
113     }
114
115     for (int ii = 0; ii < propertyCaches.count(); ++ii) 
116         propertyCaches.at(ii)->release();
117
118     for (int ii = 0; ii < contextCaches.count(); ++ii)
119         contextCaches.at(ii)->release();
120
121     for (int ii = 0; ii < scripts.count(); ++ii)
122         scripts.at(ii)->release();
123
124     if (importCache)
125         importCache->release();
126
127     if (rootPropertyCache)
128         rootPropertyCache->release();
129
130     qDeleteAll(cachedClosures);
131
132     for (int ii = 0; ii < v8bindings.count(); ++ii)
133         qPersistentDispose(v8bindings[ii]);
134 }
135
136 void QDeclarativeCompiledData::clear()
137 {
138     qDeleteAll(cachedClosures);
139     for (int ii = 0; ii < cachedClosures.count(); ++ii)
140         cachedClosures[ii] = 0;
141 }
142
143 const QMetaObject *QDeclarativeCompiledData::TypeReference::metaObject() const
144 {
145     if (type) {
146         return type->metaObject();
147     } else {
148         Q_ASSERT(component);
149         return component->root;
150     }
151 }
152
153 /*!
154 Returns the property cache, if one alread exists.  The cache is not referenced.
155 */
156 QDeclarativePropertyCache *QDeclarativeCompiledData::TypeReference::propertyCache() const
157 {
158     if (type)
159         return typePropertyCache;
160     else
161         return component->rootPropertyCache;
162 }
163
164 /*!
165 Returns the property cache, creating one if it doesn't already exist.  The cache is not referenced.
166 */
167 QDeclarativePropertyCache *QDeclarativeCompiledData::TypeReference::createPropertyCache(QDeclarativeEngine *engine) 
168 {
169     if (typePropertyCache) {
170         return typePropertyCache;
171     } else if (type) {
172         typePropertyCache = QDeclarativeEnginePrivate::get(engine)->cache(type->metaObject());
173         typePropertyCache->addref();
174         return typePropertyCache;
175     } else {
176         return component->rootPropertyCache;
177     }
178 }
179
180
181 void QDeclarativeCompiledData::dumpInstructions()
182 {
183     if (!name.isEmpty())
184         qWarning() << name;
185     qWarning().nospace() << "Index\tOperation\t\tData1\tData2\tData3\tComments";
186     qWarning().nospace() << "-------------------------------------------------------------------------------";
187
188     const char *instructionStream = bytecode.constData();
189     const char *endInstructionStream = bytecode.constData() + bytecode.size();
190
191     int instructionCount = 0;
192     while (instructionStream < endInstructionStream) {
193         QDeclarativeInstruction *instr = (QDeclarativeInstruction *)instructionStream;
194         dump(instr, instructionCount);
195         instructionStream += instr->size();
196         instructionCount++;
197     }
198
199     qWarning().nospace() << "-------------------------------------------------------------------------------";
200 }
201
202 int QDeclarativeCompiledData::addInstruction(const QDeclarativeInstruction &instr) 
203
204     int ptrOffset = bytecode.size();
205     int size = instr.size();
206     if (bytecode.capacity() <= bytecode.size() + size)
207         bytecode.reserve(bytecode.size() + size + 512);
208     bytecode.append(reinterpret_cast<const char *>(&instr), size);
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