ArrayConstructor.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/Function.h>
  28. #include <LibJS/Heap/Heap.h>
  29. #include <LibJS/Runtime/Array.h>
  30. #include <LibJS/Runtime/ArrayConstructor.h>
  31. #include <LibJS/Runtime/Error.h>
  32. #include <LibJS/Runtime/GlobalObject.h>
  33. #include <LibJS/Runtime/IteratorOperations.h>
  34. #include <LibJS/Runtime/Shape.h>
  35. namespace JS {
  36. ArrayConstructor::ArrayConstructor(GlobalObject& global_object)
  37. : NativeFunction(vm().names.Array, *global_object.function_prototype())
  38. {
  39. }
  40. ArrayConstructor::~ArrayConstructor()
  41. {
  42. }
  43. void ArrayConstructor::initialize(GlobalObject& global_object)
  44. {
  45. auto& vm = this->vm();
  46. NativeFunction::initialize(global_object);
  47. define_property(vm.names.prototype, global_object.array_prototype(), 0);
  48. define_property(vm.names.length, Value(1), Attribute::Configurable);
  49. u8 attr = Attribute::Writable | Attribute::Configurable;
  50. define_native_function(vm.names.from, from, 1, attr);
  51. define_native_function(vm.names.isArray, is_array, 1, attr);
  52. define_native_function(vm.names.of, of, 0, attr);
  53. }
  54. Value ArrayConstructor::call()
  55. {
  56. if (vm().argument_count() <= 0)
  57. return Array::create(global_object());
  58. if (vm().argument_count() == 1 && vm().argument(0).is_number()) {
  59. auto array_length_value = vm().argument(0);
  60. if (!array_length_value.is_integer() || array_length_value.as_i32() < 0) {
  61. vm().throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "array");
  62. return {};
  63. }
  64. auto* array = Array::create(global_object());
  65. array->indexed_properties().set_array_like_size(array_length_value.as_i32());
  66. return array;
  67. }
  68. auto* array = Array::create(global_object());
  69. for (size_t i = 0; i < vm().argument_count(); ++i)
  70. array->indexed_properties().append(vm().argument(i));
  71. return array;
  72. }
  73. Value ArrayConstructor::construct(Function&)
  74. {
  75. return call();
  76. }
  77. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from)
  78. {
  79. auto value = vm.argument(0);
  80. auto object = value.to_object(global_object);
  81. if (!object)
  82. return {};
  83. auto* array = Array::create(global_object);
  84. Function* map_fn = nullptr;
  85. if (!vm.argument(1).is_undefined()) {
  86. auto callback = vm.argument(1);
  87. if (!callback.is_function()) {
  88. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, callback.to_string_without_side_effects());
  89. return {};
  90. }
  91. map_fn = &callback.as_function();
  92. }
  93. auto this_arg = vm.argument(2);
  94. // Array.from() lets you create Arrays from:
  95. if (auto size = object->indexed_properties().array_like_size()) {
  96. // * array-like objects (objects with a length property and indexed elements)
  97. MarkedValueList elements(vm.heap());
  98. elements.ensure_capacity(size);
  99. for (size_t i = 0; i < size; ++i) {
  100. if (map_fn) {
  101. auto element = object->get(i);
  102. if (vm.exception())
  103. return {};
  104. auto map_fn_result = vm.call(*map_fn, this_arg, element, Value((i32)i));
  105. if (vm.exception())
  106. return {};
  107. elements.append(map_fn_result);
  108. } else {
  109. elements.append(object->get(i));
  110. if (vm.exception())
  111. return {};
  112. }
  113. }
  114. array->set_indexed_property_elements(move(elements));
  115. } else {
  116. // * iterable objects
  117. i32 i = 0;
  118. get_iterator_values(global_object, value, [&](Value element) {
  119. if (vm.exception())
  120. return IterationDecision::Break;
  121. if (map_fn) {
  122. auto map_fn_result = vm.call(*map_fn, this_arg, element, Value(i));
  123. i++;
  124. if (vm.exception())
  125. return IterationDecision::Break;
  126. array->indexed_properties().append(map_fn_result);
  127. } else {
  128. array->indexed_properties().append(element);
  129. }
  130. return IterationDecision::Continue;
  131. });
  132. if (vm.exception())
  133. return {};
  134. }
  135. return array;
  136. }
  137. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::is_array)
  138. {
  139. auto value = vm.argument(0);
  140. return Value(value.is_array());
  141. }
  142. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::of)
  143. {
  144. auto* array = Array::create(global_object);
  145. for (size_t i = 0; i < vm.argument_count(); ++i)
  146. array->indexed_properties().append(vm.argument(i));
  147. return array;
  148. }
  149. }