Skip to main content

script/dom/stream/
countqueuingstrategy.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use std::rc::Rc;
6
7use dom_struct::dom_struct;
8use js::context::JSContext;
9use js::jsapi::CallArgs;
10use js::jsval::{Int32Value, JSVal};
11use js::rust::HandleObject;
12use script_bindings::reflector::{Reflector, reflect_dom_object_with_proto_and_cx};
13
14use crate::dom::bindings::codegen::Bindings::FunctionBinding::Function;
15use crate::dom::bindings::codegen::Bindings::QueuingStrategyBinding::{
16    CountQueuingStrategyMethods, QueuingStrategy, QueuingStrategyInit, QueuingStrategySize,
17};
18use crate::dom::bindings::error::{Error, Fallible};
19use crate::dom::bindings::reflector::DomGlobal;
20use crate::dom::bindings::root::DomRoot;
21use crate::dom::types::GlobalScope;
22use crate::{native_fn, native_raw_obj_fn};
23
24#[dom_struct]
25pub(crate) struct CountQueuingStrategy {
26    reflector_: Reflector,
27    high_water_mark: f64,
28}
29
30impl CountQueuingStrategy {
31    pub(crate) fn new_inherited(init: f64) -> Self {
32        Self {
33            reflector_: Reflector::new(),
34            high_water_mark: init,
35        }
36    }
37
38    pub(crate) fn new(
39        cx: &mut JSContext,
40        global: &GlobalScope,
41        proto: Option<HandleObject>,
42        init: f64,
43    ) -> DomRoot<Self> {
44        reflect_dom_object_with_proto_and_cx(Box::new(Self::new_inherited(init)), global, proto, cx)
45    }
46}
47
48impl CountQueuingStrategyMethods<crate::DomTypeHolder> for CountQueuingStrategy {
49    /// <https://streams.spec.whatwg.org/#cqs-constructor>
50    fn Constructor(
51        cx: &mut JSContext,
52        global: &GlobalScope,
53        proto: Option<HandleObject>,
54        init: &QueuingStrategyInit,
55    ) -> DomRoot<Self> {
56        Self::new(cx, global, proto, init.highWaterMark)
57    }
58
59    /// <https://streams.spec.whatwg.org/#cqs-high-water-mark>
60    fn HighWaterMark(&self) -> f64 {
61        self.high_water_mark
62    }
63
64    /// <https://streams.spec.whatwg.org/#cqs-size>
65    fn GetSize(&self, cx: &mut JSContext) -> Fallible<Rc<Function>> {
66        let global = self.global();
67        // Return this's relevant global object's count queuing strategy
68        // size function.
69        if let Some(fun) = global.get_count_queuing_strategy_size() {
70            return Ok(fun);
71        }
72
73        // Step 1. Let steps be the following steps, given chunk
74        // Note: See ByteLengthQueuingStrategySize instead.
75
76        // Step 2. Let F be !CreateBuiltinFunction(steps, 1, "size", « »,
77        // globalObject’s relevant Realm).
78        let fun = native_fn!(cx, count_queuing_strategy_size, c"size", 0, 0);
79        // Step 3. Set globalObject’s count queuing strategy size function to
80        // a Function that represents a reference to F,
81        // with callback context equal to globalObject’s relevant settings object.
82        global.set_count_queuing_strategy_size(fun.clone());
83        Ok(fun)
84    }
85}
86
87/// <https://streams.spec.whatwg.org/#count-queuing-strategy-size-function>
88pub(crate) fn count_queuing_strategy_size(_cx: &mut JSContext, args: CallArgs) -> bool {
89    // Step 1.1. Return 1.
90    args.rval().set(Int32Value(1));
91    true
92}
93
94/// Extract the high water mark from a QueuingStrategy.
95/// If the high water mark is not set, return the default value.
96///
97/// <https://streams.spec.whatwg.org/#validate-and-normalize-high-water-mark>
98pub(crate) fn extract_high_water_mark(
99    strategy: &QueuingStrategy,
100    default_hwm: f64,
101) -> Result<f64, Error> {
102    if strategy.highWaterMark.is_none() {
103        return Ok(default_hwm);
104    }
105
106    let high_water_mark = strategy.highWaterMark.unwrap();
107    if high_water_mark.is_nan() || high_water_mark < 0.0 {
108        return Err(Error::Range(
109            c"High water mark must be a non-negative number.".to_owned(),
110        ));
111    }
112
113    Ok(high_water_mark)
114}
115
116/// Extract the size algorithm from a QueuingStrategy.
117/// If the size algorithm is not set, return a fallback function which always returns 1.
118///
119/// <https://streams.spec.whatwg.org/#make-size-algorithm-from-size-function>
120pub(crate) fn extract_size_algorithm(
121    cx: &mut JSContext,
122    strategy: &QueuingStrategy,
123) -> Rc<QueuingStrategySize> {
124    if strategy.size.is_none() {
125        let fun_obj = native_raw_obj_fn!(cx, count_queuing_strategy_size, c"size", 0, 0);
126        #[expect(unsafe_code)]
127        unsafe {
128            return QueuingStrategySize::new(cx, fun_obj);
129        };
130    }
131    strategy.size.as_ref().unwrap().clone()
132}