ladybird/Userland/Libraries/LibJS/Runtime/WeakMap.cpp
Idan Horowitz b92871f7ef LibJS: Visit WeakMap's values as long as their keys were not collected
While the WeakMap only holds a weak reference to its keys, their
accompanying values should be kept alive as long as they're accessible.
2021-09-11 18:27:56 +02:00

39 lines
734 B
C++

/*
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/WeakMap.h>
namespace JS {
WeakMap* WeakMap::create(GlobalObject& global_object)
{
return global_object.heap().allocate<WeakMap>(global_object, *global_object.weak_map_prototype());
}
WeakMap::WeakMap(Object& prototype)
: Object(prototype)
, WeakContainer(heap())
{
}
WeakMap::~WeakMap()
{
}
void WeakMap::remove_swept_cells(Badge<Heap>, Span<Cell*> cells)
{
for (auto* cell : cells)
m_values.remove(cell);
}
void WeakMap::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
for (auto& entry : m_values)
visitor.visit(entry.value);
}
}