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 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    /// <https://streams.spec.whatwg.org/#blqs-constructor>
52    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    /// <https://streams.spec.whatwg.org/#blqs-high-water-mark>
61    fn HighWaterMark(&self) -> f64 {
62        self.high_water_mark
63    }
64
65    /// <https://streams.spec.whatwg.org/#blqs-size>
66    fn GetSize(&self, cx: &mut js::context::JSContext) -> Fallible<Rc<Function>> {
67        let global = self.global();
68        // Return this's relevant global object's byte length queuing strategy
69        // size function.
70        if let Some(fun) = global.get_byte_length_queuing_strategy_size() {
71            return Ok(fun);
72        }
73
74        // Step 1. Let steps be the following steps, given chunk
75        // Note: See ByteLengthQueuingStrategySize instead.
76
77        // Step 2. Let F be !CreateBuiltinFunction(steps, 1, "size", « »,
78        // globalObject’s relevant Realm).
79        let fun = native_fn!(cx, byte_length_queuing_strategy_size, c"size", 1, 0);
80        // Step 3. Set globalObject’s byte length queuing strategy size function to
81        // a Function that represents a reference to F,
82        // with callback context equal to globalObject's relevant settings object.
83        global.set_byte_length_queuing_strategy_size(fun.clone());
84        Ok(fun)
85    }
86}
87
88/// <https://streams.spec.whatwg.org/#byte-length-queuing-strategy-size-function>
89fn byte_length_queuing_strategy_size(cx: &mut js::context::JSContext, args: CallArgs) -> bool {
90    // Step 1. Let steps be the following steps, given chunk:
91    // Step 1.1. Return ? GetV(chunk, "byteLength").
92    rooted!(&in(cx) let chunk = args.get(0).get());
93
94    // https://tc39.es/ecma262/#sec-getv
95    // Let O be ? ToObject(V).
96    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        // Return ? O.[[Get]]("byteLength", V).
106        // undefined for primitives without the property.
107        args.rval().set(UndefinedValue());
108        return true;
109    }
110
111    rooted!(&in(cx) let object = chunk.to_object());
112
113    // Return ? O.[[Get]](P, V).
114    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}