| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2020-2024, Andreas Kling <andreas@ladybird.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>
 | 
					
						
							| 
									
										
										
										
											2022-03-16 18:26:49 -06:00
										 |  |  | #include <AK/StringView.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>
 | 
					
						
							| 
									
										
										
										
											2025-05-15 17:14:00 +03:00
										 |  |  | #include <LibJS/Runtime/PropertyKey.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  | #include <LibJS/Runtime/Value.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct PropertyMetadata { | 
					
						
							| 
									
										
										
										
											2022-01-31 15:55:54 +01:00
										 |  |  |     u32 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 { | 
					
						
							| 
									
										
										
										
											2025-05-15 17:14:00 +03:00
										 |  |  |     PropertyKey property_key; | 
					
						
							| 
									
										
										
										
											2020-06-02 17:13:09 -07:00
										 |  |  |     PropertyAttributes attributes { 0 }; | 
					
						
							| 
									
										
										
										
											2020-04-10 16:33:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     bool operator==(TransitionKey const& other) const | 
					
						
							| 
									
										
										
										
											2020-04-10 16:33:44 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-02-06 15:59:04 +00:00
										 |  |  |         return property_key == other.property_key && attributes == other.attributes; | 
					
						
							| 
									
										
										
										
											2020-04-10 16:33:44 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-28 21:39:13 -07:00
										 |  |  | class JS_API PrototypeChainValidity final : public Cell | 
					
						
							| 
									
										
										
										
											2025-03-28 20:09:28 +00:00
										 |  |  |     , public Weakable<PrototypeChainValidity> { | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC_CELL(PrototypeChainValidity, Cell); | 
					
						
							|  |  |  |     GC_DECLARE_ALLOCATOR(PrototypeChainValidity); | 
					
						
							| 
									
										
										
										
											2024-05-04 15:48:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     [[nodiscard]] bool is_valid() const { return m_valid; } | 
					
						
							|  |  |  |     void set_valid(bool valid) { m_valid = valid; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     bool m_valid { true }; | 
					
						
							|  |  |  |     size_t padding { 0 }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-28 21:39:13 -07:00
										 |  |  | class JS_API Shape final : public Cell | 
					
						
							| 
									
										
										
										
											2025-03-28 20:09:28 +00:00
										 |  |  |     , public Weakable<Shape> { | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC_CELL(Shape, Cell); | 
					
						
							|  |  |  |     GC_DECLARE_ALLOCATOR(Shape); | 
					
						
							| 
									
										
										
										
											2022-08-28 22:11:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2024-05-04 15:48:23 +02:00
										 |  |  |     virtual ~Shape() override; | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-12 15:26:46 +01:00
										 |  |  |     enum class TransitionType : u8 { | 
					
						
							| 
									
										
										
										
											2020-04-09 22:55:17 +02:00
										 |  |  |         Invalid, | 
					
						
							|  |  |  |         Put, | 
					
						
							|  |  |  |         Configure, | 
					
						
							|  |  |  |         Prototype, | 
					
						
							| 
									
										
										
										
											2023-12-11 14:29:40 +01:00
										 |  |  |         Delete, | 
					
						
							| 
									
										
										
										
											2023-12-16 11:34:01 +01:00
										 |  |  |         CacheableDictionary, | 
					
						
							|  |  |  |         UncacheableDictionary, | 
					
						
							| 
									
										
										
										
											2020-04-09 22:55:17 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-15 17:14:00 +03:00
										 |  |  |     [[nodiscard]] GC::Ref<Shape> create_put_transition(PropertyKey const&, PropertyAttributes attributes); | 
					
						
							|  |  |  |     [[nodiscard]] GC::Ref<Shape> create_configure_transition(PropertyKey const&, PropertyAttributes attributes); | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     [[nodiscard]] GC::Ref<Shape> create_prototype_transition(Object* new_prototype); | 
					
						
							| 
									
										
										
										
											2025-05-15 17:14:00 +03:00
										 |  |  |     [[nodiscard]] GC::Ref<Shape> create_delete_transition(PropertyKey const&); | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     [[nodiscard]] GC::Ref<Shape> create_cacheable_dictionary_transition(); | 
					
						
							|  |  |  |     [[nodiscard]] GC::Ref<Shape> create_uncacheable_dictionary_transition(); | 
					
						
							|  |  |  |     [[nodiscard]] GC::Ref<Shape> clone_for_prototype(); | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-24 16:01:24 +02:00
										 |  |  |     void add_property_without_transition(PropertyKey const&, PropertyAttributes); | 
					
						
							| 
									
										
										
										
											2020-10-05 20:08:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-15 17:14:00 +03:00
										 |  |  |     void remove_property_without_transition(PropertyKey const&, u32 offset); | 
					
						
							|  |  |  |     void set_property_attributes_without_transition(PropertyKey const&, PropertyAttributes); | 
					
						
							| 
									
										
										
										
											2023-12-16 11:34:01 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     [[nodiscard]] bool is_cacheable() const { return m_cacheable; } | 
					
						
							|  |  |  |     [[nodiscard]] bool is_dictionary() const { return m_dictionary; } | 
					
						
							|  |  |  |     [[nodiscard]] bool is_cacheable_dictionary() const { return m_dictionary && m_cacheable; } | 
					
						
							|  |  |  |     [[nodiscard]] bool is_uncacheable_dictionary() const { return m_dictionary && !m_cacheable; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-04 15:48:23 +02:00
										 |  |  |     [[nodiscard]] bool is_prototype_shape() const { return m_is_prototype_shape; } | 
					
						
							|  |  |  |     void set_prototype_shape(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC::Ptr<PrototypeChainValidity> prototype_chain_validity() const { return m_prototype_chain_validity; } | 
					
						
							| 
									
										
										
										
											2024-05-04 15:48:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-01 20:27:20 +02:00
										 |  |  |     Realm& realm() const { return m_realm; } | 
					
						
							| 
									
										
										
										
											2020-06-08 12:15:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  |     Object* prototype() { return m_prototype; } | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     Object const* prototype() const { return m_prototype; } | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-15 17:14:00 +03:00
										 |  |  |     Optional<PropertyMetadata> lookup(PropertyKey const&) const; | 
					
						
							|  |  |  |     OrderedHashMap<PropertyKey, PropertyMetadata> const& property_table() const; | 
					
						
							| 
									
										
										
										
											2022-01-31 15:55:54 +01:00
										 |  |  |     u32 property_count() const { return m_property_count; } | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-28 19:19:31 -07:00
										 |  |  |     struct Property { | 
					
						
							| 
									
										
										
										
											2025-05-15 17:14:00 +03:00
										 |  |  |         PropertyKey key; | 
					
						
							| 
									
										
										
										
											2020-04-28 19:19:31 -07:00
										 |  |  |         PropertyMetadata value; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-04 15:48:23 +02:00
										 |  |  |     void set_prototype_without_transition(Object* new_prototype); | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2022-08-28 23:51:28 +02:00
										 |  |  |     explicit Shape(Realm&); | 
					
						
							| 
									
										
										
										
											2025-05-15 17:14:00 +03:00
										 |  |  |     Shape(Shape& previous_shape, PropertyKey const& property_key, PropertyAttributes attributes, TransitionType); | 
					
						
							|  |  |  |     Shape(Shape& previous_shape, PropertyKey const& property_key, TransitionType); | 
					
						
							| 
									
										
										
										
											2022-08-28 23:51:28 +02:00
										 |  |  |     Shape(Shape& previous_shape, Object* new_prototype); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     void invalidate_prototype_if_needed_for_new_prototype(GC::Ref<Shape> new_prototype_shape); | 
					
						
							| 
									
										
										
										
											2024-05-04 15:48:23 +02:00
										 |  |  |     void invalidate_all_prototype_chains_leading_to_this(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-28 14:33:36 +01:00
										 |  |  |     virtual void visit_edges(Visitor&) override; | 
					
						
							| 
									
										
										
										
											2021-10-02 16:35:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     [[nodiscard]] GC::Ptr<Shape> get_or_prune_cached_forward_transition(TransitionKey const&); | 
					
						
							|  |  |  |     [[nodiscard]] GC::Ptr<Shape> get_or_prune_cached_prototype_transition(Object* prototype); | 
					
						
							| 
									
										
										
										
											2025-05-15 17:14:00 +03:00
										 |  |  |     [[nodiscard]] GC::Ptr<Shape> get_or_prune_cached_delete_transition(PropertyKey const&); | 
					
						
							| 
									
										
										
										
											2021-10-01 02:43:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  |     void ensure_property_table() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC::Ref<Realm> m_realm; | 
					
						
							| 
									
										
										
										
											2020-06-08 12:15:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-15 17:14:00 +03:00
										 |  |  |     mutable OwnPtr<OrderedHashMap<PropertyKey, PropertyMetadata>> m_property_table; | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-31 12:43:30 +01:00
										 |  |  |     OwnPtr<HashMap<TransitionKey, WeakPtr<Shape>>> m_forward_transitions; | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     OwnPtr<HashMap<GC::Ptr<Object>, WeakPtr<Shape>>> m_prototype_transitions; | 
					
						
							| 
									
										
										
										
											2025-05-15 17:14:00 +03:00
										 |  |  |     OwnPtr<HashMap<PropertyKey, WeakPtr<Shape>>> m_delete_transitions; | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC::Ptr<Shape> m_previous; | 
					
						
							| 
									
										
										
										
											2025-05-15 17:14:00 +03:00
										 |  |  |     Optional<PropertyKey> m_property_key; | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC::Ptr<Object> m_prototype; | 
					
						
							| 
									
										
										
										
											2024-05-04 15:48:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC::Ptr<PrototypeChainValidity> m_prototype_chain_validity; | 
					
						
							| 
									
										
										
										
											2024-05-04 15:48:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-31 15:55:54 +01:00
										 |  |  |     u32 m_property_count { 0 }; | 
					
						
							| 
									
										
										
										
											2022-01-31 12:49:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     PropertyAttributes m_attributes { 0 }; | 
					
						
							| 
									
										
										
										
											2023-11-03 23:59:15 -04:00
										 |  |  |     TransitionType m_transition_type { TransitionType::Invalid }; | 
					
						
							| 
									
										
										
										
											2023-12-16 11:34:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-04 07:04:10 -06:00
										 |  |  |     bool m_dictionary : 1 { false }; | 
					
						
							|  |  |  |     bool m_cacheable : 1 { true }; | 
					
						
							|  |  |  |     bool m_is_prototype_shape : 1 { false }; | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-04-10 16:33:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-11-08 20:29:12 +01:00
										 |  |  | struct AK::Traits<JS::TransitionKey> : public DefaultTraits<JS::TransitionKey> { | 
					
						
							| 
									
										
										
										
											2020-04-10 16:33:44 +02:00
										 |  |  |     static unsigned hash(const JS::TransitionKey& key) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-05-15 17:14:00 +03:00
										 |  |  |         return pair_int_hash(key.attributes.bits(), Traits<JS::PropertyKey>::hash(key.property_key)); | 
					
						
							| 
									
										
										
										
											2020-04-10 16:33:44 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | }; |