ArrayBufferConstructor.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TypeCasts.h>
  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. GC_DEFINE_ALLOCATOR(ArrayBufferConstructor);
  15. ArrayBufferConstructor::ArrayBufferConstructor(Realm& realm)
  16. : NativeFunction(realm.vm().names.ArrayBuffer.as_string(), realm.intrinsics().function_prototype())
  17. {
  18. }
  19. void ArrayBufferConstructor::initialize(Realm& realm)
  20. {
  21. auto& vm = this->vm();
  22. Base::initialize(realm);
  23. // 25.1.5.2 ArrayBuffer.prototype, https://tc39.es/ecma262/#sec-arraybuffer.prototype
  24. define_direct_property(vm.names.prototype, realm.intrinsics().array_buffer_prototype(), 0);
  25. u8 attr = Attribute::Writable | Attribute::Configurable;
  26. define_native_function(realm, vm.names.isView, is_view, 1, attr);
  27. // 25.1.6.7 ArrayBuffer.prototype [ @@toStringTag ], https://tc39.es/ecma262/#sec-arraybuffer.prototype-@@tostringtag
  28. define_native_accessor(realm, vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
  29. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  30. }
  31. // 25.1.4.1 ArrayBuffer ( length [ , options ] ), https://tc39.es/ecma262/#sec-arraybuffer-length
  32. ThrowCompletionOr<Value> ArrayBufferConstructor::call()
  33. {
  34. auto& vm = this->vm();
  35. // 1. If NewTarget is undefined, throw a TypeError exception.
  36. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.ArrayBuffer);
  37. }
  38. // 25.1.4.1 ArrayBuffer ( length [ , options ] ), https://tc39.es/ecma262/#sec-arraybuffer-length
  39. ThrowCompletionOr<GC::Ref<Object>> ArrayBufferConstructor::construct(FunctionObject& new_target)
  40. {
  41. auto& vm = this->vm();
  42. auto length = vm.argument(0);
  43. auto options = vm.argument(1);
  44. // 2. Let byteLength be ? ToIndex(length).
  45. auto byte_length_or_error = length.to_index(vm);
  46. if (byte_length_or_error.is_error()) {
  47. auto error = byte_length_or_error.release_error();
  48. if (error.value()->is_object() && is<RangeError>(error.value()->as_object())) {
  49. // Re-throw more specific RangeError
  50. return vm.throw_completion<RangeError>(ErrorType::InvalidLength, "array buffer");
  51. }
  52. return error;
  53. }
  54. // 3. Let requestedMaxByteLength be ? GetArrayBufferMaxByteLengthOption(options).
  55. auto requested_max_byte_length = TRY(get_array_buffer_max_byte_length_option(vm, options));
  56. // 3. Return ? AllocateArrayBuffer(NewTarget, byteLength, requestedMaxByteLength).
  57. return *TRY(allocate_array_buffer(vm, new_target, byte_length_or_error.release_value(), requested_max_byte_length));
  58. }
  59. // 25.1.5.1 ArrayBuffer.isView ( arg ), https://tc39.es/ecma262/#sec-arraybuffer.isview
  60. JS_DEFINE_NATIVE_FUNCTION(ArrayBufferConstructor::is_view)
  61. {
  62. auto arg = vm.argument(0);
  63. // 1. If arg is not an Object, return false.
  64. if (!arg.is_object())
  65. return Value(false);
  66. // 2. If arg has a [[ViewedArrayBuffer]] internal slot, return true.
  67. if (arg.as_object().is_typed_array())
  68. return Value(true);
  69. if (is<DataView>(arg.as_object()))
  70. return Value(true);
  71. // 3. Return false.
  72. return Value(false);
  73. }
  74. // 25.1.5.3 get ArrayBuffer [ @@species ], https://tc39.es/ecma262/#sec-get-arraybuffer-@@species
  75. JS_DEFINE_NATIVE_FUNCTION(ArrayBufferConstructor::symbol_species_getter)
  76. {
  77. // 1. Return the this value.
  78. return vm.this_value();
  79. }
  80. }