Update to 5.0.0-beta1
[profile/ivi/qtdeclarative.git] / src / qml / qml / v8 / qjsvalueiterator_impl_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QJSVALUEITERATOR_IMPL_P_H
43 #define QJSVALUEITERATOR_IMPL_P_H
44
45 #include "qjsvalueiterator_p.h"
46 #include <private/qv8engine_p.h>
47 #include "qjsconverter_p.h"
48
49 QT_BEGIN_NAMESPACE
50
51 inline QJSValueIteratorPrivate::QJSValueIteratorPrivate(const QJSValuePrivate* value)
52     : m_object(const_cast<QJSValuePrivate*>(value))
53     , m_index(0)
54     , m_count(0)
55 {
56     Q_ASSERT(value);
57     QV8Engine *engine = m_object->engine();
58     if (!m_object->isObject())
59         m_object = 0;
60     else {
61         QScriptIsolate api(engine, QScriptIsolate::NotNullEngine);
62         v8::HandleScope scope;
63
64         v8::Handle<v8::Value> tmp = *value;
65         v8::Handle<v8::Object> obj = v8::Handle<v8::Object>::Cast(tmp);
66         v8::Local<v8::Array> names;
67
68         // FIXME we need newer V8!
69         //names = obj->GetOwnPropertyNames();
70         names = engine->getOwnPropertyNames(obj);
71         m_names = v8::Persistent<v8::Array>::New(names);
72         m_count = names->Length();
73
74         engine->registerValueIterator(this);
75     }
76 }
77
78 inline QJSValueIteratorPrivate::~QJSValueIteratorPrivate()
79 {
80     if (isValid()) {
81         engine()->unregisterValueIterator(this);
82         m_names.Dispose();
83     }
84 }
85
86 inline void QJSValueIteratorPrivate::invalidate()
87 {
88     m_names.Dispose();
89     m_object.reset();
90     m_index = 0;
91     m_count = 0;
92 }
93
94 inline bool QJSValueIteratorPrivate::hasNext() const
95 {
96     return isValid() ? m_index < m_count : false;
97 }
98
99 inline bool QJSValueIteratorPrivate::next()
100 {
101     if (hasNext()) {
102         ++m_index;
103         return true;
104     }
105     return false;
106 }
107
108 inline QString QJSValueIteratorPrivate::name() const
109 {
110     if (!isValid())
111         return QString();
112
113     v8::HandleScope handleScope;
114     return QJSConverter::toString(m_names->Get(m_index - 1)->ToString());
115 }
116
117 inline QScriptPassPointer<QJSValuePrivate> QJSValueIteratorPrivate::value() const
118 {
119     if (!isValid())
120         return new QJSValuePrivate();
121
122     v8::HandleScope handleScope;
123     return m_object->property(m_names->Get(m_index - 1)->ToString());
124 }
125
126 inline bool QJSValueIteratorPrivate::isValid() const
127 {
128     bool result = m_object ? !m_object->isUndefined() : false;
129     // We know that if this object is still valid then it is an object
130     // if this assumption is not correct then some other logic in this class
131     // have to be changed too.
132     Q_ASSERT(!result || m_object->isObject());
133     return result;
134 }
135
136 inline QV8Engine* QJSValueIteratorPrivate::engine() const
137 {
138     return m_object ? m_object->engine() : 0;
139 }
140
141 QT_END_NAMESPACE
142
143 #endif // QJSVALUEITERATOR_IMPL_P_H