WeakRef.h 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * Copyright (c) 2021-2022, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Object.h>
  9. #include <LibJS/Runtime/WeakContainer.h>
  10. namespace JS {
  11. class WeakRef final
  12. : public Object
  13. , public WeakContainer {
  14. JS_OBJECT(WeakRef, Object);
  15. JS_DECLARE_ALLOCATOR(WeakRef);
  16. public:
  17. static NonnullGCPtr<WeakRef> create(Realm&, Object&);
  18. static NonnullGCPtr<WeakRef> create(Realm&, Symbol&);
  19. virtual ~WeakRef() override = default;
  20. auto const& value() const { return m_value; }
  21. void update_execution_generation() { m_last_execution_generation = vm().execution_generation(); }
  22. virtual void remove_dead_cells(Badge<Heap>) override;
  23. private:
  24. explicit WeakRef(Object&, Object& prototype);
  25. explicit WeakRef(Symbol&, Object& prototype);
  26. virtual void visit_edges(Visitor&) override;
  27. Variant<GCPtr<Object>, GCPtr<Symbol>, Empty> m_value;
  28. u32 m_last_execution_generation { 0 };
  29. };
  30. }