Shape.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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::create_put_transition(const StringOrSymbol& property_name, PropertyAttributes attributes)
  35. {
  36. TransitionKey key { property_name, attributes };
  37. if (auto* existing_shape = get_or_prune_cached_forward_transition(key))
  38. return existing_shape;
  39. auto* new_shape = heap().allocate_without_global_object<Shape>(*this, property_name, attributes, TransitionType::Put);
  40. m_forward_transitions.set(key, new_shape);
  41. return new_shape;
  42. }
  43. Shape* Shape::create_configure_transition(const StringOrSymbol& property_name, PropertyAttributes attributes)
  44. {
  45. TransitionKey key { property_name, attributes };
  46. if (auto* existing_shape = get_or_prune_cached_forward_transition(key))
  47. return existing_shape;
  48. auto* new_shape = heap().allocate_without_global_object<Shape>(*this, property_name, attributes, TransitionType::Configure);
  49. m_forward_transitions.set(key, new_shape);
  50. return new_shape;
  51. }
  52. Shape* Shape::create_prototype_transition(Object* new_prototype)
  53. {
  54. return heap().allocate_without_global_object<Shape>(*this, new_prototype);
  55. }
  56. Shape::Shape(ShapeWithoutGlobalObjectTag)
  57. {
  58. }
  59. Shape::Shape(Object& global_object)
  60. : m_global_object(&global_object)
  61. {
  62. }
  63. Shape::Shape(Shape& previous_shape, const StringOrSymbol& property_name, PropertyAttributes attributes, TransitionType transition_type)
  64. : m_attributes(attributes)
  65. , m_transition_type(transition_type)
  66. , m_global_object(previous_shape.m_global_object)
  67. , m_previous(&previous_shape)
  68. , m_property_name(property_name)
  69. , m_prototype(previous_shape.m_prototype)
  70. , m_property_count(transition_type == TransitionType::Put ? previous_shape.m_property_count + 1 : previous_shape.m_property_count)
  71. {
  72. }
  73. Shape::Shape(Shape& previous_shape, Object* new_prototype)
  74. : m_transition_type(TransitionType::Prototype)
  75. , m_global_object(previous_shape.m_global_object)
  76. , m_previous(&previous_shape)
  77. , m_prototype(new_prototype)
  78. , m_property_count(previous_shape.m_property_count)
  79. {
  80. }
  81. Shape::~Shape()
  82. {
  83. }
  84. void Shape::visit_edges(Cell::Visitor& visitor)
  85. {
  86. Cell::visit_edges(visitor);
  87. visitor.visit(m_global_object);
  88. visitor.visit(m_prototype);
  89. visitor.visit(m_previous);
  90. m_property_name.visit_edges(visitor);
  91. if (m_property_table) {
  92. for (auto& it : *m_property_table)
  93. it.key.visit_edges(visitor);
  94. }
  95. }
  96. Optional<PropertyMetadata> Shape::lookup(const StringOrSymbol& property_name) const
  97. {
  98. if (m_property_count == 0)
  99. return {};
  100. auto property = property_table().get(property_name);
  101. if (!property.has_value())
  102. return {};
  103. return property;
  104. }
  105. FLATTEN HashMap<StringOrSymbol, PropertyMetadata> const& Shape::property_table() const
  106. {
  107. ensure_property_table();
  108. return *m_property_table;
  109. }
  110. size_t Shape::property_count() const
  111. {
  112. return m_property_count;
  113. }
  114. Vector<Shape::Property> Shape::property_table_ordered() const
  115. {
  116. auto vec = Vector<Shape::Property>();
  117. vec.resize(property_count());
  118. for (auto& it : property_table()) {
  119. vec[it.value.offset] = { it.key, it.value };
  120. }
  121. return vec;
  122. }
  123. void Shape::ensure_property_table() const
  124. {
  125. if (m_property_table)
  126. return;
  127. m_property_table = make<HashMap<StringOrSymbol, PropertyMetadata>>();
  128. u32 next_offset = 0;
  129. Vector<const Shape*, 64> transition_chain;
  130. for (auto* shape = m_previous; shape; shape = shape->m_previous) {
  131. if (shape->m_property_table) {
  132. *m_property_table = *shape->m_property_table;
  133. next_offset = shape->m_property_count;
  134. break;
  135. }
  136. transition_chain.append(shape);
  137. }
  138. transition_chain.append(this);
  139. for (ssize_t i = transition_chain.size() - 1; i >= 0; --i) {
  140. auto* shape = transition_chain[i];
  141. if (!shape->m_property_name.is_valid()) {
  142. // Ignore prototype transitions as they don't affect the key map.
  143. continue;
  144. }
  145. if (shape->m_transition_type == TransitionType::Put) {
  146. m_property_table->set(shape->m_property_name, { next_offset++, shape->m_attributes });
  147. } else if (shape->m_transition_type == TransitionType::Configure) {
  148. auto it = m_property_table->find(shape->m_property_name);
  149. VERIFY(it != m_property_table->end());
  150. it->value.attributes = shape->m_attributes;
  151. }
  152. }
  153. }
  154. void Shape::add_property_to_unique_shape(const StringOrSymbol& property_name, PropertyAttributes attributes)
  155. {
  156. VERIFY(is_unique());
  157. VERIFY(m_property_table);
  158. VERIFY(!m_property_table->contains(property_name));
  159. m_property_table->set(property_name, { m_property_table->size(), attributes });
  160. ++m_property_count;
  161. }
  162. void Shape::reconfigure_property_in_unique_shape(const StringOrSymbol& property_name, PropertyAttributes attributes)
  163. {
  164. VERIFY(is_unique());
  165. VERIFY(m_property_table);
  166. auto it = m_property_table->find(property_name);
  167. VERIFY(it != m_property_table->end());
  168. it->value.attributes = attributes;
  169. m_property_table->set(property_name, it->value);
  170. }
  171. void Shape::remove_property_from_unique_shape(const StringOrSymbol& property_name, size_t offset)
  172. {
  173. VERIFY(is_unique());
  174. VERIFY(m_property_table);
  175. if (m_property_table->remove(property_name))
  176. --m_property_count;
  177. for (auto& it : *m_property_table) {
  178. VERIFY(it.value.offset != offset);
  179. if (it.value.offset > offset)
  180. --it.value.offset;
  181. }
  182. }
  183. void Shape::add_property_without_transition(StringOrSymbol const& property_name, PropertyAttributes attributes)
  184. {
  185. VERIFY(property_name.is_valid());
  186. ensure_property_table();
  187. if (m_property_table->set(property_name, { m_property_count, attributes }) == AK::HashSetResult::InsertedNewEntry)
  188. ++m_property_count;
  189. }
  190. FLATTEN void Shape::add_property_without_transition(PropertyName const& property_name, PropertyAttributes attributes)
  191. {
  192. VERIFY(property_name.is_valid());
  193. add_property_without_transition(property_name.to_string_or_symbol(), attributes);
  194. }
  195. void Shape::did_become_zombie()
  196. {
  197. revoke_weak_ptrs();
  198. }
  199. }