ArrayPrototype.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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/StringBuilder.h>
  30. #include <LibJS/Heap/Heap.h>
  31. #include <LibJS/Interpreter.h>
  32. #include <LibJS/Runtime/Array.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/MarkedValueList.h>
  38. #include <LibJS/Runtime/ObjectPrototype.h>
  39. #include <LibJS/Runtime/Value.h>
  40. namespace JS {
  41. ArrayPrototype::ArrayPrototype()
  42. : Object(interpreter().global_object().object_prototype())
  43. {
  44. u8 attr = Attribute::Writable | Attribute::Configurable;
  45. define_native_function("filter", filter, 1, attr);
  46. define_native_function("forEach", for_each, 1, attr);
  47. define_native_function("map", map, 1, attr);
  48. define_native_function("pop", pop, 0, attr);
  49. define_native_function("push", push, 1, attr);
  50. define_native_function("shift", shift, 0, attr);
  51. define_native_function("toString", to_string, 0, attr);
  52. define_native_function("toLocaleString", to_locale_string, 0, attr);
  53. define_native_function("unshift", unshift, 1, attr);
  54. define_native_function("join", join, 1, attr);
  55. define_native_function("concat", concat, 1, attr);
  56. define_native_function("slice", slice, 2, attr);
  57. define_native_function("indexOf", index_of, 1, attr);
  58. define_native_function("reduce", reduce, 1, attr);
  59. define_native_function("reduceRight", reduce_right, 1, attr);
  60. define_native_function("reverse", reverse, 0, attr);
  61. define_native_function("lastIndexOf", last_index_of, 1, attr);
  62. define_native_function("includes", includes, 1, attr);
  63. define_native_function("find", find, 1, attr);
  64. define_native_function("findIndex", find_index, 1, attr);
  65. define_native_function("some", some, 1, attr);
  66. define_native_function("every", every, 1, attr);
  67. define_native_function("splice", splice, 2, attr);
  68. define_native_function("fill", fill, 1, attr);
  69. define_property("length", Value(0), Attribute::Configurable);
  70. }
  71. ArrayPrototype::~ArrayPrototype()
  72. {
  73. }
  74. static Function* callback_from_args(Interpreter& interpreter, const String& name)
  75. {
  76. if (interpreter.argument_count() < 1) {
  77. interpreter.throw_exception<TypeError>(String::format("Array.prototype.%s() requires at least one argument", name.characters()));
  78. return nullptr;
  79. }
  80. auto callback = interpreter.argument(0);
  81. if (!callback.is_function()) {
  82. interpreter.throw_exception<TypeError>(String::format("%s is not a function", callback.to_string_without_side_effects().characters()));
  83. return nullptr;
  84. }
  85. return &callback.as_function();
  86. }
  87. static size_t get_length(Interpreter& interpreter, Object& object)
  88. {
  89. auto length_property = object.get("length");
  90. if (interpreter.exception())
  91. return 0;
  92. return length_property.to_size_t(interpreter);
  93. }
  94. static void for_each_item(Interpreter& interpreter, const String& name, AK::Function<IterationDecision(size_t index, Value value, Value callback_result)> callback, bool skip_empty = true)
  95. {
  96. auto* this_object = interpreter.this_value().to_object(interpreter);
  97. if (!this_object)
  98. return;
  99. auto initial_length = get_length(interpreter, *this_object);
  100. if (interpreter.exception())
  101. return;
  102. auto* callback_function = callback_from_args(interpreter, name);
  103. if (!callback_function)
  104. return;
  105. auto this_value = interpreter.argument(1);
  106. for (size_t i = 0; i < initial_length; ++i) {
  107. auto value = this_object->get(i);
  108. if (interpreter.exception())
  109. return;
  110. if (value.is_empty()) {
  111. if (skip_empty)
  112. continue;
  113. value = js_undefined();
  114. }
  115. MarkedValueList arguments(interpreter.heap());
  116. arguments.append(value);
  117. arguments.append(Value((i32)i));
  118. arguments.append(this_object);
  119. auto callback_result = interpreter.call(*callback_function, this_value, move(arguments));
  120. if (interpreter.exception())
  121. return;
  122. if (callback(i, value, callback_result) == IterationDecision::Break)
  123. break;
  124. }
  125. }
  126. Value ArrayPrototype::filter(Interpreter& interpreter)
  127. {
  128. auto* new_array = Array::create(interpreter.global_object());
  129. for_each_item(interpreter, "filter", [&](auto, auto value, auto callback_result) {
  130. if (callback_result.to_boolean())
  131. new_array->indexed_properties().append(value);
  132. return IterationDecision::Continue;
  133. });
  134. return Value(new_array);
  135. }
  136. Value ArrayPrototype::for_each(Interpreter& interpreter)
  137. {
  138. for_each_item(interpreter, "forEach", [](auto, auto, auto) {
  139. return IterationDecision::Continue;
  140. });
  141. return js_undefined();
  142. }
  143. Value ArrayPrototype::map(Interpreter& interpreter)
  144. {
  145. auto* this_object = interpreter.this_value().to_object(interpreter);
  146. if (!this_object)
  147. return {};
  148. auto initial_length = get_length(interpreter, *this_object);
  149. if (interpreter.exception())
  150. return {};
  151. auto* new_array = Array::create(interpreter.global_object());
  152. new_array->indexed_properties().set_array_like_size(initial_length);
  153. for_each_item(interpreter, "map", [&](auto index, auto, auto callback_result) {
  154. new_array->put(index, callback_result);
  155. return IterationDecision::Continue;
  156. });
  157. return Value(new_array);
  158. }
  159. Value ArrayPrototype::push(Interpreter& interpreter)
  160. {
  161. auto* this_object = interpreter.this_value().to_object(interpreter);
  162. if (!this_object)
  163. return {};
  164. if (this_object->is_array()) {
  165. auto* array = static_cast<Array*>(this_object);
  166. for (size_t i = 0; i < interpreter.argument_count(); ++i)
  167. array->indexed_properties().append(interpreter.argument(i));
  168. return Value(static_cast<i32>(array->indexed_properties().array_like_size()));
  169. }
  170. auto length = get_length(interpreter, *this_object);
  171. if (interpreter.exception())
  172. return {};
  173. auto argument_count = interpreter.argument_count();
  174. auto new_length = length + argument_count;
  175. if (new_length > MAX_ARRAY_LIKE_INDEX)
  176. return interpreter.throw_exception<TypeError>("Maximum array size exceeded");
  177. for (size_t i = 0; i < argument_count; ++i)
  178. this_object->put(length + i, interpreter.argument(i));
  179. auto new_length_value = Value((i32)new_length);
  180. this_object->put("length", new_length_value);
  181. if (interpreter.exception())
  182. return {};
  183. return new_length_value;
  184. }
  185. Value ArrayPrototype::unshift(Interpreter& interpreter)
  186. {
  187. auto* array = array_from(interpreter);
  188. if (!array)
  189. return {};
  190. for (size_t i = 0; i < interpreter.argument_count(); ++i)
  191. array->indexed_properties().insert(i, interpreter.argument(i));
  192. return Value(static_cast<i32>(array->indexed_properties().array_like_size()));
  193. }
  194. Value ArrayPrototype::pop(Interpreter& interpreter)
  195. {
  196. auto* this_object = interpreter.this_value().to_object(interpreter);
  197. if (!this_object)
  198. return {};
  199. if (this_object->is_array()) {
  200. auto* array = static_cast<Array*>(this_object);
  201. if (array->indexed_properties().is_empty())
  202. return js_undefined();
  203. return array->indexed_properties().take_last(array).value.value_or(js_undefined());
  204. }
  205. auto length = get_length(interpreter, *this_object);
  206. if (length == 0) {
  207. this_object->put("length", Value(0));
  208. return js_undefined();
  209. }
  210. auto index = length - 1;
  211. auto element = this_object->get(index).value_or(js_undefined());
  212. if (interpreter.exception())
  213. return {};
  214. this_object->delete_property(index);
  215. this_object->put("length", Value((i32)index));
  216. if (interpreter.exception())
  217. return {};
  218. return element;
  219. }
  220. Value ArrayPrototype::shift(Interpreter& interpreter)
  221. {
  222. auto* array = array_from(interpreter);
  223. if (!array)
  224. return {};
  225. if (array->indexed_properties().is_empty())
  226. return js_undefined();
  227. auto result = array->indexed_properties().take_first(array);
  228. if (interpreter.exception())
  229. return {};
  230. return result.value.value_or(js_undefined());
  231. }
  232. Value ArrayPrototype::to_string(Interpreter& interpreter)
  233. {
  234. auto* this_object = interpreter.this_value().to_object(interpreter);
  235. if (!this_object)
  236. return {};
  237. auto join_function = this_object->get("join");
  238. if (interpreter.exception())
  239. return {};
  240. if (!join_function.is_function())
  241. return ObjectPrototype::to_string(interpreter);
  242. return interpreter.call(join_function.as_function(), this_object);
  243. }
  244. Value ArrayPrototype::to_locale_string(Interpreter& interpreter)
  245. {
  246. auto* this_object = interpreter.this_value().to_object(interpreter);
  247. if (!this_object)
  248. return {};
  249. String separator = ","; // NOTE: This is implementation-specific.
  250. auto length = get_length(interpreter, *this_object);
  251. if (interpreter.exception())
  252. return {};
  253. StringBuilder builder;
  254. for (size_t i = 0; i < length; ++i) {
  255. if (i > 0)
  256. builder.append(separator);
  257. auto value = this_object->get(i).value_or(js_undefined());
  258. if (interpreter.exception())
  259. return {};
  260. if (value.is_undefined() || value.is_null())
  261. continue;
  262. auto* value_object = value.to_object(interpreter);
  263. ASSERT(value_object);
  264. auto locale_string_result = value_object->invoke("toLocaleString");
  265. if (interpreter.exception())
  266. return {};
  267. auto string = locale_string_result.to_string(interpreter);
  268. if (interpreter.exception())
  269. return {};
  270. builder.append(string);
  271. }
  272. return js_string(interpreter, builder.to_string());
  273. }
  274. Value ArrayPrototype::join(Interpreter& interpreter)
  275. {
  276. auto* this_object = interpreter.this_value().to_object(interpreter);
  277. if (!this_object)
  278. return {};
  279. String separator = ",";
  280. if (interpreter.argument_count()) {
  281. separator = interpreter.argument(0).to_string(interpreter);
  282. if (interpreter.exception())
  283. return {};
  284. }
  285. auto length = get_length(interpreter, *this_object);
  286. if (interpreter.exception())
  287. return {};
  288. StringBuilder builder;
  289. for (size_t i = 0; i < length; ++i) {
  290. if (i > 0)
  291. builder.append(separator);
  292. auto value = this_object->get(i).value_or(js_undefined());
  293. if (interpreter.exception())
  294. return {};
  295. if (value.is_undefined() || value.is_null())
  296. continue;
  297. auto string = value.to_string(interpreter);
  298. if (interpreter.exception())
  299. return {};
  300. builder.append(string);
  301. }
  302. return js_string(interpreter, builder.to_string());
  303. }
  304. Value ArrayPrototype::concat(Interpreter& interpreter)
  305. {
  306. auto* array = array_from(interpreter);
  307. if (!array)
  308. return {};
  309. auto* new_array = Array::create(interpreter.global_object());
  310. new_array->indexed_properties().append_all(array, array->indexed_properties());
  311. if (interpreter.exception())
  312. return {};
  313. for (size_t i = 0; i < interpreter.argument_count(); ++i) {
  314. auto argument = interpreter.argument(i);
  315. if (argument.is_array()) {
  316. auto& argument_object = argument.as_object();
  317. new_array->indexed_properties().append_all(&argument_object, argument_object.indexed_properties());
  318. if (interpreter.exception())
  319. return {};
  320. } else {
  321. new_array->indexed_properties().append(argument);
  322. }
  323. }
  324. return Value(new_array);
  325. }
  326. Value ArrayPrototype::slice(Interpreter& interpreter)
  327. {
  328. auto* array = array_from(interpreter);
  329. if (!array)
  330. return {};
  331. auto* new_array = Array::create(interpreter.global_object());
  332. if (interpreter.argument_count() == 0) {
  333. new_array->indexed_properties().append_all(array, array->indexed_properties());
  334. if (interpreter.exception())
  335. return {};
  336. return new_array;
  337. }
  338. ssize_t array_size = static_cast<ssize_t>(array->indexed_properties().array_like_size());
  339. auto start_slice = interpreter.argument(0).to_i32(interpreter);
  340. if (interpreter.exception())
  341. return {};
  342. auto end_slice = array_size;
  343. if (start_slice > array_size)
  344. return new_array;
  345. if (start_slice < 0)
  346. start_slice = end_slice + start_slice;
  347. if (interpreter.argument_count() >= 2) {
  348. end_slice = interpreter.argument(1).to_i32(interpreter);
  349. if (interpreter.exception())
  350. return {};
  351. if (end_slice < 0)
  352. end_slice = array_size + end_slice;
  353. else if (end_slice > array_size)
  354. end_slice = array_size;
  355. }
  356. for (ssize_t i = start_slice; i < end_slice; ++i) {
  357. new_array->indexed_properties().append(array->get(i));
  358. if (interpreter.exception())
  359. return {};
  360. }
  361. return new_array;
  362. }
  363. Value ArrayPrototype::index_of(Interpreter& interpreter)
  364. {
  365. auto* this_object = interpreter.this_value().to_object(interpreter);
  366. if (!this_object)
  367. return {};
  368. i32 length = get_length(interpreter, *this_object);
  369. if (interpreter.exception())
  370. return {};
  371. if (length == 0)
  372. return Value(-1);
  373. i32 from_index = 0;
  374. if (interpreter.argument_count() >= 2) {
  375. from_index = interpreter.argument(1).to_i32(interpreter);
  376. if (interpreter.exception())
  377. return {};
  378. if (from_index >= length)
  379. return Value(-1);
  380. if (from_index < 0)
  381. from_index = max(length + from_index, 0);
  382. }
  383. auto search_element = interpreter.argument(0);
  384. for (i32 i = from_index; i < length; ++i) {
  385. auto element = this_object->get(i);
  386. if (interpreter.exception())
  387. return {};
  388. if (strict_eq(interpreter, element, search_element))
  389. return Value(i);
  390. }
  391. return Value(-1);
  392. }
  393. Value ArrayPrototype::reduce(Interpreter& interpreter)
  394. {
  395. auto* this_object = interpreter.this_value().to_object(interpreter);
  396. if (!this_object)
  397. return {};
  398. auto initial_length = get_length(interpreter, *this_object);
  399. if (interpreter.exception())
  400. return {};
  401. auto* callback_function = callback_from_args(interpreter, "reduce");
  402. if (!callback_function)
  403. return {};
  404. size_t start = 0;
  405. auto accumulator = js_undefined();
  406. if (interpreter.argument_count() > 1) {
  407. accumulator = interpreter.argument(1);
  408. } else {
  409. bool start_found = false;
  410. while (!start_found && start < initial_length) {
  411. auto value = this_object->get(start);
  412. if (interpreter.exception())
  413. return {};
  414. start_found = !value.is_empty();
  415. if (start_found)
  416. accumulator = value;
  417. start += 1;
  418. }
  419. if (!start_found) {
  420. interpreter.throw_exception<TypeError>("Reduce of empty array with no initial value");
  421. return {};
  422. }
  423. }
  424. auto this_value = js_undefined();
  425. for (size_t i = start; i < initial_length; ++i) {
  426. auto value = this_object->get(i);
  427. if (interpreter.exception())
  428. return {};
  429. if (value.is_empty())
  430. continue;
  431. MarkedValueList arguments(interpreter.heap());
  432. arguments.append(accumulator);
  433. arguments.append(value);
  434. arguments.append(Value((i32)i));
  435. arguments.append(this_object);
  436. accumulator = interpreter.call(*callback_function, this_value, move(arguments));
  437. if (interpreter.exception())
  438. return {};
  439. }
  440. return accumulator;
  441. }
  442. Value ArrayPrototype::reduce_right(Interpreter& interpreter)
  443. {
  444. auto* this_object = interpreter.this_value().to_object(interpreter);
  445. if (!this_object)
  446. return {};
  447. auto initial_length = get_length(interpreter, *this_object);
  448. if (interpreter.exception())
  449. return {};
  450. auto* callback_function = callback_from_args(interpreter, "reduceRight");
  451. if (!callback_function)
  452. return {};
  453. int start = initial_length - 1;
  454. auto accumulator = js_undefined();
  455. if (interpreter.argument_count() > 1) {
  456. accumulator = interpreter.argument(1);
  457. } else {
  458. bool start_found = false;
  459. while (!start_found && start >= 0) {
  460. auto value = this_object->get(start);
  461. if (interpreter.exception())
  462. return {};
  463. start_found = !value.is_empty();
  464. if (start_found)
  465. accumulator = value;
  466. start -= 1;
  467. }
  468. if (!start_found) {
  469. interpreter.throw_exception<TypeError>("Reduce of empty array with no initial value");
  470. return {};
  471. }
  472. }
  473. auto this_value = js_undefined();
  474. for (int i = start; i >= 0; --i) {
  475. auto value = this_object->get(i);
  476. if (interpreter.exception())
  477. return {};
  478. if (value.is_empty())
  479. continue;
  480. MarkedValueList arguments(interpreter.heap());
  481. arguments.append(accumulator);
  482. arguments.append(value);
  483. arguments.append(Value(i));
  484. arguments.append(this_object);
  485. accumulator = interpreter.call(*callback_function, this_value, move(arguments));
  486. if (interpreter.exception())
  487. return {};
  488. }
  489. return accumulator;
  490. }
  491. Value ArrayPrototype::reverse(Interpreter& interpreter)
  492. {
  493. auto* array = array_from(interpreter);
  494. if (!array)
  495. return {};
  496. if (array->indexed_properties().is_empty())
  497. return array;
  498. Vector<Value> array_reverse;
  499. auto size = array->indexed_properties().array_like_size();
  500. array_reverse.ensure_capacity(size);
  501. for (ssize_t i = size - 1; i >= 0; --i) {
  502. array_reverse.append(array->get(i));
  503. if (interpreter.exception())
  504. return {};
  505. }
  506. array->set_indexed_property_elements(move(array_reverse));
  507. return array;
  508. }
  509. Value ArrayPrototype::last_index_of(Interpreter& interpreter)
  510. {
  511. auto* this_object = interpreter.this_value().to_object(interpreter);
  512. if (!this_object)
  513. return {};
  514. i32 length = get_length(interpreter, *this_object);
  515. if (interpreter.exception())
  516. return {};
  517. if (length == 0)
  518. return Value(-1);
  519. i32 from_index = length - 1;
  520. if (interpreter.argument_count() >= 2) {
  521. from_index = interpreter.argument(1).to_i32(interpreter);
  522. if (interpreter.exception())
  523. return {};
  524. if (from_index >= 0)
  525. from_index = min(from_index, length - 1);
  526. else
  527. from_index = length + from_index;
  528. }
  529. auto search_element = interpreter.argument(0);
  530. for (i32 i = from_index; i >= 0; --i) {
  531. auto element = this_object->get(i);
  532. if (interpreter.exception())
  533. return {};
  534. if (strict_eq(interpreter, element, search_element))
  535. return Value(i);
  536. }
  537. return Value(-1);
  538. }
  539. Value ArrayPrototype::includes(Interpreter& interpreter)
  540. {
  541. auto* this_object = interpreter.this_value().to_object(interpreter);
  542. if (!this_object)
  543. return {};
  544. i32 length = get_length(interpreter, *this_object);
  545. if (interpreter.exception())
  546. return {};
  547. if (length == 0)
  548. return Value(false);
  549. i32 from_index = 0;
  550. if (interpreter.argument_count() >= 2) {
  551. from_index = interpreter.argument(1).to_i32(interpreter);
  552. if (interpreter.exception())
  553. return {};
  554. if (from_index >= length)
  555. return Value(false);
  556. if (from_index < 0)
  557. from_index = max(length + from_index, 0);
  558. }
  559. auto value_to_find = interpreter.argument(0);
  560. for (i32 i = from_index; i < length; ++i) {
  561. auto element = this_object->get(i).value_or(js_undefined());
  562. if (interpreter.exception())
  563. return {};
  564. if (same_value_zero(interpreter, element, value_to_find))
  565. return Value(true);
  566. }
  567. return Value(false);
  568. }
  569. Value ArrayPrototype::find(Interpreter& interpreter)
  570. {
  571. auto result = js_undefined();
  572. for_each_item(
  573. interpreter, "find", [&](auto, auto value, auto callback_result) {
  574. if (callback_result.to_boolean()) {
  575. result = value;
  576. return IterationDecision::Break;
  577. }
  578. return IterationDecision::Continue;
  579. },
  580. false);
  581. return result;
  582. }
  583. Value ArrayPrototype::find_index(Interpreter& interpreter)
  584. {
  585. auto result_index = -1;
  586. for_each_item(
  587. interpreter, "findIndex", [&](auto index, auto, auto callback_result) {
  588. if (callback_result.to_boolean()) {
  589. result_index = index;
  590. return IterationDecision::Break;
  591. }
  592. return IterationDecision::Continue;
  593. },
  594. false);
  595. return Value(result_index);
  596. }
  597. Value ArrayPrototype::some(Interpreter& interpreter)
  598. {
  599. auto result = false;
  600. for_each_item(interpreter, "some", [&](auto, auto, auto callback_result) {
  601. if (callback_result.to_boolean()) {
  602. result = true;
  603. return IterationDecision::Break;
  604. }
  605. return IterationDecision::Continue;
  606. });
  607. return Value(result);
  608. }
  609. Value ArrayPrototype::every(Interpreter& interpreter)
  610. {
  611. auto result = true;
  612. for_each_item(interpreter, "every", [&](auto, auto, auto callback_result) {
  613. if (!callback_result.to_boolean()) {
  614. result = false;
  615. return IterationDecision::Break;
  616. }
  617. return IterationDecision::Continue;
  618. });
  619. return Value(result);
  620. }
  621. Value ArrayPrototype::splice(Interpreter& interpreter)
  622. {
  623. auto* this_object = interpreter.this_value().to_object(interpreter);
  624. if (!this_object)
  625. return {};
  626. auto initial_length = get_length(interpreter, *this_object);
  627. if (interpreter.exception())
  628. return {};
  629. auto relative_start = interpreter.argument(0).to_i32(interpreter);
  630. if (interpreter.exception())
  631. return {};
  632. size_t actual_start;
  633. if (relative_start < 0)
  634. actual_start = max((ssize_t)initial_length + relative_start, (ssize_t)0);
  635. else
  636. actual_start = min((size_t)relative_start, initial_length);
  637. size_t insert_count = 0;
  638. size_t actual_delete_count = 0;
  639. if (interpreter.argument_count() == 1) {
  640. actual_delete_count = initial_length - actual_start;
  641. } else if (interpreter.argument_count() >= 2) {
  642. insert_count = interpreter.argument_count() - 2;
  643. i32 delete_count = interpreter.argument(1).to_i32(interpreter);
  644. if (interpreter.exception())
  645. return {};
  646. actual_delete_count = min((size_t)max(delete_count, 0), initial_length - actual_start);
  647. }
  648. size_t new_length = initial_length + insert_count - actual_delete_count;
  649. if (new_length > MAX_ARRAY_LIKE_INDEX)
  650. return interpreter.throw_exception<TypeError>("Maximum array size exceeded");
  651. auto removed_elements = Array::create(interpreter.global_object());
  652. for (size_t i = 0; i < actual_delete_count; ++i) {
  653. auto value = this_object->get(actual_start + i);
  654. if (interpreter.exception())
  655. return {};
  656. removed_elements->indexed_properties().append(value);
  657. }
  658. if (insert_count < actual_delete_count) {
  659. for (size_t i = actual_start; i < initial_length - actual_delete_count; ++i) {
  660. auto from = this_object->get(i + actual_delete_count);
  661. if (interpreter.exception())
  662. return {};
  663. auto to = i + insert_count;
  664. if (!from.is_empty()) {
  665. this_object->put(to, from);
  666. if (interpreter.exception())
  667. return {};
  668. } else {
  669. this_object->delete_property(to);
  670. }
  671. }
  672. for (size_t i = initial_length; i > new_length; --i)
  673. this_object->delete_property(i - 1);
  674. } else if (insert_count > actual_delete_count) {
  675. for (size_t i = initial_length - actual_delete_count; i > actual_start; --i) {
  676. auto from = this_object->get(i + actual_delete_count - 1);
  677. if (interpreter.exception())
  678. return {};
  679. auto to = i + insert_count - 1;
  680. if (!from.is_empty()) {
  681. this_object->put(to, from);
  682. if (interpreter.exception())
  683. return {};
  684. } else {
  685. this_object->delete_property(to);
  686. }
  687. }
  688. }
  689. for (size_t i = 0; i < insert_count; ++i) {
  690. this_object->put(actual_start + i, interpreter.argument(i + 2));
  691. if (interpreter.exception())
  692. return {};
  693. }
  694. this_object->put("length", Value((i32)new_length));
  695. if (interpreter.exception())
  696. return {};
  697. return removed_elements;
  698. }
  699. Value ArrayPrototype::fill(Interpreter& interpreter)
  700. {
  701. auto* this_object = interpreter.this_value().to_object(interpreter);
  702. if (!this_object)
  703. return {};
  704. ssize_t length = get_length(interpreter, *this_object);
  705. if (interpreter.exception())
  706. return {};
  707. ssize_t relative_start = 0;
  708. ssize_t relative_end = length;
  709. if (interpreter.argument_count() >= 2) {
  710. relative_start = interpreter.argument(1).to_i32(interpreter);
  711. if (interpreter.exception())
  712. return {};
  713. }
  714. if (interpreter.argument_count() >= 3) {
  715. relative_end = interpreter.argument(2).to_i32(interpreter);
  716. if (interpreter.exception())
  717. return {};
  718. }
  719. size_t from, to;
  720. if (relative_start < 0)
  721. from = max(length + relative_start, 0L);
  722. else
  723. from = min(relative_start, length);
  724. if (relative_end < 0)
  725. to = max(length + relative_end, 0L);
  726. else
  727. to = min(relative_end, length);
  728. for (size_t i = from; i < to; i++) {
  729. this_object->put(i, interpreter.argument(0));
  730. if (interpreter.exception())
  731. return {};
  732. }
  733. return this_object;
  734. }
  735. }