| 
									
										
										
										
											2021-06-15 22:16:17 +03:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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>
 | 
					
						
							|  |  |  | #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: | 
					
						
							| 
									
										
										
										
											2021-06-27 21:48:34 +02:00
										 |  |  |     static FinalizationRegistry* create(GlobalObject&, FunctionObject&); | 
					
						
							| 
									
										
										
										
											2021-06-15 22:16:17 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-27 21:48:34 +02:00
										 |  |  |     explicit FinalizationRegistry(FunctionObject&, Object& prototype); | 
					
						
							| 
									
										
										
										
											2021-06-15 22:16:17 +03:00
										 |  |  |     virtual ~FinalizationRegistry() override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void add_finalization_record(Cell& target, Value held_value, Object* unregister_token); | 
					
						
							|  |  |  |     bool remove_by_token(Object& unregister_token); | 
					
						
							| 
									
										
										
										
											2021-06-27 21:48:34 +02:00
										 |  |  |     void cleanup(FunctionObject* callback = nullptr); | 
					
						
							| 
									
										
										
										
											2021-06-15 22:16:17 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     virtual void remove_sweeped_cells(Badge<Heap>, Vector<Cell*>&) override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual void visit_edges(Visitor& visitor) override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-27 21:48:34 +02:00
										 |  |  |     FunctionObject* m_cleanup_callback { nullptr }; | 
					
						
							| 
									
										
										
										
											2021-06-15 22:16:17 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     struct FinalizationRecord { | 
					
						
							|  |  |  |         Cell* target { nullptr }; | 
					
						
							|  |  |  |         Value held_value; | 
					
						
							|  |  |  |         Object* unregister_token { nullptr }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     SinglyLinkedList<FinalizationRecord> m_records; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |