Shape.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. static FlatPtr is_unique_offset() { return OFFSET_OF(Shape, m_unique); }
  49. Shape* create_unique_clone() const;
  50. Realm& realm() const { return m_realm; }
  51. Object* prototype() { return m_prototype; }
  52. Object const* prototype() const { return m_prototype; }
  53. Optional<PropertyMetadata> lookup(StringOrSymbol const&) const;
  54. OrderedHashMap<StringOrSymbol, PropertyMetadata> const& property_table() const;
  55. u32 property_count() const { return m_property_count; }
  56. struct Property {
  57. StringOrSymbol key;
  58. PropertyMetadata value;
  59. };
  60. void set_prototype_without_transition(Object* new_prototype) { m_prototype = new_prototype; }
  61. void remove_property_from_unique_shape(StringOrSymbol const&, size_t offset);
  62. void add_property_to_unique_shape(StringOrSymbol const&, PropertyAttributes attributes);
  63. void reconfigure_property_in_unique_shape(StringOrSymbol const& property_key, PropertyAttributes attributes);
  64. [[nodiscard]] u64 unique_shape_serial_number() const { return m_unique_shape_serial_number; }
  65. static FlatPtr unique_shape_serial_number_offset() { return OFFSET_OF(Shape, m_unique_shape_serial_number); }
  66. private:
  67. explicit Shape(Realm&);
  68. Shape(Shape& previous_shape, StringOrSymbol const& property_key, PropertyAttributes attributes, TransitionType);
  69. Shape(Shape& previous_shape, Object* new_prototype);
  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. NonnullGCPtr<Realm> m_realm;
  75. mutable OwnPtr<OrderedHashMap<StringOrSymbol, PropertyMetadata>> m_property_table;
  76. OwnPtr<HashMap<TransitionKey, WeakPtr<Shape>>> m_forward_transitions;
  77. OwnPtr<HashMap<GCPtr<Object>, WeakPtr<Shape>>> m_prototype_transitions;
  78. GCPtr<Shape> m_previous;
  79. StringOrSymbol m_property_key;
  80. GCPtr<Object> m_prototype;
  81. u32 m_property_count { 0 };
  82. PropertyAttributes m_attributes { 0 };
  83. TransitionType m_transition_type : 6 { TransitionType::Invalid };
  84. bool m_unique { false };
  85. // Since unique shapes never change identity, inline caches use this incrementing serial number
  86. // to know whether its property table has been modified since last time we checked.
  87. u64 m_unique_shape_serial_number { 0 };
  88. };
  89. }
  90. template<>
  91. struct AK::Traits<JS::TransitionKey> : public DefaultTraits<JS::TransitionKey> {
  92. static unsigned hash(const JS::TransitionKey& key)
  93. {
  94. return pair_int_hash(key.attributes.bits(), Traits<JS::StringOrSymbol>::hash(key.property_key));
  95. }
  96. };