Cell.h 1.6 KB

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