ArrayConstructor.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. put("prototype", interpreter().global_object().array_prototype(), 0);
  40. put("length", Value(1), Attribute::Configurable);
  41. u8 attr = Attribute::Writable | Attribute::Configurable;
  42. put_native_function("isArray", is_array, 1, attr);
  43. }
  44. ArrayConstructor::~ArrayConstructor()
  45. {
  46. }
  47. Value ArrayConstructor::call(Interpreter& interpreter)
  48. {
  49. if (interpreter.argument_count() <= 0)
  50. return Array::create(interpreter.global_object());
  51. if (interpreter.argument_count() == 1 && interpreter.argument(0).is_number()) {
  52. auto array_length_value = interpreter.argument(0);
  53. if (!array_length_value.is_integer() || array_length_value.to_i32() < 0) {
  54. interpreter.throw_exception<TypeError>("Invalid array length");
  55. return {};
  56. }
  57. auto* array = Array::create(interpreter.global_object());
  58. array->elements().resize(array_length_value.to_i32());
  59. return array;
  60. }
  61. auto* array = Array::create(interpreter.global_object());
  62. for (size_t i = 0; i < interpreter.argument_count(); ++i)
  63. array->elements().append(interpreter.argument(i));
  64. return array;
  65. }
  66. Value ArrayConstructor::construct(Interpreter& interpreter)
  67. {
  68. return call(interpreter);
  69. }
  70. Value ArrayConstructor::is_array(Interpreter& interpreter)
  71. {
  72. auto value = interpreter.argument(0);
  73. if (!value.is_array())
  74. return Value(false);
  75. // Exclude TypedArray and similar
  76. return Value(StringView(value.as_object().class_name()) == "Array");
  77. }
  78. }