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
48 using namespace QV4;
49
init(ExecutionEngine *)50 void ForInIteratorPrototype::init(ExecutionEngine *)
51 {
52 defineDefaultProperty(QStringLiteral("next"), method_next, 0);
53 }
54
next(Property * pd,PropertyAttributes * attrs)55 PropertyKey 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(object, 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
nextPropertyName(Value * value)76 ReturnedValue 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(p, &attrs));
85 if (!key->isValid())
86 return Encode::null();
87
88 *value = object->getValue(p->value, attrs);
89 if (key->isArrayIndex())
90 return Encode(key->asArrayIndex());
91 Q_ASSERT(key->isStringOrSymbol());
92 return key->asStringOrSymbol()->asReturnedValue();
93 }
94
nextPropertyNameAsString(Value * value)95 ReturnedValue 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(p, &attrs));
104 if (!key->isValid())
105 return Encode::null();
106
107 *value = object->getValue(p->value, attrs);
108
109 return key->toStringOrSymbol(engine)->asReturnedValue();
110 }
111
nextPropertyNameAsString()112 ReturnedValue ObjectIterator::nextPropertyNameAsString()
113 {
114 if (!object)
115 return Encode::null();
116
117 PropertyAttributes attrs;
118 Scope scope(engine);
119 ScopedPropertyKey key(scope, next(nullptr, &attrs));
120 if (!key->isValid())
121 return Encode::null();
122
123 return key->toStringOrSymbol(engine)->asReturnedValue();
124 }
125
126
127 DEFINE_OBJECT_VTABLE(ForInIteratorObject);
128
markObjects(Heap::Base * that,MarkStack * markStack)129 void 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(that, markStack);
139 }
140
destroy()141 void Heap::ForInIteratorObject::destroy()
142 {
143 delete iterator;
144 }
145
method_next(const FunctionObject * b,const Value * thisObject,const Value *,int)146 ReturnedValue 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(scope.engine));
157 return IteratorPrototype::createIterResultObject(scope.engine, s, done);
158 }
159
160
nextProperty() const161 PropertyKey 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(t, p, &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(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(scope.engine, c->d());
200 if (!c)
201 break;
202 delete d()->iterator;
203 d()->iterator = c->ownPropertyKeys(t.getRef());
204 d()->target.set(scope.engine, t->d());
205 if (!d()->iterator) {
206 scope.engine->throwTypeError();
207 return PropertyKey::invalid();
208 }
209 }
210 return PropertyKey::invalid();
211 }
212