mirror of
https://github.com/servo/servo.git
synced 2025-07-22 23:03:42 +01:00
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:
parent
af8d7c2de7
commit
81b09790cd
1 changed files with 33 additions and 49 deletions
|
@ -15,12 +15,12 @@ use js::rust::{HandleObject, HandleValue as SafeHandleValue, HandleValue, Mutabl
|
||||||
use js::typedarray::Uint8;
|
use js::typedarray::Uint8;
|
||||||
|
|
||||||
use super::bindings::codegen::Bindings::QueuingStrategyBinding::QueuingStrategySize;
|
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::buffer_source::create_buffer_source;
|
||||||
use crate::dom::bindings::callback::ExceptionHandling;
|
use crate::dom::bindings::callback::ExceptionHandling;
|
||||||
use crate::dom::bindings::codegen::Bindings::ReadableStreamDefaultControllerBinding::ReadableStreamDefaultControllerMethods;
|
use crate::dom::bindings::codegen::Bindings::ReadableStreamDefaultControllerBinding::ReadableStreamDefaultControllerMethods;
|
||||||
use crate::dom::bindings::import::module::UnionTypes::ReadableStreamDefaultControllerOrReadableByteStreamController as Controller;
|
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::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::reflector::{reflect_dom_object, DomObject, Reflector};
|
||||||
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
|
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
|
||||||
use crate::dom::bindings::trace::RootedTraceableBox;
|
use crate::dom::bindings::trace::RootedTraceableBox;
|
||||||
|
@ -37,28 +37,25 @@ use crate::script_runtime::{CanGc, JSContext, JSContext as SafeJSContext};
|
||||||
/// The fulfillment handler for
|
/// The fulfillment handler for
|
||||||
/// <https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed>
|
/// <https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed>
|
||||||
#[derive(Clone, JSTraceable, MallocSizeOf)]
|
#[derive(Clone, JSTraceable, MallocSizeOf)]
|
||||||
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
|
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
|
||||||
struct PullAlgorithmFulfillmentHandler {
|
struct PullAlgorithmFulfillmentHandler {
|
||||||
#[ignore_malloc_size_of = "Trusted are hard"]
|
controller: Dom<ReadableStreamDefaultController>,
|
||||||
controller: Trusted<ReadableStreamDefaultController>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Callback for PullAlgorithmFulfillmentHandler {
|
impl Callback for PullAlgorithmFulfillmentHandler {
|
||||||
/// Continuation of <https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed>
|
/// Continuation of <https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed>
|
||||||
/// Upon fulfillment of pullPromise
|
/// Upon fulfillment of pullPromise
|
||||||
fn callback(&self, _cx: JSContext, _v: HandleValue, _realm: InRealm, can_gc: CanGc) {
|
fn callback(&self, _cx: JSContext, _v: HandleValue, _realm: InRealm, can_gc: CanGc) {
|
||||||
let controller = self.controller.root();
|
|
||||||
|
|
||||||
// Set controller.[[pulling]] to false.
|
// Set controller.[[pulling]] to false.
|
||||||
controller.pulling.set(false);
|
self.controller.pulling.set(false);
|
||||||
|
|
||||||
// If controller.[[pullAgain]] is true,
|
// If controller.[[pullAgain]] is true,
|
||||||
if controller.pull_again.get() {
|
if self.controller.pull_again.get() {
|
||||||
// Set controller.[[pullAgain]] to false.
|
// Set controller.[[pullAgain]] to false.
|
||||||
controller.pull_again.set(false);
|
self.controller.pull_again.set(false);
|
||||||
|
|
||||||
// Perform ! ReadableStreamDefaultControllerCallPullIfNeeded(controller).
|
// 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
|
/// The rejection handler for
|
||||||
/// <https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed>
|
/// <https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed>
|
||||||
#[derive(Clone, JSTraceable, MallocSizeOf)]
|
#[derive(Clone, JSTraceable, MallocSizeOf)]
|
||||||
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
|
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
|
||||||
struct PullAlgorithmRejectionHandler {
|
struct PullAlgorithmRejectionHandler {
|
||||||
#[ignore_malloc_size_of = "Trusted are hard"]
|
controller: Dom<ReadableStreamDefaultController>,
|
||||||
controller: Trusted<ReadableStreamDefaultController>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Callback for PullAlgorithmRejectionHandler {
|
impl Callback for PullAlgorithmRejectionHandler {
|
||||||
/// Continuation of <https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed>
|
/// Continuation of <https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed>
|
||||||
/// Upon rejection of pullPromise with reason e.
|
/// Upon rejection of pullPromise with reason e.
|
||||||
fn callback(&self, _cx: JSContext, v: HandleValue, _realm: InRealm, _can_gc: CanGc) {
|
fn callback(&self, _cx: JSContext, v: HandleValue, _realm: InRealm, _can_gc: CanGc) {
|
||||||
let controller = self.controller.root();
|
|
||||||
|
|
||||||
// Perform ! ReadableStreamDefaultControllerError(controller, e).
|
// Perform ! ReadableStreamDefaultControllerError(controller, e).
|
||||||
controller.error(v);
|
self.controller.error(v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The fulfillment handler for
|
/// The fulfillment handler for
|
||||||
/// <https://streams.spec.whatwg.org/#dom-underlyingsource-start>
|
/// <https://streams.spec.whatwg.org/#dom-underlyingsource-start>
|
||||||
#[derive(Clone, JSTraceable, MallocSizeOf)]
|
#[derive(Clone, JSTraceable, MallocSizeOf)]
|
||||||
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
|
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
|
||||||
struct StartAlgorithmFulfillmentHandler {
|
struct StartAlgorithmFulfillmentHandler {
|
||||||
#[ignore_malloc_size_of = "Trusted are hard"]
|
controller: Dom<ReadableStreamDefaultController>,
|
||||||
controller: Trusted<ReadableStreamDefaultController>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Callback for StartAlgorithmFulfillmentHandler {
|
impl Callback for StartAlgorithmFulfillmentHandler {
|
||||||
/// Continuation of <https://streams.spec.whatwg.org/#set-up-readable-stream-default-controller>
|
/// Continuation of <https://streams.spec.whatwg.org/#set-up-readable-stream-default-controller>
|
||||||
/// Upon fulfillment of startPromise,
|
/// Upon fulfillment of startPromise,
|
||||||
fn callback(&self, _cx: JSContext, _v: HandleValue, _realm: InRealm, can_gc: CanGc) {
|
fn callback(&self, _cx: JSContext, _v: HandleValue, _realm: InRealm, can_gc: CanGc) {
|
||||||
let controller = self.controller.root();
|
|
||||||
|
|
||||||
// Set controller.[[started]] to true.
|
// Set controller.[[started]] to true.
|
||||||
controller.started.set(true);
|
self.controller.started.set(true);
|
||||||
|
|
||||||
// Perform ! ReadableStreamDefaultControllerCallPullIfNeeded(controller).
|
// Perform ! ReadableStreamDefaultControllerCallPullIfNeeded(controller).
|
||||||
controller.call_pull_if_needed(can_gc);
|
self.controller.call_pull_if_needed(can_gc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The rejection handler for
|
/// The rejection handler for
|
||||||
/// <https://streams.spec.whatwg.org/#dom-underlyingsource-start>
|
/// <https://streams.spec.whatwg.org/#dom-underlyingsource-start>
|
||||||
#[derive(Clone, JSTraceable, MallocSizeOf)]
|
#[derive(Clone, JSTraceable, MallocSizeOf)]
|
||||||
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
|
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
|
||||||
struct StartAlgorithmRejectionHandler {
|
struct StartAlgorithmRejectionHandler {
|
||||||
#[ignore_malloc_size_of = "Trusted are hard"]
|
controller: Dom<ReadableStreamDefaultController>,
|
||||||
controller: Trusted<ReadableStreamDefaultController>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Callback for StartAlgorithmRejectionHandler {
|
impl Callback for StartAlgorithmRejectionHandler {
|
||||||
/// Continuation of <https://streams.spec.whatwg.org/#set-up-readable-stream-default-controller>
|
/// Continuation of <https://streams.spec.whatwg.org/#set-up-readable-stream-default-controller>
|
||||||
/// Upon rejection of startPromise with reason r,
|
/// Upon rejection of startPromise with reason r,
|
||||||
fn callback(&self, _cx: JSContext, v: HandleValue, _realm: InRealm, _can_gc: CanGc) {
|
fn callback(&self, _cx: JSContext, v: HandleValue, _realm: InRealm, _can_gc: CanGc) {
|
||||||
let controller = self.controller.root();
|
|
||||||
|
|
||||||
// Perform ! ReadableStreamDefaultControllerError(controller, r).
|
// 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 startPromise be a promise resolved with startResult.
|
||||||
let start_promise = start_result?;
|
let start_promise = start_result?;
|
||||||
|
|
||||||
// Upon fulfillment of startPromise,
|
// Upon fulfillment of startPromise, Upon rejection of startPromise with reason r,
|
||||||
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),
|
|
||||||
});
|
|
||||||
let handler = PromiseNativeHandler::new(
|
let handler = PromiseNativeHandler::new(
|
||||||
global,
|
global,
|
||||||
Some(fulfillment_handler),
|
Some(Box::new(StartAlgorithmFulfillmentHandler {
|
||||||
Some(rejection_handler),
|
controller: Dom::from_ref(&rooted_default_controller),
|
||||||
|
})),
|
||||||
|
Some(Box::new(StartAlgorithmRejectionHandler {
|
||||||
|
controller: Dom::from_ref(&rooted_default_controller),
|
||||||
|
})),
|
||||||
);
|
);
|
||||||
let realm = enter_realm(global);
|
let realm = enter_realm(global);
|
||||||
let comp = InRealm::Entered(&realm);
|
let comp = InRealm::Entered(&realm);
|
||||||
|
@ -493,15 +477,15 @@ impl ReadableStreamDefaultController {
|
||||||
let Some(underlying_source) = self.underlying_source.get() else {
|
let Some(underlying_source) = self.underlying_source.get() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
let handler = PromiseNativeHandler::new(
|
||||||
let fulfillment_handler = Box::new(PullAlgorithmFulfillmentHandler {
|
&global,
|
||||||
controller: Trusted::new(&*rooted_default_controller),
|
Some(Box::new(PullAlgorithmFulfillmentHandler {
|
||||||
});
|
controller: Dom::from_ref(&rooted_default_controller),
|
||||||
let rejection_handler = Box::new(PullAlgorithmRejectionHandler {
|
})),
|
||||||
controller: Trusted::new(&*rooted_default_controller),
|
Some(Box::new(PullAlgorithmRejectionHandler {
|
||||||
});
|
controller: Dom::from_ref(&rooted_default_controller),
|
||||||
let handler =
|
})),
|
||||||
PromiseNativeHandler::new(&global, Some(fulfillment_handler), Some(rejection_handler));
|
);
|
||||||
|
|
||||||
let realm = enter_realm(&*global);
|
let realm = enter_realm(&*global);
|
||||||
let comp = InRealm::Entered(&realm);
|
let comp = InRealm::Entered(&realm);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue