2020-04-02 14:32:21 -03:00
|
|
|
/*
|
2021-05-17 16:25:57 -03:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.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>
|
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>
|
2020-07-08 01:38:46 -03:00
|
|
|
#include <LibJS/Runtime/StringOrSymbol.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 {
|
2022-02-06 12:59:04 -03:00
|
|
|
StringOrSymbol 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
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-17 16:25:57 -03:00
|
|
|
class Shape final
|
|
|
|
|
: public Cell
|
|
|
|
|
, public Weakable<Shape> {
|
2022-08-28 17:11:20 -03:00
|
|
|
JS_CELL(Shape, Cell);
|
|
|
|
|
|
2020-04-02 14:32:21 -03:00
|
|
|
public:
|
2022-03-14 13:25:06 -03:00
|
|
|
virtual ~Shape() override = default;
|
2020-04-02 14:32:21 -03:00
|
|
|
|
2020-04-09 17:55:17 -03:00
|
|
|
enum class TransitionType {
|
|
|
|
|
Invalid,
|
|
|
|
|
Put,
|
|
|
|
|
Configure,
|
|
|
|
|
Prototype,
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
Shape* create_put_transition(StringOrSymbol const&, PropertyAttributes attributes);
|
|
|
|
|
Shape* create_configure_transition(StringOrSymbol const&, PropertyAttributes attributes);
|
2020-04-02 14:32:21 -03:00
|
|
|
Shape* create_prototype_transition(Object* new_prototype);
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
void add_property_without_transition(StringOrSymbol const&, PropertyAttributes);
|
2021-10-24 11:01:24 -03:00
|
|
|
void add_property_without_transition(PropertyKey const&, PropertyAttributes);
|
2020-10-05 15:08:14 -03:00
|
|
|
|
2020-04-26 08:53:40 -03:00
|
|
|
bool is_unique() const { return m_unique; }
|
|
|
|
|
Shape* create_unique_clone() const;
|
|
|
|
|
|
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
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
Optional<PropertyMetadata> lookup(StringOrSymbol const&) const;
|
|
|
|
|
HashMap<StringOrSymbol, 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 {
|
2020-07-08 01:38:46 -03:00
|
|
|
StringOrSymbol key;
|
2020-04-28 23:19:31 -03:00
|
|
|
PropertyMetadata value;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Vector<Property> property_table_ordered() const;
|
|
|
|
|
|
2020-04-02 14:32:21 -03:00
|
|
|
void set_prototype_without_transition(Object* new_prototype) { m_prototype = new_prototype; }
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
void remove_property_from_unique_shape(StringOrSymbol const&, size_t offset);
|
|
|
|
|
void add_property_to_unique_shape(StringOrSymbol const&, PropertyAttributes attributes);
|
|
|
|
|
void reconfigure_property_in_unique_shape(StringOrSymbol const& property_key, PropertyAttributes attributes);
|
2020-04-26 08:53:40 -03:00
|
|
|
|
2020-04-02 14:32:21 -03:00
|
|
|
private:
|
2022-08-28 18:51:28 -03:00
|
|
|
explicit Shape(Realm&);
|
|
|
|
|
Shape(Shape& previous_shape, StringOrSymbol const& property_key, PropertyAttributes attributes, TransitionType);
|
|
|
|
|
Shape(Shape& previous_shape, Object* new_prototype);
|
|
|
|
|
|
2020-11-28 10:33:36 -03:00
|
|
|
virtual void visit_edges(Visitor&) override;
|
2021-10-02 11:35:55 -03:00
|
|
|
|
2021-05-17 16:25:57 -03:00
|
|
|
Shape* get_or_prune_cached_forward_transition(TransitionKey const&);
|
2021-10-01 01:33:39 -03:00
|
|
|
Shape* get_or_prune_cached_prototype_transition(Object* prototype);
|
2021-09-30 21:43:57 -03:00
|
|
|
|
2020-04-02 14:32:21 -03:00
|
|
|
void ensure_property_table() const;
|
|
|
|
|
|
2022-08-01 15:27:20 -03:00
|
|
|
Realm& m_realm;
|
2020-06-08 07:15:58 -03:00
|
|
|
|
2020-07-08 01:38:46 -03:00
|
|
|
mutable OwnPtr<HashMap<StringOrSymbol, 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;
|
|
|
|
|
OwnPtr<HashMap<Object*, WeakPtr<Shape>>> m_prototype_transitions;
|
2020-04-02 14:32:21 -03:00
|
|
|
Shape* m_previous { nullptr };
|
2022-02-06 12:59:04 -03:00
|
|
|
StringOrSymbol m_property_key;
|
2020-04-02 14:32:21 -03:00
|
|
|
Object* m_prototype { nullptr };
|
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 };
|
|
|
|
|
TransitionType m_transition_type : 6 { TransitionType::Invalid };
|
|
|
|
|
bool m_unique : 1 { false };
|
2020-04-02 14:32:21 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
2020-04-10 11:33:44 -03:00
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
struct AK::Traits<JS::TransitionKey> : public GenericTraits<JS::TransitionKey> {
|
|
|
|
|
static unsigned hash(const JS::TransitionKey& key)
|
|
|
|
|
{
|
2022-02-06 12:59:04 -03:00
|
|
|
return pair_int_hash(key.attributes.bits(), Traits<JS::StringOrSymbol>::hash(key.property_key));
|
2020-04-10 11:33:44 -03:00
|
|
|
}
|
|
|
|
|
};
|