ArrayPrototype.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  4. * Copyright (c) 2020, Marcin Gasperowicz <xnooga@gmail.com>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice, this
  11. * list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <AK/Function.h>
  29. #include <AK/HashTable.h>
  30. #include <AK/StringBuilder.h>
  31. #include <LibJS/Runtime/Array.h>
  32. #include <LibJS/Runtime/ArrayIterator.h>
  33. #include <LibJS/Runtime/ArrayPrototype.h>
  34. #include <LibJS/Runtime/Error.h>
  35. #include <LibJS/Runtime/Function.h>
  36. #include <LibJS/Runtime/GlobalObject.h>
  37. #include <LibJS/Runtime/ObjectPrototype.h>
  38. #include <LibJS/Runtime/Value.h>
  39. namespace JS {
  40. static HashTable<Object*> s_array_join_seen_objects;
  41. ArrayPrototype::ArrayPrototype(GlobalObject& global_object)
  42. : Object(*global_object.object_prototype())
  43. {
  44. }
  45. void ArrayPrototype::initialize(GlobalObject& global_object)
  46. {
  47. auto& vm = this->vm();
  48. Object::initialize(global_object);
  49. u8 attr = Attribute::Writable | Attribute::Configurable;
  50. define_native_function(vm.names.filter, filter, 1, attr);
  51. define_native_function(vm.names.forEach, for_each, 1, attr);
  52. define_native_function(vm.names.map, map, 1, attr);
  53. define_native_function(vm.names.pop, pop, 0, attr);
  54. define_native_function(vm.names.push, push, 1, attr);
  55. define_native_function(vm.names.shift, shift, 0, attr);
  56. define_native_function(vm.names.toString, to_string, 0, attr);
  57. define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
  58. define_native_function(vm.names.unshift, unshift, 1, attr);
  59. define_native_function(vm.names.join, join, 1, attr);
  60. define_native_function(vm.names.concat, concat, 1, attr);
  61. define_native_function(vm.names.slice, slice, 2, attr);
  62. define_native_function(vm.names.indexOf, index_of, 1, attr);
  63. define_native_function(vm.names.reduce, reduce, 1, attr);
  64. define_native_function(vm.names.reduceRight, reduce_right, 1, attr);
  65. define_native_function(vm.names.reverse, reverse, 0, attr);
  66. define_native_function(vm.names.lastIndexOf, last_index_of, 1, attr);
  67. define_native_function(vm.names.includes, includes, 1, attr);
  68. define_native_function(vm.names.find, find, 1, attr);
  69. define_native_function(vm.names.findIndex, find_index, 1, attr);
  70. define_native_function(vm.names.some, some, 1, attr);
  71. define_native_function(vm.names.every, every, 1, attr);
  72. define_native_function(vm.names.splice, splice, 2, attr);
  73. define_native_function(vm.names.fill, fill, 1, attr);
  74. define_native_function(vm.names.values, values, 0, attr);
  75. define_property(vm.names.length, Value(0), Attribute::Configurable);
  76. // Use define_property here instead of define_native_function so that
  77. // Object.is(Array.prototype[Symbol.iterator], Array.prototype.values)
  78. // evaluates to true
  79. define_property(vm.well_known_symbol_iterator(), get(vm.names.values), attr);
  80. }
  81. ArrayPrototype::~ArrayPrototype()
  82. {
  83. }
  84. static Function* callback_from_args(GlobalObject& global_object, const String& name)
  85. {
  86. auto& vm = global_object.vm();
  87. if (vm.argument_count() < 1) {
  88. vm.throw_exception<TypeError>(global_object, ErrorType::ArrayPrototypeOneArg, name);
  89. return nullptr;
  90. }
  91. auto callback = vm.argument(0);
  92. if (!callback.is_function()) {
  93. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, callback.to_string_without_side_effects());
  94. return nullptr;
  95. }
  96. return &callback.as_function();
  97. }
  98. static size_t get_length(VM& vm, Object& object)
  99. {
  100. auto length_property = object.get(vm.names.length);
  101. if (vm.exception())
  102. return 0;
  103. return length_property.to_size_t(object.global_object());
  104. }
  105. static void for_each_item(VM& vm, GlobalObject& global_object, const String& name, AK::Function<IterationDecision(size_t index, Value value, Value callback_result)> callback, bool skip_empty = true)
  106. {
  107. auto* this_object = vm.this_value(global_object).to_object(global_object);
  108. if (!this_object)
  109. return;
  110. auto initial_length = get_length(vm, *this_object);
  111. if (vm.exception())
  112. return;
  113. auto* callback_function = callback_from_args(global_object, name);
  114. if (!callback_function)
  115. return;
  116. auto this_value = vm.argument(1);
  117. for (size_t i = 0; i < initial_length; ++i) {
  118. auto value = this_object->get(i);
  119. if (vm.exception())
  120. return;
  121. if (value.is_empty()) {
  122. if (skip_empty)
  123. continue;
  124. value = js_undefined();
  125. }
  126. auto callback_result = vm.call(*callback_function, this_value, value, Value((i32)i), this_object);
  127. if (vm.exception())
  128. return;
  129. if (callback(i, value, callback_result) == IterationDecision::Break)
  130. break;
  131. }
  132. }
  133. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::filter)
  134. {
  135. auto* new_array = Array::create(global_object);
  136. for_each_item(vm, global_object, "filter", [&](auto, auto value, auto callback_result) {
  137. if (callback_result.to_boolean())
  138. new_array->indexed_properties().append(value);
  139. return IterationDecision::Continue;
  140. });
  141. return Value(new_array);
  142. }
  143. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::for_each)
  144. {
  145. for_each_item(vm, global_object, "forEach", [](auto, auto, auto) {
  146. return IterationDecision::Continue;
  147. });
  148. return js_undefined();
  149. }
  150. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::map)
  151. {
  152. auto* this_object = vm.this_value(global_object).to_object(global_object);
  153. if (!this_object)
  154. return {};
  155. auto initial_length = get_length(vm, *this_object);
  156. if (vm.exception())
  157. return {};
  158. auto* new_array = Array::create(global_object);
  159. new_array->indexed_properties().set_array_like_size(initial_length);
  160. for_each_item(vm, global_object, "map", [&](auto index, auto, auto callback_result) {
  161. if (vm.exception())
  162. return IterationDecision::Break;
  163. new_array->define_property(index, callback_result);
  164. return IterationDecision::Continue;
  165. });
  166. return Value(new_array);
  167. }
  168. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::push)
  169. {
  170. auto* this_object = vm.this_value(global_object).to_object(global_object);
  171. if (!this_object)
  172. return {};
  173. if (this_object->is_array()) {
  174. auto* array = static_cast<Array*>(this_object);
  175. for (size_t i = 0; i < vm.argument_count(); ++i)
  176. array->indexed_properties().append(vm.argument(i));
  177. return Value(static_cast<i32>(array->indexed_properties().array_like_size()));
  178. }
  179. auto length = get_length(vm, *this_object);
  180. if (vm.exception())
  181. return {};
  182. auto argument_count = vm.argument_count();
  183. auto new_length = length + argument_count;
  184. if (new_length > MAX_ARRAY_LIKE_INDEX) {
  185. vm.throw_exception<TypeError>(global_object, ErrorType::ArrayMaxSize);
  186. return {};
  187. }
  188. for (size_t i = 0; i < argument_count; ++i) {
  189. this_object->put(length + i, vm.argument(i));
  190. if (vm.exception())
  191. return {};
  192. }
  193. auto new_length_value = Value((i32)new_length);
  194. this_object->put(vm.names.length, new_length_value);
  195. if (vm.exception())
  196. return {};
  197. return new_length_value;
  198. }
  199. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::unshift)
  200. {
  201. auto* array = Array::typed_this(vm, global_object);
  202. if (!array)
  203. return {};
  204. for (size_t i = 0; i < vm.argument_count(); ++i)
  205. array->indexed_properties().insert(i, vm.argument(i));
  206. return Value(static_cast<i32>(array->indexed_properties().array_like_size()));
  207. }
  208. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::pop)
  209. {
  210. auto* this_object = vm.this_value(global_object).to_object(global_object);
  211. if (!this_object)
  212. return {};
  213. if (this_object->is_array()) {
  214. auto* array = static_cast<Array*>(this_object);
  215. if (array->indexed_properties().is_empty())
  216. return js_undefined();
  217. return array->indexed_properties().take_last(array).value.value_or(js_undefined());
  218. }
  219. auto length = get_length(vm, *this_object);
  220. if (length == 0) {
  221. this_object->put(vm.names.length, Value(0));
  222. return js_undefined();
  223. }
  224. auto index = length - 1;
  225. auto element = this_object->get(index).value_or(js_undefined());
  226. if (vm.exception())
  227. return {};
  228. this_object->delete_property(index);
  229. if (vm.exception())
  230. return {};
  231. this_object->put(vm.names.length, Value((i32)index));
  232. if (vm.exception())
  233. return {};
  234. return element;
  235. }
  236. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::shift)
  237. {
  238. auto* array = Array::typed_this(vm, global_object);
  239. if (!array)
  240. return {};
  241. if (array->indexed_properties().is_empty())
  242. return js_undefined();
  243. auto result = array->indexed_properties().take_first(array);
  244. if (vm.exception())
  245. return {};
  246. return result.value.value_or(js_undefined());
  247. }
  248. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::to_string)
  249. {
  250. auto* this_object = vm.this_value(global_object).to_object(global_object);
  251. if (!this_object)
  252. return {};
  253. auto join_function = this_object->get(vm.names.join);
  254. if (vm.exception())
  255. return {};
  256. if (!join_function.is_function())
  257. return ObjectPrototype::to_string(vm, global_object);
  258. return vm.call(join_function.as_function(), this_object);
  259. }
  260. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::to_locale_string)
  261. {
  262. auto* this_object = vm.this_value(global_object).to_object(global_object);
  263. if (!this_object)
  264. return {};
  265. String separator = ","; // NOTE: This is implementation-specific.
  266. auto length = get_length(vm, *this_object);
  267. if (vm.exception())
  268. return {};
  269. StringBuilder builder;
  270. for (size_t i = 0; i < length; ++i) {
  271. if (i > 0)
  272. builder.append(separator);
  273. auto value = this_object->get(i).value_or(js_undefined());
  274. if (vm.exception())
  275. return {};
  276. if (value.is_nullish())
  277. continue;
  278. auto* value_object = value.to_object(global_object);
  279. ASSERT(value_object);
  280. auto locale_string_result = value_object->invoke("toLocaleString");
  281. if (vm.exception())
  282. return {};
  283. auto string = locale_string_result.to_string(global_object);
  284. if (vm.exception())
  285. return {};
  286. builder.append(string);
  287. }
  288. return js_string(vm, builder.to_string());
  289. }
  290. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::join)
  291. {
  292. auto* this_object = vm.this_value(global_object).to_object(global_object);
  293. if (!this_object)
  294. return {};
  295. // This is not part of the spec, but all major engines do some kind of circular reference checks.
  296. // FWIW: engine262, a "100% spec compliant" ECMA-262 impl, aborts with "too much recursion".
  297. if (s_array_join_seen_objects.contains(this_object))
  298. return js_string(vm, "");
  299. s_array_join_seen_objects.set(this_object);
  300. auto length = get_length(vm, *this_object);
  301. if (vm.exception())
  302. return {};
  303. String separator = ",";
  304. if (!vm.argument(0).is_undefined()) {
  305. separator = vm.argument(0).to_string(global_object);
  306. if (vm.exception())
  307. return {};
  308. }
  309. StringBuilder builder;
  310. for (size_t i = 0; i < length; ++i) {
  311. if (i > 0)
  312. builder.append(separator);
  313. auto value = this_object->get(i).value_or(js_undefined());
  314. if (vm.exception())
  315. return {};
  316. if (value.is_nullish())
  317. continue;
  318. auto string = value.to_string(global_object);
  319. if (vm.exception())
  320. return {};
  321. builder.append(string);
  322. }
  323. s_array_join_seen_objects.remove(this_object);
  324. return js_string(vm, builder.to_string());
  325. }
  326. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::concat)
  327. {
  328. auto* array = Array::typed_this(vm, global_object);
  329. if (!array)
  330. return {};
  331. auto* new_array = Array::create(global_object);
  332. new_array->indexed_properties().append_all(array, array->indexed_properties());
  333. if (vm.exception())
  334. return {};
  335. for (size_t i = 0; i < vm.argument_count(); ++i) {
  336. auto argument = vm.argument(i);
  337. if (argument.is_array()) {
  338. auto& argument_object = argument.as_object();
  339. new_array->indexed_properties().append_all(&argument_object, argument_object.indexed_properties());
  340. if (vm.exception())
  341. return {};
  342. } else {
  343. new_array->indexed_properties().append(argument);
  344. }
  345. }
  346. return Value(new_array);
  347. }
  348. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::slice)
  349. {
  350. auto* array = Array::typed_this(vm, global_object);
  351. if (!array)
  352. return {};
  353. auto* new_array = Array::create(global_object);
  354. if (vm.argument_count() == 0) {
  355. new_array->indexed_properties().append_all(array, array->indexed_properties());
  356. if (vm.exception())
  357. return {};
  358. return new_array;
  359. }
  360. ssize_t array_size = static_cast<ssize_t>(array->indexed_properties().array_like_size());
  361. auto start_slice = vm.argument(0).to_i32(global_object);
  362. if (vm.exception())
  363. return {};
  364. auto end_slice = array_size;
  365. if (start_slice > array_size)
  366. return new_array;
  367. if (start_slice < 0)
  368. start_slice = end_slice + start_slice;
  369. if (vm.argument_count() >= 2) {
  370. end_slice = vm.argument(1).to_i32(global_object);
  371. if (vm.exception())
  372. return {};
  373. if (end_slice < 0)
  374. end_slice = array_size + end_slice;
  375. else if (end_slice > array_size)
  376. end_slice = array_size;
  377. }
  378. for (ssize_t i = start_slice; i < end_slice; ++i) {
  379. new_array->indexed_properties().append(array->get(i));
  380. if (vm.exception())
  381. return {};
  382. }
  383. return new_array;
  384. }
  385. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::index_of)
  386. {
  387. auto* this_object = vm.this_value(global_object).to_object(global_object);
  388. if (!this_object)
  389. return {};
  390. i32 length = get_length(vm, *this_object);
  391. if (vm.exception())
  392. return {};
  393. if (length == 0)
  394. return Value(-1);
  395. i32 from_index = 0;
  396. if (vm.argument_count() >= 2) {
  397. from_index = vm.argument(1).to_i32(global_object);
  398. if (vm.exception())
  399. return {};
  400. if (from_index >= length)
  401. return Value(-1);
  402. if (from_index < 0)
  403. from_index = max(length + from_index, 0);
  404. }
  405. auto search_element = vm.argument(0);
  406. for (i32 i = from_index; i < length; ++i) {
  407. auto element = this_object->get(i);
  408. if (vm.exception())
  409. return {};
  410. if (strict_eq(element, search_element))
  411. return Value(i);
  412. }
  413. return Value(-1);
  414. }
  415. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::reduce)
  416. {
  417. auto* this_object = vm.this_value(global_object).to_object(global_object);
  418. if (!this_object)
  419. return {};
  420. auto initial_length = get_length(vm, *this_object);
  421. if (vm.exception())
  422. return {};
  423. auto* callback_function = callback_from_args(global_object, "reduce");
  424. if (!callback_function)
  425. return {};
  426. size_t start = 0;
  427. auto accumulator = js_undefined();
  428. if (vm.argument_count() > 1) {
  429. accumulator = vm.argument(1);
  430. } else {
  431. bool start_found = false;
  432. while (!start_found && start < initial_length) {
  433. auto value = this_object->get(start);
  434. if (vm.exception())
  435. return {};
  436. start_found = !value.is_empty();
  437. if (start_found)
  438. accumulator = value;
  439. start += 1;
  440. }
  441. if (!start_found) {
  442. vm.throw_exception<TypeError>(global_object, ErrorType::ReduceNoInitial);
  443. return {};
  444. }
  445. }
  446. auto this_value = js_undefined();
  447. for (size_t i = start; i < initial_length; ++i) {
  448. auto value = this_object->get(i);
  449. if (vm.exception())
  450. return {};
  451. if (value.is_empty())
  452. continue;
  453. accumulator = vm.call(*callback_function, this_value, accumulator, value, Value((i32)i), this_object);
  454. if (vm.exception())
  455. return {};
  456. }
  457. return accumulator;
  458. }
  459. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::reduce_right)
  460. {
  461. auto* this_object = vm.this_value(global_object).to_object(global_object);
  462. if (!this_object)
  463. return {};
  464. auto initial_length = get_length(vm, *this_object);
  465. if (vm.exception())
  466. return {};
  467. auto* callback_function = callback_from_args(global_object, "reduceRight");
  468. if (!callback_function)
  469. return {};
  470. int start = initial_length - 1;
  471. auto accumulator = js_undefined();
  472. if (vm.argument_count() > 1) {
  473. accumulator = vm.argument(1);
  474. } else {
  475. bool start_found = false;
  476. while (!start_found && start >= 0) {
  477. auto value = this_object->get(start);
  478. if (vm.exception())
  479. return {};
  480. start_found = !value.is_empty();
  481. if (start_found)
  482. accumulator = value;
  483. start -= 1;
  484. }
  485. if (!start_found) {
  486. vm.throw_exception<TypeError>(global_object, ErrorType::ReduceNoInitial);
  487. return {};
  488. }
  489. }
  490. auto this_value = js_undefined();
  491. for (int i = start; i >= 0; --i) {
  492. auto value = this_object->get(i);
  493. if (vm.exception())
  494. return {};
  495. if (value.is_empty())
  496. continue;
  497. accumulator = vm.call(*callback_function, this_value, accumulator, value, Value((i32)i), this_object);
  498. if (vm.exception())
  499. return {};
  500. }
  501. return accumulator;
  502. }
  503. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::reverse)
  504. {
  505. auto* array = Array::typed_this(vm, global_object);
  506. if (!array)
  507. return {};
  508. if (array->indexed_properties().is_empty())
  509. return array;
  510. MarkedValueList array_reverse(vm.heap());
  511. auto size = array->indexed_properties().array_like_size();
  512. array_reverse.ensure_capacity(size);
  513. for (ssize_t i = size - 1; i >= 0; --i) {
  514. array_reverse.append(array->get(i));
  515. if (vm.exception())
  516. return {};
  517. }
  518. array->set_indexed_property_elements(move(array_reverse));
  519. return array;
  520. }
  521. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::last_index_of)
  522. {
  523. auto* this_object = vm.this_value(global_object).to_object(global_object);
  524. if (!this_object)
  525. return {};
  526. i32 length = get_length(vm, *this_object);
  527. if (vm.exception())
  528. return {};
  529. if (length == 0)
  530. return Value(-1);
  531. i32 from_index = length - 1;
  532. if (vm.argument_count() >= 2) {
  533. from_index = vm.argument(1).to_i32(global_object);
  534. if (vm.exception())
  535. return {};
  536. if (from_index >= 0)
  537. from_index = min(from_index, length - 1);
  538. else
  539. from_index = length + from_index;
  540. }
  541. auto search_element = vm.argument(0);
  542. for (i32 i = from_index; i >= 0; --i) {
  543. auto element = this_object->get(i);
  544. if (vm.exception())
  545. return {};
  546. if (strict_eq(element, search_element))
  547. return Value(i);
  548. }
  549. return Value(-1);
  550. }
  551. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::includes)
  552. {
  553. auto* this_object = vm.this_value(global_object).to_object(global_object);
  554. if (!this_object)
  555. return {};
  556. i32 length = get_length(vm, *this_object);
  557. if (vm.exception())
  558. return {};
  559. if (length == 0)
  560. return Value(false);
  561. i32 from_index = 0;
  562. if (vm.argument_count() >= 2) {
  563. from_index = vm.argument(1).to_i32(global_object);
  564. if (vm.exception())
  565. return {};
  566. if (from_index >= length)
  567. return Value(false);
  568. if (from_index < 0)
  569. from_index = max(length + from_index, 0);
  570. }
  571. auto value_to_find = vm.argument(0);
  572. for (i32 i = from_index; i < length; ++i) {
  573. auto element = this_object->get(i).value_or(js_undefined());
  574. if (vm.exception())
  575. return {};
  576. if (same_value_zero(element, value_to_find))
  577. return Value(true);
  578. }
  579. return Value(false);
  580. }
  581. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::find)
  582. {
  583. auto result = js_undefined();
  584. for_each_item(
  585. vm, global_object, "find", [&](auto, auto value, auto callback_result) {
  586. if (callback_result.to_boolean()) {
  587. result = value;
  588. return IterationDecision::Break;
  589. }
  590. return IterationDecision::Continue;
  591. },
  592. false);
  593. return result;
  594. }
  595. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::find_index)
  596. {
  597. auto result_index = -1;
  598. for_each_item(
  599. vm, global_object, "findIndex", [&](auto index, auto, auto callback_result) {
  600. if (callback_result.to_boolean()) {
  601. result_index = index;
  602. return IterationDecision::Break;
  603. }
  604. return IterationDecision::Continue;
  605. },
  606. false);
  607. return Value(result_index);
  608. }
  609. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::some)
  610. {
  611. auto result = false;
  612. for_each_item(vm, global_object, "some", [&](auto, auto, auto callback_result) {
  613. if (callback_result.to_boolean()) {
  614. result = true;
  615. return IterationDecision::Break;
  616. }
  617. return IterationDecision::Continue;
  618. });
  619. return Value(result);
  620. }
  621. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::every)
  622. {
  623. auto result = true;
  624. for_each_item(vm, global_object, "every", [&](auto, auto, auto callback_result) {
  625. if (!callback_result.to_boolean()) {
  626. result = false;
  627. return IterationDecision::Break;
  628. }
  629. return IterationDecision::Continue;
  630. });
  631. return Value(result);
  632. }
  633. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::splice)
  634. {
  635. auto* this_object = vm.this_value(global_object).to_object(global_object);
  636. if (!this_object)
  637. return {};
  638. auto initial_length = get_length(vm, *this_object);
  639. if (vm.exception())
  640. return {};
  641. auto relative_start = vm.argument(0).to_i32(global_object);
  642. if (vm.exception())
  643. return {};
  644. size_t actual_start;
  645. if (relative_start < 0)
  646. actual_start = max((ssize_t)initial_length + relative_start, (ssize_t)0);
  647. else
  648. actual_start = min((size_t)relative_start, initial_length);
  649. size_t insert_count = 0;
  650. size_t actual_delete_count = 0;
  651. if (vm.argument_count() == 1) {
  652. actual_delete_count = initial_length - actual_start;
  653. } else if (vm.argument_count() >= 2) {
  654. insert_count = vm.argument_count() - 2;
  655. i32 delete_count = vm.argument(1).to_i32(global_object);
  656. if (vm.exception())
  657. return {};
  658. actual_delete_count = min((size_t)max(delete_count, 0), initial_length - actual_start);
  659. }
  660. size_t new_length = initial_length + insert_count - actual_delete_count;
  661. if (new_length > MAX_ARRAY_LIKE_INDEX) {
  662. vm.throw_exception<TypeError>(global_object, ErrorType::ArrayMaxSize);
  663. return {};
  664. }
  665. auto removed_elements = Array::create(global_object);
  666. for (size_t i = 0; i < actual_delete_count; ++i) {
  667. auto value = this_object->get(actual_start + i);
  668. if (vm.exception())
  669. return {};
  670. removed_elements->indexed_properties().append(value);
  671. }
  672. if (insert_count < actual_delete_count) {
  673. for (size_t i = actual_start; i < initial_length - actual_delete_count; ++i) {
  674. auto from = this_object->get(i + actual_delete_count);
  675. if (vm.exception())
  676. return {};
  677. auto to = i + insert_count;
  678. if (!from.is_empty()) {
  679. this_object->put(to, from);
  680. } else {
  681. this_object->delete_property(to);
  682. }
  683. if (vm.exception())
  684. return {};
  685. }
  686. for (size_t i = initial_length; i > new_length; --i) {
  687. this_object->delete_property(i - 1);
  688. if (vm.exception())
  689. return {};
  690. }
  691. } else if (insert_count > actual_delete_count) {
  692. for (size_t i = initial_length - actual_delete_count; i > actual_start; --i) {
  693. auto from = this_object->get(i + actual_delete_count - 1);
  694. if (vm.exception())
  695. return {};
  696. auto to = i + insert_count - 1;
  697. if (!from.is_empty()) {
  698. this_object->put(to, from);
  699. } else {
  700. this_object->delete_property(to);
  701. }
  702. if (vm.exception())
  703. return {};
  704. }
  705. }
  706. for (size_t i = 0; i < insert_count; ++i) {
  707. this_object->put(actual_start + i, vm.argument(i + 2));
  708. if (vm.exception())
  709. return {};
  710. }
  711. this_object->put(vm.names.length, Value((i32)new_length));
  712. if (vm.exception())
  713. return {};
  714. return removed_elements;
  715. }
  716. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::fill)
  717. {
  718. auto* this_object = vm.this_value(global_object).to_object(global_object);
  719. if (!this_object)
  720. return {};
  721. ssize_t length = get_length(vm, *this_object);
  722. if (vm.exception())
  723. return {};
  724. ssize_t relative_start = 0;
  725. ssize_t relative_end = length;
  726. if (vm.argument_count() >= 2) {
  727. relative_start = vm.argument(1).to_i32(global_object);
  728. if (vm.exception())
  729. return {};
  730. }
  731. if (vm.argument_count() >= 3) {
  732. relative_end = vm.argument(2).to_i32(global_object);
  733. if (vm.exception())
  734. return {};
  735. }
  736. size_t from, to;
  737. if (relative_start < 0)
  738. from = max(length + relative_start, 0L);
  739. else
  740. from = min(relative_start, length);
  741. if (relative_end < 0)
  742. to = max(length + relative_end, 0L);
  743. else
  744. to = min(relative_end, length);
  745. for (size_t i = from; i < to; i++) {
  746. this_object->put(i, vm.argument(0));
  747. if (vm.exception())
  748. return {};
  749. }
  750. return this_object;
  751. }
  752. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::values)
  753. {
  754. auto* this_object = vm.this_value(global_object).to_object(global_object);
  755. if (!this_object)
  756. return {};
  757. return ArrayIterator::create(global_object, this_object, Object::PropertyKind::Value);
  758. }
  759. }