ArrayPrototype.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/Function.h>
  28. #include <AK/StringBuilder.h>
  29. #include <LibJS/Heap/Heap.h>
  30. #include <LibJS/Interpreter.h>
  31. #include <LibJS/Runtime/Array.h>
  32. #include <LibJS/Runtime/ArrayPrototype.h>
  33. #include <LibJS/Runtime/Error.h>
  34. #include <LibJS/Runtime/Function.h>
  35. #include <LibJS/Runtime/GlobalObject.h>
  36. #include <LibJS/Runtime/MarkedValueList.h>
  37. #include <LibJS/Runtime/Value.h>
  38. namespace JS {
  39. ArrayPrototype::ArrayPrototype()
  40. : Object(interpreter().global_object().object_prototype())
  41. {
  42. u8 attr = Attribute::Writable | Attribute::Configurable;
  43. put_native_function("filter", filter, 1, attr);
  44. put_native_function("forEach", for_each, 1, attr);
  45. put_native_function("map", map, 1, attr);
  46. put_native_function("pop", pop, 0, attr);
  47. put_native_function("push", push, 1, attr);
  48. put_native_function("shift", shift, 0, attr);
  49. put_native_function("toString", to_string, 0, attr);
  50. put_native_function("unshift", unshift, 1, attr);
  51. put_native_function("join", join, 1, attr);
  52. put_native_function("concat", concat, 1, attr);
  53. put_native_function("slice", slice, 2, attr);
  54. put_native_function("indexOf", index_of, 1, attr);
  55. put_native_function("reverse", reverse, 0, attr);
  56. put_native_function("lastIndexOf", last_index_of, 1, attr);
  57. put_native_function("includes", includes, 1, attr);
  58. put_native_function("find", find, 1, attr);
  59. put_native_function("findIndex", find_index, 1, attr);
  60. put_native_function("some", some, 1, attr);
  61. put_native_function("every", every, 1, attr);
  62. put("length", Value(0), Attribute::Configurable);
  63. }
  64. ArrayPrototype::~ArrayPrototype()
  65. {
  66. }
  67. static Function* callback_from_args(Interpreter& interpreter, const String& name)
  68. {
  69. if (interpreter.argument_count() < 1) {
  70. interpreter.throw_exception<TypeError>(String::format("Array.prototype.%s() requires at least one argument", name.characters()));
  71. return nullptr;
  72. }
  73. auto callback = interpreter.argument(0);
  74. if (!callback.is_function()) {
  75. interpreter.throw_exception<TypeError>(String::format("%s is not a function", callback.to_string_without_side_effects().characters()));
  76. return nullptr;
  77. }
  78. return &callback.as_function();
  79. }
  80. Value ArrayPrototype::filter(Interpreter& interpreter)
  81. {
  82. auto* array = array_from(interpreter);
  83. if (!array)
  84. return {};
  85. auto* callback = callback_from_args(interpreter, "filter");
  86. if (!callback)
  87. return {};
  88. auto this_value = interpreter.argument(1);
  89. auto initial_array_size = array->elements().size();
  90. auto* new_array = Array::create(interpreter.global_object());
  91. for (size_t i = 0; i < initial_array_size; ++i) {
  92. if (i >= array->elements().size())
  93. break;
  94. auto value = array->elements()[i];
  95. if (value.is_empty())
  96. continue;
  97. MarkedValueList arguments(interpreter.heap());
  98. arguments.append(value);
  99. arguments.append(Value((i32)i));
  100. arguments.append(array);
  101. auto result = interpreter.call(*callback, this_value, move(arguments));
  102. if (interpreter.exception())
  103. return {};
  104. if (result.to_boolean())
  105. new_array->elements().append(value);
  106. }
  107. return Value(new_array);
  108. }
  109. Value ArrayPrototype::for_each(Interpreter& interpreter)
  110. {
  111. auto* array = array_from(interpreter);
  112. if (!array)
  113. return {};
  114. auto* callback = callback_from_args(interpreter, "forEach");
  115. if (!callback)
  116. return {};
  117. auto this_value = interpreter.argument(1);
  118. auto initial_array_size = array->elements().size();
  119. for (size_t i = 0; i < initial_array_size; ++i) {
  120. if (i >= array->elements().size())
  121. break;
  122. auto value = array->elements()[i];
  123. if (value.is_empty())
  124. continue;
  125. MarkedValueList arguments(interpreter.heap());
  126. arguments.append(value);
  127. arguments.append(Value((i32)i));
  128. arguments.append(array);
  129. interpreter.call(*callback, this_value, move(arguments));
  130. if (interpreter.exception())
  131. return {};
  132. }
  133. return js_undefined();
  134. }
  135. Value ArrayPrototype::map(Interpreter& interpreter)
  136. {
  137. // FIXME: Make generic, i.e. work with length and numeric properties only
  138. // This should work: Array.prototype.map.call("abc", ch => ...)
  139. auto* array = array_from(interpreter);
  140. if (!array)
  141. return {};
  142. auto* callback = callback_from_args(interpreter, "map");
  143. if (!callback)
  144. return {};
  145. auto this_value = interpreter.argument(1);
  146. auto initial_array_size = array->elements().size();
  147. auto* new_array = Array::create(interpreter.global_object());
  148. new_array->elements().resize(initial_array_size);
  149. for (size_t i = 0; i < initial_array_size; ++i) {
  150. if (i >= array->elements().size())
  151. break;
  152. auto value = array->elements()[i];
  153. if (value.is_empty())
  154. continue;
  155. MarkedValueList arguments(interpreter.heap());
  156. arguments.append(value);
  157. arguments.append(Value((i32)i));
  158. arguments.append(array);
  159. auto result = interpreter.call(*callback, this_value, move(arguments));
  160. if (interpreter.exception())
  161. return {};
  162. new_array->elements()[i] = result;
  163. }
  164. return Value(new_array);
  165. }
  166. Value ArrayPrototype::push(Interpreter& interpreter)
  167. {
  168. auto* array = array_from(interpreter);
  169. if (!array)
  170. return {};
  171. for (size_t i = 0; i < interpreter.argument_count(); ++i)
  172. array->elements().append(interpreter.argument(i));
  173. return Value(array->length());
  174. }
  175. Value ArrayPrototype::unshift(Interpreter& interpreter)
  176. {
  177. auto* array = array_from(interpreter);
  178. if (!array)
  179. return {};
  180. for (size_t i = 0; i < interpreter.argument_count(); ++i)
  181. array->elements().insert(i, interpreter.argument(i));
  182. return Value(array->length());
  183. }
  184. Value ArrayPrototype::pop(Interpreter& interpreter)
  185. {
  186. auto* array = array_from(interpreter);
  187. if (!array)
  188. return {};
  189. if (array->elements().is_empty())
  190. return js_undefined();
  191. return array->elements().take_last().value_or(js_undefined());
  192. }
  193. Value ArrayPrototype::shift(Interpreter& interpreter)
  194. {
  195. auto* array = array_from(interpreter);
  196. if (!array)
  197. return {};
  198. if (array->elements().is_empty())
  199. return js_undefined();
  200. return array->elements().take_first().value_or(js_undefined());
  201. }
  202. static Value join_array_with_separator(Interpreter& interpreter, const Array& array, StringView separator)
  203. {
  204. StringBuilder builder;
  205. for (size_t i = 0; i < array.elements().size(); ++i) {
  206. if (i != 0)
  207. builder.append(separator);
  208. auto value = array.elements()[i];
  209. if (!value.is_empty() && !value.is_undefined() && !value.is_null()) {
  210. auto string = value.to_string(interpreter);
  211. if (interpreter.exception())
  212. return {};
  213. builder.append(string);
  214. }
  215. }
  216. return js_string(interpreter, builder.to_string());
  217. }
  218. Value ArrayPrototype::to_string(Interpreter& interpreter)
  219. {
  220. auto* array = array_from(interpreter);
  221. if (!array)
  222. return {};
  223. return join_array_with_separator(interpreter, *array, ",");
  224. }
  225. Value ArrayPrototype::join(Interpreter& interpreter)
  226. {
  227. auto* array = array_from(interpreter);
  228. if (!array)
  229. return {};
  230. String separator = ",";
  231. if (interpreter.argument_count() == 1) {
  232. separator = interpreter.argument(0).to_string(interpreter);
  233. if (interpreter.exception())
  234. return {};
  235. }
  236. return join_array_with_separator(interpreter, *array, separator);
  237. }
  238. Value ArrayPrototype::concat(Interpreter& interpreter)
  239. {
  240. auto* array = array_from(interpreter);
  241. if (!array)
  242. return {};
  243. auto* new_array = Array::create(interpreter.global_object());
  244. new_array->elements().append(array->elements());
  245. for (size_t i = 0; i < interpreter.argument_count(); ++i) {
  246. auto argument = interpreter.argument(i);
  247. if (argument.is_array()) {
  248. auto& argument_object = argument.as_object();
  249. new_array->elements().append(argument_object.elements());
  250. } else {
  251. new_array->elements().append(argument);
  252. }
  253. }
  254. return Value(new_array);
  255. }
  256. Value ArrayPrototype::slice(Interpreter& interpreter)
  257. {
  258. auto* array = array_from(interpreter);
  259. if (!array)
  260. return {};
  261. auto* new_array = Array::create(interpreter.global_object());
  262. if (interpreter.argument_count() == 0) {
  263. new_array->elements().append(array->elements());
  264. return new_array;
  265. }
  266. ssize_t array_size = static_cast<ssize_t>(array->elements().size());
  267. auto start_slice = interpreter.argument(0).to_i32(interpreter);
  268. if (interpreter.exception())
  269. return {};
  270. auto end_slice = array_size;
  271. if (start_slice > array_size)
  272. return new_array;
  273. if (start_slice < 0)
  274. start_slice = end_slice + start_slice;
  275. if (interpreter.argument_count() >= 2) {
  276. end_slice = interpreter.argument(1).to_i32(interpreter);
  277. if (interpreter.exception())
  278. return {};
  279. if (end_slice < 0)
  280. end_slice = array_size + end_slice;
  281. else if (end_slice > array_size)
  282. end_slice = array_size;
  283. }
  284. size_t array_capacity = start_slice + array_size - end_slice;
  285. new_array->elements().ensure_capacity(array_capacity);
  286. for (ssize_t i = start_slice; i < end_slice; ++i) {
  287. new_array->elements().append(array->elements().at(i));
  288. }
  289. return new_array;
  290. }
  291. Value ArrayPrototype::index_of(Interpreter& interpreter)
  292. {
  293. auto* array = array_from(interpreter);
  294. if (!array)
  295. return {};
  296. i32 array_size = static_cast<i32>(array->elements().size());
  297. if (interpreter.argument_count() == 0 || array_size == 0)
  298. return Value(-1);
  299. i32 from_index = 0;
  300. if (interpreter.argument_count() >= 2) {
  301. from_index = interpreter.argument(1).to_i32(interpreter);
  302. if (interpreter.exception())
  303. return {};
  304. if (from_index >= array_size)
  305. return Value(-1);
  306. auto negative_min_index = ((array_size - 1) * -1);
  307. if (from_index < negative_min_index)
  308. from_index = 0;
  309. else if (from_index < 0)
  310. from_index = array_size + from_index;
  311. }
  312. auto search_element = interpreter.argument(0);
  313. for (i32 i = from_index; i < array_size; ++i) {
  314. auto& element = array->elements().at(i);
  315. if (strict_eq(interpreter, element, search_element))
  316. return Value(i);
  317. }
  318. return Value(-1);
  319. }
  320. Value ArrayPrototype::reverse(Interpreter& interpreter)
  321. {
  322. auto* array = array_from(interpreter);
  323. if (!array)
  324. return {};
  325. if (array->elements().size() == 0)
  326. return array;
  327. Vector<Value> array_reverse;
  328. array_reverse.ensure_capacity(array->elements().size());
  329. for (ssize_t i = array->elements().size() - 1; i >= 0; --i)
  330. array_reverse.append(array->elements().at(i));
  331. array->elements() = move(array_reverse);
  332. return array;
  333. }
  334. Value ArrayPrototype::last_index_of(Interpreter& interpreter)
  335. {
  336. auto* array = array_from(interpreter);
  337. if (!array)
  338. return {};
  339. i32 array_size = static_cast<i32>(array->elements().size());
  340. if (interpreter.argument_count() == 0 || array_size == 0)
  341. return Value(-1);
  342. i32 from_index = 0;
  343. if (interpreter.argument_count() >= 2) {
  344. from_index = interpreter.argument(1).to_i32(interpreter);
  345. if (interpreter.exception())
  346. return {};
  347. if (from_index >= array_size)
  348. return Value(-1);
  349. auto negative_min_index = ((array_size - 1) * -1);
  350. if (from_index < negative_min_index)
  351. from_index = 0;
  352. else if (from_index < 0)
  353. from_index = array_size + from_index;
  354. }
  355. auto search_element = interpreter.argument(0);
  356. for (i32 i = array_size - 1; i >= from_index; --i) {
  357. auto& element = array->elements().at(i);
  358. if (strict_eq(interpreter, element, search_element))
  359. return Value(i);
  360. }
  361. return Value(-1);
  362. }
  363. Value ArrayPrototype::includes(Interpreter& interpreter)
  364. {
  365. auto* array = array_from(interpreter);
  366. if (!array)
  367. return {};
  368. i32 array_size = array->elements().size();
  369. if (array_size == 0)
  370. return Value(false);
  371. i32 from_index = 0;
  372. if (interpreter.argument_count() >= 2) {
  373. from_index = interpreter.argument(1).to_i32(interpreter);
  374. if (interpreter.exception())
  375. return {};
  376. if (from_index >= array_size)
  377. return Value(false);
  378. auto negative_min_index = ((array_size - 1) * -1);
  379. if (from_index < negative_min_index)
  380. from_index = 0;
  381. else if (from_index < 0)
  382. from_index = array_size + from_index;
  383. }
  384. auto value_to_find = interpreter.argument(0);
  385. for (i32 i = from_index; i < array_size; ++i) {
  386. auto& element = array->elements().at(i);
  387. if (same_value_zero(interpreter, element, value_to_find))
  388. return Value(true);
  389. }
  390. return Value(false);
  391. }
  392. Value ArrayPrototype::find(Interpreter& interpreter)
  393. {
  394. auto* array = array_from(interpreter);
  395. if (!array)
  396. return {};
  397. auto* callback = callback_from_args(interpreter, "find");
  398. if (!callback)
  399. return {};
  400. auto this_value = interpreter.argument(1);
  401. auto array_size = array->elements().size();
  402. for (size_t i = 0; i < array_size; ++i) {
  403. auto value = js_undefined();
  404. if (i < array->elements().size()) {
  405. value = array->elements().at(i);
  406. if (value.is_empty())
  407. value = js_undefined();
  408. }
  409. MarkedValueList arguments(interpreter.heap());
  410. arguments.append(value);
  411. arguments.append(Value((i32)i));
  412. arguments.append(array);
  413. auto result = interpreter.call(*callback, this_value, move(arguments));
  414. if (interpreter.exception())
  415. return {};
  416. if (result.to_boolean())
  417. return value;
  418. }
  419. return js_undefined();
  420. }
  421. Value ArrayPrototype::find_index(Interpreter& interpreter)
  422. {
  423. auto* array = array_from(interpreter);
  424. if (!array)
  425. return {};
  426. auto* callback = callback_from_args(interpreter, "findIndex");
  427. if (!callback)
  428. return {};
  429. auto this_value = interpreter.argument(1);
  430. auto array_size = array->elements().size();
  431. for (size_t i = 0; i < array_size; ++i) {
  432. auto value = js_undefined();
  433. if (i < array->elements().size()) {
  434. value = array->elements().at(i);
  435. if (value.is_empty())
  436. value = js_undefined();
  437. }
  438. MarkedValueList arguments(interpreter.heap());
  439. arguments.append(value);
  440. arguments.append(Value((i32)i));
  441. arguments.append(array);
  442. auto result = interpreter.call(*callback, this_value, move(arguments));
  443. if (interpreter.exception())
  444. return {};
  445. if (result.to_boolean())
  446. return Value((i32)i);
  447. }
  448. return Value(-1);
  449. }
  450. Value ArrayPrototype::some(Interpreter& interpreter)
  451. {
  452. auto* array = array_from(interpreter);
  453. if (!array)
  454. return {};
  455. auto* callback = callback_from_args(interpreter, "some");
  456. if (!callback)
  457. return {};
  458. auto this_value = interpreter.argument(1);
  459. auto array_size = array->elements().size();
  460. for (size_t i = 0; i < array_size; ++i) {
  461. if (i >= array->elements().size())
  462. break;
  463. auto value = array->elements().at(i);
  464. if (value.is_empty())
  465. continue;
  466. MarkedValueList arguments(interpreter.heap());
  467. arguments.append(value);
  468. arguments.append(Value((i32)i));
  469. arguments.append(array);
  470. auto result = interpreter.call(*callback, this_value, move(arguments));
  471. if (interpreter.exception())
  472. return {};
  473. if (result.to_boolean())
  474. return Value(true);
  475. }
  476. return Value(false);
  477. }
  478. Value ArrayPrototype::every(Interpreter& interpreter)
  479. {
  480. auto* array = array_from(interpreter);
  481. if (!array)
  482. return {};
  483. auto* callback = callback_from_args(interpreter, "every");
  484. if (!callback)
  485. return {};
  486. auto this_value = interpreter.argument(1);
  487. auto array_size = array->elements().size();
  488. for (size_t i = 0; i < array_size; ++i) {
  489. if (i >= array->elements().size())
  490. break;
  491. auto value = array->elements().at(i);
  492. if (value.is_empty())
  493. continue;
  494. MarkedValueList arguments(interpreter.heap());
  495. arguments.append(value);
  496. arguments.append(Value((i32)i));
  497. arguments.append(array);
  498. auto result = interpreter.call(*callback, this_value, move(arguments));
  499. if (interpreter.exception())
  500. return {};
  501. if (!result.to_boolean())
  502. return Value(false);
  503. }
  504. return Value(true);
  505. }
  506. }