Shape.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/Shape.h>
  28. namespace JS {
  29. Shape* Shape::create_put_transition(const FlyString& property_name, u8 attributes)
  30. {
  31. auto* new_shape = m_forward_transitions.get(property_name).value_or(nullptr);
  32. if (new_shape && new_shape->m_attributes == attributes)
  33. return new_shape;
  34. new_shape = heap().allocate<Shape>(this, property_name, attributes, TransitionType::Put);
  35. m_forward_transitions.set(property_name, new_shape);
  36. return new_shape;
  37. }
  38. Shape* Shape::create_configure_transition(const FlyString& property_name, u8 attributes)
  39. {
  40. auto* new_shape = m_forward_transitions.get(property_name).value_or(nullptr);
  41. if (new_shape && new_shape->m_attributes == attributes)
  42. return new_shape;
  43. new_shape = heap().allocate<Shape>(this, property_name, attributes, TransitionType::Configure);
  44. m_forward_transitions.set(property_name, new_shape);
  45. return new_shape;
  46. }
  47. Shape* Shape::create_prototype_transition(Object* new_prototype)
  48. {
  49. return heap().allocate<Shape>(this, new_prototype);
  50. }
  51. Shape::Shape()
  52. {
  53. }
  54. Shape::Shape(Shape* previous_shape, const FlyString& property_name, u8 attributes, TransitionType transition_type)
  55. : m_previous(previous_shape)
  56. , m_property_name(property_name)
  57. , m_attributes(attributes)
  58. , m_prototype(previous_shape->m_prototype)
  59. , m_transition_type(transition_type)
  60. {
  61. }
  62. Shape::Shape(Shape* previous_shape, Object* new_prototype)
  63. : m_previous(previous_shape)
  64. , m_prototype(new_prototype)
  65. , m_transition_type(TransitionType::Prototype)
  66. {
  67. }
  68. Shape::~Shape()
  69. {
  70. }
  71. void Shape::visit_children(Cell::Visitor& visitor)
  72. {
  73. Cell::visit_children(visitor);
  74. if (m_prototype)
  75. visitor.visit(m_prototype);
  76. if (m_previous)
  77. visitor.visit(m_previous);
  78. for (auto& it : m_forward_transitions)
  79. visitor.visit(it.value);
  80. }
  81. Optional<PropertyMetadata> Shape::lookup(const FlyString& property_name) const
  82. {
  83. return property_table().get(property_name);
  84. }
  85. const HashMap<FlyString, PropertyMetadata>& Shape::property_table() const
  86. {
  87. ensure_property_table();
  88. return *m_property_table;
  89. }
  90. size_t Shape::property_count() const
  91. {
  92. return property_table().size();
  93. }
  94. void Shape::ensure_property_table() const
  95. {
  96. if (m_property_table)
  97. return;
  98. m_property_table = make<HashMap<FlyString, PropertyMetadata>>();
  99. // FIXME: We need to make sure the GC doesn't collect the transition chain as we're building it.
  100. // Maybe some kind of RAII "prevent GC for a moment" helper thingy?
  101. Vector<const Shape*> transition_chain;
  102. for (auto* shape = this; shape->m_previous; shape = shape->m_previous) {
  103. transition_chain.append(shape);
  104. }
  105. u32 next_offset = 0;
  106. for (ssize_t i = transition_chain.size() - 1; i >= 0; --i) {
  107. auto* shape = transition_chain[i];
  108. if (shape->m_property_name.is_null()) {
  109. // Ignore prototype transitions as they don't affect the key map.
  110. continue;
  111. }
  112. if (shape->m_transition_type == TransitionType::Put) {
  113. m_property_table->set(shape->m_property_name, { next_offset++, shape->m_attributes });
  114. } else if (shape->m_transition_type == TransitionType::Configure) {
  115. auto it = m_property_table->find(shape->m_property_name);
  116. ASSERT(it != m_property_table->end());
  117. it->value.attributes = shape->m_attributes;
  118. }
  119. }
  120. }
  121. }