Shape.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibJS/Heap/DeferGC.h>
  27. #include <LibJS/Runtime/GlobalObject.h>
  28. #include <LibJS/Runtime/Shape.h>
  29. namespace JS {
  30. Shape* Shape::create_unique_clone() const
  31. {
  32. ASSERT(m_global_object);
  33. auto* new_shape = heap().allocate_without_global_object<Shape>(*m_global_object);
  34. new_shape->m_unique = true;
  35. new_shape->m_prototype = m_prototype;
  36. ensure_property_table();
  37. new_shape->ensure_property_table();
  38. (*new_shape->m_property_table) = *m_property_table;
  39. new_shape->m_property_count = new_shape->m_property_table->size();
  40. return new_shape;
  41. }
  42. Shape* Shape::create_put_transition(const StringOrSymbol& property_name, PropertyAttributes attributes)
  43. {
  44. TransitionKey key { property_name, attributes };
  45. if (auto* existing_shape = m_forward_transitions.get(key).value_or(nullptr))
  46. return existing_shape;
  47. auto* new_shape = heap().allocate_without_global_object<Shape>(*this, property_name, attributes, TransitionType::Put);
  48. m_forward_transitions.set(key, new_shape);
  49. return new_shape;
  50. }
  51. Shape* Shape::create_configure_transition(const StringOrSymbol& property_name, PropertyAttributes attributes)
  52. {
  53. TransitionKey key { property_name, attributes };
  54. if (auto* existing_shape = m_forward_transitions.get(key).value_or(nullptr))
  55. return existing_shape;
  56. auto* new_shape = heap().allocate_without_global_object<Shape>(*this, property_name, attributes, TransitionType::Configure);
  57. m_forward_transitions.set(key, new_shape);
  58. return new_shape;
  59. }
  60. Shape* Shape::create_prototype_transition(Object* new_prototype)
  61. {
  62. return heap().allocate_without_global_object<Shape>(*this, new_prototype);
  63. }
  64. Shape::Shape(ShapeWithoutGlobalObjectTag)
  65. {
  66. }
  67. Shape::Shape(Object& global_object)
  68. : m_global_object(&global_object)
  69. {
  70. }
  71. Shape::Shape(Shape& previous_shape, const StringOrSymbol& property_name, PropertyAttributes attributes, TransitionType transition_type)
  72. : m_attributes(attributes)
  73. , m_transition_type(transition_type)
  74. , m_global_object(previous_shape.m_global_object)
  75. , m_previous(&previous_shape)
  76. , m_property_name(property_name)
  77. , m_prototype(previous_shape.m_prototype)
  78. , m_property_count(transition_type == TransitionType::Put ? previous_shape.m_property_count + 1 : previous_shape.m_property_count)
  79. {
  80. }
  81. Shape::Shape(Shape& previous_shape, Object* new_prototype)
  82. : m_transition_type(TransitionType::Prototype)
  83. , m_global_object(previous_shape.m_global_object)
  84. , m_previous(&previous_shape)
  85. , m_prototype(new_prototype)
  86. , m_property_count(previous_shape.m_property_count)
  87. {
  88. }
  89. Shape::~Shape()
  90. {
  91. }
  92. void Shape::visit_edges(Cell::Visitor& visitor)
  93. {
  94. Cell::visit_edges(visitor);
  95. visitor.visit(m_global_object);
  96. visitor.visit(m_prototype);
  97. visitor.visit(m_previous);
  98. m_property_name.visit_edges(visitor);
  99. for (auto& it : m_forward_transitions)
  100. visitor.visit(it.value);
  101. if (m_property_table) {
  102. for (auto& it : *m_property_table)
  103. it.key.visit_edges(visitor);
  104. }
  105. }
  106. Optional<PropertyMetadata> Shape::lookup(const StringOrSymbol& property_name) const
  107. {
  108. if (m_property_count == 0)
  109. return {};
  110. auto property = property_table().get(property_name);
  111. if (!property.has_value())
  112. return {};
  113. return property;
  114. }
  115. const HashMap<StringOrSymbol, PropertyMetadata>& Shape::property_table() const
  116. {
  117. ensure_property_table();
  118. return *m_property_table;
  119. }
  120. size_t Shape::property_count() const
  121. {
  122. return m_property_count;
  123. }
  124. Vector<Shape::Property> Shape::property_table_ordered() const
  125. {
  126. auto vec = Vector<Shape::Property>();
  127. vec.resize(property_count());
  128. for (auto& it : property_table()) {
  129. vec[it.value.offset] = { it.key, it.value };
  130. }
  131. return vec;
  132. }
  133. void Shape::ensure_property_table() const
  134. {
  135. if (m_property_table)
  136. return;
  137. m_property_table = make<HashMap<StringOrSymbol, PropertyMetadata>>();
  138. u32 next_offset = 0;
  139. Vector<const Shape*, 64> transition_chain;
  140. for (auto* shape = m_previous; shape; shape = shape->m_previous) {
  141. if (shape->m_property_table) {
  142. *m_property_table = *shape->m_property_table;
  143. next_offset = shape->m_property_count;
  144. break;
  145. }
  146. transition_chain.append(shape);
  147. }
  148. transition_chain.append(this);
  149. for (ssize_t i = transition_chain.size() - 1; i >= 0; --i) {
  150. auto* shape = transition_chain[i];
  151. if (!shape->m_property_name.is_valid()) {
  152. // Ignore prototype transitions as they don't affect the key map.
  153. continue;
  154. }
  155. if (shape->m_transition_type == TransitionType::Put) {
  156. m_property_table->set(shape->m_property_name, { next_offset++, shape->m_attributes });
  157. } else if (shape->m_transition_type == TransitionType::Configure) {
  158. auto it = m_property_table->find(shape->m_property_name);
  159. ASSERT(it != m_property_table->end());
  160. it->value.attributes = shape->m_attributes;
  161. }
  162. }
  163. }
  164. void Shape::add_property_to_unique_shape(const StringOrSymbol& property_name, PropertyAttributes attributes)
  165. {
  166. ASSERT(is_unique());
  167. ASSERT(m_property_table);
  168. ASSERT(!m_property_table->contains(property_name));
  169. m_property_table->set(property_name, { m_property_table->size(), attributes });
  170. ++m_property_count;
  171. }
  172. void Shape::reconfigure_property_in_unique_shape(const StringOrSymbol& property_name, PropertyAttributes attributes)
  173. {
  174. ASSERT(is_unique());
  175. ASSERT(m_property_table);
  176. auto it = m_property_table->find(property_name);
  177. ASSERT(it != m_property_table->end());
  178. it->value.attributes = attributes;
  179. m_property_table->set(property_name, it->value);
  180. }
  181. void Shape::remove_property_from_unique_shape(const StringOrSymbol& property_name, size_t offset)
  182. {
  183. ASSERT(is_unique());
  184. ASSERT(m_property_table);
  185. if (m_property_table->remove(property_name))
  186. --m_property_count;
  187. for (auto& it : *m_property_table) {
  188. ASSERT(it.value.offset != offset);
  189. if (it.value.offset > offset)
  190. --it.value.offset;
  191. }
  192. }
  193. void Shape::add_property_without_transition(const StringOrSymbol& property_name, PropertyAttributes attributes)
  194. {
  195. ensure_property_table();
  196. if (m_property_table->set(property_name, { m_property_count, attributes }) == AK::HashSetResult::InsertedNewEntry)
  197. ++m_property_count;
  198. }
  199. }