summaryrefslogtreecommitdiff
path: root/js/src/builtin/Stream.h
blob: 1bd62a87728b1876c832a4104a889e84ac7ae88a (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * 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/. */

#ifndef builtin_Stream_h
#define builtin_Stream_h

#include "builtin/Promise.h"
#include "vm/NativeObject.h"


namespace js {

class AutoSetNewObjectMetadata;

class ReadableStream : public NativeObject
{
  public:
    static ReadableStream* createDefaultStream(JSContext* cx, HandleValue underlyingSource,
                                               HandleValue size, HandleValue highWaterMark,
                                               HandleObject proto = nullptr);
    static ReadableStream* createByteStream(JSContext* cx, HandleValue underlyingSource,
                                            HandleValue highWaterMark,
                                            HandleObject proto = nullptr);
    static ReadableStream* createExternalSourceStream(JSContext* cx, void* underlyingSource,
                                                      uint8_t flags, HandleObject proto = nullptr);

    bool readable() const;
    bool closed() const;
    bool errored() const;
    bool disturbed() const;

    bool locked() const;

    void desiredSize(bool* hasSize, double* size) const;

    JS::ReadableStreamMode mode() const;

    [[nodiscard]] static bool close(JSContext* cx, Handle<ReadableStream*> stream);
    [[nodiscard]] static JSObject* cancel(JSContext* cx, Handle<ReadableStream*> stream,
                                          HandleValue reason);
    [[nodiscard]] static bool error(JSContext* cx, Handle<ReadableStream*> stream,
                                    HandleValue error);

    [[nodiscard]] static NativeObject* getReader(JSContext* cx, Handle<ReadableStream*> stream,
                                                 JS::ReadableStreamReaderMode mode);

    [[nodiscard]] static bool tee(JSContext* cx,
                                  Handle<ReadableStream*> stream, bool cloneForBranch2,
                                  MutableHandle<ReadableStream*> branch1Stream,
                                  MutableHandle<ReadableStream*> branch2Stream);

    [[nodiscard]] static bool enqueue(JSContext* cx, Handle<ReadableStream*> stream,
                                      HandleValue chunk);
    [[nodiscard]] static bool enqueueBuffer(JSContext* cx, Handle<ReadableStream*> stream,
                                            Handle<ArrayBufferObject*> chunk);
    [[nodiscard]] static bool getExternalSource(JSContext* cx, Handle<ReadableStream*> stream,
                                                void** source);
    void releaseExternalSource();
    uint8_t embeddingFlags() const;
    [[nodiscard]] static bool updateDataAvailableFromSource(JSContext* cx,
                                                            Handle<ReadableStream*> stream,
                                                            uint32_t availableData);

    enum State {
         Readable  = 1 << 0,
         Closed    = 1 << 1,
         Errored   = 1 << 2,
         Disturbed = 1 << 3
    };

  private:
    [[nodiscard]] static ReadableStream* createStream(JSContext* cx, HandleObject proto = nullptr);

  public:
    static bool constructor(JSContext* cx, unsigned argc, Value* vp);
    static const ClassSpec classSpec_;
    static const Class class_;
    static const ClassSpec protoClassSpec_;
    static const Class protoClass_;
};

class ReadableStreamDefaultReader : public NativeObject
{
  public:
    [[nodiscard]] static JSObject* read(JSContext* cx, Handle<ReadableStreamDefaultReader*> reader);

    static bool constructor(JSContext* cx, unsigned argc, Value* vp);
    static const ClassSpec classSpec_;
    static const Class class_;
    static const ClassSpec protoClassSpec_;
    static const Class protoClass_;
};

class ReadableStreamBYOBReader : public NativeObject
{
  public:
    [[nodiscard]] static JSObject* read(JSContext* cx, Handle<ReadableStreamBYOBReader*> reader,
                                        Handle<ArrayBufferViewObject*> view);

    static bool constructor(JSContext* cx, unsigned argc, Value* vp);
    static const ClassSpec classSpec_;
    static const Class class_;
    static const ClassSpec protoClassSpec_;
    static const Class protoClass_;
};

bool ReadableStreamReaderIsClosed(const JSObject* reader);

[[nodiscard]] bool ReadableStreamReaderCancel(JSContext* cx, HandleObject reader,
                                             HandleValue reason);

[[nodiscard]] bool ReadableStreamReaderReleaseLock(JSContext* cx, HandleObject reader);

class ReadableStreamDefaultController : public NativeObject
{
  public:
    static bool constructor(JSContext* cx, unsigned argc, Value* vp);
    static const ClassSpec classSpec_;
    static const Class class_;
    static const ClassSpec protoClassSpec_;
    static const Class protoClass_;
};

class ReadableByteStreamController : public NativeObject
{
  public:
    bool hasExternalSource();

    static bool constructor(JSContext* cx, unsigned argc, Value* vp);
    static const ClassSpec classSpec_;
    static const Class class_;
    static const ClassSpec protoClassSpec_;
    static const Class protoClass_;
};

class ReadableStreamBYOBRequest : public NativeObject
{
  public:
    static bool constructor(JSContext* cx, unsigned argc, Value* vp);
    static const ClassSpec classSpec_;
    static const Class class_;
    static const ClassSpec protoClassSpec_;
    static const Class protoClass_;
};

class ByteLengthQueuingStrategy : public NativeObject
{
  public:
    static bool constructor(JSContext* cx, unsigned argc, Value* vp);
    static const ClassSpec classSpec_;
    static const Class class_;
    static const ClassSpec protoClassSpec_;
    static const Class protoClass_;
};

class CountQueuingStrategy : public NativeObject
{
  public:
    static bool constructor(JSContext* cx, unsigned argc, Value* vp);
    static const ClassSpec classSpec_;
    static const Class class_;
    static const ClassSpec protoClassSpec_;
    static const Class protoClass_;
};

} // namespace js

#endif /* builtin_Stream_h */