ArrayPrototype.cpp 28 KB

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