IndexedProperties.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. protected:
  37. explicit IndexedPropertyStorage(IsSimpleStorage is_simple_storage)
  38. : m_is_simple_storage(is_simple_storage == IsSimpleStorage::Yes) {};
  39. private:
  40. bool m_is_simple_storage { false };
  41. };
  42. class SimpleIndexedPropertyStorage final : public IndexedPropertyStorage {
  43. public:
  44. SimpleIndexedPropertyStorage()
  45. : IndexedPropertyStorage(IsSimpleStorage::Yes) {};
  46. explicit SimpleIndexedPropertyStorage(Vector<Value>&& initial_values);
  47. virtual bool has_index(u32 index) const override;
  48. virtual Optional<ValueAndAttributes> get(u32 index) const override;
  49. virtual void put(u32 index, Value value, PropertyAttributes attributes = default_attributes) override;
  50. virtual void remove(u32 index) override;
  51. virtual ValueAndAttributes take_first() override;
  52. virtual ValueAndAttributes take_last() override;
  53. virtual size_t size() const override { return m_packed_elements.size(); }
  54. virtual size_t array_like_size() const override { return m_array_size; }
  55. virtual bool set_array_like_size(size_t new_size) override;
  56. Vector<Value> const& elements() const { return m_packed_elements; }
  57. static FlatPtr elements_offset() { return OFFSET_OF(SimpleIndexedPropertyStorage, m_packed_elements); }
  58. private:
  59. friend GenericIndexedPropertyStorage;
  60. void grow_storage_if_needed();
  61. size_t m_array_size { 0 };
  62. Vector<Value> m_packed_elements;
  63. };
  64. class GenericIndexedPropertyStorage final : public IndexedPropertyStorage {
  65. public:
  66. explicit GenericIndexedPropertyStorage(SimpleIndexedPropertyStorage&&);
  67. explicit GenericIndexedPropertyStorage()
  68. : IndexedPropertyStorage(IsSimpleStorage::No) {};
  69. virtual bool has_index(u32 index) const override;
  70. virtual Optional<ValueAndAttributes> get(u32 index) const override;
  71. virtual void put(u32 index, Value value, PropertyAttributes attributes = default_attributes) override;
  72. virtual void remove(u32 index) override;
  73. virtual ValueAndAttributes take_first() override;
  74. virtual ValueAndAttributes take_last() override;
  75. virtual size_t size() const override { return m_sparse_elements.size(); }
  76. virtual size_t array_like_size() const override { return m_array_size; }
  77. virtual bool set_array_like_size(size_t new_size) override;
  78. HashMap<u32, ValueAndAttributes> const& sparse_elements() const { return m_sparse_elements; }
  79. private:
  80. size_t m_array_size { 0 };
  81. HashMap<u32, ValueAndAttributes> m_sparse_elements;
  82. };
  83. class IndexedPropertyIterator {
  84. public:
  85. IndexedPropertyIterator(IndexedProperties const&, u32 starting_index, bool skip_empty);
  86. IndexedPropertyIterator& operator++();
  87. IndexedPropertyIterator& operator*();
  88. bool operator!=(IndexedPropertyIterator const&) const;
  89. u32 index() const { return m_index; }
  90. private:
  91. void skip_empty_indices();
  92. IndexedProperties const& m_indexed_properties;
  93. Vector<u32> m_cached_indices;
  94. u32 m_index { 0 };
  95. bool m_skip_empty { false };
  96. };
  97. class IndexedProperties {
  98. public:
  99. IndexedProperties() = default;
  100. explicit IndexedProperties(Vector<Value> values)
  101. {
  102. if (!values.is_empty())
  103. m_storage = make<SimpleIndexedPropertyStorage>(move(values));
  104. }
  105. bool has_index(u32 index) const { return m_storage ? m_storage->has_index(index) : false; }
  106. Optional<ValueAndAttributes> get(u32 index) const;
  107. void put(u32 index, Value value, PropertyAttributes attributes = default_attributes);
  108. void remove(u32 index);
  109. void append(Value value, PropertyAttributes attributes = default_attributes) { put(array_like_size(), value, attributes); }
  110. IndexedPropertyIterator begin(bool skip_empty = true) const { return IndexedPropertyIterator(*this, 0, skip_empty); }
  111. IndexedPropertyIterator end() const { return IndexedPropertyIterator(*this, array_like_size(), false); }
  112. bool is_empty() const { return array_like_size() == 0; }
  113. size_t array_like_size() const { return m_storage ? m_storage->array_like_size() : 0; }
  114. bool set_array_like_size(size_t);
  115. IndexedPropertyStorage* storage() { return m_storage; }
  116. IndexedPropertyStorage const* storage() const { return m_storage; }
  117. size_t real_size() const;
  118. Vector<u32> indices() const;
  119. template<typename Callback>
  120. void for_each_value(Callback callback)
  121. {
  122. if (!m_storage)
  123. return;
  124. if (m_storage->is_simple_storage()) {
  125. for (auto& value : static_cast<SimpleIndexedPropertyStorage&>(*m_storage).elements())
  126. callback(value);
  127. } else {
  128. for (auto& element : static_cast<GenericIndexedPropertyStorage const&>(*m_storage).sparse_elements())
  129. callback(element.value.value);
  130. }
  131. }
  132. static FlatPtr storage_offset() { return OFFSET_OF(IndexedProperties, m_storage); }
  133. private:
  134. void switch_to_generic_storage();
  135. void ensure_storage();
  136. OwnPtr<IndexedPropertyStorage> m_storage;
  137. };
  138. }