ArrayConstructor.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. define_property(vm.names.prototype, global_object.array_prototype(), 0);
  28. define_property(vm.names.length, Value(1), Attribute::Configurable);
  29. u8 attr = Attribute::Writable | Attribute::Configurable;
  30. define_native_function(vm.names.from, from, 1, attr);
  31. define_native_function(vm.names.isArray, is_array, 1, attr);
  32. define_native_function(vm.names.of, of, 0, attr);
  33. define_native_property(vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
  34. }
  35. Value ArrayConstructor::call()
  36. {
  37. if (vm().argument_count() <= 0)
  38. return Array::create(global_object());
  39. if (vm().argument_count() == 1 && vm().argument(0).is_number()) {
  40. auto array_length_value = vm().argument(0);
  41. if (!array_length_value.is_integer() || array_length_value.as_i32() < 0) {
  42. vm().throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "array");
  43. return {};
  44. }
  45. auto* array = Array::create(global_object());
  46. array->indexed_properties().set_array_like_size(array_length_value.as_i32());
  47. return array;
  48. }
  49. auto* array = Array::create(global_object());
  50. for (size_t i = 0; i < vm().argument_count(); ++i)
  51. array->indexed_properties().append(vm().argument(i));
  52. return array;
  53. }
  54. Value ArrayConstructor::construct(Function&)
  55. {
  56. return call();
  57. }
  58. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from)
  59. {
  60. auto value = vm.argument(0);
  61. auto object = value.to_object(global_object);
  62. if (!object)
  63. return {};
  64. auto* array = Array::create(global_object);
  65. Function* map_fn = nullptr;
  66. if (!vm.argument(1).is_undefined()) {
  67. auto callback = vm.argument(1);
  68. if (!callback.is_function()) {
  69. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, callback.to_string_without_side_effects());
  70. return {};
  71. }
  72. map_fn = &callback.as_function();
  73. }
  74. auto this_arg = vm.argument(2);
  75. // Array.from() lets you create Arrays from:
  76. if (auto size = object->indexed_properties().array_like_size()) {
  77. // * array-like objects (objects with a length property and indexed elements)
  78. MarkedValueList elements(vm.heap());
  79. elements.ensure_capacity(size);
  80. for (size_t i = 0; i < size; ++i) {
  81. if (map_fn) {
  82. auto element = object->get(i);
  83. if (vm.exception())
  84. return {};
  85. auto map_fn_result = vm.call(*map_fn, this_arg, element, Value((i32)i));
  86. if (vm.exception())
  87. return {};
  88. elements.append(map_fn_result);
  89. } else {
  90. elements.append(object->get(i));
  91. if (vm.exception())
  92. return {};
  93. }
  94. }
  95. array->set_indexed_property_elements(move(elements));
  96. } else {
  97. // * iterable objects
  98. i32 i = 0;
  99. get_iterator_values(global_object, value, [&](Value element) {
  100. if (vm.exception())
  101. return IterationDecision::Break;
  102. if (map_fn) {
  103. auto map_fn_result = vm.call(*map_fn, this_arg, element, Value(i));
  104. i++;
  105. if (vm.exception())
  106. return IterationDecision::Break;
  107. array->indexed_properties().append(map_fn_result);
  108. } else {
  109. array->indexed_properties().append(element);
  110. }
  111. return IterationDecision::Continue;
  112. });
  113. if (vm.exception())
  114. return {};
  115. }
  116. return array;
  117. }
  118. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::is_array)
  119. {
  120. auto value = vm.argument(0);
  121. return Value(value.is_array(global_object));
  122. }
  123. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::of)
  124. {
  125. auto* array = Array::create(global_object);
  126. for (size_t i = 0; i < vm.argument_count(); ++i)
  127. array->indexed_properties().append(vm.argument(i));
  128. return array;
  129. }
  130. JS_DEFINE_NATIVE_GETTER(ArrayConstructor::symbol_species_getter)
  131. {
  132. return vm.this_value(global_object);
  133. }
  134. }