DataViewConstructor.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/DataView.h>
  8. #include <LibJS/Runtime/DataViewConstructor.h>
  9. #include <LibJS/Runtime/Error.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. namespace JS {
  12. DataViewConstructor::DataViewConstructor(GlobalObject& global_object)
  13. : NativeFunction(vm().names.DataView.as_string(), *global_object.function_prototype())
  14. {
  15. }
  16. void DataViewConstructor::initialize(GlobalObject& global_object)
  17. {
  18. auto& vm = this->vm();
  19. NativeFunction::initialize(global_object);
  20. // 25.3.3.1 DataView.prototype, https://tc39.es/ecma262/#sec-dataview.prototype
  21. define_direct_property(vm.names.prototype, global_object.data_view_prototype(), 0);
  22. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  23. }
  24. DataViewConstructor::~DataViewConstructor()
  25. {
  26. }
  27. // 25.3.2.1 DataView ( buffer [ , byteOffset [ , byteLength ] ] ), https://tc39.es/ecma262/#sec-dataview-buffer-byteoffset-bytelength
  28. Value DataViewConstructor::call()
  29. {
  30. auto& vm = this->vm();
  31. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.DataView);
  32. return {};
  33. }
  34. // 25.3.2.1 DataView ( buffer [ , byteOffset [ , byteLength ] ] ), https://tc39.es/ecma262/#sec-dataview-buffer-byteoffset-bytelength
  35. Value DataViewConstructor::construct(FunctionObject& new_target)
  36. {
  37. auto& vm = this->vm();
  38. auto& global_object = this->global_object();
  39. auto buffer = vm.argument(0);
  40. if (!buffer.is_object() || !is<ArrayBuffer>(buffer.as_object())) {
  41. vm.throw_exception<TypeError>(global_object, ErrorType::IsNotAn, buffer.to_string_without_side_effects(), vm.names.ArrayBuffer);
  42. return {};
  43. }
  44. auto& array_buffer = static_cast<ArrayBuffer&>(buffer.as_object());
  45. auto offset = vm.argument(1).to_index(global_object);
  46. if (vm.exception())
  47. return {};
  48. if (array_buffer.is_detached()) {
  49. vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
  50. return {};
  51. }
  52. auto buffer_byte_length = array_buffer.byte_length();
  53. if (offset > buffer_byte_length) {
  54. vm.throw_exception<RangeError>(global_object, ErrorType::DataViewOutOfRangeByteOffset, offset, buffer_byte_length);
  55. return {};
  56. }
  57. size_t view_byte_length;
  58. if (vm.argument(2).is_undefined()) {
  59. view_byte_length = buffer_byte_length - offset;
  60. } else {
  61. view_byte_length = vm.argument(2).to_index(global_object);
  62. if (vm.exception())
  63. return {};
  64. if (offset + view_byte_length > buffer_byte_length) {
  65. vm.throw_exception<RangeError>(global_object, ErrorType::InvalidLength, vm.names.DataView);
  66. return {};
  67. }
  68. }
  69. auto* data_view = TRY_OR_DISCARD(ordinary_create_from_constructor<DataView>(global_object, new_target, &GlobalObject::data_view_prototype, &array_buffer, view_byte_length, offset));
  70. if (array_buffer.is_detached()) {
  71. vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
  72. return {};
  73. }
  74. return data_view;
  75. }
  76. }