1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 Crimson AS <info@crimson.no> |
4 | ** Copyright (C) 2018 The Qt Company Ltd. |
5 | ** Contact: https://www.qt.io/licensing/ |
6 | ** |
7 | ** This file is part of the QtQml module of the Qt Toolkit. |
8 | ** |
9 | ** $QT_BEGIN_LICENSE:LGPL$ |
10 | ** Commercial License Usage |
11 | ** Licensees holding valid commercial Qt licenses may use this file in |
12 | ** accordance with the commercial license agreement provided with the |
13 | ** Software or, alternatively, in accordance with the terms contained in |
14 | ** a written agreement between you and The Qt Company. For licensing terms |
15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
16 | ** information use the contact form at https://www.qt.io/contact-us. |
17 | ** |
18 | ** GNU Lesser General Public License Usage |
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
20 | ** General Public License version 3 as published by the Free Software |
21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
22 | ** packaging of this file. Please review the following information to |
23 | ** ensure the GNU Lesser General Public License version 3 requirements |
24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
25 | ** |
26 | ** GNU General Public License Usage |
27 | ** Alternatively, this file may be used under the terms of the GNU |
28 | ** General Public License version 2.0 or (at your option) the GNU General |
29 | ** Public license version 3 or any later version approved by the KDE Free |
30 | ** Qt Foundation. The licenses are as published by the Free Software |
31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
32 | ** included in the packaging of this file. Please review the following |
33 | ** information to ensure the GNU General Public License requirements will |
34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
36 | ** |
37 | ** $QT_END_LICENSE$ |
38 | ** |
39 | ****************************************************************************/ |
40 | |
41 | #include <private/qv4iterator_p.h> |
42 | #include <private/qv4arrayiterator_p.h> |
43 | #include <private/qv4typedarray_p.h> |
44 | #include <private/qv4symbol_p.h> |
45 | |
46 | using namespace QV4; |
47 | |
48 | DEFINE_OBJECT_VTABLE(ArrayIteratorObject); |
49 | |
50 | void ArrayIteratorPrototype::init(ExecutionEngine *e) |
51 | { |
52 | defineDefaultProperty(QStringLiteral("next" ), code: method_next, argumentCount: 0); |
53 | |
54 | Scope scope(e); |
55 | ScopedString val(scope, e->newString(s: QLatin1String("Array Iterator" ))); |
56 | defineReadonlyConfigurableProperty(name: e->symbol_toStringTag(), value: val); |
57 | } |
58 | |
59 | ReturnedValue ArrayIteratorPrototype::method_next(const FunctionObject *b, const Value *that, const Value *, int) |
60 | { |
61 | Scope scope(b); |
62 | const ArrayIteratorObject *thisObject = that->as<ArrayIteratorObject>(); |
63 | if (!thisObject) |
64 | return scope.engine->throwTypeError(message: QLatin1String("Not an Array Iterator instance" )); |
65 | |
66 | ScopedObject a(scope, thisObject->d()->iteratedObject); |
67 | if (!a) { |
68 | QV4::Value undefined = Value::undefinedValue(); |
69 | return IteratorPrototype::createIterResultObject(engine: scope.engine, value: undefined, done: true); |
70 | } |
71 | |
72 | quint32 index = thisObject->d()->nextIndex; |
73 | IteratorKind itemKind = thisObject->d()->iterationKind; |
74 | |
75 | Scoped<TypedArray> ta(scope, a->as<TypedArray>()); |
76 | quint32 len = a->getLength(); |
77 | |
78 | if (index >= len) { |
79 | thisObject->d()->iteratedObject.set(e: scope.engine, newVal: nullptr); |
80 | QV4::Value undefined = Value::undefinedValue(); |
81 | return IteratorPrototype::createIterResultObject(engine: scope.engine, value: undefined, done: true); |
82 | } |
83 | |
84 | thisObject->d()->nextIndex = index + 1; |
85 | if (itemKind == KeyIteratorKind) { |
86 | return IteratorPrototype::createIterResultObject(engine: scope.engine, value: Value::fromInt32(i: index), done: false); |
87 | } |
88 | |
89 | ReturnedValue elementValue = a->get(idx: index); |
90 | CHECK_EXCEPTION(); |
91 | |
92 | if (itemKind == ValueIteratorKind) { |
93 | return IteratorPrototype::createIterResultObject(engine: scope.engine, value: Value::fromReturnedValue(val: elementValue), done: false); |
94 | } else { |
95 | Q_ASSERT(itemKind == KeyValueIteratorKind); |
96 | |
97 | ScopedArrayObject resultArray(scope, scope.engine->newArrayObject()); |
98 | resultArray->arrayReserve(n: 2); |
99 | resultArray->arrayPut(index: 0, value: Value::fromInt32(i: index)); |
100 | resultArray->arrayPut(index: 1, value: Value::fromReturnedValue(val: elementValue)); |
101 | resultArray->setArrayLengthUnchecked(2); |
102 | |
103 | return IteratorPrototype::createIterResultObject(engine: scope.engine, value: resultArray, done: false); |
104 | } |
105 | } |
106 | |
107 | |