1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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"
52 #include <QtCore/qdebug.h>
54 #include <private/qobject_p.h>
58 int QQmlCompiledData::pack(const char *data, size_t size)
60 const char *p = packData.constData();
61 unsigned int ps = packData.size();
63 for (unsigned int ii = 0; (ii + size) <= ps; ii += sizeof(int)) {
64 if (0 == ::memcmp(p + ii, data, size))
68 int rv = packData.size();
69 packData.append(data, size);
73 int QQmlCompiledData::indexForString(const QString &data)
75 int idx = primitives.indexOf(data);
77 idx = primitives.count();
83 int QQmlCompiledData::indexForByteArray(const QByteArray &data)
85 int idx = datas.indexOf(data);
93 int QQmlCompiledData::indexForUrl(const QUrl &data)
95 int idx = urls.indexOf(data);
103 QQmlCompiledData::QQmlCompiledData(QQmlEngine *engine)
104 : engine(engine), importCache(0), root(0), rootPropertyCache(0)
108 bytecode.reserve(1024);
111 void QQmlCompiledData::destroy()
113 if (engine && hasEngine())
114 QQmlEnginePrivate::deleteInEngineThread(engine, this);
119 QQmlCompiledData::~QQmlCompiledData()
123 for (int ii = 0; ii < types.count(); ++ii) {
124 if (types.at(ii).component)
125 types.at(ii).component->release();
126 if (types.at(ii).typePropertyCache)
127 types.at(ii).typePropertyCache->release();
130 for (int ii = 0; ii < propertyCaches.count(); ++ii)
131 propertyCaches.at(ii)->release();
133 for (int ii = 0; ii < contextCaches.count(); ++ii)
134 contextCaches.at(ii)->release();
136 for (int ii = 0; ii < scripts.count(); ++ii)
137 scripts.at(ii)->release();
140 importCache->release();
142 if (rootPropertyCache)
143 rootPropertyCache->release();
146 void QQmlCompiledData::clear()
148 for (int ii = 0; ii < programs.count(); ++ii)
149 qPersistentDispose(programs[ii].bindings);
152 const QMetaObject *QQmlCompiledData::TypeReference::metaObject() const
155 return type->metaObject();
158 return component->root;
163 Returns the property cache, if one alread exists. The cache is not referenced.
165 QQmlPropertyCache *QQmlCompiledData::TypeReference::propertyCache() const
168 return typePropertyCache;
170 return component->rootPropertyCache;
174 Returns the property cache, creating one if it doesn't already exist. The cache is not referenced.
176 QQmlPropertyCache *QQmlCompiledData::TypeReference::createPropertyCache(QQmlEngine *engine)
178 if (typePropertyCache) {
179 return typePropertyCache;
181 typePropertyCache = QQmlEnginePrivate::get(engine)->cache(type->metaObject());
182 typePropertyCache->addref();
183 return typePropertyCache;
185 return component->rootPropertyCache;
190 void QQmlCompiledData::dumpInstructions()
194 qWarning().nospace() << "Index\tOperation\t\tData1\tData2\tData3\tComments";
195 qWarning().nospace() << "-------------------------------------------------------------------------------";
197 const char *instructionStream = bytecode.constData();
198 const char *endInstructionStream = bytecode.constData() + bytecode.size();
200 int instructionCount = 0;
201 while (instructionStream < endInstructionStream) {
202 QQmlInstruction *instr = (QQmlInstruction *)instructionStream;
203 dump(instr, instructionCount);
204 instructionStream += QQmlInstruction::size(instructionType(instr));
208 qWarning().nospace() << "-------------------------------------------------------------------------------";
211 int QQmlCompiledData::addInstructionHelper(QQmlInstruction::Type type, QQmlInstruction &instr)
213 #ifdef QML_THREADED_VME_INTERPRETER
214 instr.common.code = QQmlVME::instructionJumpTable()[static_cast<int>(type)];
216 instr.common.instructionType = type;
218 int ptrOffset = bytecode.size();
219 int size = QQmlInstruction::size(type);
220 if (bytecode.capacity() <= bytecode.size() + size)
221 bytecode.reserve(bytecode.size() + size + 512);
222 bytecode.append(reinterpret_cast<const char *>(&instr), size);
226 int QQmlCompiledData::nextInstructionIndex()
228 return bytecode.size();
231 QQmlInstruction *QQmlCompiledData::instruction(int index)
233 return (QQmlInstruction *)(bytecode.constData() + index);
236 QQmlInstruction::Type QQmlCompiledData::instructionType(const QQmlInstruction *instr)
238 #ifdef QML_THREADED_VME_INTERPRETER
239 void **jumpTable = QQmlVME::instructionJumpTable();
240 void *code = instr->common.code;
242 # define QML_CHECK_INSTR_CODE(I, FMT) \
243 if (jumpTable[static_cast<int>(QQmlInstruction::I)] == code) \
244 return QQmlInstruction::I;
246 FOR_EACH_QML_INSTR(QML_CHECK_INSTR_CODE)
247 Q_ASSERT_X(false, Q_FUNC_INFO, "Invalid instruction address");
248 return static_cast<QQmlInstruction::Type>(0);
249 # undef QML_CHECK_INSTR_CODE
251 return static_cast<QQmlInstruction::Type>(instr->common.instructionType);
255 void QQmlCompiledData::initialize(QQmlEngine *engine)
257 Q_ASSERT(!hasEngine());
258 QQmlCleanup::addToEngine(engine);