ArrayPrototype.cpp 7.6 KB

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