ArrayConstructor.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2022, 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(Realm& realm)
  17. : NativeFunction(vm().names.Array.as_string(), *realm.global_object().function_prototype())
  18. {
  19. }
  20. void ArrayConstructor::initialize(Realm& realm)
  21. {
  22. auto& vm = this->vm();
  23. NativeFunction::initialize(realm);
  24. // 23.1.2.4 Array.prototype, https://tc39.es/ecma262/#sec-array.prototype
  25. define_direct_property(vm.names.prototype, realm.global_object().array_prototype(), 0);
  26. u8 attr = Attribute::Writable | Attribute::Configurable;
  27. define_native_function(vm.names.from, from, 1, attr);
  28. define_native_function(vm.names.isArray, is_array, 1, attr);
  29. define_native_function(vm.names.of, of, 0, attr);
  30. // 23.1.2.5 get Array [ @@species ], https://tc39.es/ecma262/#sec-get-array-@@species
  31. define_native_accessor(*vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
  32. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  33. }
  34. // 23.1.1.1 Array ( ...values ), https://tc39.es/ecma262/#sec-array
  35. ThrowCompletionOr<Value> ArrayConstructor::call()
  36. {
  37. return TRY(construct(*this));
  38. }
  39. // 23.1.1.1 Array ( ...values ), https://tc39.es/ecma262/#sec-array
  40. ThrowCompletionOr<Object*> ArrayConstructor::construct(FunctionObject& new_target)
  41. {
  42. auto& vm = this->vm();
  43. auto& global_object = this->global_object();
  44. auto& realm = *global_object.associated_realm();
  45. auto* proto = TRY(get_prototype_from_constructor(global_object, new_target, &GlobalObject::array_prototype));
  46. if (vm.argument_count() == 0)
  47. return MUST(Array::create(realm, 0, proto));
  48. if (vm.argument_count() == 1) {
  49. auto length = vm.argument(0);
  50. auto* array = MUST(Array::create(realm, 0, proto));
  51. size_t int_length;
  52. if (!length.is_number()) {
  53. MUST(array->create_data_property_or_throw(0, length));
  54. int_length = 1;
  55. } else {
  56. int_length = MUST(length.to_u32(vm));
  57. if (int_length != length.as_double())
  58. return vm.throw_completion<RangeError>(ErrorType::InvalidLength, "array");
  59. }
  60. TRY(array->set(vm.names.length, Value(int_length), Object::ShouldThrowExceptions::Yes));
  61. return array;
  62. }
  63. auto* array = TRY(Array::create(realm, vm.argument_count(), proto));
  64. for (size_t k = 0; k < vm.argument_count(); ++k)
  65. MUST(array->create_data_property_or_throw(k, vm.argument(k)));
  66. return array;
  67. }
  68. // 23.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] ), https://tc39.es/ecma262/#sec-array.from
  69. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from)
  70. {
  71. auto& realm = *global_object.associated_realm();
  72. auto constructor = vm.this_value();
  73. FunctionObject* map_fn = nullptr;
  74. if (!vm.argument(1).is_undefined()) {
  75. auto callback = vm.argument(1);
  76. if (!callback.is_function())
  77. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, callback.to_string_without_side_effects());
  78. map_fn = &callback.as_function();
  79. }
  80. auto this_arg = vm.argument(2);
  81. auto items = vm.argument(0);
  82. auto using_iterator = TRY(items.get_method(vm, *vm.well_known_symbol_iterator()));
  83. if (using_iterator) {
  84. Object* array;
  85. if (constructor.is_constructor())
  86. array = TRY(JS::construct(global_object, constructor.as_function(), {}));
  87. else
  88. array = MUST(Array::create(realm, 0));
  89. auto iterator = TRY(get_iterator(vm, items, IteratorHint::Sync, using_iterator));
  90. size_t k = 0;
  91. while (true) {
  92. if (k >= MAX_ARRAY_LIKE_INDEX) {
  93. auto error = vm.throw_completion<TypeError>(ErrorType::ArrayMaxSize);
  94. return TRY(iterator_close(vm, iterator, move(error)));
  95. }
  96. auto* next = TRY(iterator_step(vm, iterator));
  97. if (!next) {
  98. TRY(array->set(vm.names.length, Value(k), Object::ShouldThrowExceptions::Yes));
  99. return array;
  100. }
  101. auto next_value = TRY(iterator_value(vm, *next));
  102. Value mapped_value;
  103. if (map_fn) {
  104. auto mapped_value_or_error = JS::call(global_object, *map_fn, this_arg, next_value, Value(k));
  105. if (mapped_value_or_error.is_error())
  106. return TRY(iterator_close(vm, iterator, mapped_value_or_error.release_error()));
  107. mapped_value = mapped_value_or_error.release_value();
  108. } else {
  109. mapped_value = next_value;
  110. }
  111. auto result_or_error = array->create_data_property_or_throw(k, mapped_value);
  112. if (result_or_error.is_error())
  113. return TRY(iterator_close(vm, iterator, result_or_error.release_error()));
  114. ++k;
  115. }
  116. }
  117. auto* array_like = MUST(items.to_object(vm));
  118. auto length = TRY(length_of_array_like(global_object, *array_like));
  119. Object* array;
  120. if (constructor.is_constructor())
  121. array = TRY(JS::construct(global_object, constructor.as_function(), Value(length)));
  122. else
  123. array = TRY(Array::create(realm, length));
  124. for (size_t k = 0; k < length; ++k) {
  125. auto k_value = TRY(array_like->get(k));
  126. Value mapped_value;
  127. if (map_fn)
  128. mapped_value = TRY(JS::call(global_object, *map_fn, this_arg, k_value, Value(k)));
  129. else
  130. mapped_value = k_value;
  131. TRY(array->create_data_property_or_throw(k, mapped_value));
  132. }
  133. TRY(array->set(vm.names.length, Value(length), Object::ShouldThrowExceptions::Yes));
  134. return array;
  135. }
  136. // 23.1.2.2 Array.isArray ( arg ), https://tc39.es/ecma262/#sec-array.isarray
  137. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::is_array)
  138. {
  139. auto value = vm.argument(0);
  140. return Value(TRY(value.is_array(vm)));
  141. }
  142. // 23.1.2.3 Array.of ( ...items ), https://tc39.es/ecma262/#sec-array.of
  143. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::of)
  144. {
  145. auto& realm = *global_object.associated_realm();
  146. auto this_value = vm.this_value();
  147. Object* array;
  148. if (this_value.is_constructor())
  149. array = TRY(JS::construct(global_object, this_value.as_function(), Value(vm.argument_count())));
  150. else
  151. array = TRY(Array::create(realm, vm.argument_count()));
  152. for (size_t k = 0; k < vm.argument_count(); ++k)
  153. TRY(array->create_data_property_or_throw(k, vm.argument(k)));
  154. TRY(array->set(vm.names.length, Value(vm.argument_count()), Object::ShouldThrowExceptions::Yes));
  155. return array;
  156. }
  157. // 23.1.2.5 get Array [ @@species ], https://tc39.es/ecma262/#sec-get-array-@@species
  158. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::symbol_species_getter)
  159. {
  160. return vm.this_value();
  161. }
  162. }