summaryrefslogtreecommitdiff
path: root/src/script/api/qscriptvalueiterator.cpp
blob: aa8bac0f92d3aa691b8928bf6044dda4f48c71ad (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/****************************************************************************
**
** 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 "qscriptvalueiterator.h"

#include "qscriptisolate_p.h"
#include "qscriptstring_p.h"
#include "qscriptvalue_p.h"
#include "qscriptclass_p.h"
#include "qscriptclasspropertyiterator.h"
#include "qscriptengine_p.h"
#include "qscript_impl_p.h"

QT_BEGIN_NAMESPACE

/*!
    \class QScriptValueIterator

    \brief The QScriptValueIterator class provides a Java-style iterator for QScriptValue.

    \ingroup script


    The QScriptValueIterator constructor takes a QScriptValue 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 QScriptValue:

    \snippet doc/src/snippets/code/src_script_qscriptvalueiterator.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
    QScriptValue, use remove(). If you want to modify the value of a
    property, use setValue().

    Note that QScriptValueIterator only iterates over the QScriptValue'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_qscriptvalueiterator.cpp 1

    Note that QScriptValueIterator will not automatically skip over
    properties that have the QScriptValue::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_qscriptvalueiterator.cpp 2

    \sa QScriptValue::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 QScriptValueIteratorPrivate {
public:
    inline QScriptValueIteratorPrivate(const QScriptValuePrivate* value);
    inline ~QScriptValueIteratorPrivate();

    inline bool hasNext();
    inline void next();

    inline bool hasPrevious();
    inline void previous();

    inline QString name() const;
    inline QScriptPassPointer<QScriptStringPrivate> scriptName() const;

    inline QScriptPassPointer<QScriptValuePrivate> value() const;
    inline void setValue(const QScriptValuePrivate* value);

    inline void remove();

    inline void toFront();
    inline void toBack();

    QScriptValue::PropertyFlags flags() const;

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

    QScriptSharedDataPointer<QScriptValuePrivate> m_object;
    QList<QScriptSharedDataPointer<QScriptStringPrivate> > m_names;
    QMutableListIterator<QScriptSharedDataPointer<QScriptStringPrivate> > m_iterator;
    QScriptClassPropertyIterator *m_classIterator;
    bool m_usingClassIterator;
};

inline QScriptValueIteratorPrivate::QScriptValueIteratorPrivate(const QScriptValuePrivate* value)
    : m_object(const_cast<QScriptValuePrivate*>(value))
    , m_iterator(m_names)
    , m_classIterator(0)
    , m_usingClassIterator(false)
{
    Q_ASSERT(value);
    QScriptEnginePrivate *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;

        // check if the value is a script class instance
        QScriptClassObject *data = QScriptClassObject::safeGet(value);
        if (data
            && data->scriptClass()
            && (m_classIterator = data->scriptClass()->userCallback()->newIterator(QScriptValuePrivate::get(value)))) {
            // we need to wrap custom iterator.
            names = engine->getOwnPropertyNames(data->original());
        } else
            names = engine->getOwnPropertyNames(obj);

        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(QScriptSharedDataPointer<QScriptStringPrivate>(new QScriptStringPrivate(engine, name)));
        }

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

inline QScriptValueIteratorPrivate::~QScriptValueIteratorPrivate()
{
    delete m_classIterator;
}

inline bool QScriptValueIteratorPrivate::hasNext()
{
    //dump("hasNext()");
    return isValid()
            ? m_iterator.hasNext() || (m_classIterator && m_classIterator->hasNext())
            : false;
}

inline void QScriptValueIteratorPrivate::next()
{
    // FIXME (Qt5) This method should return a value (QTBUG-11226).
    //dump("next();");
    if (m_iterator.hasNext())
        m_iterator.next();
    else if (m_classIterator) {
        m_usingClassIterator = true;
        m_classIterator->next();
    }
}

inline bool QScriptValueIteratorPrivate::hasPrevious()
{
    //dump("hasPrevious()");
    return isValid()
            ? (m_classIterator && m_classIterator->hasPrevious()) || m_iterator.hasPrevious()
            : false;
}

inline void QScriptValueIteratorPrivate::previous()
{
    // FIXME (Qt5) This method should return a value (QTBUG-11226).
    //dump("previous();");
    if (m_classIterator && m_classIterator->hasPrevious())
        m_classIterator->previous();
    else {
        m_usingClassIterator = false;
        m_iterator.previous();
    }
}

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

    if (m_usingClassIterator)
        return m_classIterator->name().toString();
    return m_iterator.value()->toString();
}

inline QScriptPassPointer<QScriptStringPrivate> QScriptValueIteratorPrivate::scriptName() const
{
    //dump("scriptName");
    if (!isValid())
        return new QScriptStringPrivate();

    if (!m_usingClassIterator)
        return m_iterator.value().data();
    return QScriptStringPrivate::get(m_classIterator->name());
}

inline QScriptPassPointer<QScriptValuePrivate> QScriptValueIteratorPrivate::value() const
{
    //dump("value()");
    if (!isValid())
        return InvalidValue();
    // FIXME it could be faster!
    if (m_usingClassIterator)
        return m_object->property(m_classIterator->name().toString(), QScriptValue::ResolveLocal);
    return m_object->property(m_iterator.value().data(), QScriptValue::ResolveLocal);
}

inline void QScriptValueIteratorPrivate::setValue(const QScriptValuePrivate* value)
{
    if (!isValid())
        return;
    if (m_usingClassIterator)
        m_object->setProperty(m_classIterator->name(), const_cast<QScriptValuePrivate*>(value));
    m_object->setProperty(m_iterator.value().data(), const_cast<QScriptValuePrivate*>(value));
}

inline void QScriptValueIteratorPrivate::remove()
{
    //dump("remove();");
    if (!isValid())
        return;
    if (m_usingClassIterator) {
        m_object->deleteProperty(m_classIterator->name());
        return;
    }
    if (m_object->deleteProperty(m_iterator.value()->toString()))
        m_iterator.remove();
}

inline void QScriptValueIteratorPrivate::toFront()
{
    //dump("toFront();");
    m_iterator.toFront();
    if (m_classIterator)
        m_classIterator->toFront();
}

inline void QScriptValueIteratorPrivate::toBack()
{
    //dump("toBack();");
    m_iterator.toBack();
    if (m_classIterator)
        m_classIterator->toBack();
}

QScriptValue::PropertyFlags QScriptValueIteratorPrivate::flags() const
{
    if (!isValid())
        return QScriptValue::PropertyFlags(0);

    v8::HandleScope scope;
    if (m_usingClassIterator)
        return m_object->propertyFlags(QScriptConverter::toString(m_classIterator->name()), QScriptValue::ResolveLocal);
    return m_object->propertyFlags(m_iterator.value().data(), QScriptValue::ResolveLocal);
}

inline bool QScriptValueIteratorPrivate::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 QScriptEnginePrivate* QScriptValueIteratorPrivate::engine() const
{
    return m_object ? m_object->engine() : 0;
}

//void QScriptValueIteratorPrivate::dump(QString fname) const
//{
//    qDebug() << "    *** " << fname << " ***";
//    foreach(Persistent<String> name, m_names) {
//        qDebug() << "        - " << QScriptConverter::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).
*/
QScriptValueIterator::QScriptValueIterator(const QScriptValue& object)
    : d_ptr(new QScriptValueIteratorPrivate(QScriptValuePrivate::get(object)))
{}

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

/*!
    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 QScriptValueIterator::hasNext() const
{
    return d_ptr->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()
*/
void QScriptValueIterator::next()
{
    d_ptr->next();
}

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

    \sa previous(), hasNext()
*/
bool QScriptValueIterator::hasPrevious() const
{
    return d_ptr->hasPrevious();
}

/*!
    Moves the iterator back by one position.

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

    \sa hasPrevious(), next(), name()
*/
void QScriptValueIterator::previous()
{
    d_ptr->previous();
}

/*!
    Moves the iterator to the front of the QScriptValue (before the
    first property).

    \sa toBack(), next()
*/
void QScriptValueIterator::toFront()
{
    d_ptr->toFront();
}

/*!
    Moves the iterator to the back of the QScriptValue (after the
    last property).

    \sa toFront(), previous()
*/
void QScriptValueIterator::toBack()
{
    d_ptr->toBack();
}

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

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

/*!
    Returns the name of the last property that was jumped over using
    next() or previous().
*/
QScriptString QScriptValueIterator::scriptName() const
{
    QScriptIsolate api(d_ptr->engine());
    return QScriptStringPrivate::get(d_ptr->scriptName());
}

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

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

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

    \sa value(), name()
*/
void QScriptValueIterator::setValue(const QScriptValue& value)
{
    Q_D(QScriptValueIterator);
    QScriptIsolate api(d->engine());
    d->setValue(QScriptValuePrivate::get(value));
}

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

    \sa setValue()
*/
void QScriptValueIterator::remove()
{
    Q_D(QScriptValueIterator);
    QScriptIsolate api(d->engine());
    d->remove();
}

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

    \sa value()
*/
QScriptValue::PropertyFlags QScriptValueIterator::flags() const
{
    Q_D(const QScriptValueIterator);
    QScriptIsolate api(d->engine());
    return d->flags();
}

/*!
    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).
*/
QScriptValueIterator& QScriptValueIterator::operator=(QScriptValue& object)
{
    d_ptr.reset(new QScriptValueIteratorPrivate(QScriptValuePrivate::get(object)));
    return *this;
}

QT_END_NAMESPACE