ladybird/Userland/Libraries/LibJS/Runtime/MapIterator.cpp
Idan Horowitz 322c8a3995 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.
2021-06-13 00:33:18 +01:00

35 lines
831 B
C++

/*
* 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);
}
}