Shape.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. public:
  35. virtual ~Shape() override = default;
  36. enum class TransitionType {
  37. Invalid,
  38. Put,
  39. Configure,
  40. Prototype,
  41. };
  42. Shape* create_put_transition(StringOrSymbol const&, PropertyAttributes attributes);
  43. Shape* create_configure_transition(StringOrSymbol const&, PropertyAttributes attributes);
  44. Shape* create_prototype_transition(Object* new_prototype);
  45. void add_property_without_transition(StringOrSymbol const&, PropertyAttributes);
  46. void add_property_without_transition(PropertyKey const&, PropertyAttributes);
  47. bool is_unique() const { return m_unique; }
  48. Shape* create_unique_clone() const;
  49. Realm& realm() const { return m_realm; }
  50. Object* prototype() { return m_prototype; }
  51. Object const* prototype() const { return m_prototype; }
  52. Optional<PropertyMetadata> lookup(StringOrSymbol const&) const;
  53. OrderedHashMap<StringOrSymbol, PropertyMetadata> const& property_table() const;
  54. u32 property_count() const { return m_property_count; }
  55. struct Property {
  56. StringOrSymbol key;
  57. PropertyMetadata value;
  58. };
  59. void set_prototype_without_transition(Object* new_prototype) { m_prototype = new_prototype; }
  60. void remove_property_from_unique_shape(StringOrSymbol const&, size_t offset);
  61. void add_property_to_unique_shape(StringOrSymbol const&, PropertyAttributes attributes);
  62. void reconfigure_property_in_unique_shape(StringOrSymbol const& property_key, PropertyAttributes attributes);
  63. [[nodiscard]] u64 unique_shape_serial_number() const { return m_unique_shape_serial_number; }
  64. private:
  65. explicit Shape(Realm&);
  66. Shape(Shape& previous_shape, StringOrSymbol const& property_key, PropertyAttributes attributes, TransitionType);
  67. Shape(Shape& previous_shape, Object* new_prototype);
  68. virtual void visit_edges(Visitor&) override;
  69. Shape* get_or_prune_cached_forward_transition(TransitionKey const&);
  70. Shape* get_or_prune_cached_prototype_transition(Object* prototype);
  71. void ensure_property_table() const;
  72. NonnullGCPtr<Realm> m_realm;
  73. mutable OwnPtr<OrderedHashMap<StringOrSymbol, PropertyMetadata>> m_property_table;
  74. OwnPtr<HashMap<TransitionKey, WeakPtr<Shape>>> m_forward_transitions;
  75. OwnPtr<HashMap<GCPtr<Object>, WeakPtr<Shape>>> m_prototype_transitions;
  76. GCPtr<Shape> m_previous;
  77. StringOrSymbol m_property_key;
  78. GCPtr<Object> m_prototype;
  79. u32 m_property_count { 0 };
  80. PropertyAttributes m_attributes { 0 };
  81. TransitionType m_transition_type : 6 { TransitionType::Invalid };
  82. bool m_unique : 1 { false };
  83. // Since unique shapes never change identity, inline caches use this incrementing serial number
  84. // to know whether its property table has been modified since last time we checked.
  85. u64 m_unique_shape_serial_number { 0 };
  86. };
  87. }
  88. template<>
  89. struct AK::Traits<JS::TransitionKey> : public GenericTraits<JS::TransitionKey> {
  90. static unsigned hash(const JS::TransitionKey& key)
  91. {
  92. return pair_int_hash(key.attributes.bits(), Traits<JS::StringOrSymbol>::hash(key.property_key));
  93. }
  94. };