Cell.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 <AK/String.h>
  11. #include <AK/TypeCasts.h>
  12. #include <LibJS/Forward.h>
  13. namespace JS {
  14. class Cell {
  15. AK_MAKE_NONCOPYABLE(Cell);
  16. AK_MAKE_NONMOVABLE(Cell);
  17. public:
  18. virtual void initialize(GlobalObject&) { }
  19. virtual ~Cell() { }
  20. bool is_marked() const { return m_mark; }
  21. void set_marked(bool b) { m_mark = b; }
  22. enum class State {
  23. Live,
  24. Dead,
  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. };