aboutsummaryrefslogtreecommitdiffstats
path: root/src/declarative/qml/v8/qjsvalueiterator.cpp
blob: ca9123f0c0f83781e3b5f3a03ec6b86b29999cde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtScript module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL-ONLY$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qjsvalueiterator.h"

#include "qscriptisolate_p.h"
#include "qjsvalue_p.h"
#include "qv8engine_p.h"
#include "qscript_impl_p.h"

QT_BEGIN_NAMESPACE

/*!
    \class QJSValueIterator

    \brief The QJSValueIterator class provides a Java-style iterator for QJSValue.

    \ingroup script


    The QJSValueIterator constructor takes a QJSValue as
    argument.  After construction, the iterator is located at the very
    beginning of the sequence of properties. Here's how to iterate over
    all the properties of a QJSValue:

    \snippet doc/src/snippets/code/src_script_QJSValueIterator.cpp 0

    The next() advances the iterator. The name(), value() and flags()
    functions return the name, value and flags of the last item that was
    jumped over.

    If you want to remove properties as you iterate over the
    QJSValue, use remove(). If you want to modify the value of a
    property, use setValue().

    Note that QJSValueIterator only iterates over the QJSValue's
    own properties; i.e. it does not follow the prototype chain. You can
    use a loop like this to follow the prototype chain:

    \snippet doc/src/snippets/code/src_script_QJSValueIterator.cpp 1

    Note that QJSValueIterator will not automatically skip over
    properties that have the QJSValue::SkipInEnumeration flag set;
    that flag only affects iteration in script code.  If you want, you
    can skip over such properties with code like the following:

    \snippet doc/src/snippets/code/src_script_QJSValueIterator.cpp 2

    \sa QJSValue::property()
*/

using v8::Persistent;
using v8::Local;
using v8::Array;
using v8::String;
using v8::Handle;
using v8::Object;
using v8::Value;

// FIXME (Qt5) This class should be refactored. It should use the common Iterator interface.
// FIXME it could be faster!
class QJSValueIteratorPrivate {
public:
    inline QJSValueIteratorPrivate(const QJSValuePrivate* value);
    inline ~QJSValueIteratorPrivate();

    inline bool hasNext() const;
    inline bool next();

    inline QString name() const;

    inline QScriptPassPointer<QJSValuePrivate> value() const;

    inline bool isValid() const;
    inline QV8Engine* engine() const;
private:
    Q_DISABLE_COPY(QJSValueIteratorPrivate)
    //void dump(QString) const;

    QScriptSharedDataPointer<QJSValuePrivate> m_object;
    QList<v8::Persistent<v8::String> > m_names;
    QMutableListIterator<v8::Persistent<v8::String> > m_iterator;
};

inline QJSValueIteratorPrivate::QJSValueIteratorPrivate(const QJSValuePrivate* value)
    : m_object(const_cast<QJSValuePrivate*>(value))
    , m_iterator(m_names)
{
    Q_ASSERT(value);
    QV8Engine *engine = m_object->engine();
    QScriptIsolate api(engine);
    if (!m_object->isObject())
        m_object = 0;
    else {
        v8::HandleScope scope;
        Handle<Value> tmp = *value;
        Handle<Object> obj = Handle<Object>::Cast(tmp);
        Local<Array> names;

        // FIXME we need newer V8!
        //names = obj->GetOwnPropertyNames();
        names = engine->getOwnPropertyNames(obj);

        // it is suboptimal, it would be better to write iterator instead
        uint32_t count = names->Length();
        Local<String> name;
        m_names.reserve(count); // The count is the maximal count of values.
        for (uint32_t i = count - 1; i < count; --i) {
            name = names->Get(i)->ToString();
            m_names.append(v8::Persistent<v8::String>::New(name));
        }

        // Reinitialize the iterator.
        m_iterator = m_names;
    }
}

inline QJSValueIteratorPrivate::~QJSValueIteratorPrivate()
{
    QMutableListIterator<v8::Persistent<v8::String> > it = m_names;
    //FIXME: we need register this QJSVAlueIterator
    if (engine()) {
        while (it.hasNext()) {
            it.next().Dispose();
        }
    } else {
        // FIXME leak ?
    }
}

inline bool QJSValueIteratorPrivate::hasNext() const
{
    //dump("hasNext()");
    return isValid()
            ? m_iterator.hasNext() : false;
}

inline bool QJSValueIteratorPrivate::next()
{
    //dump("next();");
    if (m_iterator.hasNext()) {
        m_iterator.next();
        return true;
    }
    return false;
}

inline QString QJSValueIteratorPrivate::name() const
{
    //dump("name");
    if (!isValid())
        return QString();

    return QJSConverter::toString(m_iterator.value());
}

inline QScriptPassPointer<QJSValuePrivate> QJSValueIteratorPrivate::value() const
{
    //dump("value()");
    if (!isValid())
        return InvalidValue();

    return m_object->property(m_iterator.value());
}

inline bool QJSValueIteratorPrivate::isValid() const
{
    bool result = m_object ? m_object->isValid() : false;
    // We know that if this object is still valid then it is an object
    // if this assumption is not correct then some other logic in this class
    // have to be changed too.
    Q_ASSERT(!result || m_object->isObject());
    return result;
}

inline QV8Engine* QJSValueIteratorPrivate::engine() const
{
    return m_object ? m_object->engine() : 0;
}

//void QJSValueIteratorPrivate::dump(QString fname) const
//{
//    qDebug() << "    *** " << fname << " ***";
//    foreach (Persistent<String> name, m_names) {
//        qDebug() << "        - " << QJSConverter::toString(name);
//    }
//}

/*!
    Constructs an iterator for traversing \a object. The iterator is
    set to be at the front of the sequence of properties (before the
    first property).
*/
QJSValueIterator::QJSValueIterator(const QJSValue& object)
    : d_ptr(new QJSValueIteratorPrivate(QJSValuePrivate::get(object)))
{}

/*!
    Destroys the iterator.
*/
QJSValueIterator::~QJSValueIterator()
{}

/*!
    Returns true if there is at least one item ahead of the iterator
    (i.e. the iterator is \e not at the back of the property sequence);
    otherwise returns false.

    \sa next(), hasPrevious()
*/
bool QJSValueIterator::hasNext() const
{
    Q_D(const QJSValueIterator);
    QScriptIsolate api(d->engine());
    return d->hasNext();
}

/*!
    Advances the iterator by one position.

    Calling this function on an iterator located at the back of the
    container leads to undefined results.

    \sa hasNext(), previous(), name()
*/
bool QJSValueIterator::next()
{
    Q_D(QJSValueIterator);
    QScriptIsolate api(d->engine());
    return d->next();
}

/*!
    Returns the name of the last property that was jumped over using
    next() or previous().

    \sa value(), flags()
*/
QString QJSValueIterator::name() const
{
    Q_D(const QJSValueIterator);
    QScriptIsolate api(d->engine());
    return d_ptr->name();
}


/*!
    Returns the value of the last property that was jumped over using
    next() or previous().

    \sa setValue(), name()
*/
QJSValue QJSValueIterator::value() const
{
    Q_D(const QJSValueIterator);
    QScriptIsolate api(d->engine());
    return QJSValuePrivate::get(d->value());
}


/*!
    Makes the iterator operate on \a object. The iterator is set to be
    at the front of the sequence of properties (before the first
    property).
*/
QJSValueIterator& QJSValueIterator::operator=(QJSValue& object)
{
    Q_D(QJSValueIterator);
    QScriptIsolate api(d->engine());
    d_ptr.reset(new QJSValueIteratorPrivate(QJSValuePrivate::get(object)));
    return *this;
}

QT_END_NAMESPACE