IndexedProperties.h 5.5 KB

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