1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
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"
49 #include <QtCore/qdebug.h>
51 #include <private/qobject_p.h>
55 int QDeclarativeCompiledData::pack(const char *data, size_t size)
57 const char *p = packData.constData();
58 unsigned int ps = packData.size();
60 for (unsigned int ii = 0; (ii + size) <= ps; ii += sizeof(int)) {
61 if (0 == ::memcmp(p + ii, data, size))
65 int rv = packData.size();
66 packData.append(data, size);
70 int QDeclarativeCompiledData::indexForString(const QString &data)
72 int idx = primitives.indexOf(data);
74 idx = primitives.count();
80 int QDeclarativeCompiledData::indexForByteArray(const QByteArray &data)
82 int idx = datas.indexOf(data);
90 int QDeclarativeCompiledData::indexForUrl(const QUrl &data)
92 int idx = urls.indexOf(data);
100 int QDeclarativeCompiledData::indexForFloat(float *data, int count)
104 for (int ii = 0; ii <= floatData.count() - count; ++ii) {
106 for (int jj = 0; jj < count; ++jj) {
107 if (floatData.at(ii + jj) != data[jj]) {
117 int idx = floatData.count();
118 for (int ii = 0; ii < count; ++ii)
119 floatData << data[ii];
124 int QDeclarativeCompiledData::indexForInt(int *data, int count)
128 for (int ii = 0; ii <= intData.count() - count; ++ii) {
130 for (int jj = 0; jj < count; ++jj) {
131 if (intData.at(ii + jj) != data[jj]) {
141 int idx = intData.count();
142 for (int ii = 0; ii < count; ++ii)
148 int QDeclarativeCompiledData::indexForLocation(const QDeclarativeParser::Location &l)
151 int rv = locations.count();
156 int QDeclarativeCompiledData::indexForLocation(const QDeclarativeParser::LocationSpan &l)
159 int rv = locations.count();
160 locations << l.start << l.end;
164 QDeclarativeCompiledData::QDeclarativeCompiledData(QDeclarativeEngine *engine)
165 : QDeclarativeCleanup(engine), importCache(0), root(0), rootPropertyCache(0)
169 QDeclarativeCompiledData::~QDeclarativeCompiledData()
171 for (int ii = 0; ii < types.count(); ++ii) {
172 if (types.at(ii).component)
173 types.at(ii).component->release();
174 if (types.at(ii).typePropertyCache)
175 types.at(ii).typePropertyCache->release();
178 for (int ii = 0; ii < propertyCaches.count(); ++ii)
179 propertyCaches.at(ii)->release();
181 for (int ii = 0; ii < contextCaches.count(); ++ii)
182 contextCaches.at(ii)->release();
184 for (int ii = 0; ii < scripts.count(); ++ii)
185 scripts.at(ii)->release();
188 importCache->release();
190 if (rootPropertyCache)
191 rootPropertyCache->release();
193 qDeleteAll(cachedPrograms);
194 qDeleteAll(cachedClosures);
197 void QDeclarativeCompiledData::clear()
199 qDeleteAll(cachedPrograms);
200 qDeleteAll(cachedClosures);
201 for (int ii = 0; ii < cachedClosures.count(); ++ii)
202 cachedClosures[ii] = 0;
203 for (int ii = 0; ii < cachedPrograms.count(); ++ii)
204 cachedPrograms[ii] = 0;
207 const QMetaObject *QDeclarativeCompiledData::TypeReference::metaObject() const
210 return type->metaObject();
213 return component->root;
218 Returns the property cache, if one alread exists. The cache is not referenced.
220 QDeclarativePropertyCache *QDeclarativeCompiledData::TypeReference::propertyCache() const
223 return typePropertyCache;
225 return component->rootPropertyCache;
229 Returns the property cache, creating one if it doesn't already exist. The cache is not referenced.
231 QDeclarativePropertyCache *QDeclarativeCompiledData::TypeReference::createPropertyCache(QDeclarativeEngine *engine)
233 if (typePropertyCache) {
234 return typePropertyCache;
236 typePropertyCache = QDeclarativeEnginePrivate::get(engine)->cache(type->metaObject());
237 typePropertyCache->addref();
238 return typePropertyCache;
240 return component->rootPropertyCache;
245 void QDeclarativeCompiledData::dumpInstructions()
249 qWarning().nospace() << "Index\tOperation\t\tData1\tData2\tData3\tComments";
250 qWarning().nospace() << "-------------------------------------------------------------------------------";
252 const char *instructionStream = bytecode.constData();
253 const char *endInstructionStream = bytecode.constData() + bytecode.size();
255 int instructionCount = 0;
256 while (instructionStream < endInstructionStream) {
257 QDeclarativeInstruction *instr = (QDeclarativeInstruction *)instructionStream;
258 dump(instr, instructionCount);
259 instructionStream += instr->size();
263 qWarning().nospace() << "-------------------------------------------------------------------------------";
266 int QDeclarativeCompiledData::addInstruction(const QDeclarativeInstruction &instr)
268 int ptrOffset = bytecode.size();
269 int size = instr.size();
270 bytecode.resize(bytecode.size() + size);
271 char *data = bytecode.data() + ptrOffset;
272 qMemCopy(data, &instr, size);
277 int QDeclarativeCompiledData::nextInstructionIndex()
279 return bytecode.size();
282 QDeclarativeInstruction *QDeclarativeCompiledData::instruction(int index)
284 return (QDeclarativeInstruction *)(bytecode.constData() + index);