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