mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 02:47:35 +00:00
LibJS: Add the MapIterator built-in and the key/values/entries methods
While this implementation should be complete it is based on HashMap's iterator, which currently follows bucket-order instead of the required insertion order. This can be simply fixed by replacing the underlying HashMap member in Map with an enhanced one that maintains a linked list in insertion order.
This commit is contained in:
parent
6c0d5163a1
commit
322c8a3995
12 changed files with 286 additions and 0 deletions
39
Userland/Libraries/LibJS/Runtime/MapIterator.h
Normal file
39
Userland/Libraries/LibJS/Runtime/MapIterator.h
Normal file
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/HashMap.h>
|
||||
#include <LibJS/Runtime/Map.h>
|
||||
#include <LibJS/Runtime/Object.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
class MapIterator final : public Object {
|
||||
JS_OBJECT(MapIterator, Object);
|
||||
|
||||
public:
|
||||
static MapIterator* create(GlobalObject&, Map& map, Object::PropertyKind iteration_kind);
|
||||
|
||||
explicit MapIterator(Object& prototype, Map& map, Object::PropertyKind iteration_kind);
|
||||
virtual ~MapIterator() override;
|
||||
|
||||
Map& map() const { return m_map; }
|
||||
bool done() const { return m_done; }
|
||||
Object::PropertyKind iteration_kind() const { return m_iteration_kind; }
|
||||
|
||||
private:
|
||||
friend class MapIteratorPrototype;
|
||||
|
||||
virtual void visit_edges(Cell::Visitor&) override;
|
||||
|
||||
Map& m_map;
|
||||
bool m_done { false };
|
||||
Object::PropertyKind m_iteration_kind;
|
||||
HashMap<Value, Value, ValueTraits>::IteratorType m_iterator;
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue