| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-04-15 13:57:57 +02:00
										 |  |  |  * Copyright (c) 2020-2024, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-24 12:37:54 +02:00
										 |  |  | #include <AK/Badge.h>
 | 
					
						
							| 
									
										
										
										
											2020-12-06 16:55:19 +00:00
										 |  |  | #include <AK/Format.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | #include <AK/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2024-04-15 13:57:57 +02:00
										 |  |  | #include <AK/HashMap.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-02 14:55:29 +02:00
										 |  |  | #include <AK/Noncopyable.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-16 18:26:49 -06:00
										 |  |  | #include <AK/StringView.h>
 | 
					
						
							| 
									
										
										
										
											2024-05-02 10:24:23 +02:00
										 |  |  | #include <AK/Weakable.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-09 22:14:51 +01:00
										 |  |  | #include <LibJS/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2022-10-02 11:40:34 +01:00
										 |  |  | #include <LibJS/Heap/GCPtr.h>
 | 
					
						
							| 
									
										
										
										
											2023-07-09 20:05:02 +03:30
										 |  |  | #include <LibJS/Heap/Internals.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 22:11:20 +02:00
										 |  |  | #define JS_CELL(class_, base_class)                \
 | 
					
						
							|  |  |  | public:                                            \ | 
					
						
							|  |  |  |     using Base = base_class;                       \ | 
					
						
							|  |  |  |     virtual StringView class_name() const override \ | 
					
						
							|  |  |  |     {                                              \ | 
					
						
							|  |  |  |         return #class_##sv;                        \ | 
					
						
							| 
									
										
										
										
											2022-08-28 22:13:05 +02:00
										 |  |  |     }                                              \ | 
					
						
							|  |  |  |     friend class JS::Heap; | 
					
						
							| 
									
										
										
										
											2022-08-28 22:11:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-02 10:24:23 +02:00
										 |  |  | class Cell : public Weakable<Cell> { | 
					
						
							| 
									
										
										
										
											2020-04-02 14:55:29 +02:00
										 |  |  |     AK_MAKE_NONCOPYABLE(Cell); | 
					
						
							|  |  |  |     AK_MAKE_NONMOVABLE(Cell); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |     virtual void initialize(Realm&); | 
					
						
							| 
									
										
										
										
											2022-03-14 10:25:06 -06:00
										 |  |  |     virtual ~Cell() = default; | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool is_marked() const { return m_mark; } | 
					
						
							|  |  |  |     void set_marked(bool b) { m_mark = b; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-03 23:59:15 -04:00
										 |  |  |     enum class State : bool { | 
					
						
							| 
									
										
										
										
											2021-05-25 18:35:27 +02:00
										 |  |  |         Live, | 
					
						
							|  |  |  |         Dead, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     State state() const { return m_state; } | 
					
						
							|  |  |  |     void set_state(State state) { m_state = state; } | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-16 18:26:49 -06:00
										 |  |  |     virtual StringView class_name() const = 0; | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     class Visitor { | 
					
						
							|  |  |  |     public: | 
					
						
							| 
									
										
										
										
											2021-05-25 18:17:41 +02:00
										 |  |  |         void visit(Cell* cell) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (cell) | 
					
						
							| 
									
										
										
										
											2021-05-25 18:39:01 +02:00
										 |  |  |                 visit_impl(*cell); | 
					
						
							| 
									
										
										
										
											2021-05-25 18:17:41 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-01-28 11:26:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-09 12:35:38 +02:00
										 |  |  |         void visit(Cell& cell) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             visit_impl(cell); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-01-28 11:26:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-02 11:40:34 +01:00
										 |  |  |         template<typename T> | 
					
						
							|  |  |  |         void visit(GCPtr<T> cell) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (cell) | 
					
						
							| 
									
										
										
										
											2023-02-25 10:44:06 -07:00
										 |  |  |                 visit_impl(const_cast<RemoveConst<T>&>(*cell.ptr())); | 
					
						
							| 
									
										
										
										
											2022-10-02 11:40:34 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-01-28 11:26:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-02 11:40:34 +01:00
										 |  |  |         template<typename T> | 
					
						
							|  |  |  |         void visit(NonnullGCPtr<T> cell) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2023-02-25 10:44:06 -07:00
										 |  |  |             visit_impl(const_cast<RemoveConst<T>&>(*cell.ptr())); | 
					
						
							| 
									
										
										
										
											2022-10-02 11:40:34 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-01-28 11:26:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-15 13:57:57 +02:00
										 |  |  |         template<typename T> | 
					
						
							|  |  |  |         void visit(ReadonlySpan<T> span) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             for (auto& value : span) | 
					
						
							|  |  |  |                 visit(value); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         template<typename T> | 
					
						
							|  |  |  |         void visit(Span<T> span) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             for (auto& value : span) | 
					
						
							|  |  |  |                 visit(value); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         template<typename T> | 
					
						
							|  |  |  |         void visit(Vector<T> const& vector) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             for (auto& value : vector) | 
					
						
							|  |  |  |                 visit(value); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         template<typename T> | 
					
						
							|  |  |  |         void visit(HashTable<T> const& table) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             for (auto& value : table) | 
					
						
							|  |  |  |                 visit(value); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         template<typename T> | 
					
						
							|  |  |  |         void visit(OrderedHashTable<T> const& table) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             for (auto& value : table) | 
					
						
							|  |  |  |                 visit(value); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         template<typename K, typename V, typename T> | 
					
						
							|  |  |  |         void visit(HashMap<K, V, T> const& map) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             for (auto& it : map) { | 
					
						
							|  |  |  |                 if constexpr (requires { visit(it.key); }) | 
					
						
							|  |  |  |                     visit(it.key); | 
					
						
							|  |  |  |                 if constexpr (requires { visit(it.value); }) | 
					
						
							|  |  |  |                     visit(it.value); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         template<typename K, typename V, typename T> | 
					
						
							|  |  |  |         void visit(OrderedHashMap<K, V, T> const& map) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             for (auto& it : map) { | 
					
						
							|  |  |  |                 if constexpr (requires { visit(it.key); }) | 
					
						
							|  |  |  |                     visit(it.key); | 
					
						
							|  |  |  |                 if constexpr (requires { visit(it.value); }) | 
					
						
							|  |  |  |                     visit(it.value); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-09 11:40:17 +03:30
										 |  |  |         void visit(Value value); | 
					
						
							| 
									
										
										
										
											2020-04-16 16:07:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 17:05:16 -07:00
										 |  |  |         // Allow explicitly ignoring a GC-allocated member in a visit_edges implementation instead
 | 
					
						
							|  |  |  |         // of just not using it.
 | 
					
						
							|  |  |  |         template<typename T> | 
					
						
							|  |  |  |         void ignore(T const&) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-18 18:21:33 +02:00
										 |  |  |         virtual void visit_possible_values(ReadonlyBytes) = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-16 16:07:50 +02:00
										 |  |  |     protected: | 
					
						
							| 
									
										
										
										
											2021-05-25 18:39:01 +02:00
										 |  |  |         virtual void visit_impl(Cell&) = 0; | 
					
						
							| 
									
										
										
										
											2021-04-15 10:43:29 -07:00
										 |  |  |         virtual ~Visitor() = default; | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-28 14:33:36 +01:00
										 |  |  |     virtual void visit_edges(Visitor&) { } | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-20 18:35:19 +02:00
										 |  |  |     // This will be called on unmarked objects by the garbage collector in a separate pass before destruction.
 | 
					
						
							|  |  |  |     virtual void finalize() { } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-24 12:37:54 +02:00
										 |  |  |     // This allows cells to survive GC by choice, even if nothing points to them.
 | 
					
						
							|  |  |  |     // It's used to implement special rules in the web platform.
 | 
					
						
							|  |  |  |     // NOTE: Cells must call set_overrides_must_survive_garbage_collection() for this to be honored.
 | 
					
						
							|  |  |  |     virtual bool must_survive_garbage_collection() const { return false; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool overrides_must_survive_garbage_collection(Badge<Heap>) const { return m_overrides_must_survive_garbage_collection; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-09 20:05:02 +03:30
										 |  |  |     ALWAYS_INLINE Heap& heap() const { return HeapBlockBase::from_cell(this)->heap(); } | 
					
						
							|  |  |  |     ALWAYS_INLINE VM& vm() const { return bit_cast<HeapBase*>(&heap())->vm(); } | 
					
						
							| 
									
										
										
										
											2020-03-13 11:01:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 14:55:29 +02:00
										 |  |  | protected: | 
					
						
							| 
									
										
										
										
											2022-03-14 10:25:06 -06:00
										 |  |  |     Cell() = default; | 
					
						
							| 
									
										
										
										
											2020-04-02 14:55:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-24 12:37:54 +02:00
										 |  |  |     void set_overrides_must_survive_garbage_collection(bool b) { m_overrides_must_survive_garbage_collection = b; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2021-05-25 18:35:27 +02:00
										 |  |  |     bool m_mark : 1 { false }; | 
					
						
							| 
									
										
										
										
											2022-10-24 19:37:36 +02:00
										 |  |  |     bool m_overrides_must_survive_garbage_collection : 1 { false }; | 
					
						
							| 
									
										
										
										
											2022-10-24 12:37:54 +02:00
										 |  |  |     State m_state : 1 { State::Live }; | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-06 16:55:19 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							| 
									
										
										
										
											2021-01-09 01:00:22 +01:00
										 |  |  | struct AK::Formatter<JS::Cell> : AK::Formatter<FormatString> { | 
					
						
							| 
									
										
										
										
											2021-11-16 01:15:21 +01:00
										 |  |  |     ErrorOr<void> format(FormatBuilder& builder, JS::Cell const* cell) | 
					
						
							| 
									
										
										
										
											2020-12-06 16:55:19 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (!cell) | 
					
						
							| 
									
										
										
										
											2022-07-11 20:23:24 +00:00
										 |  |  |             return builder.put_string("Cell{nullptr}"sv); | 
					
						
							|  |  |  |         return Formatter<FormatString>::format(builder, "{}({})"sv, cell->class_name(), cell); | 
					
						
							| 
									
										
										
										
											2020-12-06 16:55:19 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | }; |