ArrayPrototype.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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("length", Value(0), Attribute::Configurable);
  62. }
  63. ArrayPrototype::~ArrayPrototype()
  64. {
  65. }
  66. static Function* callback_from_args(Interpreter& interpreter, const String& name)
  67. {
  68. if (interpreter.argument_count() < 1) {
  69. interpreter.throw_exception<TypeError>(String::format("Array.prototype.%s() requires at least one argument", name.characters()));
  70. return nullptr;
  71. }
  72. auto callback = interpreter.argument(0);
  73. if (!callback.is_function()) {
  74. interpreter.throw_exception<TypeError>(String::format("%s is not a function", callback.to_string_without_side_effects().characters()));
  75. return nullptr;
  76. }
  77. return &callback.as_function();
  78. }
  79. Value ArrayPrototype::filter(Interpreter& interpreter)
  80. {
  81. auto* array = array_from(interpreter);
  82. if (!array)
  83. return {};
  84. auto* callback = callback_from_args(interpreter, "filter");
  85. if (!callback)
  86. return {};
  87. auto this_value = interpreter.argument(1);
  88. auto initial_array_size = array->elements().size();
  89. auto* new_array = Array::create(interpreter.global_object());
  90. for (size_t i = 0; i < initial_array_size; ++i) {
  91. if (i >= array->elements().size())
  92. break;
  93. auto value = array->elements()[i];
  94. if (value.is_empty())
  95. continue;
  96. MarkedValueList arguments(interpreter.heap());
  97. arguments.append(value);
  98. arguments.append(Value((i32)i));
  99. arguments.append(array);
  100. auto result = interpreter.call(*callback, this_value, move(arguments));
  101. if (interpreter.exception())
  102. return {};
  103. if (result.to_boolean())
  104. new_array->elements().append(value);
  105. }
  106. return Value(new_array);
  107. }
  108. Value ArrayPrototype::for_each(Interpreter& interpreter)
  109. {
  110. auto* array = array_from(interpreter);
  111. if (!array)
  112. return {};
  113. auto* callback = callback_from_args(interpreter, "forEach");
  114. if (!callback)
  115. return {};
  116. auto this_value = interpreter.argument(1);
  117. auto initial_array_size = array->elements().size();
  118. for (size_t i = 0; i < initial_array_size; ++i) {
  119. if (i >= array->elements().size())
  120. break;
  121. auto value = array->elements()[i];
  122. if (value.is_empty())
  123. continue;
  124. MarkedValueList arguments(interpreter.heap());
  125. arguments.append(value);
  126. arguments.append(Value((i32)i));
  127. arguments.append(array);
  128. interpreter.call(*callback, this_value, move(arguments));
  129. if (interpreter.exception())
  130. return {};
  131. }
  132. return js_undefined();
  133. }
  134. Value ArrayPrototype::map(Interpreter& interpreter)
  135. {
  136. // FIXME: Make generic, i.e. work with length and numeric properties only
  137. // This should work: Array.prototype.map.call("abc", ch => ...)
  138. auto* array = array_from(interpreter);
  139. if (!array)
  140. return {};
  141. auto* callback = callback_from_args(interpreter, "map");
  142. if (!callback)
  143. return {};
  144. auto this_value = interpreter.argument(1);
  145. auto initial_array_size = array->elements().size();
  146. auto* new_array = Array::create(interpreter.global_object());
  147. new_array->elements().resize(initial_array_size);
  148. for (size_t i = 0; i < initial_array_size; ++i) {
  149. if (i >= array->elements().size())
  150. break;
  151. auto value = array->elements()[i];
  152. if (value.is_empty())
  153. continue;
  154. MarkedValueList arguments(interpreter.heap());
  155. arguments.append(value);
  156. arguments.append(Value((i32)i));
  157. arguments.append(array);
  158. auto result = interpreter.call(*callback, this_value, move(arguments));
  159. if (interpreter.exception())
  160. return {};
  161. new_array->elements()[i] = result;
  162. }
  163. return Value(new_array);
  164. }
  165. Value ArrayPrototype::push(Interpreter& interpreter)
  166. {
  167. auto* array = array_from(interpreter);
  168. if (!array)
  169. return {};
  170. for (size_t i = 0; i < interpreter.argument_count(); ++i)
  171. array->elements().append(interpreter.argument(i));
  172. return Value(array->length());
  173. }
  174. Value ArrayPrototype::unshift(Interpreter& interpreter)
  175. {
  176. auto* array = array_from(interpreter);
  177. if (!array)
  178. return {};
  179. for (size_t i = 0; i < interpreter.argument_count(); ++i)
  180. array->elements().insert(i, interpreter.argument(i));
  181. return Value(array->length());
  182. }
  183. Value ArrayPrototype::pop(Interpreter& interpreter)
  184. {
  185. auto* array = array_from(interpreter);
  186. if (!array)
  187. return {};
  188. if (array->elements().is_empty())
  189. return js_undefined();
  190. return array->elements().take_last().value_or(js_undefined());
  191. }
  192. Value ArrayPrototype::shift(Interpreter& interpreter)
  193. {
  194. auto* array = array_from(interpreter);
  195. if (!array)
  196. return {};
  197. if (array->elements().is_empty())
  198. return js_undefined();
  199. return array->elements().take_first().value_or(js_undefined());
  200. }
  201. static Value join_array_with_separator(Interpreter& interpreter, const Array& array, StringView separator)
  202. {
  203. StringBuilder builder;
  204. for (size_t i = 0; i < array.elements().size(); ++i) {
  205. if (i != 0)
  206. builder.append(separator);
  207. auto value = array.elements()[i];
  208. if (!value.is_empty() && !value.is_undefined() && !value.is_null()) {
  209. auto string = value.to_string(interpreter);
  210. if (interpreter.exception())
  211. return {};
  212. builder.append(string);
  213. }
  214. }
  215. return js_string(interpreter, builder.to_string());
  216. }
  217. Value ArrayPrototype::to_string(Interpreter& interpreter)
  218. {
  219. auto* array = array_from(interpreter);
  220. if (!array)
  221. return {};
  222. return join_array_with_separator(interpreter, *array, ",");
  223. }
  224. Value ArrayPrototype::join(Interpreter& interpreter)
  225. {
  226. auto* array = array_from(interpreter);
  227. if (!array)
  228. return {};
  229. String separator = ",";
  230. if (interpreter.argument_count() == 1) {
  231. separator = interpreter.argument(0).to_string(interpreter);
  232. if (interpreter.exception())
  233. return {};
  234. }
  235. return join_array_with_separator(interpreter, *array, separator);
  236. }
  237. Value ArrayPrototype::concat(Interpreter& interpreter)
  238. {
  239. auto* array = array_from(interpreter);
  240. if (!array)
  241. return {};
  242. auto* new_array = Array::create(interpreter.global_object());
  243. new_array->elements().append(array->elements());
  244. for (size_t i = 0; i < interpreter.argument_count(); ++i) {
  245. auto argument = interpreter.argument(i);
  246. if (argument.is_array()) {
  247. auto& argument_object = argument.as_object();
  248. new_array->elements().append(argument_object.elements());
  249. } else {
  250. new_array->elements().append(argument);
  251. }
  252. }
  253. return Value(new_array);
  254. }
  255. Value ArrayPrototype::slice(Interpreter& interpreter)
  256. {
  257. auto* array = array_from(interpreter);
  258. if (!array)
  259. return {};
  260. auto* new_array = Array::create(interpreter.global_object());
  261. if (interpreter.argument_count() == 0) {
  262. new_array->elements().append(array->elements());
  263. return new_array;
  264. }
  265. ssize_t array_size = static_cast<ssize_t>(array->elements().size());
  266. auto start_slice = interpreter.argument(0).to_i32();
  267. auto end_slice = array_size;
  268. if (start_slice > array_size)
  269. return new_array;
  270. if (start_slice < 0)
  271. start_slice = end_slice + start_slice;
  272. if (interpreter.argument_count() >= 2) {
  273. end_slice = interpreter.argument(1).to_i32();
  274. if (end_slice < 0)
  275. end_slice = array_size + end_slice;
  276. else if (end_slice > array_size)
  277. end_slice = array_size;
  278. }
  279. size_t array_capacity = start_slice + array_size - end_slice;
  280. new_array->elements().ensure_capacity(array_capacity);
  281. for (ssize_t i = start_slice; i < end_slice; ++i) {
  282. new_array->elements().append(array->elements().at(i));
  283. }
  284. return new_array;
  285. }
  286. Value ArrayPrototype::index_of(Interpreter& interpreter)
  287. {
  288. auto* array = array_from(interpreter);
  289. if (!array)
  290. return {};
  291. i32 array_size = static_cast<i32>(array->elements().size());
  292. if (interpreter.argument_count() == 0 || array_size == 0)
  293. return Value(-1);
  294. i32 from_index = 0;
  295. if (interpreter.argument_count() >= 2) {
  296. from_index = interpreter.argument(1).to_number().to_i32();
  297. if (from_index >= array_size)
  298. return Value(-1);
  299. auto negative_min_index = ((array_size - 1) * -1);
  300. if (from_index < negative_min_index)
  301. from_index = 0;
  302. else if (from_index < 0)
  303. from_index = array_size + from_index;
  304. }
  305. auto search_element = interpreter.argument(0);
  306. for (i32 i = from_index; i < array_size; ++i) {
  307. auto& element = array->elements().at(i);
  308. if (strict_eq(interpreter, element, search_element))
  309. return Value(i);
  310. }
  311. return Value(-1);
  312. }
  313. Value ArrayPrototype::reverse(Interpreter& interpreter)
  314. {
  315. auto* array = array_from(interpreter);
  316. if (!array)
  317. return {};
  318. if (array->elements().size() == 0)
  319. return array;
  320. Vector<Value> array_reverse;
  321. array_reverse.ensure_capacity(array->elements().size());
  322. for (ssize_t i = array->elements().size() - 1; i >= 0; --i)
  323. array_reverse.append(array->elements().at(i));
  324. array->elements() = move(array_reverse);
  325. return array;
  326. }
  327. Value ArrayPrototype::last_index_of(Interpreter& interpreter)
  328. {
  329. auto* array = array_from(interpreter);
  330. if (!array)
  331. return {};
  332. i32 array_size = static_cast<i32>(array->elements().size());
  333. if (interpreter.argument_count() == 0 || array_size == 0)
  334. return Value(-1);
  335. i32 from_index = 0;
  336. if (interpreter.argument_count() >= 2) {
  337. from_index = interpreter.argument(1).to_number().to_i32();
  338. if (from_index >= array_size)
  339. return Value(-1);
  340. auto negative_min_index = ((array_size - 1) * -1);
  341. if (from_index < negative_min_index)
  342. from_index = 0;
  343. else if (from_index < 0)
  344. from_index = array_size + from_index;
  345. }
  346. auto search_element = interpreter.argument(0);
  347. for (i32 i = array_size - 1; i >= from_index; --i) {
  348. auto& element = array->elements().at(i);
  349. if (strict_eq(interpreter, element, search_element))
  350. return Value(i);
  351. }
  352. return Value(-1);
  353. }
  354. Value ArrayPrototype::includes(Interpreter& interpreter)
  355. {
  356. auto* array = array_from(interpreter);
  357. if (!array)
  358. return {};
  359. i32 array_size = static_cast<i32>(array->elements().size());
  360. if (interpreter.argument_count() == 0 || array_size == 0)
  361. return Value(false);
  362. i32 from_index = 0;
  363. if (interpreter.argument_count() >= 2) {
  364. from_index = interpreter.argument(1).to_i32();
  365. if (from_index >= array_size)
  366. return Value(false);
  367. auto negative_min_index = ((array_size - 1) * -1);
  368. if (from_index < negative_min_index)
  369. from_index = 0;
  370. else if (from_index < 0)
  371. from_index = array_size + from_index;
  372. }
  373. auto value_to_find = interpreter.argument(0);
  374. for (i32 i = from_index; i < array_size; ++i) {
  375. auto& element = array->elements().at(i);
  376. if (same_value_zero(interpreter, element, value_to_find))
  377. return Value(true);
  378. }
  379. return Value(false);
  380. }
  381. Value ArrayPrototype::find(Interpreter& interpreter)
  382. {
  383. auto* array = array_from(interpreter);
  384. if (!array)
  385. return {};
  386. auto* callback = callback_from_args(interpreter, "find");
  387. if (!callback)
  388. return {};
  389. auto this_value = interpreter.argument(1);
  390. auto array_size = array->elements().size();
  391. for (size_t i = 0; i < array_size; ++i) {
  392. auto value = js_undefined();
  393. if (i < array->elements().size()) {
  394. value = array->elements().at(i);
  395. if (value.is_empty())
  396. value = js_undefined();
  397. }
  398. MarkedValueList arguments(interpreter.heap());
  399. arguments.append(value);
  400. arguments.append(Value((i32)i));
  401. arguments.append(array);
  402. auto result = interpreter.call(*callback, this_value, move(arguments));
  403. if (interpreter.exception())
  404. return {};
  405. if (result.to_boolean())
  406. return value;
  407. }
  408. return js_undefined();
  409. }
  410. Value ArrayPrototype::find_index(Interpreter& interpreter)
  411. {
  412. auto* array = array_from(interpreter);
  413. if (!array)
  414. return {};
  415. auto* callback = callback_from_args(interpreter, "findIndex");
  416. if (!callback)
  417. return {};
  418. auto this_value = interpreter.argument(1);
  419. auto array_size = array->elements().size();
  420. for (size_t i = 0; i < array_size; ++i) {
  421. auto value = js_undefined();
  422. if (i < array->elements().size()) {
  423. value = array->elements().at(i);
  424. if (value.is_empty())
  425. value = js_undefined();
  426. }
  427. MarkedValueList arguments(interpreter.heap());
  428. arguments.append(value);
  429. arguments.append(Value((i32)i));
  430. arguments.append(array);
  431. auto result = interpreter.call(*callback, this_value, move(arguments));
  432. if (interpreter.exception())
  433. return {};
  434. if (result.to_boolean())
  435. return Value((i32)i);
  436. }
  437. return Value(-1);
  438. }
  439. Value ArrayPrototype::some(Interpreter& interpreter)
  440. {
  441. auto* array = array_from(interpreter);
  442. if (!array)
  443. return {};
  444. auto* callback = callback_from_args(interpreter, "some");
  445. if (!callback)
  446. return {};
  447. auto this_value = interpreter.argument(1);
  448. auto array_size = array->elements().size();
  449. for (size_t i = 0; i < array_size; ++i) {
  450. if (i >= array->elements().size())
  451. break;
  452. auto value = array->elements().at(i);
  453. if (value.is_empty())
  454. continue;
  455. MarkedValueList arguments(interpreter.heap());
  456. arguments.append(value);
  457. arguments.append(Value((i32)i));
  458. arguments.append(array);
  459. auto result = interpreter.call(*callback, this_value, move(arguments));
  460. if (interpreter.exception())
  461. return {};
  462. if (result.to_boolean())
  463. return Value(true);
  464. }
  465. return Value(false);
  466. }
  467. }