ArrayConstructor.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/Heap/Heap.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_property(vm.names.prototype, global_object.array_prototype(), 0);
  29. define_property(vm.names.length, Value(1), Attribute::Configurable);
  30. u8 attr = Attribute::Writable | Attribute::Configurable;
  31. define_native_function(vm.names.from, from, 1, attr);
  32. define_native_function(vm.names.isArray, is_array, 1, attr);
  33. define_native_function(vm.names.of, of, 0, attr);
  34. // 23.1.2.5 get Array [ @@species ], https://tc39.es/ecma262/#sec-get-array-@@species
  35. define_native_accessor(*vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
  36. }
  37. // 23.1.1.1 Array ( ...values ), https://tc39.es/ecma262/#sec-array
  38. Value ArrayConstructor::call()
  39. {
  40. if (vm().argument_count() <= 0)
  41. return Array::create(global_object());
  42. if (vm().argument_count() == 1 && vm().argument(0).is_number()) {
  43. auto length = vm().argument(0);
  44. auto int_length = length.to_u32(global_object());
  45. if (int_length != length.as_double()) {
  46. vm().throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "array");
  47. return {};
  48. }
  49. auto* array = Array::create(global_object());
  50. array->indexed_properties().set_array_like_size(int_length);
  51. return array;
  52. }
  53. auto* array = Array::create(global_object());
  54. for (size_t i = 0; i < vm().argument_count(); ++i)
  55. array->indexed_properties().append(vm().argument(i));
  56. return array;
  57. }
  58. // 23.1.1.1 Array ( ...values ), https://tc39.es/ecma262/#sec-array
  59. Value ArrayConstructor::construct(FunctionObject&)
  60. {
  61. return call();
  62. }
  63. // 23.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] ), https://tc39.es/ecma262/#sec-array.from
  64. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from)
  65. {
  66. auto value = vm.argument(0);
  67. auto object = value.to_object(global_object);
  68. if (!object)
  69. return {};
  70. auto* array = Array::create(global_object);
  71. FunctionObject* map_fn = nullptr;
  72. if (!vm.argument(1).is_undefined()) {
  73. auto callback = vm.argument(1);
  74. if (!callback.is_function()) {
  75. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, callback.to_string_without_side_effects());
  76. return {};
  77. }
  78. map_fn = &callback.as_function();
  79. }
  80. auto this_arg = vm.argument(2);
  81. // Array.from() lets you create Arrays from:
  82. if (auto size = object->indexed_properties().array_like_size()) {
  83. // * array-like objects (objects with a length property and indexed elements)
  84. MarkedValueList elements(vm.heap());
  85. elements.ensure_capacity(size);
  86. for (size_t i = 0; i < size; ++i) {
  87. if (map_fn) {
  88. auto element = object->get(i);
  89. if (vm.exception())
  90. return {};
  91. auto map_fn_result = vm.call(*map_fn, this_arg, element, Value((i32)i));
  92. if (vm.exception())
  93. return {};
  94. elements.append(map_fn_result);
  95. } else {
  96. elements.append(object->get(i));
  97. if (vm.exception())
  98. return {};
  99. }
  100. }
  101. array->set_indexed_property_elements(move(elements));
  102. } else {
  103. // * iterable objects
  104. i32 i = 0;
  105. get_iterator_values(global_object, value, [&](Value element) {
  106. if (vm.exception())
  107. return IterationDecision::Break;
  108. if (map_fn) {
  109. auto map_fn_result = vm.call(*map_fn, this_arg, element, Value(i));
  110. i++;
  111. if (vm.exception())
  112. return IterationDecision::Break;
  113. array->indexed_properties().append(map_fn_result);
  114. } else {
  115. array->indexed_properties().append(element);
  116. }
  117. return IterationDecision::Continue;
  118. });
  119. if (vm.exception())
  120. return {};
  121. }
  122. return array;
  123. }
  124. // 23.1.2.2 Array.isArray ( arg ), https://tc39.es/ecma262/#sec-array.isarray
  125. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::is_array)
  126. {
  127. auto value = vm.argument(0);
  128. return Value(value.is_array(global_object));
  129. }
  130. // 23.1.2.3 Array.of ( ...items ), https://tc39.es/ecma262/#sec-array.of
  131. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::of)
  132. {
  133. auto this_value = vm.this_value(global_object);
  134. Value array;
  135. if (this_value.is_constructor()) {
  136. MarkedValueList arguments(vm.heap());
  137. arguments.empend(vm.argument_count());
  138. array = vm.construct(this_value.as_function(), this_value.as_function(), move(arguments));
  139. if (vm.exception())
  140. return {};
  141. } else {
  142. array = Array::create(global_object);
  143. }
  144. auto& array_object = array.as_object();
  145. for (size_t k = 0; k < vm.argument_count(); ++k) {
  146. array_object.define_property(k, vm.argument(k));
  147. if (vm.exception())
  148. return {};
  149. }
  150. array_object.put(vm.names.length, Value(vm.argument_count()));
  151. if (vm.exception())
  152. return {};
  153. return array;
  154. }
  155. // 23.1.2.5 get Array [ @@species ], https://tc39.es/ecma262/#sec-get-array-@@species
  156. JS_DEFINE_NATIVE_GETTER(ArrayConstructor::symbol_species_getter)
  157. {
  158. return vm.this_value(global_object);
  159. }
  160. }