2020-04-06 09:09:01 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2020-2024, Andreas Kling <andreas@ladybird.org>
|
2020-04-06 09:09:01 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-06 09:09:01 +00:00
|
|
|
*/
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
#include <LibGC/DeferGC.h>
|
2020-04-02 17:32:21 +00:00
|
|
|
#include <LibJS/Runtime/Shape.h>
|
2022-08-28 14:03:26 +00:00
|
|
|
#include <LibJS/Runtime/VM.h>
|
2020-04-02 17:32:21 +00:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC_DEFINE_ALLOCATOR(Shape);
|
|
|
|
GC_DEFINE_ALLOCATOR(PrototypeChainValidity);
|
2024-05-04 13:48:23 +00:00
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
static HashTable<GC::Ptr<Shape>> s_all_prototype_shapes;
|
2024-05-04 13:48:23 +00:00
|
|
|
|
|
|
|
Shape::~Shape()
|
|
|
|
{
|
|
|
|
if (m_is_prototype_shape)
|
|
|
|
s_all_prototype_shapes.remove(this);
|
|
|
|
}
|
2023-11-19 08:45:05 +00:00
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ref<Shape> Shape::create_cacheable_dictionary_transition()
|
2023-12-16 10:34:01 +00:00
|
|
|
{
|
2024-11-13 17:13:46 +00:00
|
|
|
auto new_shape = heap().allocate<Shape>(m_realm);
|
2023-12-16 10:34:01 +00:00
|
|
|
new_shape->m_dictionary = true;
|
|
|
|
new_shape->m_cacheable = true;
|
|
|
|
new_shape->m_prototype = m_prototype;
|
2024-05-04 13:48:23 +00:00
|
|
|
invalidate_prototype_if_needed_for_new_prototype(new_shape);
|
2023-12-16 10:34:01 +00:00
|
|
|
ensure_property_table();
|
|
|
|
new_shape->ensure_property_table();
|
|
|
|
(*new_shape->m_property_table) = *m_property_table;
|
|
|
|
new_shape->m_property_count = new_shape->m_property_table->size();
|
|
|
|
return new_shape;
|
|
|
|
}
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ref<Shape> Shape::create_uncacheable_dictionary_transition()
|
2023-12-16 10:34:01 +00:00
|
|
|
{
|
2024-11-13 17:13:46 +00:00
|
|
|
auto new_shape = heap().allocate<Shape>(m_realm);
|
2023-12-16 10:34:01 +00:00
|
|
|
new_shape->m_dictionary = true;
|
|
|
|
new_shape->m_cacheable = true;
|
|
|
|
new_shape->m_prototype = m_prototype;
|
2024-05-04 13:48:23 +00:00
|
|
|
invalidate_prototype_if_needed_for_new_prototype(new_shape);
|
2023-12-16 10:34:01 +00:00
|
|
|
ensure_property_table();
|
|
|
|
new_shape->ensure_property_table();
|
|
|
|
(*new_shape->m_property_table) = *m_property_table;
|
|
|
|
new_shape->m_property_count = new_shape->m_property_table->size();
|
|
|
|
return new_shape;
|
|
|
|
}
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ptr<Shape> Shape::get_or_prune_cached_forward_transition(TransitionKey const& key)
|
2021-05-17 19:25:57 +00:00
|
|
|
{
|
2024-05-04 13:48:23 +00:00
|
|
|
if (m_is_prototype_shape)
|
|
|
|
return nullptr;
|
2022-01-31 11:43:30 +00:00
|
|
|
if (!m_forward_transitions)
|
|
|
|
return nullptr;
|
|
|
|
auto it = m_forward_transitions->find(key);
|
|
|
|
if (it == m_forward_transitions->end())
|
2021-05-17 19:25:57 +00:00
|
|
|
return nullptr;
|
|
|
|
if (!it->value) {
|
|
|
|
// The cached forward transition has gone stale (from garbage collection). Prune it.
|
2022-01-31 11:43:30 +00:00
|
|
|
m_forward_transitions->remove(it);
|
2021-05-17 19:25:57 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2024-05-04 13:48:23 +00:00
|
|
|
return it->value.ptr();
|
2021-05-17 19:25:57 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ptr<Shape> Shape::get_or_prune_cached_delete_transition(StringOrSymbol const& key)
|
2023-12-11 13:29:40 +00:00
|
|
|
{
|
2024-05-04 13:48:23 +00:00
|
|
|
if (m_is_prototype_shape)
|
|
|
|
return nullptr;
|
2023-12-11 13:29:40 +00:00
|
|
|
if (!m_delete_transitions)
|
|
|
|
return nullptr;
|
|
|
|
auto it = m_delete_transitions->find(key);
|
|
|
|
if (it == m_delete_transitions->end())
|
|
|
|
return nullptr;
|
|
|
|
if (!it->value) {
|
|
|
|
// The cached delete transition has gone stale (from garbage collection). Prune it.
|
|
|
|
m_delete_transitions->remove(it);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return it->value.ptr();
|
|
|
|
}
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ptr<Shape> Shape::get_or_prune_cached_prototype_transition(Object* prototype)
|
2021-10-01 00:43:57 +00:00
|
|
|
{
|
2024-05-04 13:48:23 +00:00
|
|
|
if (m_is_prototype_shape)
|
|
|
|
return nullptr;
|
2022-01-31 11:43:30 +00:00
|
|
|
if (!m_prototype_transitions)
|
|
|
|
return nullptr;
|
|
|
|
auto it = m_prototype_transitions->find(prototype);
|
|
|
|
if (it == m_prototype_transitions->end())
|
2021-10-01 00:43:57 +00:00
|
|
|
return nullptr;
|
|
|
|
if (!it->value) {
|
|
|
|
// The cached prototype transition has gone stale (from garbage collection). Prune it.
|
2022-01-31 11:43:30 +00:00
|
|
|
m_prototype_transitions->remove(it);
|
2021-10-01 00:43:57 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2024-05-04 13:48:23 +00:00
|
|
|
return it->value.ptr();
|
2021-10-01 00:43:57 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ref<Shape> Shape::create_put_transition(StringOrSymbol const& property_key, PropertyAttributes attributes)
|
2020-04-02 17:32:21 +00:00
|
|
|
{
|
2022-02-06 15:59:04 +00:00
|
|
|
TransitionKey key { property_key, attributes };
|
2024-05-04 13:48:23 +00:00
|
|
|
if (auto existing_shape = get_or_prune_cached_forward_transition(key))
|
|
|
|
return *existing_shape;
|
2024-11-13 17:13:46 +00:00
|
|
|
auto new_shape = heap().allocate<Shape>(*this, property_key, attributes, TransitionType::Put);
|
2024-05-04 13:48:23 +00:00
|
|
|
invalidate_prototype_if_needed_for_new_prototype(new_shape);
|
|
|
|
if (!m_is_prototype_shape) {
|
|
|
|
if (!m_forward_transitions)
|
|
|
|
m_forward_transitions = make<HashMap<TransitionKey, WeakPtr<Shape>>>();
|
|
|
|
m_forward_transitions->set(key, new_shape.ptr());
|
|
|
|
}
|
2020-04-09 20:55:17 +00:00
|
|
|
return new_shape;
|
|
|
|
}
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ref<Shape> Shape::create_configure_transition(StringOrSymbol const& property_key, PropertyAttributes attributes)
|
2020-04-09 20:55:17 +00:00
|
|
|
{
|
2022-02-06 15:59:04 +00:00
|
|
|
TransitionKey key { property_key, attributes };
|
2024-05-04 13:48:23 +00:00
|
|
|
if (auto existing_shape = get_or_prune_cached_forward_transition(key))
|
|
|
|
return *existing_shape;
|
2024-11-13 17:13:46 +00:00
|
|
|
auto new_shape = heap().allocate<Shape>(*this, property_key, attributes, TransitionType::Configure);
|
2024-05-04 13:48:23 +00:00
|
|
|
invalidate_prototype_if_needed_for_new_prototype(new_shape);
|
|
|
|
if (!m_is_prototype_shape) {
|
|
|
|
if (!m_forward_transitions)
|
|
|
|
m_forward_transitions = make<HashMap<TransitionKey, WeakPtr<Shape>>>();
|
|
|
|
m_forward_transitions->set(key, new_shape.ptr());
|
|
|
|
}
|
2020-04-02 17:32:21 +00:00
|
|
|
return new_shape;
|
|
|
|
}
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ref<Shape> Shape::create_prototype_transition(Object* new_prototype)
|
2020-04-02 17:32:21 +00:00
|
|
|
{
|
2024-05-04 13:48:23 +00:00
|
|
|
if (new_prototype)
|
|
|
|
new_prototype->convert_to_prototype_if_needed();
|
|
|
|
if (auto existing_shape = get_or_prune_cached_prototype_transition(new_prototype))
|
|
|
|
return *existing_shape;
|
2024-11-13 17:13:46 +00:00
|
|
|
auto new_shape = heap().allocate<Shape>(*this, new_prototype);
|
2024-05-04 13:48:23 +00:00
|
|
|
invalidate_prototype_if_needed_for_new_prototype(new_shape);
|
|
|
|
if (!m_is_prototype_shape) {
|
|
|
|
if (!m_prototype_transitions)
|
2024-11-14 15:01:23 +00:00
|
|
|
m_prototype_transitions = make<HashMap<GC::Ptr<Object>, WeakPtr<Shape>>>();
|
2024-05-04 13:48:23 +00:00
|
|
|
m_prototype_transitions->set(new_prototype, new_shape.ptr());
|
|
|
|
}
|
2021-10-01 00:43:57 +00:00
|
|
|
return new_shape;
|
2020-04-02 17:32:21 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 18:27:20 +00:00
|
|
|
Shape::Shape(Realm& realm)
|
|
|
|
: m_realm(realm)
|
2020-04-02 17:32:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
Shape::Shape(Shape& previous_shape, StringOrSymbol const& property_key, PropertyAttributes attributes, TransitionType transition_type)
|
2022-08-01 18:27:20 +00:00
|
|
|
: m_realm(previous_shape.m_realm)
|
2020-06-08 10:15:58 +00:00
|
|
|
, m_previous(&previous_shape)
|
2022-02-06 15:59:04 +00:00
|
|
|
, m_property_key(property_key)
|
2020-06-08 10:15:58 +00:00
|
|
|
, m_prototype(previous_shape.m_prototype)
|
2020-10-04 16:02:28 +00:00
|
|
|
, m_property_count(transition_type == TransitionType::Put ? previous_shape.m_property_count + 1 : previous_shape.m_property_count)
|
2022-01-31 11:49:52 +00:00
|
|
|
, m_attributes(attributes)
|
|
|
|
, m_transition_type(transition_type)
|
2020-04-02 17:32:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-12-11 13:29:40 +00:00
|
|
|
Shape::Shape(Shape& previous_shape, StringOrSymbol const& property_key, TransitionType transition_type)
|
|
|
|
: m_realm(previous_shape.m_realm)
|
|
|
|
, m_previous(&previous_shape)
|
|
|
|
, m_property_key(property_key)
|
|
|
|
, m_prototype(previous_shape.m_prototype)
|
|
|
|
, m_property_count(previous_shape.m_property_count - 1)
|
|
|
|
, m_transition_type(transition_type)
|
|
|
|
{
|
|
|
|
VERIFY(transition_type == TransitionType::Delete);
|
|
|
|
}
|
|
|
|
|
2020-06-08 10:15:58 +00:00
|
|
|
Shape::Shape(Shape& previous_shape, Object* new_prototype)
|
2022-08-01 18:27:20 +00:00
|
|
|
: m_realm(previous_shape.m_realm)
|
2020-06-08 10:15:58 +00:00
|
|
|
, m_previous(&previous_shape)
|
2020-04-02 17:32:21 +00:00
|
|
|
, m_prototype(new_prototype)
|
2020-10-04 16:02:28 +00:00
|
|
|
, m_property_count(previous_shape.m_property_count)
|
2022-01-31 11:49:52 +00:00
|
|
|
, m_transition_type(TransitionType::Prototype)
|
2020-04-02 17:32:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-11-28 13:33:36 +00:00
|
|
|
void Shape::visit_edges(Cell::Visitor& visitor)
|
2020-04-02 17:32:21 +00:00
|
|
|
{
|
2023-03-20 20:37:11 +00:00
|
|
|
Base::visit_edges(visitor);
|
2023-02-26 23:09:02 +00:00
|
|
|
visitor.visit(m_realm);
|
2020-04-16 14:07:50 +00:00
|
|
|
visitor.visit(m_prototype);
|
|
|
|
visitor.visit(m_previous);
|
2022-02-06 15:59:04 +00:00
|
|
|
m_property_key.visit_edges(visitor);
|
2023-12-11 15:50:04 +00:00
|
|
|
|
|
|
|
// NOTE: We don't need to mark the keys in the property table, since they are guaranteed
|
|
|
|
// to also be marked by the chain of shapes leading up to this one.
|
|
|
|
|
2023-03-23 00:05:16 +00:00
|
|
|
visitor.ignore(m_prototype_transitions);
|
2023-12-11 10:18:59 +00:00
|
|
|
|
|
|
|
// FIXME: The forward transition keys should be weak, but we have to mark them for now in case they go stale.
|
|
|
|
if (m_forward_transitions) {
|
|
|
|
for (auto& it : *m_forward_transitions)
|
|
|
|
it.key.property_key.visit_edges(visitor);
|
|
|
|
}
|
2023-12-11 13:29:40 +00:00
|
|
|
|
|
|
|
// FIXME: The delete transition keys should be weak, but we have to mark them for now in case they go stale.
|
|
|
|
if (m_delete_transitions) {
|
|
|
|
for (auto& it : *m_delete_transitions)
|
|
|
|
it.key.visit_edges(visitor);
|
|
|
|
}
|
2024-05-04 13:48:23 +00:00
|
|
|
|
|
|
|
visitor.visit(m_prototype_chain_validity);
|
2020-04-02 17:32:21 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
Optional<PropertyMetadata> Shape::lookup(StringOrSymbol const& property_key) const
|
2020-04-02 17:32:21 +00:00
|
|
|
{
|
2020-10-05 18:48:26 +00:00
|
|
|
if (m_property_count == 0)
|
|
|
|
return {};
|
2022-02-06 15:59:04 +00:00
|
|
|
auto property = property_table().get(property_key);
|
2020-04-26 11:53:40 +00:00
|
|
|
if (!property.has_value())
|
|
|
|
return {};
|
|
|
|
return property;
|
2020-04-02 17:32:21 +00:00
|
|
|
}
|
|
|
|
|
2023-09-17 09:29:43 +00:00
|
|
|
FLATTEN OrderedHashMap<StringOrSymbol, PropertyMetadata> const& Shape::property_table() const
|
2020-04-02 17:32:21 +00:00
|
|
|
{
|
|
|
|
ensure_property_table();
|
|
|
|
return *m_property_table;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shape::ensure_property_table() const
|
|
|
|
{
|
|
|
|
if (m_property_table)
|
|
|
|
return;
|
2023-09-17 09:29:43 +00:00
|
|
|
m_property_table = make<OrderedHashMap<StringOrSymbol, PropertyMetadata>>();
|
2020-04-02 17:32:21 +00:00
|
|
|
|
2020-10-05 16:16:22 +00:00
|
|
|
u32 next_offset = 0;
|
|
|
|
|
2022-04-13 17:23:30 +00:00
|
|
|
Vector<Shape const&, 64> transition_chain;
|
2023-12-11 13:29:40 +00:00
|
|
|
transition_chain.append(*this);
|
2023-02-26 23:09:02 +00:00
|
|
|
for (auto shape = m_previous; shape; shape = shape->m_previous) {
|
2020-10-05 16:16:22 +00:00
|
|
|
if (shape->m_property_table) {
|
|
|
|
*m_property_table = *shape->m_property_table;
|
|
|
|
next_offset = shape->m_property_count;
|
|
|
|
break;
|
|
|
|
}
|
2022-04-13 17:23:30 +00:00
|
|
|
transition_chain.append(*shape);
|
2020-04-02 17:32:21 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 19:01:29 +00:00
|
|
|
for (auto const& shape : transition_chain.in_reverse()) {
|
2022-04-13 17:23:30 +00:00
|
|
|
if (!shape.m_property_key.is_valid()) {
|
2020-04-02 17:32:21 +00:00
|
|
|
// Ignore prototype transitions as they don't affect the key map.
|
|
|
|
continue;
|
|
|
|
}
|
2022-04-13 17:23:30 +00:00
|
|
|
if (shape.m_transition_type == TransitionType::Put) {
|
|
|
|
m_property_table->set(shape.m_property_key, { next_offset++, shape.m_attributes });
|
|
|
|
} else if (shape.m_transition_type == TransitionType::Configure) {
|
|
|
|
auto it = m_property_table->find(shape.m_property_key);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(it != m_property_table->end());
|
2022-04-13 17:23:30 +00:00
|
|
|
it->value.attributes = shape.m_attributes;
|
2023-12-11 13:29:40 +00:00
|
|
|
} else if (shape.m_transition_type == TransitionType::Delete) {
|
|
|
|
auto remove_it = m_property_table->find(shape.m_property_key);
|
|
|
|
VERIFY(remove_it != m_property_table->end());
|
|
|
|
auto removed_offset = remove_it->value.offset;
|
|
|
|
m_property_table->remove(remove_it);
|
|
|
|
for (auto& it : *m_property_table) {
|
|
|
|
if (it.value.offset > removed_offset)
|
|
|
|
--it.value.offset;
|
|
|
|
}
|
|
|
|
--next_offset;
|
2020-04-09 20:55:17 +00:00
|
|
|
}
|
2020-04-02 17:32:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ref<Shape> Shape::create_delete_transition(StringOrSymbol const& property_key)
|
2020-04-26 11:53:40 +00:00
|
|
|
{
|
2023-12-11 13:29:40 +00:00
|
|
|
if (auto existing_shape = get_or_prune_cached_delete_transition(property_key))
|
|
|
|
return *existing_shape;
|
2024-11-13 17:13:46 +00:00
|
|
|
auto new_shape = heap().allocate<Shape>(*this, property_key, TransitionType::Delete);
|
2024-05-04 13:48:23 +00:00
|
|
|
invalidate_prototype_if_needed_for_new_prototype(new_shape);
|
2023-12-11 13:29:40 +00:00
|
|
|
if (!m_delete_transitions)
|
|
|
|
m_delete_transitions = make<HashMap<StringOrSymbol, WeakPtr<Shape>>>();
|
|
|
|
m_delete_transitions->set(property_key, new_shape.ptr());
|
|
|
|
return new_shape;
|
2020-04-26 11:53:40 +00:00
|
|
|
}
|
|
|
|
|
2022-02-06 15:59:04 +00:00
|
|
|
void Shape::add_property_without_transition(StringOrSymbol const& property_key, PropertyAttributes attributes)
|
2020-10-05 18:08:14 +00:00
|
|
|
{
|
|
|
|
ensure_property_table();
|
2022-02-06 15:59:04 +00:00
|
|
|
if (m_property_table->set(property_key, { m_property_count, attributes }) == AK::HashSetResult::InsertedNewEntry) {
|
2022-01-31 14:55:54 +00:00
|
|
|
VERIFY(m_property_count < NumericLimits<u32>::max());
|
2020-10-05 18:08:14 +00:00
|
|
|
++m_property_count;
|
2022-01-31 14:55:54 +00:00
|
|
|
}
|
2020-10-05 18:08:14 +00:00
|
|
|
}
|
|
|
|
|
2022-02-06 15:59:04 +00:00
|
|
|
FLATTEN void Shape::add_property_without_transition(PropertyKey const& property_key, PropertyAttributes attributes)
|
2021-06-13 16:59:07 +00:00
|
|
|
{
|
2022-02-06 15:59:04 +00:00
|
|
|
add_property_without_transition(property_key.to_string_or_symbol(), attributes);
|
2021-06-13 16:59:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 10:34:01 +00:00
|
|
|
void Shape::set_property_attributes_without_transition(StringOrSymbol const& property_key, PropertyAttributes attributes)
|
|
|
|
{
|
|
|
|
VERIFY(is_dictionary());
|
|
|
|
VERIFY(m_property_table);
|
|
|
|
auto it = m_property_table->find(property_key);
|
|
|
|
VERIFY(it != m_property_table->end());
|
|
|
|
it->value.attributes = attributes;
|
|
|
|
m_property_table->set(property_key, it->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shape::remove_property_without_transition(StringOrSymbol const& property_key, u32 offset)
|
|
|
|
{
|
|
|
|
VERIFY(is_uncacheable_dictionary());
|
|
|
|
VERIFY(m_property_table);
|
|
|
|
if (m_property_table->remove(property_key))
|
|
|
|
--m_property_count;
|
|
|
|
for (auto& it : *m_property_table) {
|
|
|
|
VERIFY(it.value.offset != offset);
|
|
|
|
if (it.value.offset > offset)
|
|
|
|
--it.value.offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ref<Shape> Shape::create_for_prototype(GC::Ref<Realm> realm, GC::Ptr<Object> prototype)
|
2024-05-04 13:48:23 +00:00
|
|
|
{
|
2024-11-13 17:13:46 +00:00
|
|
|
auto new_shape = realm->heap().allocate<Shape>(realm);
|
2024-05-04 13:48:23 +00:00
|
|
|
s_all_prototype_shapes.set(new_shape);
|
|
|
|
new_shape->m_is_prototype_shape = true;
|
|
|
|
new_shape->m_prototype = prototype;
|
2024-11-13 17:13:46 +00:00
|
|
|
new_shape->m_prototype_chain_validity = realm->heap().allocate<PrototypeChainValidity>();
|
2024-05-04 13:48:23 +00:00
|
|
|
return new_shape;
|
|
|
|
}
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ref<Shape> Shape::clone_for_prototype()
|
2024-05-04 13:48:23 +00:00
|
|
|
{
|
|
|
|
VERIFY(!m_is_prototype_shape);
|
|
|
|
VERIFY(!m_prototype_chain_validity);
|
2024-11-13 17:13:46 +00:00
|
|
|
auto new_shape = heap().allocate<Shape>(m_realm);
|
2024-05-04 13:48:23 +00:00
|
|
|
s_all_prototype_shapes.set(new_shape);
|
|
|
|
new_shape->m_is_prototype_shape = true;
|
|
|
|
new_shape->m_prototype = m_prototype;
|
|
|
|
ensure_property_table();
|
|
|
|
new_shape->ensure_property_table();
|
|
|
|
(*new_shape->m_property_table) = *m_property_table;
|
|
|
|
new_shape->m_property_count = new_shape->m_property_table->size();
|
2024-11-13 17:13:46 +00:00
|
|
|
new_shape->m_prototype_chain_validity = heap().allocate<PrototypeChainValidity>();
|
2024-05-04 13:48:23 +00:00
|
|
|
return new_shape;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shape::set_prototype_without_transition(Object* new_prototype)
|
|
|
|
{
|
|
|
|
VERIFY(new_prototype);
|
|
|
|
new_prototype->convert_to_prototype_if_needed();
|
|
|
|
m_prototype = new_prototype;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shape::set_prototype_shape()
|
|
|
|
{
|
|
|
|
VERIFY(!m_is_prototype_shape);
|
|
|
|
s_all_prototype_shapes.set(this);
|
|
|
|
m_is_prototype_shape = true;
|
2024-11-13 17:13:46 +00:00
|
|
|
m_prototype_chain_validity = heap().allocate<PrototypeChainValidity>();
|
2024-05-04 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
void Shape::invalidate_prototype_if_needed_for_new_prototype(GC::Ref<Shape> new_prototype_shape)
|
2024-05-04 13:48:23 +00:00
|
|
|
{
|
|
|
|
if (!m_is_prototype_shape)
|
|
|
|
return;
|
|
|
|
new_prototype_shape->set_prototype_shape();
|
|
|
|
m_prototype_chain_validity->set_valid(false);
|
|
|
|
|
|
|
|
invalidate_all_prototype_chains_leading_to_this();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shape::invalidate_all_prototype_chains_leading_to_this()
|
|
|
|
{
|
|
|
|
HashTable<Shape*> shapes_to_invalidate;
|
|
|
|
for (auto& candidate : s_all_prototype_shapes) {
|
|
|
|
if (!candidate->m_prototype)
|
|
|
|
continue;
|
|
|
|
for (auto* current_prototype_shape = &candidate->m_prototype->shape(); current_prototype_shape; current_prototype_shape = current_prototype_shape->prototype() ? ¤t_prototype_shape->prototype()->shape() : nullptr) {
|
|
|
|
if (current_prototype_shape == this) {
|
|
|
|
VERIFY(candidate->m_is_prototype_shape);
|
|
|
|
shapes_to_invalidate.set(candidate);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (shapes_to_invalidate.is_empty())
|
|
|
|
return;
|
|
|
|
for (auto* shape : shapes_to_invalidate) {
|
|
|
|
shape->m_prototype_chain_validity->set_valid(false);
|
2024-11-13 17:13:46 +00:00
|
|
|
shape->m_prototype_chain_validity = heap().allocate<PrototypeChainValidity>();
|
2024-05-04 13:48:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 17:32:21 +00:00
|
|
|
}
|