1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #ifndef QJSVALUEITERATOR_IMPL_P_H
43 #define QJSVALUEITERATOR_IMPL_P_H
45 #include "qjsvalueiterator_p.h"
46 #include <private/qv8engine_p.h>
47 #include "qjsconverter_p.h"
51 inline QJSValueIteratorPrivate::QJSValueIteratorPrivate(const QJSValuePrivate* value)
52 : m_object(const_cast<QJSValuePrivate*>(value))
57 QV8Engine *engine = m_object->engine();
58 if (!m_object->isObject())
61 QScriptIsolate api(engine, QScriptIsolate::NotNullEngine);
62 v8::HandleScope scope;
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;
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();
74 engine->registerValueIterator(this);
78 inline QJSValueIteratorPrivate::~QJSValueIteratorPrivate()
81 engine()->unregisterValueIterator(this);
86 inline void QJSValueIteratorPrivate::invalidate()
94 inline bool QJSValueIteratorPrivate::hasNext() const
96 return isValid() ? m_index < m_count : false;
99 inline bool QJSValueIteratorPrivate::next()
108 inline QString QJSValueIteratorPrivate::name() const
113 v8::HandleScope handleScope;
114 return QJSConverter::toString(m_names->Get(m_index - 1)->ToString());
117 inline QScriptPassPointer<QJSValuePrivate> QJSValueIteratorPrivate::value() const
120 return new QJSValuePrivate();
122 v8::HandleScope handleScope;
123 return m_object->property(m_names->Get(m_index - 1)->ToString());
126 inline bool QJSValueIteratorPrivate::isValid() const
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());
136 inline QV8Engine* QJSValueIteratorPrivate::engine() const
138 return m_object ? m_object->engine() : 0;
143 #endif // QJSVALUEITERATOR_IMPL_P_H