ArrayPrototype.cpp 25 KB

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