ArrayConstructor.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Function.h>
  8. #include <LibJS/Runtime/AbstractOperations.h>
  9. #include <LibJS/Runtime/Array.h>
  10. #include <LibJS/Runtime/ArrayConstructor.h>
  11. #include <LibJS/Runtime/Error.h>
  12. #include <LibJS/Runtime/GlobalObject.h>
  13. #include <LibJS/Runtime/IteratorOperations.h>
  14. #include <LibJS/Runtime/Shape.h>
  15. namespace JS {
  16. ArrayConstructor::ArrayConstructor(GlobalObject& global_object)
  17. : NativeFunction(vm().names.Array.as_string(), *global_object.function_prototype())
  18. {
  19. }
  20. ArrayConstructor::~ArrayConstructor()
  21. {
  22. }
  23. void ArrayConstructor::initialize(GlobalObject& global_object)
  24. {
  25. auto& vm = this->vm();
  26. NativeFunction::initialize(global_object);
  27. // 23.1.2.4 Array.prototype, https://tc39.es/ecma262/#sec-array.prototype
  28. define_direct_property(vm.names.prototype, global_object.array_prototype(), 0);
  29. u8 attr = Attribute::Writable | Attribute::Configurable;
  30. define_native_function(vm.names.from, from, 1, attr);
  31. define_native_function(vm.names.isArray, is_array, 1, attr);
  32. define_native_function(vm.names.of, of, 0, attr);
  33. // 23.1.2.5 get Array [ @@species ], https://tc39.es/ecma262/#sec-get-array-@@species
  34. define_native_accessor(*vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
  35. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  36. }
  37. // 23.1.1.1 Array ( ...values ), https://tc39.es/ecma262/#sec-array
  38. Value ArrayConstructor::call()
  39. {
  40. return construct(*this);
  41. }
  42. // 23.1.1.1 Array ( ...values ), https://tc39.es/ecma262/#sec-array
  43. Value ArrayConstructor::construct(FunctionObject& new_target)
  44. {
  45. auto& vm = this->vm();
  46. auto* proto = get_prototype_from_constructor(global_object(), new_target, &GlobalObject::array_prototype);
  47. if (vm.exception())
  48. return {};
  49. if (vm.argument_count() == 0)
  50. return Array::create(global_object(), 0, proto);
  51. if (vm.argument_count() == 1) {
  52. auto length = vm.argument(0);
  53. auto* array = Array::create(global_object(), 0, proto);
  54. size_t int_length;
  55. if (!length.is_number()) {
  56. array->create_data_property_or_throw(0, length);
  57. int_length = 1;
  58. } else {
  59. int_length = length.to_u32(global_object());
  60. if (int_length != length.as_double()) {
  61. vm.throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "array");
  62. return {};
  63. }
  64. }
  65. array->set(vm.names.length, Value(int_length), Object::ShouldThrowExceptions::Yes);
  66. if (vm.exception())
  67. return {};
  68. return array;
  69. }
  70. auto* array = Array::create(global_object(), vm.argument_count(), proto);
  71. if (vm.exception())
  72. return {};
  73. for (size_t k = 0; k < vm.argument_count(); ++k)
  74. array->create_data_property_or_throw(k, vm.argument(k));
  75. return array;
  76. }
  77. // 23.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] ), https://tc39.es/ecma262/#sec-array.from
  78. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from)
  79. {
  80. auto constructor = vm.this_value(global_object);
  81. FunctionObject* map_fn = nullptr;
  82. if (!vm.argument(1).is_undefined()) {
  83. auto callback = vm.argument(1);
  84. if (!callback.is_function()) {
  85. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, callback.to_string_without_side_effects());
  86. return {};
  87. }
  88. map_fn = &callback.as_function();
  89. }
  90. auto this_arg = vm.argument(2);
  91. auto items = vm.argument(0);
  92. auto using_iterator = items.get_method(global_object, *vm.well_known_symbol_iterator());
  93. if (vm.exception())
  94. return {};
  95. if (using_iterator) {
  96. Value array;
  97. if (constructor.is_constructor()) {
  98. array = vm.construct(constructor.as_function(), constructor.as_function(), {});
  99. if (vm.exception())
  100. return {};
  101. } else {
  102. array = Array::create(global_object, 0);
  103. }
  104. auto iterator = get_iterator(global_object, items, IteratorHint::Sync, using_iterator);
  105. if (vm.exception())
  106. return {};
  107. auto& array_object = array.as_object();
  108. size_t k = 0;
  109. while (true) {
  110. if (k >= MAX_ARRAY_LIKE_INDEX) {
  111. vm.throw_exception<TypeError>(global_object, ErrorType::ArrayMaxSize);
  112. iterator_close(*iterator);
  113. return {};
  114. }
  115. auto next = iterator_step(global_object, *iterator);
  116. if (vm.exception())
  117. return {};
  118. if (!next) {
  119. array_object.set(vm.names.length, Value(k), Object::ShouldThrowExceptions::Yes);
  120. if (vm.exception())
  121. return {};
  122. return array;
  123. }
  124. auto next_value = iterator_value(global_object, *next);
  125. if (vm.exception())
  126. return {};
  127. Value mapped_value;
  128. if (map_fn) {
  129. mapped_value = vm.call(*map_fn, this_arg, next_value, Value(k));
  130. if (vm.exception()) {
  131. iterator_close(*iterator);
  132. return {};
  133. }
  134. } else {
  135. mapped_value = next_value;
  136. }
  137. array_object.create_data_property_or_throw(k, mapped_value);
  138. if (vm.exception()) {
  139. iterator_close(*iterator);
  140. return {};
  141. }
  142. ++k;
  143. }
  144. }
  145. auto* array_like = items.to_object(global_object);
  146. auto length = length_of_array_like(global_object, *array_like);
  147. if (vm.exception())
  148. return {};
  149. Value array;
  150. if (constructor.is_constructor()) {
  151. MarkedValueList arguments(vm.heap());
  152. arguments.empend(length);
  153. array = vm.construct(constructor.as_function(), constructor.as_function(), move(arguments));
  154. if (vm.exception())
  155. return {};
  156. } else {
  157. array = Array::create(global_object, length);
  158. if (vm.exception())
  159. return {};
  160. }
  161. auto& array_object = array.as_object();
  162. for (size_t k = 0; k < length; ++k) {
  163. auto k_value = array_like->get(k);
  164. Value mapped_value;
  165. if (map_fn) {
  166. mapped_value = vm.call(*map_fn, this_arg, k_value, Value(k));
  167. if (vm.exception())
  168. return {};
  169. } else {
  170. mapped_value = k_value;
  171. }
  172. array_object.create_data_property_or_throw(k, mapped_value);
  173. }
  174. array_object.set(vm.names.length, Value(length), Object::ShouldThrowExceptions::Yes);
  175. if (vm.exception())
  176. return {};
  177. return array;
  178. }
  179. // 23.1.2.2 Array.isArray ( arg ), https://tc39.es/ecma262/#sec-array.isarray
  180. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::is_array)
  181. {
  182. auto value = vm.argument(0);
  183. return Value(value.is_array(global_object));
  184. }
  185. // 23.1.2.3 Array.of ( ...items ), https://tc39.es/ecma262/#sec-array.of
  186. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::of)
  187. {
  188. auto this_value = vm.this_value(global_object);
  189. Value array;
  190. if (this_value.is_constructor()) {
  191. MarkedValueList arguments(vm.heap());
  192. arguments.empend(vm.argument_count());
  193. array = vm.construct(this_value.as_function(), this_value.as_function(), move(arguments));
  194. if (vm.exception())
  195. return {};
  196. } else {
  197. array = Array::create(global_object, vm.argument_count());
  198. if (vm.exception())
  199. return {};
  200. }
  201. auto& array_object = array.as_object();
  202. for (size_t k = 0; k < vm.argument_count(); ++k) {
  203. array_object.create_data_property_or_throw(k, vm.argument(k));
  204. if (vm.exception())
  205. return {};
  206. }
  207. array_object.set(vm.names.length, Value(vm.argument_count()), Object::ShouldThrowExceptions::Yes);
  208. if (vm.exception())
  209. return {};
  210. return array;
  211. }
  212. // 23.1.2.5 get Array [ @@species ], https://tc39.es/ecma262/#sec-get-array-@@species
  213. JS_DEFINE_NATIVE_GETTER(ArrayConstructor::symbol_species_getter)
  214. {
  215. return vm.this_value(global_object);
  216. }
  217. }