ArrayConstructor.cpp 4.6 KB

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