Shape.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include <LibJS/Interpreter.h>
  2. #include <LibJS/Runtime/Shape.h>
  3. namespace JS {
  4. Shape* Shape::create_put_transition(const FlyString& property_name, u8 property_attributes)
  5. {
  6. auto* new_shape = m_forward_transitions.get(property_name).value_or(nullptr);
  7. if (new_shape && new_shape->m_property_attributes == property_attributes)
  8. return new_shape;
  9. new_shape = heap().allocate<Shape>(this, property_name, property_attributes);
  10. m_forward_transitions.set(property_name, new_shape);
  11. return new_shape;
  12. }
  13. Shape* Shape::create_prototype_transition(Object* new_prototype)
  14. {
  15. return heap().allocate<Shape>(this, new_prototype);
  16. }
  17. Shape::Shape()
  18. {
  19. }
  20. Shape::Shape(Shape* previous_shape, const FlyString& property_name, u8 property_attributes)
  21. : m_previous(previous_shape)
  22. , m_property_name(property_name)
  23. , m_property_attributes(property_attributes)
  24. , m_prototype(previous_shape->m_prototype)
  25. {
  26. }
  27. Shape::Shape(Shape* previous_shape, Object* new_prototype)
  28. : m_previous(previous_shape)
  29. , m_prototype(new_prototype)
  30. {
  31. }
  32. Shape::~Shape()
  33. {
  34. }
  35. void Shape::visit_children(Cell::Visitor& visitor)
  36. {
  37. Cell::visit_children(visitor);
  38. if (m_prototype)
  39. visitor.visit(m_prototype);
  40. if (m_previous)
  41. visitor.visit(m_previous);
  42. for (auto& it : m_forward_transitions)
  43. visitor.visit(it.value);
  44. }
  45. Optional<PropertyMetadata> Shape::lookup(const FlyString& property_name) const
  46. {
  47. return property_table().get(property_name);
  48. }
  49. const HashMap<FlyString, PropertyMetadata>& Shape::property_table() const
  50. {
  51. ensure_property_table();
  52. return *m_property_table;
  53. }
  54. size_t Shape::property_count() const
  55. {
  56. return property_table().size();
  57. }
  58. void Shape::ensure_property_table() const
  59. {
  60. if (m_property_table)
  61. return;
  62. m_property_table = make<HashMap<FlyString, PropertyMetadata>>();
  63. // FIXME: We need to make sure the GC doesn't collect the transition chain as we're building it.
  64. // Maybe some kind of RAII "prevent GC for a moment" helper thingy?
  65. Vector<const Shape*> transition_chain;
  66. for (auto* shape = this; shape->m_previous; shape = shape->m_previous) {
  67. transition_chain.append(shape);
  68. }
  69. u32 next_offset = 0;
  70. for (ssize_t i = transition_chain.size() - 1; i >= 0; --i) {
  71. auto* shape = transition_chain[i];
  72. if (shape->m_property_name.is_null()) {
  73. // Ignore prototype transitions as they don't affect the key map.
  74. continue;
  75. }
  76. m_property_table->set(shape->m_property_name, { next_offset++, shape->m_property_attributes });
  77. }
  78. }
  79. }