| 
									
										
										
										
											2021-06-15 22:16:17 +03:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-06-22 23:10:11 +03:00
										 |  |  |  * Copyright (c) 2021-2022, Idan Horowitz <idan.horowitz@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-06-15 22:16:17 +03:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/SinglyLinkedList.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-27 21:48:34 +02:00
										 |  |  | #include <LibJS/Runtime/FunctionObject.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-15 22:16:17 +03:00
										 |  |  | #include <LibJS/Runtime/GlobalObject.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-06 03:46:45 +00:00
										 |  |  | #include <LibJS/Runtime/JobCallback.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-15 22:16:17 +03:00
										 |  |  | #include <LibJS/Runtime/Object.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/Value.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/WeakContainer.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FinalizationRegistry final | 
					
						
							|  |  |  |     : public Object | 
					
						
							|  |  |  |     , public WeakContainer { | 
					
						
							|  |  |  |     JS_OBJECT(FinalizationRegistry, Object); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2022-04-03 15:19:33 +01:00
										 |  |  |     explicit FinalizationRegistry(Realm&, JobCallback, Object& prototype); | 
					
						
							| 
									
										
										
										
											2022-03-14 10:25:06 -06:00
										 |  |  |     virtual ~FinalizationRegistry() override = default; | 
					
						
							| 
									
										
										
										
											2021-06-15 22:16:17 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-22 23:10:11 +03:00
										 |  |  |     void add_finalization_record(Cell& target, Value held_value, Cell* unregister_token); | 
					
						
							|  |  |  |     bool remove_by_token(Cell& unregister_token); | 
					
						
							| 
									
										
										
										
											2022-02-06 03:46:45 +00:00
										 |  |  |     ThrowCompletionOr<void> cleanup(Optional<JobCallback> = {}); | 
					
						
							| 
									
										
										
										
											2021-06-15 22:16:17 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 18:44:31 +02:00
										 |  |  |     virtual void remove_dead_cells(Badge<Heap>) override; | 
					
						
							| 
									
										
										
										
											2021-06-15 22:16:17 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-06 03:46:45 +00:00
										 |  |  |     Realm& realm() { return *m_realm.cell(); } | 
					
						
							|  |  |  |     Realm const& realm() const { return *m_realm.cell(); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     JobCallback& cleanup_callback() { return m_cleanup_callback; } | 
					
						
							|  |  |  |     JobCallback const& cleanup_callback() const { return m_cleanup_callback; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-15 22:16:17 +03:00
										 |  |  | private: | 
					
						
							|  |  |  |     virtual void visit_edges(Visitor& visitor) override; | 
					
						
							| 
									
										
										
										
											2021-10-02 16:35:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-06 03:46:45 +00:00
										 |  |  |     Handle<Realm> m_realm; | 
					
						
							| 
									
										
										
										
											2022-04-03 15:19:33 +01:00
										 |  |  |     JobCallback m_cleanup_callback; | 
					
						
							| 
									
										
										
										
											2021-06-15 22:16:17 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     struct FinalizationRecord { | 
					
						
							|  |  |  |         Cell* target { nullptr }; | 
					
						
							|  |  |  |         Value held_value; | 
					
						
							| 
									
										
										
										
											2022-06-22 23:10:11 +03:00
										 |  |  |         Cell* unregister_token { nullptr }; | 
					
						
							| 
									
										
										
										
											2021-06-15 22:16:17 +03:00
										 |  |  |     }; | 
					
						
							|  |  |  |     SinglyLinkedList<FinalizationRecord> m_records; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |