Shape.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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/GlobalObject.h>
  8. #include <LibJS/Runtime/Shape.h>
  9. namespace JS {
  10. Shape* Shape::create_unique_clone() const
  11. {
  12. VERIFY(m_global_object);
  13. auto* new_shape = heap().allocate_without_global_object<Shape>(*m_global_object);
  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(const StringOrSymbol& 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_global_object<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);
  59. return new_shape;
  60. }
  61. Shape* Shape::create_configure_transition(const StringOrSymbol& 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_global_object<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);
  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_global_object<Shape>(*this, new_prototype);
  77. if (!m_prototype_transitions)
  78. m_prototype_transitions = make<HashMap<Object*, WeakPtr<Shape>>>();
  79. m_prototype_transitions->set(new_prototype, new_shape);
  80. return new_shape;
  81. }
  82. Shape::Shape(ShapeWithoutGlobalObjectTag)
  83. {
  84. }
  85. Shape::Shape(Object& global_object)
  86. : m_global_object(&global_object)
  87. {
  88. }
  89. Shape::Shape(Shape& previous_shape, const StringOrSymbol& property_key, PropertyAttributes attributes, TransitionType transition_type)
  90. : m_global_object(previous_shape.m_global_object)
  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, Object* new_prototype)
  100. : m_global_object(previous_shape.m_global_object)
  101. , m_previous(&previous_shape)
  102. , m_prototype(new_prototype)
  103. , m_property_count(previous_shape.m_property_count)
  104. , m_transition_type(TransitionType::Prototype)
  105. {
  106. }
  107. Shape::~Shape()
  108. {
  109. }
  110. void Shape::visit_edges(Cell::Visitor& visitor)
  111. {
  112. Cell::visit_edges(visitor);
  113. visitor.visit(m_global_object);
  114. visitor.visit(m_prototype);
  115. visitor.visit(m_previous);
  116. m_property_key.visit_edges(visitor);
  117. if (m_property_table) {
  118. for (auto& it : *m_property_table)
  119. it.key.visit_edges(visitor);
  120. }
  121. }
  122. Optional<PropertyMetadata> Shape::lookup(const StringOrSymbol& 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 HashMap<StringOrSymbol, PropertyMetadata> const& Shape::property_table() const
  132. {
  133. ensure_property_table();
  134. return *m_property_table;
  135. }
  136. Vector<Shape::Property> Shape::property_table_ordered() const
  137. {
  138. auto vec = Vector<Shape::Property>();
  139. vec.resize(property_count());
  140. for (auto& it : property_table()) {
  141. vec[it.value.offset] = { it.key, it.value };
  142. }
  143. return vec;
  144. }
  145. void Shape::ensure_property_table() const
  146. {
  147. if (m_property_table)
  148. return;
  149. m_property_table = make<HashMap<StringOrSymbol, PropertyMetadata>>();
  150. u32 next_offset = 0;
  151. Vector<const Shape*, 64> transition_chain;
  152. for (auto* shape = m_previous; shape; shape = shape->m_previous) {
  153. if (shape->m_property_table) {
  154. *m_property_table = *shape->m_property_table;
  155. next_offset = shape->m_property_count;
  156. break;
  157. }
  158. transition_chain.append(shape);
  159. }
  160. transition_chain.append(this);
  161. for (ssize_t i = transition_chain.size() - 1; i >= 0; --i) {
  162. auto* shape = transition_chain[i];
  163. if (!shape->m_property_key.is_valid()) {
  164. // Ignore prototype transitions as they don't affect the key map.
  165. continue;
  166. }
  167. if (shape->m_transition_type == TransitionType::Put) {
  168. m_property_table->set(shape->m_property_key, { next_offset++, shape->m_attributes });
  169. } else if (shape->m_transition_type == TransitionType::Configure) {
  170. auto it = m_property_table->find(shape->m_property_key);
  171. VERIFY(it != m_property_table->end());
  172. it->value.attributes = shape->m_attributes;
  173. }
  174. }
  175. }
  176. void Shape::add_property_to_unique_shape(const StringOrSymbol& property_key, PropertyAttributes attributes)
  177. {
  178. VERIFY(is_unique());
  179. VERIFY(m_property_table);
  180. VERIFY(!m_property_table->contains(property_key));
  181. m_property_table->set(property_key, { static_cast<u32>(m_property_table->size()), attributes });
  182. VERIFY(m_property_count < NumericLimits<u32>::max());
  183. ++m_property_count;
  184. }
  185. void Shape::reconfigure_property_in_unique_shape(const StringOrSymbol& property_key, PropertyAttributes attributes)
  186. {
  187. VERIFY(is_unique());
  188. VERIFY(m_property_table);
  189. auto it = m_property_table->find(property_key);
  190. VERIFY(it != m_property_table->end());
  191. it->value.attributes = attributes;
  192. m_property_table->set(property_key, it->value);
  193. }
  194. void Shape::remove_property_from_unique_shape(const StringOrSymbol& property_key, size_t offset)
  195. {
  196. VERIFY(is_unique());
  197. VERIFY(m_property_table);
  198. if (m_property_table->remove(property_key))
  199. --m_property_count;
  200. for (auto& it : *m_property_table) {
  201. VERIFY(it.value.offset != offset);
  202. if (it.value.offset > offset)
  203. --it.value.offset;
  204. }
  205. }
  206. void Shape::add_property_without_transition(StringOrSymbol const& property_key, PropertyAttributes attributes)
  207. {
  208. VERIFY(property_key.is_valid());
  209. ensure_property_table();
  210. if (m_property_table->set(property_key, { m_property_count, attributes }) == AK::HashSetResult::InsertedNewEntry) {
  211. VERIFY(m_property_count < NumericLimits<u32>::max());
  212. ++m_property_count;
  213. }
  214. }
  215. FLATTEN void Shape::add_property_without_transition(PropertyKey const& property_key, PropertyAttributes attributes)
  216. {
  217. VERIFY(property_key.is_valid());
  218. add_property_without_transition(property_key.to_string_or_symbol(), attributes);
  219. }
  220. }