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