summaryrefslogtreecommitdiffstats
path: root/js/src/builtin/streams/QueueWithSizes.h
blob: c781a46bfd45778eba8fec1f01c783b2b101f64f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* Queue-with-sizes operations. */

#ifndef builtin_streams_QueueWithSizes_h
#define builtin_streams_QueueWithSizes_h

#include "mozilla/Assertions.h"  // MOZ_ASSERT
#include "mozilla/Attributes.h"  // MOZ_MUST_USE

#include "jstypes.h"        // JS_PUBLIC_API
#include "js/RootingAPI.h"  // JS::{,Mutable}Handle
#include "js/Value.h"       // JS::Value
#include "vm/List.h"        // js::ListObject

struct JS_PUBLIC_API JSContext;

namespace js {

class StreamController;

/**
 * Streams spec, 6.2.1. DequeueValue ( container ) nothrow
 */
extern MOZ_MUST_USE bool DequeueValue(
    JSContext* cx, JS::Handle<StreamController*> unwrappedContainer,
    JS::MutableHandle<JS::Value> chunk);

/**
 * Streams spec, 6.2.1. DequeueValue ( container ) nothrow
 * when the dequeued value is ignored.
 */
extern void DequeueValue(StreamController* unwrappedContainer, JSContext* cx);

/**
 * Streams spec, 6.2.2. EnqueueValueWithSize ( container, value, size ) throws
 */
extern MOZ_MUST_USE bool EnqueueValueWithSize(
    JSContext* cx, JS::Handle<StreamController*> unwrappedContainer,
    JS::Handle<JS::Value> value, JS::Handle<JS::Value> sizeVal);

/**
 * Streams spec, 6.2.4. ResetQueue ( container ) nothrow
 */
extern MOZ_MUST_USE bool ResetQueue(
    JSContext* cx, JS::Handle<StreamController*> unwrappedContainer);

inline bool QueueIsEmpty(ListObject* unwrappedQueue) {
  if (unwrappedQueue->isEmpty()) {
    return true;
  }

  MOZ_ASSERT((unwrappedQueue->length() % 2) == 0,
             "queue-with-sizes must consist of (value, size) element pairs and "
             "so must have even length");
  return false;
}

}  // namespace js

#endif  // builtin_streams_QueueWithSizes_h