ArrayBufferConstructor.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. ArrayBufferConstructor::ArrayBufferConstructor(Realm& realm)
  15. : NativeFunction(realm.vm().names.ArrayBuffer.as_string(), realm.intrinsics().function_prototype())
  16. {
  17. }
  18. void ArrayBufferConstructor::initialize(Realm& realm)
  19. {
  20. auto& vm = this->vm();
  21. Base::initialize(realm);
  22. // 25.1.4.2 ArrayBuffer.prototype, https://tc39.es/ecma262/#sec-arraybuffer.prototype
  23. define_direct_property(vm.names.prototype, realm.intrinsics().array_buffer_prototype(), 0);
  24. u8 attr = Attribute::Writable | Attribute::Configurable;
  25. define_native_function(realm, 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(realm, 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. // 1. If NewTarget is undefined, throw a TypeError exception.
  35. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.ArrayBuffer);
  36. }
  37. // 25.1.3.1 ArrayBuffer ( length ), https://tc39.es/ecma262/#sec-arraybuffer-length
  38. ThrowCompletionOr<NonnullGCPtr<Object>> ArrayBufferConstructor::construct(FunctionObject& new_target)
  39. {
  40. auto& vm = this->vm();
  41. // 2. Let byteLength be ? ToIndex(length).
  42. auto byte_length_or_error = vm.argument(0).to_index(vm);
  43. if (byte_length_or_error.is_error()) {
  44. auto error = byte_length_or_error.release_error();
  45. if (error.value()->is_object() && is<RangeError>(error.value()->as_object())) {
  46. // Re-throw more specific RangeError
  47. return vm.throw_completion<RangeError>(ErrorType::InvalidLength, "array buffer");
  48. }
  49. return error;
  50. }
  51. // 3. Return ? AllocateArrayBuffer(NewTarget, byteLength).
  52. return *TRY(allocate_array_buffer(vm, new_target, byte_length_or_error.release_value()));
  53. }
  54. // 25.1.4.1 ArrayBuffer.isView ( arg ), https://tc39.es/ecma262/#sec-arraybuffer.isview
  55. JS_DEFINE_NATIVE_FUNCTION(ArrayBufferConstructor::is_view)
  56. {
  57. auto arg = vm.argument(0);
  58. // 1. If arg is not an Object, return false.
  59. if (!arg.is_object())
  60. return Value(false);
  61. // 2. If arg has a [[ViewedArrayBuffer]] internal slot, return true.
  62. if (arg.as_object().is_typed_array())
  63. return Value(true);
  64. if (is<DataView>(arg.as_object()))
  65. return Value(true);
  66. // 3. Return false.
  67. return Value(false);
  68. }
  69. // 25.1.4.3 get ArrayBuffer [ @@species ], https://tc39.es/ecma262/#sec-get-arraybuffer-@@species
  70. JS_DEFINE_NATIVE_FUNCTION(ArrayBufferConstructor::symbol_species_getter)
  71. {
  72. // 1. Return the this value.
  73. return vm.this_value();
  74. }
  75. }