fire_timer moved due timers into an unrooted Vec; a GC inside one callback swept the functions of the timers still waiting. Heavy-web reruns and cargo test still pending. Co-Authored-By: Claude Opus 5.5 <[email protected]>
202 lines
7.3 KiB
Rust
202 lines
7.3 KiB
Rust
/* 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::Cell;
|
|
use std::rc::Rc;
|
|
|
|
use dom_struct::dom_struct;
|
|
use js::context::JSContext;
|
|
use js::conversions::ToJSValConvertible;
|
|
use js::jsapi::{Heap, Value};
|
|
use js::jsval::UndefinedValue;
|
|
use js::rust::HandleValue as SafeHandleValue;
|
|
use script_bindings::reflector::{Reflector, reflect_dom_object};
|
|
|
|
use crate::dom::bindings::error::Error;
|
|
use crate::dom::bindings::reflector::DomGlobal;
|
|
use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
|
|
use crate::dom::globalscope::GlobalScope;
|
|
use crate::dom::promise::{Promise, RootedPromise, TracedPromise};
|
|
use crate::dom::stream::defaultteereadrequest::DefaultTeeReadRequest;
|
|
use crate::dom::stream::readablestreamdefaultreader::ReadRequest;
|
|
use crate::dom::types::{ReadableStream, ReadableStreamDefaultReader};
|
|
|
|
#[derive(JSTraceable, MallocSizeOf)]
|
|
pub(crate) enum DefaultTeeCancelAlgorithm {
|
|
Cancel1Algorithm,
|
|
Cancel2Algorithm,
|
|
}
|
|
|
|
#[dom_struct]
|
|
/// <https://streams.spec.whatwg.org/#abstract-opdef-readablestreamdefaulttee>
|
|
pub(crate) struct DefaultTeeUnderlyingSource {
|
|
reflector_: Reflector,
|
|
reader: Dom<ReadableStreamDefaultReader>,
|
|
stream: Dom<ReadableStream>,
|
|
branch_1: MutNullableDom<ReadableStream>,
|
|
branch_2: MutNullableDom<ReadableStream>,
|
|
#[conditional_malloc_size_of]
|
|
reading: Rc<Cell<bool>>,
|
|
#[conditional_malloc_size_of]
|
|
read_again: Rc<Cell<bool>>,
|
|
#[conditional_malloc_size_of]
|
|
canceled_1: Rc<Cell<bool>>,
|
|
#[conditional_malloc_size_of]
|
|
canceled_2: Rc<Cell<bool>>,
|
|
#[conditional_malloc_size_of]
|
|
clone_for_branch_2: Rc<Cell<bool>>,
|
|
#[ignore_malloc_size_of = "mozjs"]
|
|
reason_1: Rc<Heap<Value>>,
|
|
#[ignore_malloc_size_of = "mozjs"]
|
|
reason_2: Rc<Heap<Value>>,
|
|
cancel_promise: TracedPromise,
|
|
tee_cancel_algorithm: DefaultTeeCancelAlgorithm,
|
|
}
|
|
|
|
impl DefaultTeeUnderlyingSource {
|
|
#[expect(clippy::too_many_arguments)]
|
|
#[cfg_attr(crown, expect(crown::unrooted_must_root))]
|
|
pub(crate) fn new(
|
|
cx: &mut JSContext,
|
|
reader: &ReadableStreamDefaultReader,
|
|
stream: &ReadableStream,
|
|
reading: Rc<Cell<bool>>,
|
|
read_again: Rc<Cell<bool>>,
|
|
canceled_1: Rc<Cell<bool>>,
|
|
canceled_2: Rc<Cell<bool>>,
|
|
clone_for_branch_2: Rc<Cell<bool>>,
|
|
reason_1: Rc<Heap<Value>>,
|
|
reason_2: Rc<Heap<Value>>,
|
|
cancel_promise: &RootedPromise,
|
|
tee_cancel_algorithm: DefaultTeeCancelAlgorithm,
|
|
) -> DomRoot<DefaultTeeUnderlyingSource> {
|
|
reflect_dom_object(
|
|
cx,
|
|
Box::new(DefaultTeeUnderlyingSource {
|
|
reflector_: Reflector::new(),
|
|
reader: Dom::from_ref(reader),
|
|
stream: Dom::from_ref(stream),
|
|
branch_1: MutNullableDom::new(None),
|
|
branch_2: MutNullableDom::new(None),
|
|
reading,
|
|
read_again,
|
|
canceled_1,
|
|
canceled_2,
|
|
clone_for_branch_2,
|
|
reason_1,
|
|
reason_2,
|
|
cancel_promise: cancel_promise.to_traced(),
|
|
tee_cancel_algorithm,
|
|
}),
|
|
&*stream.global(),
|
|
)
|
|
}
|
|
|
|
pub(crate) fn set_branch_1(&self, stream: &ReadableStream) {
|
|
self.branch_1.set(Some(stream));
|
|
}
|
|
|
|
pub(crate) fn set_branch_2(&self, stream: &ReadableStream) {
|
|
self.branch_2.set(Some(stream));
|
|
}
|
|
|
|
/// <https://streams.spec.whatwg.org/#abstract-opdef-readablestreamdefaulttee>
|
|
/// Let pullAlgorithm be the following steps:
|
|
pub(crate) fn pull_algorithm(&self, cx: &mut js::context::JSContext) -> RootedPromise {
|
|
// If reading is true,
|
|
if self.reading.get() {
|
|
// Set readAgain to true.
|
|
self.read_again.set(true);
|
|
// Return a promise resolved with undefined.
|
|
return Promise::new_resolved(cx, &self.stream.global(), ());
|
|
}
|
|
|
|
// Set reading to true.
|
|
self.reading.set(true);
|
|
|
|
// Let readRequest be a read request with the following items:
|
|
let tee_read_request = DefaultTeeReadRequest::new(
|
|
cx,
|
|
&self.stream,
|
|
&self.branch_1.get().expect("Branch 1 should be set."),
|
|
&self.branch_2.get().expect("Branch 2 should be set."),
|
|
self.reading.clone(),
|
|
self.read_again.clone(),
|
|
self.canceled_1.clone(),
|
|
self.canceled_2.clone(),
|
|
self.clone_for_branch_2.clone(),
|
|
&self.cancel_promise.root(cx),
|
|
self,
|
|
);
|
|
|
|
// Rooting: the tee read request is rooted above.
|
|
rooted!(&in(cx) let read_request = ReadRequest::DefaultTee {
|
|
tee_read_request: Dom::from_ref(&tee_read_request),
|
|
});
|
|
|
|
// Perform ! ReadableStreamDefaultReaderRead(reader, readRequest).
|
|
self.reader.read(cx, &read_request);
|
|
|
|
// Return a promise resolved with undefined.
|
|
Promise::new_resolved(cx, &self.stream.global(), ())
|
|
}
|
|
|
|
/// <https://streams.spec.whatwg.org/#abstract-opdef-readablestreamdefaulttee>
|
|
/// Let cancel1Algorithm be the following steps, taking a reason argument
|
|
/// and
|
|
/// Let cancel2Algorithm be the following steps, taking a reason argument
|
|
pub(crate) fn cancel_algorithm(
|
|
&self,
|
|
cx: &mut js::context::JSContext,
|
|
global: &GlobalScope,
|
|
reason: SafeHandleValue,
|
|
) -> Option<Result<RootedPromise, Error>> {
|
|
match self.tee_cancel_algorithm {
|
|
DefaultTeeCancelAlgorithm::Cancel1Algorithm => {
|
|
// Set canceled_1 to true.
|
|
self.canceled_1.set(true);
|
|
|
|
// Set reason_1 to reason.
|
|
self.reason_1.set(reason.get());
|
|
|
|
// If canceled_2 is true,
|
|
if self.canceled_2.get() {
|
|
self.resolve_cancel_promise(cx, global);
|
|
}
|
|
// Return cancelPromise.
|
|
Some(Ok(self.cancel_promise.root(cx)))
|
|
},
|
|
DefaultTeeCancelAlgorithm::Cancel2Algorithm => {
|
|
// Set canceled_2 to true.
|
|
self.canceled_2.set(true);
|
|
|
|
// Set reason_2 to reason.
|
|
self.reason_2.set(reason.get());
|
|
|
|
// If canceled_1 is true,
|
|
if self.canceled_1.get() {
|
|
self.resolve_cancel_promise(cx, global);
|
|
}
|
|
// Return cancelPromise.
|
|
Some(Ok(self.cancel_promise.root(cx)))
|
|
},
|
|
}
|
|
}
|
|
|
|
fn resolve_cancel_promise(&self, cx: &mut js::context::JSContext, global: &GlobalScope) {
|
|
// Let compositeReason be ! CreateArrayFromList(« reason_1, reason_2 »).
|
|
rooted_vec!(let mut reasons_values);
|
|
reasons_values.push(self.reason_1.get());
|
|
reasons_values.push(self.reason_2.get());
|
|
|
|
rooted!(&in(cx) let mut reasons_value = UndefinedValue());
|
|
reasons_values.to_jsval(cx, reasons_value.handle_mut());
|
|
|
|
// Let cancelResult be ! ReadableStreamCancel(stream, compositeReason).
|
|
let cancel_result = self.stream.cancel(cx, global, reasons_value.handle());
|
|
|
|
// Resolve cancelPromise with cancelResult.
|
|
self.cancel_promise.resolve_native(cx, &cancel_result);
|
|
}
|
|
}
|