Cell.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. enum class State {
  21. Live,
  22. Dead,
  23. };
  24. State state() const { return m_state; }
  25. void set_state(State state) { m_state = state; }
  26. virtual const char* class_name() const = 0;
  27. class Visitor {
  28. public:
  29. void visit(Cell* cell)
  30. {
  31. if (cell)
  32. visit_impl(*cell);
  33. }
  34. void visit(Value);
  35. protected:
  36. virtual void visit_impl(Cell&) = 0;
  37. virtual ~Visitor() = default;
  38. };
  39. virtual bool is_environment() const { return false; }
  40. virtual void visit_edges(Visitor&) { }
  41. Heap& heap() const;
  42. VM& vm() const;
  43. protected:
  44. Cell() { }
  45. private:
  46. bool m_mark : 1 { false };
  47. State m_state : 7 { State::Live };
  48. };
  49. }
  50. template<>
  51. struct AK::Formatter<JS::Cell> : AK::Formatter<FormatString> {
  52. void format(FormatBuilder& builder, const JS::Cell* cell)
  53. {
  54. if (!cell)
  55. Formatter<FormatString>::format(builder, "Cell{nullptr}");
  56. else
  57. Formatter<FormatString>::format(builder, "{}({})", cell->class_name(), cell);
  58. }
  59. };