IndexedProperties.h 5.3 KB

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