ArrayPrototype.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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/Value.h>
  37. namespace JS {
  38. ArrayPrototype::ArrayPrototype()
  39. : Object(interpreter().global_object().object_prototype())
  40. {
  41. put_native_function("filter", filter, 1);
  42. put_native_function("forEach", for_each, 1);
  43. put_native_function("map", map, 1);
  44. put_native_function("pop", pop, 0);
  45. put_native_function("push", push, 1);
  46. put_native_function("shift", shift, 0);
  47. put_native_function("toString", to_string, 0);
  48. put_native_function("unshift", unshift, 1);
  49. put_native_function("join", join, 1);
  50. put_native_function("concat", concat, 1);
  51. put("length", Value(0));
  52. }
  53. ArrayPrototype::~ArrayPrototype()
  54. {
  55. }
  56. static Array* array_from(Interpreter& interpreter)
  57. {
  58. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  59. if (!this_object)
  60. return {};
  61. if (!this_object->is_array()) {
  62. interpreter.throw_exception<TypeError>("Not an Array");
  63. return nullptr;
  64. }
  65. return static_cast<Array*>(this_object);
  66. }
  67. static Function* callback_from_args(Interpreter& interpreter, const String& name)
  68. {
  69. if (interpreter.argument_count() < 1) {
  70. interpreter.throw_exception<TypeError>(String::format("Array.prototype.%s() requires at least one argument", name.characters()));
  71. return nullptr;
  72. }
  73. auto callback = interpreter.argument(0);
  74. if (!callback.is_object() || !callback.as_object().is_function()) {
  75. interpreter.throw_exception<TypeError>(String::format("%s is not a function", callback.to_string().characters()));
  76. return nullptr;
  77. }
  78. return static_cast<Function*>(&callback.as_object());
  79. }
  80. Value ArrayPrototype::filter(Interpreter& interpreter)
  81. {
  82. auto* array = array_from(interpreter);
  83. if (!array)
  84. return {};
  85. auto* callback = callback_from_args(interpreter, "filter");
  86. if (!callback)
  87. return {};
  88. auto this_value = interpreter.argument(1);
  89. auto initial_array_size = array->elements().size();
  90. auto* new_array = Array::create(interpreter.global_object());
  91. for (size_t i = 0; i < initial_array_size; ++i) {
  92. if (i >= array->elements().size())
  93. break;
  94. auto value = array->elements()[i];
  95. if (value.is_empty())
  96. continue;
  97. auto result = interpreter.call(callback, this_value, { value, Value((i32)i), array });
  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. interpreter.call(callback, this_value, { value, Value((i32)i), array });
  122. if (interpreter.exception())
  123. return {};
  124. }
  125. return js_undefined();
  126. }
  127. Value ArrayPrototype::map(Interpreter& interpreter)
  128. {
  129. auto* array = array_from(interpreter);
  130. if (!array)
  131. return {};
  132. auto* callback = callback_from_args(interpreter, "map");
  133. if (!callback)
  134. return {};
  135. auto this_value = interpreter.argument(1);
  136. auto initial_array_size = array->elements().size();
  137. auto* new_array = Array::create(interpreter.global_object());
  138. for (size_t i = 0; i < initial_array_size; ++i) {
  139. if (i >= array->elements().size())
  140. break;
  141. auto value = array->elements()[i];
  142. if (value.is_empty())
  143. continue;
  144. auto result = interpreter.call(callback, this_value, { value, Value((i32)i), array });
  145. if (interpreter.exception())
  146. return {};
  147. new_array->elements().append(result);
  148. }
  149. return Value(new_array);
  150. }
  151. Value ArrayPrototype::push(Interpreter& interpreter)
  152. {
  153. auto* array = array_from(interpreter);
  154. if (!array)
  155. return {};
  156. for (size_t i = 0; i < interpreter.argument_count(); ++i)
  157. array->elements().append(interpreter.argument(i));
  158. return Value(array->length());
  159. }
  160. Value ArrayPrototype::unshift(Interpreter& interpreter)
  161. {
  162. auto* array = array_from(interpreter);
  163. if (!array)
  164. return {};
  165. for (size_t i = 0; i < interpreter.argument_count(); ++i)
  166. array->elements().insert(i, interpreter.argument(i));
  167. return Value(array->length());
  168. }
  169. Value ArrayPrototype::pop(Interpreter& interpreter)
  170. {
  171. auto* array = array_from(interpreter);
  172. if (!array)
  173. return {};
  174. if (array->elements().is_empty())
  175. return js_undefined();
  176. return array->elements().take_last();
  177. }
  178. Value ArrayPrototype::shift(Interpreter& interpreter)
  179. {
  180. auto* array = array_from(interpreter);
  181. if (!array)
  182. return {};
  183. if (array->elements().is_empty())
  184. return js_undefined();
  185. return array->elements().take_first();
  186. }
  187. static Value join_array_with_separator(Interpreter& interpreter, const Array& array, StringView separator)
  188. {
  189. StringBuilder builder;
  190. for (size_t i = 0; i < array.elements().size(); ++i) {
  191. if (i != 0)
  192. builder.append(separator);
  193. if (!array.elements()[i].is_empty())
  194. builder.append(array.elements()[i].to_string());
  195. }
  196. return js_string(interpreter, builder.to_string());
  197. }
  198. Value ArrayPrototype::to_string(Interpreter& interpreter)
  199. {
  200. auto* array = array_from(interpreter);
  201. if (!array)
  202. return {};
  203. return join_array_with_separator(interpreter, *array, ",");
  204. }
  205. Value ArrayPrototype::join(Interpreter& interpreter)
  206. {
  207. auto* array = array_from(interpreter);
  208. if (!array)
  209. return {};
  210. String separator = ",";
  211. if (interpreter.argument_count() == 1)
  212. separator = interpreter.argument(0).to_string();
  213. return join_array_with_separator(interpreter, *array, separator);
  214. }
  215. Value ArrayPrototype::concat(Interpreter& interpreter)
  216. {
  217. auto* array = array_from(interpreter);
  218. if (!array)
  219. return {};
  220. auto* new_array = Array::create(interpreter.global_object());
  221. new_array->elements().append(array->elements());
  222. for (size_t i = 0; i < interpreter.argument_count(); ++i) {
  223. auto argument = interpreter.argument(i);
  224. if (argument.is_array()) {
  225. auto& argument_object = argument.as_object();
  226. new_array->elements().append(argument_object.elements());
  227. } else {
  228. new_array->elements().append(argument);
  229. }
  230. }
  231. return Value(new_array);
  232. }
  233. }