script/dom/stream/
countqueuingstrategy.rs1use std::rc::Rc;
6
7use dom_struct::dom_struct;
8use js::jsapi::{CallArgs, JSContext};
9use js::jsval::{Int32Value, JSVal};
10use js::rust::HandleObject;
11
12use crate::dom::bindings::codegen::Bindings::FunctionBinding::Function;
13use crate::dom::bindings::codegen::Bindings::QueuingStrategyBinding::{
14 CountQueuingStrategyMethods, QueuingStrategy, QueuingStrategyInit, QueuingStrategySize,
15};
16use crate::dom::bindings::error::{Error, Fallible};
17use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object_with_proto};
18use crate::dom::bindings::root::DomRoot;
19use crate::dom::types::GlobalScope;
20use crate::script_runtime::CanGc;
21use crate::{native_fn, native_raw_obj_fn};
22
23#[dom_struct]
24pub(crate) struct CountQueuingStrategy {
25 reflector_: Reflector,
26 high_water_mark: f64,
27}
28
29impl CountQueuingStrategy {
30 pub(crate) fn new_inherited(init: f64) -> Self {
31 Self {
32 reflector_: Reflector::new(),
33 high_water_mark: init,
34 }
35 }
36
37 pub(crate) fn new(
38 global: &GlobalScope,
39 proto: Option<HandleObject>,
40 init: f64,
41 can_gc: CanGc,
42 ) -> DomRoot<Self> {
43 reflect_dom_object_with_proto(Box::new(Self::new_inherited(init)), global, proto, can_gc)
44 }
45}
46
47impl CountQueuingStrategyMethods<crate::DomTypeHolder> for CountQueuingStrategy {
48 fn Constructor(
50 global: &GlobalScope,
51 proto: Option<HandleObject>,
52 can_gc: CanGc,
53 init: &QueuingStrategyInit,
54 ) -> DomRoot<Self> {
55 Self::new(global, proto, init.highWaterMark, can_gc)
56 }
57
58 fn HighWaterMark(&self) -> f64 {
60 self.high_water_mark
61 }
62
63 fn GetSize(&self, cx: &mut js::context::JSContext) -> Fallible<Rc<Function>> {
65 let global = self.global();
66 if let Some(fun) = global.get_count_queuing_strategy_size() {
69 return Ok(fun);
70 }
71
72 let fun = native_fn!(cx, count_queuing_strategy_size, c"size", 0, 0);
78 global.set_count_queuing_strategy_size(fun.clone());
82 Ok(fun)
83 }
84}
85
86pub(crate) fn count_queuing_strategy_size(
88 _cx: &mut js::context::JSContext,
89 args: CallArgs,
90) -> bool {
91 args.rval().set(Int32Value(1));
93 true
94}
95
96pub(crate) fn extract_high_water_mark(
101 strategy: &QueuingStrategy,
102 default_hwm: f64,
103) -> Result<f64, Error> {
104 if strategy.highWaterMark.is_none() {
105 return Ok(default_hwm);
106 }
107
108 let high_water_mark = strategy.highWaterMark.unwrap();
109 if high_water_mark.is_nan() || high_water_mark < 0.0 {
110 return Err(Error::Range(
111 c"High water mark must be a non-negative number.".to_owned(),
112 ));
113 }
114
115 Ok(high_water_mark)
116}
117
118pub(crate) fn extract_size_algorithm(
123 strategy: &QueuingStrategy,
124 _can_gc: CanGc,
125) -> Rc<QueuingStrategySize> {
126 if strategy.size.is_none() {
127 let cx = GlobalScope::get_cx();
128 let fun_obj = native_raw_obj_fn!(cx, count_queuing_strategy_size, c"size", 0, 0);
129 #[expect(unsafe_code)]
130 unsafe {
131 return QueuingStrategySize::new(cx, fun_obj);
132 };
133 }
134 strategy.size.as_ref().unwrap().clone()
135}