ArrayConstructor.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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, *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 array_length_value = vm().argument(0);
  44. if (!array_length_value.is_integral_number() || array_length_value.as_i32() < 0) {
  45. vm().throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "array");
  46. return {};
  47. }
  48. auto* array = Array::create(global_object());
  49. array->indexed_properties().set_array_like_size(array_length_value.as_i32());
  50. return array;
  51. }
  52. auto* array = Array::create(global_object());
  53. for (size_t i = 0; i < vm().argument_count(); ++i)
  54. array->indexed_properties().append(vm().argument(i));
  55. return array;
  56. }
  57. // 23.1.1.1 Array ( ...values ), https://tc39.es/ecma262/#sec-array
  58. Value ArrayConstructor::construct(Function&)
  59. {
  60. return call();
  61. }
  62. // 23.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] ), https://tc39.es/ecma262/#sec-array.from
  63. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from)
  64. {
  65. auto value = vm.argument(0);
  66. auto object = value.to_object(global_object);
  67. if (!object)
  68. return {};
  69. auto* array = Array::create(global_object);
  70. Function* map_fn = nullptr;
  71. if (!vm.argument(1).is_undefined()) {
  72. auto callback = vm.argument(1);
  73. if (!callback.is_function()) {
  74. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, callback.to_string_without_side_effects());
  75. return {};
  76. }
  77. map_fn = &callback.as_function();
  78. }
  79. auto this_arg = vm.argument(2);
  80. // Array.from() lets you create Arrays from:
  81. if (auto size = object->indexed_properties().array_like_size()) {
  82. // * array-like objects (objects with a length property and indexed elements)
  83. MarkedValueList elements(vm.heap());
  84. elements.ensure_capacity(size);
  85. for (size_t i = 0; i < size; ++i) {
  86. if (map_fn) {
  87. auto element = object->get(i);
  88. if (vm.exception())
  89. return {};
  90. auto map_fn_result = vm.call(*map_fn, this_arg, element, Value((i32)i));
  91. if (vm.exception())
  92. return {};
  93. elements.append(map_fn_result);
  94. } else {
  95. elements.append(object->get(i));
  96. if (vm.exception())
  97. return {};
  98. }
  99. }
  100. array->set_indexed_property_elements(move(elements));
  101. } else {
  102. // * iterable objects
  103. i32 i = 0;
  104. get_iterator_values(global_object, value, [&](Value element) {
  105. if (vm.exception())
  106. return IterationDecision::Break;
  107. if (map_fn) {
  108. auto map_fn_result = vm.call(*map_fn, this_arg, element, Value(i));
  109. i++;
  110. if (vm.exception())
  111. return IterationDecision::Break;
  112. array->indexed_properties().append(map_fn_result);
  113. } else {
  114. array->indexed_properties().append(element);
  115. }
  116. return IterationDecision::Continue;
  117. });
  118. if (vm.exception())
  119. return {};
  120. }
  121. return array;
  122. }
  123. // 23.1.2.2 Array.isArray ( arg ), https://tc39.es/ecma262/#sec-array.isarray
  124. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::is_array)
  125. {
  126. auto value = vm.argument(0);
  127. return Value(value.is_array(global_object));
  128. }
  129. // 23.1.2.3 Array.of ( ...items ), https://tc39.es/ecma262/#sec-array.of
  130. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::of)
  131. {
  132. auto* array = Array::create(global_object);
  133. for (size_t i = 0; i < vm.argument_count(); ++i)
  134. array->indexed_properties().append(vm.argument(i));
  135. return array;
  136. }
  137. // 23.1.2.5 get Array [ @@species ], https://tc39.es/ecma262/#sec-get-array-@@species
  138. JS_DEFINE_NATIVE_GETTER(ArrayConstructor::symbol_species_getter)
  139. {
  140. return vm.this_value(global_object);
  141. }
  142. }