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
qv4mapiterator.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 Crimson AS <info@crimson.no>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
5#include <private/qv4iterator_p.h>
6#include <private/qv4estable_p.h>
7#include <private/qv4mapiterator_p.h>
8#include <private/qv4mapobject_p.h>
9#include <private/qv4symbol_p.h>
10
11using namespace QV4;
12
14
15void MapIteratorPrototype::init(ExecutionEngine *e)
16{
17 defineDefaultProperty(QStringLiteral("next"), method_next, 0);
18
19 Scope scope(e);
20 ScopedString val(scope, e->newString(QLatin1String("Map Iterator")));
21 defineReadonlyConfigurableProperty(e->symbol_toStringTag(), val);
22}
23
24ReturnedValue MapIteratorPrototype::method_next(const FunctionObject *b, const Value *that, const Value *, int)
25{
26 Scope scope(b);
27 const MapIteratorObject *thisObject = that->as<MapIteratorObject>();
28 if (!thisObject)
29 return scope.engine->throwTypeError(QLatin1String("Not a Map Iterator instance"));
30
31 Scoped<MapObject> s(scope, thisObject->d()->iteratedMap);
32 uint index = thisObject->d()->mapNextIndex;
33 IteratorKind itemKind = thisObject->d()->iterationKind;
34
35 if (!s) {
36 QV4::Value undefined = Value::undefinedValue();
37 return IteratorPrototype::createIterResultObject(scope.engine, undefined, true);
38 }
39
40 Value *arguments = scope.constructUndefined(2);
41
42 while (index < s->d()->esTable->size()) {
43 s->d()->esTable->iterate(index, &arguments[0], &arguments[1]);
44 thisObject->d()->mapNextIndex = index + 1;
45
46 ScopedValue result(scope);
47
48 if (itemKind == KeyIteratorKind) {
49 result = arguments[0];
50 } else if (itemKind == ValueIteratorKind) {
51 result = arguments[1];
52 } else {
53 Q_ASSERT(itemKind == KeyValueIteratorKind);
54
55 result = scope.engine->newArrayObject();
56
57 Scoped<ArrayObject> resultArray(scope, result);
58 resultArray->arrayReserve(2);
59 resultArray->arrayPut(0, arguments[0]);
60 resultArray->arrayPut(1, arguments[1]);
61 resultArray->setArrayLengthUnchecked(2);
62 }
63
64 return IteratorPrototype::createIterResultObject(scope.engine, result, false);
65 }
66
67 thisObject->d()->iteratedMap.set(scope.engine, nullptr);
68 QV4::Value undefined = Value::undefinedValue();
69 return IteratorPrototype::createIterResultObject(scope.engine, undefined, true);
70}
DEFINE_OBJECT_VTABLE(MapIteratorObject)