script/dom/stream/
bytelengthqueuingstrategy.rs1use std::rc::Rc;
6
7use dom_struct::dom_struct;
8use js::context::JSContext;
9use js::error::throw_type_error;
10use js::gc::{HandleValue, MutableHandleValue};
11use js::jsapi::CallArgs;
12use js::jsval::{JSVal, UndefinedValue};
13use js::rust::HandleObject;
14use script_bindings::reflector::{Reflector, reflect_dom_object_with_proto_and_cx};
15
16use crate::dom::bindings::codegen::Bindings::FunctionBinding::Function;
17use crate::dom::bindings::codegen::Bindings::QueuingStrategyBinding::{
18 ByteLengthQueuingStrategyMethods, QueuingStrategyInit,
19};
20use crate::dom::bindings::conversions::get_property_jsval;
21use crate::dom::bindings::error::Fallible;
22use crate::dom::bindings::reflector::DomGlobal;
23use crate::dom::bindings::root::DomRoot;
24use crate::dom::types::GlobalScope;
25use crate::native_fn;
26
27#[dom_struct]
28pub(crate) struct ByteLengthQueuingStrategy {
29 reflector_: Reflector,
30 high_water_mark: f64,
31}
32
33impl ByteLengthQueuingStrategy {
34 pub(crate) fn new_inherited(init: f64) -> Self {
35 Self {
36 reflector_: Reflector::new(),
37 high_water_mark: init,
38 }
39 }
40
41 pub(crate) fn new(
42 cx: &mut JSContext,
43 global: &GlobalScope,
44 proto: Option<HandleObject>,
45 init: f64,
46 ) -> DomRoot<Self> {
47 reflect_dom_object_with_proto_and_cx(Box::new(Self::new_inherited(init)), global, proto, cx)
48 }
49}
50
51impl ByteLengthQueuingStrategyMethods<crate::DomTypeHolder> for ByteLengthQueuingStrategy {
52 fn Constructor(
54 cx: &mut JSContext,
55 global: &GlobalScope,
56 proto: Option<HandleObject>,
57 init: &QueuingStrategyInit,
58 ) -> DomRoot<Self> {
59 Self::new(cx, global, proto, init.highWaterMark)
60 }
61 fn HighWaterMark(&self) -> f64 {
63 self.high_water_mark
64 }
65
66 fn GetSize(&self, cx: &mut js::context::JSContext) -> Fallible<Rc<Function>> {
68 let global = self.global();
69 if let Some(fun) = global.get_byte_length_queuing_strategy_size() {
72 return Ok(fun);
73 }
74
75 let fun = native_fn!(cx, byte_length_queuing_strategy_size, c"size", 1, 0);
81 global.set_byte_length_queuing_strategy_size(fun.clone());
85 Ok(fun)
86 }
87}
88
89#[expect(unsafe_code)]
91fn byte_length_queuing_strategy_size(cx: &mut js::context::JSContext, args: CallArgs) -> bool {
92 let chunk = unsafe { HandleValue::from_raw(args.get(0)) };
95
96 if chunk.is_undefined() || chunk.is_null() {
99 unsafe {
100 throw_type_error(
101 cx.raw_cx(),
102 c"ByteLengthQueuingStrategy size called with undefined or nulll",
103 )
104 };
105 return false;
106 }
107
108 if !chunk.is_object() {
109 args.rval().set(UndefinedValue());
112 return true;
113 }
114
115 rooted!(&in(cx) let object = chunk.to_object());
116
117 get_property_jsval(cx, object.handle(), c"byteLength", unsafe {
119 MutableHandleValue::from_raw(args.rval())
120 })
121 .is_ok()
122}