ArrayConstructor.cpp 5.0 KB

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