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 ** 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.
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.
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.
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.
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 QDeclarativeCompiledData::QDeclarativeCompiledData(QDeclarativeEngine *engine)
101 : QDeclarativeCleanup(engine), importCache(0), root(0), rootPropertyCache(0)
105 QDeclarativeCompiledData::~QDeclarativeCompiledData()
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();
114 for (int ii = 0; ii < propertyCaches.count(); ++ii)
115 propertyCaches.at(ii)->release();
117 for (int ii = 0; ii < contextCaches.count(); ++ii)
118 contextCaches.at(ii)->release();
120 for (int ii = 0; ii < scripts.count(); ++ii)
121 scripts.at(ii)->release();
124 importCache->release();
126 if (rootPropertyCache)
127 rootPropertyCache->release();
129 qDeleteAll(cachedPrograms);
130 qDeleteAll(cachedClosures);
133 void QDeclarativeCompiledData::clear()
135 qDeleteAll(cachedPrograms);
136 qDeleteAll(cachedClosures);
137 for (int ii = 0; ii < cachedClosures.count(); ++ii)
138 cachedClosures[ii] = 0;
139 for (int ii = 0; ii < cachedPrograms.count(); ++ii)
140 cachedPrograms[ii] = 0;
143 const QMetaObject *QDeclarativeCompiledData::TypeReference::metaObject() const
146 return type->metaObject();
149 return component->root;
154 Returns the property cache, if one alread exists. The cache is not referenced.
156 QDeclarativePropertyCache *QDeclarativeCompiledData::TypeReference::propertyCache() const
159 return typePropertyCache;
161 return component->rootPropertyCache;
165 Returns the property cache, creating one if it doesn't already exist. The cache is not referenced.
167 QDeclarativePropertyCache *QDeclarativeCompiledData::TypeReference::createPropertyCache(QDeclarativeEngine *engine)
169 if (typePropertyCache) {
170 return typePropertyCache;
172 typePropertyCache = QDeclarativeEnginePrivate::get(engine)->cache(type->metaObject());
173 typePropertyCache->addref();
174 return typePropertyCache;
176 return component->rootPropertyCache;
181 void QDeclarativeCompiledData::dumpInstructions()
185 qWarning().nospace() << "Index\tOperation\t\tData1\tData2\tData3\tComments";
186 qWarning().nospace() << "-------------------------------------------------------------------------------";
188 const char *instructionStream = bytecode.constData();
189 const char *endInstructionStream = bytecode.constData() + bytecode.size();
191 int instructionCount = 0;
192 while (instructionStream < endInstructionStream) {
193 QDeclarativeInstruction *instr = (QDeclarativeInstruction *)instructionStream;
194 dump(instr, instructionCount);
195 instructionStream += instr->size();
199 qWarning().nospace() << "-------------------------------------------------------------------------------";
202 int QDeclarativeCompiledData::addInstruction(const QDeclarativeInstruction &instr)
204 int ptrOffset = bytecode.size();
205 int size = instr.size();
206 bytecode.resize(bytecode.size() + size);
207 char *data = bytecode.data() + ptrOffset;
208 qMemCopy(data, &instr, size);
213 int QDeclarativeCompiledData::nextInstructionIndex()
215 return bytecode.size();
218 QDeclarativeInstruction *QDeclarativeCompiledData::instruction(int index)
220 return (QDeclarativeInstruction *)(bytecode.constData() + index);