1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39#include "qv4objectiterator_p.h"
40#include "qv4object_p.h"
41#include "qv4stringobject_p.h"
42#include "qv4identifier_p.h"
43#include "qv4argumentsobject_p.h"
44#include "qv4string_p.h"
45#include "qv4iterator_p.h"
46#include "qv4propertykey_p.h"
47
48using namespace QV4;
49
50void ForInIteratorPrototype::init(ExecutionEngine *)
51{
52 defineDefaultProperty(QStringLiteral("next"), code: method_next, argumentCount: 0);
53}
54
55PropertyKey ObjectIterator::next(Property *pd, PropertyAttributes *attrs)
56{
57 if (!object || !iterator)
58 return PropertyKey::invalid();
59
60 Scope scope(engine);
61 ScopedPropertyKey key(scope);
62
63 while (1) {
64 key = iterator->next(o: object, p: pd, attrs);
65 if (!key->isValid()) {
66 object = nullptr;
67 return key;
68 }
69 if ((!(flags & WithSymbols) && key->isSymbol()) ||
70 ((flags & EnumerableOnly) && !attrs->isEnumerable()))
71 continue;
72 return key;
73 }
74}
75
76ReturnedValue ObjectIterator::nextPropertyName(Value *value)
77{
78 if (!object)
79 return Encode::null();
80
81 PropertyAttributes attrs;
82 Scope scope(engine);
83 ScopedProperty p(scope);
84 ScopedPropertyKey key(scope, next(pd: p, attrs: &attrs));
85 if (!key->isValid())
86 return Encode::null();
87
88 *value = object->getValue(v: p->value, attrs);
89 if (key->isArrayIndex())
90 return Encode(key->asArrayIndex());
91 Q_ASSERT(key->isStringOrSymbol());
92 return key->asStringOrSymbol()->asReturnedValue();
93}
94
95ReturnedValue ObjectIterator::nextPropertyNameAsString(Value *value)
96{
97 if (!object)
98 return Encode::null();
99
100 PropertyAttributes attrs;
101 Scope scope(engine);
102 ScopedProperty p(scope);
103 ScopedPropertyKey key(scope, next(pd: p, attrs: &attrs));
104 if (!key->isValid())
105 return Encode::null();
106
107 *value = object->getValue(v: p->value, attrs);
108
109 return key->toStringOrSymbol(e: engine)->asReturnedValue();
110}
111
112ReturnedValue ObjectIterator::nextPropertyNameAsString()
113{
114 if (!object)
115 return Encode::null();
116
117 PropertyAttributes attrs;
118 Scope scope(engine);
119 ScopedPropertyKey key(scope, next(pd: nullptr, attrs: &attrs));
120 if (!key->isValid())
121 return Encode::null();
122
123 return key->toStringOrSymbol(e: engine)->asReturnedValue();
124}
125
126
127DEFINE_OBJECT_VTABLE(ForInIteratorObject);
128
129void Heap::ForInIteratorObject::markObjects(Heap::Base *that, MarkStack *markStack)
130{
131 ForInIteratorObject *o = static_cast<ForInIteratorObject *>(that);
132 if (o->object)
133 o->object->mark(markStack);
134 if (o->current)
135 o->current->mark(markStack);
136 o->workArea[0].mark(markStack);
137 o->workArea[1].mark(markStack);
138 Object::markObjects(base: that, stack: markStack);
139}
140
141void Heap::ForInIteratorObject::destroy()
142{
143 delete iterator;
144}
145
146ReturnedValue ForInIteratorPrototype::method_next(const FunctionObject *b, const Value *thisObject, const Value *, int)
147{
148 const ForInIteratorObject *forIn = static_cast<const ForInIteratorObject *>(thisObject);
149 Q_ASSERT(forIn);
150 Scope scope(b);
151
152 ScopedPropertyKey key(scope, forIn->nextProperty());
153 bool done = false;
154 if (!key->isValid())
155 done = true;
156 ScopedStringOrSymbol s(scope, key->toStringOrSymbol(e: scope.engine));
157 return IteratorPrototype::createIterResultObject(engine: scope.engine, value: s, done);
158}
159
160
161PropertyKey ForInIteratorObject::nextProperty() const
162{
163 if (!d()->current)
164 return PropertyKey::invalid();
165
166 Scope scope(this);
167 ScopedObject c(scope, d()->current);
168 ScopedObject t(scope, d()->target);
169 ScopedObject o(scope);
170 ScopedProperty p(scope);
171 ScopedPropertyKey key(scope);
172 PropertyAttributes attrs;
173
174 while (1) {
175 while (1) {
176 key = d()->iterator->next(o: t, p, attrs: &attrs);
177 if (!key->isValid())
178 break;
179 if (!attrs.isEnumerable() || key->isSymbol())
180 continue;
181 // check the property is not already defined earlier in the proto chain
182 if (d()->current != d()->object) {
183 o = d()->object;
184 bool shadowed = false;
185 while (o && o->d() != c->heapObject()) {
186 if (o->getOwnProperty(id: key) != Attr_Invalid) {
187 shadowed = true;
188 break;
189 }
190 o = o->getPrototypeOf();
191 }
192 if (shadowed)
193 continue;
194 }
195 return key;
196 }
197
198 c = c->getPrototypeOf();
199 d()->current.set(e: scope.engine, newVal: c->d());
200 if (!c)
201 break;
202 delete d()->iterator;
203 d()->iterator = c->ownPropertyKeys(target: t.getRef());
204 d()->target.set(e: scope.engine, newVal: t->d());
205 if (!d()->iterator) {
206 scope.engine->throwTypeError();
207 return PropertyKey::invalid();
208 }
209 }
210 return PropertyKey::invalid();
211}
212

source code of qtdeclarative/src/qml/jsruntime/qv4objectiterator.cpp