Shape.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/HashMap.h>
  8. #include <AK/OwnPtr.h>
  9. #include <AK/StringView.h>
  10. #include <AK/WeakPtr.h>
  11. #include <AK/Weakable.h>
  12. #include <LibJS/Forward.h>
  13. #include <LibJS/Heap/Cell.h>
  14. #include <LibJS/Runtime/PropertyAttributes.h>
  15. #include <LibJS/Runtime/StringOrSymbol.h>
  16. #include <LibJS/Runtime/Value.h>
  17. namespace JS {
  18. struct PropertyMetadata {
  19. u32 offset { 0 };
  20. PropertyAttributes attributes { 0 };
  21. };
  22. struct TransitionKey {
  23. StringOrSymbol property_key;
  24. PropertyAttributes attributes { 0 };
  25. bool operator==(TransitionKey const& other) const
  26. {
  27. return property_key == other.property_key && attributes == other.attributes;
  28. }
  29. };
  30. class Shape final
  31. : public Cell
  32. , public Weakable<Shape> {
  33. public:
  34. virtual ~Shape() override = default;
  35. enum class TransitionType {
  36. Invalid,
  37. Put,
  38. Configure,
  39. Prototype,
  40. };
  41. enum class ShapeWithoutGlobalObjectTag { Tag };
  42. explicit Shape(ShapeWithoutGlobalObjectTag) {};
  43. explicit Shape(Object& global_object);
  44. Shape(Shape& previous_shape, StringOrSymbol const& property_key, PropertyAttributes attributes, TransitionType);
  45. Shape(Shape& previous_shape, Object* new_prototype);
  46. Shape* create_put_transition(StringOrSymbol const&, PropertyAttributes attributes);
  47. Shape* create_configure_transition(StringOrSymbol const&, PropertyAttributes attributes);
  48. Shape* create_prototype_transition(Object* new_prototype);
  49. void add_property_without_transition(StringOrSymbol const&, PropertyAttributes);
  50. void add_property_without_transition(PropertyKey const&, PropertyAttributes);
  51. bool is_unique() const { return m_unique; }
  52. Shape* create_unique_clone() const;
  53. GlobalObject* global_object() const;
  54. Object* prototype() { return m_prototype; }
  55. Object const* prototype() const { return m_prototype; }
  56. Optional<PropertyMetadata> lookup(StringOrSymbol const&) const;
  57. HashMap<StringOrSymbol, PropertyMetadata> const& property_table() const;
  58. u32 property_count() const { return m_property_count; }
  59. struct Property {
  60. StringOrSymbol key;
  61. PropertyMetadata value;
  62. };
  63. Vector<Property> property_table_ordered() const;
  64. void set_prototype_without_transition(Object* new_prototype) { m_prototype = new_prototype; }
  65. void remove_property_from_unique_shape(StringOrSymbol const&, size_t offset);
  66. void add_property_to_unique_shape(StringOrSymbol const&, PropertyAttributes attributes);
  67. void reconfigure_property_in_unique_shape(StringOrSymbol const& property_key, PropertyAttributes attributes);
  68. private:
  69. virtual StringView class_name() const override { return "Shape"sv; }
  70. virtual void visit_edges(Visitor&) override;
  71. Shape* get_or_prune_cached_forward_transition(TransitionKey const&);
  72. Shape* get_or_prune_cached_prototype_transition(Object* prototype);
  73. void ensure_property_table() const;
  74. Object* m_global_object { nullptr };
  75. mutable OwnPtr<HashMap<StringOrSymbol, PropertyMetadata>> m_property_table;
  76. OwnPtr<HashMap<TransitionKey, WeakPtr<Shape>>> m_forward_transitions;
  77. OwnPtr<HashMap<Object*, WeakPtr<Shape>>> m_prototype_transitions;
  78. Shape* m_previous { nullptr };
  79. StringOrSymbol m_property_key;
  80. Object* m_prototype { nullptr };
  81. u32 m_property_count { 0 };
  82. PropertyAttributes m_attributes { 0 };
  83. TransitionType m_transition_type : 6 { TransitionType::Invalid };
  84. bool m_unique : 1 { false };
  85. };
  86. }
  87. template<>
  88. struct AK::Traits<JS::TransitionKey> : public GenericTraits<JS::TransitionKey> {
  89. static unsigned hash(const JS::TransitionKey& key)
  90. {
  91. return pair_int_hash(key.attributes.bits(), Traits<JS::StringOrSymbol>::hash(key.property_key));
  92. }
  93. };