DataViewConstructor.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. JS_DEFINE_ALLOCATOR(DataViewConstructor);
  15. DataViewConstructor::DataViewConstructor(Realm& realm)
  16. : NativeFunction(realm.vm().names.DataView.as_string(), realm.intrinsics().function_prototype())
  17. {
  18. }
  19. void DataViewConstructor::initialize(Realm& realm)
  20. {
  21. auto& vm = this->vm();
  22. Base::initialize(realm);
  23. // 25.3.3.1 DataView.prototype, https://tc39.es/ecma262/#sec-dataview.prototype
  24. define_direct_property(vm.names.prototype, realm.intrinsics().data_view_prototype(), 0);
  25. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  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, 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 ArrayBufferByteLength(buffer, seq-cst).
  51. auto buffer_byte_length = array_buffer_byte_length(array_buffer, ArrayBuffer::Order::SeqCst);
  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. // 7. Let bufferIsFixedLength be IsFixedLengthArrayBuffer(buffer).
  56. auto buffer_is_fixed_length = array_buffer.is_fixed_length();
  57. ByteLength view_byte_length { 0 };
  58. // 8. If byteLength is undefined, then
  59. if (byte_length.is_undefined()) {
  60. // a. If bufferIsFixedLength is true, then
  61. if (buffer_is_fixed_length) {
  62. // i. Let viewByteLength be bufferByteLength - offset.
  63. view_byte_length = buffer_byte_length - offset;
  64. }
  65. // b. Else,
  66. else {
  67. // i. Let viewByteLength be auto.
  68. view_byte_length = ByteLength::auto_();
  69. }
  70. }
  71. // 9. Else,
  72. else {
  73. // a. Let viewByteLength be ? ToIndex(byteLength).
  74. view_byte_length = TRY(byte_length.to_index(vm));
  75. // b. If offset + viewByteLength > bufferByteLength, throw a RangeError exception.
  76. auto checked_add = AK::make_checked(offset) + AK::make_checked(static_cast<size_t>(view_byte_length.length()));
  77. if (checked_add.has_overflow() || checked_add.value() > buffer_byte_length)
  78. return vm.throw_completion<RangeError>(ErrorType::InvalidLength, vm.names.DataView);
  79. }
  80. // 10. Let O be ? OrdinaryCreateFromConstructor(NewTarget, "%DataView.prototype%", « [[DataView]], [[ViewedArrayBuffer]], [[ByteLength]], [[ByteOffset]] »).
  81. auto data_view = TRY(ordinary_create_from_constructor<DataView>(vm, new_target, &Intrinsics::data_view_prototype, &array_buffer, move(view_byte_length), offset));
  82. // 11. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
  83. if (array_buffer.is_detached())
  84. return vm.throw_completion<TypeError>(ErrorType::DetachedArrayBuffer);
  85. // 12. Set bufferByteLength to ArrayBufferByteLength(buffer, seq-cst).
  86. buffer_byte_length = array_buffer_byte_length(array_buffer, ArrayBuffer::Order::SeqCst);
  87. // 13. If offset > bufferByteLength, throw a RangeError exception.
  88. if (offset > buffer_byte_length)
  89. return vm.throw_completion<RangeError>(ErrorType::DataViewOutOfRangeByteOffset, offset, buffer_byte_length);
  90. // 14. If byteLength is not undefined, then
  91. if (!byte_length.is_undefined()) {
  92. // a. If offset + viewByteLength > bufferByteLength, throw a RangeError exception.
  93. auto checked_add = AK::make_checked(offset) + AK::make_checked(static_cast<size_t>(view_byte_length.length()));
  94. if (checked_add.has_overflow() || checked_add.value() > buffer_byte_length)
  95. return vm.throw_completion<RangeError>(ErrorType::InvalidLength, vm.names.DataView);
  96. }
  97. // 15. Set O.[[ViewedArrayBuffer]] to buffer.
  98. // 16. Set O.[[ByteLength]] to viewByteLength.
  99. // 17. Set O.[[ByteOffset]] to offset.
  100. // 18. Return O.
  101. return data_view;
  102. }
  103. }