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
qv4setiterator.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/qv4setiterator_p.h>
8#include <private/qv4setobject_p.h>
9#include <private/qv4symbol_p.h>
10
11using namespace QV4;
12
14
15void SetIteratorPrototype::init(ExecutionEngine *e)
16{
17 defineDefaultProperty(QStringLiteral("next"), method_next, 0);
18
19 Scope scope(e);
20 ScopedString val(scope, e->newString(QLatin1String("Set Iterator")));
21 defineReadonlyConfigurableProperty(e->symbol_toStringTag(), val);
22}
23
24ReturnedValue SetIteratorPrototype::method_next(const FunctionObject *b, const Value *that, const Value *, int)
25{
26 Scope scope(b);
27 const SetIteratorObject *thisObject = that->as<SetIteratorObject>();
28 if (!thisObject)
29 return scope.engine->throwTypeError(QLatin1String("Not a Set Iterator instance"));
30
31 Scoped<SetObject> s(scope, thisObject->d()->iteratedSet);
32 uint index = thisObject->d()->setNextIndex;
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()->setNextIndex = index + 1;
45
46 if (itemKind == KeyValueIteratorKind) {
47 ScopedArrayObject resultArray(scope, scope.engine->newArrayObject());
48 resultArray->arrayReserve(2);
49 resultArray->arrayPut(0, arguments[0]);
50 resultArray->arrayPut(1, arguments[0]); // yes, the key is repeated.
51 resultArray->setArrayLengthUnchecked(2);
52
53 return IteratorPrototype::createIterResultObject(scope.engine, resultArray, false);
54 }
55
56 return IteratorPrototype::createIterResultObject(scope.engine, arguments[0], false);
57 }
58
59 thisObject->d()->iteratedSet.set(scope.engine, nullptr);
60 QV4::Value undefined = Value::undefinedValue();
61 return IteratorPrototype::createIterResultObject(scope.engine, undefined, true);
62}
DEFINE_OBJECT_VTABLE(SetIteratorObject)