Skip to content

Commit 0068818

Browse files
committed
Finish ReadableStreamDefaultController Constructor
1 parent 94bae1d commit 0068818

File tree

1 file changed

+88
-20
lines changed

1 file changed

+88
-20
lines changed

components/script/dom/readablestreamdefaultcontroller.rs

Lines changed: 88 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct ReadableStreamDefaultController {
3535
/// All algoritems packed together:
3636
/// - Close 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
38-
algorithms: DomRefCell<Option<ControllerAlgorithms>>,
38+
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
4040
close_requested: Cell<bool>,
4141
/// 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
@@ -68,7 +68,7 @@ impl ReadableStreamDefaultController {
6868
pulling: Cell::new(false),
6969
started: Cell::new(false),
7070
strategy_highwatermark: Cell::new(0.),
71-
algorithms: DomRefCell::new(None),
71+
algorithms: DomRefCell::new(ControllerAlgorithms::Undefined),
7272
strategy_size_algorithm: DomRefCell::new(None),
7373
stream,
7474
}
@@ -78,9 +78,6 @@ impl ReadableStreamDefaultController {
7878
reflect_dom_object(Box::new(Self::new_inherited(stream)), global)
7979
}
8080

81-
/// <https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed>
82-
fn call_pull_if_needed(&self) {}
83-
8481
/// <https://streams.spec.whatwg.org/#readable-stream-default-controller-should-call-pull>
8582
fn should_call_pull(&self) -> bool {
8683
// Step 1
@@ -130,6 +127,11 @@ impl ReadableStreamDefaultController {
130127
},
131128
}
132129
}
130+
131+
/// <https://streams.spec.whatwg.org/#readable-stream-default-controller-error>
132+
fn error(&self, e: SafeHandleValue) {
133+
// TODO
134+
}
133135
}
134136

135137
impl ReadableStreamDefaultControllerMethods for ReadableStreamDefaultController {
@@ -197,8 +199,7 @@ fn set_up_readable_stream_default_controller(
197199
*controller.strategy_size_algorithm.borrow_mut() = Some(size_algorithm);
198200
controller.strategy_highwatermark.set(highwatermark);
199201
// Step 6 & 7
200-
*controller.algorithms.borrow_mut() = Some(algorithms);
201-
//
202+
*controller.algorithms.borrow_mut() = algorithms;
202203
// Step 8
203204
controller
204205
.stream
@@ -207,7 +208,7 @@ fn set_up_readable_stream_default_controller(
207208
));
208209
// Step 9
209210
rooted!(in(*cx) let mut start_result = UndefinedValue());
210-
controller.algorithms.borrow().as_ref().unwrap().start(
211+
controller.algorithms.borrow().start(
211212
cx,
212213
ReadableStreamController::ReadableStreamDefaultController(controller.clone()),
213214
start_result.handle_mut(),
@@ -239,43 +240,93 @@ fn set_up_readable_stream_default_controller(
239240
}
240241

241242
impl Callback for ResolveHandler {
242-
fn callback(&self, cx: SafeJSContext, v: SafeHandleValue, realm: InRealm) {
243+
fn callback(&self, cx: SafeJSContext, _v: SafeHandleValue, _realm: InRealm) {
243244
// Step 11.1
244245
self.controller.started.set(true);
245246
// Step 11.2
246247
assert!(!self.controller.pulling.get());
247248
// Step 11.3
248249
assert!(!self.controller.pull_again.get());
249250
// Step 11.4
250-
self.controller.call_pull_if_needed();
251+
assert!(readable_stream_default_controller_call_pull_if_needed(
252+
cx,
253+
self.controller.clone()
254+
)
255+
.is_ok());
256+
}
257+
}
258+
259+
Ok(())
260+
}
261+
262+
/// <https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed>
263+
fn readable_stream_default_controller_call_pull_if_needed(
264+
cx: SafeJSContext,
265+
controller: DomRoot<ReadableStreamDefaultController>,
266+
) -> Fallible<()> {
267+
// Step 1 & 2
268+
if controller.should_call_pull() {
269+
// Step 3
270+
if controller.pulling.get() {
271+
controller.pull_again.set(true);
272+
} else {
273+
// Step 4
274+
assert!(!controller.pull_again.get());
275+
// Step 5
276+
controller.pulling.set(true);
277+
// Step 6
278+
let pull_promise = controller.algorithms.borrow().pull(
279+
cx,
280+
ReadableStreamController::ReadableStreamDefaultController(controller.clone()),
281+
)?;
282+
let global = &*controller.global();
283+
let realm = enter_realm(&*global);
284+
let comp = InRealm::Entered(&realm);
285+
pull_promise.append_native_handler(
286+
&PromiseNativeHandler::new(
287+
global,
288+
Some(ResolveHandler::new(controller.clone())),
289+
Some(RejectHandler::new(controller)),
290+
),
291+
comp,
292+
);
251293
}
252294
}
253295

254296
#[derive(JSTraceable, MallocSizeOf)]
255-
struct RejectHandler {
297+
struct ResolveHandler {
256298
controller: DomRoot<ReadableStreamDefaultController>,
257299
}
258300

259-
impl RejectHandler {
301+
impl ResolveHandler {
260302
pub fn new(controller: DomRoot<ReadableStreamDefaultController>) -> Box<dyn Callback> {
261303
Box::new(Self { controller })
262304
}
263305
}
264306

265-
impl Callback for RejectHandler {
266-
fn callback(&self, cx: SafeJSContext, v: SafeHandleValue, realm: InRealm) {
267-
todo!()
307+
impl Callback for ResolveHandler {
308+
fn callback(&self, cx: SafeJSContext, _v: SafeHandleValue, _realm: InRealm) {
309+
// Step 7.1
310+
self.controller.pulling.set(false);
311+
// Step 7.2
312+
if self.controller.pull_again.get() {
313+
self.controller.pull_again.set(false);
314+
assert!(readable_stream_default_controller_call_pull_if_needed(
315+
cx,
316+
self.controller.clone()
317+
)
318+
.is_ok());
319+
}
268320
}
269321
}
270-
271322
Ok(())
272323
}
273324

274325
/// Algorithms for [setup_readable_stream_default_controller_from_underlying_source]
275326
#[derive(JSTraceable, MallocSizeOf)]
276327
pub enum ControllerAlgorithms {
277328
UnderlyingSource(UnderlyingSourceAlgorithms),
278-
None,
329+
Undefined,
279330
}
280331

281332
impl ControllerAlgorithms {
@@ -287,7 +338,7 @@ impl ControllerAlgorithms {
287338
) -> Fallible<()> {
288339
match self {
289340
ControllerAlgorithms::UnderlyingSource(s) => s.start(cx, controller, retval),
290-
ControllerAlgorithms::None => unreachable!(),
341+
ControllerAlgorithms::Undefined => unreachable!(),
291342
}
292343
}
293344

@@ -298,14 +349,14 @@ impl ControllerAlgorithms {
298349
) -> Fallible<Rc<Promise>> {
299350
match self {
300351
ControllerAlgorithms::UnderlyingSource(s) => s.pull(cx, controller),
301-
ControllerAlgorithms::None => unreachable!(),
352+
ControllerAlgorithms::Undefined => unreachable!(),
302353
}
303354
}
304355

305356
fn cancel(&self, cx: SafeJSContext, reason: Option<HandleValue>) -> Fallible<Rc<Promise>> {
306357
match self {
307358
ControllerAlgorithms::UnderlyingSource(s) => s.cancel(cx, reason),
308-
ControllerAlgorithms::None => unreachable!(),
359+
ControllerAlgorithms::Undefined => unreachable!(),
309360
}
310361
}
311362
}
@@ -390,3 +441,20 @@ impl UnderlyingSourceAlgorithms {
390441
}
391442
}
392443
}
444+
445+
#[derive(JSTraceable, MallocSizeOf)]
446+
struct RejectHandler {
447+
controller: DomRoot<ReadableStreamDefaultController>,
448+
}
449+
450+
impl RejectHandler {
451+
pub fn new(controller: DomRoot<ReadableStreamDefaultController>) -> Box<dyn Callback> {
452+
Box::new(Self { controller })
453+
}
454+
}
455+
456+
impl Callback for RejectHandler {
457+
fn callback(&self, cx: SafeJSContext, v: SafeHandleValue, realm: InRealm) {
458+
self.controller.error(v);
459+
}
460+
}

0 commit comments

Comments
 (0)