| 
									
										
										
										
											2021-06-12 17:38:34 +03:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/WeakRef.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | WeakRef* WeakRef::create(GlobalObject& global_object, Object* object) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-06-19 23:21:08 +01:00
										 |  |  |     return global_object.heap().allocate<WeakRef>(global_object, object, *global_object.weak_ref_prototype()); | 
					
						
							| 
									
										
										
										
											2021-06-12 17:38:34 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-19 23:21:08 +01:00
										 |  |  | WeakRef::WeakRef(Object* object, Object& prototype) | 
					
						
							| 
									
										
										
										
											2021-06-12 17:38:34 +03:00
										 |  |  |     : Object(prototype) | 
					
						
							|  |  |  |     , WeakContainer(heap()) | 
					
						
							|  |  |  |     , m_value(object) | 
					
						
							|  |  |  |     , m_last_execution_generation(vm().execution_generation()) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 18:44:31 +02:00
										 |  |  | void WeakRef::remove_dead_cells(Badge<Heap>) | 
					
						
							| 
									
										
										
										
											2021-06-12 17:38:34 +03:00
										 |  |  | { | 
					
						
							|  |  |  |     VERIFY(m_value); | 
					
						
							| 
									
										
										
										
											2021-10-05 18:44:31 +02:00
										 |  |  |     if (m_value->state() == Cell::State::Live) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_value = nullptr; | 
					
						
							|  |  |  |     // This is an optimization, we deregister from the garbage collector early (even if we were not garbage collected ourself yet)
 | 
					
						
							|  |  |  |     // to reduce the garbage collection overhead, which we can do because a cleared weak ref cannot be reused.
 | 
					
						
							|  |  |  |     WeakContainer::deregister(); | 
					
						
							| 
									
										
										
										
											2021-06-12 17:38:34 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void WeakRef::visit_edges(Visitor& visitor) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-09-11 14:05:12 +02:00
										 |  |  |     Base::visit_edges(visitor); | 
					
						
							| 
									
										
										
										
											2021-06-12 17:38:34 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (vm().execution_generation() == m_last_execution_generation) | 
					
						
							|  |  |  |         visitor.visit(m_value); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |