1 /****************************************************************************
3 ** Copyright (C) 2010 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 QtScript module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL-ONLY$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser
12 ** General Public License version 2.1 as published by the Free Software
13 ** Foundation and appearing in the file LICENSE.LGPL included in the
14 ** packaging of this file. Please review the following information to
15 ** ensure the GNU Lesser General Public License version 2.1 requirements
16 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** If you have questions regarding the use of this file, please contact
19 ** Nokia at qt-info@nokia.com.
22 ****************************************************************************/
25 #include "qscriptvalueiterator.h"
27 #include "qscriptstring.h"
28 #include "qscriptengine.h"
29 #include "qscriptengine_p.h"
30 #include "qscriptvalue_p.h"
31 #include "qlinkedlist.h"
35 #include "PropertyNameArray.h"
37 #include "JSFunction.h"
43 \class QScriptValueIterator
45 \brief The QScriptValueIterator class provides a Java-style iterator for QScriptValue.
50 The QScriptValueIterator constructor takes a QScriptValue as
51 argument. After construction, the iterator is located at the very
52 beginning of the sequence of properties. Here's how to iterate over
53 all the properties of a QScriptValue:
55 \snippet doc/src/snippets/code/src_script_qscriptvalueiterator.cpp 0
57 The next() advances the iterator. The name(), value() and flags()
58 functions return the name, value and flags of the last item that was
61 If you want to remove properties as you iterate over the
62 QScriptValue, use remove(). If you want to modify the value of a
63 property, use setValue().
65 Note that QScriptValueIterator only iterates over the QScriptValue's
66 own properties; i.e. it does not follow the prototype chain. You can
67 use a loop like this to follow the prototype chain:
69 \snippet doc/src/snippets/code/src_script_qscriptvalueiterator.cpp 1
71 Note that QScriptValueIterator will not automatically skip over
72 properties that have the QScriptValue::SkipInEnumeration flag set;
73 that flag only affects iteration in script code. If you want, you
74 can skip over such properties with code like the following:
76 \snippet doc/src/snippets/code/src_script_qscriptvalueiterator.cpp 2
78 \sa QScriptValue::property()
81 class QScriptValueIteratorPrivate
84 QScriptValueIteratorPrivate()
88 QScriptValuePrivate *object() const
90 return QScriptValuePrivate::get(objectValue);
93 QScriptEnginePrivate *engine() const
95 return QScriptEnginePrivate::get(objectValue.engine());
98 void ensureInitialized()
102 QScriptEnginePrivate *eng_p = engine();
103 QScript::APIShim shim(eng_p);
104 JSC::ExecState *exec = eng_p->globalExec();
105 JSC::PropertyNameArray propertyNamesArray(exec);
106 JSC::asObject(object()->jscValue)->getOwnPropertyNames(exec, propertyNamesArray, JSC::IncludeDontEnumProperties);
108 JSC::PropertyNameArray::const_iterator propertyNamesIt = propertyNamesArray.begin();
109 for(; propertyNamesIt != propertyNamesArray.end(); ++propertyNamesIt) {
110 propertyNames.append(*propertyNamesIt);
112 it = propertyNames.begin();
116 QScriptValue objectValue;
117 QLinkedList<JSC::Identifier> propertyNames;
118 QLinkedList<JSC::Identifier>::iterator it;
119 QLinkedList<JSC::Identifier>::iterator current;
124 Constructs an iterator for traversing \a object. The iterator is
125 set to be at the front of the sequence of properties (before the
128 QScriptValueIterator::QScriptValueIterator(const QScriptValue &object)
131 if (object.isObject()) {
132 d_ptr.reset(new QScriptValueIteratorPrivate());
133 d_ptr->objectValue = object;
138 Destroys the iterator.
140 QScriptValueIterator::~QScriptValueIterator()
145 Returns true if there is at least one item ahead of the iterator
146 (i.e. the iterator is \e not at the back of the property sequence);
147 otherwise returns false.
149 \sa next(), hasPrevious()
151 bool QScriptValueIterator::hasNext() const
153 Q_D(const QScriptValueIterator);
157 const_cast<QScriptValueIteratorPrivate*>(d)->ensureInitialized();
158 return d->it != d->propertyNames.end();
162 Advances the iterator by one position.
164 Calling this function on an iterator located at the back of the
165 container leads to undefined results.
167 \sa hasNext(), previous(), name()
169 void QScriptValueIterator::next()
171 Q_D(QScriptValueIterator);
174 d->ensureInitialized();
181 Returns true if there is at least one item behind the iterator
182 (i.e. the iterator is \e not at the front of the property sequence);
183 otherwise returns false.
185 \sa previous(), hasNext()
187 bool QScriptValueIterator::hasPrevious() const
189 Q_D(const QScriptValueIterator);
193 const_cast<QScriptValueIteratorPrivate*>(d)->ensureInitialized();
194 return d->it != d->propertyNames.begin();
198 Moves the iterator back by one position.
200 Calling this function on an iterator located at the front of the
201 container leads to undefined results.
203 \sa hasPrevious(), next(), name()
205 void QScriptValueIterator::previous()
207 Q_D(QScriptValueIterator);
210 d->ensureInitialized();
216 Moves the iterator to the front of the QScriptValue (before the
221 void QScriptValueIterator::toFront()
223 Q_D(QScriptValueIterator);
226 d->ensureInitialized();
227 d->it = d->propertyNames.begin();
231 Moves the iterator to the back of the QScriptValue (after the
234 \sa toFront(), previous()
236 void QScriptValueIterator::toBack()
238 Q_D(QScriptValueIterator);
241 d->ensureInitialized();
242 d->it = d->propertyNames.end();
246 Returns the name of the last property that was jumped over using
247 next() or previous().
251 QString QScriptValueIterator::name() const
253 Q_D(const QScriptValueIterator);
254 if (!d || !d->initialized || !d->engine())
256 return d->current->ustring();
262 Returns the name of the last property that was jumped over using
263 next() or previous().
265 QScriptString QScriptValueIterator::scriptName() const
267 Q_D(const QScriptValueIterator);
268 if (!d || !d->initialized || !d->engine())
269 return QScriptString();
270 return d->engine()->toStringHandle(*d->current);
274 Returns the value of the last property that was jumped over using
275 next() or previous().
277 \sa setValue(), name()
279 QScriptValue QScriptValueIterator::value() const
281 Q_D(const QScriptValueIterator);
282 if (!d || !d->initialized || !d->engine())
283 return QScriptValue();
284 JSC::JSValue jsValue = d->object()->property(*d->current);
285 return d->engine()->scriptValueFromJSCValue(jsValue);
289 Sets the \a value of the last property that was jumped over using
290 next() or previous().
294 void QScriptValueIterator::setValue(const QScriptValue &value)
296 Q_D(QScriptValueIterator);
297 if (!d || !d->initialized || !d->engine())
299 JSC::JSValue jsValue = d->engine()->scriptValueToJSCValue(value);
300 d->object()->setProperty(*d->current, jsValue);
304 Returns the flags of the last property that was jumped over using
305 next() or previous().
309 QScriptValue::PropertyFlags QScriptValueIterator::flags() const
311 Q_D(const QScriptValueIterator);
312 if (!d || !d->initialized || !d->engine())
314 return d->object()->propertyFlags(*d->current);
318 Removes the last property that was jumped over using next()
323 void QScriptValueIterator::remove()
325 Q_D(QScriptValueIterator);
326 if (!d || !d->initialized || !d->engine())
328 d->object()->setProperty(*d->current, JSC::JSValue());
329 d->propertyNames.erase(d->current);
333 Makes the iterator operate on \a object. The iterator is set to be
334 at the front of the sequence of properties (before the first
337 QScriptValueIterator& QScriptValueIterator::operator=(QScriptValue &object)
340 if (object.isObject()) {
341 d_ptr.reset(new QScriptValueIteratorPrivate());
342 d_ptr->objectValue = object;