Cell.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/StringView.h>
  11. #include <LibJS/Forward.h>
  12. namespace JS {
  13. class Cell {
  14. AK_MAKE_NONCOPYABLE(Cell);
  15. AK_MAKE_NONMOVABLE(Cell);
  16. public:
  17. virtual void initialize(GlobalObject&) { }
  18. virtual ~Cell() = default;
  19. bool is_marked() const { return m_mark; }
  20. void set_marked(bool b) { m_mark = b; }
  21. enum class State {
  22. Live,
  23. Dead,
  24. };
  25. State state() const { return m_state; }
  26. void set_state(State state) { m_state = state; }
  27. virtual StringView class_name() const = 0;
  28. class Visitor {
  29. public:
  30. void visit(Cell* cell)
  31. {
  32. if (cell)
  33. visit_impl(*cell);
  34. }
  35. void visit(Value);
  36. protected:
  37. virtual void visit_impl(Cell&) = 0;
  38. virtual ~Visitor() = default;
  39. };
  40. virtual bool is_environment() const { return false; }
  41. virtual void visit_edges(Visitor&) { }
  42. Heap& heap() const;
  43. VM& vm() const;
  44. protected:
  45. Cell() = default;
  46. private:
  47. bool m_mark : 1 { false };
  48. State m_state : 7 { State::Live };
  49. };
  50. }
  51. template<>
  52. struct AK::Formatter<JS::Cell> : AK::Formatter<FormatString> {
  53. ErrorOr<void> format(FormatBuilder& builder, JS::Cell const* cell)
  54. {
  55. if (!cell)
  56. return Formatter<FormatString>::format(builder, "Cell{nullptr}");
  57. else
  58. return Formatter<FormatString>::format(builder, "{}({})", cell->class_name(), cell);
  59. }
  60. };