Shape.cpp 7.6 KB

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