IndexedProperties.cpp 14 KB

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