Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qv4arrayiterator.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 Crimson AS <info@crimson.no>
2// Copyright (C) 2018 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant
5
6#include <private/qv4iterator_p.h>
7#include <private/qv4arrayiterator_p.h>
8#include <private/qv4typedarray_p.h>
9#include <private/qv4symbol_p.h>
10
11using namespace QV4;
12
14
15void ArrayIteratorPrototype::init(ExecutionEngine *e)
16{
17 defineDefaultProperty(QStringLiteral("next"), method_next, 0);
18
19 Scope scope(e);
20 ScopedString val(scope, e->newString(QLatin1String("Array Iterator")));
21 defineReadonlyConfigurableProperty(e->symbol_toStringTag(), val);
22}
23
24ReturnedValue ArrayIteratorPrototype::method_next(const FunctionObject *b, const Value *that, const Value *, int)
25{
26 Scope scope(b);
27 const ArrayIteratorObject *thisObject = that->as<ArrayIteratorObject>();
28 if (!thisObject)
29 return scope.engine->throwTypeError(QLatin1String("Not an Array Iterator instance"));
30
31 ScopedObject a(scope, thisObject->d()->iteratedObject);
32 if (!a) {
33 QV4::Value undefined = Value::undefinedValue();
34 return IteratorPrototype::createIterResultObject(scope.engine, undefined, true);
35 }
36
37 quint32 index = thisObject->d()->nextIndex;
38 IteratorKind itemKind = thisObject->d()->iterationKind;
39
40 quint32 len = a->getLength();
41
42 if (index >= len) {
43 thisObject->d()->iteratedObject.set(scope.engine, nullptr);
44 QV4::Value undefined = Value::undefinedValue();
45 return IteratorPrototype::createIterResultObject(scope.engine, undefined, true);
46 }
47
48 thisObject->d()->nextIndex = index + 1;
49 if (itemKind == KeyIteratorKind) {
50 return IteratorPrototype::createIterResultObject(scope.engine, Value::fromInt32(index), false);
51 }
52
53 QV4::ScopedValue elementValue(scope, a->get(index));
54 CHECK_EXCEPTION();
55
56 if (itemKind == ValueIteratorKind) {
57 return IteratorPrototype::createIterResultObject(scope.engine, elementValue, false);
58 } else {
59 Q_ASSERT(itemKind == KeyValueIteratorKind);
60
61 ScopedArrayObject resultArray(scope, scope.engine->newArrayObject());
62 resultArray->arrayReserve(2);
63 resultArray->arrayPut(0, Value::fromInt32(index));
64 resultArray->arrayPut(1, elementValue);
65 resultArray->setArrayLengthUnchecked(2);
66
67 return IteratorPrototype::createIterResultObject(scope.engine, resultArray, false);
68 }
69}
DEFINE_OBJECT_VTABLE(ArrayIteratorObject)