Skip to main content

script/dom/stream/
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 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    /// <https://streams.spec.whatwg.org/#blqs-constructor>
51    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    /// <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, cx: &mut js::context::JSContext) -> Fallible<RootedCallback<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(cx) {
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!(cx, 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>
88fn byte_length_queuing_strategy_size(cx: &mut js::context::JSContext, args: CallArgs) -> bool {
89    // Step 1. Let steps be the following steps, given chunk:
90    // Step 1.1. Return ? GetV(chunk, "byteLength").
91    rooted!(&in(cx) let chunk = args.get(0).get());
92
93    // https://tc39.es/ecma262/#sec-getv
94    // Let O be ? ToObject(V).
95    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        // Return ? O.[[Get]]("byteLength", V).
105        // undefined for primitives without the property.
106        args.rval().set(UndefinedValue());
107        return true;
108    }
109
110    rooted!(&in(cx) let object = chunk.to_object());
111
112    // Return ? O.[[Get]](P, V).
113    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}