| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the QtScript module of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 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.
|
|---|
| 17 | **
|
|---|
| 18 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 19 | ** Nokia at [email protected].
|
|---|
| 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 |
|
|---|
| 39 | QT_BEGIN_NAMESPACE
|
|---|
| 40 |
|
|---|
| 41 | /*!
|
|---|
| 42 | \since 4.3
|
|---|
| 43 | \class QScriptValueIterator
|
|---|
| 44 |
|
|---|
| 45 | \brief The QScriptValueIterator class provides a Java-style iterator for QScriptValue.
|
|---|
| 46 |
|
|---|
| 47 | \ingroup script
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 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:
|
|---|
| 54 |
|
|---|
| 55 | \snippet doc/src/snippets/code/src_script_qscriptvalueiterator.cpp 0
|
|---|
| 56 |
|
|---|
| 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
|
|---|
| 59 | jumped over.
|
|---|
| 60 |
|
|---|
| 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().
|
|---|
| 64 |
|
|---|
| 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:
|
|---|
| 68 |
|
|---|
| 69 | \snippet doc/src/snippets/code/src_script_qscriptvalueiterator.cpp 1
|
|---|
| 70 |
|
|---|
| 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:
|
|---|
| 75 |
|
|---|
| 76 | \snippet doc/src/snippets/code/src_script_qscriptvalueiterator.cpp 2
|
|---|
| 77 |
|
|---|
| 78 | \sa QScriptValue::property()
|
|---|
| 79 | */
|
|---|
| 80 |
|
|---|
| 81 | class QScriptValueIteratorPrivate
|
|---|
| 82 | {
|
|---|
| 83 | public:
|
|---|
| 84 | QScriptValueIteratorPrivate()
|
|---|
| 85 | : initialized(false)
|
|---|
| 86 | {}
|
|---|
| 87 | void ensureInitialized()
|
|---|
| 88 | {
|
|---|
| 89 | if (initialized)
|
|---|
| 90 | return;
|
|---|
| 91 | QScriptEnginePrivate *eng_p = QScriptEnginePrivate::get(object.engine());
|
|---|
| 92 | JSC::ExecState *exec = eng_p->globalExec();
|
|---|
| 93 | JSC::PropertyNameArray propertyNamesArray(exec);
|
|---|
| 94 | propertyNamesArray.setShouldCache(false);
|
|---|
| 95 | JSC::asObject(QScriptValuePrivate::get(object)->jscValue)->getOwnPropertyNames(exec, propertyNamesArray, /*includeNonEnumerable=*/true);
|
|---|
| 96 |
|
|---|
| 97 | JSC::PropertyNameArray::const_iterator propertyNamesIt = propertyNamesArray.begin();
|
|---|
| 98 | for(; propertyNamesIt != propertyNamesArray.end(); ++propertyNamesIt) {
|
|---|
| 99 | propertyNames.append(propertyNamesIt->ustring());
|
|---|
| 100 | }
|
|---|
| 101 | it = propertyNames.begin();
|
|---|
| 102 | initialized = true;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | QScriptValue object;
|
|---|
|
|---|