1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::cell::RefCell;
use std::collections::VecDeque;
use std::rc::Rc;
use dom_struct::dom_struct;
use js::jsapi::Heap;
use js::rust::{HandleValue as SafeHandleValue, HandleValue};
use crate::dom::bindings::codegen::Bindings::ReadableStreamDefaultControllerBinding::{
ReadableStreamDefaultControllerMethods, ValueWithSize,
};
use crate::dom::bindings::import::module::UnionTypes::ReadableStreamDefaultControllerOrReadableByteStreamController as Controller;
use crate::dom::bindings::import::module::{Error, Fallible};
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
use crate::dom::globalscope::GlobalScope;
use crate::dom::promisenativehandler::{Callback, PromiseNativeHandler};
use crate::dom::readablestream::ReadableStream;
use crate::dom::readablestreamdefaultreader::ReadRequest;
use crate::dom::underlyingsourcecontainer::{UnderlyingSourceContainer, UnderlyingSourceType};
use crate::realms::{enter_realm, InRealm};
use crate::script_runtime::{JSContext, JSContext as SafeJSContext};
/// The fulfillment handler for
/// <https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed>
#[derive(Clone, JSTraceable, MallocSizeOf)]
#[allow(crown::unrooted_must_root)]
struct PullAlgorithmFulfillmentHandler {
controller: Dom<ReadableStreamDefaultController>,
}
impl Callback for PullAlgorithmFulfillmentHandler {
/// Handle fufillment of pull algo promise.
fn callback(&self, _cx: JSContext, _v: HandleValue, _realm: InRealm) {
todo!();
}
}
/// The rejection handler for
/// <https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed>
#[derive(Clone, JSTraceable, MallocSizeOf)]
#[allow(crown::unrooted_must_root)]
struct PullAlgorithmRejectionHandler {
controller: Dom<ReadableStreamDefaultController>,
}
impl Callback for PullAlgorithmRejectionHandler {
/// Handle rejection of pull algo promise.
fn callback(&self, _cx: JSContext, _v: HandleValue, _realm: InRealm) {
todo!();
}
}
/// <https://streams.spec.whatwg.org/#value-with-size>
#[derive(JSTraceable)]
#[allow(crown::unrooted_must_root)]
pub enum EnqueuedValue {
/// A value enqueued from Rust.
Native(Vec<u8>),
/// A Js value.
Js(ValueWithSize),
}
/// <https://streams.spec.whatwg.org/#queue-with-sizes>
#[derive(Default, JSTraceable, MallocSizeOf)]
pub struct QueueWithSizes {
total_size: usize,
#[ignore_malloc_size_of = "Rc is hard"]
queue: VecDeque<EnqueuedValue>,
}
impl QueueWithSizes {
/// <https://streams.spec.whatwg.org/#dequeue-value>
fn dequeue_value(&mut self) -> EnqueuedValue {
self.queue
.pop_front()
.expect("Buffer cannot be empty when dequeue value is called into.")
}
/// <https://streams.spec.whatwg.org/#enqueue-value-with-size>
fn enqueue_value_with_size(&mut self, value: EnqueuedValue) {
self.queue.push_back(value);
}
fn is_empty(&self) -> bool {
self.queue.is_empty()
}
/// Only used with native sources.
fn get_in_memory_bytes(&self) -> Vec<u8> {
self.queue
.iter()
.flat_map(|value| {
let EnqueuedValue::Native(chunk) = value else {
unreachable!(
"`get_in_memory_bytes` can only be called on a queue with native values."
)
};
chunk.clone()
})
.collect()
}
}
/// <https://streams.spec.whatwg.org/#readablestreamdefaultcontroller>
#[dom_struct]
pub struct ReadableStreamDefaultController {
reflector_: Reflector,
/// <https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-queue>
queue: RefCell<QueueWithSizes>,
underlying_source: Dom<UnderlyingSourceContainer>,
stream: MutNullableDom<ReadableStream>,
}
impl ReadableStreamDefaultController {
fn new_inherited(
global: &GlobalScope,
underlying_source_type: UnderlyingSourceType,
) -> ReadableStreamDefaultController {
ReadableStreamDefaultController {
reflector_: Reflector::new(),
queue: RefCell::new(Default::default()),
stream: MutNullableDom::new(None),
underlying_source: Dom::from_ref(&*UnderlyingSourceContainer::new(
global,
underlying_source_type,
)),
}
}
pub fn new(
global: &GlobalScope,
underlying_source: UnderlyingSourceType,
) -> DomRoot<ReadableStreamDefaultController> {
reflect_dom_object(
Box::new(ReadableStreamDefaultController::new_inherited(
global,
underlying_source,
)),
global,
)
}
pub fn set_stream(&self, stream: &ReadableStream) {
self.stream.set(Some(stream))
}
/// <https://streams.spec.whatwg.org/#dequeue-value>
fn dequeue_value(&self) -> EnqueuedValue {
let mut queue = self.queue.borrow_mut();
queue.dequeue_value()
}
/// <https://streams.spec.whatwg.org/#readable-stream-default-controller-should-call-pull>
fn should_pull(&self) -> bool {
// TODO: implement the algo.
true
}
/// <https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed>
fn call_pull_if_needed(&self) {
if !self.should_pull() {
return;
}
let global = self.global();
let controller = Controller::ReadableStreamDefaultController(DomRoot::from_ref(self));
if let Some(promise) = self.underlying_source.call_pull_algorithm(controller) {
let fulfillment_handler = Box::new(PullAlgorithmFulfillmentHandler {
controller: Dom::from_ref(self),
});
let rejection_handler = Box::new(PullAlgorithmRejectionHandler {
controller: Dom::from_ref(self),
});
let handler = PromiseNativeHandler::new(
&global,
Some(fulfillment_handler),
Some(rejection_handler),
);
let realm = enter_realm(&*global);
let comp = InRealm::Entered(&realm);
promise.append_native_handler(&handler, comp);
}
}
/// <https://streams.spec.whatwg.org/#ref-for-abstract-opdef-readablestreamcontroller-pullsteps>
pub fn perform_pull_steps(&self, read_request: ReadRequest) {
// if queue contains bytes, perform chunk steps.
if !self.queue.borrow().is_empty() {
// TODO: use <https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-strategysizealgorithm>
let chunk = self.dequeue_value();
// TODO: handle close requested.
self.call_pull_if_needed();
if let EnqueuedValue::Native(chunk) = chunk {
read_request.chunk_steps(chunk);
}
}
// else, append read request to reader.
self.stream
.get()
.expect("Controller must have a stream when pull steps are called into.")
.add_read_request(read_request);
self.call_pull_if_needed();
}
/// Native call to
/// <https://streams.spec.whatwg.org/#readable-stream-default-controller-enqueue>
pub fn enqueue_native(&self, chunk: Vec<u8>) {
let stream = self
.stream
.get()
.expect("Controller must have a stream when a chunk is enqueued.");
if stream.is_locked() && stream.get_num_read_requests() > 0 {
stream.fulfill_read_request(chunk, false);
return;
}
// TODO: strategy size algo.
// <https://streams.spec.whatwg.org/#enqueue-value-with-size>
let mut queue = self.queue.borrow_mut();
queue.enqueue_value_with_size(EnqueuedValue::Native(chunk));
self.call_pull_if_needed();
}
/// Does the stream have all data in memory?
pub fn in_memory(&self) -> bool {
self.underlying_source.in_memory()
}
/// Return bytes synchronously if the stream has all data in memory.
pub fn get_in_memory_bytes(&self) -> Option<Vec<u8>> {
if self.underlying_source.in_memory() {
return Some(self.queue.borrow().get_in_memory_bytes());
}
None
}
/// <https://streams.spec.whatwg.org/#readable-stream-default-controller-close>
pub fn close(&self) {
todo!()
}
/// <https://streams.spec.whatwg.org/#ref-for-readable-stream-error>
pub fn error(&self) {
todo!()
}
}
impl ReadableStreamDefaultControllerMethods for ReadableStreamDefaultController {
/// <https://streams.spec.whatwg.org/#rs-default-controller-desired-size>
fn GetDesiredSize(&self) -> Option<f64> {
// TODO
None
}
/// <https://streams.spec.whatwg.org/#rs-default-controller-close>
fn Close(&self) -> Fallible<()> {
// TODO
Err(Error::NotFound)
}
/// <https://streams.spec.whatwg.org/#rs-default-controller-enqueue>
fn Enqueue(&self, _cx: SafeJSContext, _chunk: SafeHandleValue) -> Fallible<()> {
// TODO
Err(Error::NotFound)
}
/// <https://streams.spec.whatwg.org/#rs-default-controller-error>
fn Error(&self, _cx: SafeJSContext, _e: SafeHandleValue) -> Fallible<()> {
// TODO
Err(Error::NotFound)
}
}
|