summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/SetIterator.cpp
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-06-09 00:17:17 +0300
committerLinus Groh <mail@linusgroh.de>2021-06-09 11:48:04 +0100
commit2a3090d29290222bcd04203111cf3d63afce5451 (patch)
tree0f2f6ae52979c70c8ccb37e17ccbe29aa930b9c7 /Userland/Libraries/LibJS/Runtime/SetIterator.cpp
parent0b0f1eda059615bd8d16daa9261239463a1554ec (diff)
downloadserenity-2a3090d29290222bcd04203111cf3d63afce5451.zip
LibJS: Add the SetIterator built-in and Set.prototype.{values, entries}
While this implementation should be complete it is based on HashTable's iterator, which currently follows bucket-order instead of the required insertion order. This can be simply fixed by replacing the underlying HashTable member in Set with an enhanced one that maintains a linked list in insertion order.
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/SetIterator.cpp')
-rw-r--r--Userland/Libraries/LibJS/Runtime/SetIterator.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/SetIterator.cpp b/Userland/Libraries/LibJS/Runtime/SetIterator.cpp
new file mode 100644
index 0000000000..588e32877c
--- /dev/null
+++ b/Userland/Libraries/LibJS/Runtime/SetIterator.cpp
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibJS/Runtime/GlobalObject.h>
+#include <LibJS/Runtime/SetIterator.h>
+
+namespace JS {
+
+SetIterator* SetIterator::create(GlobalObject& global_object, Set& set, Object::PropertyKind iteration_kind)
+{
+ return global_object.heap().allocate<SetIterator>(global_object, *global_object.set_iterator_prototype(), set, iteration_kind);
+}
+
+SetIterator::SetIterator(Object& prototype, Set& set, Object::PropertyKind iteration_kind)
+ : Object(prototype)
+ , m_set(set)
+ , m_iteration_kind(iteration_kind)
+ , m_iterator(set.values().begin())
+{
+}
+
+SetIterator::~SetIterator()
+{
+}
+
+void SetIterator::visit_edges(Cell::Visitor& visitor)
+{
+ Base::visit_edges(visitor);
+ visitor.visit(&m_set);
+}
+
+}