ArrayPrototype.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  4. * Copyright (c) 2020, Marcin Gasperowicz <xnooga@gmail.com>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice, this
  11. * list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <AK/Function.h>
  29. #include <AK/StringBuilder.h>
  30. #include <LibJS/Interpreter.h>
  31. #include <LibJS/Runtime/Array.h>
  32. #include <LibJS/Runtime/ArrayIterator.h>
  33. #include <LibJS/Runtime/ArrayPrototype.h>
  34. #include <LibJS/Runtime/Error.h>
  35. #include <LibJS/Runtime/Function.h>
  36. #include <LibJS/Runtime/GlobalObject.h>
  37. #include <LibJS/Runtime/ObjectPrototype.h>
  38. #include <LibJS/Runtime/Value.h>
  39. namespace JS {
  40. ArrayPrototype::ArrayPrototype(GlobalObject& global_object)
  41. : Object(*global_object.object_prototype())
  42. {
  43. }
  44. void ArrayPrototype::initialize(GlobalObject& global_object)
  45. {
  46. Object::initialize(global_object);
  47. u8 attr = Attribute::Writable | Attribute::Configurable;
  48. define_native_function("filter", filter, 1, attr);
  49. define_native_function("forEach", for_each, 1, attr);
  50. define_native_function("map", map, 1, attr);
  51. define_native_function("pop", pop, 0, attr);
  52. define_native_function("push", push, 1, attr);
  53. define_native_function("shift", shift, 0, attr);
  54. define_native_function("toString", to_string, 0, attr);
  55. define_native_function("toLocaleString", to_locale_string, 0, attr);
  56. define_native_function("unshift", unshift, 1, attr);
  57. define_native_function("join", join, 1, attr);
  58. define_native_function("concat", concat, 1, attr);
  59. define_native_function("slice", slice, 2, attr);
  60. define_native_function("indexOf", index_of, 1, attr);
  61. define_native_function("reduce", reduce, 1, attr);
  62. define_native_function("reduceRight", reduce_right, 1, attr);
  63. define_native_function("reverse", reverse, 0, attr);
  64. define_native_function("lastIndexOf", last_index_of, 1, attr);
  65. define_native_function("includes", includes, 1, attr);
  66. define_native_function("find", find, 1, attr);
  67. define_native_function("findIndex", find_index, 1, attr);
  68. define_native_function("some", some, 1, attr);
  69. define_native_function("every", every, 1, attr);
  70. define_native_function("splice", splice, 2, attr);
  71. define_native_function("fill", fill, 1, attr);
  72. define_native_function("values", values, 0, attr);
  73. define_property("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(global_object.interpreter().well_known_symbol_iterator(), get("values"), attr);
  78. }
  79. ArrayPrototype::~ArrayPrototype()
  80. {
  81. }
  82. static Function* callback_from_args(Interpreter& interpreter, const String& name)
  83. {
  84. if (interpreter.argument_count() < 1) {
  85. interpreter.throw_exception<TypeError>(ErrorType::ArrayPrototypeOneArg, name.characters());
  86. return nullptr;
  87. }
  88. auto callback = interpreter.argument(0);
  89. if (!callback.is_function()) {
  90. interpreter.throw_exception<TypeError>(ErrorType::NotAFunction, callback.to_string_without_side_effects().characters());
  91. return nullptr;
  92. }
  93. return &callback.as_function();
  94. }
  95. static size_t get_length(Interpreter& interpreter, Object& object)
  96. {
  97. auto length_property = object.get("length");
  98. if (interpreter.exception())
  99. return 0;
  100. return length_property.to_size_t(interpreter);
  101. }
  102. 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)
  103. {
  104. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  105. if (!this_object)
  106. return;
  107. auto initial_length = get_length(interpreter, *this_object);
  108. if (interpreter.exception())
  109. return;
  110. auto* callback_function = callback_from_args(interpreter, name);
  111. if (!callback_function)
  112. return;
  113. auto this_value = interpreter.argument(1);
  114. for (size_t i = 0; i < initial_length; ++i) {
  115. auto value = this_object->get(i);
  116. if (interpreter.exception())
  117. return;
  118. if (value.is_empty()) {
  119. if (skip_empty)
  120. continue;
  121. value = js_undefined();
  122. }
  123. auto callback_result = interpreter.call(*callback_function, this_value, value, Value((i32)i), this_object);
  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. if (interpreter.exception())
  159. return IterationDecision::Break;
  160. new_array->define_property(index, callback_result);
  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. interpreter.throw_exception<TypeError>(ErrorType::ArrayMaxSize);
  183. return {};
  184. }
  185. for (size_t i = 0; i < argument_count; ++i) {
  186. this_object->put(length + i, interpreter.argument(i));
  187. if (interpreter.exception())
  188. return {};
  189. }
  190. auto new_length_value = Value((i32)new_length);
  191. this_object->put("length", new_length_value);
  192. if (interpreter.exception())
  193. return {};
  194. return new_length_value;
  195. }
  196. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::unshift)
  197. {
  198. auto* array = Array::typed_this(interpreter, global_object);
  199. if (!array)
  200. return {};
  201. for (size_t i = 0; i < interpreter.argument_count(); ++i)
  202. array->indexed_properties().insert(i, interpreter.argument(i));
  203. return Value(static_cast<i32>(array->indexed_properties().array_like_size()));
  204. }
  205. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::pop)
  206. {
  207. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  208. if (!this_object)
  209. return {};
  210. if (this_object->is_array()) {
  211. auto* array = static_cast<Array*>(this_object);
  212. if (array->indexed_properties().is_empty())
  213. return js_undefined();
  214. return array->indexed_properties().take_last(array).value.value_or(js_undefined());
  215. }
  216. auto length = get_length(interpreter, *this_object);
  217. if (length == 0) {
  218. this_object->put("length", Value(0));
  219. return js_undefined();
  220. }
  221. auto index = length - 1;
  222. auto element = this_object->get(index).value_or(js_undefined());
  223. if (interpreter.exception())
  224. return {};
  225. this_object->delete_property(index);
  226. if (interpreter.exception())
  227. return {};
  228. this_object->put("length", Value((i32)index));
  229. if (interpreter.exception())
  230. return {};
  231. return element;
  232. }
  233. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::shift)
  234. {
  235. auto* array = Array::typed_this(interpreter, global_object);
  236. if (!array)
  237. return {};
  238. if (array->indexed_properties().is_empty())
  239. return js_undefined();
  240. auto result = array->indexed_properties().take_first(array);
  241. if (interpreter.exception())
  242. return {};
  243. return result.value.value_or(js_undefined());
  244. }
  245. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::to_string)
  246. {
  247. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  248. if (!this_object)
  249. return {};
  250. auto join_function = this_object->get("join");
  251. if (interpreter.exception())
  252. return {};
  253. if (!join_function.is_function())
  254. return ObjectPrototype::to_string(interpreter, global_object);
  255. return interpreter.call(join_function.as_function(), this_object);
  256. }
  257. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::to_locale_string)
  258. {
  259. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  260. if (!this_object)
  261. return {};
  262. String separator = ","; // NOTE: This is implementation-specific.
  263. auto length = get_length(interpreter, *this_object);
  264. if (interpreter.exception())
  265. return {};
  266. StringBuilder builder;
  267. for (size_t i = 0; i < length; ++i) {
  268. if (i > 0)
  269. builder.append(separator);
  270. auto value = this_object->get(i).value_or(js_undefined());
  271. if (interpreter.exception())
  272. return {};
  273. if (value.is_undefined() || value.is_null())
  274. continue;
  275. auto* value_object = value.to_object(interpreter, global_object);
  276. ASSERT(value_object);
  277. auto locale_string_result = value_object->invoke("toLocaleString");
  278. if (interpreter.exception())
  279. return {};
  280. auto string = locale_string_result.to_string(interpreter);
  281. if (interpreter.exception())
  282. return {};
  283. builder.append(string);
  284. }
  285. return js_string(interpreter, builder.to_string());
  286. }
  287. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::join)
  288. {
  289. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  290. if (!this_object)
  291. return {};
  292. String separator = ",";
  293. if (interpreter.argument_count()) {
  294. separator = interpreter.argument(0).to_string(interpreter);
  295. if (interpreter.exception())
  296. return {};
  297. }
  298. auto length = get_length(interpreter, *this_object);
  299. if (interpreter.exception())
  300. return {};
  301. StringBuilder builder;
  302. for (size_t i = 0; i < length; ++i) {
  303. if (i > 0)
  304. builder.append(separator);
  305. auto value = this_object->get(i).value_or(js_undefined());
  306. if (interpreter.exception())
  307. return {};
  308. if (value.is_undefined() || value.is_null())
  309. continue;
  310. auto string = value.to_string(interpreter);
  311. if (interpreter.exception())
  312. return {};
  313. builder.append(string);
  314. }
  315. return js_string(interpreter, builder.to_string());
  316. }
  317. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::concat)
  318. {
  319. auto* array = Array::typed_this(interpreter, global_object);
  320. if (!array)
  321. return {};
  322. auto* new_array = Array::create(global_object);
  323. new_array->indexed_properties().append_all(array, array->indexed_properties());
  324. if (interpreter.exception())
  325. return {};
  326. for (size_t i = 0; i < interpreter.argument_count(); ++i) {
  327. auto argument = interpreter.argument(i);
  328. if (argument.is_array()) {
  329. auto& argument_object = argument.as_object();
  330. new_array->indexed_properties().append_all(&argument_object, argument_object.indexed_properties());
  331. if (interpreter.exception())
  332. return {};
  333. } else {
  334. new_array->indexed_properties().append(argument);
  335. }
  336. }
  337. return Value(new_array);
  338. }
  339. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::slice)
  340. {
  341. auto* array = Array::typed_this(interpreter, global_object);
  342. if (!array)
  343. return {};
  344. auto* new_array = Array::create(global_object);
  345. if (interpreter.argument_count() == 0) {
  346. new_array->indexed_properties().append_all(array, array->indexed_properties());
  347. if (interpreter.exception())
  348. return {};
  349. return new_array;
  350. }
  351. ssize_t array_size = static_cast<ssize_t>(array->indexed_properties().array_like_size());
  352. auto start_slice = interpreter.argument(0).to_i32(interpreter);
  353. if (interpreter.exception())
  354. return {};
  355. auto end_slice = array_size;
  356. if (start_slice > array_size)
  357. return new_array;
  358. if (start_slice < 0)
  359. start_slice = end_slice + start_slice;
  360. if (interpreter.argument_count() >= 2) {
  361. end_slice = interpreter.argument(1).to_i32(interpreter);
  362. if (interpreter.exception())
  363. return {};
  364. if (end_slice < 0)
  365. end_slice = array_size + end_slice;
  366. else if (end_slice > array_size)
  367. end_slice = array_size;
  368. }
  369. for (ssize_t i = start_slice; i < end_slice; ++i) {
  370. new_array->indexed_properties().append(array->get(i));
  371. if (interpreter.exception())
  372. return {};
  373. }
  374. return new_array;
  375. }
  376. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::index_of)
  377. {
  378. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  379. if (!this_object)
  380. return {};
  381. i32 length = get_length(interpreter, *this_object);
  382. if (interpreter.exception())
  383. return {};
  384. if (length == 0)
  385. return Value(-1);
  386. i32 from_index = 0;
  387. if (interpreter.argument_count() >= 2) {
  388. from_index = interpreter.argument(1).to_i32(interpreter);
  389. if (interpreter.exception())
  390. return {};
  391. if (from_index >= length)
  392. return Value(-1);
  393. if (from_index < 0)
  394. from_index = max(length + from_index, 0);
  395. }
  396. auto search_element = interpreter.argument(0);
  397. for (i32 i = from_index; i < length; ++i) {
  398. auto element = this_object->get(i);
  399. if (interpreter.exception())
  400. return {};
  401. if (strict_eq(interpreter, element, search_element))
  402. return Value(i);
  403. }
  404. return Value(-1);
  405. }
  406. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::reduce)
  407. {
  408. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  409. if (!this_object)
  410. return {};
  411. auto initial_length = get_length(interpreter, *this_object);
  412. if (interpreter.exception())
  413. return {};
  414. auto* callback_function = callback_from_args(interpreter, "reduce");
  415. if (!callback_function)
  416. return {};
  417. size_t start = 0;
  418. auto accumulator = js_undefined();
  419. if (interpreter.argument_count() > 1) {
  420. accumulator = interpreter.argument(1);
  421. } else {
  422. bool start_found = false;
  423. while (!start_found && start < initial_length) {
  424. auto value = this_object->get(start);
  425. if (interpreter.exception())
  426. return {};
  427. start_found = !value.is_empty();
  428. if (start_found)
  429. accumulator = value;
  430. start += 1;
  431. }
  432. if (!start_found) {
  433. interpreter.throw_exception<TypeError>(ErrorType::ReduceNoInitial);
  434. return {};
  435. }
  436. }
  437. auto this_value = js_undefined();
  438. for (size_t i = start; i < initial_length; ++i) {
  439. auto value = this_object->get(i);
  440. if (interpreter.exception())
  441. return {};
  442. if (value.is_empty())
  443. continue;
  444. accumulator = interpreter.call(*callback_function, this_value, accumulator, value, Value((i32)i), this_object);
  445. if (interpreter.exception())
  446. return {};
  447. }
  448. return accumulator;
  449. }
  450. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::reduce_right)
  451. {
  452. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  453. if (!this_object)
  454. return {};
  455. auto initial_length = get_length(interpreter, *this_object);
  456. if (interpreter.exception())
  457. return {};
  458. auto* callback_function = callback_from_args(interpreter, "reduceRight");
  459. if (!callback_function)
  460. return {};
  461. int start = initial_length - 1;
  462. auto accumulator = js_undefined();
  463. if (interpreter.argument_count() > 1) {
  464. accumulator = interpreter.argument(1);
  465. } else {
  466. bool start_found = false;
  467. while (!start_found && start >= 0) {
  468. auto value = this_object->get(start);
  469. if (interpreter.exception())
  470. return {};
  471. start_found = !value.is_empty();
  472. if (start_found)
  473. accumulator = value;
  474. start -= 1;
  475. }
  476. if (!start_found) {
  477. interpreter.throw_exception<TypeError>(ErrorType::ReduceNoInitial);
  478. return {};
  479. }
  480. }
  481. auto this_value = js_undefined();
  482. for (int i = start; i >= 0; --i) {
  483. auto value = this_object->get(i);
  484. if (interpreter.exception())
  485. return {};
  486. if (value.is_empty())
  487. continue;
  488. accumulator = interpreter.call(*callback_function, this_value, accumulator, value, Value((i32)i), this_object);
  489. if (interpreter.exception())
  490. return {};
  491. }
  492. return accumulator;
  493. }
  494. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::reverse)
  495. {
  496. auto* array = Array::typed_this(interpreter, global_object);
  497. if (!array)
  498. return {};
  499. if (array->indexed_properties().is_empty())
  500. return array;
  501. MarkedValueList array_reverse(interpreter.heap());
  502. auto size = array->indexed_properties().array_like_size();
  503. array_reverse.ensure_capacity(size);
  504. for (ssize_t i = size - 1; i >= 0; --i) {
  505. array_reverse.append(array->get(i));
  506. if (interpreter.exception())
  507. return {};
  508. }
  509. array->set_indexed_property_elements(move(array_reverse));
  510. return array;
  511. }
  512. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::last_index_of)
  513. {
  514. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  515. if (!this_object)
  516. return {};
  517. i32 length = get_length(interpreter, *this_object);
  518. if (interpreter.exception())
  519. return {};
  520. if (length == 0)
  521. return Value(-1);
  522. i32 from_index = length - 1;
  523. if (interpreter.argument_count() >= 2) {
  524. from_index = interpreter.argument(1).to_i32(interpreter);
  525. if (interpreter.exception())
  526. return {};
  527. if (from_index >= 0)
  528. from_index = min(from_index, length - 1);
  529. else
  530. from_index = length + from_index;
  531. }
  532. auto search_element = interpreter.argument(0);
  533. for (i32 i = from_index; i >= 0; --i) {
  534. auto element = this_object->get(i);
  535. if (interpreter.exception())
  536. return {};
  537. if (strict_eq(interpreter, element, search_element))
  538. return Value(i);
  539. }
  540. return Value(-1);
  541. }
  542. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::includes)
  543. {
  544. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  545. if (!this_object)
  546. return {};
  547. i32 length = get_length(interpreter, *this_object);
  548. if (interpreter.exception())
  549. return {};
  550. if (length == 0)
  551. return Value(false);
  552. i32 from_index = 0;
  553. if (interpreter.argument_count() >= 2) {
  554. from_index = interpreter.argument(1).to_i32(interpreter);
  555. if (interpreter.exception())
  556. return {};
  557. if (from_index >= length)
  558. return Value(false);
  559. if (from_index < 0)
  560. from_index = max(length + from_index, 0);
  561. }
  562. auto value_to_find = interpreter.argument(0);
  563. for (i32 i = from_index; i < length; ++i) {
  564. auto element = this_object->get(i).value_or(js_undefined());
  565. if (interpreter.exception())
  566. return {};
  567. if (same_value_zero(interpreter, element, value_to_find))
  568. return Value(true);
  569. }
  570. return Value(false);
  571. }
  572. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::find)
  573. {
  574. auto result = js_undefined();
  575. for_each_item(
  576. interpreter, global_object, "find", [&](auto, auto value, auto callback_result) {
  577. if (callback_result.to_boolean()) {
  578. result = value;
  579. return IterationDecision::Break;
  580. }
  581. return IterationDecision::Continue;
  582. },
  583. false);
  584. return result;
  585. }
  586. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::find_index)
  587. {
  588. auto result_index = -1;
  589. for_each_item(
  590. interpreter, global_object, "findIndex", [&](auto index, auto, auto callback_result) {
  591. if (callback_result.to_boolean()) {
  592. result_index = index;
  593. return IterationDecision::Break;
  594. }
  595. return IterationDecision::Continue;
  596. },
  597. false);
  598. return Value(result_index);
  599. }
  600. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::some)
  601. {
  602. auto result = false;
  603. for_each_item(interpreter, global_object, "some", [&](auto, auto, auto callback_result) {
  604. if (callback_result.to_boolean()) {
  605. result = true;
  606. return IterationDecision::Break;
  607. }
  608. return IterationDecision::Continue;
  609. });
  610. return Value(result);
  611. }
  612. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::every)
  613. {
  614. auto result = true;
  615. for_each_item(interpreter, global_object, "every", [&](auto, auto, auto callback_result) {
  616. if (!callback_result.to_boolean()) {
  617. result = false;
  618. return IterationDecision::Break;
  619. }
  620. return IterationDecision::Continue;
  621. });
  622. return Value(result);
  623. }
  624. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::splice)
  625. {
  626. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  627. if (!this_object)
  628. return {};
  629. auto initial_length = get_length(interpreter, *this_object);
  630. if (interpreter.exception())
  631. return {};
  632. auto relative_start = interpreter.argument(0).to_i32(interpreter);
  633. if (interpreter.exception())
  634. return {};
  635. size_t actual_start;
  636. if (relative_start < 0)
  637. actual_start = max((ssize_t)initial_length + relative_start, (ssize_t)0);
  638. else
  639. actual_start = min((size_t)relative_start, initial_length);
  640. size_t insert_count = 0;
  641. size_t actual_delete_count = 0;
  642. if (interpreter.argument_count() == 1) {
  643. actual_delete_count = initial_length - actual_start;
  644. } else if (interpreter.argument_count() >= 2) {
  645. insert_count = interpreter.argument_count() - 2;
  646. i32 delete_count = interpreter.argument(1).to_i32(interpreter);
  647. if (interpreter.exception())
  648. return {};
  649. actual_delete_count = min((size_t)max(delete_count, 0), initial_length - actual_start);
  650. }
  651. size_t new_length = initial_length + insert_count - actual_delete_count;
  652. if (new_length > MAX_ARRAY_LIKE_INDEX) {
  653. interpreter.throw_exception<TypeError>(ErrorType::ArrayMaxSize);
  654. return {};
  655. }
  656. auto removed_elements = Array::create(global_object);
  657. for (size_t i = 0; i < actual_delete_count; ++i) {
  658. auto value = this_object->get(actual_start + i);
  659. if (interpreter.exception())
  660. return {};
  661. removed_elements->indexed_properties().append(value);
  662. }
  663. if (insert_count < actual_delete_count) {
  664. for (size_t i = actual_start; i < initial_length - actual_delete_count; ++i) {
  665. auto from = this_object->get(i + actual_delete_count);
  666. if (interpreter.exception())
  667. return {};
  668. auto to = i + insert_count;
  669. if (!from.is_empty()) {
  670. this_object->put(to, from);
  671. } else {
  672. this_object->delete_property(to);
  673. }
  674. if (interpreter.exception())
  675. return {};
  676. }
  677. for (size_t i = initial_length; i > new_length; --i) {
  678. this_object->delete_property(i - 1);
  679. if (interpreter.exception())
  680. return {};
  681. }
  682. } else if (insert_count > actual_delete_count) {
  683. for (size_t i = initial_length - actual_delete_count; i > actual_start; --i) {
  684. auto from = this_object->get(i + actual_delete_count - 1);
  685. if (interpreter.exception())
  686. return {};
  687. auto to = i + insert_count - 1;
  688. if (!from.is_empty()) {
  689. this_object->put(to, from);
  690. } else {
  691. this_object->delete_property(to);
  692. }
  693. if (interpreter.exception())
  694. return {};
  695. }
  696. }
  697. for (size_t i = 0; i < insert_count; ++i) {
  698. this_object->put(actual_start + i, interpreter.argument(i + 2));
  699. if (interpreter.exception())
  700. return {};
  701. }
  702. this_object->put("length", Value((i32)new_length));
  703. if (interpreter.exception())
  704. return {};
  705. return removed_elements;
  706. }
  707. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::fill)
  708. {
  709. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  710. if (!this_object)
  711. return {};
  712. ssize_t length = get_length(interpreter, *this_object);
  713. if (interpreter.exception())
  714. return {};
  715. ssize_t relative_start = 0;
  716. ssize_t relative_end = length;
  717. if (interpreter.argument_count() >= 2) {
  718. relative_start = interpreter.argument(1).to_i32(interpreter);
  719. if (interpreter.exception())
  720. return {};
  721. }
  722. if (interpreter.argument_count() >= 3) {
  723. relative_end = interpreter.argument(2).to_i32(interpreter);
  724. if (interpreter.exception())
  725. return {};
  726. }
  727. size_t from, to;
  728. if (relative_start < 0)
  729. from = max(length + relative_start, 0L);
  730. else
  731. from = min(relative_start, length);
  732. if (relative_end < 0)
  733. to = max(length + relative_end, 0L);
  734. else
  735. to = min(relative_end, length);
  736. for (size_t i = from; i < to; i++) {
  737. this_object->put(i, interpreter.argument(0));
  738. if (interpreter.exception())
  739. return {};
  740. }
  741. return this_object;
  742. }
  743. JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::values)
  744. {
  745. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  746. if (!this_object)
  747. return {};
  748. return ArrayIterator::create(global_object, this_object, Object::PropertyKind::Value);
  749. }
  750. }