IndexedProperties.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. };
  15. class IndexedProperties;
  16. class IndexedPropertyIterator;
  17. class GenericIndexedPropertyStorage;
  18. class IndexedPropertyStorage {
  19. public:
  20. virtual ~IndexedPropertyStorage() = default;
  21. virtual bool has_index(u32 index) const = 0;
  22. virtual Optional<ValueAndAttributes> get(u32 index) const = 0;
  23. virtual void put(u32 index, Value value, PropertyAttributes attributes = default_attributes) = 0;
  24. virtual void remove(u32 index) = 0;
  25. virtual ValueAndAttributes take_first() = 0;
  26. virtual ValueAndAttributes take_last() = 0;
  27. virtual size_t size() const = 0;
  28. virtual size_t array_like_size() const = 0;
  29. virtual bool set_array_like_size(size_t new_size) = 0;
  30. virtual bool is_simple_storage() const { return false; }
  31. };
  32. class SimpleIndexedPropertyStorage final : public IndexedPropertyStorage {
  33. public:
  34. SimpleIndexedPropertyStorage() = default;
  35. explicit SimpleIndexedPropertyStorage(Vector<Value>&& initial_values);
  36. virtual bool has_index(u32 index) const override;
  37. virtual Optional<ValueAndAttributes> get(u32 index) const override;
  38. virtual void put(u32 index, Value value, PropertyAttributes attributes = default_attributes) override;
  39. virtual void remove(u32 index) override;
  40. virtual ValueAndAttributes take_first() override;
  41. virtual ValueAndAttributes take_last() override;
  42. virtual size_t size() const override { return m_packed_elements.size(); }
  43. virtual size_t array_like_size() const override { return m_array_size; }
  44. virtual bool set_array_like_size(size_t new_size) override;
  45. virtual bool is_simple_storage() const override { return true; }
  46. Vector<Value> const& elements() const { return m_packed_elements; }
  47. private:
  48. friend GenericIndexedPropertyStorage;
  49. void grow_storage_if_needed();
  50. size_t m_array_size { 0 };
  51. Vector<Value> m_packed_elements;
  52. };
  53. class GenericIndexedPropertyStorage final : public IndexedPropertyStorage {
  54. public:
  55. explicit GenericIndexedPropertyStorage(SimpleIndexedPropertyStorage&&);
  56. explicit GenericIndexedPropertyStorage() = default;
  57. virtual bool has_index(u32 index) const override;
  58. virtual Optional<ValueAndAttributes> get(u32 index) const override;
  59. virtual void put(u32 index, Value value, PropertyAttributes attributes = default_attributes) override;
  60. virtual void remove(u32 index) override;
  61. virtual ValueAndAttributes take_first() override;
  62. virtual ValueAndAttributes take_last() override;
  63. virtual size_t size() const override { return m_sparse_elements.size(); }
  64. virtual size_t array_like_size() const override { return m_array_size; }
  65. virtual bool set_array_like_size(size_t new_size) override;
  66. HashMap<u32, ValueAndAttributes> const& sparse_elements() const { return m_sparse_elements; }
  67. private:
  68. size_t m_array_size { 0 };
  69. HashMap<u32, ValueAndAttributes> m_sparse_elements;
  70. };
  71. class IndexedPropertyIterator {
  72. public:
  73. IndexedPropertyIterator(IndexedProperties const&, u32 starting_index, bool skip_empty);
  74. IndexedPropertyIterator& operator++();
  75. IndexedPropertyIterator& operator*();
  76. bool operator!=(IndexedPropertyIterator const&) const;
  77. u32 index() const { return m_index; };
  78. private:
  79. void skip_empty_indices();
  80. IndexedProperties const& m_indexed_properties;
  81. Vector<u32> m_cached_indices;
  82. u32 m_index { 0 };
  83. bool m_skip_empty { false };
  84. };
  85. class IndexedProperties {
  86. public:
  87. IndexedProperties() = default;
  88. explicit IndexedProperties(Vector<Value> values)
  89. {
  90. if (!values.is_empty())
  91. m_storage = make<SimpleIndexedPropertyStorage>(move(values));
  92. }
  93. bool has_index(u32 index) const { return m_storage ? m_storage->has_index(index) : false; }
  94. Optional<ValueAndAttributes> get(u32 index) const;
  95. void put(u32 index, Value value, PropertyAttributes attributes = default_attributes);
  96. void remove(u32 index);
  97. void append(Value value, PropertyAttributes attributes = default_attributes) { put(array_like_size(), value, attributes); }
  98. IndexedPropertyIterator begin(bool skip_empty = true) const { return IndexedPropertyIterator(*this, 0, skip_empty); };
  99. IndexedPropertyIterator end() const { return IndexedPropertyIterator(*this, array_like_size(), false); };
  100. bool is_empty() const { return array_like_size() == 0; }
  101. size_t array_like_size() const { return m_storage ? m_storage->array_like_size() : 0; }
  102. bool set_array_like_size(size_t);
  103. size_t real_size() const;
  104. Vector<u32> indices() const;
  105. template<typename Callback>
  106. void for_each_value(Callback callback)
  107. {
  108. if (!m_storage)
  109. return;
  110. if (m_storage->is_simple_storage()) {
  111. for (auto& value : static_cast<SimpleIndexedPropertyStorage&>(*m_storage).elements())
  112. callback(value);
  113. } else {
  114. for (auto& element : static_cast<GenericIndexedPropertyStorage const&>(*m_storage).sparse_elements())
  115. callback(element.value.value);
  116. }
  117. }
  118. private:
  119. void switch_to_generic_storage();
  120. void ensure_storage();
  121. OwnPtr<IndexedPropertyStorage> m_storage;
  122. };
  123. }