ArrayPrototype.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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("length", Value(0));
  54. }
  55. ArrayPrototype::~ArrayPrototype()
  56. {
  57. }
  58. static Array* array_from(Interpreter& interpreter)
  59. {
  60. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  61. if (!this_object)
  62. return {};
  63. if (!this_object->is_array()) {
  64. interpreter.throw_exception<TypeError>("Not an Array");
  65. return nullptr;
  66. }
  67. return static_cast<Array*>(this_object);
  68. }
  69. static Function* callback_from_args(Interpreter& interpreter, const String& name)
  70. {
  71. if (interpreter.argument_count() < 1) {
  72. interpreter.throw_exception<TypeError>(String::format("Array.prototype.%s() requires at least one argument", name.characters()));
  73. return nullptr;
  74. }
  75. auto callback = interpreter.argument(0);
  76. if (!callback.is_object() || !callback.as_object().is_function()) {
  77. interpreter.throw_exception<TypeError>(String::format("%s is not a function", callback.to_string().characters()));
  78. return nullptr;
  79. }
  80. return static_cast<Function*>(&callback.as_object());
  81. }
  82. Value ArrayPrototype::filter(Interpreter& interpreter)
  83. {
  84. auto* array = array_from(interpreter);
  85. if (!array)
  86. return {};
  87. auto* callback = callback_from_args(interpreter, "filter");
  88. if (!callback)
  89. return {};
  90. auto this_value = interpreter.argument(1);
  91. auto initial_array_size = array->elements().size();
  92. auto* new_array = Array::create(interpreter.global_object());
  93. for (size_t i = 0; i < initial_array_size; ++i) {
  94. if (i >= array->elements().size())
  95. break;
  96. auto value = array->elements()[i];
  97. if (value.is_empty())
  98. continue;
  99. MarkedValueList arguments(interpreter.heap());
  100. arguments.append(value);
  101. arguments.append(Value((i32)i));
  102. arguments.append(array);
  103. auto result = interpreter.call(callback, this_value, move(arguments));
  104. if (interpreter.exception())
  105. return {};
  106. if (result.to_boolean())
  107. new_array->elements().append(value);
  108. }
  109. return Value(new_array);
  110. }
  111. Value ArrayPrototype::for_each(Interpreter& interpreter)
  112. {
  113. auto* array = array_from(interpreter);
  114. if (!array)
  115. return {};
  116. auto* callback = callback_from_args(interpreter, "forEach");
  117. if (!callback)
  118. return {};
  119. auto this_value = interpreter.argument(1);
  120. auto initial_array_size = array->elements().size();
  121. for (size_t i = 0; i < initial_array_size; ++i) {
  122. if (i >= array->elements().size())
  123. break;
  124. auto value = array->elements()[i];
  125. if (value.is_empty())
  126. continue;
  127. MarkedValueList arguments(interpreter.heap());
  128. arguments.append(value);
  129. arguments.append(Value((i32)i));
  130. arguments.append(array);
  131. interpreter.call(callback, this_value, move(arguments));
  132. if (interpreter.exception())
  133. return {};
  134. }
  135. return js_undefined();
  136. }
  137. Value ArrayPrototype::map(Interpreter& interpreter)
  138. {
  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. 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().append(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();
  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();
  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. if (!array.elements()[i].is_empty())
  208. builder.append(array.elements()[i].to_string());
  209. }
  210. return js_string(interpreter, builder.to_string());
  211. }
  212. Value ArrayPrototype::to_string(Interpreter& interpreter)
  213. {
  214. auto* array = array_from(interpreter);
  215. if (!array)
  216. return {};
  217. return join_array_with_separator(interpreter, *array, ",");
  218. }
  219. Value ArrayPrototype::join(Interpreter& interpreter)
  220. {
  221. auto* array = array_from(interpreter);
  222. if (!array)
  223. return {};
  224. String separator = ",";
  225. if (interpreter.argument_count() == 1)
  226. separator = interpreter.argument(0).to_string();
  227. return join_array_with_separator(interpreter, *array, separator);
  228. }
  229. Value ArrayPrototype::concat(Interpreter& interpreter)
  230. {
  231. auto* array = array_from(interpreter);
  232. if (!array)
  233. return {};
  234. auto* new_array = Array::create(interpreter.global_object());
  235. new_array->elements().append(array->elements());
  236. for (size_t i = 0; i < interpreter.argument_count(); ++i) {
  237. auto argument = interpreter.argument(i);
  238. if (argument.is_array()) {
  239. auto& argument_object = argument.as_object();
  240. new_array->elements().append(argument_object.elements());
  241. } else {
  242. new_array->elements().append(argument);
  243. }
  244. }
  245. return Value(new_array);
  246. }
  247. Value ArrayPrototype::slice(Interpreter& interpreter)
  248. {
  249. auto* array = array_from(interpreter);
  250. if (!array)
  251. return {};
  252. auto* new_array = Array::create(interpreter.global_object());
  253. if (interpreter.argument_count() == 0) {
  254. new_array->elements().append(array->elements());
  255. return new_array;
  256. }
  257. ssize_t array_size = static_cast<ssize_t>(array->elements().size());
  258. auto start_slice = interpreter.argument(0).to_i32();
  259. auto end_slice = array_size;
  260. if (start_slice > array_size)
  261. return new_array;
  262. if (start_slice < 0)
  263. start_slice = end_slice + start_slice;
  264. if (interpreter.argument_count() >= 2) {
  265. end_slice = interpreter.argument(1).to_i32();
  266. if (end_slice < 0)
  267. end_slice = array_size + end_slice;
  268. else if (end_slice > array_size)
  269. end_slice = array_size;
  270. }
  271. size_t array_capacity = start_slice + array_size - end_slice;
  272. new_array->elements().ensure_capacity(array_capacity);
  273. for (ssize_t i = start_slice; i < end_slice; ++i) {
  274. new_array->elements().append(array->elements().at(i));
  275. }
  276. return new_array;
  277. }
  278. }