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