Bug 1850713: remove duplicated setting of early hint preloader id in `ScriptLoader...
[gecko.git] / dom / streams / QueueWithSizes.h
blob5a5bb4903642187d6ee4ae350761d74c182fb77a
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 <cmath>
11 #include "js/TypeDecls.h"
12 #include "js/Value.h"
13 #include "mozilla/ErrorResult.h"
14 #include "mozilla/UniquePtr.h"
15 #include "nsTArray.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::Handle<JS::Value> aValue, double aSize)
24 : mValue(aValue), mSize(aSize){};
26 JS::Heap<JS::Value> mValue;
27 double mSize = 0.0f;
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
38 // or `clear`ed.
39 using QueueWithSizes = AutoCleanLinkedList<ValueWithSize>;
41 // https://streams.spec.whatwg.org/#is-non-negative-number
42 inline bool IsNonNegativeNumber(double v) {
43 // Step 1. Implicit.
44 // Step 2.
45 if (std::isnan(v)) {
46 return false;
49 // Step 3.
50 return !(v < 0);
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,
57 ErrorResult& aRv) {
58 // Step 1. Assert: container has [[queue]] and [[queueTotalSize]] internal
59 // slots. (Implicit by template instantiation.)
60 // Step 2. If ! IsNonNegativeNumber(size) is false, throw a RangeError
61 // exception.
62 if (!IsNonNegativeNumber(aSize)) {
63 aRv.ThrowRangeError("invalid size");
64 return;
67 // Step 3. If size is +∞, throw a RangeError exception.
68 if (std::isinf(aSize)) {
69 aRv.ThrowRangeError("Infinite queue size");
70 return;
73 // Step 4. Append a new value-with-size with value value and size size to
74 // container.[[queue]].
75 // (See the comment on QueueWithSizes for the lifetime reasoning around this
76 // allocation.)
77 ValueWithSize* valueWithSize = new ValueWithSize(aValue, aSize);
78 aContainer->Queue().insertBack(valueWithSize);
79 // Step 5. Set container.[[queueTotalSize]] to container.[[queueTotalSize]] +
80 // size.
81 aContainer->SetQueueTotalSize(aContainer->QueueTotalSize() + aSize);
84 // https://streams.spec.whatwg.org/#dequeue-value
85 template <class QueueContainingClass>
86 inline void DequeueValue(QueueContainingClass aContainer,
87 JS::MutableHandle<JS::Value> aResultValue) {
88 // Step 1. Implicit via template instantiation.
89 // Step 2.
90 MOZ_ASSERT(!aContainer->Queue().isEmpty());
92 // Step 3+4
93 // UniquePtr to ensure memory is freed.
94 UniquePtr<ValueWithSize> valueWithSize(aContainer->Queue().popFirst());
96 // Step 5.
97 aContainer->SetQueueTotalSize(aContainer->QueueTotalSize() -
98 valueWithSize->mSize);
100 // Step 6.
101 if (aContainer->QueueTotalSize() < 0) {
102 aContainer->SetQueueTotalSize(0);
105 // Step 7.
106 aResultValue.set(valueWithSize->mValue);
109 // https://streams.spec.whatwg.org/#peek-queue-value
110 template <class QueueContainingClass>
111 inline void PeekQueueValue(QueueContainingClass aContainer,
112 JS::MutableHandle<JS::Value> aResultValue) {
113 // Step 1. Assert: container has [[queue]] and [[queueTotalSize]] internal
114 // slots.
115 // Step 2. Assert: container.[[queue]] is not empty.
116 MOZ_ASSERT(!aContainer->Queue().isEmpty());
118 // Step 3. Let valueWithSize be container.[[queue]][0].
119 ValueWithSize* valueWithSize = aContainer->Queue().getFirst();
121 // Step 4. Return valueWithSize’s value.
122 aResultValue.set(valueWithSize->mValue);
125 // https://streams.spec.whatwg.org/#reset-queue
126 template <class QueueContainingClass>
127 inline void ResetQueue(QueueContainingClass aContainer) {
128 // Step 1. Assert: container has [[queue]] and [[queueTotalSize]] internal
129 // slots. (implicit)
131 // Step 2. Set container.[[queue]] to a new empty list.
132 aContainer->Queue().clear();
134 // Step 3. Set container.[[queueTotalSize]] to 0.
135 aContainer->SetQueueTotalSize(0.0);
138 } // namespace mozilla::dom
140 #endif