Shape.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Copyright (c) 2020-2024, Andreas Kling <andreas@ladybird.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. JS_DEFINE_ALLOCATOR(PrototypeChainValidity);
  12. static HashTable<JS::GCPtr<Shape>> s_all_prototype_shapes;
  13. Shape::~Shape()
  14. {
  15. if (m_is_prototype_shape)
  16. s_all_prototype_shapes.remove(this);
  17. }
  18. NonnullGCPtr<Shape> Shape::create_cacheable_dictionary_transition()
  19. {
  20. auto new_shape = heap().allocate<Shape>(m_realm);
  21. new_shape->m_dictionary = true;
  22. new_shape->m_cacheable = true;
  23. new_shape->m_prototype = m_prototype;
  24. invalidate_prototype_if_needed_for_new_prototype(new_shape);
  25. ensure_property_table();
  26. new_shape->ensure_property_table();
  27. (*new_shape->m_property_table) = *m_property_table;
  28. new_shape->m_property_count = new_shape->m_property_table->size();
  29. return new_shape;
  30. }
  31. NonnullGCPtr<Shape> Shape::create_uncacheable_dictionary_transition()
  32. {
  33. auto new_shape = heap().allocate<Shape>(m_realm);
  34. new_shape->m_dictionary = true;
  35. new_shape->m_cacheable = true;
  36. new_shape->m_prototype = m_prototype;
  37. invalidate_prototype_if_needed_for_new_prototype(new_shape);
  38. ensure_property_table();
  39. new_shape->ensure_property_table();
  40. (*new_shape->m_property_table) = *m_property_table;
  41. new_shape->m_property_count = new_shape->m_property_table->size();
  42. return new_shape;
  43. }
  44. GCPtr<Shape> Shape::get_or_prune_cached_forward_transition(TransitionKey const& key)
  45. {
  46. if (m_is_prototype_shape)
  47. return nullptr;
  48. if (!m_forward_transitions)
  49. return nullptr;
  50. auto it = m_forward_transitions->find(key);
  51. if (it == m_forward_transitions->end())
  52. return nullptr;
  53. if (!it->value) {
  54. // The cached forward transition has gone stale (from garbage collection). Prune it.
  55. m_forward_transitions->remove(it);
  56. return nullptr;
  57. }
  58. return it->value.ptr();
  59. }
  60. GCPtr<Shape> Shape::get_or_prune_cached_delete_transition(StringOrSymbol const& key)
  61. {
  62. if (m_is_prototype_shape)
  63. return nullptr;
  64. if (!m_delete_transitions)
  65. return nullptr;
  66. auto it = m_delete_transitions->find(key);
  67. if (it == m_delete_transitions->end())
  68. return nullptr;
  69. if (!it->value) {
  70. // The cached delete transition has gone stale (from garbage collection). Prune it.
  71. m_delete_transitions->remove(it);
  72. return nullptr;
  73. }
  74. return it->value.ptr();
  75. }
  76. GCPtr<Shape> Shape::get_or_prune_cached_prototype_transition(Object* prototype)
  77. {
  78. if (m_is_prototype_shape)
  79. return nullptr;
  80. if (!m_prototype_transitions)
  81. return nullptr;
  82. auto it = m_prototype_transitions->find(prototype);
  83. if (it == m_prototype_transitions->end())
  84. return nullptr;
  85. if (!it->value) {
  86. // The cached prototype transition has gone stale (from garbage collection). Prune it.
  87. m_prototype_transitions->remove(it);
  88. return nullptr;
  89. }
  90. return it->value.ptr();
  91. }
  92. NonnullGCPtr<Shape> Shape::create_put_transition(StringOrSymbol const& property_key, PropertyAttributes attributes)
  93. {
  94. TransitionKey key { property_key, attributes };
  95. if (auto existing_shape = get_or_prune_cached_forward_transition(key))
  96. return *existing_shape;
  97. auto new_shape = heap().allocate<Shape>(*this, property_key, attributes, TransitionType::Put);
  98. invalidate_prototype_if_needed_for_new_prototype(new_shape);
  99. if (!m_is_prototype_shape) {
  100. if (!m_forward_transitions)
  101. m_forward_transitions = make<HashMap<TransitionKey, WeakPtr<Shape>>>();
  102. m_forward_transitions->set(key, new_shape.ptr());
  103. }
  104. return new_shape;
  105. }
  106. NonnullGCPtr<Shape> Shape::create_configure_transition(StringOrSymbol const& property_key, PropertyAttributes attributes)
  107. {
  108. TransitionKey key { property_key, attributes };
  109. if (auto existing_shape = get_or_prune_cached_forward_transition(key))
  110. return *existing_shape;
  111. auto new_shape = heap().allocate<Shape>(*this, property_key, attributes, TransitionType::Configure);
  112. invalidate_prototype_if_needed_for_new_prototype(new_shape);
  113. if (!m_is_prototype_shape) {
  114. if (!m_forward_transitions)
  115. m_forward_transitions = make<HashMap<TransitionKey, WeakPtr<Shape>>>();
  116. m_forward_transitions->set(key, new_shape.ptr());
  117. }
  118. return new_shape;
  119. }
  120. NonnullGCPtr<Shape> Shape::create_prototype_transition(Object* new_prototype)
  121. {
  122. if (new_prototype)
  123. new_prototype->convert_to_prototype_if_needed();
  124. if (auto existing_shape = get_or_prune_cached_prototype_transition(new_prototype))
  125. return *existing_shape;
  126. auto new_shape = heap().allocate<Shape>(*this, new_prototype);
  127. invalidate_prototype_if_needed_for_new_prototype(new_shape);
  128. if (!m_is_prototype_shape) {
  129. if (!m_prototype_transitions)
  130. m_prototype_transitions = make<HashMap<GCPtr<Object>, WeakPtr<Shape>>>();
  131. m_prototype_transitions->set(new_prototype, new_shape.ptr());
  132. }
  133. return new_shape;
  134. }
  135. Shape::Shape(Realm& realm)
  136. : m_realm(realm)
  137. {
  138. }
  139. Shape::Shape(Shape& previous_shape, StringOrSymbol const& property_key, PropertyAttributes attributes, TransitionType transition_type)
  140. : m_realm(previous_shape.m_realm)
  141. , m_previous(&previous_shape)
  142. , m_property_key(property_key)
  143. , m_prototype(previous_shape.m_prototype)
  144. , m_property_count(transition_type == TransitionType::Put ? previous_shape.m_property_count + 1 : previous_shape.m_property_count)
  145. , m_attributes(attributes)
  146. , m_transition_type(transition_type)
  147. {
  148. }
  149. Shape::Shape(Shape& previous_shape, StringOrSymbol const& property_key, TransitionType transition_type)
  150. : m_realm(previous_shape.m_realm)
  151. , m_previous(&previous_shape)
  152. , m_property_key(property_key)
  153. , m_prototype(previous_shape.m_prototype)
  154. , m_property_count(previous_shape.m_property_count - 1)
  155. , m_transition_type(transition_type)
  156. {
  157. VERIFY(transition_type == TransitionType::Delete);
  158. }
  159. Shape::Shape(Shape& previous_shape, Object* new_prototype)
  160. : m_realm(previous_shape.m_realm)
  161. , m_previous(&previous_shape)
  162. , m_prototype(new_prototype)
  163. , m_property_count(previous_shape.m_property_count)
  164. , m_transition_type(TransitionType::Prototype)
  165. {
  166. }
  167. void Shape::visit_edges(Cell::Visitor& visitor)
  168. {
  169. Base::visit_edges(visitor);
  170. visitor.visit(m_realm);
  171. visitor.visit(m_prototype);
  172. visitor.visit(m_previous);
  173. m_property_key.visit_edges(visitor);
  174. // NOTE: We don't need to mark the keys in the property table, since they are guaranteed
  175. // to also be marked by the chain of shapes leading up to this one.
  176. visitor.ignore(m_prototype_transitions);
  177. // FIXME: The forward transition keys should be weak, but we have to mark them for now in case they go stale.
  178. if (m_forward_transitions) {
  179. for (auto& it : *m_forward_transitions)
  180. it.key.property_key.visit_edges(visitor);
  181. }
  182. // FIXME: The delete transition keys should be weak, but we have to mark them for now in case they go stale.
  183. if (m_delete_transitions) {
  184. for (auto& it : *m_delete_transitions)
  185. it.key.visit_edges(visitor);
  186. }
  187. visitor.visit(m_prototype_chain_validity);
  188. }
  189. Optional<PropertyMetadata> Shape::lookup(StringOrSymbol const& property_key) const
  190. {
  191. if (m_property_count == 0)
  192. return {};
  193. auto property = property_table().get(property_key);
  194. if (!property.has_value())
  195. return {};
  196. return property;
  197. }
  198. FLATTEN OrderedHashMap<StringOrSymbol, PropertyMetadata> const& Shape::property_table() const
  199. {
  200. ensure_property_table();
  201. return *m_property_table;
  202. }
  203. void Shape::ensure_property_table() const
  204. {
  205. if (m_property_table)
  206. return;
  207. m_property_table = make<OrderedHashMap<StringOrSymbol, PropertyMetadata>>();
  208. u32 next_offset = 0;
  209. Vector<Shape const&, 64> transition_chain;
  210. transition_chain.append(*this);
  211. for (auto shape = m_previous; shape; shape = shape->m_previous) {
  212. if (shape->m_property_table) {
  213. *m_property_table = *shape->m_property_table;
  214. next_offset = shape->m_property_count;
  215. break;
  216. }
  217. transition_chain.append(*shape);
  218. }
  219. for (auto const& shape : transition_chain.in_reverse()) {
  220. if (!shape.m_property_key.is_valid()) {
  221. // Ignore prototype transitions as they don't affect the key map.
  222. continue;
  223. }
  224. if (shape.m_transition_type == TransitionType::Put) {
  225. m_property_table->set(shape.m_property_key, { next_offset++, shape.m_attributes });
  226. } else if (shape.m_transition_type == TransitionType::Configure) {
  227. auto it = m_property_table->find(shape.m_property_key);
  228. VERIFY(it != m_property_table->end());
  229. it->value.attributes = shape.m_attributes;
  230. } else if (shape.m_transition_type == TransitionType::Delete) {
  231. auto remove_it = m_property_table->find(shape.m_property_key);
  232. VERIFY(remove_it != m_property_table->end());
  233. auto removed_offset = remove_it->value.offset;
  234. m_property_table->remove(remove_it);
  235. for (auto& it : *m_property_table) {
  236. if (it.value.offset > removed_offset)
  237. --it.value.offset;
  238. }
  239. --next_offset;
  240. }
  241. }
  242. }
  243. NonnullGCPtr<Shape> Shape::create_delete_transition(StringOrSymbol const& property_key)
  244. {
  245. if (auto existing_shape = get_or_prune_cached_delete_transition(property_key))
  246. return *existing_shape;
  247. auto new_shape = heap().allocate<Shape>(*this, property_key, TransitionType::Delete);
  248. invalidate_prototype_if_needed_for_new_prototype(new_shape);
  249. if (!m_delete_transitions)
  250. m_delete_transitions = make<HashMap<StringOrSymbol, WeakPtr<Shape>>>();
  251. m_delete_transitions->set(property_key, new_shape.ptr());
  252. return new_shape;
  253. }
  254. void Shape::add_property_without_transition(StringOrSymbol const& property_key, PropertyAttributes attributes)
  255. {
  256. ensure_property_table();
  257. if (m_property_table->set(property_key, { m_property_count, attributes }) == AK::HashSetResult::InsertedNewEntry) {
  258. VERIFY(m_property_count < NumericLimits<u32>::max());
  259. ++m_property_count;
  260. }
  261. }
  262. FLATTEN void Shape::add_property_without_transition(PropertyKey const& property_key, PropertyAttributes attributes)
  263. {
  264. add_property_without_transition(property_key.to_string_or_symbol(), attributes);
  265. }
  266. void Shape::set_property_attributes_without_transition(StringOrSymbol const& property_key, PropertyAttributes attributes)
  267. {
  268. VERIFY(is_dictionary());
  269. VERIFY(m_property_table);
  270. auto it = m_property_table->find(property_key);
  271. VERIFY(it != m_property_table->end());
  272. it->value.attributes = attributes;
  273. m_property_table->set(property_key, it->value);
  274. }
  275. void Shape::remove_property_without_transition(StringOrSymbol const& property_key, u32 offset)
  276. {
  277. VERIFY(is_uncacheable_dictionary());
  278. VERIFY(m_property_table);
  279. if (m_property_table->remove(property_key))
  280. --m_property_count;
  281. for (auto& it : *m_property_table) {
  282. VERIFY(it.value.offset != offset);
  283. if (it.value.offset > offset)
  284. --it.value.offset;
  285. }
  286. }
  287. NonnullGCPtr<Shape> Shape::create_for_prototype(NonnullGCPtr<Realm> realm, GCPtr<Object> prototype)
  288. {
  289. auto new_shape = realm->heap().allocate<Shape>(realm);
  290. s_all_prototype_shapes.set(new_shape);
  291. new_shape->m_is_prototype_shape = true;
  292. new_shape->m_prototype = prototype;
  293. new_shape->m_prototype_chain_validity = realm->heap().allocate<PrototypeChainValidity>();
  294. return new_shape;
  295. }
  296. NonnullGCPtr<Shape> Shape::clone_for_prototype()
  297. {
  298. VERIFY(!m_is_prototype_shape);
  299. VERIFY(!m_prototype_chain_validity);
  300. auto new_shape = heap().allocate<Shape>(m_realm);
  301. s_all_prototype_shapes.set(new_shape);
  302. new_shape->m_is_prototype_shape = true;
  303. new_shape->m_prototype = m_prototype;
  304. ensure_property_table();
  305. new_shape->ensure_property_table();
  306. (*new_shape->m_property_table) = *m_property_table;
  307. new_shape->m_property_count = new_shape->m_property_table->size();
  308. new_shape->m_prototype_chain_validity = heap().allocate<PrototypeChainValidity>();
  309. return new_shape;
  310. }
  311. void Shape::set_prototype_without_transition(Object* new_prototype)
  312. {
  313. VERIFY(new_prototype);
  314. new_prototype->convert_to_prototype_if_needed();
  315. m_prototype = new_prototype;
  316. }
  317. void Shape::set_prototype_shape()
  318. {
  319. VERIFY(!m_is_prototype_shape);
  320. s_all_prototype_shapes.set(this);
  321. m_is_prototype_shape = true;
  322. m_prototype_chain_validity = heap().allocate<PrototypeChainValidity>();
  323. }
  324. void Shape::invalidate_prototype_if_needed_for_new_prototype(NonnullGCPtr<Shape> new_prototype_shape)
  325. {
  326. if (!m_is_prototype_shape)
  327. return;
  328. new_prototype_shape->set_prototype_shape();
  329. m_prototype_chain_validity->set_valid(false);
  330. invalidate_all_prototype_chains_leading_to_this();
  331. }
  332. void Shape::invalidate_all_prototype_chains_leading_to_this()
  333. {
  334. HashTable<Shape*> shapes_to_invalidate;
  335. for (auto& candidate : s_all_prototype_shapes) {
  336. if (!candidate->m_prototype)
  337. continue;
  338. for (auto* current_prototype_shape = &candidate->m_prototype->shape(); current_prototype_shape; current_prototype_shape = current_prototype_shape->prototype() ? &current_prototype_shape->prototype()->shape() : nullptr) {
  339. if (current_prototype_shape == this) {
  340. VERIFY(candidate->m_is_prototype_shape);
  341. shapes_to_invalidate.set(candidate);
  342. break;
  343. }
  344. }
  345. }
  346. if (shapes_to_invalidate.is_empty())
  347. return;
  348. for (auto* shape : shapes_to_invalidate) {
  349. shape->m_prototype_chain_validity->set_valid(false);
  350. shape->m_prototype_chain_validity = heap().allocate<PrototypeChainValidity>();
  351. }
  352. }
  353. }