ArrayConstructor.cpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/Shape.h>
  35. namespace JS {
  36. ArrayConstructor::ArrayConstructor()
  37. : NativeFunction("Array", *interpreter().global_object().function_prototype())
  38. {
  39. define_property("prototype", interpreter().global_object().array_prototype(), 0);
  40. define_property("length", Value(1), Attribute::Configurable);
  41. u8 attr = Attribute::Writable | Attribute::Configurable;
  42. define_native_function("isArray", is_array, 1, attr);
  43. define_native_function("of", of, 0, attr);
  44. }
  45. ArrayConstructor::~ArrayConstructor()
  46. {
  47. }
  48. Value ArrayConstructor::call(Interpreter& interpreter)
  49. {
  50. if (interpreter.argument_count() <= 0)
  51. return Array::create(interpreter.global_object());
  52. if (interpreter.argument_count() == 1 && interpreter.argument(0).is_number()) {
  53. auto array_length_value = interpreter.argument(0);
  54. if (!array_length_value.is_integer() || array_length_value.as_i32() < 0) {
  55. interpreter.throw_exception<TypeError>(ErrorType::ArrayInvalidLength);
  56. return {};
  57. }
  58. auto* array = Array::create(interpreter.global_object());
  59. array->indexed_properties().set_array_like_size(array_length_value.as_i32());
  60. return array;
  61. }
  62. auto* array = Array::create(interpreter.global_object());
  63. for (size_t i = 0; i < interpreter.argument_count(); ++i)
  64. array->indexed_properties().append(interpreter.argument(i));
  65. return array;
  66. }
  67. Value ArrayConstructor::construct(Interpreter& interpreter)
  68. {
  69. return call(interpreter);
  70. }
  71. Value ArrayConstructor::is_array(Interpreter& interpreter)
  72. {
  73. auto value = interpreter.argument(0);
  74. if (!value.is_array())
  75. return Value(false);
  76. // Exclude TypedArray and similar
  77. return Value(StringView(value.as_object().class_name()) == "Array");
  78. }
  79. Value ArrayConstructor::of(Interpreter& interpreter)
  80. {
  81. auto* array = Array::create(interpreter.global_object());
  82. for (size_t i = 0; i < interpreter.argument_count(); ++i)
  83. array->indexed_properties().append(interpreter.argument(i));
  84. return array;
  85. }
  86. }