ArrayConstructor.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_old_native_function(vm.names.from, from, 1, attr);
  31. define_old_native_function(vm.names.isArray, is_array, 1, attr);
  32. define_old_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_old_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. ThrowCompletionOr<Value> ArrayConstructor::call()
  39. {
  40. return TRY(construct(*this));
  41. }
  42. // 23.1.1.1 Array ( ...values ), https://tc39.es/ecma262/#sec-array
  43. ThrowCompletionOr<Object*> ArrayConstructor::construct(FunctionObject& new_target)
  44. {
  45. auto& vm = this->vm();
  46. auto* proto = TRY(get_prototype_from_constructor(global_object(), new_target, &GlobalObject::array_prototype));
  47. if (vm.argument_count() == 0)
  48. return MUST(Array::create(global_object(), 0, proto));
  49. if (vm.argument_count() == 1) {
  50. auto length = vm.argument(0);
  51. auto* array = MUST(Array::create(global_object(), 0, proto));
  52. size_t int_length;
  53. if (!length.is_number()) {
  54. MUST(array->create_data_property_or_throw(0, length));
  55. int_length = 1;
  56. } else {
  57. int_length = MUST(length.to_u32(global_object()));
  58. if (int_length != length.as_double())
  59. return vm.throw_completion<RangeError>(global_object(), ErrorType::InvalidLength, "array");
  60. }
  61. TRY(array->set(vm.names.length, Value(int_length), Object::ShouldThrowExceptions::Yes));
  62. return array;
  63. }
  64. auto* array = TRY(Array::create(global_object(), vm.argument_count(), proto));
  65. for (size_t k = 0; k < vm.argument_count(); ++k)
  66. MUST(array->create_data_property_or_throw(k, vm.argument(k)));
  67. return array;
  68. }
  69. // 23.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] ), https://tc39.es/ecma262/#sec-array.from
  70. JS_DEFINE_OLD_NATIVE_FUNCTION(ArrayConstructor::from)
  71. {
  72. auto constructor = vm.this_value(global_object);
  73. FunctionObject* map_fn = nullptr;
  74. if (!vm.argument(1).is_undefined()) {
  75. auto callback = vm.argument(1);
  76. if (!callback.is_function()) {
  77. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, callback.to_string_without_side_effects());
  78. return {};
  79. }
  80. map_fn = &callback.as_function();
  81. }
  82. auto this_arg = vm.argument(2);
  83. auto items = vm.argument(0);
  84. auto using_iterator = TRY_OR_DISCARD(items.get_method(global_object, *vm.well_known_symbol_iterator()));
  85. if (using_iterator) {
  86. Object* array;
  87. if (constructor.is_constructor())
  88. array = TRY_OR_DISCARD(JS::construct(global_object, constructor.as_function(), {}));
  89. else
  90. array = MUST(Array::create(global_object, 0));
  91. auto iterator = TRY_OR_DISCARD(get_iterator(global_object, items, IteratorHint::Sync, using_iterator));
  92. size_t k = 0;
  93. while (true) {
  94. if (k >= MAX_ARRAY_LIKE_INDEX) {
  95. auto error = vm.throw_completion<TypeError>(global_object, ErrorType::ArrayMaxSize);
  96. return TRY_OR_DISCARD(iterator_close(*iterator, move(error)));
  97. }
  98. auto* next = TRY_OR_DISCARD(iterator_step(global_object, *iterator));
  99. if (!next) {
  100. TRY_OR_DISCARD(array->set(vm.names.length, Value(k), Object::ShouldThrowExceptions::Yes));
  101. return array;
  102. }
  103. auto next_value = TRY_OR_DISCARD(iterator_value(global_object, *next));
  104. Value mapped_value;
  105. if (map_fn) {
  106. auto mapped_value_or_error = vm.call(*map_fn, this_arg, next_value, Value(k));
  107. if (mapped_value_or_error.is_error())
  108. return TRY_OR_DISCARD(iterator_close(*iterator, mapped_value_or_error.release_error()));
  109. mapped_value = mapped_value_or_error.release_value();
  110. } else {
  111. mapped_value = next_value;
  112. }
  113. auto result_or_error = array->create_data_property_or_throw(k, mapped_value);
  114. if (result_or_error.is_error())
  115. return TRY_OR_DISCARD(iterator_close(*iterator, result_or_error.release_error()));
  116. ++k;
  117. }
  118. }
  119. auto* array_like = MUST(items.to_object(global_object));
  120. auto length = TRY_OR_DISCARD(length_of_array_like(global_object, *array_like));
  121. Object* array;
  122. if (constructor.is_constructor()) {
  123. MarkedValueList arguments(vm.heap());
  124. arguments.empend(length);
  125. array = TRY_OR_DISCARD(JS::construct(global_object, constructor.as_function(), move(arguments)));
  126. } else {
  127. array = TRY_OR_DISCARD(Array::create(global_object, length));
  128. }
  129. for (size_t k = 0; k < length; ++k) {
  130. auto k_value = TRY_OR_DISCARD(array_like->get(k));
  131. Value mapped_value;
  132. if (map_fn)
  133. mapped_value = TRY_OR_DISCARD(vm.call(*map_fn, this_arg, k_value, Value(k)));
  134. else
  135. mapped_value = k_value;
  136. TRY_OR_DISCARD(array->create_data_property_or_throw(k, mapped_value));
  137. }
  138. TRY_OR_DISCARD(array->set(vm.names.length, Value(length), Object::ShouldThrowExceptions::Yes));
  139. return array;
  140. }
  141. // 23.1.2.2 Array.isArray ( arg ), https://tc39.es/ecma262/#sec-array.isarray
  142. JS_DEFINE_OLD_NATIVE_FUNCTION(ArrayConstructor::is_array)
  143. {
  144. auto value = vm.argument(0);
  145. return Value(TRY_OR_DISCARD(value.is_array(global_object)));
  146. }
  147. // 23.1.2.3 Array.of ( ...items ), https://tc39.es/ecma262/#sec-array.of
  148. JS_DEFINE_OLD_NATIVE_FUNCTION(ArrayConstructor::of)
  149. {
  150. auto this_value = vm.this_value(global_object);
  151. Object* array;
  152. if (this_value.is_constructor()) {
  153. MarkedValueList arguments(vm.heap());
  154. arguments.empend(vm.argument_count());
  155. array = TRY_OR_DISCARD(JS::construct(global_object, this_value.as_function(), move(arguments)));
  156. } else {
  157. array = TRY_OR_DISCARD(Array::create(global_object, vm.argument_count()));
  158. }
  159. for (size_t k = 0; k < vm.argument_count(); ++k)
  160. TRY_OR_DISCARD(array->create_data_property_or_throw(k, vm.argument(k)));
  161. TRY_OR_DISCARD(array->set(vm.names.length, Value(vm.argument_count()), Object::ShouldThrowExceptions::Yes));
  162. return array;
  163. }
  164. // 23.1.2.5 get Array [ @@species ], https://tc39.es/ecma262/#sec-get-array-@@species
  165. JS_DEFINE_OLD_NATIVE_FUNCTION(ArrayConstructor::symbol_species_getter)
  166. {
  167. return vm.this_value(global_object);
  168. }
  169. }