DataViewConstructor.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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(vm().names.DataView.as_string(), *realm.global_object().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.global_object().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>(global_object(), 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& global_object = this->global_object();
  36. auto buffer = vm.argument(0);
  37. if (!buffer.is_object() || !is<ArrayBuffer>(buffer.as_object()))
  38. return vm.throw_completion<TypeError>(global_object, ErrorType::IsNotAn, buffer.to_string_without_side_effects(), vm.names.ArrayBuffer);
  39. auto& array_buffer = static_cast<ArrayBuffer&>(buffer.as_object());
  40. auto offset = TRY(vm.argument(1).to_index(global_object));
  41. if (array_buffer.is_detached())
  42. return vm.throw_completion<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
  43. auto buffer_byte_length = array_buffer.byte_length();
  44. if (offset > buffer_byte_length)
  45. return vm.throw_completion<RangeError>(global_object, ErrorType::DataViewOutOfRangeByteOffset, offset, buffer_byte_length);
  46. size_t view_byte_length;
  47. if (vm.argument(2).is_undefined()) {
  48. view_byte_length = buffer_byte_length - offset;
  49. } else {
  50. view_byte_length = TRY(vm.argument(2).to_index(global_object));
  51. auto const checked_add = AK::make_checked(view_byte_length) + AK::make_checked(offset);
  52. if (checked_add.has_overflow() || checked_add.value() > buffer_byte_length)
  53. return vm.throw_completion<RangeError>(global_object, ErrorType::InvalidLength, vm.names.DataView);
  54. }
  55. auto* data_view = TRY(ordinary_create_from_constructor<DataView>(global_object, new_target, &GlobalObject::data_view_prototype, &array_buffer, view_byte_length, offset));
  56. if (array_buffer.is_detached())
  57. return vm.throw_completion<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
  58. return data_view;
  59. }
  60. }