ReadableStreamBYOBRequest.cpp 3.1 KB

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