script/dom/
bytelengthqueuingstrategy.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use 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    /// <https://streams.spec.whatwg.org/#blqs-constructor>
51    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    /// <https://streams.spec.whatwg.org/#blqs-high-water-mark>
60    fn HighWaterMark(&self) -> f64 {
61        self.high_water_mark
62    }
63
64    /// <https://streams.spec.whatwg.org/#blqs-size>
65    fn GetSize(&self, _can_gc: CanGc) -> Fallible<Rc<Function>> {
66        let global = self.global();
67        // Return this's relevant global object's byte length queuing strategy
68        // size function.
69        if let Some(fun) = global.get_byte_length_queuing_strategy_size() {
70            return Ok(fun);
71        }
72
73        // Step 1. Let steps be the following steps, given chunk
74        // Note: See ByteLengthQueuingStrategySize instead.
75
76        // Step 2. Let F be !CreateBuiltinFunction(steps, 1, "size", « »,
77        // globalObject’s relevant Realm).
78        let fun = native_fn!(byte_length_queuing_strategy_size, c"size", 1, 0);
79        // Step 3. Set globalObject’s byte length queuing strategy size function to
80        // a Function that represents a reference to F,
81        // with callback context equal to globalObject's relevant settings object.
82        global.set_byte_length_queuing_strategy_size(fun.clone());
83        Ok(fun)
84    }
85}
86
87/// <https://streams.spec.whatwg.org/#byte-length-queuing-strategy-size-function>
88#[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    // Step 1.1: Return ? GetV(chunk, "byteLength").
97    let val = HandleValue::from_raw(args.get(0));
98
99    // https://tc39.es/ecma262/multipage/abstract-operations.html#sec-getv
100    // Let O be ? ToObject(V).
101    if !val.is_object() {
102        return false;
103    }
104    rooted!(in(cx) let object = val.to_object());
105
106    // Return ? O.[[Get]](P, V).
107    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}