Skip to main content

quickjs_jit_stdlib/llrt/llrt_stream_web/utils/
queue.rs

1// Vendored from LLRT d9fd873e793c (modules/llrt_stream_web/src/utils/queue.rs); module paths and
2// feature names adapted by scripts/import-stdlib.py.
3use std::collections::VecDeque;
4
5use rquickjs::{class::Trace, Ctx, Exception, JsLifetime, Result, Value};
6
7use crate::stream_web::queuing_strategy::SizeValue;
8
9/// QueueWithSize is present in readable and writable streams and abstracts away certain queue operations
10/// https://streams.spec.whatwg.org/#queue-with-sizes
11#[derive(JsLifetime, Trace, Default)]
12pub struct QueueWithSizes<'js> {
13    pub queue: VecDeque<ValueWithSize<'js>>,
14    pub queue_total_size: f64,
15}
16
17impl<'js> QueueWithSizes<'js> {
18    pub fn new() -> Self {
19        Self {
20            queue: VecDeque::new(),
21            queue_total_size: 0.0,
22        }
23    }
24
25    pub(crate) fn enqueue_value_with_size(
26        &mut self,
27        ctx: &Ctx<'js>,
28        value: Value<'js>,
29        size: SizeValue<'js>,
30    ) -> Result<()> {
31        let size = match is_non_negative_number(size) {
32            None => {
33                // If ! IsNonNegativeNumber(size) is false, throw a RangeError exception.
34                return Err(Exception::throw_range(
35                    ctx,
36                    "Size must be a finite, non-NaN, non-negative number.",
37                ));
38            }
39            Some(size) => size,
40        };
41
42        // If size is +∞, throw a RangeError exception.
43        if size.is_infinite() {
44            return Err(Exception::throw_range(
45                ctx,
46                "Size must be a finite, non-NaN, non-negative number.",
47            ));
48        };
49
50        // Append a new value-with-size with value value and size size to container.[[queue]].
51        self.queue.push_back(ValueWithSize { value, size });
52
53        // Set container.[[queueTotalSize]] to container.[[queueTotalSize]] + size.
54        self.queue_total_size += size;
55
56        Ok(())
57    }
58
59    pub fn dequeue_value(&mut self) -> Value<'js> {
60        // Let valueWithSize be container.[[queue]][0].
61        // Remove valueWithSize from container.[[queue]].
62        let value_with_size = self
63            .queue
64            .pop_front()
65            .expect("DequeueValue called with empty queue");
66        // Set container.[[queueTotalSize]] to container.[[queueTotalSize]] − valueWithSize’s size.
67        self.queue_total_size -= value_with_size.size;
68        // If container.[[queueTotalSize]] < 0, set container.[[queueTotalSize]] to 0. (This can occur due to rounding errors.)
69        if self.queue_total_size < 0.0 {
70            self.queue_total_size = 0.0
71        }
72        value_with_size.value
73    }
74
75    pub fn reset_queue(&mut self) {
76        // Set container.[[queue]] to a new empty list.
77        self.queue.clear();
78        // Set container.[[queueTotalSize]] to 0.
79        self.queue_total_size = 0.0;
80    }
81}
82
83#[derive(JsLifetime, Trace, Clone)]
84pub struct ValueWithSize<'js> {
85    pub value: Value<'js>,
86    size: f64,
87}
88
89fn is_non_negative_number(value: SizeValue<'_>) -> Option<f64> {
90    // If Type(v) is not Number, return false.
91    let number = value.as_number()?;
92    // If v is NaN, return false.
93    if number.is_nan() {
94        return None;
95    }
96
97    // If v < 0, return false.
98    if number < 0.0 {
99        return None;
100    }
101
102    // Return true.
103    Some(number)
104}