2013-01-10 21:42:58 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
|
|
|
|
** Contact: http://www.qt-project.org/legal
|
|
|
|
**
|
|
|
|
** This file is part of the V4VM module of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
|
|
|
**
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
**
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3.0 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU General Public License version 3.0 requirements will be
|
|
|
|
** met: http://www.gnu.org/copyleft/gpl.html.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
2013-04-15 09:50:16 +00:00
|
|
|
#include "qv4objectiterator_p.h"
|
|
|
|
#include "qv4object_p.h"
|
|
|
|
#include "qv4stringobject_p.h"
|
|
|
|
#include "qv4identifier_p.h"
|
2013-01-10 21:42:58 +00:00
|
|
|
|
|
|
|
namespace QQmlJS {
|
|
|
|
namespace VM {
|
|
|
|
|
2013-04-18 13:33:37 +00:00
|
|
|
ObjectIterator::ObjectIterator(Object *o, uint flags)
|
|
|
|
: object(o)
|
2013-01-10 21:42:58 +00:00
|
|
|
, current(o)
|
|
|
|
, arrayNode(0)
|
|
|
|
, arrayIndex(0)
|
2013-02-10 21:22:53 +00:00
|
|
|
, memberIndex(0)
|
2013-01-10 21:42:58 +00:00
|
|
|
, flags(flags)
|
|
|
|
{
|
2013-02-10 21:22:53 +00:00
|
|
|
if (current) {
|
|
|
|
if (current->asStringObject())
|
|
|
|
this->flags |= CurrentIsString;
|
|
|
|
}
|
2013-01-10 21:42:58 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
Property *ObjectIterator::next(String **name, uint *index, PropertyAttributes *attrs)
|
2013-01-10 21:42:58 +00:00
|
|
|
{
|
2013-04-10 08:46:23 +00:00
|
|
|
Property *p = 0;
|
2013-01-10 21:42:58 +00:00
|
|
|
*name = 0;
|
|
|
|
*index = UINT_MAX;
|
|
|
|
while (1) {
|
|
|
|
if (!current)
|
|
|
|
break;
|
|
|
|
|
2013-01-14 14:54:07 +00:00
|
|
|
if (flags & CurrentIsString) {
|
|
|
|
StringObject *s = static_cast<StringObject *>(current);
|
|
|
|
uint slen = s->value.stringValue()->toQString().length();
|
|
|
|
while (arrayIndex < slen) {
|
|
|
|
*index = arrayIndex;
|
|
|
|
++arrayIndex;
|
2013-04-10 08:46:23 +00:00
|
|
|
if (attrs)
|
|
|
|
*attrs = s->arrayAttributes ? s->arrayAttributes[arrayIndex] : PropertyAttributes(Attr_NotWritable|Attr_NotConfigurable);
|
2013-04-12 12:43:58 +00:00
|
|
|
return s->__getOwnProperty__(*index);
|
2013-01-14 14:54:07 +00:00
|
|
|
}
|
|
|
|
flags &= ~CurrentIsString;
|
2013-02-03 10:36:55 +00:00
|
|
|
arrayNode = current->sparseArrayBegin();
|
2013-01-14 14:54:07 +00:00
|
|
|
// iterate until we're past the end of the string
|
|
|
|
while (arrayNode && arrayNode->key() < slen)
|
|
|
|
arrayNode = arrayNode->nextNode();
|
|
|
|
}
|
|
|
|
|
2013-01-10 21:42:58 +00:00
|
|
|
if (!arrayIndex)
|
2013-02-03 10:36:55 +00:00
|
|
|
arrayNode = current->sparseArrayBegin();
|
2013-01-10 21:42:58 +00:00
|
|
|
|
|
|
|
// sparse arrays
|
|
|
|
if (arrayNode) {
|
2013-02-03 10:36:55 +00:00
|
|
|
while (arrayNode != current->sparseArrayEnd()) {
|
2013-01-10 21:42:58 +00:00
|
|
|
int k = arrayNode->key();
|
2013-04-10 08:46:23 +00:00
|
|
|
uint pidx = arrayNode->value;
|
|
|
|
p = current->arrayData + pidx;
|
2013-01-10 21:42:58 +00:00
|
|
|
arrayNode = arrayNode->nextNode();
|
2013-04-10 08:46:23 +00:00
|
|
|
PropertyAttributes a = current->arrayAttributes ? current->arrayAttributes[pidx] : PropertyAttributes(Attr_Data);
|
|
|
|
if (!(flags & EnumberableOnly) || a.isEnumerable()) {
|
2013-01-10 21:42:58 +00:00
|
|
|
arrayIndex = k + 1;
|
|
|
|
*index = k;
|
2013-04-10 08:46:23 +00:00
|
|
|
if (attrs)
|
|
|
|
*attrs = a;
|
2013-01-10 21:42:58 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arrayNode = 0;
|
|
|
|
arrayIndex = UINT_MAX;
|
|
|
|
}
|
|
|
|
// dense arrays
|
2013-02-03 21:08:39 +00:00
|
|
|
while (arrayIndex < current->arrayDataLen) {
|
2013-04-10 08:46:23 +00:00
|
|
|
uint pidx = current->propertyIndexFromArrayIndex(arrayIndex);
|
|
|
|
p = current->arrayData + pidx;
|
|
|
|
PropertyAttributes a = current->arrayAttributes ? current->arrayAttributes[pidx] : PropertyAttributes(Attr_Data);
|
2013-01-10 21:42:58 +00:00
|
|
|
++arrayIndex;
|
2013-04-10 08:46:23 +00:00
|
|
|
if ((!current->arrayAttributes || !current->arrayAttributes[pidx].isGeneric())
|
|
|
|
&& (!(flags & EnumberableOnly) || a.isEnumerable())) {
|
2013-01-10 21:42:58 +00:00
|
|
|
*index = arrayIndex - 1;
|
2013-04-10 08:46:23 +00:00
|
|
|
if (attrs)
|
|
|
|
*attrs = a;
|
2013-01-10 21:42:58 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-10 08:46:23 +00:00
|
|
|
if (memberIndex == current->internalClass->size) {
|
2013-01-10 21:42:58 +00:00
|
|
|
if (flags & WithProtoChain)
|
|
|
|
current = current->prototype;
|
|
|
|
else
|
|
|
|
current = 0;
|
2013-01-14 14:54:07 +00:00
|
|
|
if (current && current->asStringObject())
|
|
|
|
flags |= CurrentIsString;
|
|
|
|
else
|
|
|
|
flags &= ~CurrentIsString;
|
|
|
|
|
|
|
|
|
2013-01-10 21:42:58 +00:00
|
|
|
arrayIndex = 0;
|
2013-02-10 21:22:53 +00:00
|
|
|
memberIndex = 0;
|
2013-01-10 21:42:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-04-10 08:46:23 +00:00
|
|
|
String *n = current->internalClass->nameMap.at(memberIndex);
|
2013-02-10 21:22:53 +00:00
|
|
|
assert(n);
|
2013-01-10 21:42:58 +00:00
|
|
|
// ### check that it's not a repeated attribute
|
2013-02-10 21:22:53 +00:00
|
|
|
|
|
|
|
p = current->memberData + memberIndex;
|
2013-04-10 08:46:23 +00:00
|
|
|
PropertyAttributes a = current->internalClass->propertyData[memberIndex];
|
2013-02-10 21:22:53 +00:00
|
|
|
++memberIndex;
|
2013-04-10 08:46:23 +00:00
|
|
|
if (!(flags & EnumberableOnly) || a.isEnumerable()) {
|
2013-02-10 21:22:53 +00:00
|
|
|
*name = n;
|
2013-04-10 08:46:23 +00:00
|
|
|
if (attrs)
|
|
|
|
*attrs = a;
|
2013-02-10 21:22:53 +00:00
|
|
|
return p;
|
2013-01-10 21:42:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value ObjectIterator::nextPropertyName()
|
|
|
|
{
|
|
|
|
uint index;
|
|
|
|
String *name;
|
|
|
|
next(&name, &index);
|
|
|
|
if (name)
|
|
|
|
return Value::fromString(name);
|
|
|
|
if (index < UINT_MAX)
|
|
|
|
return Value::fromDouble(index);
|
|
|
|
return Value::nullValue();
|
|
|
|
}
|
|
|
|
|
2013-01-14 14:54:07 +00:00
|
|
|
Value ObjectIterator::nextPropertyNameAsString()
|
2013-01-10 21:42:58 +00:00
|
|
|
{
|
|
|
|
uint index;
|
|
|
|
String *name;
|
|
|
|
next(&name, &index);
|
|
|
|
if (name)
|
|
|
|
return Value::fromString(name);
|
|
|
|
if (index < UINT_MAX)
|
2013-04-18 13:33:37 +00:00
|
|
|
return __qmljs_to_string(Value::fromDouble(index), object->internalClass->engine->current);
|
2013-01-10 21:42:58 +00:00
|
|
|
return Value::nullValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|