| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-06 16:55:19 +00:00
										 |  |  | #include <AK/Format.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | #include <AK/Forward.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>
 | 
					
						
							| 
									
										
										
										
											2020-03-09 22:14:51 +01:00
										 |  |  | #include <LibJS/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class 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: | 
					
						
							| 
									
										
										
										
											2020-07-22 17:50:18 +02:00
										 |  |  |     virtual void initialize(GlobalObject&) { } | 
					
						
							| 
									
										
										
										
											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; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-25 18:35:27 +02:00
										 |  |  |     enum class State { | 
					
						
							|  |  |  |         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
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-03-09 22:14:51 +01:00
										 |  |  |         void visit(Value); | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  |     virtual bool is_environment() const { return false; } | 
					
						
							| 
									
										
										
										
											2020-11-28 14:33:36 +01:00
										 |  |  |     virtual void visit_edges(Visitor&) { } | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-16 00:19:41 +02:00
										 |  |  |     Heap& heap() const; | 
					
						
							| 
									
										
										
										
											2020-09-21 15:30:14 +02:00
										 |  |  |     VM& vm() const; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2021-05-25 18:35:27 +02:00
										 |  |  |     bool m_mark : 1 { false }; | 
					
						
							|  |  |  |     State m_state : 7 { 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
										 |  |  |     } | 
					
						
							|  |  |  | }; |