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>
|
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>
|
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 {
|
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 {
|
2022-02-06 15:59:04 +00:00
|
|
|
StringOrSymbol 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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-17 21:25:57 +02:00
|
|
|
class Shape final
|
|
|
|
: public Cell
|
|
|
|
, public Weakable<Shape> {
|
2022-08-28 22:11:20 +02:00
|
|
|
JS_CELL(Shape, Cell);
|
|
|
|
|
2020-04-02 19:32:21 +02:00
|
|
|
public:
|
2022-03-14 10:25:06 -06:00
|
|
|
virtual ~Shape() override = default;
|
2020-04-02 19:32:21 +02:00
|
|
|
|
2020-04-09 22:55:17 +02:00
|
|
|
enum class TransitionType {
|
|
|
|
Invalid,
|
|
|
|
Put,
|
|
|
|
Configure,
|
|
|
|
Prototype,
|
|
|
|
};
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Shape* create_put_transition(StringOrSymbol const&, PropertyAttributes attributes);
|
|
|
|
Shape* create_configure_transition(StringOrSymbol const&, PropertyAttributes attributes);
|
2020-04-02 19:32:21 +02:00
|
|
|
Shape* create_prototype_transition(Object* new_prototype);
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void add_property_without_transition(StringOrSymbol const&, 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;
|
|
|
|
|
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
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Optional<PropertyMetadata> lookup(StringOrSymbol const&) const;
|
|
|
|
HashMap<StringOrSymbol, 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 {
|
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; }
|
|
|
|
|
2022-04-01 20: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 13:53:40 +02:00
|
|
|
|
2020-04-02 19:32:21 +02:00
|
|
|
private:
|
2022-08-28 23:51:28 +02: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 14:33:36 +01:00
|
|
|
virtual void visit_edges(Visitor&) override;
|
2021-10-02 16:35:55 +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;
|
|
|
|
|
2022-08-01 20:27:20 +02:00
|
|
|
Realm& m_realm;
|
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
|
|
|
|
2022-01-31 12:43:30 +01:00
|
|
|
OwnPtr<HashMap<TransitionKey, WeakPtr<Shape>>> m_forward_transitions;
|
|
|
|
OwnPtr<HashMap<Object*, WeakPtr<Shape>>> m_prototype_transitions;
|
2020-04-02 19:32:21 +02:00
|
|
|
Shape* m_previous { nullptr };
|
2022-02-06 15:59:04 +00:00
|
|
|
StringOrSymbol m_property_key;
|
2020-04-02 19:32:21 +02:00
|
|
|
Object* m_prototype { nullptr };
|
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 };
|
|
|
|
TransitionType m_transition_type : 6 { TransitionType::Invalid };
|
|
|
|
bool m_unique : 1 { false };
|
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)
|
|
|
|
{
|
2022-02-06 15:59:04 +00:00
|
|
|
return pair_int_hash(key.attributes.bits(), Traits<JS::StringOrSymbol>::hash(key.property_key));
|
2020-04-10 16:33:44 +02:00
|
|
|
}
|
|
|
|
};
|