Shape.cpp 8.6 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. // NOTE: We don't need to mark the keys in the property table, since they are guaranteed
  125. // to also be marked by the chain of shapes leading up to this one.
  126. visitor.ignore(m_prototype_transitions);
  127. // FIXME: The forward transition keys should be weak, but we have to mark them for now in case they go stale.
  128. if (m_forward_transitions) {
  129. for (auto& it : *m_forward_transitions)
  130. it.key.property_key.visit_edges(visitor);
  131. }
  132. // FIXME: The delete transition keys should be weak, but we have to mark them for now in case they go stale.
  133. if (m_delete_transitions) {
  134. for (auto& it : *m_delete_transitions)
  135. it.key.visit_edges(visitor);
  136. }
  137. }
  138. Optional<PropertyMetadata> Shape::lookup(StringOrSymbol const& property_key) const
  139. {
  140. if (m_property_count == 0)
  141. return {};
  142. auto property = property_table().get(property_key);
  143. if (!property.has_value())
  144. return {};
  145. return property;
  146. }
  147. FLATTEN OrderedHashMap<StringOrSymbol, PropertyMetadata> const& Shape::property_table() const
  148. {
  149. ensure_property_table();
  150. return *m_property_table;
  151. }
  152. void Shape::ensure_property_table() const
  153. {
  154. if (m_property_table)
  155. return;
  156. m_property_table = make<OrderedHashMap<StringOrSymbol, PropertyMetadata>>();
  157. u32 next_offset = 0;
  158. Vector<Shape const&, 64> transition_chain;
  159. transition_chain.append(*this);
  160. for (auto shape = m_previous; shape; shape = shape->m_previous) {
  161. if (shape->m_property_table) {
  162. *m_property_table = *shape->m_property_table;
  163. next_offset = shape->m_property_count;
  164. break;
  165. }
  166. transition_chain.append(*shape);
  167. }
  168. for (auto const& shape : transition_chain.in_reverse()) {
  169. if (!shape.m_property_key.is_valid()) {
  170. // Ignore prototype transitions as they don't affect the key map.
  171. continue;
  172. }
  173. if (shape.m_transition_type == TransitionType::Put) {
  174. m_property_table->set(shape.m_property_key, { next_offset++, shape.m_attributes });
  175. } else if (shape.m_transition_type == TransitionType::Configure) {
  176. auto it = m_property_table->find(shape.m_property_key);
  177. VERIFY(it != m_property_table->end());
  178. it->value.attributes = shape.m_attributes;
  179. } else if (shape.m_transition_type == TransitionType::Delete) {
  180. auto remove_it = m_property_table->find(shape.m_property_key);
  181. VERIFY(remove_it != m_property_table->end());
  182. auto removed_offset = remove_it->value.offset;
  183. m_property_table->remove(remove_it);
  184. for (auto& it : *m_property_table) {
  185. if (it.value.offset > removed_offset)
  186. --it.value.offset;
  187. }
  188. --next_offset;
  189. }
  190. }
  191. }
  192. NonnullGCPtr<Shape> Shape::create_delete_transition(StringOrSymbol const& property_key)
  193. {
  194. if (auto existing_shape = get_or_prune_cached_delete_transition(property_key))
  195. return *existing_shape;
  196. auto new_shape = heap().allocate_without_realm<Shape>(*this, property_key, TransitionType::Delete);
  197. if (!m_delete_transitions)
  198. m_delete_transitions = make<HashMap<StringOrSymbol, WeakPtr<Shape>>>();
  199. m_delete_transitions->set(property_key, new_shape.ptr());
  200. return new_shape;
  201. }
  202. void Shape::add_property_without_transition(StringOrSymbol const& property_key, PropertyAttributes attributes)
  203. {
  204. VERIFY(property_key.is_valid());
  205. ensure_property_table();
  206. if (m_property_table->set(property_key, { m_property_count, attributes }) == AK::HashSetResult::InsertedNewEntry) {
  207. VERIFY(m_property_count < NumericLimits<u32>::max());
  208. ++m_property_count;
  209. }
  210. }
  211. FLATTEN void Shape::add_property_without_transition(PropertyKey const& property_key, PropertyAttributes attributes)
  212. {
  213. VERIFY(property_key.is_valid());
  214. add_property_without_transition(property_key.to_string_or_symbol(), attributes);
  215. }
  216. }