summaryrefslogtreecommitdiffstats
path: root/Source/JavaScriptCore/qt/api/qscriptvalueiterator_p.h
blob: b93b518a31b73104a7a932ade46c24eb09be57a3 (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
/*
    Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#ifndef qscriptvalueiterator_p_h
#define qscriptvalueiterator_p_h

#include "qscriptvalue_p.h"
#include <JavaScriptCore/JavaScript.h>
#include <JavaScriptCore/JSRetainPtr.h>
#include <QtCore/qshareddata.h>
#include <QtCore/qvector.h>

class QScriptValueIteratorPrivate: public QSharedData {
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 QScriptStringPrivate* scriptName() const;

    inline 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;
private:
    inline QScriptEnginePrivate* engine() const;

    QExplicitlySharedDataPointer<QScriptValuePrivate> m_object;
    QVector<JSStringRef> m_names;
    QMutableVectorIterator<JSStringRef> m_idx;
};

inline QScriptValueIteratorPrivate::QScriptValueIteratorPrivate(const QScriptValuePrivate* value)
    : m_object(const_cast<QScriptValuePrivate*>(value))
    , m_idx(m_names)
{
    if (m_object->isObject()) {
        m_names = engine()->objectGetOwnPropertyNames(*m_object);
        m_idx = m_names;
    } else
        m_object = 0;
}

inline QScriptValueIteratorPrivate::~QScriptValueIteratorPrivate()
{
    QVector<JSStringRef>::const_iterator i = m_names.constBegin();
    for (; i != m_names.constEnd(); ++i)
        JSStringRelease(*i);
}

inline bool QScriptValueIteratorPrivate::hasNext()
{
    return m_idx.hasNext();
}

inline void QScriptValueIteratorPrivate::next()
{
    // FIXME (Qt5) This method should return a value (QTBUG-11226).
    m_idx.next();
}

inline bool QScriptValueIteratorPrivate::hasPrevious()
{
    return m_idx.hasPrevious();
}

inline void QScriptValueIteratorPrivate::previous()
{
    m_idx.previous();
}

inline QString QScriptValueIteratorPrivate::name() const
{
    if (!isValid())
        return QString();
    return QScriptConverter::toString(m_idx.value());
}

inline QScriptStringPrivate* QScriptValueIteratorPrivate::scriptName() const
{
    if (!isValid())
        return new QScriptStringPrivate();
    return new QScriptStringPrivate(QScriptConverter::toString(m_idx.value()));
}

inline QScriptValuePrivate* QScriptValueIteratorPrivate::value() const
{
    if (!isValid())
        return new QScriptValuePrivate();
    JSValueRef exception = 0;
    JSValueRef value = m_object->property(m_idx.value(), &exception);
    engine()->setException(exception);
    return new QScriptValuePrivate(engine(), value);
}

inline void QScriptValueIteratorPrivate::setValue(const QScriptValuePrivate* value)
{
    if (!isValid())
        return;
    JSValueRef exception = 0;
    m_object->setProperty(m_idx.value(), *value, /* flags */ 0, &exception);
    engine()->setException(exception);
}

inline void QScriptValueIteratorPrivate::remove()
{
    if (!isValid())
        return;
    JSValueRef exception = 0;
    m_object->deleteProperty(m_idx.value(), &exception);
    engine()->setException(exception);
    m_idx.remove();
}

inline void QScriptValueIteratorPrivate::toFront()
{
    m_idx.toFront();
}

inline void QScriptValueIteratorPrivate::toBack()
{
    m_idx.toBack();
}

QScriptValue::PropertyFlags QScriptValueIteratorPrivate::flags() const
{
    if (!isValid())
        return QScriptValue::PropertyFlags(0);
    return m_object->propertyFlags(m_idx.value(), QScriptValue::ResolveLocal);
}

inline bool QScriptValueIteratorPrivate::isValid() const
{
    return m_object;
}

inline QScriptEnginePrivate* QScriptValueIteratorPrivate::engine() const
{
    Q_ASSERT(isValid());
    return m_object->engine();
}

#endif // qscriptvalueiterator_p_h