1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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.
18 ** If you have questions regarding the use of this file, please contact
19 ** us via http://www.qt-project.org/.
22 ****************************************************************************/
24 #ifndef QJSVALUEITERATOR_IMPL_P_H
25 #define QJSVALUEITERATOR_IMPL_P_H
27 #include "qjsvalueiterator_p.h"
28 #include <private/qv8engine_p.h>
29 #include "qjsconverter_p.h"
31 inline QJSValueIteratorPrivate::QJSValueIteratorPrivate(const QJSValuePrivate* value)
32 : m_object(const_cast<QJSValuePrivate*>(value))
37 QV8Engine *engine = m_object->engine();
38 if (!m_object->isObject())
41 QScriptIsolate api(engine, QScriptIsolate::NotNullEngine);
42 v8::HandleScope scope;
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;
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();
54 engine->registerValueIterator(this);
58 inline QJSValueIteratorPrivate::~QJSValueIteratorPrivate()
61 engine()->unregisterValueIterator(this);
66 inline void QJSValueIteratorPrivate::invalidate()
74 inline bool QJSValueIteratorPrivate::hasNext() const
76 return isValid() ? m_index < m_count : false;
79 inline bool QJSValueIteratorPrivate::next()
88 inline QString QJSValueIteratorPrivate::name() const
93 v8::HandleScope handleScope;
94 return QJSConverter::toString(m_names->Get(m_index - 1)->ToString());
97 inline QScriptPassPointer<QJSValuePrivate> QJSValueIteratorPrivate::value() const
100 return InvalidValue();
102 v8::HandleScope handleScope;
103 return m_object->property(m_names->Get(m_index - 1)->ToString());
106 inline bool QJSValueIteratorPrivate::isValid() const
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());
116 inline QV8Engine* QJSValueIteratorPrivate::engine() const
118 return m_object ? m_object->engine() : 0;
121 #endif // QJSVALUEITERATOR_IMPL_P_H