ArrayPrototype.cpp 15 KB

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