ArrayConstructor.cpp 6.9 KB

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