Shape.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. JS_CELL(Shape, Cell);
  34. JS_DECLARE_ALLOCATOR(Shape);
  35. public:
  36. virtual ~Shape() override = default;
  37. enum class TransitionType : u8 {
  38. Invalid,
  39. Put,
  40. Configure,
  41. Prototype,
  42. Delete,
  43. CacheableDictionary,
  44. UncacheableDictionary,
  45. };
  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. [[nodiscard]] NonnullGCPtr<Shape> create_delete_transition(StringOrSymbol const&);
  50. [[nodiscard]] NonnullGCPtr<Shape> create_cacheable_dictionary_transition();
  51. [[nodiscard]] NonnullGCPtr<Shape> create_uncacheable_dictionary_transition();
  52. void add_property_without_transition(StringOrSymbol const&, PropertyAttributes);
  53. void add_property_without_transition(PropertyKey const&, PropertyAttributes);
  54. void remove_property_without_transition(StringOrSymbol const&, u32 offset);
  55. void set_property_attributes_without_transition(StringOrSymbol const&, PropertyAttributes);
  56. [[nodiscard]] bool is_cacheable() const { return m_cacheable; }
  57. [[nodiscard]] bool is_dictionary() const { return m_dictionary; }
  58. [[nodiscard]] bool is_cacheable_dictionary() const { return m_dictionary && m_cacheable; }
  59. [[nodiscard]] bool is_uncacheable_dictionary() const { return m_dictionary && !m_cacheable; }
  60. Realm& realm() const { return m_realm; }
  61. Object* prototype() { return m_prototype; }
  62. Object const* prototype() const { return m_prototype; }
  63. Optional<PropertyMetadata> lookup(StringOrSymbol const&) const;
  64. OrderedHashMap<StringOrSymbol, PropertyMetadata> const& property_table() const;
  65. u32 property_count() const { return m_property_count; }
  66. struct Property {
  67. StringOrSymbol key;
  68. PropertyMetadata value;
  69. };
  70. void set_prototype_without_transition(Object* new_prototype) { m_prototype = new_prototype; }
  71. private:
  72. explicit Shape(Realm&);
  73. Shape(Shape& previous_shape, StringOrSymbol const& property_key, PropertyAttributes attributes, TransitionType);
  74. Shape(Shape& previous_shape, StringOrSymbol const& property_key, TransitionType);
  75. Shape(Shape& previous_shape, Object* new_prototype);
  76. virtual void visit_edges(Visitor&) override;
  77. Shape* get_or_prune_cached_forward_transition(TransitionKey const&);
  78. Shape* get_or_prune_cached_prototype_transition(Object* prototype);
  79. [[nodiscard]] GCPtr<Shape> get_or_prune_cached_delete_transition(StringOrSymbol const&);
  80. void ensure_property_table() const;
  81. NonnullGCPtr<Realm> m_realm;
  82. mutable OwnPtr<OrderedHashMap<StringOrSymbol, PropertyMetadata>> m_property_table;
  83. OwnPtr<HashMap<TransitionKey, WeakPtr<Shape>>> m_forward_transitions;
  84. OwnPtr<HashMap<GCPtr<Object>, WeakPtr<Shape>>> m_prototype_transitions;
  85. OwnPtr<HashMap<StringOrSymbol, WeakPtr<Shape>>> m_delete_transitions;
  86. GCPtr<Shape> m_previous;
  87. StringOrSymbol m_property_key;
  88. GCPtr<Object> m_prototype;
  89. u32 m_property_count { 0 };
  90. PropertyAttributes m_attributes { 0 };
  91. TransitionType m_transition_type { TransitionType::Invalid };
  92. bool m_dictionary { false };
  93. bool m_cacheable { true };
  94. };
  95. }
  96. template<>
  97. struct AK::Traits<JS::TransitionKey> : public DefaultTraits<JS::TransitionKey> {
  98. static unsigned hash(const JS::TransitionKey& key)
  99. {
  100. return pair_int_hash(key.attributes.bits(), Traits<JS::StringOrSymbol>::hash(key.property_key));
  101. }
  102. };