aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@digia.com>2013-06-04 10:36:39 +0200
committerSimon Hausmann <simon.hausmann@digia.com>2013-06-04 11:00:55 +0200
commit1dbbecda8b4e34196d07ecfea65de87b0bd8b83a (patch)
treea3e027c397ad5d44d5acdb624d245b2b1d6d416a
parent734f65f151ccd491a2dd79fe511a7822016cf52b (diff)
Simplify the object iterator API
We were really missing methods to get both the key and the value in one go. Change-Id: I996bde79265f45312e68fcc6bdce76704385ea5b Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
-rw-r--r--src/imports/localstorage/plugin.cpp20
-rw-r--r--src/qml/qml/v4/qv4functionobject_p.h1
-rw-r--r--src/qml/qml/v4/qv4jsonobject.cpp26
-rw-r--r--src/qml/qml/v4/qv4objectiterator.cpp32
-rw-r--r--src/qml/qml/v4/qv4objectiterator_p.h4
-rw-r--r--src/qml/qml/v4/qv4objectproto.cpp21
-rw-r--r--src/qml/qml/v4/qv4v8.cpp12
-rw-r--r--src/qml/qml/v8/qv8engine.cpp11
-rw-r--r--src/qml/types/qqmllistmodel.cpp22
9 files changed, 64 insertions, 85 deletions
diff --git a/src/imports/localstorage/plugin.cpp b/src/imports/localstorage/plugin.cpp
index 92df9c3755..323b89b3e5 100644
--- a/src/imports/localstorage/plugin.cpp
+++ b/src/imports/localstorage/plugin.cpp
@@ -268,17 +268,17 @@ static Value qmlsqldatabase_executeSql(SimpleCallContext *ctx)
} else if (Object *object = values.asObject()) {
ObjectIterator it(object, ObjectIterator::WithProtoChain|ObjectIterator::EnumerableOnly);
while (1) {
- String *name;
- uint index;
- PropertyAttributes attrs;
- Property *p = it.next(&name, &index, &attrs);
- if (!p)
+ Value value;
+ Value key = it.nextPropertyName(&value);
+ if (key.isNull())
break;
- QVariant v = engine->toVariant(object->getValue(ctx, p, attrs), -1);
- if (name)
- query.bindValue(name->toQString(), v);
- else
- query.bindValue(index, v);
+ QVariant v = engine->toVariant(value, -1);
+ if (key.isString()) {
+ query.bindValue(key.stringValue()->toQString(), v);
+ } else {
+ assert(key.isInteger());
+ query.bindValue(key.integerValue(), v);
+ }
}
} else {
query.bindValue(0, engine->toVariant(values, -1));
diff --git a/src/qml/qml/v4/qv4functionobject_p.h b/src/qml/qml/v4/qv4functionobject_p.h
index 7e43e41561..1d04306b7e 100644
--- a/src/qml/qml/v4/qv4functionobject_p.h
+++ b/src/qml/qml/v4/qv4functionobject_p.h
@@ -66,7 +66,6 @@ namespace QV4 {
struct Value;
struct Function;
struct Object;
-struct ObjectIterator;
struct BooleanObject;
struct NumberObject;
struct StringObject;
diff --git a/src/qml/qml/v4/qv4jsonobject.cpp b/src/qml/qml/v4/qv4jsonobject.cpp
index cdc5ae70f2..45fe90dba2 100644
--- a/src/qml/qml/v4/qv4jsonobject.cpp
+++ b/src/qml/qml/v4/qv4jsonobject.cpp
@@ -778,18 +778,11 @@ QString Stringify::JO(Object *o)
ObjectIterator it(o, ObjectIterator::EnumerableOnly);
while (1) {
- String *name;
- uint index;
- PropertyAttributes attrs;
- Property *pd = it.next(&name, &index, &attrs);
- if (!pd)
+ Value v;
+ Value name = it.nextPropertyNameAsString(&v);
+ if (name.isNull())
break;
- Value v = o->getValue(ctx, pd, attrs);
- QString key;
- if (name)
- key = name->toQString();
- else
- key = QString::number(index);
+ QString key = name.toQString();
QString member = makeMember(key, v);
if (!member.isEmpty())
partial += member;
@@ -993,15 +986,12 @@ QJsonObject JsonObject::toJsonObject(QV4::Object *o, V4ObjectSet &visitedObjects
ObjectIterator it(o, ObjectIterator::EnumerableOnly);
while (1) {
- PropertyAttributes attributes;
- String *name;
- uint idx;
- Property *p = it.next(&name, &idx, &attributes);
- if (!p)
+ Value v;
+ Value name = it.nextPropertyNameAsString(&v);
+ if (name.isNull())
break;
- Value v = o->getValue(o->engine()->current, p, attributes);
- QString key = name ? name->toQString() : QString::number(idx);
+ QString key = name.toQString();
result.insert(key, toJsonValue(v, visitedObjects));
}
diff --git a/src/qml/qml/v4/qv4objectiterator.cpp b/src/qml/qml/v4/qv4objectiterator.cpp
index ce30aeec5f..67eb47b933 100644
--- a/src/qml/qml/v4/qv4objectiterator.cpp
+++ b/src/qml/qml/v4/qv4objectiterator.cpp
@@ -210,26 +210,38 @@ Property *ObjectIterator::next(String **name, uint *index, PropertyAttributes *a
return 0;
}
-Value ObjectIterator::nextPropertyName()
+Value ObjectIterator::nextPropertyName(Value *value)
{
+ PropertyAttributes attrs;
uint index;
String *name;
- next(&name, &index);
+ Property *p = next(&name, &index, &attrs);
+ if (!p)
+ return Value::nullValue();
+
+ if (value)
+ *value = object->getValue(object->engine()->current, p, attrs);
+
if (name)
return Value::fromString(name);
- if (index < UINT_MAX)
- return Value::fromDouble(index);
- return Value::nullValue();
+ assert(index < UINT_MAX);
+ return Value::fromDouble(index);
}
-Value ObjectIterator::nextPropertyNameAsString()
+Value ObjectIterator::nextPropertyNameAsString(Value *value)
{
+ PropertyAttributes attrs;
uint index;
String *name;
- next(&name, &index);
+ Property *p = next(&name, &index, &attrs);
+ if (!p)
+ return Value::nullValue();
+
+ if (value)
+ *value = object->getValue(object->engine()->current, p, attrs);
+
if (name)
return Value::fromString(name);
- if (index < UINT_MAX)
- return __qmljs_to_string(Value::fromDouble(index), object->internalClass->engine->current);
- return Value::nullValue();
+ assert(index < UINT_MAX);
+ return Value::fromString(object->engine()->newString(QString::number(index)));
}
diff --git a/src/qml/qml/v4/qv4objectiterator_p.h b/src/qml/qml/v4/qv4objectiterator_p.h
index bab0aa41a0..1036129979 100644
--- a/src/qml/qml/v4/qv4objectiterator_p.h
+++ b/src/qml/qml/v4/qv4objectiterator_p.h
@@ -82,8 +82,8 @@ struct Q_QML_EXPORT ObjectIterator
ObjectIterator(Object *o, uint flags);
Property *next(String **name, uint *index, PropertyAttributes *attributes = 0);
- Value nextPropertyName();
- Value nextPropertyNameAsString();
+ Value nextPropertyName(Value *value = 0);
+ Value nextPropertyNameAsString(Value *value = 0);
};
}
diff --git a/src/qml/qml/v4/qv4objectproto.cpp b/src/qml/qml/v4/qv4objectproto.cpp
index 5d34d331c6..efb40eefbb 100644
--- a/src/qml/qml/v4/qv4objectproto.cpp
+++ b/src/qml/qml/v4/qv4objectproto.cpp
@@ -351,19 +351,10 @@ Value ObjectPrototype::method_keys(SimpleCallContext *ctx)
ObjectIterator it(o, ObjectIterator::EnumerableOnly);
while (1) {
- uint index;
- String *name;
- Property *pd = it.next(&name, &index);
- if (!pd)
+ Value name = it.nextPropertyNameAsString();
+ if (name.isNull())
break;
- Value key;
- if (name) {
- key = Value::fromString(name);
- } else {
- key = Value::fromDouble(index);
- key = __qmljs_to_string(key, ctx);
- }
- a->push_back(key);
+ a->push_back(name);
}
return Value::fromObject(a);
@@ -567,10 +558,10 @@ ArrayObject *ObjectPrototype::getOwnPropertyNames(ExecutionEngine *v4, const Val
ObjectIterator it(O, ObjectIterator::NoFlags);
while (1) {
- Value v = it.nextPropertyNameAsString();
- if (v.isNull())
+ Value name = it.nextPropertyNameAsString();
+ if (name.isNull())
break;
- array->push_back(v);
+ array->push_back(name);
}
return array;
}
diff --git a/src/qml/qml/v4/qv4v8.cpp b/src/qml/qml/v4/qv4v8.cpp
index ccd560da7e..3b31311998 100644
--- a/src/qml/qml/v4/qv4v8.cpp
+++ b/src/qml/qml/v4/qv4v8.cpp
@@ -528,10 +528,10 @@ Handle<Array> Object::GetPropertyNames()
QV4::ArrayObject *array = currentEngine()->newArrayObject();
ObjectIterator it(o, ObjectIterator::WithProtoChain|ObjectIterator::EnumerableOnly);
while (1) {
- QV4::Value v = it.nextPropertyNameAsString();
- if (v.isNull())
+ QV4::Value name = it.nextPropertyNameAsString();
+ if (name.isNull())
break;
- array->push_back(v);
+ array->push_back(name);
}
return QV4::Value::fromObject(array);
}
@@ -544,10 +544,10 @@ Handle<Array> Object::GetOwnPropertyNames()
ArrayObject *array = currentEngine()->newArrayObject();
ObjectIterator it(o, ObjectIterator::EnumerableOnly);
while (1) {
- QV4::Value v = it.nextPropertyNameAsString();
- if (v.isNull())
+ QV4::Value name = it.nextPropertyNameAsString();
+ if (name.isNull())
break;
- array->push_back(v);
+ array->push_back(name);
}
return QV4::Value::fromObject(array);
}
diff --git a/src/qml/qml/v8/qv8engine.cpp b/src/qml/qml/v8/qv8engine.cpp
index 4af730fb41..3af2efb1c2 100644
--- a/src/qml/qml/v8/qv8engine.cpp
+++ b/src/qml/qml/v8/qv8engine.cpp
@@ -626,15 +626,12 @@ QVariantMap QV8Engine::variantMapFromJS(QV4::Object *o,
QV4::ObjectIterator it(o, QV4::ObjectIterator::EnumerableOnly);
while (1) {
- QV4::PropertyAttributes attributes;
- QV4::String *name;
- uint idx;
- QV4::Property *p = it.next(&name, &idx, &attributes);
- if (!p)
+ QV4::Value v;
+ QV4::Value name = it.nextPropertyNameAsString(&v);
+ if (name.isNull())
break;
- QV4::Value v = o->getValue(m_v4Engine->current, p, attributes);
- QString key = name ? name->toQString() : QString::number(idx);
+ QString key = name.toQString();
result.insert(key, variantFromJS(v, visitedObjects));
}
diff --git a/src/qml/types/qqmllistmodel.cpp b/src/qml/types/qqmllistmodel.cpp
index 4bb057756b..f016a9ab22 100644
--- a/src/qml/types/qqmllistmodel.cpp
+++ b/src/qml/types/qqmllistmodel.cpp
@@ -418,16 +418,11 @@ void ListModel::set(int elementIndex, QV4::Object *object, QVector<int> *roles,
QV4::ExecutionEngine *v4 = object->engine();
QV4::ObjectIterator it(object, QV4::ObjectIterator::WithProtoChain|QV4::ObjectIterator::EnumerableOnly);
while (1) {
- QV4::String *name;
- uint index;
- QV4::PropertyAttributes attrs;
- QV4::Property *p = it.next(&name, &index, &attrs);
- if (!p)
+ QV4::Value propertyValue;
+ QV4::Value propertyName = it.nextPropertyNameAsString(&propertyValue);
+ if (propertyName.isNull())
break;
- QV4::Value propertyName = QV4::Value::fromString(name ? name : v4->newString(QString::number(index)));
- QV4::Value propertyValue = object->getValue(v4->current, p, attrs);
-
// Check if this key exists yet
int roleIndex = -1;
@@ -489,16 +484,11 @@ void ListModel::set(int elementIndex, QV4::Object *object, QV8Engine *eng)
QV4::ExecutionEngine *v4 = object->engine();
QV4::ObjectIterator it(object, QV4::ObjectIterator::WithProtoChain|QV4::ObjectIterator::EnumerableOnly);
while (1) {
- QV4::String *name;
- uint index;
- QV4::PropertyAttributes attrs;
- QV4::Property *p = it.next(&name, &index, &attrs);
- if (!p)
+ QV4::Value propertyValue;
+ QV4::Value propertyName = it.nextPropertyNameAsString(&propertyValue);
+ if (propertyName.isNull())
break;
- QV4::Value propertyName = QV4::Value::fromString(name ? name : v4->newString(QString::number(index)));
- QV4::Value propertyValue = object->getValue(v4->current, p, attrs);
-
// Add the value now
if (QV4::String *s = propertyValue.asString()) {
const ListLayout::Role &r = m_layout->getRoleOrCreate(propertyName, ListLayout::Role::String);