ArrayBufferConstructor.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // 25.1.3.1 ArrayBuffer ( length ), https://tc39.es/ecma262/#sec-arraybuffer-length
  31. ThrowCompletionOr<Value> ArrayBufferConstructor::call()
  32. {
  33. auto& vm = this->vm();
  34. return vm.throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.ArrayBuffer);
  35. }
  36. // 1.1.6 GetArrayBufferMaxByteLengthOption ( options ), https://tc39.es/proposal-resizablearraybuffer/#sec-getarraybuffermaxbytelengthoption
  37. static ThrowCompletionOr<Optional<size_t>> get_array_buffer_max_byte_length_option(VM& vm, GlobalObject& global_object, Value options)
  38. {
  39. // 1. If Type(options) is not Object, return empty.
  40. if (!options.is_object())
  41. return Optional<size_t>();
  42. // 2. Let maxByteLength be ? Get(options, "maxByteLength").
  43. auto max_byte_length = TRY(options.get(global_object, vm.names.maxByteLength));
  44. // 3. If maxByteLength is undefined, return empty.
  45. if (max_byte_length.is_undefined())
  46. return Optional<size_t>();
  47. // 4. Return ? ToIndex(maxByteLength).
  48. return TRY(max_byte_length.to_index(global_object));
  49. }
  50. // 25.1.3.1 ArrayBuffer ( length ), https://tc39.es/ecma262/#sec-arraybuffer-length
  51. // 1.2.1 ArrayBuffer ( length [, options ] ), https://tc39.es/proposal-resizablearraybuffer/#sec-arraybuffer-constructor
  52. ThrowCompletionOr<Object*> ArrayBufferConstructor::construct(FunctionObject& new_target)
  53. {
  54. auto& global_object = this->global_object();
  55. auto& vm = this->vm();
  56. // 1. If NewTarget is undefined, throw a TypeError exception.
  57. // NOTE: See `ArrayBufferConstructor::call()`
  58. // 2. Let byteLength be ? ToIndex(length).
  59. auto byte_length_or_error = vm.argument(0).to_index(global_object);
  60. if (byte_length_or_error.is_error()) {
  61. auto error = byte_length_or_error.release_error();
  62. if (error.value()->is_object() && is<RangeError>(error.value()->as_object())) {
  63. // Re-throw more specific RangeError
  64. return vm.throw_completion<RangeError>(global_object, ErrorType::InvalidLength, "array buffer");
  65. }
  66. return error;
  67. }
  68. auto byte_length = byte_length_or_error.release_value();
  69. // 3. Let requestedMaxByteLength be ? GetArrayBufferMaxByteLengthOption(options).
  70. auto options = vm.argument(1);
  71. auto requested_max_byte_length_value = TRY(get_array_buffer_max_byte_length_option(vm, global_object, options));
  72. // 4. If requestedMaxByteLength is empty, then
  73. if (!requested_max_byte_length_value.has_value()) {
  74. // a. Return ? AllocateArrayBuffer(NewTarget, byteLength).
  75. return TRY(allocate_array_buffer(global_object, new_target, byte_length));
  76. }
  77. auto requested_max_byte_length = requested_max_byte_length_value.release_value();
  78. // 5. If byteLength > requestedMaxByteLength, throw a RangeError exception.
  79. if (byte_length > requested_max_byte_length)
  80. return vm.throw_completion<RangeError>(global_object, ErrorType::ByteLengthBeyondRequestedMax);
  81. // 6. Return ? AllocateArrayBuffer(NewTarget, byteLength, requestedMaxByteLength).
  82. return TRY(allocate_array_buffer(global_object, new_target, byte_length, requested_max_byte_length));
  83. }
  84. // 25.1.4.1 ArrayBuffer.isView ( arg ), https://tc39.es/ecma262/#sec-arraybuffer.isview
  85. JS_DEFINE_NATIVE_FUNCTION(ArrayBufferConstructor::is_view)
  86. {
  87. auto arg = vm.argument(0);
  88. if (!arg.is_object())
  89. return Value(false);
  90. if (arg.as_object().is_typed_array())
  91. return Value(true);
  92. if (is<DataView>(arg.as_object()))
  93. return Value(true);
  94. return Value(false);
  95. }
  96. // 25.1.4.3 get ArrayBuffer [ @@species ], https://tc39.es/ecma262/#sec-get-arraybuffer-@@species
  97. JS_DEFINE_NATIVE_FUNCTION(ArrayBufferConstructor::symbol_species_getter)
  98. {
  99. return vm.this_value(global_object);
  100. }
  101. }