1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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