Bug 1769952 - Fix running raptor on a Win10-64 VM r=sparky
[gecko.git] / dom / streams / QueueWithSizes.h
blobe326b58ddf9aa9f6509378d3c07cab997c5744e2
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"
11 #include "js/Value.h"
12 #include "mozilla/ErrorResult.h"
13 #include "mozilla/FloatingPoint.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 (mozilla::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. Implicit by template instantiation.
59 // Step 2.
60 if (!IsNonNegativeNumber(aSize)) {
61 aRv.ThrowRangeError("invalid size");
62 return;
65 // Step 3.
66 if (mozilla::IsInfinite(aSize)) {
67 aRv.ThrowRangeError("Infinite queue size");
68 return;
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);
75 // Step 5.
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::MutableHandle<JS::Value> aResultValue) {
83 // Step 1. Implicit via template instantiation.
84 // Step 2.
85 MOZ_ASSERT(!aContainer->Queue().isEmpty());
87 // Step 3+4
88 // UniquePtr to ensure memory is freed.
89 UniquePtr<ValueWithSize> valueWithSize(aContainer->Queue().popFirst());
91 // Step 5.
92 aContainer->SetQueueTotalSize(aContainer->QueueTotalSize() -
93 valueWithSize->mSize);
95 // Step 6.
96 if (aContainer->QueueTotalSize() < 0) {
97 aContainer->SetQueueTotalSize(0);
100 // Step 7.
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
109 // slots.
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
124 // slots. (implicit)
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
135 #endif