mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 22:07: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
35
Userland/Libraries/LibJS/Runtime/MapIterator.cpp
Normal file
35
Userland/Libraries/LibJS/Runtime/MapIterator.cpp
Normal file
|
@ -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/MapIterator.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
MapIterator* MapIterator::create(GlobalObject& global_object, Map& map, Object::PropertyKind iteration_kind)
|
||||
{
|
||||
return global_object.heap().allocate<MapIterator>(global_object, *global_object.map_iterator_prototype(), map, iteration_kind);
|
||||
}
|
||||
|
||||
MapIterator::MapIterator(Object& prototype, Map& map, Object::PropertyKind iteration_kind)
|
||||
: Object(prototype)
|
||||
, m_map(map)
|
||||
, m_iteration_kind(iteration_kind)
|
||||
, m_iterator(map.entries().begin())
|
||||
{
|
||||
}
|
||||
|
||||
MapIterator::~MapIterator()
|
||||
{
|
||||
}
|
||||
|
||||
void MapIterator::visit_edges(Cell::Visitor& visitor)
|
||||
{
|
||||
Base::visit_edges(visitor);
|
||||
visitor.visit(&m_map);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue