Use Dom in native promise handlers instead of Trusted (#35145)

* Use Dom in native promise handlers instead of Trusted

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Remove #[cfg_attr(crown, allow(crown::unrooted_must_root))]

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

---------

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>
This commit is contained in:
Taym Haddadi 2025-01-25 01:16:00 +01:00 committed by GitHub
parent af8d7c2de7
commit 81b09790cd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -15,12 +15,12 @@ use js::rust::{HandleObject, HandleValue as SafeHandleValue, HandleValue, Mutabl
use js::typedarray::Uint8;
use super::bindings::codegen::Bindings::QueuingStrategyBinding::QueuingStrategySize;
use super::bindings::root::Dom;
use crate::dom::bindings::buffer_source::create_buffer_source;
use crate::dom::bindings::callback::ExceptionHandling;
use crate::dom::bindings::codegen::Bindings::ReadableStreamDefaultControllerBinding::ReadableStreamDefaultControllerMethods;
use crate::dom::bindings::import::module::UnionTypes::ReadableStreamDefaultControllerOrReadableByteStreamController as Controller;
use crate::dom::bindings::import::module::{throw_dom_exception, Error, Fallible};
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
use crate::dom::bindings::trace::RootedTraceableBox;
@ -37,28 +37,25 @@ use crate::script_runtime::{CanGc, 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)]
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
struct PullAlgorithmFulfillmentHandler {
#[ignore_malloc_size_of = "Trusted are hard"]
controller: Trusted<ReadableStreamDefaultController>,
controller: Dom<ReadableStreamDefaultController>,
}
impl Callback for PullAlgorithmFulfillmentHandler {
/// Continuation of <https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed>
/// Upon fulfillment of pullPromise
fn callback(&self, _cx: JSContext, _v: HandleValue, _realm: InRealm, can_gc: CanGc) {
let controller = self.controller.root();
// Set controller.[[pulling]] to false.
controller.pulling.set(false);
self.controller.pulling.set(false);
// If controller.[[pullAgain]] is true,
if controller.pull_again.get() {
if self.controller.pull_again.get() {
// Set controller.[[pullAgain]] to false.
controller.pull_again.set(false);
self.controller.pull_again.set(false);
// Perform ! ReadableStreamDefaultControllerCallPullIfNeeded(controller).
controller.call_pull_if_needed(can_gc);
self.controller.call_pull_if_needed(can_gc);
}
}
}
@ -66,63 +63,54 @@ impl Callback for PullAlgorithmFulfillmentHandler {
/// The rejection handler for
/// <https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed>
#[derive(Clone, JSTraceable, MallocSizeOf)]
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
struct PullAlgorithmRejectionHandler {
#[ignore_malloc_size_of = "Trusted are hard"]
controller: Trusted<ReadableStreamDefaultController>,
controller: Dom<ReadableStreamDefaultController>,
}
impl Callback for PullAlgorithmRejectionHandler {
/// Continuation of <https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed>
/// Upon rejection of pullPromise with reason e.
fn callback(&self, _cx: JSContext, v: HandleValue, _realm: InRealm, _can_gc: CanGc) {
let controller = self.controller.root();
// Perform ! ReadableStreamDefaultControllerError(controller, e).
controller.error(v);
self.controller.error(v);
}
}
/// The fulfillment handler for
/// <https://streams.spec.whatwg.org/#dom-underlyingsource-start>
#[derive(Clone, JSTraceable, MallocSizeOf)]
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
struct StartAlgorithmFulfillmentHandler {
#[ignore_malloc_size_of = "Trusted are hard"]
controller: Trusted<ReadableStreamDefaultController>,
controller: Dom<ReadableStreamDefaultController>,
}
impl Callback for StartAlgorithmFulfillmentHandler {
/// Continuation of <https://streams.spec.whatwg.org/#set-up-readable-stream-default-controller>
/// Upon fulfillment of startPromise,
fn callback(&self, _cx: JSContext, _v: HandleValue, _realm: InRealm, can_gc: CanGc) {
let controller = self.controller.root();
// Set controller.[[started]] to true.
controller.started.set(true);
self.controller.started.set(true);
// Perform ! ReadableStreamDefaultControllerCallPullIfNeeded(controller).
controller.call_pull_if_needed(can_gc);
self.controller.call_pull_if_needed(can_gc);
}
}
/// The rejection handler for
/// <https://streams.spec.whatwg.org/#dom-underlyingsource-start>
#[derive(Clone, JSTraceable, MallocSizeOf)]
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
struct StartAlgorithmRejectionHandler {
#[ignore_malloc_size_of = "Trusted are hard"]
controller: Trusted<ReadableStreamDefaultController>,
controller: Dom<ReadableStreamDefaultController>,
}
impl Callback for StartAlgorithmRejectionHandler {
/// Continuation of <https://streams.spec.whatwg.org/#set-up-readable-stream-default-controller>
/// Upon rejection of startPromise with reason r,
fn callback(&self, _cx: JSContext, v: HandleValue, _realm: InRealm, _can_gc: CanGc) {
let controller = self.controller.root();
// Perform ! ReadableStreamDefaultControllerError(controller, r).
controller.error(v);
self.controller.error(v);
}
}
@ -393,19 +381,15 @@ impl ReadableStreamDefaultController {
// Let startPromise be a promise resolved with startResult.
let start_promise = start_result?;
// Upon fulfillment of startPromise,
let fulfillment_handler = Box::new(StartAlgorithmFulfillmentHandler {
controller: Trusted::new(&*rooted_default_controller),
});
// Upon rejection of startPromise with reason r,
let rejection_handler = Box::new(StartAlgorithmRejectionHandler {
controller: Trusted::new(&*rooted_default_controller),
});
// Upon fulfillment of startPromise, Upon rejection of startPromise with reason r,
let handler = PromiseNativeHandler::new(
global,
Some(fulfillment_handler),
Some(rejection_handler),
Some(Box::new(StartAlgorithmFulfillmentHandler {
controller: Dom::from_ref(&rooted_default_controller),
})),
Some(Box::new(StartAlgorithmRejectionHandler {
controller: Dom::from_ref(&rooted_default_controller),
})),
);
let realm = enter_realm(global);
let comp = InRealm::Entered(&realm);
@ -493,15 +477,15 @@ impl ReadableStreamDefaultController {
let Some(underlying_source) = self.underlying_source.get() else {
return;
};
let fulfillment_handler = Box::new(PullAlgorithmFulfillmentHandler {
controller: Trusted::new(&*rooted_default_controller),
});
let rejection_handler = Box::new(PullAlgorithmRejectionHandler {
controller: Trusted::new(&*rooted_default_controller),
});
let handler =
PromiseNativeHandler::new(&global, Some(fulfillment_handler), Some(rejection_handler));
let handler = PromiseNativeHandler::new(
&global,
Some(Box::new(PullAlgorithmFulfillmentHandler {
controller: Dom::from_ref(&rooted_default_controller),
})),
Some(Box::new(PullAlgorithmRejectionHandler {
controller: Dom::from_ref(&rooted_default_controller),
})),
);
let realm = enter_realm(&*global);
let comp = InRealm::Entered(&realm);