IndexedProperties.h 5.8 KB

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