implement Writablestreamdefaultcontroller abortcontroller (#37511)

using abort signal and abortcontroller in
Writablestreamdefaultcontroller

Part of https://github.com/servo/servo/issues/34866

---------

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>
This commit is contained in:
Taym Haddadi 2025-06-18 08:26:05 -07:00 committed by GitHub
parent 3ee339eb6d
commit fc2135cc02
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 63 additions and 119 deletions

View file

@ -35,7 +35,7 @@ impl AbortController {
}
/// <https://dom.spec.whatwg.org/#dom-abortcontroller-abortcontroller>
fn new_with_proto(
pub(crate) fn new_with_proto(
global: &GlobalScope,
proto: Option<HandleObject>,
can_gc: CanGc,
@ -52,10 +52,22 @@ impl AbortController {
}
/// <https://dom.spec.whatwg.org/#abortcontroller-signal-abort>
fn signal_abort(&self, cx: JSContext, reason: HandleValue, realm: InRealm, can_gc: CanGc) {
pub(crate) fn signal_abort(
&self,
cx: JSContext,
reason: HandleValue,
realm: InRealm,
can_gc: CanGc,
) {
// signal abort on controllers signal with reason if it is given.
self.signal.signal_abort(cx, reason, realm, can_gc);
}
/// <https://dom.spec.whatwg.org/#abortcontroller-signal>
pub(crate) fn signal(&self) -> DomRoot<AbortSignal> {
// The signal getter steps are to return thiss signal.
self.signal.as_rooted()
}
}
impl AbortControllerMethods<crate::DomTypeHolder> for AbortController {

View file

@ -59,7 +59,6 @@ impl AbortSignal {
}
}
#[allow(dead_code)]
pub(crate) fn new_with_proto(
global: &GlobalScope,
proto: Option<HandleObject>,

View file

@ -738,7 +738,7 @@ impl PipeTo {
let promise = match action {
ShutdownAction::WritableStreamAbort => {
let dest = self.writer.get_stream().expect("Stream must be set");
dest.abort(cx, global, error.handle(), can_gc)
dest.abort(cx, global, error.handle(), realm, can_gc)
},
ShutdownAction::ReadableStreamCancel => {
let source = self
@ -771,7 +771,7 @@ impl PipeTo {
// If dest.[[state]] is "writable",
let promise = if dest.is_writable() {
// return ! WritableStreamAbort(dest, error)
dest.abort(cx, global, error.handle(), can_gc)
dest.abort(cx, global, error.handle(), realm, can_gc)
} else {
// Otherwise, return a promise resolved with undefined.
Promise::new_resolved(global, cx, (), can_gc)

View file

@ -655,6 +655,7 @@ impl WritableStream {
cx: SafeJSContext,
global: &GlobalScope,
provided_reason: SafeHandleValue,
realm: InRealm,
can_gc: CanGc,
) -> Rc<Promise> {
// If stream.[[state]] is "closed" or "errored",
@ -663,10 +664,21 @@ impl WritableStream {
return Promise::new_resolved(global, cx, (), can_gc);
}
// TODO: Signal abort on stream.[[controller]].[[abortController]] with reason.
// Signal abort on stream.[[controller]].[[abortController]] with reason.
self.get_controller()
.expect("Stream must have a controller.")
.signal_abort(cx, provided_reason, realm, can_gc);
// TODO: If state is "closed" or "errored", return a promise resolved with undefined.
// Note: state may have changed because of signal above.
// Let state be stream.[[state]].
let state = self.state.get();
// If state is "closed" or "errored", return a promise resolved with undefined.
if matches!(
state,
WritableStreamState::Closed | WritableStreamState::Errored
) {
return Promise::new_resolved(global, cx, (), can_gc);
}
// If stream.[[pendingAbortRequest]] is not undefined,
if self.pending_abort_request.borrow().is_some() {
@ -1077,7 +1089,7 @@ impl WritableStreamMethods<crate::DomTypeHolder> for WritableStream {
}
// Return ! WritableStreamAbort(this, reason).
self.abort(cx, &global, reason, can_gc)
self.abort(cx, &global, reason, realm, can_gc)
}
/// <https://streams.spec.whatwg.org/#ws-close>

View file

@ -27,6 +27,7 @@ use crate::dom::messageport::MessagePort;
use crate::dom::promise::Promise;
use crate::dom::promisenativehandler::{Callback, PromiseNativeHandler};
use crate::dom::readablestreamdefaultcontroller::{EnqueuedValue, QueueWithSizes, ValueWithSize};
use crate::dom::types::{AbortController, AbortSignal};
use crate::dom::writablestream::WritableStream;
use crate::realms::{InRealm, enter_realm};
use crate::script_runtime::{CanGc, JSContext as SafeJSContext};
@ -339,15 +340,20 @@ pub struct WritableStreamDefaultController {
/// <https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-stream>
stream: MutNullableDom<WritableStream>,
/// <https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-abortcontroller>
abort_controller: Dom<AbortController>,
}
impl WritableStreamDefaultController {
/// <https://streams.spec.whatwg.org/#set-up-writable-stream-default-controller-from-underlying-sink>
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
fn new_inherited(
global: &GlobalScope,
underlying_sink_type: UnderlyingSinkType,
strategy_hwm: f64,
strategy_size: Rc<QueuingStrategySize>,
can_gc: CanGc,
) -> WritableStreamDefaultController {
WritableStreamDefaultController {
reflector_: Reflector::new(),
@ -358,6 +364,7 @@ impl WritableStreamDefaultController {
strategy_hwm,
strategy_size: RefCell::new(Some(strategy_size)),
started: Default::default(),
abort_controller: Dom::from_ref(&AbortController::new_with_proto(global, None, can_gc)),
}
}
@ -371,9 +378,11 @@ impl WritableStreamDefaultController {
) -> DomRoot<WritableStreamDefaultController> {
reflect_dom_object(
Box::new(WritableStreamDefaultController::new_inherited(
global,
underlying_sink_type,
strategy_hwm,
strategy_size,
can_gc,
)),
global,
can_gc,
@ -389,6 +398,18 @@ impl WritableStreamDefaultController {
self.underlying_sink_obj.set(*this_object);
}
/// "Signal abort" call from <https://streams.spec.whatwg.org/#writable-stream-abort>
pub(crate) fn signal_abort(
&self,
cx: SafeJSContext,
reason: SafeHandleValue,
realm: InRealm,
can_gc: CanGc,
) {
self.abort_controller
.signal_abort(cx, reason, realm, can_gc);
}
/// <https://streams.spec.whatwg.org/#writable-stream-default-controller-clear-algorithms>
fn clear_algorithms(&self) {
match &self.underlying_sink_type {
@ -1085,4 +1106,10 @@ impl WritableStreamDefaultControllerMethods<crate::DomTypeHolder>
// Perform ! WritableStreamDefaultControllerError(this, e).
self.error(&stream, cx, e, &global, can_gc);
}
/// <https://streams.spec.whatwg.org/#ws-default-controller-signal>
fn Signal(&self) -> DomRoot<AbortSignal> {
// Return this.[[abortController]]s signal.
self.abort_controller.signal()
}
}

View file

@ -238,6 +238,7 @@ impl WritableStreamDefaultWriter {
cx: SafeJSContext,
global: &GlobalScope,
reason: SafeHandleValue,
realm: InRealm,
can_gc: CanGc,
) -> Rc<Promise> {
// Let stream be writer.[[stream]].
@ -247,7 +248,7 @@ impl WritableStreamDefaultWriter {
};
// Return ! WritableStreamAbort(stream, reason).
stream.abort(cx, global, reason, can_gc)
stream.abort(cx, global, reason, realm, can_gc)
}
/// <https://streams.spec.whatwg.org/#writable-stream-default-writer-close>
@ -468,7 +469,7 @@ impl WritableStreamDefaultWriterMethods<crate::DomTypeHolder> for WritableStream
}
// Return ! WritableStreamDefaultWriterAbort(this, reason).
self.abort(cx, &global, reason, can_gc)
self.abort(cx, &global, reason, realm, can_gc)
}
/// <https://streams.spec.whatwg.org/#default-writer-close>