Cell.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Format.h>
  8. #include <AK/Forward.h>
  9. #include <AK/Noncopyable.h>
  10. #include <LibJS/Forward.h>
  11. namespace JS {
  12. class Cell {
  13. AK_MAKE_NONCOPYABLE(Cell);
  14. AK_MAKE_NONMOVABLE(Cell);
  15. public:
  16. virtual void initialize(GlobalObject&) { }
  17. virtual ~Cell() { }
  18. bool is_marked() const { return m_mark; }
  19. void set_marked(bool b) { m_mark = b; }
  20. #ifdef JS_TRACK_ZOMBIE_CELLS
  21. virtual void did_become_zombie()
  22. {
  23. }
  24. #endif
  25. enum class State {
  26. Live,
  27. Dead,
  28. #ifdef JS_TRACK_ZOMBIE_CELLS
  29. Zombie,
  30. #endif
  31. };
  32. State state() const { return m_state; }
  33. void set_state(State state) { m_state = state; }
  34. virtual const char* class_name() const = 0;
  35. class Visitor {
  36. public:
  37. void visit(Cell* cell)
  38. {
  39. if (cell)
  40. visit_impl(*cell);
  41. }
  42. void visit(Value);
  43. protected:
  44. virtual void visit_impl(Cell&) = 0;
  45. virtual ~Visitor() = default;
  46. };
  47. virtual bool is_environment() const { return false; }
  48. virtual void visit_edges(Visitor&) { }
  49. Heap& heap() const;
  50. VM& vm() const;
  51. protected:
  52. Cell() { }
  53. private:
  54. bool m_mark : 1 { false };
  55. State m_state : 7 { State::Live };
  56. };
  57. }
  58. template<>
  59. struct AK::Formatter<JS::Cell> : AK::Formatter<FormatString> {
  60. void format(FormatBuilder& builder, const JS::Cell* cell)
  61. {
  62. if (!cell)
  63. Formatter<FormatString>::format(builder, "Cell{nullptr}");
  64. else
  65. Formatter<FormatString>::format(builder, "{}({})", cell->class_name(), cell);
  66. }
  67. };