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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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/. */
#include "mozilla/dom/ReadableStreamBYOBReader.h"
#include "ReadIntoRequest.h"
#include "js/ArrayBuffer.h"
#include "js/experimental/TypedData.h"
#include "mozilla/dom/ReadableStreamBYOBReader.h"
#include "mozilla/dom/ReadableStream.h"
#include "mozilla/dom/ReadableStreamBYOBReaderBinding.h"
#include "mozilla/dom/ReadableStreamGenericReader.h"
#include "mozilla/dom/RootedDictionary.h"
#include "nsCOMPtr.h"
#include "nsISupportsImpl.h"
// Temporary Includes
#include "mozilla/dom/ReadableByteStreamController.h"
#include "mozilla/dom/ReadableStreamBYOBRequest.h"
namespace mozilla::dom {
using namespace streams_abstract;
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_INHERITED(ReadableStreamBYOBReader,
ReadableStreamGenericReader,
mReadIntoRequests)
NS_IMPL_ADDREF_INHERITED(ReadableStreamBYOBReader, ReadableStreamGenericReader)
NS_IMPL_RELEASE_INHERITED(ReadableStreamBYOBReader, ReadableStreamGenericReader)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ReadableStreamBYOBReader)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_END_INHERITING(ReadableStreamGenericReader)
ReadableStreamBYOBReader::ReadableStreamBYOBReader(nsISupports* aGlobal)
: ReadableStreamGenericReader(do_QueryInterface(aGlobal)) {}
JSObject* ReadableStreamBYOBReader::WrapObject(
JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
return ReadableStreamBYOBReader_Binding::Wrap(aCx, this, aGivenProto);
}
// https://streams.spec.whatwg.org/#set-up-readable-stream-byob-reader
void SetUpReadableStreamBYOBReader(ReadableStreamBYOBReader* reader,
ReadableStream& stream, ErrorResult& rv) {
// Step 1. If !IsReadableStreamLocked(stream) is true, throw a TypeError
// exception.
if (IsReadableStreamLocked(&stream)) {
rv.ThrowTypeError("Trying to read locked stream");
return;
}
// Step 2. If stream.[[controller]] does not implement
// ReadableByteStreamController, throw a TypeError exception.
if (!stream.Controller()->IsByte()) {
rv.ThrowTypeError("Trying to read with incompatible controller");
return;
}
// Step 3. Perform ! ReadableStreamReaderGenericInitialize(reader, stream).
ReadableStreamReaderGenericInitialize(reader, &stream);
// Step 4. Set reader.[[readIntoRequests]] to a new empty list.
reader->ReadIntoRequests().clear();
}
// https://streams.spec.whatwg.org/#byob-reader-constructor
/* static */ already_AddRefed<ReadableStreamBYOBReader>
ReadableStreamBYOBReader::Constructor(const GlobalObject& global,
ReadableStream& stream, ErrorResult& rv) {
nsCOMPtr<nsIGlobalObject> globalObject =
do_QueryInterface(global.GetAsSupports());
RefPtr<ReadableStreamBYOBReader> reader =
new ReadableStreamBYOBReader(globalObject);
// Step 1.
SetUpReadableStreamBYOBReader(reader, stream, rv);
if (rv.Failed()) {
return nullptr;
}
return reader.forget();
}
struct Read_ReadIntoRequest final : public ReadIntoRequest {
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(Read_ReadIntoRequest,
ReadIntoRequest)
RefPtr<Promise> mPromise;
explicit Read_ReadIntoRequest(Promise* aPromise) : mPromise(aPromise) {}
void ChunkSteps(JSContext* aCx, JS::Handle<JS::Value> aChunk,
ErrorResult& aRv) override {
MOZ_ASSERT(aChunk.isObject());
// https://streams.spec.whatwg.org/#byob-reader-read Step 6.
//
// chunk steps, given chunk:
// Resolve promise with «[ "value" → chunk, "done" → false ]».
// We need to wrap this as the chunk could have come from
// another compartment.
JS::Rooted<JSObject*> chunk(aCx, &aChunk.toObject());
if (!JS_WrapObject(aCx, &chunk)) {
aRv.StealExceptionFromJSContext(aCx);
return;
}
RootedDictionary<ReadableStreamReadResult> result(aCx);
result.mValue = aChunk;
result.mDone.Construct(false);
mPromise->MaybeResolve(result);
}
void CloseSteps(JSContext* aCx, JS::Handle<JS::Value> aChunk,
ErrorResult& aRv) override {
MOZ_ASSERT(aChunk.isObject() || aChunk.isUndefined());
// https://streams.spec.whatwg.org/#byob-reader-read Step 6.
//
// close steps, given chunk:
// Resolve promise with «[ "value" → chunk, "done" → true ]».
RootedDictionary<ReadableStreamReadResult> result(aCx);
if (aChunk.isObject()) {
// We need to wrap this as the chunk could have come from
// another compartment.
JS::Rooted<JSObject*> chunk(aCx, &aChunk.toObject());
if (!JS_WrapObject(aCx, &chunk)) {
aRv.StealExceptionFromJSContext(aCx);
return;
}
result.mValue = aChunk;
}
result.mDone.Construct(true);
mPromise->MaybeResolve(result);
}
void ErrorSteps(JSContext* aCx, JS::Handle<JS::Value> e,
ErrorResult& aRv) override {
// https://streams.spec.whatwg.org/#byob-reader-read Step 6.
//
// error steps, given e:
// Reject promise with e.
mPromise->MaybeReject(e);
}
protected:
~Read_ReadIntoRequest() override = default;
};
NS_IMPL_CYCLE_COLLECTION(ReadIntoRequest)
NS_IMPL_CYCLE_COLLECTING_ADDREF(ReadIntoRequest)
NS_IMPL_CYCLE_COLLECTING_RELEASE(ReadIntoRequest)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ReadIntoRequest)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTION_INHERITED(Read_ReadIntoRequest, ReadIntoRequest,
mPromise)
NS_IMPL_ADDREF_INHERITED(Read_ReadIntoRequest, ReadIntoRequest)
NS_IMPL_RELEASE_INHERITED(Read_ReadIntoRequest, ReadIntoRequest)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Read_ReadIntoRequest)
NS_INTERFACE_MAP_END_INHERITING(ReadIntoRequest)
namespace streams_abstract {
// https://streams.spec.whatwg.org/#readable-stream-byob-reader-read
void ReadableStreamBYOBReaderRead(JSContext* aCx,
ReadableStreamBYOBReader* aReader,
JS::Handle<JSObject*> aView,
ReadIntoRequest* aReadIntoRequest,
ErrorResult& aRv) {
// Step 1.Let stream be reader.[[stream]].
ReadableStream* stream = aReader->GetStream();
// Step 2. Assert: stream is not undefined.
MOZ_ASSERT(stream);
// Step 3. Set stream.[[disturbed]] to true.
stream->SetDisturbed(true);
// Step 4. If stream.[[state]] is "errored", perform readIntoRequest’s error
// steps given stream.[[storedError]].
if (stream->State() == ReadableStream::ReaderState::Errored) {
JS::Rooted<JS::Value> error(aCx, stream->StoredError());
aReadIntoRequest->ErrorSteps(aCx, error, aRv);
return;
}
// Step 5. Otherwise, perform
// !ReadableByteStreamControllerPullInto(stream.[[controller]], view,
// readIntoRequest).
MOZ_ASSERT(stream->Controller()->IsByte());
RefPtr<ReadableByteStreamController> controller(
stream->Controller()->AsByte());
ReadableByteStreamControllerPullInto(aCx, controller, aView, aReadIntoRequest,
aRv);
}
} // namespace streams_abstract
// https://streams.spec.whatwg.org/#byob-reader-read
already_AddRefed<Promise> ReadableStreamBYOBReader::Read(
const ArrayBufferView& aArray, ErrorResult& aRv) {
AutoJSAPI jsapi;
if (!jsapi.Init(GetParentObject())) {
aRv.ThrowUnknownError("Internal error");
return nullptr;
}
JSContext* cx = jsapi.cx();
JS::Rooted<JSObject*> view(cx, aArray.Obj());
// Step 1. If view.[[ByteLength]] is 0, return a promise rejected with a
// TypeError exception.
if (JS_GetArrayBufferViewByteLength(view) == 0) {
// Binding code should convert this thrown value into a rejected promise.
aRv.ThrowTypeError("Zero Length View");
return nullptr;
}
// Step 2. If view.[[ViewedArrayBuffer]].[[ArrayBufferByteLength]] is 0,
// return a promise rejected with a TypeError exception.
bool isSharedMemory;
JS::Rooted<JSObject*> viewedArrayBuffer(
cx, JS_GetArrayBufferViewBuffer(cx, view, &isSharedMemory));
if (!viewedArrayBuffer) {
aRv.StealExceptionFromJSContext(cx);
return nullptr;
}
if (JS::GetArrayBufferByteLength(viewedArrayBuffer) == 0) {
aRv.ThrowTypeError("zero length viewed buffer");
return nullptr;
}
// Step 3. If ! IsDetachedBuffer(view.[[ViewedArrayBuffer]]) is true, return a
// promise rejected with a TypeError exception.
if (JS::IsDetachedArrayBufferObject(viewedArrayBuffer)) {
aRv.ThrowTypeError("Detached Buffer");
return nullptr;
}
// Step 4. If this.[[stream]] is undefined, return a promise rejected with a
// TypeError exception.
if (!GetStream()) {
aRv.ThrowTypeError("Reader has undefined stream");
return nullptr;
}
// Step 5.
RefPtr<Promise> promise = Promise::CreateInfallible(GetParentObject());
// Step 6. Let readIntoRequest be a new read-into request with the following
// items:
RefPtr<ReadIntoRequest> readIntoRequest = new Read_ReadIntoRequest(promise);
// Step 7. Perform ! ReadableStreamBYOBReaderRead(this, view,
// readIntoRequest).
ReadableStreamBYOBReaderRead(cx, this, view, readIntoRequest, aRv);
if (aRv.Failed()) {
return nullptr;
}
// Step 8. Return promise.
return promise.forget();
}
namespace streams_abstract {
// https://streams.spec.whatwg.org/#abstract-opdef-readablestreambyobreadererrorreadintorequests
void ReadableStreamBYOBReaderErrorReadIntoRequests(
JSContext* aCx, ReadableStreamBYOBReader* aReader,
JS::Handle<JS::Value> aError, ErrorResult& aRv) {
// Step 1. Let readIntoRequests be reader.[[readIntoRequests]].
LinkedList<RefPtr<ReadIntoRequest>> readIntoRequests =
std::move(aReader->ReadIntoRequests());
// Step 2. Set reader.[[readIntoRequests]] to a new empty list.
// Note: The std::move already cleared this anyway.
aReader->ReadIntoRequests().clear();
// Step 3. For each readIntoRequest of readIntoRequests,
while (RefPtr<ReadIntoRequest> readIntoRequest =
readIntoRequests.popFirst()) {
// Step 3.1. Perform readIntoRequest’s error steps, given e.
readIntoRequest->ErrorSteps(aCx, aError, aRv);
if (aRv.Failed()) {
return;
}
}
}
// https://streams.spec.whatwg.org/#abstract-opdef-readablestreambyobreaderrelease
void ReadableStreamBYOBReaderRelease(JSContext* aCx,
ReadableStreamBYOBReader* aReader,
ErrorResult& aRv) {
// Step 1. Perform ! ReadableStreamReaderGenericRelease(reader).
ReadableStreamReaderGenericRelease(aReader, aRv);
if (aRv.Failed()) {
return;
}
// Step 2. Let e be a new TypeError exception.
ErrorResult rv;
rv.ThrowTypeError("Releasing lock");
JS::Rooted<JS::Value> error(aCx);
MOZ_ALWAYS_TRUE(ToJSValue(aCx, std::move(rv), &error));
// Step 3. Perform ! ReadableStreamBYOBReaderErrorReadIntoRequests(reader, e).
ReadableStreamBYOBReaderErrorReadIntoRequests(aCx, aReader, error, aRv);
}
} // namespace streams_abstract
// https://streams.spec.whatwg.org/#byob-reader-release-lock
void ReadableStreamBYOBReader::ReleaseLock(ErrorResult& aRv) {
// Step 1. If this.[[stream]] is undefined, return.
if (!mStream) {
return;
}
AutoJSAPI jsapi;
if (!jsapi.Init(mGlobal)) {
return aRv.ThrowUnknownError("Internal error");
}
JSContext* cx = jsapi.cx();
// Step 2. Perform ! ReadableStreamBYOBReaderRelease(this).
RefPtr<ReadableStreamBYOBReader> thisRefPtr = this;
ReadableStreamBYOBReaderRelease(cx, thisRefPtr, aRv);
}
namespace streams_abstract {
// https://streams.spec.whatwg.org/#acquire-readable-stream-byob-reader
already_AddRefed<ReadableStreamBYOBReader> AcquireReadableStreamBYOBReader(
ReadableStream* aStream, ErrorResult& aRv) {
// Step 1. Let reader be a new ReadableStreamBYOBReader.
RefPtr<ReadableStreamBYOBReader> reader =
new ReadableStreamBYOBReader(aStream->GetParentObject());
// Step 2. Perform ? SetUpReadableStreamBYOBReader(reader, stream).
SetUpReadableStreamBYOBReader(reader, *aStream, aRv);
if (aRv.Failed()) {
return nullptr;
}
// Step 3. Return reader.
return reader.forget();
}
} // namespace streams_abstract
} // namespace mozilla::dom
|