1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_QueueWithSizes_h
8 #define mozilla_dom_QueueWithSizes_h
10 #include "js/TypeDecls.h"
12 #include "mozilla/ErrorResult.h"
13 #include "mozilla/FloatingPoint.h"
14 #include "mozilla/UniquePtr.h"
17 namespace mozilla::dom
{
19 // Note: Consumers of QueueWithSize need to ensure it is traced. See
20 // ReadableStreamDefaultController.cpp for an example.
22 struct ValueWithSize
: LinkedListElement
<ValueWithSize
> {
23 ValueWithSize(JS::HandleValue aValue
, double aSize
)
24 : mValue(aValue
), mSize(aSize
){};
26 JS::Heap
<JS::Value
> mValue
;
30 // This type is a little tricky lifetime wise: Despite the fact that we're
31 // talking about linked list of VaueWithSize, what is actually stored in the
32 // list are dynamically allocated ValueWithSize*. As a result, these have to be
33 // deleted. There are two ways this lifetime is managed:
35 // 1. In DequeueValue we pop the first element into a UniquePtr, so that it is
36 // correctly cleaned up at destruction time.
37 // 2. We use an AutoCleanLinkedList which will delete elements when destroyed
39 using QueueWithSizes
= AutoCleanLinkedList
<ValueWithSize
>;
41 // https://streams.spec.whatwg.org/#is-non-negative-number
42 inline bool IsNonNegativeNumber(double v
) {
45 if (mozilla::IsNaN(v
)) {
53 // https://streams.spec.whatwg.org/#enqueue-value-with-size
54 template <class QueueContainingClass
>
55 inline void EnqueueValueWithSize(QueueContainingClass aContainer
,
56 JS::Handle
<JS::Value
> aValue
, double aSize
,
58 // Step 1. Implicit by template instantiation.
60 if (!IsNonNegativeNumber(aSize
)) {
61 aRv
.ThrowRangeError("invalid size");
66 if (mozilla::IsInfinite(aSize
)) {
67 aRv
.ThrowRangeError("Infinite queue size");
71 // Step 4. See the comment on QueueWithSizes for the lifetime reasoning
72 // around this allocation.
73 ValueWithSize
* valueWithSize
= new ValueWithSize(aValue
, aSize
);
74 aContainer
->Queue().insertBack(valueWithSize
);
76 aContainer
->SetQueueTotalSize(aContainer
->QueueTotalSize() + aSize
);
79 // https://streams.spec.whatwg.org/#dequeue-value
80 template <class QueueContainingClass
>
81 inline void DequeueValue(QueueContainingClass aContainer
,
82 JS::MutableHandleValue aResultValue
) {
83 // Step 1. Implicit via template instantiation.
85 MOZ_ASSERT(!aContainer
->Queue().isEmpty());
88 // UniquePtr to ensure memory is freed.
89 UniquePtr
<ValueWithSize
> valueWithSize(aContainer
->Queue().popFirst());
92 aContainer
->SetQueueTotalSize(aContainer
->QueueTotalSize() -
93 valueWithSize
->mSize
);
96 if (aContainer
->QueueTotalSize() < 0) {
97 aContainer
->SetQueueTotalSize(0);
101 aResultValue
.set(valueWithSize
->mValue
);
104 // https://streams.spec.whatwg.org/#peek-queue-value
105 template <class QueueContainingClass
>
106 inline void PeekQueueValue(QueueContainingClass aContainer
,
107 JS::MutableHandle
<JS::Value
> aResultValue
) {
108 // Step 1. Assert: container has [[queue]] and [[queueTotalSize]] internal
110 // Step 2. Assert: container.[[queue]] is not empty.
111 MOZ_ASSERT(!aContainer
->Queue().isEmpty());
113 // Step 3. Let valueWithSize be container.[[queue]][0].
114 ValueWithSize
* valueWithSize
= aContainer
->Queue().getFirst();
116 // Step 4. Return valueWithSize’s value.
117 aResultValue
.set(valueWithSize
->mValue
);
120 // https://streams.spec.whatwg.org/#reset-queue
121 template <class QueueContainingClass
>
122 inline void ResetQueue(QueueContainingClass aContainer
) {
123 // Step 1. Assert: container has [[queue]] and [[queueTotalSize]] internal
126 // Step 2. Set container.[[queue]] to a new empty list.
127 aContainer
->Queue().clear();
129 // Step 3. Set container.[[queueTotalSize]] to 0.
130 aContainer
->SetQueueTotalSize(0.0);
133 } // namespace mozilla::dom