ArrayPrototype.cpp 14 KB

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