ArrayBufferConstructor.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/ArrayBuffer.h>
  8. #include <LibJS/Runtime/ArrayBufferConstructor.h>
  9. #include <LibJS/Runtime/DataView.h>
  10. #include <LibJS/Runtime/Error.h>
  11. #include <LibJS/Runtime/GlobalObject.h>
  12. #include <LibJS/Runtime/TypedArray.h>
  13. namespace JS {
  14. ArrayBufferConstructor::ArrayBufferConstructor(GlobalObject& global_object)
  15. : NativeFunction(vm().names.ArrayBuffer.as_string(), *global_object.function_prototype())
  16. {
  17. }
  18. void ArrayBufferConstructor::initialize(GlobalObject& global_object)
  19. {
  20. auto& vm = this->vm();
  21. NativeFunction::initialize(global_object);
  22. // 25.1.4.2 ArrayBuffer.prototype, https://tc39.es/ecma262/#sec-arraybuffer.prototype
  23. define_direct_property(vm.names.prototype, global_object.array_buffer_prototype(), 0);
  24. u8 attr = Attribute::Writable | Attribute::Configurable;
  25. define_native_function(vm.names.isView, is_view, 1, attr);
  26. // 25.1.5.4 ArrayBuffer.prototype [ @@toStringTag ], https://tc39.es/ecma262/#sec-arraybuffer.prototype-@@tostringtag
  27. define_native_accessor(*vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
  28. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  29. }
  30. ArrayBufferConstructor::~ArrayBufferConstructor()
  31. {
  32. }
  33. // 25.1.3.1 ArrayBuffer ( length ), https://tc39.es/ecma262/#sec-arraybuffer-length
  34. ThrowCompletionOr<Value> ArrayBufferConstructor::call()
  35. {
  36. auto& vm = this->vm();
  37. return vm.throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.ArrayBuffer);
  38. }
  39. // 1.1.6 GetArrayBufferMaxByteLengthOption ( options ), https://tc39.es/proposal-resizablearraybuffer/#sec-getarraybuffermaxbytelengthoption
  40. static ThrowCompletionOr<Optional<size_t>> get_array_buffer_max_byte_length_option(VM& vm, GlobalObject& global_object, Value options)
  41. {
  42. // 1. If Type(options) is not Object, return empty.
  43. if (!options.is_object())
  44. return Optional<size_t>();
  45. // 2. Let maxByteLength be ? Get(options, "maxByteLength").
  46. auto max_byte_length = TRY(options.get(global_object, vm.names.maxByteLength));
  47. // 3. If maxByteLength is undefined, return empty.
  48. if (max_byte_length.is_undefined())
  49. return Optional<size_t>();
  50. // 4. Return ? ToIndex(maxByteLength).
  51. return TRY(max_byte_length.to_index(global_object));
  52. }
  53. // 25.1.3.1 ArrayBuffer ( length ), https://tc39.es/ecma262/#sec-arraybuffer-length
  54. // 1.2.1 ArrayBuffer ( length [, options ] ), https://tc39.es/proposal-resizablearraybuffer/#sec-arraybuffer-constructor
  55. ThrowCompletionOr<Object*> ArrayBufferConstructor::construct(FunctionObject& new_target)
  56. {
  57. auto& global_object = new_target.realm()->global_object();
  58. auto& vm = this->vm();
  59. // 1. If NewTarget is undefined, throw a TypeError exception.
  60. // NOTE: See `ArrayBufferConstructor::call()`
  61. // 2. Let byteLength be ? ToIndex(length).
  62. auto byte_length_or_error = vm.argument(0).to_index(global_object);
  63. if (byte_length_or_error.is_error()) {
  64. auto error = byte_length_or_error.release_error();
  65. if (error.value()->is_object() && is<RangeError>(error.value()->as_object())) {
  66. // Re-throw more specific RangeError
  67. return vm.throw_completion<RangeError>(global_object, ErrorType::InvalidLength, "array buffer");
  68. }
  69. return error;
  70. }
  71. auto byte_length = byte_length_or_error.release_value();
  72. // 3. Let requestedMaxByteLength be ? GetArrayBufferMaxByteLengthOption(options).
  73. auto options = vm.argument(1);
  74. auto requested_max_byte_length_value = TRY(get_array_buffer_max_byte_length_option(vm, global_object, options));
  75. // 4. If requestedMaxByteLength is empty, then
  76. if (!requested_max_byte_length_value.has_value()) {
  77. // a. Return ? AllocateArrayBuffer(NewTarget, byteLength).
  78. return TRY(allocate_array_buffer(global_object, new_target, byte_length));
  79. }
  80. auto requested_max_byte_length = requested_max_byte_length_value.release_value();
  81. // 5. If byteLength > requestedMaxByteLength, throw a RangeError exception.
  82. if (byte_length > requested_max_byte_length)
  83. return vm.throw_completion<RangeError>(global_object, ErrorType::ByteLengthBeyondRequestedMax);
  84. // 6. Return ? AllocateArrayBuffer(NewTarget, byteLength, requestedMaxByteLength).
  85. return TRY(allocate_array_buffer(global_object, new_target, byte_length, requested_max_byte_length));
  86. }
  87. // 25.1.4.1 ArrayBuffer.isView ( arg ), https://tc39.es/ecma262/#sec-arraybuffer.isview
  88. JS_DEFINE_NATIVE_FUNCTION(ArrayBufferConstructor::is_view)
  89. {
  90. auto arg = vm.argument(0);
  91. if (!arg.is_object())
  92. return Value(false);
  93. if (arg.as_object().is_typed_array())
  94. return Value(true);
  95. if (is<DataView>(arg.as_object()))
  96. return Value(true);
  97. return Value(false);
  98. }
  99. // 25.1.4.3 get ArrayBuffer [ @@species ], https://tc39.es/ecma262/#sec-get-arraybuffer-@@species
  100. JS_DEFINE_NATIVE_FUNCTION(ArrayBufferConstructor::symbol_species_getter)
  101. {
  102. return vm.this_value(global_object);
  103. }
  104. }