Shape.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/Interpreter.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<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. return new_shape;
  39. }
  40. Shape* Shape::create_put_transition(const FlyString& property_name, PropertyAttributes attributes)
  41. {
  42. TransitionKey key { property_name, attributes };
  43. if (auto* existing_shape = m_forward_transitions.get(key).value_or(nullptr))
  44. return existing_shape;
  45. auto* new_shape = heap().allocate<Shape>(*this, property_name, attributes, TransitionType::Put);
  46. m_forward_transitions.set(key, new_shape);
  47. return new_shape;
  48. }
  49. Shape* Shape::create_configure_transition(const FlyString& property_name, PropertyAttributes attributes)
  50. {
  51. TransitionKey key { property_name, attributes };
  52. if (auto* existing_shape = m_forward_transitions.get(key).value_or(nullptr))
  53. return existing_shape;
  54. auto* new_shape = heap().allocate<Shape>(*this, property_name, attributes, TransitionType::Configure);
  55. m_forward_transitions.set(key, new_shape);
  56. return new_shape;
  57. }
  58. Shape* Shape::create_prototype_transition(Object* new_prototype)
  59. {
  60. return heap().allocate<Shape>(*this, new_prototype);
  61. }
  62. Shape::Shape(GlobalObject& global_object)
  63. : m_global_object(global_object)
  64. {
  65. }
  66. Shape::Shape(Shape& previous_shape, const FlyString& property_name, PropertyAttributes attributes, TransitionType transition_type)
  67. : m_global_object(previous_shape.m_global_object)
  68. , m_previous(&previous_shape)
  69. , m_property_name(property_name)
  70. , m_attributes(attributes)
  71. , m_prototype(previous_shape.m_prototype)
  72. , m_transition_type(transition_type)
  73. {
  74. }
  75. Shape::Shape(Shape& previous_shape, Object* new_prototype)
  76. : m_global_object(previous_shape.m_global_object)
  77. , m_previous(&previous_shape)
  78. , m_prototype(new_prototype)
  79. , m_transition_type(TransitionType::Prototype)
  80. {
  81. }
  82. Shape::~Shape()
  83. {
  84. }
  85. void Shape::visit_children(Cell::Visitor& visitor)
  86. {
  87. Cell::visit_children(visitor);
  88. visitor.visit(&m_global_object);
  89. visitor.visit(m_prototype);
  90. visitor.visit(m_previous);
  91. for (auto& it : m_forward_transitions)
  92. visitor.visit(it.value);
  93. }
  94. Optional<PropertyMetadata> Shape::lookup(const FlyString& property_name) const
  95. {
  96. auto property = property_table().get(property_name);
  97. if (!property.has_value())
  98. return {};
  99. return property;
  100. }
  101. const HashMap<FlyString, PropertyMetadata>& Shape::property_table() const
  102. {
  103. ensure_property_table();
  104. return *m_property_table;
  105. }
  106. size_t Shape::property_count() const
  107. {
  108. return property_table().size();
  109. }
  110. Vector<Shape::Property> Shape::property_table_ordered() const
  111. {
  112. auto vec = Vector<Shape::Property>();
  113. vec.resize(property_table().size());
  114. for (auto& it : property_table()) {
  115. vec[it.value.offset] = { it.key, it.value };
  116. }
  117. return vec;
  118. }
  119. void Shape::ensure_property_table() const
  120. {
  121. if (m_property_table)
  122. return;
  123. m_property_table = make<HashMap<FlyString, PropertyMetadata>>();
  124. // FIXME: We need to make sure the GC doesn't collect the transition chain as we're building it.
  125. // Maybe some kind of RAII "prevent GC for a moment" helper thingy?
  126. Vector<const Shape*> transition_chain;
  127. for (auto* shape = this; shape->m_previous; shape = shape->m_previous) {
  128. transition_chain.append(shape);
  129. }
  130. u32 next_offset = 0;
  131. for (ssize_t i = transition_chain.size() - 1; i >= 0; --i) {
  132. auto* shape = transition_chain[i];
  133. if (shape->m_property_name.is_null()) {
  134. // Ignore prototype transitions as they don't affect the key map.
  135. continue;
  136. }
  137. if (shape->m_transition_type == TransitionType::Put) {
  138. m_property_table->set(shape->m_property_name, { next_offset++, shape->m_attributes });
  139. } else if (shape->m_transition_type == TransitionType::Configure) {
  140. auto it = m_property_table->find(shape->m_property_name);
  141. ASSERT(it != m_property_table->end());
  142. it->value.attributes = shape->m_attributes;
  143. }
  144. }
  145. }
  146. void Shape::add_property_to_unique_shape(const FlyString& property_name, PropertyAttributes attributes)
  147. {
  148. ASSERT(is_unique());
  149. ASSERT(m_property_table);
  150. ASSERT(!m_property_table->contains(property_name));
  151. m_property_table->set(property_name, { m_property_table->size(), attributes });
  152. }
  153. void Shape::reconfigure_property_in_unique_shape(const FlyString& property_name, PropertyAttributes attributes)
  154. {
  155. ASSERT(is_unique());
  156. ASSERT(m_property_table);
  157. ASSERT(m_property_table->contains(property_name));
  158. m_property_table->set(property_name, { m_property_table->size(), attributes });
  159. }
  160. void Shape::remove_property_from_unique_shape(const FlyString& property_name, size_t offset)
  161. {
  162. ASSERT(is_unique());
  163. ASSERT(m_property_table);
  164. m_property_table->remove(property_name);
  165. for (auto& it : *m_property_table) {
  166. ASSERT(it.value.offset != offset);
  167. if (it.value.offset > offset)
  168. --it.value.offset;
  169. }
  170. }
  171. }