ArrayBufferConstructor.cpp 3.3 KB

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