ArrayConstructor.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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("Array", *global_object.function_prototype())
  38. {
  39. }
  40. ArrayConstructor::~ArrayConstructor()
  41. {
  42. }
  43. void ArrayConstructor::initialize(GlobalObject& global_object)
  44. {
  45. NativeFunction::initialize(global_object);
  46. define_property("prototype", global_object.array_prototype(), 0);
  47. define_property("length", Value(1), Attribute::Configurable);
  48. u8 attr = Attribute::Writable | Attribute::Configurable;
  49. define_native_function("from", from, 1, attr);
  50. define_native_function("isArray", is_array, 1, attr);
  51. define_native_function("of", of, 0, attr);
  52. }
  53. Value ArrayConstructor::call()
  54. {
  55. if (vm().argument_count() <= 0)
  56. return Array::create(global_object());
  57. if (vm().argument_count() == 1 && vm().argument(0).is_number()) {
  58. auto array_length_value = vm().argument(0);
  59. if (!array_length_value.is_integer() || array_length_value.as_i32() < 0) {
  60. vm().throw_exception<TypeError>(global_object(), ErrorType::ArrayInvalidLength);
  61. return {};
  62. }
  63. auto* array = Array::create(global_object());
  64. array->indexed_properties().set_array_like_size(array_length_value.as_i32());
  65. return array;
  66. }
  67. auto* array = Array::create(global_object());
  68. for (size_t i = 0; i < vm().argument_count(); ++i)
  69. array->indexed_properties().append(vm().argument(i));
  70. return array;
  71. }
  72. Value ArrayConstructor::construct(Function&)
  73. {
  74. return call();
  75. }
  76. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from)
  77. {
  78. auto value = vm.argument(0);
  79. auto object = value.to_object(global_object);
  80. if (!object)
  81. return {};
  82. auto* array = Array::create(global_object);
  83. // Array.from() lets you create Arrays from:
  84. if (auto size = object->indexed_properties().array_like_size()) {
  85. // * array-like objects (objects with a length property and indexed elements)
  86. MarkedValueList elements(vm.heap());
  87. elements.ensure_capacity(size);
  88. for (size_t i = 0; i < size; ++i) {
  89. elements.append(object->get(i));
  90. if (vm.exception())
  91. return {};
  92. }
  93. array->set_indexed_property_elements(move(elements));
  94. } else {
  95. // * iterable objects
  96. get_iterator_values(global_object, value, [&](Value element) {
  97. if (vm.exception())
  98. return IterationDecision::Break;
  99. array->indexed_properties().append(element);
  100. return IterationDecision::Continue;
  101. });
  102. if (vm.exception())
  103. return {};
  104. }
  105. // FIXME: if interpreter.argument_count() >= 2: mapFn
  106. // FIXME: if interpreter.argument_count() >= 3: thisArg
  107. return array;
  108. }
  109. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::is_array)
  110. {
  111. auto value = vm.argument(0);
  112. return Value(value.is_array());
  113. }
  114. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::of)
  115. {
  116. auto* array = Array::create(global_object);
  117. for (size_t i = 0; i < vm.argument_count(); ++i)
  118. array->indexed_properties().append(vm.argument(i));
  119. return array;
  120. }
  121. }