ArrayPrototype.cpp 28 KB

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