initial import
[vuplus_webkit] / Source / JavaScriptCore / qt / api / qscriptvalueiterator_p.h
1 /*
2     Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
3
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Library General Public
6     License as published by the Free Software Foundation; either
7     version 2 of the License, or (at your option) any later version.
8
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12     Library General Public License for more details.
13
14     You should have received a copy of the GNU Library General Public License
15     along with this library; see the file COPYING.LIB.  If not, write to
16     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17     Boston, MA 02110-1301, USA.
18 */
19
20 #ifndef qscriptvalueiterator_p_h
21 #define qscriptvalueiterator_p_h
22
23 #include "qscriptvalue_p.h"
24 #include <JavaScriptCore/JavaScript.h>
25 #include <JavaScriptCore/JSRetainPtr.h>
26 #include <QtCore/qshareddata.h>
27 #include <QtCore/qvector.h>
28
29 class QScriptValueIteratorPrivate: public QSharedData {
30 public:
31     inline QScriptValueIteratorPrivate(const QScriptValuePrivate* value);
32     inline ~QScriptValueIteratorPrivate();
33
34     inline bool hasNext();
35     inline void next();
36
37     inline bool hasPrevious();
38     inline void previous();
39
40     inline QString name() const;
41     inline QScriptStringPrivate* scriptName() const;
42
43     inline QScriptValuePrivate* value() const;
44     inline void setValue(const QScriptValuePrivate* value);
45
46     inline void remove();
47
48     inline void toFront();
49     inline void toBack();
50
51     QScriptValue::PropertyFlags flags() const;
52
53     inline bool isValid() const;
54 private:
55     inline QScriptEnginePrivate* engine() const;
56
57     QExplicitlySharedDataPointer<QScriptValuePrivate> m_object;
58     QVector<JSStringRef> m_names;
59     QMutableVectorIterator<JSStringRef> m_idx;
60 };
61
62 inline QScriptValueIteratorPrivate::QScriptValueIteratorPrivate(const QScriptValuePrivate* value)
63     : m_object(const_cast<QScriptValuePrivate*>(value))
64     , m_idx(m_names)
65 {
66     if (m_object->isObject()) {
67         m_names = engine()->objectGetOwnPropertyNames(*m_object);
68         m_idx = m_names;
69     } else
70         m_object = 0;
71 }
72
73 inline QScriptValueIteratorPrivate::~QScriptValueIteratorPrivate()
74 {
75     QVector<JSStringRef>::const_iterator i = m_names.constBegin();
76     for (; i != m_names.constEnd(); ++i)
77         JSStringRelease(*i);
78 }
79
80 inline bool QScriptValueIteratorPrivate::hasNext()
81 {
82     return m_idx.hasNext();
83 }
84
85 inline void QScriptValueIteratorPrivate::next()
86 {
87     // FIXME (Qt5) This method should return a value (QTBUG-11226).
88     m_idx.next();
89 }
90
91 inline bool QScriptValueIteratorPrivate::hasPrevious()
92 {
93     return m_idx.hasPrevious();
94 }
95
96 inline void QScriptValueIteratorPrivate::previous()
97 {
98     m_idx.previous();
99 }
100
101 inline QString QScriptValueIteratorPrivate::name() const
102 {
103     if (!isValid())
104         return QString();
105     return QScriptConverter::toString(m_idx.value());
106 }
107
108 inline QScriptStringPrivate* QScriptValueIteratorPrivate::scriptName() const
109 {
110     if (!isValid())
111         return new QScriptStringPrivate();
112     return new QScriptStringPrivate(QScriptConverter::toString(m_idx.value()));
113 }
114
115 inline QScriptValuePrivate* QScriptValueIteratorPrivate::value() const
116 {
117     if (!isValid())
118         return new QScriptValuePrivate();
119     JSValueRef exception = 0;
120     JSValueRef value = m_object->property(m_idx.value(), &exception);
121     engine()->setException(exception);
122     return new QScriptValuePrivate(engine(), value);
123 }
124
125 inline void QScriptValueIteratorPrivate::setValue(const QScriptValuePrivate* value)
126 {
127     if (!isValid())
128         return;
129     JSValueRef exception = 0;
130     m_object->setProperty(m_idx.value(), *value, /* flags */ 0, &exception);
131     engine()->setException(exception);
132 }
133
134 inline void QScriptValueIteratorPrivate::remove()
135 {
136     if (!isValid())
137         return;
138     JSValueRef exception = 0;
139     m_object->deleteProperty(m_idx.value(), &exception);
140     engine()->setException(exception);
141     m_idx.remove();
142 }
143
144 inline void QScriptValueIteratorPrivate::toFront()
145 {
146     m_idx.toFront();
147 }
148
149 inline void QScriptValueIteratorPrivate::toBack()
150 {
151     m_idx.toBack();
152 }
153
154 QScriptValue::PropertyFlags QScriptValueIteratorPrivate::flags() const
155 {
156     if (!isValid())
157         return QScriptValue::PropertyFlags(0);
158     return m_object->propertyFlags(m_idx.value(), QScriptValue::ResolveLocal);
159 }
160
161 inline bool QScriptValueIteratorPrivate::isValid() const
162 {
163     return m_object;
164 }
165
166 inline QScriptEnginePrivate* QScriptValueIteratorPrivate::engine() const
167 {
168     Q_ASSERT(isValid());
169     return m_object->engine();
170 }
171
172 #endif // qscriptvalueiterator_p_h