Array.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Function.h>
  8. #include <LibJS/Runtime/Array.h>
  9. #include <LibJS/Runtime/Error.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. namespace JS {
  12. // 10.4.2.2 ArrayCreate ( length [ , proto ] ), https://tc39.es/ecma262/#sec-arraycreate
  13. Array* Array::create(GlobalObject& global_object, size_t length, Object* prototype)
  14. {
  15. auto& vm = global_object.vm();
  16. if (length > NumericLimits<u32>::max()) {
  17. vm.throw_exception<RangeError>(global_object, ErrorType::InvalidLength, "array");
  18. return nullptr;
  19. }
  20. if (!prototype)
  21. prototype = global_object.array_prototype();
  22. auto* array = global_object.heap().allocate<Array>(global_object, *prototype);
  23. array->put(vm.names.length, Value(length));
  24. return array;
  25. }
  26. // 7.3.17 CreateArrayFromList ( elements ), https://tc39.es/ecma262/#sec-createarrayfromlist
  27. Array* Array::create_from(GlobalObject& global_object, const Vector<Value>& elements)
  28. {
  29. auto* array = Array::create(global_object, 0);
  30. for (size_t i = 0; i < elements.size(); ++i)
  31. array->define_property(i, elements[i]);
  32. return array;
  33. }
  34. Array::Array(Object& prototype)
  35. : Object(prototype)
  36. {
  37. }
  38. void Array::initialize(GlobalObject& global_object)
  39. {
  40. auto& vm = this->vm();
  41. Object::initialize(global_object);
  42. define_native_property(vm.names.length, length_getter, length_setter, Attribute::Writable);
  43. }
  44. Array::~Array()
  45. {
  46. }
  47. Array* Array::typed_this(VM& vm, GlobalObject& global_object)
  48. {
  49. auto* this_object = vm.this_value(global_object).to_object(global_object);
  50. if (!this_object)
  51. return {};
  52. if (!this_object->is_array()) {
  53. vm.throw_exception<TypeError>(global_object, ErrorType::NotAn, "Array");
  54. return nullptr;
  55. }
  56. return static_cast<Array*>(this_object);
  57. }
  58. JS_DEFINE_NATIVE_GETTER(Array::length_getter)
  59. {
  60. auto* this_object = vm.this_value(global_object).to_object(global_object);
  61. if (!this_object)
  62. return {};
  63. // TODO: could be incorrect if receiver/this_value is fixed or changed
  64. if (!this_object->is_array()) {
  65. Value val = this_object->get_own_property(vm.names.length.to_string_or_symbol(), this_object);
  66. if (vm.exception())
  67. return {};
  68. return val;
  69. }
  70. return Value(this_object->indexed_properties().array_like_size());
  71. }
  72. JS_DEFINE_NATIVE_SETTER(Array::length_setter)
  73. {
  74. auto* this_object = vm.this_value(global_object).to_object(global_object);
  75. if (!this_object)
  76. return;
  77. // TODO: could be incorrect if receiver/this_value is fixed or changed
  78. if (!this_object->is_array()) {
  79. this_object->define_property(vm.names.length.to_string_or_symbol(), value, default_attributes);
  80. if (vm.exception())
  81. return;
  82. return;
  83. }
  84. auto length = value.to_number(global_object);
  85. if (vm.exception())
  86. return;
  87. u32 val = length.as_double();
  88. if (val != length.as_double()
  89. || length.is_nan()
  90. || length.is_infinity()
  91. || length.as_double() < 0
  92. || length.as_double() > NumericLimits<u32>::max()) {
  93. vm.throw_exception<RangeError>(global_object, ErrorType::InvalidLength, "array");
  94. return;
  95. }
  96. this_object->indexed_properties().set_array_like_size(val);
  97. }
  98. }