IndexedProperties.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/NonnullOwnPtr.h>
  8. #include <LibJS/Runtime/Shape.h>
  9. #include <LibJS/Runtime/Value.h>
  10. namespace JS {
  11. struct ValueAndAttributes {
  12. Value value;
  13. PropertyAttributes attributes { default_attributes };
  14. Optional<u32> property_offset {};
  15. };
  16. class IndexedProperties;
  17. class IndexedPropertyIterator;
  18. class GenericIndexedPropertyStorage;
  19. class IndexedPropertyStorage {
  20. public:
  21. virtual ~IndexedPropertyStorage() = default;
  22. enum class IsSimpleStorage {
  23. No,
  24. Yes,
  25. };
  26. virtual bool has_index(u32 index) const = 0;
  27. virtual Optional<ValueAndAttributes> get(u32 index) const = 0;
  28. virtual void put(u32 index, Value value, PropertyAttributes attributes = default_attributes) = 0;
  29. virtual void remove(u32 index) = 0;
  30. virtual ValueAndAttributes take_first() = 0;
  31. virtual ValueAndAttributes take_last() = 0;
  32. virtual size_t size() const = 0;
  33. virtual size_t array_like_size() const = 0;
  34. virtual bool set_array_like_size(size_t new_size) = 0;
  35. bool is_simple_storage() const { return m_is_simple_storage; }
  36. static FlatPtr is_simple_storage_offset() { return OFFSET_OF(IndexedPropertyStorage, m_is_simple_storage); }
  37. protected:
  38. explicit IndexedPropertyStorage(IsSimpleStorage is_simple_storage)
  39. : m_is_simple_storage(is_simple_storage == IsSimpleStorage::Yes) {};
  40. private:
  41. bool m_is_simple_storage { false };
  42. };
  43. class SimpleIndexedPropertyStorage final : public IndexedPropertyStorage {
  44. public:
  45. SimpleIndexedPropertyStorage()
  46. : IndexedPropertyStorage(IsSimpleStorage::Yes) {};
  47. explicit SimpleIndexedPropertyStorage(Vector<Value>&& initial_values);
  48. virtual bool has_index(u32 index) const override;
  49. virtual Optional<ValueAndAttributes> get(u32 index) const override;
  50. virtual void put(u32 index, Value value, PropertyAttributes attributes = default_attributes) override;
  51. virtual void remove(u32 index) override;
  52. virtual ValueAndAttributes take_first() override;
  53. virtual ValueAndAttributes take_last() override;
  54. virtual size_t size() const override { return m_packed_elements.size(); }
  55. virtual size_t array_like_size() const override { return m_array_size; }
  56. virtual bool set_array_like_size(size_t new_size) override;
  57. Vector<Value> const& elements() const { return m_packed_elements; }
  58. static FlatPtr array_size_offset() { return OFFSET_OF(SimpleIndexedPropertyStorage, m_array_size); }
  59. static FlatPtr elements_offset() { return OFFSET_OF(SimpleIndexedPropertyStorage, m_packed_elements); }
  60. private:
  61. friend GenericIndexedPropertyStorage;
  62. void grow_storage_if_needed();
  63. size_t m_array_size { 0 };
  64. Vector<Value> m_packed_elements;
  65. };
  66. class GenericIndexedPropertyStorage final : public IndexedPropertyStorage {
  67. public:
  68. explicit GenericIndexedPropertyStorage(SimpleIndexedPropertyStorage&&);
  69. explicit GenericIndexedPropertyStorage()
  70. : IndexedPropertyStorage(IsSimpleStorage::No) {};
  71. virtual bool has_index(u32 index) const override;
  72. virtual Optional<ValueAndAttributes> get(u32 index) const override;
  73. virtual void put(u32 index, Value value, PropertyAttributes attributes = default_attributes) override;
  74. virtual void remove(u32 index) override;
  75. virtual ValueAndAttributes take_first() override;
  76. virtual ValueAndAttributes take_last() override;
  77. virtual size_t size() const override { return m_sparse_elements.size(); }
  78. virtual size_t array_like_size() const override { return m_array_size; }
  79. virtual bool set_array_like_size(size_t new_size) override;
  80. HashMap<u32, ValueAndAttributes> const& sparse_elements() const { return m_sparse_elements; }
  81. private:
  82. size_t m_array_size { 0 };
  83. HashMap<u32, ValueAndAttributes> m_sparse_elements;
  84. };
  85. class IndexedPropertyIterator {
  86. public:
  87. IndexedPropertyIterator(IndexedProperties const&, u32 starting_index, bool skip_empty);
  88. IndexedPropertyIterator& operator++();
  89. IndexedPropertyIterator& operator*();
  90. bool operator!=(IndexedPropertyIterator const&) const;
  91. u32 index() const { return m_index; }
  92. private:
  93. void skip_empty_indices();
  94. IndexedProperties const& m_indexed_properties;
  95. Vector<u32> m_cached_indices;
  96. u32 m_index { 0 };
  97. bool m_skip_empty { false };
  98. };
  99. class IndexedProperties {
  100. public:
  101. IndexedProperties() = default;
  102. explicit IndexedProperties(Vector<Value> values)
  103. {
  104. if (!values.is_empty())
  105. m_storage = make<SimpleIndexedPropertyStorage>(move(values));
  106. }
  107. bool has_index(u32 index) const { return m_storage ? m_storage->has_index(index) : false; }
  108. Optional<ValueAndAttributes> get(u32 index) const;
  109. void put(u32 index, Value value, PropertyAttributes attributes = default_attributes);
  110. void remove(u32 index);
  111. void append(Value value, PropertyAttributes attributes = default_attributes) { put(array_like_size(), value, attributes); }
  112. IndexedPropertyIterator begin(bool skip_empty = true) const { return IndexedPropertyIterator(*this, 0, skip_empty); }
  113. IndexedPropertyIterator end() const { return IndexedPropertyIterator(*this, array_like_size(), false); }
  114. bool is_empty() const { return array_like_size() == 0; }
  115. size_t array_like_size() const { return m_storage ? m_storage->array_like_size() : 0; }
  116. bool set_array_like_size(size_t);
  117. IndexedPropertyStorage* storage() { return m_storage; }
  118. IndexedPropertyStorage const* storage() const { return m_storage; }
  119. size_t real_size() const;
  120. Vector<u32> indices() const;
  121. template<typename Callback>
  122. void for_each_value(Callback callback)
  123. {
  124. if (!m_storage)
  125. return;
  126. if (m_storage->is_simple_storage()) {
  127. for (auto& value : static_cast<SimpleIndexedPropertyStorage&>(*m_storage).elements())
  128. callback(value);
  129. } else {
  130. for (auto& element : static_cast<GenericIndexedPropertyStorage const&>(*m_storage).sparse_elements())
  131. callback(element.value.value);
  132. }
  133. }
  134. static FlatPtr storage_offset() { return OFFSET_OF(IndexedProperties, m_storage); }
  135. private:
  136. void switch_to_generic_storage();
  137. void ensure_storage();
  138. OwnPtr<IndexedPropertyStorage> m_storage;
  139. };
  140. }