ArrayPrototype.cpp 6.2 KB

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