WeakMap.cpp 792 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/WeakMap.h>
  7. namespace JS {
  8. JS_DEFINE_ALLOCATOR(WeakMap);
  9. NonnullGCPtr<WeakMap> WeakMap::create(Realm& realm)
  10. {
  11. return realm.heap().allocate<WeakMap>(realm, realm.intrinsics().weak_map_prototype());
  12. }
  13. WeakMap::WeakMap(Object& prototype)
  14. : Object(ConstructWithPrototypeTag::Tag, prototype)
  15. , WeakContainer(heap())
  16. {
  17. }
  18. void WeakMap::remove_dead_cells(Badge<Heap>)
  19. {
  20. m_values.remove_all_matching([](Cell* key, Value) {
  21. return key->state() != Cell::State::Live;
  22. });
  23. }
  24. void WeakMap::visit_edges(Visitor& visitor)
  25. {
  26. Base::visit_edges(visitor);
  27. for (auto& entry : m_values)
  28. visitor.visit(entry.value);
  29. }
  30. }