/src/serenity/Userland/Libraries/LibJS/Runtime/Shape.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2020-2024, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/HashMap.h> |
10 | | #include <AK/OwnPtr.h> |
11 | | #include <AK/StringView.h> |
12 | | #include <AK/WeakPtr.h> |
13 | | #include <AK/Weakable.h> |
14 | | #include <LibJS/Forward.h> |
15 | | #include <LibJS/Heap/Cell.h> |
16 | | #include <LibJS/Runtime/PropertyAttributes.h> |
17 | | #include <LibJS/Runtime/StringOrSymbol.h> |
18 | | #include <LibJS/Runtime/Value.h> |
19 | | |
20 | | namespace JS { |
21 | | |
22 | | struct PropertyMetadata { |
23 | | u32 offset { 0 }; |
24 | | PropertyAttributes attributes { 0 }; |
25 | | }; |
26 | | |
27 | | struct TransitionKey { |
28 | | StringOrSymbol property_key; |
29 | | PropertyAttributes attributes { 0 }; |
30 | | |
31 | | bool operator==(TransitionKey const& other) const |
32 | 38.9k | { |
33 | 38.9k | return property_key == other.property_key && attributes == other.attributes; |
34 | 38.9k | } |
35 | | }; |
36 | | |
37 | | class PrototypeChainValidity final : public Cell { |
38 | | JS_CELL(PrototypeChainValidity, Cell); |
39 | | JS_DECLARE_ALLOCATOR(PrototypeChainValidity); |
40 | | |
41 | | public: |
42 | 0 | [[nodiscard]] bool is_valid() const { return m_valid; } |
43 | 7.62k | void set_valid(bool valid) { m_valid = valid; } |
44 | | |
45 | | private: |
46 | | bool m_valid { true }; |
47 | | size_t padding { 0 }; |
48 | | }; |
49 | | |
50 | | class Shape final : public Cell { |
51 | | JS_CELL(Shape, Cell); |
52 | | JS_DECLARE_ALLOCATOR(Shape); |
53 | | |
54 | | public: |
55 | | virtual ~Shape() override; |
56 | | |
57 | | enum class TransitionType : u8 { |
58 | | Invalid, |
59 | | Put, |
60 | | Configure, |
61 | | Prototype, |
62 | | Delete, |
63 | | CacheableDictionary, |
64 | | UncacheableDictionary, |
65 | | }; |
66 | | |
67 | | [[nodiscard]] NonnullGCPtr<Shape> create_put_transition(StringOrSymbol const&, PropertyAttributes attributes); |
68 | | [[nodiscard]] NonnullGCPtr<Shape> create_configure_transition(StringOrSymbol const&, PropertyAttributes attributes); |
69 | | [[nodiscard]] NonnullGCPtr<Shape> create_prototype_transition(Object* new_prototype); |
70 | | [[nodiscard]] NonnullGCPtr<Shape> create_delete_transition(StringOrSymbol const&); |
71 | | [[nodiscard]] NonnullGCPtr<Shape> create_cacheable_dictionary_transition(); |
72 | | [[nodiscard]] NonnullGCPtr<Shape> create_uncacheable_dictionary_transition(); |
73 | | [[nodiscard]] NonnullGCPtr<Shape> clone_for_prototype(); |
74 | | [[nodiscard]] static NonnullGCPtr<Shape> create_for_prototype(NonnullGCPtr<Realm>, GCPtr<Object> prototype); |
75 | | |
76 | | void add_property_without_transition(StringOrSymbol const&, PropertyAttributes); |
77 | | void add_property_without_transition(PropertyKey const&, PropertyAttributes); |
78 | | |
79 | | void remove_property_without_transition(StringOrSymbol const&, u32 offset); |
80 | | void set_property_attributes_without_transition(StringOrSymbol const&, PropertyAttributes); |
81 | | |
82 | 1 | [[nodiscard]] bool is_cacheable() const { return m_cacheable; } |
83 | 115k | [[nodiscard]] bool is_dictionary() const { return m_dictionary; } |
84 | 0 | [[nodiscard]] bool is_cacheable_dictionary() const { return m_dictionary && m_cacheable; } |
85 | 0 | [[nodiscard]] bool is_uncacheable_dictionary() const { return m_dictionary && !m_cacheable; } |
86 | | |
87 | 19.7k | [[nodiscard]] bool is_prototype_shape() const { return m_is_prototype_shape; } |
88 | | void set_prototype_shape(); |
89 | | |
90 | 0 | GCPtr<PrototypeChainValidity> prototype_chain_validity() const { return m_prototype_chain_validity; } |
91 | | |
92 | 11.2k | Realm& realm() const { return m_realm; } |
93 | | |
94 | 23.8k | Object* prototype() { return m_prototype; } |
95 | 18 | Object const* prototype() const { return m_prototype; } |
96 | | |
97 | | Optional<PropertyMetadata> lookup(StringOrSymbol const&) const; |
98 | | OrderedHashMap<StringOrSymbol, PropertyMetadata> const& property_table() const; |
99 | 58.5k | u32 property_count() const { return m_property_count; } |
100 | | |
101 | | struct Property { |
102 | | StringOrSymbol key; |
103 | | PropertyMetadata value; |
104 | | }; |
105 | | |
106 | | void set_prototype_without_transition(Object* new_prototype); |
107 | | |
108 | | private: |
109 | | explicit Shape(Realm&); |
110 | | Shape(Shape& previous_shape, StringOrSymbol const& property_key, PropertyAttributes attributes, TransitionType); |
111 | | Shape(Shape& previous_shape, StringOrSymbol const& property_key, TransitionType); |
112 | | Shape(Shape& previous_shape, Object* new_prototype); |
113 | | |
114 | | void invalidate_prototype_if_needed_for_new_prototype(NonnullGCPtr<Shape> new_prototype_shape); |
115 | | void invalidate_all_prototype_chains_leading_to_this(); |
116 | | |
117 | | virtual void visit_edges(Visitor&) override; |
118 | | |
119 | | [[nodiscard]] GCPtr<Shape> get_or_prune_cached_forward_transition(TransitionKey const&); |
120 | | [[nodiscard]] GCPtr<Shape> get_or_prune_cached_prototype_transition(Object* prototype); |
121 | | [[nodiscard]] GCPtr<Shape> get_or_prune_cached_delete_transition(StringOrSymbol const&); |
122 | | |
123 | | void ensure_property_table() const; |
124 | | |
125 | | NonnullGCPtr<Realm> m_realm; |
126 | | |
127 | | mutable OwnPtr<OrderedHashMap<StringOrSymbol, PropertyMetadata>> m_property_table; |
128 | | |
129 | | OwnPtr<HashMap<TransitionKey, WeakPtr<Shape>>> m_forward_transitions; |
130 | | OwnPtr<HashMap<GCPtr<Object>, WeakPtr<Shape>>> m_prototype_transitions; |
131 | | OwnPtr<HashMap<StringOrSymbol, WeakPtr<Shape>>> m_delete_transitions; |
132 | | GCPtr<Shape> m_previous; |
133 | | StringOrSymbol m_property_key; |
134 | | GCPtr<Object> m_prototype; |
135 | | |
136 | | GCPtr<PrototypeChainValidity> m_prototype_chain_validity; |
137 | | |
138 | | u32 m_property_count { 0 }; |
139 | | |
140 | | PropertyAttributes m_attributes { 0 }; |
141 | | TransitionType m_transition_type { TransitionType::Invalid }; |
142 | | |
143 | | bool m_dictionary : 1 { false }; |
144 | | bool m_cacheable : 1 { true }; |
145 | | bool m_is_prototype_shape : 1 { false }; |
146 | | }; |
147 | | |
148 | | } |
149 | | |
150 | | template<> |
151 | | struct AK::Traits<JS::TransitionKey> : public DefaultTraits<JS::TransitionKey> { |
152 | | static unsigned hash(const JS::TransitionKey& key) |
153 | 57.8k | { |
154 | 57.8k | return pair_int_hash(key.attributes.bits(), Traits<JS::StringOrSymbol>::hash(key.property_key)); |
155 | 57.8k | } |
156 | | }; |