ArrayPrototype.cpp 24 KB

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