/src/serenity/Userland/Libraries/LibWeb/Streams/ReadableStreamBYOBReader.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/PromiseCapability.h> |
9 | | #include <LibJS/Runtime/TypedArray.h> |
10 | | #include <LibWeb/Bindings/Intrinsics.h> |
11 | | #include <LibWeb/Bindings/ReadableStreamBYOBReaderPrototype.h> |
12 | | #include <LibWeb/Streams/AbstractOperations.h> |
13 | | #include <LibWeb/Streams/ReadableStream.h> |
14 | | #include <LibWeb/Streams/ReadableStreamBYOBReader.h> |
15 | | #include <LibWeb/WebIDL/Buffers.h> |
16 | | #include <LibWeb/WebIDL/ExceptionOr.h> |
17 | | |
18 | | namespace Web::Streams { |
19 | | |
20 | | JS_DEFINE_ALLOCATOR(ReadableStreamBYOBReader); |
21 | | |
22 | | ReadableStreamBYOBReader::ReadableStreamBYOBReader(JS::Realm& realm) |
23 | 0 | : Bindings::PlatformObject(realm) |
24 | 0 | , ReadableStreamGenericReaderMixin(realm) |
25 | 0 | { |
26 | 0 | } |
27 | | |
28 | | void ReadableStreamBYOBReader::initialize(JS::Realm& realm) |
29 | 0 | { |
30 | 0 | Base::initialize(realm); |
31 | 0 | WEB_SET_PROTOTYPE_FOR_INTERFACE(ReadableStreamBYOBReader); |
32 | 0 | } |
33 | | |
34 | | // https://streams.spec.whatwg.org/#byob-reader-constructor |
35 | | WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStreamBYOBReader>> ReadableStreamBYOBReader::construct_impl(JS::Realm& realm, JS::NonnullGCPtr<ReadableStream> stream) |
36 | 0 | { |
37 | 0 | auto reader = realm.heap().allocate<ReadableStreamBYOBReader>(realm, realm); |
38 | | |
39 | | // 1. Perform ? SetUpReadableStreamBYOBReader(this, stream). |
40 | 0 | TRY(set_up_readable_stream_byob_reader(reader, *stream)); |
41 | |
|
42 | 0 | return reader; |
43 | 0 | } |
44 | | |
45 | | // https://streams.spec.whatwg.org/#byob-reader-release-lock |
46 | | void ReadableStreamBYOBReader::release_lock() |
47 | 0 | { |
48 | | // 1. If this.[[stream]] is undefined, return. |
49 | 0 | if (!m_stream) |
50 | 0 | return; |
51 | | |
52 | | // 2. Perform ! ReadableStreamBYOBReaderRelease(this). |
53 | 0 | readable_stream_byob_reader_release(*this); |
54 | 0 | } |
55 | | |
56 | | void ReadableStreamBYOBReader::visit_edges(Cell::Visitor& visitor) |
57 | 0 | { |
58 | 0 | Base::visit_edges(visitor); |
59 | 0 | ReadableStreamGenericReaderMixin::visit_edges(visitor); |
60 | 0 | visitor.visit(m_read_into_requests); |
61 | 0 | } |
62 | | |
63 | | class BYOBReaderReadIntoRequest : public ReadIntoRequest { |
64 | | JS_CELL(BYOBReaderReadIntoRequest, ReadIntoRequest); |
65 | | JS_DECLARE_ALLOCATOR(BYOBReaderReadIntoRequest); |
66 | | |
67 | | public: |
68 | | BYOBReaderReadIntoRequest(JS::Realm& realm, WebIDL::Promise& promise) |
69 | 0 | : m_realm(realm) |
70 | 0 | , m_promise(promise) |
71 | 0 | { |
72 | 0 | } |
73 | | |
74 | | // chunk steps, given chunk |
75 | | virtual void on_chunk(JS::Value chunk) override |
76 | 0 | { |
77 | | // 1. Resolve promise with «[ "value" → chunk, "done" → false ]». |
78 | 0 | WebIDL::resolve_promise(m_realm, m_promise, JS::create_iterator_result_object(m_realm->vm(), chunk, false)); |
79 | 0 | } |
80 | | |
81 | | // close steps, given chunk |
82 | | virtual void on_close(JS::Value chunk) override |
83 | 0 | { |
84 | | // 1. Resolve promise with «[ "value" → chunk, "done" → true ]». |
85 | 0 | WebIDL::resolve_promise(m_realm, m_promise, JS::create_iterator_result_object(m_realm->vm(), chunk, true)); |
86 | 0 | } |
87 | | |
88 | | // error steps, given e |
89 | | virtual void on_error(JS::Value error) override |
90 | 0 | { |
91 | | // 1. Reject promise with e. |
92 | 0 | WebIDL::reject_promise(m_realm, m_promise, error); |
93 | 0 | } |
94 | | |
95 | | private: |
96 | | virtual void visit_edges(Visitor& visitor) override |
97 | 0 | { |
98 | 0 | Base::visit_edges(visitor); |
99 | 0 | visitor.visit(m_realm); |
100 | 0 | visitor.visit(m_promise); |
101 | 0 | } |
102 | | |
103 | | JS::NonnullGCPtr<JS::Realm> m_realm; |
104 | | JS::NonnullGCPtr<WebIDL::Promise> m_promise; |
105 | | }; |
106 | | |
107 | | JS_DEFINE_ALLOCATOR(BYOBReaderReadIntoRequest); |
108 | | |
109 | | // https://streams.spec.whatwg.org/#byob-reader-read |
110 | | JS::NonnullGCPtr<JS::Promise> ReadableStreamBYOBReader::read(JS::Handle<WebIDL::ArrayBufferView>& view, ReadableStreamBYOBReaderReadOptions options) |
111 | 0 | { |
112 | 0 | auto& realm = this->realm(); |
113 | | |
114 | | // 1. If view.[[ByteLength]] is 0, return a promise rejected with a TypeError exception. |
115 | 0 | if (view->byte_length() == 0) { |
116 | 0 | WebIDL::SimpleException exception { WebIDL::SimpleExceptionType::TypeError, "Cannot read in an empty buffer"sv }; |
117 | 0 | return WebIDL::create_rejected_promise_from_exception(realm, move(exception)); |
118 | 0 | } |
119 | | |
120 | | // 2. If view.[[ViewedArrayBuffer]].[[ArrayBufferByteLength]] is 0, return a promise rejected with a TypeError exception. |
121 | 0 | if (view->viewed_array_buffer()->byte_length() == 0) { |
122 | 0 | WebIDL::SimpleException exception { WebIDL::SimpleExceptionType::TypeError, "Cannot read in an empty buffer"sv }; |
123 | 0 | return WebIDL::create_rejected_promise_from_exception(realm, move(exception)); |
124 | 0 | } |
125 | | |
126 | | // 3. If ! IsDetachedBuffer(view.[[ViewedArrayBuffer]]) is true, return a promise rejected with a TypeError exception. |
127 | 0 | if (view->viewed_array_buffer()->is_detached()) { |
128 | 0 | WebIDL::SimpleException exception { WebIDL::SimpleExceptionType::TypeError, "Cannot read in a detached buffer"sv }; |
129 | 0 | return WebIDL::create_rejected_promise_from_exception(realm, move(exception)); |
130 | 0 | } |
131 | | |
132 | | // 4. If options["min"] is 0, return a promise rejected with a TypeError exception. |
133 | 0 | if (options.min == 0) { |
134 | 0 | WebIDL::SimpleException exception { WebIDL::SimpleExceptionType::TypeError, "options[\"min\'] cannot have a value of 0."sv }; |
135 | 0 | return WebIDL::create_rejected_promise_from_exception(realm, move(exception)); |
136 | 0 | } |
137 | | |
138 | | // 5. If view has a [[TypedArrayName]] internal slot, |
139 | 0 | if (view->is_typed_array_base()) { |
140 | 0 | auto const& typed_array = *view->bufferable_object().get<JS::NonnullGCPtr<JS::TypedArrayBase>>(); |
141 | | |
142 | | // 1. If options["min"] > view.[[ArrayLength]], return a promise rejected with a RangeError exception. |
143 | 0 | if (options.min > typed_array.array_length().length()) { |
144 | 0 | WebIDL::SimpleException exception { WebIDL::SimpleExceptionType::RangeError, "options[\"min\"] cannot be larger than the length of the view."sv }; |
145 | 0 | return WebIDL::create_rejected_promise_from_exception(realm, move(exception)); |
146 | 0 | } |
147 | 0 | } |
148 | | |
149 | | // 6. Otherwise (i.e., it is a DataView), |
150 | 0 | if (view->is_data_view()) { |
151 | | // 1. If options["min"] > view.[[ByteLength]], return a promise rejected with a RangeError exception. |
152 | 0 | if (options.min > view->byte_length()) { |
153 | 0 | WebIDL::SimpleException exception { WebIDL::SimpleExceptionType::RangeError, "options[\"min\"] cannot be larger than the length of the view."sv }; |
154 | 0 | return WebIDL::create_rejected_promise_from_exception(realm, move(exception)); |
155 | 0 | } |
156 | 0 | } |
157 | | |
158 | | // 7. If this.[[stream]] is undefined, return a promise rejected with a TypeError exception. |
159 | 0 | if (!m_stream) { |
160 | 0 | WebIDL::SimpleException exception { WebIDL::SimpleExceptionType::TypeError, "Cannot read from an empty stream"sv }; |
161 | 0 | return WebIDL::create_rejected_promise_from_exception(realm, move(exception)); |
162 | 0 | } |
163 | | |
164 | | // 8. Let promise be a new promise. |
165 | 0 | auto promise_capability = WebIDL::create_promise(realm); |
166 | | |
167 | | // 9. Let readIntoRequest be a new read-into request with the following items: |
168 | | // chunk steps, given chunk |
169 | | // Resolve promise with «[ "value" → chunk, "done" → false ]». |
170 | | // close steps, given chunk |
171 | | // Resolve promise with «[ "value" → chunk, "done" → true ]». |
172 | | // error steps, given e |
173 | | // Reject promise with e. |
174 | 0 | auto read_into_request = heap().allocate_without_realm<BYOBReaderReadIntoRequest>(realm, promise_capability); |
175 | | |
176 | | // 10. Perform ! ReadableStreamBYOBReaderRead(this, view, options["min"], readIntoRequest). |
177 | 0 | readable_stream_byob_reader_read(*this, *view, options.min, *read_into_request); |
178 | | |
179 | | // 11. Return promise. |
180 | 0 | return JS::NonnullGCPtr { verify_cast<JS::Promise>(*promise_capability->promise()) }; |
181 | 0 | } |
182 | | } |