Shape.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Heap/DeferGC.h>
  7. #include <LibJS/Runtime/Shape.h>
  8. #include <LibJS/Runtime/VM.h>
  9. namespace JS {
  10. Shape* Shape::create_unique_clone() const
  11. {
  12. auto new_shape = heap().allocate_without_realm<Shape>(m_realm);
  13. new_shape->m_unique = true;
  14. new_shape->m_prototype = m_prototype;
  15. ensure_property_table();
  16. new_shape->ensure_property_table();
  17. (*new_shape->m_property_table) = *m_property_table;
  18. new_shape->m_property_count = new_shape->m_property_table->size();
  19. return new_shape;
  20. }
  21. Shape* Shape::get_or_prune_cached_forward_transition(TransitionKey const& key)
  22. {
  23. if (!m_forward_transitions)
  24. return nullptr;
  25. auto it = m_forward_transitions->find(key);
  26. if (it == m_forward_transitions->end())
  27. return nullptr;
  28. if (!it->value) {
  29. // The cached forward transition has gone stale (from garbage collection). Prune it.
  30. m_forward_transitions->remove(it);
  31. return nullptr;
  32. }
  33. return it->value;
  34. }
  35. Shape* Shape::get_or_prune_cached_prototype_transition(Object* prototype)
  36. {
  37. if (!m_prototype_transitions)
  38. return nullptr;
  39. auto it = m_prototype_transitions->find(prototype);
  40. if (it == m_prototype_transitions->end())
  41. return nullptr;
  42. if (!it->value) {
  43. // The cached prototype transition has gone stale (from garbage collection). Prune it.
  44. m_prototype_transitions->remove(it);
  45. return nullptr;
  46. }
  47. return it->value;
  48. }
  49. Shape* Shape::create_put_transition(StringOrSymbol const& property_key, PropertyAttributes attributes)
  50. {
  51. TransitionKey key { property_key, attributes };
  52. if (auto* existing_shape = get_or_prune_cached_forward_transition(key))
  53. return existing_shape;
  54. auto new_shape = heap().allocate_without_realm<Shape>(*this, property_key, attributes, TransitionType::Put);
  55. if (!m_forward_transitions)
  56. m_forward_transitions = make<HashMap<TransitionKey, WeakPtr<Shape>>>();
  57. m_forward_transitions->set(key, new_shape.ptr());
  58. return new_shape;
  59. }
  60. Shape* Shape::create_configure_transition(StringOrSymbol const& property_key, PropertyAttributes attributes)
  61. {
  62. TransitionKey key { property_key, attributes };
  63. if (auto* existing_shape = get_or_prune_cached_forward_transition(key))
  64. return existing_shape;
  65. auto new_shape = heap().allocate_without_realm<Shape>(*this, property_key, attributes, TransitionType::Configure);
  66. if (!m_forward_transitions)
  67. m_forward_transitions = make<HashMap<TransitionKey, WeakPtr<Shape>>>();
  68. m_forward_transitions->set(key, new_shape.ptr());
  69. return new_shape;
  70. }
  71. Shape* Shape::create_prototype_transition(Object* new_prototype)
  72. {
  73. if (auto* existing_shape = get_or_prune_cached_prototype_transition(new_prototype))
  74. return existing_shape;
  75. auto new_shape = heap().allocate_without_realm<Shape>(*this, new_prototype);
  76. if (!m_prototype_transitions)
  77. m_prototype_transitions = make<HashMap<GCPtr<Object>, WeakPtr<Shape>>>();
  78. m_prototype_transitions->set(new_prototype, new_shape.ptr());
  79. return new_shape;
  80. }
  81. Shape::Shape(Realm& realm)
  82. : m_realm(realm)
  83. {
  84. }
  85. Shape::Shape(Shape& previous_shape, StringOrSymbol const& property_key, PropertyAttributes attributes, TransitionType transition_type)
  86. : m_realm(previous_shape.m_realm)
  87. , m_previous(&previous_shape)
  88. , m_property_key(property_key)
  89. , m_prototype(previous_shape.m_prototype)
  90. , m_property_count(transition_type == TransitionType::Put ? previous_shape.m_property_count + 1 : previous_shape.m_property_count)
  91. , m_attributes(attributes)
  92. , m_transition_type(transition_type)
  93. {
  94. }
  95. Shape::Shape(Shape& previous_shape, Object* new_prototype)
  96. : m_realm(previous_shape.m_realm)
  97. , m_previous(&previous_shape)
  98. , m_prototype(new_prototype)
  99. , m_property_count(previous_shape.m_property_count)
  100. , m_transition_type(TransitionType::Prototype)
  101. {
  102. }
  103. void Shape::visit_edges(Cell::Visitor& visitor)
  104. {
  105. Base::visit_edges(visitor);
  106. visitor.visit(m_realm);
  107. visitor.visit(m_prototype);
  108. visitor.visit(m_previous);
  109. m_property_key.visit_edges(visitor);
  110. if (m_property_table) {
  111. for (auto& it : *m_property_table)
  112. it.key.visit_edges(visitor);
  113. }
  114. visitor.ignore(m_prototype_transitions);
  115. }
  116. Optional<PropertyMetadata> Shape::lookup(StringOrSymbol const& property_key) const
  117. {
  118. if (m_property_count == 0)
  119. return {};
  120. auto property = property_table().get(property_key);
  121. if (!property.has_value())
  122. return {};
  123. return property;
  124. }
  125. FLATTEN OrderedHashMap<StringOrSymbol, PropertyMetadata> const& Shape::property_table() const
  126. {
  127. ensure_property_table();
  128. return *m_property_table;
  129. }
  130. void Shape::ensure_property_table() const
  131. {
  132. if (m_property_table)
  133. return;
  134. m_property_table = make<OrderedHashMap<StringOrSymbol, PropertyMetadata>>();
  135. u32 next_offset = 0;
  136. Vector<Shape const&, 64> transition_chain;
  137. for (auto shape = m_previous; shape; shape = shape->m_previous) {
  138. if (shape->m_property_table) {
  139. *m_property_table = *shape->m_property_table;
  140. next_offset = shape->m_property_count;
  141. break;
  142. }
  143. transition_chain.append(*shape);
  144. }
  145. transition_chain.append(*this);
  146. for (auto const& shape : transition_chain.in_reverse()) {
  147. if (!shape.m_property_key.is_valid()) {
  148. // Ignore prototype transitions as they don't affect the key map.
  149. continue;
  150. }
  151. if (shape.m_transition_type == TransitionType::Put) {
  152. m_property_table->set(shape.m_property_key, { next_offset++, shape.m_attributes });
  153. } else if (shape.m_transition_type == TransitionType::Configure) {
  154. auto it = m_property_table->find(shape.m_property_key);
  155. VERIFY(it != m_property_table->end());
  156. it->value.attributes = shape.m_attributes;
  157. }
  158. }
  159. }
  160. void Shape::add_property_to_unique_shape(StringOrSymbol const& property_key, PropertyAttributes attributes)
  161. {
  162. VERIFY(is_unique());
  163. VERIFY(m_property_table);
  164. VERIFY(!m_property_table->contains(property_key));
  165. m_property_table->set(property_key, { static_cast<u32>(m_property_table->size()), attributes });
  166. VERIFY(m_property_count < NumericLimits<u32>::max());
  167. ++m_property_count;
  168. ++m_unique_shape_serial_number;
  169. }
  170. void Shape::reconfigure_property_in_unique_shape(StringOrSymbol const& property_key, PropertyAttributes attributes)
  171. {
  172. VERIFY(is_unique());
  173. VERIFY(m_property_table);
  174. auto it = m_property_table->find(property_key);
  175. VERIFY(it != m_property_table->end());
  176. it->value.attributes = attributes;
  177. m_property_table->set(property_key, it->value);
  178. ++m_unique_shape_serial_number;
  179. }
  180. void Shape::remove_property_from_unique_shape(StringOrSymbol const& property_key, size_t offset)
  181. {
  182. VERIFY(is_unique());
  183. VERIFY(m_property_table);
  184. if (m_property_table->remove(property_key))
  185. --m_property_count;
  186. for (auto& it : *m_property_table) {
  187. VERIFY(it.value.offset != offset);
  188. if (it.value.offset > offset)
  189. --it.value.offset;
  190. }
  191. ++m_unique_shape_serial_number;
  192. }
  193. void Shape::add_property_without_transition(StringOrSymbol const& property_key, PropertyAttributes attributes)
  194. {
  195. VERIFY(property_key.is_valid());
  196. ensure_property_table();
  197. if (m_property_table->set(property_key, { m_property_count, attributes }) == AK::HashSetResult::InsertedNewEntry) {
  198. VERIFY(m_property_count < NumericLimits<u32>::max());
  199. ++m_property_count;
  200. }
  201. }
  202. FLATTEN void Shape::add_property_without_transition(PropertyKey const& property_key, PropertyAttributes attributes)
  203. {
  204. VERIFY(property_key.is_valid());
  205. add_property_without_transition(property_key.to_string_or_symbol(), attributes);
  206. }
  207. }