script/dom/
bytelengthqueuingstrategy.rs1use std::rc::Rc;
6
7use dom_struct::dom_struct;
8use js::gc::{HandleValue, MutableHandleValue};
9use js::jsapi::{CallArgs, JSContext};
10use js::jsval::JSVal;
11use js::rust::HandleObject;
12
13use super::bindings::codegen::Bindings::FunctionBinding::Function;
14use super::bindings::codegen::Bindings::QueuingStrategyBinding::{
15 ByteLengthQueuingStrategyMethods, QueuingStrategyInit,
16};
17use super::bindings::error::Fallible;
18use super::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object_with_proto};
19use super::bindings::root::DomRoot;
20use super::types::GlobalScope;
21use crate::dom::bindings::utils::get_dictionary_property;
22use crate::native_fn;
23use crate::script_runtime::CanGc;
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 global: &GlobalScope,
41 proto: Option<HandleObject>,
42 init: f64,
43 can_gc: CanGc,
44 ) -> DomRoot<Self> {
45 reflect_dom_object_with_proto(Box::new(Self::new_inherited(init)), global, proto, can_gc)
46 }
47}
48
49impl ByteLengthQueuingStrategyMethods<crate::DomTypeHolder> for ByteLengthQueuingStrategy {
50 fn Constructor(
52 global: &GlobalScope,
53 proto: Option<HandleObject>,
54 can_gc: CanGc,
55 init: &QueuingStrategyInit,
56 ) -> DomRoot<Self> {
57 Self::new(global, proto, init.highWaterMark, can_gc)
58 }
59 fn HighWaterMark(&self) -> f64 {
61 self.high_water_mark
62 }
63
64 fn GetSize(&self, _can_gc: CanGc) -> Fallible<Rc<Function>> {
66 let global = self.global();
67 if let Some(fun) = global.get_byte_length_queuing_strategy_size() {
70 return Ok(fun);
71 }
72
73 let fun = native_fn!(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
87#[allow(unsafe_code)]
89pub(crate) unsafe fn byte_length_queuing_strategy_size(
90 cx: *mut JSContext,
91 argc: u32,
92 vp: *mut JSVal,
93) -> bool {
94 let args = CallArgs::from_vp(vp, argc);
95
96 let val = HandleValue::from_raw(args.get(0));
98
99 if !val.is_object() {
102 return false;
103 }
104 rooted!(in(cx) let object = val.to_object());
105
106 get_dictionary_property(
108 cx,
109 object.handle(),
110 "byteLength",
111 MutableHandleValue::from_raw(args.rval()),
112 CanGc::note(),
113 )
114 .unwrap_or(false)
115}