ArrayPrototype.cpp 8.3 KB

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