Cell.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Badge.h>
  8. #include <AK/Format.h>
  9. #include <AK/Forward.h>
  10. #include <AK/Noncopyable.h>
  11. #include <AK/StringView.h>
  12. #include <LibJS/Forward.h>
  13. #include <LibJS/Heap/GCPtr.h>
  14. #include <LibJS/Runtime/Completion.h>
  15. #include <LibJS/Runtime/Value.h>
  16. namespace JS {
  17. #define JS_CELL(class_, base_class) \
  18. public: \
  19. using Base = base_class; \
  20. virtual StringView class_name() const override \
  21. { \
  22. return #class_##sv; \
  23. } \
  24. friend class JS::Heap;
  25. class Cell {
  26. AK_MAKE_NONCOPYABLE(Cell);
  27. AK_MAKE_NONMOVABLE(Cell);
  28. public:
  29. virtual ThrowCompletionOr<void> initialize(Realm&) { return {}; }
  30. virtual ~Cell() = default;
  31. bool is_marked() const { return m_mark; }
  32. void set_marked(bool b) { m_mark = b; }
  33. enum class State {
  34. Live,
  35. Dead,
  36. };
  37. State state() const { return m_state; }
  38. void set_state(State state) { m_state = state; }
  39. virtual StringView class_name() const = 0;
  40. class Visitor {
  41. public:
  42. void visit(Cell* cell)
  43. {
  44. if (cell)
  45. visit_impl(*cell);
  46. }
  47. void visit(Cell& cell)
  48. {
  49. visit_impl(cell);
  50. }
  51. template<typename T>
  52. void visit(GCPtr<T> cell)
  53. {
  54. if (cell)
  55. visit_impl(const_cast<RemoveConst<T>&>(*cell.ptr()));
  56. }
  57. template<typename T>
  58. void visit(NonnullGCPtr<T> cell)
  59. {
  60. visit_impl(const_cast<RemoveConst<T>&>(*cell.ptr()));
  61. }
  62. void visit(Value value)
  63. {
  64. if (value.is_cell())
  65. visit_impl(value.as_cell());
  66. }
  67. protected:
  68. virtual void visit_impl(Cell&) = 0;
  69. virtual ~Visitor() = default;
  70. };
  71. virtual bool is_environment() const { return false; }
  72. virtual void visit_edges(Visitor&) { }
  73. // This will be called on unmarked objects by the garbage collector in a separate pass before destruction.
  74. virtual void finalize() { }
  75. // This allows cells to survive GC by choice, even if nothing points to them.
  76. // It's used to implement special rules in the web platform.
  77. // NOTE: Cells must call set_overrides_must_survive_garbage_collection() for this to be honored.
  78. virtual bool must_survive_garbage_collection() const { return false; }
  79. bool overrides_must_survive_garbage_collection(Badge<Heap>) const { return m_overrides_must_survive_garbage_collection; }
  80. Heap& heap() const;
  81. VM& vm() const;
  82. protected:
  83. Cell() = default;
  84. void set_overrides_must_survive_garbage_collection(bool b) { m_overrides_must_survive_garbage_collection = b; }
  85. private:
  86. bool m_mark : 1 { false };
  87. bool m_overrides_must_survive_garbage_collection : 1 { false };
  88. State m_state : 1 { State::Live };
  89. };
  90. }
  91. template<>
  92. struct AK::Formatter<JS::Cell> : AK::Formatter<FormatString> {
  93. ErrorOr<void> format(FormatBuilder& builder, JS::Cell const* cell)
  94. {
  95. if (!cell)
  96. return builder.put_string("Cell{nullptr}"sv);
  97. return Formatter<FormatString>::format(builder, "{}({})"sv, cell->class_name(), cell);
  98. }
  99. };