IndexedProperties.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. #include <LibJS/Runtime/Accessor.h>
  27. #include <LibJS/Runtime/IndexedProperties.h>
  28. namespace JS {
  29. SimpleIndexedPropertyStorage::SimpleIndexedPropertyStorage(Vector<Value>&& initial_values)
  30. : m_array_size(initial_values.size())
  31. , m_packed_elements(move(initial_values))
  32. {
  33. }
  34. bool SimpleIndexedPropertyStorage::has_index(u32 index) const
  35. {
  36. return index < m_array_size && !m_packed_elements[index].is_empty();
  37. }
  38. Optional<ValueAndAttributes> SimpleIndexedPropertyStorage::get(u32 index) const
  39. {
  40. if (index >= m_array_size)
  41. return {};
  42. return ValueAndAttributes { m_packed_elements[index], default_attributes };
  43. }
  44. void SimpleIndexedPropertyStorage::put(u32 index, Value value, PropertyAttributes attributes)
  45. {
  46. ASSERT(attributes == default_attributes);
  47. ASSERT(index < SPARSE_ARRAY_THRESHOLD);
  48. if (index >= m_array_size) {
  49. m_array_size = index + 1;
  50. if (index >= m_packed_elements.size())
  51. m_packed_elements.resize(index + MIN_PACKED_RESIZE_AMOUNT >= SPARSE_ARRAY_THRESHOLD ? SPARSE_ARRAY_THRESHOLD : index + MIN_PACKED_RESIZE_AMOUNT);
  52. }
  53. m_packed_elements[index] = value;
  54. }
  55. void SimpleIndexedPropertyStorage::remove(u32 index)
  56. {
  57. if (index < m_array_size)
  58. m_packed_elements[index] = {};
  59. }
  60. void SimpleIndexedPropertyStorage::insert(u32 index, Value value, PropertyAttributes attributes)
  61. {
  62. ASSERT(attributes == default_attributes);
  63. ASSERT(index < SPARSE_ARRAY_THRESHOLD);
  64. m_array_size++;
  65. ASSERT(m_array_size <= SPARSE_ARRAY_THRESHOLD);
  66. m_packed_elements.insert(index, value);
  67. }
  68. ValueAndAttributes SimpleIndexedPropertyStorage::take_first()
  69. {
  70. m_array_size--;
  71. return { m_packed_elements.take_first(), default_attributes };
  72. }
  73. ValueAndAttributes SimpleIndexedPropertyStorage::take_last()
  74. {
  75. m_array_size--;
  76. auto last_element = m_packed_elements[m_array_size];
  77. m_packed_elements[m_array_size] = {};
  78. return { last_element, default_attributes };
  79. }
  80. void SimpleIndexedPropertyStorage::set_array_like_size(size_t new_size)
  81. {
  82. ASSERT(new_size <= SPARSE_ARRAY_THRESHOLD);
  83. m_array_size = new_size;
  84. m_packed_elements.resize(new_size);
  85. }
  86. GenericIndexedPropertyStorage::GenericIndexedPropertyStorage(SimpleIndexedPropertyStorage&& storage)
  87. {
  88. m_array_size = storage.array_like_size();
  89. for (auto& element : move(storage.m_packed_elements))
  90. m_packed_elements.append({ element, default_attributes });
  91. }
  92. bool GenericIndexedPropertyStorage::has_index(u32 index) const
  93. {
  94. if (index < SPARSE_ARRAY_THRESHOLD)
  95. return index < m_packed_elements.size() && !m_packed_elements[index].value.is_empty();
  96. return m_sparse_elements.contains(index);
  97. }
  98. Optional<ValueAndAttributes> GenericIndexedPropertyStorage::get(u32 index) const
  99. {
  100. if (index >= m_array_size)
  101. return {};
  102. if (index < SPARSE_ARRAY_THRESHOLD) {
  103. if (index >= m_packed_elements.size())
  104. return {};
  105. return m_packed_elements[index];
  106. }
  107. return m_sparse_elements.get(index);
  108. }
  109. void GenericIndexedPropertyStorage::put(u32 index, Value value, PropertyAttributes attributes)
  110. {
  111. if (index >= m_array_size)
  112. m_array_size = index + 1;
  113. if (index < SPARSE_ARRAY_THRESHOLD) {
  114. if (index >= m_packed_elements.size())
  115. m_packed_elements.resize(index + MIN_PACKED_RESIZE_AMOUNT >= SPARSE_ARRAY_THRESHOLD ? SPARSE_ARRAY_THRESHOLD : index + MIN_PACKED_RESIZE_AMOUNT);
  116. m_packed_elements[index] = { value, attributes };
  117. } else {
  118. m_sparse_elements.set(index, { value, attributes });
  119. }
  120. }
  121. void GenericIndexedPropertyStorage::remove(u32 index)
  122. {
  123. if (index >= m_array_size)
  124. return;
  125. if (index + 1 == m_array_size) {
  126. take_last();
  127. return;
  128. }
  129. if (index < SPARSE_ARRAY_THRESHOLD) {
  130. if (index < m_packed_elements.size())
  131. m_packed_elements[index] = {};
  132. } else {
  133. m_sparse_elements.remove(index);
  134. }
  135. }
  136. void GenericIndexedPropertyStorage::insert(u32 index, Value value, PropertyAttributes attributes)
  137. {
  138. if (index >= m_array_size) {
  139. put(index, value, attributes);
  140. return;
  141. }
  142. m_array_size++;
  143. if (!m_sparse_elements.is_empty()) {
  144. HashMap<u32, ValueAndAttributes> new_sparse_elements;
  145. for (auto& entry : m_sparse_elements)
  146. new_sparse_elements.set(entry.key >= index ? entry.key + 1 : entry.key, entry.value);
  147. m_sparse_elements = move(new_sparse_elements);
  148. }
  149. if (index < SPARSE_ARRAY_THRESHOLD) {
  150. m_packed_elements.insert(index, { value, attributes });
  151. } else {
  152. m_sparse_elements.set(index, { value, attributes });
  153. }
  154. }
  155. ValueAndAttributes GenericIndexedPropertyStorage::take_first()
  156. {
  157. ASSERT(m_array_size > 0);
  158. m_array_size--;
  159. if (!m_sparse_elements.is_empty()) {
  160. HashMap<u32, ValueAndAttributes> new_sparse_elements;
  161. for (auto& entry : m_sparse_elements)
  162. new_sparse_elements.set(entry.key - 1, entry.value);
  163. m_sparse_elements = move(new_sparse_elements);
  164. }
  165. return m_packed_elements.take_first();
  166. }
  167. ValueAndAttributes GenericIndexedPropertyStorage::take_last()
  168. {
  169. ASSERT(m_array_size > 0);
  170. m_array_size--;
  171. if (m_array_size <= SPARSE_ARRAY_THRESHOLD) {
  172. auto last_element = m_packed_elements[m_array_size];
  173. m_packed_elements[m_array_size] = {};
  174. return last_element;
  175. } else {
  176. auto result = m_sparse_elements.get(m_array_size);
  177. m_sparse_elements.remove(m_array_size);
  178. ASSERT(result.has_value());
  179. return result.value();
  180. }
  181. }
  182. void GenericIndexedPropertyStorage::set_array_like_size(size_t new_size)
  183. {
  184. if (new_size < SPARSE_ARRAY_THRESHOLD) {
  185. m_packed_elements.resize(new_size);
  186. m_sparse_elements.clear();
  187. } else {
  188. m_packed_elements.resize(SPARSE_ARRAY_THRESHOLD);
  189. HashMap<u32, ValueAndAttributes> new_sparse_elements;
  190. for (auto& entry : m_sparse_elements) {
  191. if (entry.key < new_size)
  192. new_sparse_elements.set(entry.key, entry.value);
  193. }
  194. m_sparse_elements = move(new_sparse_elements);
  195. }
  196. }
  197. IndexedPropertyIterator::IndexedPropertyIterator(const IndexedProperties& indexed_properties, u32 staring_index, bool skip_empty)
  198. : m_indexed_properties(indexed_properties)
  199. , m_index(staring_index)
  200. , m_skip_empty(skip_empty)
  201. {
  202. while (m_skip_empty && m_index < m_indexed_properties.array_like_size()) {
  203. if (m_indexed_properties.has_index(m_index))
  204. break;
  205. m_index++;
  206. }
  207. }
  208. IndexedPropertyIterator& IndexedPropertyIterator::operator++()
  209. {
  210. m_index++;
  211. while (m_skip_empty && m_index < m_indexed_properties.array_like_size()) {
  212. if (m_indexed_properties.has_index(m_index))
  213. break;
  214. m_index++;
  215. };
  216. return *this;
  217. }
  218. IndexedPropertyIterator& IndexedPropertyIterator::operator*()
  219. {
  220. return *this;
  221. }
  222. bool IndexedPropertyIterator::operator!=(const IndexedPropertyIterator& other) const
  223. {
  224. return m_index != other.m_index;
  225. }
  226. ValueAndAttributes IndexedPropertyIterator::value_and_attributes(Object* this_object, bool evaluate_accessors)
  227. {
  228. if (m_index < m_indexed_properties.array_like_size())
  229. return m_indexed_properties.get(this_object, m_index, evaluate_accessors).value();
  230. return {};
  231. }
  232. Optional<ValueAndAttributes> IndexedProperties::get(Object* this_object, u32 index, bool evaluate_accessors) const
  233. {
  234. auto result = m_storage->get(index);
  235. if (!evaluate_accessors)
  236. return result;
  237. if (!result.has_value())
  238. return {};
  239. auto value = result.value();
  240. if (value.value.is_accessor()) {
  241. ASSERT(this_object);
  242. auto& accessor = value.value.as_accessor();
  243. return ValueAndAttributes { accessor.call_getter(this_object), value.attributes };
  244. }
  245. return result;
  246. }
  247. void IndexedProperties::put(Object* this_object, u32 index, Value value, PropertyAttributes attributes, bool evaluate_accessors)
  248. {
  249. if (m_storage->is_simple_storage() && (index >= SPARSE_ARRAY_THRESHOLD || attributes != default_attributes))
  250. switch_to_generic_storage();
  251. if (m_storage->is_simple_storage() || !evaluate_accessors) {
  252. m_storage->put(index, value, attributes);
  253. return;
  254. }
  255. auto value_here = m_storage->get(index);
  256. if (value_here.has_value() && value_here.value().value.is_accessor()) {
  257. ASSERT(this_object);
  258. value_here.value().value.as_accessor().call_setter(this_object, value);
  259. } else {
  260. m_storage->put(index, value, attributes);
  261. }
  262. }
  263. bool IndexedProperties::remove(u32 index)
  264. {
  265. auto result = m_storage->get(index);
  266. if (!result.has_value())
  267. return true;
  268. if (!result.value().attributes.is_configurable())
  269. return false;
  270. m_storage->remove(index);
  271. return true;
  272. }
  273. void IndexedProperties::insert(u32 index, Value value, PropertyAttributes attributes)
  274. {
  275. if (m_storage->is_simple_storage() && (index >= SPARSE_ARRAY_THRESHOLD || attributes != default_attributes || array_like_size() == SPARSE_ARRAY_THRESHOLD))
  276. switch_to_generic_storage();
  277. m_storage->insert(index, value, attributes);
  278. }
  279. ValueAndAttributes IndexedProperties::take_first(Object *this_object)
  280. {
  281. auto first = m_storage->take_first();
  282. if (first.value.is_accessor())
  283. return { first.value.as_accessor().call_getter(this_object), first.attributes };
  284. return first;
  285. }
  286. ValueAndAttributes IndexedProperties::take_last(Object *this_object)
  287. {
  288. auto last = m_storage->take_last();
  289. if (last.value.is_accessor())
  290. return { last.value.as_accessor().call_getter(this_object), last.attributes };
  291. return last;
  292. }
  293. void IndexedProperties::append_all(Object* this_object, const IndexedProperties& properties, bool evaluate_accessors)
  294. {
  295. if (m_storage->is_simple_storage() && !properties.m_storage->is_simple_storage())
  296. switch_to_generic_storage();
  297. for (auto it = properties.begin(false); it != properties.end(); ++it) {
  298. auto element = it.value_and_attributes(this_object, evaluate_accessors);
  299. if (this_object && this_object->interpreter().exception())
  300. return;
  301. m_storage->put(m_storage->array_like_size(), element.value, element.attributes);
  302. }
  303. }
  304. Vector<ValueAndAttributes> IndexedProperties::values_unordered() const
  305. {
  306. if (m_storage->is_simple_storage()) {
  307. auto elements = static_cast<const SimpleIndexedPropertyStorage&>(*m_storage).elements();
  308. Vector<ValueAndAttributes> with_attributes;
  309. for (auto& value : elements)
  310. with_attributes.append({ value, default_attributes });
  311. return with_attributes;
  312. }
  313. auto storage = static_cast<const GenericIndexedPropertyStorage&>(*m_storage);
  314. auto values = storage.packed_elements();
  315. values.ensure_capacity(values.size() + storage.sparse_elements().size());
  316. for (auto& entry : storage.sparse_elements())
  317. values.unchecked_append(entry.value);
  318. return values;
  319. }
  320. void IndexedProperties::switch_to_generic_storage()
  321. {
  322. auto storage = static_cast<const SimpleIndexedPropertyStorage&>(*m_storage);
  323. m_storage = make<GenericIndexedPropertyStorage>(move(storage));
  324. }
  325. }