WeakMap.h 946 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/HashMap.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/Object.h>
  10. #include <LibJS/Runtime/WeakContainer.h>
  11. namespace JS {
  12. class WeakMap final
  13. : public Object
  14. , public WeakContainer {
  15. JS_OBJECT(WeakMap, Object);
  16. JS_DECLARE_ALLOCATOR(WeakMap);
  17. public:
  18. static NonnullGCPtr<WeakMap> create(Realm&);
  19. virtual ~WeakMap() override = default;
  20. HashMap<GCPtr<Cell>, Value> const& values() const { return m_values; }
  21. HashMap<GCPtr<Cell>, Value>& values() { return m_values; }
  22. virtual void remove_dead_cells(Badge<Heap>) override;
  23. private:
  24. explicit WeakMap(Object& prototype);
  25. void visit_edges(Visitor&) override;
  26. HashMap<GCPtr<Cell>, Value> m_values; // This stores Cell pointers instead of Object pointers to aide with sweeping
  27. };
  28. }