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