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