2021-06-12 20:54:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibJS/Runtime/Map.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC_DEFINE_ALLOCATOR(Map);
|
2023-11-19 08:45:05 +00:00
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ref<Map> Map::create(Realm& realm)
|
2021-06-12 20:54:40 +00:00
|
|
|
{
|
2024-11-13 16:50:17 +00:00
|
|
|
return realm.create<Map>(realm.intrinsics().map_prototype());
|
2021-06-12 20:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Map::Map(Object& prototype)
|
2022-12-14 11:17:58 +00:00
|
|
|
: Object(ConstructWithPrototypeTag::Tag, prototype)
|
2021-06-12 20:54:40 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-02-09 13:04:52 +00:00
|
|
|
// 24.1.3.1 Map.prototype.clear ( ), https://tc39.es/ecma262/#sec-map.prototype.clear
|
|
|
|
void Map::map_clear()
|
|
|
|
{
|
|
|
|
m_keys.clear();
|
|
|
|
m_entries.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 24.1.3.3 Map.prototype.delete ( key ), https://tc39.es/ecma262/#sec-map.prototype.delete
|
|
|
|
bool Map::map_remove(Value const& key)
|
|
|
|
{
|
|
|
|
Optional<size_t> index;
|
|
|
|
|
2022-02-10 10:13:53 +00:00
|
|
|
for (auto it = m_keys.begin(); !it.is_end(); ++it) {
|
2022-02-09 13:04:52 +00:00
|
|
|
if (ValueTraits::equals(*it, key)) {
|
|
|
|
index = it.key();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!index.has_value())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_keys.remove(*index);
|
|
|
|
m_entries.remove(key);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 24.1.3.6 Map.prototype.get ( key ), https://tc39.es/ecma262/#sec-map.prototype.get
|
|
|
|
Optional<Value> Map::map_get(Value const& key) const
|
|
|
|
{
|
|
|
|
if (auto it = m_entries.find(key); it != m_entries.end())
|
|
|
|
return it->value;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// 24.1.3.7 Map.prototype.has ( key ), https://tc39.es/ecma262/#sec-map.prototype.has
|
|
|
|
bool Map::map_has(Value const& key) const
|
|
|
|
{
|
|
|
|
return m_entries.contains(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 24.1.3.9 Map.prototype.set ( key, value ), https://tc39.es/ecma262/#sec-map.prototype.set
|
|
|
|
void Map::map_set(Value const& key, Value value)
|
|
|
|
{
|
|
|
|
auto it = m_entries.find(key);
|
|
|
|
if (it != m_entries.end()) {
|
|
|
|
it->value = value;
|
|
|
|
} else {
|
|
|
|
auto index = m_next_insertion_id++;
|
|
|
|
m_keys.insert(index, key);
|
|
|
|
m_entries.set(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Map::map_size() const
|
|
|
|
{
|
|
|
|
return m_keys.size();
|
|
|
|
}
|
|
|
|
|
2021-06-12 20:54:40 +00:00
|
|
|
void Map::visit_edges(Cell::Visitor& visitor)
|
|
|
|
{
|
2021-09-11 12:05:12 +00:00
|
|
|
Base::visit_edges(visitor);
|
2021-06-12 20:54:40 +00:00
|
|
|
for (auto& value : m_entries) {
|
|
|
|
visitor.visit(value.key);
|
|
|
|
visitor.visit(value.value);
|
|
|
|
}
|
2024-04-07 06:52:46 +00:00
|
|
|
// NOTE: The entries in m_keys are already visited by the walk over m_entries above.
|
|
|
|
visitor.ignore(m_keys);
|
2021-06-12 20:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|