ArrayPrototype.cpp 27 KB

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