ArrayPrototype.cpp 27 KB

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