ReadableStreamBYOBRequest.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. WebIDL::ExceptionOr<void> ReadableStreamBYOBRequest::respond(u64 bytes_written)
  35. {
  36. // 1. If this.[[controller]] is undefined, throw a TypeError exception.
  37. if (!m_controller)
  38. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Controller is undefined"_string };
  39. // 2. If ! IsDetachedBuffer(this.[[view]].[[ArrayBuffer]]) is true, throw a TypeError exception.
  40. if (m_view->viewed_array_buffer()->is_detached())
  41. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Unable to respond to detached ArrayBuffer"_string };
  42. // 3. Assert: this.[[view]].[[ByteLength]] > 0.
  43. VERIFY(m_view->viewed_array_buffer()->byte_length() > 0);
  44. // 4. Assert: this.[[view]].[[ViewedArrayBuffer]].[[ByteLength]] > 0.
  45. VERIFY(m_view->viewed_array_buffer()->byte_length() > 0);
  46. // 5. Perform ? ReadableByteStreamControllerRespond(this.[[controller]], bytesWritten).
  47. return readable_byte_stream_controller_respond(*m_controller, bytes_written);
  48. }
  49. }