IndexedProperties.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/NonnullOwnPtr.h>
  28. #include <LibJS/Runtime/Shape.h>
  29. #include <LibJS/Runtime/Value.h>
  30. namespace JS {
  31. const u32 SPARSE_ARRAY_THRESHOLD = 200;
  32. const u32 MIN_PACKED_RESIZE_AMOUNT = 20;
  33. struct ValueAndAttributes {
  34. Value value;
  35. PropertyAttributes attributes { default_attributes };
  36. };
  37. class IndexedProperties;
  38. class IndexedPropertyIterator;
  39. class GenericIndexedPropertyStorage;
  40. class IndexedPropertyStorage {
  41. public:
  42. virtual ~IndexedPropertyStorage() {};
  43. virtual bool has_index(u32 index) const = 0;
  44. virtual Optional<ValueAndAttributes> get(u32 index) const = 0;
  45. virtual void put(u32 index, Value value, PropertyAttributes attributes = default_attributes) = 0;
  46. virtual void remove(u32 index) = 0;
  47. virtual void insert(u32 index, Value value, PropertyAttributes attributes = default_attributes) = 0;
  48. virtual ValueAndAttributes take_first() = 0;
  49. virtual ValueAndAttributes take_last() = 0;
  50. virtual size_t size() const = 0;
  51. virtual size_t array_like_size() const = 0;
  52. virtual void set_array_like_size(size_t new_size) = 0;
  53. virtual bool is_simple_storage() const { return false; }
  54. };
  55. class SimpleIndexedPropertyStorage final : public IndexedPropertyStorage {
  56. public:
  57. SimpleIndexedPropertyStorage() = default;
  58. explicit SimpleIndexedPropertyStorage(Vector<Value>&& initial_values);
  59. virtual bool has_index(u32 index) const override;
  60. virtual Optional<ValueAndAttributes> get(u32 index) const override;
  61. virtual void put(u32 index, Value value, PropertyAttributes attributes = default_attributes) override;
  62. virtual void remove(u32 index) override;
  63. virtual void insert(u32 index, Value value, PropertyAttributes attributes = default_attributes) override;
  64. virtual ValueAndAttributes take_first() override;
  65. virtual ValueAndAttributes take_last() override;
  66. virtual size_t size() const override { return m_packed_elements.size(); }
  67. virtual size_t array_like_size() const override { return m_array_size; }
  68. virtual void set_array_like_size(size_t new_size) override;
  69. virtual bool is_simple_storage() const override { return true; }
  70. Vector<Value> elements() const { return m_packed_elements; }
  71. private:
  72. friend GenericIndexedPropertyStorage;
  73. size_t m_array_size { 0 };
  74. Vector<Value> m_packed_elements;
  75. };
  76. class GenericIndexedPropertyStorage final : public IndexedPropertyStorage {
  77. public:
  78. explicit GenericIndexedPropertyStorage(SimpleIndexedPropertyStorage&&);
  79. virtual bool has_index(u32 index) const override;
  80. virtual Optional<ValueAndAttributes> get(u32 index) const override;
  81. virtual void put(u32 index, Value value, PropertyAttributes attributes = default_attributes) override;
  82. virtual void remove(u32 index) override;
  83. virtual void insert(u32 index, Value value, PropertyAttributes attributes = default_attributes) override;
  84. virtual ValueAndAttributes take_first() override;
  85. virtual ValueAndAttributes take_last() override;
  86. virtual size_t size() const override { return m_packed_elements.size() + m_sparse_elements.size(); }
  87. virtual size_t array_like_size() const override { return m_array_size; }
  88. virtual void set_array_like_size(size_t new_size) override;
  89. Vector<ValueAndAttributes> packed_elements() const { return m_packed_elements; }
  90. HashMap<u32, ValueAndAttributes> sparse_elements() const { return m_sparse_elements; }
  91. private:
  92. size_t m_array_size { 0 };
  93. Vector<ValueAndAttributes> m_packed_elements;
  94. HashMap<u32, ValueAndAttributes> m_sparse_elements;
  95. };
  96. class IndexedPropertyIterator {
  97. public:
  98. IndexedPropertyIterator(const IndexedProperties&, u32 starting_index, bool skip_empty);
  99. IndexedPropertyIterator& operator++();
  100. IndexedPropertyIterator& operator*();
  101. bool operator!=(const IndexedPropertyIterator&) const;
  102. u32 index() const { return m_index; };
  103. ValueAndAttributes value_and_attributes(Object* this_object, bool evaluate_accessors = true);
  104. private:
  105. const IndexedProperties& m_indexed_properties;
  106. u32 m_index;
  107. bool m_skip_empty;
  108. };
  109. class IndexedProperties {
  110. public:
  111. IndexedProperties() = default;
  112. IndexedProperties(Vector<Value>&& values)
  113. : m_storage(make<SimpleIndexedPropertyStorage>(move(values)))
  114. {
  115. }
  116. bool has_index(u32 index) const { return m_storage->has_index(index); }
  117. Optional<ValueAndAttributes> get(Object* this_object, u32 index, bool evaluate_accessors = true) const;
  118. void put(Object* this_object, u32 index, Value value, PropertyAttributes attributes = default_attributes, bool evaluate_accessors = true);
  119. bool remove(u32 index);
  120. void insert(u32 index, Value value, PropertyAttributes attributes = default_attributes);
  121. ValueAndAttributes take_first(Object* this_object);
  122. ValueAndAttributes take_last(Object* this_object);
  123. void append(Value value, PropertyAttributes attributes = default_attributes) { put(nullptr, array_like_size(), value, attributes, false); }
  124. void append_all(Object* this_object, const IndexedProperties& properties, bool evaluate_accessors = true);
  125. IndexedPropertyIterator begin(bool skip_empty = true) const { return IndexedPropertyIterator(*this, 0, skip_empty); };
  126. IndexedPropertyIterator end() const { return IndexedPropertyIterator(*this, array_like_size(), false); };
  127. size_t size() const { return m_storage->size(); }
  128. bool is_empty() const { return size() == 0; }
  129. size_t array_like_size() const { return m_storage->array_like_size(); }
  130. void set_array_like_size(size_t new_size) { m_storage->set_array_like_size(new_size); };
  131. Vector<ValueAndAttributes> values_unordered() const;
  132. private:
  133. void switch_to_generic_storage();
  134. NonnullOwnPtr<IndexedPropertyStorage> m_storage { make<SimpleIndexedPropertyStorage>() };
  135. };
  136. }