ArrayPrototype.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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("length", Value(0));
  56. }
  57. ArrayPrototype::~ArrayPrototype()
  58. {
  59. }
  60. static Array* array_from(Interpreter& interpreter)
  61. {
  62. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  63. if (!this_object)
  64. return {};
  65. if (!this_object->is_array()) {
  66. interpreter.throw_exception<TypeError>("Not an Array");
  67. return nullptr;
  68. }
  69. return static_cast<Array*>(this_object);
  70. }
  71. static Function* callback_from_args(Interpreter& interpreter, const String& name)
  72. {
  73. if (interpreter.argument_count() < 1) {
  74. interpreter.throw_exception<TypeError>(String::format("Array.prototype.%s() requires at least one argument", name.characters()));
  75. return nullptr;
  76. }
  77. auto callback = interpreter.argument(0);
  78. if (!callback.is_object() || !callback.as_object().is_function()) {
  79. interpreter.throw_exception<TypeError>(String::format("%s is not a function", callback.to_string().characters()));
  80. return nullptr;
  81. }
  82. return static_cast<Function*>(&callback.as_object());
  83. }
  84. Value ArrayPrototype::filter(Interpreter& interpreter)
  85. {
  86. auto* array = array_from(interpreter);
  87. if (!array)
  88. return {};
  89. auto* callback = callback_from_args(interpreter, "filter");
  90. if (!callback)
  91. return {};
  92. auto this_value = interpreter.argument(1);
  93. auto initial_array_size = array->elements().size();
  94. auto* new_array = Array::create(interpreter.global_object());
  95. for (size_t i = 0; i < initial_array_size; ++i) {
  96. if (i >= array->elements().size())
  97. break;
  98. auto value = array->elements()[i];
  99. if (value.is_empty())
  100. continue;
  101. MarkedValueList arguments(interpreter.heap());
  102. arguments.append(value);
  103. arguments.append(Value((i32)i));
  104. arguments.append(array);
  105. auto result = interpreter.call(callback, this_value, move(arguments));
  106. if (interpreter.exception())
  107. return {};
  108. if (result.to_boolean())
  109. new_array->elements().append(value);
  110. }
  111. return Value(new_array);
  112. }
  113. Value ArrayPrototype::for_each(Interpreter& interpreter)
  114. {
  115. auto* array = array_from(interpreter);
  116. if (!array)
  117. return {};
  118. auto* callback = callback_from_args(interpreter, "forEach");
  119. if (!callback)
  120. return {};
  121. auto this_value = interpreter.argument(1);
  122. auto initial_array_size = array->elements().size();
  123. for (size_t i = 0; i < initial_array_size; ++i) {
  124. if (i >= array->elements().size())
  125. break;
  126. auto value = array->elements()[i];
  127. if (value.is_empty())
  128. continue;
  129. MarkedValueList arguments(interpreter.heap());
  130. arguments.append(value);
  131. arguments.append(Value((i32)i));
  132. arguments.append(array);
  133. interpreter.call(callback, this_value, move(arguments));
  134. if (interpreter.exception())
  135. return {};
  136. }
  137. return js_undefined();
  138. }
  139. Value ArrayPrototype::map(Interpreter& interpreter)
  140. {
  141. auto* array = array_from(interpreter);
  142. if (!array)
  143. return {};
  144. auto* callback = callback_from_args(interpreter, "map");
  145. if (!callback)
  146. return {};
  147. auto this_value = interpreter.argument(1);
  148. auto initial_array_size = array->elements().size();
  149. auto* new_array = Array::create(interpreter.global_object());
  150. for (size_t i = 0; i < initial_array_size; ++i) {
  151. if (i >= array->elements().size())
  152. break;
  153. auto value = array->elements()[i];
  154. if (value.is_empty())
  155. continue;
  156. MarkedValueList arguments(interpreter.heap());
  157. arguments.append(value);
  158. arguments.append(Value((i32)i));
  159. arguments.append(array);
  160. auto result = interpreter.call(callback, this_value, move(arguments));
  161. if (interpreter.exception())
  162. return {};
  163. new_array->elements().append(result);
  164. }
  165. return Value(new_array);
  166. }
  167. Value ArrayPrototype::push(Interpreter& interpreter)
  168. {
  169. auto* array = array_from(interpreter);
  170. if (!array)
  171. return {};
  172. for (size_t i = 0; i < interpreter.argument_count(); ++i)
  173. array->elements().append(interpreter.argument(i));
  174. return Value(array->length());
  175. }
  176. Value ArrayPrototype::unshift(Interpreter& interpreter)
  177. {
  178. auto* array = array_from(interpreter);
  179. if (!array)
  180. return {};
  181. for (size_t i = 0; i < interpreter.argument_count(); ++i)
  182. array->elements().insert(i, interpreter.argument(i));
  183. return Value(array->length());
  184. }
  185. Value ArrayPrototype::pop(Interpreter& interpreter)
  186. {
  187. auto* array = array_from(interpreter);
  188. if (!array)
  189. return {};
  190. if (array->elements().is_empty())
  191. return js_undefined();
  192. return array->elements().take_last();
  193. }
  194. Value ArrayPrototype::shift(Interpreter& interpreter)
  195. {
  196. auto* array = array_from(interpreter);
  197. if (!array)
  198. return {};
  199. if (array->elements().is_empty())
  200. return js_undefined();
  201. return array->elements().take_first();
  202. }
  203. static Value join_array_with_separator(Interpreter& interpreter, const Array& array, StringView separator)
  204. {
  205. StringBuilder builder;
  206. for (size_t i = 0; i < array.elements().size(); ++i) {
  207. if (i != 0)
  208. builder.append(separator);
  209. if (!array.elements()[i].is_empty())
  210. builder.append(array.elements()[i].to_string());
  211. }
  212. return js_string(interpreter, builder.to_string());
  213. }
  214. Value ArrayPrototype::to_string(Interpreter& interpreter)
  215. {
  216. auto* array = array_from(interpreter);
  217. if (!array)
  218. return {};
  219. return join_array_with_separator(interpreter, *array, ",");
  220. }
  221. Value ArrayPrototype::join(Interpreter& interpreter)
  222. {
  223. auto* array = array_from(interpreter);
  224. if (!array)
  225. return {};
  226. String separator = ",";
  227. if (interpreter.argument_count() == 1)
  228. separator = interpreter.argument(0).to_string();
  229. return join_array_with_separator(interpreter, *array, separator);
  230. }
  231. Value ArrayPrototype::concat(Interpreter& interpreter)
  232. {
  233. auto* array = array_from(interpreter);
  234. if (!array)
  235. return {};
  236. auto* new_array = Array::create(interpreter.global_object());
  237. new_array->elements().append(array->elements());
  238. for (size_t i = 0; i < interpreter.argument_count(); ++i) {
  239. auto argument = interpreter.argument(i);
  240. if (argument.is_array()) {
  241. auto& argument_object = argument.as_object();
  242. new_array->elements().append(argument_object.elements());
  243. } else {
  244. new_array->elements().append(argument);
  245. }
  246. }
  247. return Value(new_array);
  248. }
  249. Value ArrayPrototype::slice(Interpreter& interpreter)
  250. {
  251. auto* array = array_from(interpreter);
  252. if (!array)
  253. return {};
  254. auto* new_array = Array::create(interpreter.global_object());
  255. if (interpreter.argument_count() == 0) {
  256. new_array->elements().append(array->elements());
  257. return new_array;
  258. }
  259. ssize_t array_size = static_cast<ssize_t>(array->elements().size());
  260. auto start_slice = interpreter.argument(0).to_i32();
  261. auto end_slice = array_size;
  262. if (start_slice > array_size)
  263. return new_array;
  264. if (start_slice < 0)
  265. start_slice = end_slice + start_slice;
  266. if (interpreter.argument_count() >= 2) {
  267. end_slice = interpreter.argument(1).to_i32();
  268. if (end_slice < 0)
  269. end_slice = array_size + end_slice;
  270. else if (end_slice > array_size)
  271. end_slice = array_size;
  272. }
  273. size_t array_capacity = start_slice + array_size - end_slice;
  274. new_array->elements().ensure_capacity(array_capacity);
  275. for (ssize_t i = start_slice; i < end_slice; ++i) {
  276. new_array->elements().append(array->elements().at(i));
  277. }
  278. return new_array;
  279. }
  280. Value ArrayPrototype::index_of(Interpreter& interpreter)
  281. {
  282. auto* array = array_from(interpreter);
  283. if (!array)
  284. return {};
  285. i32 array_size = static_cast<i32>(array->elements().size());
  286. if (interpreter.argument_count() == 0 || array_size == 0)
  287. return Value(-1);
  288. i32 from_index = 0;
  289. if (interpreter.argument_count() >= 2) {
  290. from_index = interpreter.argument(1).to_number().to_i32();
  291. if (from_index >= array_size)
  292. return Value(-1);
  293. auto negative_min_index = ((array_size - 1) * -1);
  294. if (from_index < negative_min_index)
  295. from_index = 0;
  296. else if (from_index < 0)
  297. from_index = (array_size - 1) + from_index;
  298. }
  299. auto search_element = interpreter.argument(0);
  300. for (i32 i = from_index; i < array_size; ++i) {
  301. auto& element = array->elements().at(i);
  302. if (typed_eq(interpreter, element, search_element).as_bool())
  303. return Value(i);
  304. }
  305. return Value(-1);
  306. }
  307. Value ArrayPrototype::reverse(Interpreter& interpreter)
  308. {
  309. auto* array = array_from(interpreter);
  310. if (!array)
  311. return {};
  312. if (array->elements().size() == 0)
  313. return array;
  314. Vector<Value> array_reverse;
  315. array_reverse.ensure_capacity(array->elements().size());
  316. for (ssize_t i = array->elements().size() - 1; i >= 0; --i)
  317. array_reverse.append(array->elements().at(i));
  318. array->elements() = move(array_reverse);
  319. return array;
  320. }
  321. }