Shape.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. auto it = m_forward_transitions.find(key);
  25. if (it == m_forward_transitions.end())
  26. return nullptr;
  27. if (!it->value) {
  28. // The cached forward transition has gone stale (from garbage collection). Prune it.
  29. m_forward_transitions.remove(it);
  30. return nullptr;
  31. }
  32. return it->value;
  33. }
  34. Shape* Shape::get_or_prune_cached_prototype_transition(Object* prototype)
  35. {
  36. auto it = m_prototype_transitions.find(prototype);
  37. if (it == m_prototype_transitions.end())
  38. return nullptr;
  39. if (!it->value) {
  40. // The cached prototype transition has gone stale (from garbage collection). Prune it.
  41. m_prototype_transitions.remove(it);
  42. return nullptr;
  43. }
  44. return it->value;
  45. }
  46. Shape* Shape::create_put_transition(const StringOrSymbol& property_name, PropertyAttributes attributes)
  47. {
  48. TransitionKey key { property_name, attributes };
  49. if (auto* existing_shape = get_or_prune_cached_forward_transition(key))
  50. return existing_shape;
  51. auto* new_shape = heap().allocate_without_global_object<Shape>(*this, property_name, attributes, TransitionType::Put);
  52. m_forward_transitions.set(key, new_shape);
  53. return new_shape;
  54. }
  55. Shape* Shape::create_configure_transition(const StringOrSymbol& property_name, PropertyAttributes attributes)
  56. {
  57. TransitionKey key { property_name, attributes };
  58. if (auto* existing_shape = get_or_prune_cached_forward_transition(key))
  59. return existing_shape;
  60. auto* new_shape = heap().allocate_without_global_object<Shape>(*this, property_name, attributes, TransitionType::Configure);
  61. m_forward_transitions.set(key, new_shape);
  62. return new_shape;
  63. }
  64. Shape* Shape::create_prototype_transition(Object* new_prototype)
  65. {
  66. if (auto* existing_shape = get_or_prune_cached_prototype_transition(new_prototype))
  67. return existing_shape;
  68. auto* new_shape = heap().allocate_without_global_object<Shape>(*this, new_prototype);
  69. m_prototype_transitions.set(new_prototype, new_shape);
  70. return new_shape;
  71. }
  72. Shape::Shape(ShapeWithoutGlobalObjectTag)
  73. {
  74. }
  75. Shape::Shape(Object& global_object)
  76. : m_global_object(&global_object)
  77. {
  78. }
  79. Shape::Shape(Shape& previous_shape, const StringOrSymbol& property_name, PropertyAttributes attributes, TransitionType transition_type)
  80. : m_attributes(attributes)
  81. , m_transition_type(transition_type)
  82. , m_global_object(previous_shape.m_global_object)
  83. , m_previous(&previous_shape)
  84. , m_property_name(property_name)
  85. , m_prototype(previous_shape.m_prototype)
  86. , m_property_count(transition_type == TransitionType::Put ? previous_shape.m_property_count + 1 : previous_shape.m_property_count)
  87. {
  88. }
  89. Shape::Shape(Shape& previous_shape, Object* new_prototype)
  90. : m_transition_type(TransitionType::Prototype)
  91. , m_global_object(previous_shape.m_global_object)
  92. , m_previous(&previous_shape)
  93. , m_prototype(new_prototype)
  94. , m_property_count(previous_shape.m_property_count)
  95. {
  96. }
  97. Shape::~Shape()
  98. {
  99. }
  100. void Shape::visit_edges(Cell::Visitor& visitor)
  101. {
  102. Cell::visit_edges(visitor);
  103. visitor.visit(m_global_object);
  104. visitor.visit(m_prototype);
  105. visitor.visit(m_previous);
  106. m_property_name.visit_edges(visitor);
  107. if (m_property_table) {
  108. for (auto& it : *m_property_table)
  109. it.key.visit_edges(visitor);
  110. }
  111. }
  112. Optional<PropertyMetadata> Shape::lookup(const StringOrSymbol& property_name) const
  113. {
  114. if (m_property_count == 0)
  115. return {};
  116. auto property = property_table().get(property_name);
  117. if (!property.has_value())
  118. return {};
  119. return property;
  120. }
  121. FLATTEN HashMap<StringOrSymbol, PropertyMetadata> const& Shape::property_table() const
  122. {
  123. ensure_property_table();
  124. return *m_property_table;
  125. }
  126. size_t Shape::property_count() const
  127. {
  128. return m_property_count;
  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_name.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_name, { next_offset++, shape->m_attributes });
  163. } else if (shape->m_transition_type == TransitionType::Configure) {
  164. auto it = m_property_table->find(shape->m_property_name);
  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_name, PropertyAttributes attributes)
  171. {
  172. VERIFY(is_unique());
  173. VERIFY(m_property_table);
  174. VERIFY(!m_property_table->contains(property_name));
  175. m_property_table->set(property_name, { m_property_table->size(), attributes });
  176. ++m_property_count;
  177. }
  178. void Shape::reconfigure_property_in_unique_shape(const StringOrSymbol& property_name, PropertyAttributes attributes)
  179. {
  180. VERIFY(is_unique());
  181. VERIFY(m_property_table);
  182. auto it = m_property_table->find(property_name);
  183. VERIFY(it != m_property_table->end());
  184. it->value.attributes = attributes;
  185. m_property_table->set(property_name, it->value);
  186. }
  187. void Shape::remove_property_from_unique_shape(const StringOrSymbol& property_name, size_t offset)
  188. {
  189. VERIFY(is_unique());
  190. VERIFY(m_property_table);
  191. if (m_property_table->remove(property_name))
  192. --m_property_count;
  193. for (auto& it : *m_property_table) {
  194. VERIFY(it.value.offset != offset);
  195. if (it.value.offset > offset)
  196. --it.value.offset;
  197. }
  198. }
  199. void Shape::add_property_without_transition(StringOrSymbol const& property_name, PropertyAttributes attributes)
  200. {
  201. VERIFY(property_name.is_valid());
  202. ensure_property_table();
  203. if (m_property_table->set(property_name, { m_property_count, attributes }) == AK::HashSetResult::InsertedNewEntry)
  204. ++m_property_count;
  205. }
  206. FLATTEN void Shape::add_property_without_transition(PropertyName const& property_name, PropertyAttributes attributes)
  207. {
  208. VERIFY(property_name.is_valid());
  209. add_property_without_transition(property_name.to_string_or_symbol(), attributes);
  210. }
  211. #ifdef JS_TRACK_ZOMBIE_CELLS
  212. void Shape::did_become_zombie()
  213. {
  214. revoke_weak_ptrs();
  215. }
  216. #endif
  217. }