DataViewConstructor.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Checked.h>
  7. #include <AK/TypeCasts.h>
  8. #include <LibJS/Runtime/AbstractOperations.h>
  9. #include <LibJS/Runtime/DataView.h>
  10. #include <LibJS/Runtime/DataViewConstructor.h>
  11. #include <LibJS/Runtime/Error.h>
  12. #include <LibJS/Runtime/GlobalObject.h>
  13. namespace JS {
  14. DataViewConstructor::DataViewConstructor(Realm& realm)
  15. : NativeFunction(realm.vm().names.DataView.as_string(), realm.intrinsics().function_prototype())
  16. {
  17. }
  18. ThrowCompletionOr<void> DataViewConstructor::initialize(Realm& realm)
  19. {
  20. auto& vm = this->vm();
  21. MUST_OR_THROW_OOM(NativeFunction::initialize(realm));
  22. // 25.3.3.1 DataView.prototype, https://tc39.es/ecma262/#sec-dataview.prototype
  23. define_direct_property(vm.names.prototype, realm.intrinsics().data_view_prototype(), 0);
  24. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  25. return {};
  26. }
  27. // 25.3.2.1 DataView ( buffer [ , byteOffset [ , byteLength ] ] ), https://tc39.es/ecma262/#sec-dataview-buffer-byteoffset-bytelength
  28. ThrowCompletionOr<Value> DataViewConstructor::call()
  29. {
  30. auto& vm = this->vm();
  31. // 1. If NewTarget is undefined, throw a TypeError exception.
  32. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.DataView);
  33. }
  34. // 25.3.2.1 DataView ( buffer [ , byteOffset [ , byteLength ] ] ), https://tc39.es/ecma262/#sec-dataview-buffer-byteoffset-bytelength
  35. ThrowCompletionOr<NonnullGCPtr<Object>> DataViewConstructor::construct(FunctionObject& new_target)
  36. {
  37. auto& vm = this->vm();
  38. auto buffer = vm.argument(0);
  39. auto byte_offset = vm.argument(1);
  40. auto byte_length = vm.argument(2);
  41. // 2. Perform ? RequireInternalSlot(buffer, [[ArrayBufferData]]).
  42. if (!buffer.is_object() || !is<ArrayBuffer>(buffer.as_object()))
  43. return vm.throw_completion<TypeError>(ErrorType::IsNotAn, TRY_OR_THROW_OOM(vm, buffer.to_string_without_side_effects()), vm.names.ArrayBuffer);
  44. auto& array_buffer = static_cast<ArrayBuffer&>(buffer.as_object());
  45. // 3. Let offset be ? ToIndex(byteOffset).
  46. auto offset = TRY(byte_offset.to_index(vm));
  47. // 4. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
  48. if (array_buffer.is_detached())
  49. return vm.throw_completion<TypeError>(ErrorType::DetachedArrayBuffer);
  50. // 5. Let bufferByteLength be buffer.[[ArrayBufferByteLength]].
  51. auto buffer_byte_length = array_buffer.byte_length();
  52. // 6. If offset > bufferByteLength, throw a RangeError exception.
  53. if (offset > buffer_byte_length)
  54. return vm.throw_completion<RangeError>(ErrorType::DataViewOutOfRangeByteOffset, offset, buffer_byte_length);
  55. size_t view_byte_length;
  56. // 7. If byteLength is undefined, then
  57. if (byte_length.is_undefined()) {
  58. // a. Let viewByteLength be bufferByteLength - offset.
  59. view_byte_length = buffer_byte_length - offset;
  60. }
  61. // 8. Else,
  62. else {
  63. // a. Let viewByteLength be ? ToIndex(byteLength).
  64. view_byte_length = TRY(byte_length.to_index(vm));
  65. // b. If offset + viewByteLength > bufferByteLength, throw a RangeError exception.
  66. auto const checked_add = AK::make_checked(view_byte_length) + AK::make_checked(offset);
  67. if (checked_add.has_overflow() || checked_add.value() > buffer_byte_length)
  68. return vm.throw_completion<RangeError>(ErrorType::InvalidLength, vm.names.DataView);
  69. }
  70. // 9. Let O be ? OrdinaryCreateFromConstructor(NewTarget, "%DataView.prototype%", « [[DataView]], [[ViewedArrayBuffer]], [[ByteLength]], [[ByteOffset]] »).
  71. // 11. Set O.[[ViewedArrayBuffer]] to buffer.
  72. // 12. Set O.[[ByteLength]] to viewByteLength.
  73. // 13. Set O.[[ByteOffset]] to offset.
  74. auto data_view = TRY(ordinary_create_from_constructor<DataView>(vm, new_target, &Intrinsics::data_view_prototype, &array_buffer, view_byte_length, offset));
  75. // 10. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
  76. if (array_buffer.is_detached())
  77. return vm.throw_completion<TypeError>(ErrorType::DetachedArrayBuffer);
  78. // 14. Return O.
  79. return data_view;
  80. }
  81. }