Shape.cpp 10 KB

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