DataViewConstructor.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 <LibJS/Runtime/AbstractOperations.h>
  8. #include <LibJS/Runtime/DataView.h>
  9. #include <LibJS/Runtime/DataViewConstructor.h>
  10. #include <LibJS/Runtime/Error.h>
  11. #include <LibJS/Runtime/GlobalObject.h>
  12. namespace JS {
  13. DataViewConstructor::DataViewConstructor(Realm& realm)
  14. : NativeFunction(realm.vm().names.DataView.as_string(), *realm.intrinsics().function_prototype())
  15. {
  16. }
  17. void DataViewConstructor::initialize(Realm& realm)
  18. {
  19. auto& vm = this->vm();
  20. NativeFunction::initialize(realm);
  21. // 25.3.3.1 DataView.prototype, https://tc39.es/ecma262/#sec-dataview.prototype
  22. define_direct_property(vm.names.prototype, realm.intrinsics().data_view_prototype(), 0);
  23. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  24. }
  25. // 25.3.2.1 DataView ( buffer [ , byteOffset [ , byteLength ] ] ), https://tc39.es/ecma262/#sec-dataview-buffer-byteoffset-bytelength
  26. ThrowCompletionOr<Value> DataViewConstructor::call()
  27. {
  28. auto& vm = this->vm();
  29. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.DataView);
  30. }
  31. // 25.3.2.1 DataView ( buffer [ , byteOffset [ , byteLength ] ] ), https://tc39.es/ecma262/#sec-dataview-buffer-byteoffset-bytelength
  32. ThrowCompletionOr<Object*> DataViewConstructor::construct(FunctionObject& new_target)
  33. {
  34. auto& vm = this->vm();
  35. auto buffer = vm.argument(0);
  36. if (!buffer.is_object() || !is<ArrayBuffer>(buffer.as_object()))
  37. return vm.throw_completion<TypeError>(ErrorType::IsNotAn, buffer.to_string_without_side_effects(), vm.names.ArrayBuffer);
  38. auto& array_buffer = static_cast<ArrayBuffer&>(buffer.as_object());
  39. auto offset = TRY(vm.argument(1).to_index(vm));
  40. if (array_buffer.is_detached())
  41. return vm.throw_completion<TypeError>(ErrorType::DetachedArrayBuffer);
  42. auto buffer_byte_length = array_buffer.byte_length();
  43. if (offset > buffer_byte_length)
  44. return vm.throw_completion<RangeError>(ErrorType::DataViewOutOfRangeByteOffset, offset, buffer_byte_length);
  45. size_t view_byte_length;
  46. if (vm.argument(2).is_undefined()) {
  47. view_byte_length = buffer_byte_length - offset;
  48. } else {
  49. view_byte_length = TRY(vm.argument(2).to_index(vm));
  50. auto const checked_add = AK::make_checked(view_byte_length) + AK::make_checked(offset);
  51. if (checked_add.has_overflow() || checked_add.value() > buffer_byte_length)
  52. return vm.throw_completion<RangeError>(ErrorType::InvalidLength, vm.names.DataView);
  53. }
  54. auto* data_view = TRY(ordinary_create_from_constructor<DataView>(vm, new_target, &Intrinsics::data_view_prototype, &array_buffer, view_byte_length, offset));
  55. if (array_buffer.is_detached())
  56. return vm.throw_completion<TypeError>(ErrorType::DetachedArrayBuffer);
  57. return data_view;
  58. }
  59. }