ReadableStreamBYOBRequest.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/TypedArray.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/Streams/ReadableByteStreamController.h>
  10. #include <LibWeb/Streams/ReadableStreamBYOBRequest.h>
  11. #include <LibWeb/WebIDL/Buffers.h>
  12. namespace Web::Streams {
  13. JS_DEFINE_ALLOCATOR(ReadableStreamBYOBRequest);
  14. // https://streams.spec.whatwg.org/#rs-byob-request-view
  15. JS::GCPtr<WebIDL::ArrayBufferView> ReadableStreamBYOBRequest::view()
  16. {
  17. // 1. Return this.[[view]].
  18. return m_view;
  19. }
  20. ReadableStreamBYOBRequest::ReadableStreamBYOBRequest(JS::Realm& realm)
  21. : Bindings::PlatformObject(realm)
  22. {
  23. }
  24. void ReadableStreamBYOBRequest::initialize(JS::Realm& realm)
  25. {
  26. Base::initialize(realm);
  27. set_prototype(&Bindings::ensure_web_prototype<Bindings::ReadableStreamBYOBRequestPrototype>(realm, "ReadableStreamBYOBRequest"_fly_string));
  28. }
  29. void ReadableStreamBYOBRequest::visit_edges(Cell::Visitor& visitor)
  30. {
  31. Base::visit_edges(visitor);
  32. visitor.visit(m_controller);
  33. visitor.visit(m_view);
  34. }
  35. // https://streams.spec.whatwg.org/#rs-byob-request-respond
  36. WebIDL::ExceptionOr<void> ReadableStreamBYOBRequest::respond(WebIDL::UnsignedLongLong bytes_written)
  37. {
  38. // 1. If this.[[controller]] is undefined, throw a TypeError exception.
  39. if (!m_controller)
  40. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Controller is undefined"_string };
  41. // 2. If ! IsDetachedBuffer(this.[[view]].[[ArrayBuffer]]) is true, throw a TypeError exception.
  42. if (m_view->viewed_array_buffer()->is_detached())
  43. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Unable to respond to detached ArrayBuffer"_string };
  44. // 3. Assert: this.[[view]].[[ByteLength]] > 0.
  45. VERIFY(m_view->viewed_array_buffer()->byte_length() > 0);
  46. // 4. Assert: this.[[view]].[[ViewedArrayBuffer]].[[ByteLength]] > 0.
  47. VERIFY(m_view->viewed_array_buffer()->byte_length() > 0);
  48. // 5. Perform ? ReadableByteStreamControllerRespond(this.[[controller]], bytesWritten).
  49. return readable_byte_stream_controller_respond(*m_controller, bytes_written);
  50. }
  51. // https://streams.spec.whatwg.org/#rs-byob-request-respond-with-new-view
  52. WebIDL::ExceptionOr<void> ReadableStreamBYOBRequest::respond_with_new_view(JS::Handle<WebIDL::ArrayBufferView> const& view)
  53. {
  54. auto& realm = this->realm();
  55. // 1. If this.[[controller]] is undefined, throw a TypeError exception.
  56. if (!m_controller)
  57. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Controller is undefined"_string };
  58. // 2. If ! IsDetachedBuffer(view.[[ViewedArrayBuffer]]) is true, throw a TypeError exception.
  59. if (view->viewed_array_buffer()->is_detached())
  60. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Unable to respond with a detached ArrayBuffer"_string };
  61. // 3. Return ? ReadableByteStreamControllerRespondWithNewView(this.[[controller]], view).
  62. return TRY(readable_byte_stream_controller_respond_with_new_view(realm, *m_controller, *view));
  63. }
  64. }