Shape.cpp 8.1 KB

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