ArrayConstructor.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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(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("isArray", is_array, 1, attr);
  50. define_native_function("of", of, 0, attr);
  51. }
  52. Value ArrayConstructor::call(Interpreter& interpreter)
  53. {
  54. if (interpreter.argument_count() <= 0)
  55. return Array::create(global_object());
  56. if (interpreter.argument_count() == 1 && interpreter.argument(0).is_number()) {
  57. auto array_length_value = interpreter.argument(0);
  58. if (!array_length_value.is_integer() || array_length_value.as_i32() < 0) {
  59. interpreter.throw_exception<TypeError>(ErrorType::ArrayInvalidLength);
  60. return {};
  61. }
  62. auto* array = Array::create(global_object());
  63. array->indexed_properties().set_array_like_size(array_length_value.as_i32());
  64. return array;
  65. }
  66. auto* array = Array::create(global_object());
  67. for (size_t i = 0; i < interpreter.argument_count(); ++i)
  68. array->indexed_properties().append(interpreter.argument(i));
  69. return array;
  70. }
  71. Value ArrayConstructor::construct(Interpreter& interpreter, Function&)
  72. {
  73. return call(interpreter);
  74. }
  75. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::is_array)
  76. {
  77. auto value = interpreter.argument(0);
  78. return Value(value.is_array());
  79. }
  80. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::of)
  81. {
  82. auto* array = Array::create(global_object);
  83. for (size_t i = 0; i < interpreter.argument_count(); ++i)
  84. array->indexed_properties().append(interpreter.argument(i));
  85. return array;
  86. }
  87. }