Shape.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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(Object& global_object)
  83. : m_global_object(&global_object)
  84. {
  85. }
  86. Shape::Shape(Shape& previous_shape, const StringOrSymbol& property_key, PropertyAttributes attributes, TransitionType transition_type)
  87. : m_global_object(previous_shape.m_global_object)
  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_global_object(previous_shape.m_global_object)
  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. Cell::visit_edges(visitor);
  107. visitor.visit(m_global_object);
  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. }
  116. Optional<PropertyMetadata> Shape::lookup(const StringOrSymbol& property_key) const
  117. {
  118. if (m_property_count == 0)
  119. return {};
  120. auto property = property_table().get(property_key);
  121. if (!property.has_value())
  122. return {};
  123. return property;
  124. }
  125. FLATTEN HashMap<StringOrSymbol, PropertyMetadata> const& Shape::property_table() const
  126. {
  127. ensure_property_table();
  128. return *m_property_table;
  129. }
  130. Vector<Shape::Property> Shape::property_table_ordered() const
  131. {
  132. auto vec = Vector<Shape::Property>();
  133. vec.resize(property_count());
  134. for (auto& it : property_table()) {
  135. vec[it.value.offset] = { it.key, it.value };
  136. }
  137. return vec;
  138. }
  139. void Shape::ensure_property_table() const
  140. {
  141. if (m_property_table)
  142. return;
  143. m_property_table = make<HashMap<StringOrSymbol, PropertyMetadata>>();
  144. u32 next_offset = 0;
  145. Vector<const Shape*, 64> transition_chain;
  146. for (auto* shape = m_previous; shape; shape = shape->m_previous) {
  147. if (shape->m_property_table) {
  148. *m_property_table = *shape->m_property_table;
  149. next_offset = shape->m_property_count;
  150. break;
  151. }
  152. transition_chain.append(shape);
  153. }
  154. transition_chain.append(this);
  155. for (ssize_t i = transition_chain.size() - 1; i >= 0; --i) {
  156. auto* shape = transition_chain[i];
  157. if (!shape->m_property_key.is_valid()) {
  158. // Ignore prototype transitions as they don't affect the key map.
  159. continue;
  160. }
  161. if (shape->m_transition_type == TransitionType::Put) {
  162. m_property_table->set(shape->m_property_key, { next_offset++, shape->m_attributes });
  163. } else if (shape->m_transition_type == TransitionType::Configure) {
  164. auto it = m_property_table->find(shape->m_property_key);
  165. VERIFY(it != m_property_table->end());
  166. it->value.attributes = shape->m_attributes;
  167. }
  168. }
  169. }
  170. void Shape::add_property_to_unique_shape(const StringOrSymbol& property_key, PropertyAttributes attributes)
  171. {
  172. VERIFY(is_unique());
  173. VERIFY(m_property_table);
  174. VERIFY(!m_property_table->contains(property_key));
  175. m_property_table->set(property_key, { static_cast<u32>(m_property_table->size()), attributes });
  176. VERIFY(m_property_count < NumericLimits<u32>::max());
  177. ++m_property_count;
  178. }
  179. void Shape::reconfigure_property_in_unique_shape(const StringOrSymbol& property_key, PropertyAttributes attributes)
  180. {
  181. VERIFY(is_unique());
  182. VERIFY(m_property_table);
  183. auto it = m_property_table->find(property_key);
  184. VERIFY(it != m_property_table->end());
  185. it->value.attributes = attributes;
  186. m_property_table->set(property_key, it->value);
  187. }
  188. void Shape::remove_property_from_unique_shape(const StringOrSymbol& property_key, size_t offset)
  189. {
  190. VERIFY(is_unique());
  191. VERIFY(m_property_table);
  192. if (m_property_table->remove(property_key))
  193. --m_property_count;
  194. for (auto& it : *m_property_table) {
  195. VERIFY(it.value.offset != offset);
  196. if (it.value.offset > offset)
  197. --it.value.offset;
  198. }
  199. }
  200. void Shape::add_property_without_transition(StringOrSymbol const& property_key, PropertyAttributes attributes)
  201. {
  202. VERIFY(property_key.is_valid());
  203. ensure_property_table();
  204. if (m_property_table->set(property_key, { m_property_count, attributes }) == AK::HashSetResult::InsertedNewEntry) {
  205. VERIFY(m_property_count < NumericLimits<u32>::max());
  206. ++m_property_count;
  207. }
  208. }
  209. FLATTEN void Shape::add_property_without_transition(PropertyKey const& property_key, PropertyAttributes attributes)
  210. {
  211. VERIFY(property_key.is_valid());
  212. add_property_without_transition(property_key.to_string_or_symbol(), attributes);
  213. }
  214. }