Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: third_party/WebKit/Source/core/streams/ReadableStream.js

Issue 2498983002: ReadableStream and WritableStream share strings (Closed)
Patch Set: Rebase to fix patch errors and fix typo Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 (function(global, binding, v8) { 5 (function(global, binding, v8) {
6 'use strict'; 6 'use strict';
7 7
8 const _reader = v8.createPrivateSymbol('[[reader]]'); 8 const _reader = v8.createPrivateSymbol('[[reader]]');
9 const _storedError = v8.createPrivateSymbol('[[storedError]]'); 9 const _storedError = v8.createPrivateSymbol('[[storedError]]');
10 const _controller = v8.createPrivateSymbol('[[controller]]'); 10 const _controller = v8.createPrivateSymbol('[[controller]]');
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 55
56 const Number = global.Number; 56 const Number = global.Number;
57 const Number_isNaN = Number.isNaN; 57 const Number_isNaN = Number.isNaN;
58 const Number_isFinite = Number.isFinite; 58 const Number_isFinite = Number.isFinite;
59 59
60 const Promise = global.Promise; 60 const Promise = global.Promise;
61 const thenPromise = v8.uncurryThis(Promise.prototype.then); 61 const thenPromise = v8.uncurryThis(Promise.prototype.then);
62 const Promise_resolve = v8.simpleBind(Promise.resolve, Promise); 62 const Promise_resolve = v8.simpleBind(Promise.resolve, Promise);
63 const Promise_reject = v8.simpleBind(Promise.reject, Promise); 63 const Promise_reject = v8.simpleBind(Promise.reject, Promise);
64 64
65 const errIllegalInvocation = 'Illegal invocation'; 65 const streamErrors = binding.streamErrors;
66 const errIllegalConstructor = 'Illegal constructor';
67 const errCancelLockedStream = 66 const errCancelLockedStream =
68 'Cannot cancel a readable stream that is locked to a reader'; 67 'Cannot cancel a readable stream that is locked to a reader';
69 const errEnqueueCloseRequestedStream = 68 const errEnqueueCloseRequestedStream =
70 'Cannot enqueue a chunk into a readable stream that is closed or has been requested to be closed'; 69 'Cannot enqueue a chunk into a readable stream that is closed or has been requested to be closed';
71 const errCancelReleasedReader = 70 const errCancelReleasedReader =
72 'This readable stream reader has been released and cannot be used to cance l its previous owner stream'; 71 'This readable stream reader has been released and cannot be used to cance l its previous owner stream';
73 const errReadReleasedReader = 72 const errReadReleasedReader =
74 'This readable stream reader has been released and cannot be used to read from its previous owner stream'; 73 'This readable stream reader has been released and cannot be used to read from its previous owner stream';
75 const errCloseCloseRequestedStream = 74 const errCloseCloseRequestedStream =
76 'Cannot close a readable stream that has already been requested to be clos ed'; 75 'Cannot close a readable stream that has already been requested to be clos ed';
77 const errEnqueueClosedStream = 'Cannot enqueue a chunk into a closed readable stream'; 76 const errEnqueueClosedStream = 'Cannot enqueue a chunk into a closed readable stream';
78 const errEnqueueErroredStream = 'Cannot enqueue a chunk into an errored readab le stream'; 77 const errEnqueueErroredStream = 'Cannot enqueue a chunk into an errored readab le stream';
79 const errCloseClosedStream = 'Cannot close a closed readable stream'; 78 const errCloseClosedStream = 'Cannot close a closed readable stream';
80 const errCloseErroredStream = 'Cannot close an errored readable stream'; 79 const errCloseErroredStream = 'Cannot close an errored readable stream';
81 const errErrorClosedStream = 'Cannot error a close readable stream'; 80 const errErrorClosedStream = 'Cannot error a close readable stream';
82 const errErrorErroredStream = 81 const errErrorErroredStream =
83 'Cannot error a readable stream that is already errored'; 82 'Cannot error a readable stream that is already errored';
84 const errGetReaderNotByteStream = 'This readable stream does not support BYOB readers'; 83 const errGetReaderNotByteStream = 'This readable stream does not support BYOB readers';
85 const errGetReaderBadMode = 'Invalid reader mode given: expected undefined or "byob"'; 84 const errGetReaderBadMode = 'Invalid reader mode given: expected undefined or "byob"';
86 const errReaderConstructorBadArgument = 85 const errReaderConstructorBadArgument =
87 'ReadableStreamReader constructor argument is not a readable stream'; 86 'ReadableStreamReader constructor argument is not a readable stream';
88 const errReaderConstructorStreamAlreadyLocked = 87 const errReaderConstructorStreamAlreadyLocked =
89 'ReadableStreamReader constructor can only accept readable streams that ar e not yet locked to a reader'; 88 'ReadableStreamReader constructor can only accept readable streams that ar e not yet locked to a reader';
90 const errReleaseReaderWithPendingRead = 89 const errReleaseReaderWithPendingRead =
91 'Cannot release a readable stream reader when it still has outstanding rea d() calls that have not yet settled'; 90 'Cannot release a readable stream reader when it still has outstanding rea d() calls that have not yet settled';
92 const errReleasedReaderClosedPromise = 91 const errReleasedReaderClosedPromise =
93 'This readable stream reader has been released and cannot be used to monit or the stream\'s state'; 92 'This readable stream reader has been released and cannot be used to monit or the stream\'s state';
94 const errInvalidSize = 93
95 'The return value of a queuing strategy\'s size function must be a finite, non-NaN, non-negative number';
96 const errSizeNotAFunction =
97 'A queuing strategy\'s size property must be a function';
98 const errInvalidHWM =
99 'A queueing strategy\'s highWaterMark property must be a nonnegative, non- NaN number';
100 const errTmplMustBeFunctionOrUndefined = name => 94 const errTmplMustBeFunctionOrUndefined = name =>
101 `${name} must be a function or undefined`; 95 `${name} must be a function or undefined`;
102 96
103 class ReadableStream { 97 class ReadableStream {
104 constructor() { 98 constructor() {
105 // TODO(domenic): when V8 gets default parameters and destructuring, all 99 // TODO(domenic): when V8 gets default parameters and destructuring, all
106 // this can be cleaned up. 100 // this can be cleaned up.
107 const underlyingSource = arguments[0] === undefined ? {} : arguments[0]; 101 const underlyingSource = arguments[0] === undefined ? {} : arguments[0];
108 const strategy = arguments[1] === undefined ? {} : arguments[1]; 102 const strategy = arguments[1] === undefined ? {} : arguments[1];
109 const size = strategy.size; 103 const size = strategy.size;
(...skipping 12 matching lines...) Expand all
122 // methods will disregard their controller argument in such situations 116 // methods will disregard their controller argument in such situations
123 // (but see below). 117 // (but see below).
124 118
125 this[_controller] = undefined; 119 this[_controller] = undefined;
126 120
127 const type = underlyingSource.type; 121 const type = underlyingSource.type;
128 const typeString = String(type); 122 const typeString = String(type);
129 if (typeString === 'bytes') { 123 if (typeString === 'bytes') {
130 throw new RangeError('bytes type is not yet implemented'); 124 throw new RangeError('bytes type is not yet implemented');
131 } else if (type !== undefined) { 125 } else if (type !== undefined) {
132 throw new RangeError('Invalid type is specified'); 126 throw new RangeError(streamErrors.invalidType);
133 } 127 }
134 128
135 this[_controller] = 129 this[_controller] =
136 new ReadableStreamDefaultController(this, underlyingSource, size, high WaterMark, arguments[2] === createWithExternalControllerSentinel); 130 new ReadableStreamDefaultController(this, underlyingSource, size, high WaterMark, arguments[2] === createWithExternalControllerSentinel);
137 } 131 }
138 132
139 get locked() { 133 get locked() {
140 if (IsReadableStream(this) === false) { 134 if (IsReadableStream(this) === false) {
141 throw new TypeError(errIllegalInvocation); 135 throw new TypeError(streamErrors.illegalInvocation);
142 } 136 }
143 137
144 return IsReadableStreamLocked(this); 138 return IsReadableStreamLocked(this);
145 } 139 }
146 140
147 cancel(reason) { 141 cancel(reason) {
148 if (IsReadableStream(this) === false) { 142 if (IsReadableStream(this) === false) {
149 return Promise_reject(new TypeError(errIllegalInvocation)); 143 return Promise_reject(new TypeError(streamErrors.illegalInvocation));
150 } 144 }
151 145
152 if (IsReadableStreamLocked(this) === true) { 146 if (IsReadableStreamLocked(this) === true) {
153 return Promise_reject(new TypeError(errCancelLockedStream)); 147 return Promise_reject(new TypeError(errCancelLockedStream));
154 } 148 }
155 149
156 return ReadableStreamCancel(this, reason); 150 return ReadableStreamCancel(this, reason);
157 } 151 }
158 152
159 getReader({ mode } = {}) { 153 getReader({ mode } = {}) {
160 if (IsReadableStream(this) === false) { 154 if (IsReadableStream(this) === false) {
161 throw new TypeError(errIllegalInvocation); 155 throw new TypeError(streamErrors.illegalInvocation);
162 } 156 }
163 157
164 if (mode === 'byob') { 158 if (mode === 'byob') {
165 // TODO(ricea): When BYOB readers are supported: 159 // TODO(ricea): When BYOB readers are supported:
166 // 160 //
167 // a. If 161 // a. If
168 // ! IsReadableByteStreamController(this.[[_controller]]) 162 // ! IsReadableByteStreamController(this.[[_controller]])
169 // is false, throw a TypeError exception. 163 // is false, throw a TypeError exception.
170 // b. Return ? AcquireReadableStreamBYOBReader(this). 164 // b. Return ? AcquireReadableStreamBYOBReader(this).
171 throw new TypeError(errGetReaderNotByteStream); 165 throw new TypeError(errGetReaderNotByteStream);
172 } 166 }
173 167
174 if (mode === undefined) { 168 if (mode === undefined) {
175 return AcquireReadableStreamDefaultReader(this); 169 return AcquireReadableStreamDefaultReader(this);
176 } 170 }
177 171
178 throw new RangeError(errGetReaderBadMode); 172 throw new RangeError(errGetReaderBadMode);
179 } 173 }
180 174
181 tee() { 175 tee() {
182 if (IsReadableStream(this) === false) { 176 if (IsReadableStream(this) === false) {
183 throw new TypeError(errIllegalInvocation); 177 throw new TypeError(streamErrors.illegalInvocation);
184 } 178 }
185 179
186 return ReadableStreamTee(this); 180 return ReadableStreamTee(this);
187 } 181 }
188 } 182 }
189 183
190 class ReadableStreamDefaultController { 184 class ReadableStreamDefaultController {
191 constructor(stream, underlyingSource, size, highWaterMark, isExternallyContr olled) { 185 constructor(stream, underlyingSource, size, highWaterMark, isExternallyContr olled) {
192 if (IsReadableStream(stream) === false) { 186 if (IsReadableStream(stream) === false) {
193 throw new TypeError(errIllegalConstructor); 187 throw new TypeError(streamErrors.illegalConstructor);
194 } 188 }
195 189
196 if (stream[_controller] !== undefined) { 190 if (stream[_controller] !== undefined) {
197 throw new TypeError(errIllegalConstructor); 191 throw new TypeError(streamErrors.illegalConstructor);
198 } 192 }
199 193
200 this[_controlledReadableStream] = stream; 194 this[_controlledReadableStream] = stream;
201 195
202 this[_underlyingSource] = underlyingSource; 196 this[_underlyingSource] = underlyingSource;
203 197
204 this[_queue] = new v8.InternalPackedArray(); 198 this[_queue] = new v8.InternalPackedArray();
205 this[_totalQueuedSize] = 0; 199 this[_totalQueuedSize] = 0;
206 200
207 this[_readableStreamDefaultControllerBits] = 0b0; 201 this[_readableStreamDefaultControllerBits] = 0b0;
(...skipping 17 matching lines...) Expand all
225 }, 219 },
226 r => { 220 r => {
227 if (ReadableStreamGetState(stream) === STATE_READABLE) { 221 if (ReadableStreamGetState(stream) === STATE_READABLE) {
228 ReadableStreamDefaultControllerError(controller, r); 222 ReadableStreamDefaultControllerError(controller, r);
229 } 223 }
230 }); 224 });
231 } 225 }
232 226
233 get desiredSize() { 227 get desiredSize() {
234 if (IsReadableStreamDefaultController(this) === false) { 228 if (IsReadableStreamDefaultController(this) === false) {
235 throw new TypeError(errIllegalInvocation); 229 throw new TypeError(streamErrors.illegalInvocation);
236 } 230 }
237 231
238 return ReadableStreamDefaultControllerGetDesiredSize(this); 232 return ReadableStreamDefaultControllerGetDesiredSize(this);
239 } 233 }
240 234
241 close() { 235 close() {
242 if (IsReadableStreamDefaultController(this) === false) { 236 if (IsReadableStreamDefaultController(this) === false) {
243 throw new TypeError(errIllegalInvocation); 237 throw new TypeError(streamErrors.illegalInvocation);
244 } 238 }
245 239
246 const stream = this[_controlledReadableStream]; 240 const stream = this[_controlledReadableStream];
247 241
248 if (this[_readableStreamDefaultControllerBits] & CLOSE_REQUESTED) { 242 if (this[_readableStreamDefaultControllerBits] & CLOSE_REQUESTED) {
249 throw new TypeError(errCloseCloseRequestedStream); 243 throw new TypeError(errCloseCloseRequestedStream);
250 } 244 }
251 245
252 const state = ReadableStreamGetState(stream); 246 const state = ReadableStreamGetState(stream);
253 if (state === STATE_ERRORED) { 247 if (state === STATE_ERRORED) {
254 throw new TypeError(errCloseErroredStream); 248 throw new TypeError(errCloseErroredStream);
255 } 249 }
256 if (state === STATE_CLOSED) { 250 if (state === STATE_CLOSED) {
257 throw new TypeError(errCloseClosedStream); 251 throw new TypeError(errCloseClosedStream);
258 } 252 }
259 253
260 return ReadableStreamDefaultControllerClose(this); 254 return ReadableStreamDefaultControllerClose(this);
261 } 255 }
262 256
263 enqueue(chunk) { 257 enqueue(chunk) {
264 if (IsReadableStreamDefaultController(this) === false) { 258 if (IsReadableStreamDefaultController(this) === false) {
265 throw new TypeError(errIllegalInvocation); 259 throw new TypeError(streamErrors.illegalInvocation);
266 } 260 }
267 261
268 const stream = this[_controlledReadableStream]; 262 const stream = this[_controlledReadableStream];
269 263
270 if (this[_readableStreamDefaultControllerBits] & CLOSE_REQUESTED) { 264 if (this[_readableStreamDefaultControllerBits] & CLOSE_REQUESTED) {
271 throw new TypeError(errEnqueueCloseRequestedStream); 265 throw new TypeError(errEnqueueCloseRequestedStream);
272 } 266 }
273 267
274 const state = ReadableStreamGetState(stream); 268 const state = ReadableStreamGetState(stream);
275 if (state === STATE_ERRORED) { 269 if (state === STATE_ERRORED) {
276 throw new TypeError(errEnqueueErroredStream); 270 throw new TypeError(errEnqueueErroredStream);
277 } 271 }
278 if (state === STATE_CLOSED) { 272 if (state === STATE_CLOSED) {
279 throw new TypeError(errEnqueueClosedStream); 273 throw new TypeError(errEnqueueClosedStream);
280 } 274 }
281 275
282 return ReadableStreamDefaultControllerEnqueue(this, chunk); 276 return ReadableStreamDefaultControllerEnqueue(this, chunk);
283 } 277 }
284 278
285 error(e) { 279 error(e) {
286 if (IsReadableStreamDefaultController(this) === false) { 280 if (IsReadableStreamDefaultController(this) === false) {
287 throw new TypeError(errIllegalInvocation); 281 throw new TypeError(streamErrors.illegalInvocation);
288 } 282 }
289 283
290 const stream = this[_controlledReadableStream]; 284 const stream = this[_controlledReadableStream];
291 285
292 const state = ReadableStreamGetState(stream); 286 const state = ReadableStreamGetState(stream);
293 if (state === STATE_ERRORED) { 287 if (state === STATE_ERRORED) {
294 throw new TypeError(errErrorErroredStream); 288 throw new TypeError(errErrorErroredStream);
295 } 289 }
296 if (state === STATE_CLOSED) { 290 if (state === STATE_CLOSED) {
297 throw new TypeError(errErrorClosedStream); 291 throw new TypeError(errErrorClosedStream);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 throw new TypeError(errReaderConstructorStreamAlreadyLocked); 338 throw new TypeError(errReaderConstructorStreamAlreadyLocked);
345 } 339 }
346 340
347 ReadableStreamReaderGenericInitialize(this, stream); 341 ReadableStreamReaderGenericInitialize(this, stream);
348 342
349 this[_readRequests] = new v8.InternalPackedArray(); 343 this[_readRequests] = new v8.InternalPackedArray();
350 } 344 }
351 345
352 get closed() { 346 get closed() {
353 if (IsReadableStreamDefaultReader(this) === false) { 347 if (IsReadableStreamDefaultReader(this) === false) {
354 return Promise_reject(new TypeError(errIllegalInvocation)); 348 return Promise_reject(new TypeError(streamErrors.illegalInvocation));
355 } 349 }
356 350
357 return this[_closedPromise]; 351 return this[_closedPromise];
358 } 352 }
359 353
360 cancel(reason) { 354 cancel(reason) {
361 if (IsReadableStreamDefaultReader(this) === false) { 355 if (IsReadableStreamDefaultReader(this) === false) {
362 return Promise_reject(new TypeError(errIllegalInvocation)); 356 return Promise_reject(new TypeError(streamErrors.illegalInvocation));
363 } 357 }
364 358
365 const stream = this[_ownerReadableStream]; 359 const stream = this[_ownerReadableStream];
366 if (stream === undefined) { 360 if (stream === undefined) {
367 return Promise_reject(new TypeError(errCancelReleasedReader)); 361 return Promise_reject(new TypeError(errCancelReleasedReader));
368 } 362 }
369 363
370 return ReadableStreamReaderGenericCancel(this, reason); 364 return ReadableStreamReaderGenericCancel(this, reason);
371 } 365 }
372 366
373 read() { 367 read() {
374 if (IsReadableStreamDefaultReader(this) === false) { 368 if (IsReadableStreamDefaultReader(this) === false) {
375 return Promise_reject(new TypeError(errIllegalInvocation)); 369 return Promise_reject(new TypeError(streamErrors.illegalInvocation));
376 } 370 }
377 371
378 if (this[_ownerReadableStream] === undefined) { 372 if (this[_ownerReadableStream] === undefined) {
379 return Promise_reject(new TypeError(errReadReleasedReader)); 373 return Promise_reject(new TypeError(errReadReleasedReader));
380 } 374 }
381 375
382 return ReadableStreamDefaultReaderRead(this); 376 return ReadableStreamDefaultReaderRead(this);
383 } 377 }
384 378
385 releaseLock() { 379 releaseLock() {
386 if (IsReadableStreamDefaultReader(this) === false) { 380 if (IsReadableStreamDefaultReader(this) === false) {
387 throw new TypeError(errIllegalInvocation); 381 throw new TypeError(streamErrors.illegalInvocation);
388 } 382 }
389 383
390 const stream = this[_ownerReadableStream]; 384 const stream = this[_ownerReadableStream];
391 if (stream === undefined) { 385 if (stream === undefined) {
392 return undefined; 386 return undefined;
393 } 387 }
394 388
395 if (this[_readRequests].length > 0) { 389 if (this[_readRequests].length > 0) {
396 throw new TypeError(errReleaseReaderWithPendingRead); 390 throw new TypeError(errReleaseReaderWithPendingRead);
397 } 391 }
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after
801 795
802 function DequeueValue(controller) { 796 function DequeueValue(controller) {
803 const result = controller[_queue].shift(); 797 const result = controller[_queue].shift();
804 controller[_totalQueuedSize] -= result.size; 798 controller[_totalQueuedSize] -= result.size;
805 return result.value; 799 return result.value;
806 } 800 }
807 801
808 function EnqueueValueWithSize(controller, value, size) { 802 function EnqueueValueWithSize(controller, value, size) {
809 size = Number(size); 803 size = Number(size);
810 if (Number_isNaN(size) || size === +Infinity || size < 0) { 804 if (Number_isNaN(size) || size === +Infinity || size < 0) {
811 throw new RangeError(errInvalidSize); 805 throw new RangeError(streamErrors.invalidSize);
812 } 806 }
813 807
814 controller[_totalQueuedSize] += size; 808 controller[_totalQueuedSize] += size;
815 controller[_queue].push({value, size}); 809 controller[_queue].push({value, size});
816 } 810 }
817 811
818 function GetTotalQueueSize(controller) { return controller[_totalQueuedSize]; } 812 function GetTotalQueueSize(controller) { return controller[_totalQueuedSize]; }
819 813
820 // 814 //
821 // Other helpers 815 // Other helpers
822 // 816 //
823 817
824 function ValidateAndNormalizeQueuingStrategy(size, highWaterMark) { 818 function ValidateAndNormalizeQueuingStrategy(size, highWaterMark) {
825 if (size !== undefined && typeof size !== 'function') { 819 if (size !== undefined && typeof size !== 'function') {
826 throw new TypeError(errSizeNotAFunction); 820 throw new TypeError(streamErrors.sizeNotAFunction);
827 } 821 }
828 822
829 highWaterMark = Number(highWaterMark); 823 highWaterMark = Number(highWaterMark);
830 if (Number_isNaN(highWaterMark) || highWaterMark < 0) { 824 if (Number_isNaN(highWaterMark)) {
831 throw new RangeError(errInvalidHWM); 825 throw new RangeError(streamErrors.errInvalidHWM);
826 }
827 if (highWaterMark < 0) {
828 throw new RangeError(streamErrors.invalidHWM);
832 } 829 }
833 830
834 return {size, highWaterMark}; 831 return {size, highWaterMark};
835 } 832 }
836 833
837 // Modified from InvokeOrNoop in spec 834 // Modified from InvokeOrNoop in spec
838 function CallOrNoop(O, P, arg, nameForError) { 835 function CallOrNoop(O, P, arg, nameForError) {
839 const method = O[P]; 836 const method = O[P];
840 if (method === undefined) { 837 if (method === undefined) {
841 return undefined; 838 return undefined;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
905 binding.ReadableStreamDefaultControllerGetDesiredSize = ReadableStreamDefaultC ontrollerGetDesiredSize; 902 binding.ReadableStreamDefaultControllerGetDesiredSize = ReadableStreamDefaultC ontrollerGetDesiredSize;
906 binding.ReadableStreamDefaultControllerEnqueue = ReadableStreamDefaultControll erEnqueue; 903 binding.ReadableStreamDefaultControllerEnqueue = ReadableStreamDefaultControll erEnqueue;
907 binding.ReadableStreamDefaultControllerError = ReadableStreamDefaultController Error; 904 binding.ReadableStreamDefaultControllerError = ReadableStreamDefaultController Error;
908 905
909 binding.createReadableStreamWithExternalController = 906 binding.createReadableStreamWithExternalController =
910 (underlyingSource, strategy) => { 907 (underlyingSource, strategy) => {
911 return new ReadableStream( 908 return new ReadableStream(
912 underlyingSource, strategy, createWithExternalControllerSentinel); 909 underlyingSource, strategy, createWithExternalControllerSentinel);
913 }; 910 };
914 }); 911 });
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/streams/CommonStrings.js ('k') | third_party/WebKit/Source/core/streams/WritableStream.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698