Shape.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. JS_DEFINE_ALLOCATOR(Shape);
  11. Shape* Shape::get_or_prune_cached_forward_transition(TransitionKey const& key)
  12. {
  13. if (!m_forward_transitions)
  14. return nullptr;
  15. auto it = m_forward_transitions->find(key);
  16. if (it == m_forward_transitions->end())
  17. return nullptr;
  18. if (!it->value) {
  19. // The cached forward transition has gone stale (from garbage collection). Prune it.
  20. m_forward_transitions->remove(it);
  21. return nullptr;
  22. }
  23. return it->value;
  24. }
  25. GCPtr<Shape> Shape::get_or_prune_cached_delete_transition(StringOrSymbol const& key)
  26. {
  27. if (!m_delete_transitions)
  28. return nullptr;
  29. auto it = m_delete_transitions->find(key);
  30. if (it == m_delete_transitions->end())
  31. return nullptr;
  32. if (!it->value) {
  33. // The cached delete transition has gone stale (from garbage collection). Prune it.
  34. m_delete_transitions->remove(it);
  35. return nullptr;
  36. }
  37. return it->value.ptr();
  38. }
  39. Shape* Shape::get_or_prune_cached_prototype_transition(Object* prototype)
  40. {
  41. if (!m_prototype_transitions)
  42. return nullptr;
  43. auto it = m_prototype_transitions->find(prototype);
  44. if (it == m_prototype_transitions->end())
  45. return nullptr;
  46. if (!it->value) {
  47. // The cached prototype transition has gone stale (from garbage collection). Prune it.
  48. m_prototype_transitions->remove(it);
  49. return nullptr;
  50. }
  51. return it->value;
  52. }
  53. Shape* Shape::create_put_transition(StringOrSymbol const& property_key, PropertyAttributes attributes)
  54. {
  55. TransitionKey key { property_key, attributes };
  56. if (auto* existing_shape = get_or_prune_cached_forward_transition(key))
  57. return existing_shape;
  58. auto new_shape = heap().allocate_without_realm<Shape>(*this, property_key, attributes, TransitionType::Put);
  59. if (!m_forward_transitions)
  60. m_forward_transitions = make<HashMap<TransitionKey, WeakPtr<Shape>>>();
  61. m_forward_transitions->set(key, new_shape.ptr());
  62. return new_shape;
  63. }
  64. Shape* Shape::create_configure_transition(StringOrSymbol const& property_key, PropertyAttributes attributes)
  65. {
  66. TransitionKey key { property_key, attributes };
  67. if (auto* existing_shape = get_or_prune_cached_forward_transition(key))
  68. return existing_shape;
  69. auto new_shape = heap().allocate_without_realm<Shape>(*this, property_key, attributes, TransitionType::Configure);
  70. if (!m_forward_transitions)
  71. m_forward_transitions = make<HashMap<TransitionKey, WeakPtr<Shape>>>();
  72. m_forward_transitions->set(key, new_shape.ptr());
  73. return new_shape;
  74. }
  75. Shape* Shape::create_prototype_transition(Object* new_prototype)
  76. {
  77. if (auto* existing_shape = get_or_prune_cached_prototype_transition(new_prototype))
  78. return existing_shape;
  79. auto new_shape = heap().allocate_without_realm<Shape>(*this, new_prototype);
  80. if (!m_prototype_transitions)
  81. m_prototype_transitions = make<HashMap<GCPtr<Object>, WeakPtr<Shape>>>();
  82. m_prototype_transitions->set(new_prototype, new_shape.ptr());
  83. return new_shape;
  84. }
  85. Shape::Shape(Realm& realm)
  86. : m_realm(realm)
  87. {
  88. }
  89. Shape::Shape(Shape& previous_shape, StringOrSymbol const& property_key, PropertyAttributes attributes, TransitionType transition_type)
  90. : m_realm(previous_shape.m_realm)
  91. , m_previous(&previous_shape)
  92. , m_property_key(property_key)
  93. , m_prototype(previous_shape.m_prototype)
  94. , m_property_count(transition_type == TransitionType::Put ? previous_shape.m_property_count + 1 : previous_shape.m_property_count)
  95. , m_attributes(attributes)
  96. , m_transition_type(transition_type)
  97. {
  98. }
  99. Shape::Shape(Shape& previous_shape, StringOrSymbol const& property_key, TransitionType transition_type)
  100. : m_realm(previous_shape.m_realm)
  101. , m_previous(&previous_shape)
  102. , m_property_key(property_key)
  103. , m_prototype(previous_shape.m_prototype)
  104. , m_property_count(previous_shape.m_property_count - 1)
  105. , m_transition_type(transition_type)
  106. {
  107. VERIFY(transition_type == TransitionType::Delete);
  108. }
  109. Shape::Shape(Shape& previous_shape, Object* new_prototype)
  110. : m_realm(previous_shape.m_realm)
  111. , m_previous(&previous_shape)
  112. , m_prototype(new_prototype)
  113. , m_property_count(previous_shape.m_property_count)
  114. , m_transition_type(TransitionType::Prototype)
  115. {
  116. }
  117. void Shape::visit_edges(Cell::Visitor& visitor)
  118. {
  119. Base::visit_edges(visitor);
  120. visitor.visit(m_realm);
  121. visitor.visit(m_prototype);
  122. visitor.visit(m_previous);
  123. m_property_key.visit_edges(visitor);
  124. if (m_property_table) {
  125. for (auto& it : *m_property_table)
  126. it.key.visit_edges(visitor);
  127. }
  128. visitor.ignore(m_prototype_transitions);
  129. // FIXME: The forward transition keys should be weak, but we have to mark them for now in case they go stale.
  130. if (m_forward_transitions) {
  131. for (auto& it : *m_forward_transitions)
  132. it.key.property_key.visit_edges(visitor);
  133. }
  134. // FIXME: The delete transition keys should be weak, but we have to mark them for now in case they go stale.
  135. if (m_delete_transitions) {
  136. for (auto& it : *m_delete_transitions)
  137. it.key.visit_edges(visitor);
  138. }
  139. }
  140. Optional<PropertyMetadata> Shape::lookup(StringOrSymbol const& property_key) const
  141. {
  142. if (m_property_count == 0)
  143. return {};
  144. auto property = property_table().get(property_key);
  145. if (!property.has_value())
  146. return {};
  147. return property;
  148. }
  149. FLATTEN OrderedHashMap<StringOrSymbol, PropertyMetadata> const& Shape::property_table() const
  150. {
  151. ensure_property_table();
  152. return *m_property_table;
  153. }
  154. void Shape::ensure_property_table() const
  155. {
  156. if (m_property_table)
  157. return;
  158. m_property_table = make<OrderedHashMap<StringOrSymbol, PropertyMetadata>>();
  159. u32 next_offset = 0;
  160. Vector<Shape const&, 64> transition_chain;
  161. transition_chain.append(*this);
  162. for (auto shape = m_previous; shape; shape = shape->m_previous) {
  163. if (shape->m_property_table) {
  164. *m_property_table = *shape->m_property_table;
  165. next_offset = shape->m_property_count;
  166. break;
  167. }
  168. transition_chain.append(*shape);
  169. }
  170. for (auto const& shape : transition_chain.in_reverse()) {
  171. if (!shape.m_property_key.is_valid()) {
  172. // Ignore prototype transitions as they don't affect the key map.
  173. continue;
  174. }
  175. if (shape.m_transition_type == TransitionType::Put) {
  176. m_property_table->set(shape.m_property_key, { next_offset++, shape.m_attributes });
  177. } else if (shape.m_transition_type == TransitionType::Configure) {
  178. auto it = m_property_table->find(shape.m_property_key);
  179. VERIFY(it != m_property_table->end());
  180. it->value.attributes = shape.m_attributes;
  181. } else if (shape.m_transition_type == TransitionType::Delete) {
  182. auto remove_it = m_property_table->find(shape.m_property_key);
  183. VERIFY(remove_it != m_property_table->end());
  184. auto removed_offset = remove_it->value.offset;
  185. m_property_table->remove(remove_it);
  186. for (auto& it : *m_property_table) {
  187. if (it.value.offset > removed_offset)
  188. --it.value.offset;
  189. }
  190. --next_offset;
  191. }
  192. }
  193. }
  194. NonnullGCPtr<Shape> Shape::create_delete_transition(StringOrSymbol const& property_key)
  195. {
  196. if (auto existing_shape = get_or_prune_cached_delete_transition(property_key))
  197. return *existing_shape;
  198. auto new_shape = heap().allocate_without_realm<Shape>(*this, property_key, TransitionType::Delete);
  199. if (!m_delete_transitions)
  200. m_delete_transitions = make<HashMap<StringOrSymbol, WeakPtr<Shape>>>();
  201. m_delete_transitions->set(property_key, new_shape.ptr());
  202. return new_shape;
  203. }
  204. void Shape::add_property_without_transition(StringOrSymbol const& property_key, PropertyAttributes attributes)
  205. {
  206. VERIFY(property_key.is_valid());
  207. ensure_property_table();
  208. if (m_property_table->set(property_key, { m_property_count, attributes }) == AK::HashSetResult::InsertedNewEntry) {
  209. VERIFY(m_property_count < NumericLimits<u32>::max());
  210. ++m_property_count;
  211. }
  212. }
  213. FLATTEN void Shape::add_property_without_transition(PropertyKey const& property_key, PropertyAttributes attributes)
  214. {
  215. VERIFY(property_key.is_valid());
  216. add_property_without_transition(property_key.to_string_or_symbol(), attributes);
  217. }
  218. }