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