ArrayConstructor.cpp 7.1 KB

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