DataViewConstructor.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. ThrowCompletionOr<Value> DataViewConstructor::call()
  29. {
  30. auto& vm = this->vm();
  31. return vm.throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.DataView);
  32. }
  33. // 25.3.2.1 DataView ( buffer [ , byteOffset [ , byteLength ] ] ), https://tc39.es/ecma262/#sec-dataview-buffer-byteoffset-bytelength
  34. ThrowCompletionOr<Object*> DataViewConstructor::construct(FunctionObject& new_target)
  35. {
  36. auto& vm = this->vm();
  37. auto& global_object = this->global_object();
  38. auto buffer = vm.argument(0);
  39. if (!buffer.is_object() || !is<ArrayBuffer>(buffer.as_object()))
  40. return vm.throw_completion<TypeError>(global_object, ErrorType::IsNotAn, buffer.to_string_without_side_effects(), vm.names.ArrayBuffer);
  41. auto& array_buffer = static_cast<ArrayBuffer&>(buffer.as_object());
  42. auto offset = TRY(vm.argument(1).to_index(global_object));
  43. if (array_buffer.is_detached())
  44. return vm.throw_completion<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
  45. auto buffer_byte_length = array_buffer.byte_length();
  46. if (offset > buffer_byte_length)
  47. return vm.throw_completion<RangeError>(global_object, ErrorType::DataViewOutOfRangeByteOffset, offset, buffer_byte_length);
  48. size_t view_byte_length;
  49. if (vm.argument(2).is_undefined()) {
  50. view_byte_length = buffer_byte_length - offset;
  51. } else {
  52. view_byte_length = TRY(vm.argument(2).to_index(global_object));
  53. if (offset + view_byte_length > buffer_byte_length)
  54. return vm.throw_completion<RangeError>(global_object, ErrorType::InvalidLength, vm.names.DataView);
  55. }
  56. auto* data_view = TRY(ordinary_create_from_constructor<DataView>(global_object, new_target, &GlobalObject::data_view_prototype, &array_buffer, view_byte_length, offset));
  57. if (array_buffer.is_detached())
  58. return vm.throw_completion<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
  59. return data_view;
  60. }
  61. }