Skip to content

Commit b5e44ee

Browse files
committed
Add ReadableByteStreamControllerShouldCallPull
1 parent e9e99e0 commit b5e44ee

File tree

4 files changed

+72
-42
lines changed

4 files changed

+72
-42
lines changed

components/script/dom/readablebytestreamcontroller.rs

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,51 @@ use js::jsval::{JSVal, UndefinedValue};
1313
use js::rust::{HandleObject as SafeHandleObject, HandleValue as SafeHandleValue};
1414

1515
use crate::dom::bindings::cell::DomRefCell;
16-
use crate::dom::bindings::codegen::Bindings::QueuingStrategyBinding::QueuingStrategySize;
1716
use crate::dom::bindings::codegen::Bindings::ReadableByteStreamControllerBinding::ReadableByteStreamControllerMethods;
1817
use crate::dom::bindings::codegen::Bindings::UnderlyingSourceBinding::{
1918
ReadableStreamController, UnderlyingSource,
2019
};
2120
use crate::dom::bindings::import::module::{Error, ExceptionHandling, Fallible, InRealm};
2221
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
23-
use crate::dom::bindings::root::DomRoot;
22+
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
2423
use crate::dom::globalscope::GlobalScope;
2524
use crate::dom::promise::Promise;
2625
use crate::dom::promisenativehandler::{Callback, PromiseNativeHandler};
2726
use crate::dom::readablestream::{ReadableStream, StreamState};
27+
use crate::dom::readablestreambyobrequest::ReadableStreamBYOBRequest;
2828
use crate::realms::enter_realm;
2929
use crate::script_runtime::JSContext as SafeJSContext;
3030

3131
/// <https://streams.spec.whatwg.org/#rs-default-controller-class-definition>
3232
#[dom_struct]
3333
pub struct ReadableByteStreamController {
3434
reflector_: Reflector,
35+
/// A positive integer, when the automatic buffer allocation feature is enabled. In that case, this value specifies the size of buffer to allocate. It is undefined otherwise.
36+
auto_allocate_chunk_size: Cell<Option<u64>>,
37+
/// A ReadableStreamBYOBRequest instance representing the current BYOB pull request, or null if
38+
/// there are no pending requests.
39+
byob_request: MutNullableDom<ReadableStreamBYOBRequest>,
3540
/// All algoritems packed together:
36-
/// - Close algorithm: A promise-returning algorithm, taking one argument (the cancel reason), which communicates a requested cancelation to the underlying source
37-
/// - Pull algorithm: A promise-returning algorithm that pulls data from the underlying source
41+
/// - Cancel algorithm: A promise-returning algorithm, taking one argument (the cancel reason), which communicates a requested cancelation to the underlying byte source
42+
/// - Pull algorithm: A promise-returning algorithm that pulls data from the underlying byte source
3843
algorithms: DomRefCell<ControllerAlgorithms>,
39-
/// A boolean flag indicating whether the stream has been closed by its underlying source, but still has chunks in its internal queue that have not yet been read
44+
/// A boolean flag indicating whether the stream has been closed by its underlying byte source, but still has chunks in its internal queue that have not yet been read
4045
close_requested: Cell<bool>,
41-
/// A boolean flag set to true if the stream’s mechanisms requested a call to the underlying source's pull algorithm to pull more data, but the pull could not yet be done since a previous call is still executing
46+
/// A boolean flag set to true if the stream’s mechanisms requested a call to the underlying byte source's pull algorithm to pull more data,
47+
/// but the pull could not yet be done since a previous call is still executing
4248
pull_again: Cell<bool>,
43-
/// A boolean flag set to true while the underlying source's pull algorithm is executing and the returned promise has not yet fulfilled, used to prevent reentrant calls
49+
/// A boolean flag set to true while the underlying byte source's pull algorithm is executing and the returned promise has not yet fulfilled, used to prevent reentrant calls
4450
pulling: Cell<bool>,
45-
/// A list representing the stream’s internal queue of chunks
51+
/// A list of pull-into descriptors
52+
#[ignore_malloc_size_of = "Defined in mozjs"]
53+
pending_pull_intos: DomRefCell<VecDeque<Heap<JSVal>>>,
54+
/// A list of readable byte stream queue entries representing the stream’s internal queue of chunks
4655
#[ignore_malloc_size_of = "Defined in mozjs"]
4756
queue: DomRefCell<VecDeque<Heap<JSVal>>>,
48-
/// A boolean flag indicating whether the underlying source has finished starting
57+
/// A boolean flag indicating whether the underlying byte source has finished starting
4958
started: Cell<bool>,
50-
/// A number supplied to the constructor as part of the stream’s queuing strategy, indicating the point at which the stream will apply backpressure to its underlying source
59+
/// A number supplied to the constructor as part of the stream’s queuing strategy, indicating the point at which the stream will apply backpressure to its underlying byte source
5160
strategy_highwatermark: Cell<f64>,
52-
/// An algorithm to calculate the size of enqueued chunks, as part of the stream’s queuing strategy
53-
///
54-
/// If missing use default value (1) per https://streams.spec.whatwg.org/#make-size-algorithm-from-size-function
55-
#[ignore_malloc_size_of = "Rc is hard"]
56-
strategy_size_algorithm: DomRefCell<Option<Rc<QueuingStrategySize>>>,
5761
/// The ReadableStream instance controlled
5862
stream: DomRoot<ReadableStream>,
5963
}
@@ -62,14 +66,16 @@ impl ReadableByteStreamController {
6266
fn new_inherited(stream: DomRoot<ReadableStream>) -> Self {
6367
Self {
6468
reflector_: Reflector::new(),
69+
auto_allocate_chunk_size: Cell::new(None),
70+
byob_request: MutNullableDom::new(None),
71+
pending_pull_intos: Default::default(),
6572
queue: Default::default(),
6673
close_requested: Cell::new(false),
6774
pull_again: Cell::new(false),
6875
pulling: Cell::new(false),
6976
started: Cell::new(false),
7077
strategy_highwatermark: Cell::new(0.),
7178
algorithms: DomRefCell::new(ControllerAlgorithms::Undefined),
72-
strategy_size_algorithm: DomRefCell::new(None),
7379
stream,
7480
}
7581
}
@@ -80,40 +86,33 @@ impl ReadableByteStreamController {
8086

8187
/// <https://streams.spec.whatwg.org/#readable-byte-stream-controller-should-call-pull>
8288
fn should_call_pull(&self) -> bool {
83-
// TODO
8489
// Step 1
8590
let stream = &self.stream;
8691
// Step 2
87-
if !self.can_close_or_enqueue() {
92+
if stream.state() == StreamState::Readable {
8893
false
8994
// Step 3
90-
} else if !self.started.get() {
95+
} else if self.close_requested.get() {
9196
false
9297
// Step 4
93-
} else if stream.is_locked() && stream.get_num_read_requests() > 0 {
94-
return true;
95-
// Step 5 ~ 7
98+
} else if !self.started.get() {
99+
false
100+
// Step 5
101+
} else if stream.has_default_reader() && stream.get_num_read_requests() > 0 {
102+
true
103+
// Step 6
104+
} else if stream.has_byob_reader() && stream.get_num_read_into_requests() > 0 {
105+
true
106+
// Step 7 ~ 9
96107
} else if self.get_desired_size().unwrap() > 0. {
97108
true
98-
// Step 8
109+
// Step 10
99110
} else {
100111
false
101112
}
102113
}
103114

104-
/// <https://streams.spec.whatwg.org/#readable-stream-default-controller-can-close-or-enqueue>
105-
fn can_close_or_enqueue(&self) -> bool {
106-
// Step 1
107-
let state = self.stream.state();
108-
// Step 2 & 3
109-
if !self.close_requested.get() && state == StreamState::Readable {
110-
return true;
111-
} else {
112-
return false;
113-
}
114-
}
115-
116-
/// <https://streams.spec.whatwg.org/#readable-stream-default-controller-get-desired-size>
115+
/// https://streams.spec.whatwg.org/#readable-byte-stream-controller-get-desired-size
117116
pub fn get_desired_size(&self) -> Option<f64> {
118117
// Step 1
119118
let state = self.stream.state();
@@ -129,7 +128,7 @@ impl ReadableByteStreamController {
129128
}
130129
}
131130

132-
/// <https://streams.spec.whatwg.org/#readable-stream-default-controller-error>
131+
/// <https://streams.spec.whatwg.org/#readable-byte-stream-controller-error>
133132
fn error(&self, e: SafeHandleValue) {
134133
// TODO
135134
}
@@ -213,8 +212,7 @@ fn set_up_readable_byte_stream_controller(
213212
// Step 4
214213
controller.pull_again.set(false);
215214
controller.pulling.set(false);
216-
// Step 5
217-
// TODO
215+
// Step 5 is done in ReadableStreamDefaultController::new already.
218216
// Step 6
219217
controller.queue.borrow_mut().clear();
220218
// Step 7
@@ -225,9 +223,11 @@ fn set_up_readable_byte_stream_controller(
225223
// Step 9 & 10
226224
*controller.algorithms.borrow_mut() = algorithms;
227225
// Step 11
228-
// TODO
226+
controller
227+
.auto_allocate_chunk_size
228+
.set(auto_allocate_chunk_size);
229229
// Step 12
230-
// TODO
230+
controller.pending_pull_intos.borrow_mut().clear();
231231
// Step 13
232232
controller
233233
.stream

components/script/dom/readablestream.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,24 @@ impl ReadableStream {
416416
// TODO
417417
0
418418
}
419+
420+
/// <https://streams.spec.whatwg.org/#readable-stream-get-num-read-into-requests>
421+
pub fn get_num_read_into_requests(&self) -> usize {
422+
// TODO
423+
0
424+
}
425+
426+
/// <https://streams.spec.whatwg.org/#readable-stream-has-default-reader>
427+
pub fn has_default_reader(&self) -> bool {
428+
// TODO
429+
false
430+
}
431+
432+
/// <https://streams.spec.whatwg.org/#readable-stream-has-byob-reader>
433+
pub fn has_byob_reader(&self) -> bool {
434+
// TODO
435+
false
436+
}
419437
}
420438

421439
#[allow(unsafe_code)]

components/script/dom/readablestreambyobrequest.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ pub struct ReadableStreamBYOBRequest {
2828
reflector_: Reflector,
2929
}
3030

31+
impl ReadableStreamBYOBRequest {
32+
fn new_inherited() -> ReadableStreamBYOBRequest {
33+
ReadableStreamBYOBRequest {
34+
reflector_: Reflector::new(),
35+
}
36+
}
37+
38+
fn new(global: &GlobalScope) -> DomRoot<ReadableStreamBYOBRequest> {
39+
reflect_dom_object(Box::new(ReadableStreamBYOBRequest::new_inherited()), global)
40+
}
41+
}
42+
3143
impl ReadableStreamBYOBRequestMethods for ReadableStreamBYOBRequest {
3244
fn GetView(&self, cx: SafeJSContext) -> Option<js::typedarray::ArrayBufferView> {
3345
todo!()

components/script/dom/readablestreamdefaultcontroller.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::script_runtime::JSContext as SafeJSContext;
3333
pub struct ReadableStreamDefaultController {
3434
reflector_: Reflector,
3535
/// All algoritems packed together:
36-
/// - Close algorithm: A promise-returning algorithm, taking one argument (the cancel reason), which communicates a requested cancelation to the underlying source
36+
/// - Cancel algorithm: A promise-returning algorithm, taking one argument (the cancel reason), which communicates a requested cancelation to the underlying source
3737
/// - Pull algorithm: A promise-returning algorithm that pulls data from the underlying source
3838
algorithms: DomRefCell<ControllerAlgorithms>,
3939
/// A boolean flag indicating whether the stream has been closed by its underlying source, but still has chunks in its internal queue that have not yet been read

0 commit comments

Comments
 (0)