Shape.cpp 7.9 KB

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