WeakRef.h 1022 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. public:
  16. static NonnullGCPtr<WeakRef> create(Realm&, Object&);
  17. static NonnullGCPtr<WeakRef> create(Realm&, Symbol&);
  18. virtual ~WeakRef() override = default;
  19. auto const& value() const { return m_value; }
  20. void update_execution_generation() { m_last_execution_generation = vm().execution_generation(); }
  21. virtual void remove_dead_cells(Badge<Heap>) override;
  22. private:
  23. explicit WeakRef(Object&, Object& prototype);
  24. explicit WeakRef(Symbol&, Object& prototype);
  25. virtual void visit_edges(Visitor&) override;
  26. Variant<GCPtr<Object>, GCPtr<Symbol>, Empty> m_value;
  27. u32 m_last_execution_generation { 0 };
  28. };
  29. }