Qt 4.8
qscriptvalueiterator.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtScript module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL-ONLY$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser
11 ** General Public License version 2.1 as published by the Free Software
12 ** Foundation and appearing in the file LICENSE.LGPL included in the
13 ** packaging of this file. Please review the following information to
14 ** ensure the GNU Lesser General Public License version 2.1 requirements
15 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** If you have questions regarding the use of this file, please contact
18 ** us via http://www.qt-project.org/.
19 **
20 ** $QT_END_LICENSE$
21 **
22 ****************************************************************************/
23 
24 #include "config.h"
25 #include "qscriptvalueiterator.h"
26 
27 #include "qscriptstring.h"
28 #include "qscriptengine.h"
29 #include "qscriptengine_p.h"
30 #include "qscriptvalue_p.h"
31 #include "qlinkedlist.h"
32 
33 
34 #include "JSObject.h"
35 #include "PropertyNameArray.h"
36 #include "JSArray.h"
37 #include "JSFunction.h"
38 
40 
85 {
86 public:
88  : initialized(false)
89  {}
90 
92  {
93  if (!initialized)
94  return;
95  QScriptEnginePrivate *eng_p = engine();
96  if (!eng_p)
97  return;
98  QScript::APIShim shim(eng_p);
99  propertyNames.clear(); //destroying the identifiers need to be done under the APIShim guard
100  }
101 
103  {
105  }
106 
108  {
110  }
111 
113  {
114  if (initialized)
115  return;
116  QScriptEnginePrivate *eng_p = engine();
117  QScript::APIShim shim(eng_p);
118  JSC::ExecState *exec = eng_p->globalExec();
119  JSC::PropertyNameArray propertyNamesArray(exec);
120  JSC::asObject(object()->jscValue)->getOwnPropertyNames(exec, propertyNamesArray, JSC::IncludeDontEnumProperties);
121 
122  JSC::PropertyNameArray::const_iterator propertyNamesIt = propertyNamesArray.begin();
123  for(; propertyNamesIt != propertyNamesArray.end(); ++propertyNamesIt) {
124  propertyNames.append(*propertyNamesIt);
125  }
126  it = propertyNames.begin();
127  initialized = true;
128  }
129 
135 };
136 
143  : d_ptr(0)
144 {
145  if (object.isObject()) {
147  d_ptr->objectValue = object;
148  }
149 }
150 
155 {
156 }
157 
166 {
167  Q_D(const QScriptValueIterator);
168  if (!d || !d->engine())
169  return false;
170 
172  return d->it != d->propertyNames.end();
173 }
174 
184 {
186  if (!d)
187  return;
188  d->ensureInitialized();
189 
190  d->current = d->it;
191  ++(d->it);
192 }
193 
202 {
203  Q_D(const QScriptValueIterator);
204  if (!d || !d->engine())
205  return false;
206 
208  return d->it != d->propertyNames.begin();
209 }
210 
220 {
222  if (!d)
223  return;
224  d->ensureInitialized();
225  --(d->it);
226  d->current = d->it;
227 }
228 
236 {
238  if (!d)
239  return;
240  d->ensureInitialized();
241  d->it = d->propertyNames.begin();
242 }
243 
251 {
253  if (!d)
254  return;
255  d->ensureInitialized();
256  d->it = d->propertyNames.end();
257 }
258 
266 {
267  Q_D(const QScriptValueIterator);
268  if (!d || !d->initialized || !d->engine())
269  return QString();
270  return d->current->ustring();
271 }
272 
283 {
284  Q_D(const QScriptValueIterator);
285  if (!d || !d->initialized || !d->engine())
286  return QScriptString();
287  return d->engine()->toStringHandle(*d->current);
288 }
289 
297 {
298  Q_D(const QScriptValueIterator);
299  if (!d || !d->initialized || !d->engine())
300  return QScriptValue();
301  QScript::APIShim shim(d->engine());
302  JSC::JSValue jsValue = d->object()->property(*d->current);
303  return d->engine()->scriptValueFromJSCValue(jsValue);
304 }
305 
313 {
315  if (!d || !d->initialized || !d->engine())
316  return;
317  QScript::APIShim shim(d->engine());
318  JSC::JSValue jsValue = d->engine()->scriptValueToJSCValue(value);
319  d->object()->setProperty(*d->current, jsValue);
320 }
321 
329 {
330  Q_D(const QScriptValueIterator);
331  if (!d || !d->initialized || !d->engine())
332  return 0;
333  QScript::APIShim shim(d->engine());
334  return d->object()->propertyFlags(*d->current);
335 }
336 
344 {
346  if (!d || !d->initialized || !d->engine())
347  return;
348  QScript::APIShim shim(d->engine());
349  d->object()->setProperty(*d->current, JSC::JSValue());
350  d->propertyNames.erase(d->current);
351 }
352 
359 {
360  d_ptr.reset();
361  if (object.isObject()) {
363  d_ptr->objectValue = object;
364  }
365  return *this;
366 }
367 
void clear()
Removes all the items in the list.
Definition: qlinkedlist.h:311
double d
Definition: qnumeric_p.h:62
JSC::ExecState * globalExec() const
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static QScriptEnginePrivate * get(QScriptEngine *q)
bool hasPrevious() const
Returns true if there is at least one item behind the iterator (i.e.
void previous()
Moves the iterator back by one position.
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlinkedlist.h:182
static QScriptValuePrivate * get(const QScriptValue &q)
QString name() const
Returns the name of the last property that was jumped over using next() or previous().
void toBack()
Moves the iterator to the back of the QScriptValue (after the last property).
void setValue(const QScriptValue &value)
Sets the value of the last property that was jumped over using next() or previous().
void append(const T &)
Inserts value at the end of the list.
Definition: qlinkedlist.h:350
QLinkedList< JSC::Identifier >::iterator current
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_D(Class)
Definition: qglobal.h:2482
The QScriptString class acts as a handle to "interned" strings in a QScriptEngine.
Definition: qscriptstring.h:38
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QScriptEngine * engine() const
Returns the QScriptEngine that created this QScriptValue, or 0 if this QScriptValue is invalid or the...
QScriptValueIterator & operator=(QScriptValue &value)
Makes the iterator operate on object.
static void ensureInitialized()
QLinkedList< JSC::Identifier >::iterator it
QScriptEnginePrivate * engine() const
QScriptValueIterator(const QScriptValue &value)
Constructs an iterator for traversing object.
QScopedPointer< QScriptValueIteratorPrivate > d_ptr
void toFront()
Moves the iterator to the front of the QScriptValue (before the first property).
void reset(T *other=0)
Deletes the existing object it is pointing to if any, and sets its pointer to other.
QScriptValue::PropertyFlags flags() const
Returns the flags of the last property that was jumped over using next() or previous().
The QScriptValueIterator class provides a Java-style iterator for QScriptValue.
PropertyFlags
Definition: qmetaobject_p.h:61
QScriptValue value() const
Returns the value of the last property that was jumped over using next() or previous().
QLinkedList< JSC::Identifier > propertyNames
~QScriptValueIterator()
Destroys the iterator.
QScriptString scriptName() const
Returns the name of the last property that was jumped over using next() or previous().
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
void next()
Advances the iterator by one position.
bool hasNext() const
Returns true if there is at least one item ahead of the iterator (i.e.
void remove()
Removes the last property that was jumped over using next() or previous().
QScriptValuePrivate * object() const