2020-04-06 09:09:01 +00:00
|
|
|
/*
|
2021-05-17 19:25:57 +00:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.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
|
|
|
*/
|
|
|
|
|
2020-09-09 19:34:02 +00:00
|
|
|
#include <LibJS/Heap/DeferGC.h>
|
2020-06-08 10:15:58 +00:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-04-02 17:32:21 +00:00
|
|
|
#include <LibJS/Runtime/Shape.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2020-04-26 11:53:40 +00:00
|
|
|
Shape* Shape::create_unique_clone() const
|
|
|
|
{
|
2022-08-01 18:27:20 +00:00
|
|
|
auto* new_shape = heap().allocate_without_global_object<Shape>(m_realm);
|
2020-04-26 11:53:40 +00:00
|
|
|
new_shape->m_unique = true;
|
|
|
|
new_shape->m_prototype = m_prototype;
|
|
|
|
ensure_property_table();
|
|
|
|
new_shape->ensure_property_table();
|
|
|
|
(*new_shape->m_property_table) = *m_property_table;
|
2020-10-04 16:02:28 +00:00
|
|
|
new_shape->m_property_count = new_shape->m_property_table->size();
|
2020-04-26 11:53:40 +00:00
|
|
|
return new_shape;
|
|
|
|
}
|
|
|
|
|
2021-05-17 19:25:57 +00:00
|
|
|
Shape* Shape::get_or_prune_cached_forward_transition(TransitionKey const& key)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
return it->value;
|
|
|
|
}
|
|
|
|
|
2021-10-01 04:33:39 +00:00
|
|
|
Shape* Shape::get_or_prune_cached_prototype_transition(Object* prototype)
|
2021-10-01 00:43:57 +00:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
return it->value;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
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 };
|
2021-05-17 19:25:57 +00:00
|
|
|
if (auto* existing_shape = get_or_prune_cached_forward_transition(key))
|
2020-04-10 14:33:44 +00:00
|
|
|
return existing_shape;
|
2022-02-06 15:59:04 +00:00
|
|
|
auto* new_shape = heap().allocate_without_global_object<Shape>(*this, property_key, attributes, TransitionType::Put);
|
2022-01-31 11:43:30 +00:00
|
|
|
if (!m_forward_transitions)
|
|
|
|
m_forward_transitions = make<HashMap<TransitionKey, WeakPtr<Shape>>>();
|
|
|
|
m_forward_transitions->set(key, new_shape);
|
2020-04-09 20:55:17 +00:00
|
|
|
return new_shape;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
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 };
|
2021-05-17 19:25:57 +00:00
|
|
|
if (auto* existing_shape = get_or_prune_cached_forward_transition(key))
|
2020-04-10 14:33:44 +00:00
|
|
|
return existing_shape;
|
2022-02-06 15:59:04 +00:00
|
|
|
auto* new_shape = heap().allocate_without_global_object<Shape>(*this, property_key, attributes, TransitionType::Configure);
|
2022-01-31 11:43:30 +00:00
|
|
|
if (!m_forward_transitions)
|
|
|
|
m_forward_transitions = make<HashMap<TransitionKey, WeakPtr<Shape>>>();
|
|
|
|
m_forward_transitions->set(key, new_shape);
|
2020-04-02 17:32:21 +00:00
|
|
|
return new_shape;
|
|
|
|
}
|
|
|
|
|
|
|
|
Shape* Shape::create_prototype_transition(Object* new_prototype)
|
|
|
|
{
|
2021-10-01 04:33:39 +00:00
|
|
|
if (auto* existing_shape = get_or_prune_cached_prototype_transition(new_prototype))
|
2021-10-01 00:43:57 +00:00
|
|
|
return existing_shape;
|
|
|
|
auto* new_shape = heap().allocate_without_global_object<Shape>(*this, new_prototype);
|
2022-01-31 11:43:30 +00:00
|
|
|
if (!m_prototype_transitions)
|
|
|
|
m_prototype_transitions = make<HashMap<Object*, WeakPtr<Shape>>>();
|
|
|
|
m_prototype_transitions->set(new_prototype, new_shape);
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2020-11-28 13:33:36 +00:00
|
|
|
Cell::visit_edges(visitor);
|
2022-08-01 18:27:20 +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);
|
2020-09-20 17:11:49 +00:00
|
|
|
if (m_property_table) {
|
|
|
|
for (auto& it : *m_property_table)
|
2020-11-28 13:33:36 +00:00
|
|
|
it.key.visit_edges(visitor);
|
2020-09-20 17:11:49 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-06-06 20:54:43 +00:00
|
|
|
FLATTEN HashMap<StringOrSymbol, PropertyMetadata> const& Shape::property_table() const
|
2020-04-02 17:32:21 +00:00
|
|
|
{
|
|
|
|
ensure_property_table();
|
|
|
|
return *m_property_table;
|
|
|
|
}
|
|
|
|
|
2020-04-29 02:19:31 +00:00
|
|
|
Vector<Shape::Property> Shape::property_table_ordered() const
|
|
|
|
{
|
|
|
|
auto vec = Vector<Shape::Property>();
|
2020-10-04 16:02:28 +00:00
|
|
|
vec.resize(property_count());
|
2020-04-29 02:19:31 +00:00
|
|
|
|
|
|
|
for (auto& it : property_table()) {
|
|
|
|
vec[it.value.offset] = { it.key, it.value };
|
|
|
|
}
|
|
|
|
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
2020-04-02 17:32:21 +00:00
|
|
|
void Shape::ensure_property_table() const
|
|
|
|
{
|
|
|
|
if (m_property_table)
|
|
|
|
return;
|
2020-07-08 04:38:46 +00:00
|
|
|
m_property_table = make<HashMap<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;
|
2020-10-05 16:16:22 +00:00
|
|
|
for (auto* shape = m_previous; shape; shape = shape->m_previous) {
|
|
|
|
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 17:23:30 +00:00
|
|
|
transition_chain.append(*this);
|
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;
|
2020-04-09 20:55:17 +00:00
|
|
|
}
|
2020-04-02 17:32:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
void Shape::add_property_to_unique_shape(StringOrSymbol const& property_key, PropertyAttributes attributes)
|
2020-04-26 11:53:40 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(is_unique());
|
|
|
|
VERIFY(m_property_table);
|
2022-02-06 15:59:04 +00:00
|
|
|
VERIFY(!m_property_table->contains(property_key));
|
|
|
|
m_property_table->set(property_key, { static_cast<u32>(m_property_table->size()), attributes });
|
2022-01-31 14:55:54 +00:00
|
|
|
|
|
|
|
VERIFY(m_property_count < NumericLimits<u32>::max());
|
2020-10-04 16:02:28 +00:00
|
|
|
++m_property_count;
|
2020-04-26 11:53:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
void Shape::reconfigure_property_in_unique_shape(StringOrSymbol const& property_key, PropertyAttributes attributes)
|
2020-04-26 11:53:40 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(is_unique());
|
|
|
|
VERIFY(m_property_table);
|
2022-02-06 15:59:04 +00:00
|
|
|
auto it = m_property_table->find(property_key);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(it != m_property_table->end());
|
2020-10-10 13:42:23 +00:00
|
|
|
it->value.attributes = attributes;
|
2022-02-06 15:59:04 +00:00
|
|
|
m_property_table->set(property_key, it->value);
|
2020-04-26 11:53:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
void Shape::remove_property_from_unique_shape(StringOrSymbol const& property_key, size_t offset)
|
2020-04-26 11:53:40 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(is_unique());
|
|
|
|
VERIFY(m_property_table);
|
2022-02-06 15:59:04 +00:00
|
|
|
if (m_property_table->remove(property_key))
|
2020-10-04 16:02:28 +00:00
|
|
|
--m_property_count;
|
2020-04-26 11:53:40 +00:00
|
|
|
for (auto& it : *m_property_table) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(it.value.offset != offset);
|
2020-04-26 11:53:40 +00:00
|
|
|
if (it.value.offset > offset)
|
|
|
|
--it.value.offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2022-02-06 15:59:04 +00:00
|
|
|
VERIFY(property_key.is_valid());
|
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
|
|
|
VERIFY(property_key.is_valid());
|
|
|
|
add_property_without_transition(property_key.to_string_or_symbol(), attributes);
|
2021-06-13 16:59:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 17:32:21 +00:00
|
|
|
}
|