| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-05-17 21:25:57 +02:00
										 |  |  |  * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/HashMap.h>
 | 
					
						
							|  |  |  | #include <AK/OwnPtr.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-17 21:25:57 +02:00
										 |  |  | #include <AK/WeakPtr.h>
 | 
					
						
							|  |  |  | #include <AK/Weakable.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  | #include <LibJS/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-17 19:50:20 +02:00
										 |  |  | #include <LibJS/Heap/Cell.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-02 17:13:09 -07:00
										 |  |  | #include <LibJS/Runtime/PropertyAttributes.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  | #include <LibJS/Runtime/StringOrSymbol.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  | #include <LibJS/Runtime/Value.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct PropertyMetadata { | 
					
						
							|  |  |  |     size_t offset { 0 }; | 
					
						
							| 
									
										
										
										
											2020-06-02 17:13:09 -07:00
										 |  |  |     PropertyAttributes attributes { 0 }; | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-10 16:33:44 +02:00
										 |  |  | struct TransitionKey { | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     StringOrSymbol property_name; | 
					
						
							| 
									
										
										
										
											2020-06-02 17:13:09 -07:00
										 |  |  |     PropertyAttributes attributes { 0 }; | 
					
						
							| 
									
										
										
										
											2020-04-10 16:33:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool operator==(const TransitionKey& other) const | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-06-02 17:13:09 -07:00
										 |  |  |         return property_name == other.property_name && attributes == other.attributes; | 
					
						
							| 
									
										
										
										
											2020-04-10 16:33:44 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-17 21:25:57 +02:00
										 |  |  | class Shape final | 
					
						
							|  |  |  |     : public Cell | 
					
						
							|  |  |  |     , public Weakable<Shape> { | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  | public: | 
					
						
							|  |  |  |     virtual ~Shape() override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-09 22:55:17 +02:00
										 |  |  |     enum class TransitionType { | 
					
						
							|  |  |  |         Invalid, | 
					
						
							|  |  |  |         Put, | 
					
						
							|  |  |  |         Configure, | 
					
						
							|  |  |  |         Prototype, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-28 13:58:20 +01:00
										 |  |  |     enum class ShapeWithoutGlobalObjectTag { Tag }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     explicit Shape(ShapeWithoutGlobalObjectTag); | 
					
						
							| 
									
										
										
										
											2021-01-05 12:02:59 +01:00
										 |  |  |     explicit Shape(Object& global_object); | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     Shape(Shape& previous_shape, const StringOrSymbol& property_name, PropertyAttributes attributes, TransitionType); | 
					
						
							| 
									
										
										
										
											2020-06-08 12:15:58 +02:00
										 |  |  |     Shape(Shape& previous_shape, Object* new_prototype); | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     Shape* create_put_transition(const StringOrSymbol&, PropertyAttributes attributes); | 
					
						
							|  |  |  |     Shape* create_configure_transition(const StringOrSymbol&, PropertyAttributes attributes); | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  |     Shape* create_prototype_transition(Object* new_prototype); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-05 20:08:14 +02:00
										 |  |  |     void add_property_without_transition(const StringOrSymbol&, PropertyAttributes); | 
					
						
							| 
									
										
										
										
											2021-10-24 16:01:24 +02:00
										 |  |  |     void add_property_without_transition(PropertyKey const&, PropertyAttributes); | 
					
						
							| 
									
										
										
										
											2020-10-05 20:08:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-26 13:53:40 +02:00
										 |  |  |     bool is_unique() const { return m_unique; } | 
					
						
							|  |  |  |     Shape* create_unique_clone() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-05 12:02:59 +01:00
										 |  |  |     GlobalObject* global_object() const; | 
					
						
							| 
									
										
										
										
											2020-06-08 12:15:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  |     Object* prototype() { return m_prototype; } | 
					
						
							|  |  |  |     const Object* prototype() const { return m_prototype; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     Optional<PropertyMetadata> lookup(const StringOrSymbol&) const; | 
					
						
							|  |  |  |     const HashMap<StringOrSymbol, PropertyMetadata>& property_table() const; | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  |     size_t property_count() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-28 19:19:31 -07:00
										 |  |  |     struct Property { | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |         StringOrSymbol key; | 
					
						
							| 
									
										
										
										
											2020-04-28 19:19:31 -07:00
										 |  |  |         PropertyMetadata value; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Vector<Property> property_table_ordered() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  |     void set_prototype_without_transition(Object* new_prototype) { m_prototype = new_prototype; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     void remove_property_from_unique_shape(const StringOrSymbol&, size_t offset); | 
					
						
							|  |  |  |     void add_property_to_unique_shape(const StringOrSymbol&, PropertyAttributes attributes); | 
					
						
							|  |  |  |     void reconfigure_property_in_unique_shape(const StringOrSymbol& property_name, PropertyAttributes attributes); | 
					
						
							| 
									
										
										
										
											2020-04-26 13:53:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "Shape"; } | 
					
						
							| 
									
										
										
										
											2020-11-28 14:33:36 +01:00
										 |  |  |     virtual void visit_edges(Visitor&) override; | 
					
						
							| 
									
										
										
										
											2021-10-02 16:35:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef JS_TRACK_ZOMBIE_CELLS
 | 
					
						
							| 
									
										
										
										
											2021-09-11 17:01:45 +02:00
										 |  |  |     virtual void did_become_zombie() override; | 
					
						
							| 
									
										
										
										
											2021-10-02 16:35:55 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-17 21:25:57 +02:00
										 |  |  |     Shape* get_or_prune_cached_forward_transition(TransitionKey const&); | 
					
						
							| 
									
										
										
										
											2021-10-01 08:03:39 +03:30
										 |  |  |     Shape* get_or_prune_cached_prototype_transition(Object* prototype); | 
					
						
							| 
									
										
										
										
											2021-10-01 02:43:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  |     void ensure_property_table() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-16 15:24:17 +02:00
										 |  |  |     PropertyAttributes m_attributes { 0 }; | 
					
						
							|  |  |  |     TransitionType m_transition_type : 6 { TransitionType::Invalid }; | 
					
						
							|  |  |  |     bool m_unique : 1 { false }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-05 12:02:59 +01:00
										 |  |  |     Object* m_global_object { nullptr }; | 
					
						
							| 
									
										
										
										
											2020-06-08 12:15:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     mutable OwnPtr<HashMap<StringOrSymbol, PropertyMetadata>> m_property_table; | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-17 21:25:57 +02:00
										 |  |  |     HashMap<TransitionKey, WeakPtr<Shape>> m_forward_transitions; | 
					
						
							| 
									
										
										
										
											2021-10-01 02:43:57 +02:00
										 |  |  |     HashMap<Object*, WeakPtr<Shape>> m_prototype_transitions; | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  |     Shape* m_previous { nullptr }; | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     StringOrSymbol m_property_name; | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  |     Object* m_prototype { nullptr }; | 
					
						
							| 
									
										
										
										
											2020-10-04 18:02:28 +02:00
										 |  |  |     size_t m_property_count { 0 }; | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-04-10 16:33:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							|  |  |  | struct AK::Traits<JS::TransitionKey> : public GenericTraits<JS::TransitionKey> { | 
					
						
							|  |  |  |     static unsigned hash(const JS::TransitionKey& key) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |         return pair_int_hash(key.attributes.bits(), Traits<JS::StringOrSymbol>::hash(key.property_name)); | 
					
						
							| 
									
										
										
										
											2020-04-10 16:33:44 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | }; |