Array.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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)
  14. {
  15. // FIXME: Support proto parameter
  16. if (length > NumericLimits<u32>::max()) {
  17. auto& vm = global_object.vm();
  18. vm.throw_exception<RangeError>(global_object, ErrorType::InvalidLength, "array");
  19. return nullptr;
  20. }
  21. auto* array = global_object.heap().allocate<Array>(global_object, *global_object.array_prototype());
  22. array->indexed_properties().set_array_like_size(length);
  23. return array;
  24. }
  25. // 7.3.17 CreateArrayFromList ( elements ), https://tc39.es/ecma262/#sec-createarrayfromlist
  26. Array* Array::create_from(GlobalObject& global_object, const Vector<Value>& elements)
  27. {
  28. auto* array = Array::create(global_object);
  29. for (size_t i = 0; i < elements.size(); ++i)
  30. array->define_property(i, elements[i]);
  31. return array;
  32. }
  33. Array::Array(Object& prototype)
  34. : Object(prototype)
  35. {
  36. }
  37. void Array::initialize(GlobalObject& global_object)
  38. {
  39. auto& vm = this->vm();
  40. Object::initialize(global_object);
  41. define_native_property(vm.names.length, length_getter, length_setter, Attribute::Writable);
  42. }
  43. Array::~Array()
  44. {
  45. }
  46. Array* Array::typed_this(VM& vm, GlobalObject& global_object)
  47. {
  48. auto* this_object = vm.this_value(global_object).to_object(global_object);
  49. if (!this_object)
  50. return {};
  51. if (!this_object->is_array()) {
  52. vm.throw_exception<TypeError>(global_object, ErrorType::NotAn, "Array");
  53. return nullptr;
  54. }
  55. return static_cast<Array*>(this_object);
  56. }
  57. JS_DEFINE_NATIVE_GETTER(Array::length_getter)
  58. {
  59. auto* array = typed_this(vm, global_object);
  60. if (!array)
  61. return {};
  62. return Value(array->indexed_properties().array_like_size());
  63. }
  64. JS_DEFINE_NATIVE_SETTER(Array::length_setter)
  65. {
  66. auto* array = typed_this(vm, global_object);
  67. if (!array)
  68. return;
  69. auto length = value.to_number(global_object);
  70. if (vm.exception())
  71. return;
  72. if (length.is_nan() || length.is_infinity() || length.as_double() < 0) {
  73. vm.throw_exception<RangeError>(global_object, ErrorType::InvalidLength, "array");
  74. return;
  75. }
  76. array->indexed_properties().set_array_like_size(length.as_double());
  77. }
  78. }