source: trunk/src/script/qscriptextenumeration.cpp@ 203

Last change on this file since 203 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 6.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtScript module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qscriptextenumeration_p.h"
43
44#ifndef QT_NO_SCRIPT
45
46#include "qscriptengine_p.h"
47#include "qscriptvalueimpl_p.h"
48#include "qscriptcontext_p.h"
49#include "qscriptmember_p.h"
50#include "qscriptobject_p.h"
51#include "qscriptvalueiteratorimpl_p.h"
52
53#include <QtCore/QtDebug>
54
55QT_BEGIN_NAMESPACE
56
57namespace QScript { namespace Ext {
58
59EnumerationClassData::EnumerationClassData(QScriptClassInfo *classInfo):
60 m_classInfo(classInfo)
61{
62}
63
64EnumerationClassData::~EnumerationClassData()
65{
66}
67
68void EnumerationClassData::mark(const QScriptValueImpl &object, int generation)
69{
70 Q_ASSERT(object.isValid());
71
72 QScriptEnginePrivate *eng = object.engine();
73
74 if (Enumeration::Instance *instance = Enumeration::Instance::get(object, classInfo())) {
75 eng->markObject(instance->object, generation);
76 if (instance->it)
77 eng->markObject(instance->it->object(), generation);
78 }
79}
80
81Enumeration::Enumeration(QScriptEnginePrivate *eng):
82 Ecma::Core(eng, QLatin1String("Enumeration"), QScriptClassInfo::EnumerationType)
83{
84 classInfo()->setData(new EnumerationClassData(classInfo()));
85
86 newEnumeration(&publicPrototype, eng->newArray());
87
88 eng->newConstructor(&ctor, this, publicPrototype);
89
90 addPrototypeFunction(QLatin1String("toFront"), method_toFront, 0);
91 addPrototypeFunction(QLatin1String("hasNext"), method_hasNext, 0);
92 addPrototypeFunction(QLatin1String("next"), method_next, 0);
93}
94
95Enumeration::~Enumeration()
96{
97}
98
99Enumeration::Instance *Enumeration::Instance::get(const QScriptValueImpl &object, QScriptClassInfo *klass)
100{
101 if (! klass || klass == object.classInfo())
102 return static_cast<Instance*> (object.objectData());
103
104 return 0;
105}
106
107void Enumeration::execute(QScriptContextPrivate *context)
108{
109 if (context->argumentCount() > 0) {
110 newEnumeration(&context->m_result, context->argument(0));
111 } else {
112 context->throwError(QScriptContext::TypeError,
113 QLatin1String("Enumeration.execute"));
114 }
115}
116
117void Enumeration::newEnumeration(QScriptValueImpl *result, const QScriptValueImpl &object)
118{
119 Instance *instance = new Instance();
120 instance->object = object;
121 if (object.isObject()) {
122 instance->it = new QScriptValueIteratorImpl(object);
123 instance->it->setIgnoresDontEnum(false);
124 instance->it->setEnumeratePrototype(true);
125 } else {
126 instance->it = 0;
127 }
128 engine()->newObject(result, publicPrototype, classInfo());
129 result->setObjectData(instance);
130}
131
132QScriptValueImpl Enumeration::method_toFront(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *classInfo)
133{
134 if (Instance *instance = Instance::get(context->thisObject(), classInfo)) {
135 if (instance->it)
136 instance->it->toFront();
137 return eng->undefinedValue();
138 } else {
139 return context->throwError(QScriptContext::TypeError,
140 QLatin1String("Enumeration.toFront"));
141 }
142}
143
144QScriptValueImpl Enumeration::method_hasNext(QScriptContextPrivate *context, QScriptEnginePrivate *, QScriptClassInfo *classInfo)
145{
146 Instance *instance = Instance::get(context->thisObject(), classInfo);
147 if (!instance) {
148 return context->throwError(QScriptContext::TypeError,
149 QLatin1String("Enumeration.hasNext"));
150 }
151
152 QScriptValueImpl v;
153 instance->hasNext(context, &v);
154 return v;
155}
156
157QScriptValueImpl Enumeration::method_next(QScriptContextPrivate *context, QScriptEnginePrivate *, QScriptClassInfo *classInfo)
158{
159 Instance *instance = Instance::get(context->thisObject(), classInfo);
160 if (!instance) {
161 return context->throwError(QScriptContext::TypeError,
162 QLatin1String("Enumeration.next"));
163 }
164
165 QScriptValueImpl v;
166 instance->next(context, &v);
167 return v;
168}
169
170Enumeration::Instance::~Instance()
171{
172 if (it) {
173 delete it;
174 it = 0;
175 }
176}
177
178void Enumeration::Instance::toFront()
179{
180 if (it)
181 it->toFront();
182}
183
184void Enumeration::Instance::hasNext(QScriptContextPrivate *, QScriptValueImpl *result)
185{
186 *result = QScriptValueImpl(it && it->hasNext());
187}
188
189void Enumeration::Instance::next(QScriptContextPrivate *context, QScriptValueImpl *result)
190{
191 QScriptEnginePrivate *eng = context->engine();
192 Q_ASSERT(it != 0);
193 it->next();
194 QScript::Member *member = it->member();
195 if (member->isObjectProperty() || member->nameId())
196 eng->newNameId(result, member->nameId());
197
198 else if (member->isNativeProperty() && !member->nameId())
199 *result = QScriptValueImpl(uint(member->id()));
200
201 else
202 *result = eng->undefinedValue();
203}
204
205} } // namespace QScript::Ext
206
207QT_END_NAMESPACE
208
209#endif // QT_NO_SCRIPT
Note: See TracBrowser for help on using the repository browser.