Nacre/vendor/servo-script/dom/stream/countqueuingstrategy.rs
astar76 0ecfd0f573 S031: root due timers in vendored servo-script (R5-01 SIGSEGV), crash-adblock regression smoke
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]>
2026-11-12 20:38:47 -06:00

131 lines
4.5 KiB
Rust

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* 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 dom_struct::dom_struct;
use js::context::JSContext;
use js::jsapi::CallArgs;
use js::jsval::{Int32Value, JSVal};
use js::rust::HandleObject;
use script_bindings::reflector::{Reflector, reflect_dom_object_with_proto};
use crate::dom::bindings::callback::RootedCallback;
use crate::dom::bindings::codegen::Bindings::FunctionBinding::Function;
use crate::dom::bindings::codegen::Bindings::QueuingStrategyBinding::{
CountQueuingStrategyMethods, QueuingStrategy, QueuingStrategyInit, QueuingStrategySize,
};
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::reflector::DomGlobal;
use crate::dom::bindings::root::DomRoot;
use crate::dom::types::GlobalScope;
use crate::{native_fn, native_raw_obj_fn};
#[dom_struct]
pub(crate) struct CountQueuingStrategy {
reflector_: Reflector,
high_water_mark: f64,
}
impl CountQueuingStrategy {
pub(crate) fn new_inherited(init: f64) -> Self {
Self {
reflector_: Reflector::new(),
high_water_mark: init,
}
}
pub(crate) fn new(
cx: &mut JSContext,
global: &GlobalScope,
proto: Option<HandleObject>,
init: f64,
) -> DomRoot<Self> {
reflect_dom_object_with_proto(cx, Box::new(Self::new_inherited(init)), global, proto)
}
}
impl CountQueuingStrategyMethods<crate::DomTypeHolder> for CountQueuingStrategy {
/// <https://streams.spec.whatwg.org/#cqs-constructor>
fn Constructor(
cx: &mut JSContext,
global: &GlobalScope,
proto: Option<HandleObject>,
init: &QueuingStrategyInit,
) -> DomRoot<Self> {
Self::new(cx, global, proto, init.highWaterMark)
}
/// <https://streams.spec.whatwg.org/#cqs-high-water-mark>
fn HighWaterMark(&self) -> f64 {
self.high_water_mark
}
/// <https://streams.spec.whatwg.org/#cqs-size>
fn GetSize(&self, cx: &mut JSContext) -> Fallible<RootedCallback<Function>> {
let global = self.global();
// Return this's relevant global object's count queuing strategy
// size function.
if let Some(fun) = global.get_count_queuing_strategy_size(cx) {
return Ok(fun);
}
// Step 1. Let steps be the following steps, given chunk
// Note: See ByteLengthQueuingStrategySize instead.
// Step 2. Let F be !CreateBuiltinFunction(steps, 1, "size", « »,
// globalObject’s relevant Realm).
let fun = native_fn!(cx, count_queuing_strategy_size, c"size", 0, 0);
// Step 3. Set globalObject’s count queuing strategy size function to
// a Function that represents a reference to F,
// with callback context equal to globalObject’s relevant settings object.
global.set_count_queuing_strategy_size(fun.clone());
Ok(fun)
}
}
/// <https://streams.spec.whatwg.org/#count-queuing-strategy-size-function>
pub(crate) fn count_queuing_strategy_size(_cx: &mut JSContext, args: CallArgs) -> bool {
// Step 1.1. Return 1.
args.rval().set(Int32Value(1));
true
}
/// Extract the high water mark from a QueuingStrategy.
/// If the high water mark is not set, return the default value.
///
/// <https://streams.spec.whatwg.org/#validate-and-normalize-high-water-mark>
pub(crate) fn extract_high_water_mark(
strategy: &QueuingStrategy,
default_hwm: f64,
) -> Result<f64, Error> {
if strategy.highWaterMark.is_none() {
return Ok(default_hwm);
}
let high_water_mark = strategy.highWaterMark.unwrap();
if high_water_mark.is_nan() || high_water_mark < 0.0 {
return Err(Error::Range(
c"High water mark must be a non-negative number.".to_owned(),
));
}
Ok(high_water_mark)
}
/// Extract the size algorithm from a QueuingStrategy.
/// If the size algorithm is not set, return a fallback function which always returns 1.
///
/// <https://streams.spec.whatwg.org/#make-size-algorithm-from-size-function>
pub(crate) fn extract_size_algorithm(
cx: &mut JSContext,
strategy: &QueuingStrategy,
) -> RootedCallback<QueuingStrategySize> {
if strategy.size.is_none() {
let fun_obj = native_raw_obj_fn!(cx, count_queuing_strategy_size, c"size", 0, 0);
#[expect(unsafe_code)]
unsafe {
return QueuingStrategySize::new(cx, fun_obj);
};
}
strategy.size.as_ref().unwrap().root(cx)
}