Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 // Implementation of WritableStream for Blink. See | 5 // Implementation of WritableStream for Blink. See |
| 6 // https://streams.spec.whatwg.org/#ws. The implementation closely follows the | 6 // https://streams.spec.whatwg.org/#ws. The implementation closely follows the |
| 7 // standard, except where required for performance or integration with Blink. In | 7 // standard, except where required for performance or integration with Blink. In |
| 8 // particular, classes, methods and abstract operations are implemented in the | 8 // particular, classes, methods and abstract operations are implemented in the |
| 9 // same order as in the standard, to simplify side-by-side reading. | 9 // same order as in the standard, to simplify side-by-side reading. |
| 10 | 10 |
| 11 (function(global, binding, v8) { | 11 (function(global, binding, v8) { |
| 12 'use strict'; | 12 'use strict'; |
| 13 | 13 |
| 14 // Private symbols. These correspond to the internal slots in the standard. | 14 // Private symbols. These correspond to the internal slots in the standard. |
| 15 // "[[X]]" in the standard is spelt _X in this implementation. | 15 // "[[X]]" in the standard is spelt _X in this implementation. |
| 16 const _pendingWriteRequest = v8.createPrivateSymbol('[[pendingWriteRequest]]') ; | 16 |
| 17 const _pendingCloseRequest = v8.createPrivateSymbol('[[pendingCloseRequest]]') ; | 17 // TODO(ricea): Optimise [[closeRequest]] and [[inFlightCloseRequest]] into a |
| 18 const _pendingAbortRequest = v8.createPrivateSymbol('[[pendingAbortRequest]]') ; | 18 // single slot + a flag to say which one is set at the moment. |
| 19 const _state = v8.createPrivateSymbol('[[state]]'); | 19 const _closeRequest = v8.createPrivateSymbol('[[closeRequest]]'); |
| 20 const _inFlightWriteRequest = v8.createPrivateSymbol('[[inFlightWriteRequest]] '); | |
| 21 const _inFlightCloseRequest = v8.createPrivateSymbol('[[inFlightCloseRequest]] '); | |
| 22 const _pendingAbortRequest = | |
| 23 v8.createPrivateSymbol('[[pendingAbortRequest]]'); | |
| 24 // Flags and state are combined into a single integer field for efficiency. | |
| 25 const _stateAndFlags = v8.createPrivateSymbol('[[stateAndFlags]]'); | |
|
domenic
2017/04/05 07:02:42
Nit: I might give this the name "[[state]] and fla
Adam Rice
2017/04/10 01:04:04
Great idea, thanks.
| |
| 20 const _storedError = v8.createPrivateSymbol('[[storedError]]'); | 26 const _storedError = v8.createPrivateSymbol('[[storedError]]'); |
| 21 const _writer = v8.createPrivateSymbol('[[writer]]'); | |
| 22 const _writableStreamController = | 27 const _writableStreamController = |
| 23 v8.createPrivateSymbol('[[writableStreamController]]'); | 28 v8.createPrivateSymbol('[[writableStreamController]]'); |
| 29 const _writer = v8.createPrivateSymbol('[[writer]]'); | |
| 24 const _writeRequests = v8.createPrivateSymbol('[[writeRequests]]'); | 30 const _writeRequests = v8.createPrivateSymbol('[[writeRequests]]'); |
| 25 const _closedPromise = v8.createPrivateSymbol('[[closedPromise]]'); | 31 const _closedPromise = v8.createPrivateSymbol('[[closedPromise]]'); |
| 26 const _ownerWritableStream = | 32 const _ownerWritableStream = |
| 27 v8.createPrivateSymbol('[[ownerWritableStream]]'); | 33 v8.createPrivateSymbol('[[ownerWritableStream]]'); |
| 28 const _readyPromise = v8.createPrivateSymbol('[[readyPromise]]'); | 34 const _readyPromise = v8.createPrivateSymbol('[[readyPromise]]'); |
| 29 const _controlledWritableStream = | 35 const _controlledWritableStream = |
| 30 v8.createPrivateSymbol('[[controlledWritableStream]]'); | 36 v8.createPrivateSymbol('[[controlledWritableStream]]'); |
| 31 const _queue = v8.createPrivateSymbol('[[queue]]'); | 37 const _queue = v8.createPrivateSymbol('[[queue]]'); |
| 32 const _queueSize = v8.createPrivateSymbol('[[queueSize]]'); | 38 const _queueTotalSize = v8.createPrivateSymbol('[[queueTotalSize]]'); |
| 39 const _started = v8.createPrivateSymbol('[[started]]'); | |
| 33 const _strategyHWM = v8.createPrivateSymbol('[[strategyHWM]]'); | 40 const _strategyHWM = v8.createPrivateSymbol('[[strategyHWM]]'); |
| 34 const _strategySize = v8.createPrivateSymbol('[[strategySize]]'); | 41 const _strategySize = v8.createPrivateSymbol('[[strategySize]]'); |
| 35 const _underlyingSink = v8.createPrivateSymbol('[[underlyingSink]]'); | 42 const _underlyingSink = v8.createPrivateSymbol('[[underlyingSink]]'); |
| 36 | 43 |
| 37 // _defaultControllerFlags combines WritableStreamDefaultController's internal | 44 // Numeric encodings of states |
| 38 // slots [[started]], [[writing]], and [[inClose]] into a single bitmask for | 45 const WRITABLE = 0; |
| 39 // efficiency. | 46 const CLOSED = 1; |
| 40 const _defaultControllerFlags = | 47 const ERRORED = 2; |
| 41 v8.createPrivateSymbol('[[defaultControllerFlags]]'); | |
| 42 const FLAG_STARTED = 0b1; | |
| 43 const FLAG_WRITING = 0b10; | |
| 44 const FLAG_INCLOSE = 0b100; | |
| 45 | 48 |
| 46 // For efficiency, WritableStream [[state]] contains numeric values. | 49 // Mask to extract or assign states to _stateAndFlags |
| 47 const WRITABLE = 0; | 50 const STATE_MASK = 0xF; |
| 48 const CLOSING = 1; | 51 |
| 49 const CLOSED = 2; | 52 const BACKPRESSURE_FLAG = 0x10; |
| 50 const ERRORED = 3; | |
| 51 | 53 |
| 52 // Javascript functions. It is important to use these copies, as the ones on | 54 // Javascript functions. It is important to use these copies, as the ones on |
| 53 // the global object may have been overwritten. See "V8 Extras Design Doc", | 55 // the global object may have been overwritten. See "V8 Extras Design Doc", |
| 54 // section "Security Considerations". | 56 // section "Security Considerations". |
| 55 // https://docs.google.com/document/d/1AT5-T0aHGp7Lt29vPWFr2-qG8r3l9CByyvKwEuA 8Ec0/edit#heading=h.9yixony1a18r | 57 // https://docs.google.com/document/d/1AT5-T0aHGp7Lt29vPWFr2-qG8r3l9CByyvKwEuA 8Ec0/edit#heading=h.9yixony1a18r |
| 56 const undefined = global.undefined; | 58 const undefined = global.undefined; |
| 57 | 59 |
| 58 const defineProperty = global.Object.defineProperty; | 60 const defineProperty = global.Object.defineProperty; |
| 59 const hasOwnProperty = v8.uncurryThis(global.Object.hasOwnProperty); | 61 const hasOwnProperty = v8.uncurryThis(global.Object.hasOwnProperty); |
| 60 | 62 |
| 61 const Function_apply = v8.uncurryThis(global.Function.prototype.apply); | 63 const Function_apply = v8.uncurryThis(global.Function.prototype.apply); |
| 62 | 64 |
| 63 const TypeError = global.TypeError; | 65 const TypeError = global.TypeError; |
| 64 const RangeError = global.RangeError; | 66 const RangeError = global.RangeError; |
| 65 | 67 |
| 66 const Boolean = global.Boolean; | 68 const Boolean = global.Boolean; |
| 67 const Number = global.Number; | 69 const Number = global.Number; |
| 68 const Number_isNaN = Number.isNaN; | 70 const Number_isNaN = Number.isNaN; |
| 69 const Number_isFinite = Number.isFinite; | 71 const Number_isFinite = Number.isFinite; |
| 70 | 72 |
| 71 const Promise = global.Promise; | 73 const Promise = global.Promise; |
| 72 const thenPromise = v8.uncurryThis(Promise.prototype.then); | 74 const thenPromise = v8.uncurryThis(Promise.prototype.then); |
| 73 const Promise_resolve = v8.simpleBind(Promise.resolve, Promise); | 75 const Promise_resolve = v8.simpleBind(Promise.resolve, Promise); |
| 74 const Promise_reject = v8.simpleBind(Promise.reject, Promise); | 76 const Promise_reject = v8.simpleBind(Promise.reject, Promise); |
| 75 | 77 |
| 76 // User-visible strings. | 78 // User-visible strings. |
| 77 const streamErrors = binding.streamErrors; | 79 const streamErrors = binding.streamErrors; |
| 78 const errAbortLockedStream = 'Cannot abort a writable stream that is locked to a writer'; | 80 const errAbortLockedStream = 'Cannot abort a writable stream that is locked to a writer'; |
| 79 const errStreamAborted = 'The stream has been aborted'; | 81 const errStreamAborted = 'The stream has been aborted'; |
| 82 const errStreamAborting = 'The stream is in the process of being aborted'; | |
| 80 const errWriterLockReleasedPrefix = 'This writable stream writer has been rele ased and cannot be '; | 83 const errWriterLockReleasedPrefix = 'This writable stream writer has been rele ased and cannot be '; |
| 81 const errCloseCloseRequestedStream = | 84 const errCloseCloseRequestedStream = |
| 82 'Cannot close a writable stream that has already been requested to be clos ed'; | 85 'Cannot close a writable stream that has already been requested to be clos ed'; |
| 83 const errWriteCloseRequestedStream = | 86 const errWriteCloseRequestedStream = |
| 84 'Cannot write to a writable stream that is due to be closed'; | 87 'Cannot write to a writable stream that is due to be closed'; |
| 85 const templateErrorCannotActionOnStateStream = | 88 const templateErrorCannotActionOnStateStream = |
| 86 (action, state) => `Cannot ${action} a ${state} writable stream`; | 89 (action, state) => `Cannot ${action} a ${state} writable stream`; |
| 87 const errReleasedWriterClosedPromise = | 90 const errReleasedWriterClosedPromise = |
| 88 'This writable stream writer has been released and cannot be used to monit or the stream\'s state'; | 91 'This writable stream writer has been released and cannot be used to monit or the stream\'s state'; |
| 89 const templateErrorIsNotAFunction = f => `${f} is not a function`; | 92 const templateErrorIsNotAFunction = f => `${f} is not a function`; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 100 } | 103 } |
| 101 | 104 |
| 102 const stateNames = {[CLOSED]: 'closed', [ERRORED]: 'errored'}; | 105 const stateNames = {[CLOSED]: 'closed', [ERRORED]: 'errored'}; |
| 103 function createCannotActionOnStateStreamError(action, state) { | 106 function createCannotActionOnStateStreamError(action, state) { |
| 104 TEMP_ASSERT(stateNames[state] !== undefined, | 107 TEMP_ASSERT(stateNames[state] !== undefined, |
| 105 `name for state ${state} exists in stateNames`); | 108 `name for state ${state} exists in stateNames`); |
| 106 return new TypeError( | 109 return new TypeError( |
| 107 templateErrorCannotActionOnStateStream(action, stateNames[state])); | 110 templateErrorCannotActionOnStateStream(action, stateNames[state])); |
| 108 } | 111 } |
| 109 | 112 |
| 110 function setDefaultControllerFlag(controller, flag, value) { | |
| 111 let flags = controller[_defaultControllerFlags]; | |
| 112 if (value) { | |
| 113 flags = flags | flag; | |
| 114 } else { | |
| 115 flags = flags & ~flag; | |
| 116 } | |
| 117 controller[_defaultControllerFlags] = flags; | |
| 118 } | |
| 119 | |
| 120 function getDefaultControllerStartedFlag(controller) { | |
| 121 return Boolean(controller[_defaultControllerFlags] & FLAG_STARTED); | |
| 122 } | |
| 123 | |
| 124 function setDefaultControllerStartedFlag(controller, value) { | |
| 125 setDefaultControllerFlag(controller, FLAG_STARTED, value); | |
| 126 } | |
| 127 | |
| 128 function getDefaultControllerWritingFlag(controller) { | |
| 129 return Boolean(controller[_defaultControllerFlags] & FLAG_WRITING); | |
| 130 } | |
| 131 | |
| 132 function setDefaultControllerWritingFlag(controller, value) { | |
| 133 setDefaultControllerFlag(controller, FLAG_WRITING, value); | |
| 134 } | |
| 135 | |
| 136 function getDefaultControllerInCloseFlag(controller) { | |
| 137 return Boolean(controller[_defaultControllerFlags] & FLAG_INCLOSE); | |
| 138 } | |
| 139 | |
| 140 function setDefaultControllerInCloseFlag(controller, value) { | |
| 141 setDefaultControllerFlag(controller, FLAG_INCLOSE, value); | |
| 142 } | |
| 143 | |
| 144 function rejectPromises(queue, e) { | 113 function rejectPromises(queue, e) { |
| 145 queue.forEach(promise => v8.rejectPromise(promise, e)); | 114 queue.forEach(promise => v8.rejectPromise(promise, e)); |
| 146 } | 115 } |
| 147 | 116 |
| 148 // https://tc39.github.io/ecma262/#sec-ispropertykey | 117 // https://tc39.github.io/ecma262/#sec-ispropertykey |
| 149 // TODO(ricea): Remove this when the asserts using it are removed. | 118 // TODO(ricea): Remove this when the asserts using it are removed. |
| 150 function IsPropertyKey(argument) { | 119 function IsPropertyKey(argument) { |
| 151 return typeof argument === 'string' || typeof argument === 'symbol'; | 120 return typeof argument === 'string' || typeof argument === 'symbol'; |
| 152 } | 121 } |
| 153 | 122 |
| 154 // TODO(ricea): Remove all asserts once the implementation has stabilised. | 123 // TODO(ricea): Remove all asserts once the implementation has stabilised. |
| 155 function TEMP_ASSERT(predicate, message) { | 124 function TEMP_ASSERT(predicate, message) { |
| 156 if (predicate) { | 125 if (predicate) { |
| 157 return; | 126 return; |
| 158 } | 127 } |
| 159 v8.log(`Assertion failed: ${message}\n`); | 128 v8.log(`Assertion failed: ${message}\n`); |
| 160 v8.logStackTrace(); | 129 v8.logStackTrace(); |
| 161 class WritableStreamInternalError extends Error { | 130 class WritableStreamInternalError extends Error { |
| 162 constructor(message) { | 131 constructor(message) { |
| 163 super(message); | 132 super(message); |
| 164 } | 133 } |
| 165 } | 134 } |
| 166 throw new WritableStreamInternalError(message); | 135 throw new WritableStreamInternalError(message); |
| 167 } | 136 } |
| 168 | 137 |
| 169 class WritableStream { | 138 class WritableStream { |
| 170 constructor(underlyingSink = {}, { size, highWaterMark = 1 } = {}) { | 139 constructor(underlyingSink = {}, { size, highWaterMark = 1 } = {}) { |
| 171 this[_state] = WRITABLE; | 140 this[_stateAndFlags] = WRITABLE; |
| 172 this[_storedError] = undefined; | 141 this[_storedError] = undefined; |
| 173 this[_writer] = undefined; | 142 this[_writer] = undefined; |
| 174 this[_writableStreamController] = undefined; | 143 this[_writableStreamController] = undefined; |
| 175 this[_pendingWriteRequest] = undefined; | 144 this[_inFlightWriteRequest] = undefined; |
| 176 this[_pendingCloseRequest] = undefined; | 145 this[_closeRequest] = undefined; |
| 146 this[_inFlightCloseRequest] = undefined; | |
| 177 this[_pendingAbortRequest] = undefined; | 147 this[_pendingAbortRequest] = undefined; |
| 178 this[_writeRequests] = new binding.SimpleQueue(); | 148 this[_writeRequests] = new binding.SimpleQueue(); |
| 179 const type = underlyingSink.type; | 149 const type = underlyingSink.type; |
| 180 if (type !== undefined) { | 150 if (type !== undefined) { |
| 181 throw new RangeError(streamErrors.invalidType); | 151 throw new RangeError(streamErrors.invalidType); |
| 182 } | 152 } |
| 183 this[_writableStreamController] = | 153 this[_writableStreamController] = |
| 184 new WritableStreamDefaultController(this, underlyingSink, size, | 154 new WritableStreamDefaultController(this, underlyingSink, size, |
| 185 highWaterMark); | 155 highWaterMark); |
| 156 WritableStreamDefaultControllerStartSteps(this[_writableStreamController]) ; | |
| 186 } | 157 } |
| 187 | 158 |
| 188 get locked() { | 159 get locked() { |
| 189 if (!IsWritableStream(this)) { | 160 if (!IsWritableStream(this)) { |
| 190 throw new TypeError(streamErrors.illegalInvocation); | 161 throw new TypeError(streamErrors.illegalInvocation); |
| 191 } | 162 } |
| 192 return IsWritableStreamLocked(this); | 163 return IsWritableStreamLocked(this); |
| 193 } | 164 } |
| 194 | 165 |
| 195 abort(reason) { | 166 abort(reason) { |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 220 return hasOwnProperty(x, _writableStreamController); | 191 return hasOwnProperty(x, _writableStreamController); |
| 221 } | 192 } |
| 222 | 193 |
| 223 function IsWritableStreamLocked(stream) { | 194 function IsWritableStreamLocked(stream) { |
| 224 TEMP_ASSERT(IsWritableStream(stream), | 195 TEMP_ASSERT(IsWritableStream(stream), |
| 225 '! IsWritableStream(stream) is true.'); | 196 '! IsWritableStream(stream) is true.'); |
| 226 return stream[_writer] !== undefined; | 197 return stream[_writer] !== undefined; |
| 227 } | 198 } |
| 228 | 199 |
| 229 function WritableStreamAbort(stream, reason) { | 200 function WritableStreamAbort(stream, reason) { |
| 230 const state = stream[_state]; | 201 const state = stream[_stateAndFlags] & STATE_MASK; |
| 231 if (state === CLOSED) { | 202 if (state === CLOSED) { |
| 232 return Promise_resolve(undefined); | 203 return Promise_resolve(undefined); |
| 233 } | 204 } |
| 234 if (state === ERRORED) { | 205 if (state === ERRORED) { |
| 235 return Promise_reject(stream[_storedError]); | 206 return Promise_reject(stream[_storedError]); |
| 236 } | 207 } |
| 237 TEMP_ASSERT(state === WRITABLE || state === CLOSING, | 208 TEMP_ASSERT(state === WRITABLE, |
|
domenic
2017/04/05 07:02:42
We need to remember to remove the asserts before s
Adam Rice
2017/04/10 01:04:04
I have filed an issue as a reminder: https://bugs.
| |
| 238 'state is "writable" or "closing".'); | 209 'state is "writable".'); |
| 239 const error = new TypeError(errStreamAborted); | 210 const error = new TypeError(errStreamAborting); |
| 240 WritableStreamError(stream, error); | 211 if (stream[_pendingAbortRequest] !== undefined) { |
| 212 return Promise_reject(error); | |
| 213 } | |
| 241 | 214 |
| 242 const controller = stream[_writableStreamController]; | 215 const controller = stream[_writableStreamController]; |
| 243 TEMP_ASSERT(controller !== undefined, | 216 TEMP_ASSERT(controller !== undefined, |
| 244 'controller is not undefined'); | 217 'controller is not undefined'); |
| 218 if (!WritableStreamHasOperationMarkedInFlight(stream) && | |
| 219 controller[_started]) { | |
| 220 WritableStreamFinishAbort(stream); | |
| 221 return WritableStreamDefaultControllerAbortSteps(controller, reason); | |
| 222 } | |
| 223 const writer = stream[_writer]; | |
| 224 if (writer !== undefined) { | |
| 225 WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer, error); | |
| 226 } | |
| 227 const promise = v8.createPromise(); | |
| 228 stream[_pendingAbortRequest] = {promise, reason}; | |
| 229 return promise; | |
| 230 } | |
| 245 | 231 |
| 246 const isWriting = getDefaultControllerWritingFlag(controller); | 232 function WritableStreamError(stream, error) { |
| 247 if (isWriting || getDefaultControllerInCloseFlag(controller)) { | 233 stream[_stateAndFlags] = (stream[_stateAndFlags] & ~STATE_MASK) | ERRORED; |
|
domenic
2017/04/05 07:02:42
Did you want to abstract out these things like you
Adam Rice
2017/04/10 01:04:04
When I did this before I felt that it didn't impro
| |
| 248 const promise = v8.createPromise(); | 234 stream[_storedError] = error; |
| 249 stream[_pendingAbortRequest] = promise; | 235 WritableStreamDefaultControllerErrorSteps(stream[_writableStreamController]) ; |
| 236 if (stream[_pendingAbortRequest] === undefined) { | |
| 237 const writer = stream[_writer]; | |
| 238 if (writer !== undefined) { | |
| 239 WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer, error); | |
| 240 } | |
| 241 } | |
| 242 if (!WritableStreamHasOperationMarkedInFlight(stream)) { | |
| 243 WritableStreamRejectPromisesInReactionToError(stream); | |
| 244 } | |
| 245 } | |
| 250 | 246 |
| 251 if (isWriting) { | 247 function WritableStreamFinishAbort(stream) { |
| 252 return thenPromise(promise, () => { | 248 const error = new TypeError(errStreamAborted); |
| 253 return WritableStreamDefaultControllerAbort(controller, reason); | 249 WritableStreamError(stream, error); |
| 254 }); | |
| 255 } | |
| 256 return promise; | |
| 257 } | |
| 258 | |
| 259 return WritableStreamDefaultControllerAbort(controller, reason); | |
| 260 } | 250 } |
| 261 | 251 |
| 262 // Writable Stream Abstract Operations Used by Controllers | 252 // Writable Stream Abstract Operations Used by Controllers |
| 263 | 253 |
| 264 function WritableStreamAddWriteRequest(stream) { | 254 function WritableStreamAddWriteRequest(stream) { |
| 265 TEMP_ASSERT(IsWritableStreamLocked(stream), | 255 TEMP_ASSERT(IsWritableStreamLocked(stream), |
| 266 '! IsWritableStreamLocked(writer) is true.'); | 256 '! IsWritableStreamLocked(writer) is true.'); |
| 267 TEMP_ASSERT(stream[_state] === WRITABLE, | 257 TEMP_ASSERT((stream[_stateAndFlags] & STATE_MASK) === WRITABLE, |
| 268 'stream.[[state]] is "writable".'); | 258 'stream.[[state]] is "writable".'); |
| 269 const promise = v8.createPromise(); | 259 const promise = v8.createPromise(); |
| 270 stream[_writeRequests].push(promise); | 260 stream[_writeRequests].push(promise); |
| 271 return promise; | 261 return promise; |
| 272 } | 262 } |
| 273 | 263 |
| 274 function WritableStreamError(stream, e) { | 264 function WritableStreamFinishInFlightWrite(stream) { |
| 275 const oldState = stream[_state]; | 265 TEMP_ASSERT(stream[_inFlightWriteRequest] !== undefined, |
| 276 TEMP_ASSERT(oldState === WRITABLE || oldState === CLOSING, | 266 '_stream_.[[inFlightWriteRequest]] is not *undefined*.'); |
| 277 'oldState is "writable" or "closing".'); | 267 v8.resolvePromise(stream[_inFlightWriteRequest], undefined); |
| 268 stream[_inFlightWriteRequest] = undefined; | |
| 269 const state = stream[_stateAndFlags] & STATE_MASK; | |
| 270 if (state === ERRORED) { | |
| 271 WritableStreamFinishInFlightWriteInErroredState(stream); | |
| 272 return; | |
| 273 } | |
| 274 TEMP_ASSERT(state === WRITABLE, '_state_ is `"writable"`.'); | |
| 275 WritableStreamHandleAbortRequestIfPending(stream); | |
| 276 } | |
| 278 | 277 |
| 279 stream[_state] = ERRORED; | 278 function WritableStreamFinishInFlightWriteInErroredState(stream) { |
| 280 stream[_storedError] = e; | 279 WritableStreamRejectAbortRequestIfPending(stream); |
| 280 WritableStreamRejectPromisesInReactionToError(stream); | |
| 281 } | |
| 281 | 282 |
| 282 const controller = stream[_writableStreamController]; | 283 function WritableStreamFinishInFlightWriteWithError(stream, error) { |
| 283 if (controller === undefined || | 284 TEMP_ASSERT(stream[_inFlightWriteRequest] !== undefined, |
| 284 (!getDefaultControllerWritingFlag(controller) && | 285 '_stream_.[[inFlightWriteRequest]] is not *undefined*.'); |
| 285 !getDefaultControllerInCloseFlag(controller))) { | 286 v8.rejectPromise(stream[_inFlightWriteRequest], error); |
| 286 WritableStreamRejectPromisesInReactionToError(stream); | 287 stream[_inFlightWriteRequest] = undefined; |
| 288 const state = stream[_stateAndFlags] & STATE_MASK; | |
| 289 if (state === ERRORED) { | |
| 290 WritableStreamFinishInFlightWriteInErroredState(stream); | |
| 291 return; | |
| 287 } | 292 } |
| 293 TEMP_ASSERT(state === WRITABLE, '_state_ is `"writable"`.'); | |
| 294 WritableStreamError(stream, error); | |
| 295 WritableStreamRejectAbortRequestIfPending(stream); | |
| 296 } | |
| 288 | 297 |
| 298 function WritableStreamFinishInFlightClose(stream) { | |
| 299 TEMP_ASSERT(stream[_inFlightCloseRequest] !== undefined, | |
| 300 '_stream_.[[inFlightCloseRequest]] is not *undefined*.'); | |
| 301 v8.resolvePromise(stream[_inFlightCloseRequest], undefined); | |
| 302 stream[_inFlightCloseRequest] = undefined; | |
| 303 const state = stream[_stateAndFlags] & STATE_MASK; | |
| 304 if (state === ERRORED) { | |
| 305 WritableStreamFinishInFlightCloseInErroredState(stream); | |
| 306 return; | |
| 307 } | |
| 308 TEMP_ASSERT(state === WRITABLE, '_state_ is `"writable"`.'); | |
| 309 stream[_stateAndFlags] = (stream[_stateAndFlags] & ~STATE_MASK) | CLOSED; | |
| 289 const writer = stream[_writer]; | 310 const writer = stream[_writer]; |
| 290 if (writer !== undefined) { | 311 if (writer !== undefined) { |
| 291 if (oldState === WRITABLE && | 312 v8.resolvePromise(writer[_closedPromise], undefined); |
| 292 WritableStreamDefaultControllerGetBackpressure(controller) === true) { | 313 } |
| 293 v8.rejectPromise(writer[_readyPromise], e); | 314 if (stream[_pendingAbortRequest] !== undefined) { |
| 294 } else { | 315 v8.resolvePromise(stream[_pendingAbortRequest].promise, undefined); |
| 295 writer[_readyPromise] = Promise_reject(e); | 316 stream[_pendingAbortRequest] = undefined; |
| 296 } | |
| 297 v8.markPromiseAsHandled(writer[_readyPromise]); | |
| 298 } | 317 } |
| 299 } | 318 } |
| 300 | 319 |
| 301 function WritableStreamFinishClose(stream) { | 320 function WritableStreamFinishInFlightCloseInErroredState(stream) { |
| 302 const state = stream[_state]; | 321 WritableStreamRejectAbortRequestIfPending(stream); |
| 303 TEMP_ASSERT(state === CLOSING || state === ERRORED, | 322 WritableStreamRejectClosedPromiseInReactionToError(stream); |
| 304 'state is "closing" or "errored"'); | 323 } |
| 305 | 324 |
| 325 function WritableStreamFinishInFlightCloseWithError(stream, error) { | |
| 326 TEMP_ASSERT(stream[_inFlightCloseRequest] !== undefined, | |
| 327 '_stream_.[[inFlightCloseRequest]] is not *undefined*.'); | |
| 328 v8.rejectPromise(stream[_inFlightCloseRequest], error); | |
| 329 stream[_inFlightCloseRequest] = undefined; | |
| 330 const state = stream[_stateAndFlags] & STATE_MASK; | |
| 331 if (state === ERRORED) { | |
| 332 WritableStreamFinishInFlightCloseInErroredState(stream); | |
| 333 return; | |
| 334 } | |
| 335 TEMP_ASSERT(state === WRITABLE, '_state_ is `"writable"`.'); | |
| 336 WritableStreamError(stream, error); | |
| 337 WritableStreamRejectAbortRequestIfPending(stream); | |
| 338 } | |
| 339 | |
| 340 function WritableStreamCloseQueuedOrInFlight(stream) { | |
| 341 return stream[_closeRequest] !== undefined || | |
| 342 stream[_inFlightCloseRequest] !== undefined; | |
| 343 } | |
| 344 | |
| 345 function WritableStreamHandleAbortRequestIfPending(stream) { | |
| 346 if (stream[_pendingAbortRequest] === undefined) { | |
| 347 return; | |
| 348 } | |
| 349 WritableStreamFinishAbort(stream); | |
| 350 const abortRequest = stream[_pendingAbortRequest]; | |
| 351 stream[_pendingAbortRequest] = undefined; | |
| 352 const promise = | |
| 353 WritableStreamDefaultControllerAbortSteps(stream[_writableStreamControll er], | |
| 354 abortRequest.reason); | |
| 355 thenPromise(promise, | |
| 356 result => v8.resolvePromise(abortRequest.promise, result), | |
| 357 reason => v8.rejectPromise(abortRequest.promise, reason)); | |
| 358 } | |
| 359 | |
| 360 function WritableStreamHasOperationMarkedInFlight(stream) { | |
| 361 return stream[_inFlightWriteRequest] !== undefined || | |
| 362 stream[_inFlightCloseRequest] !== undefined; | |
| 363 } | |
| 364 | |
| 365 function WritableStreamMarkCloseRequestInFlight(stream) { | |
| 366 TEMP_ASSERT(stream[_inFlightCloseRequest] === undefined, | |
| 367 '_stream_.[[inFlightCloseRequest]] is *undefined*.'); | |
| 368 TEMP_ASSERT(stream[_closeRequest] !== undefined, | |
| 369 '_stream_.[[closeRequest]] is not *undefined*.'); | |
| 370 stream[_inFlightCloseRequest] = stream[_closeRequest]; | |
| 371 stream[_closeRequest] = undefined; | |
| 372 } | |
| 373 | |
| 374 function WritableStreamMarkFirstWriteRequestInFlight(stream) { | |
| 375 TEMP_ASSERT(stream[_inFlightWriteRequest] === undefined, | |
| 376 '_stream_.[[inFlightWriteRequest]] is *undefined*.'); | |
| 377 TEMP_ASSERT(stream[_writeRequests].length !== 0, | |
| 378 '_stream_.[[writeRequests]] is not empty.'); | |
| 379 const writeRequest = stream[_writeRequests].shift(); | |
| 380 stream[_inFlightWriteRequest] = writeRequest; | |
| 381 } | |
| 382 | |
| 383 function WritableStreamRejectClosedPromiseInReactionToError(stream) { | |
| 306 const writer = stream[_writer]; | 384 const writer = stream[_writer]; |
| 307 if (state === CLOSING) { | 385 if (writer !== undefined) { |
| 308 if (writer !== undefined) { | |
| 309 v8.resolvePromise(writer[_closedPromise], undefined); | |
| 310 } | |
| 311 stream[_state] = CLOSED; | |
| 312 } else if (writer !== undefined) { | |
| 313 TEMP_ASSERT(state === ERRORED, 'state is "errored"'); | |
| 314 v8.rejectPromise(writer[_closedPromise], stream[_storedError]); | 386 v8.rejectPromise(writer[_closedPromise], stream[_storedError]); |
| 315 v8.markPromiseAsHandled(writer[_closedPromise]); | 387 v8.markPromiseAsHandled(writer[_closedPromise]); |
| 316 } | 388 } |
| 389 } | |
| 317 | 390 |
| 391 function WritableStreamRejectAbortRequestIfPending(stream) { | |
| 318 if (stream[_pendingAbortRequest] !== undefined) { | 392 if (stream[_pendingAbortRequest] !== undefined) { |
| 319 v8.resolvePromise(stream[_pendingAbortRequest], undefined); | 393 v8.rejectPromise(stream[_pendingAbortRequest].promise, |
| 394 stream[_storedError]); | |
| 320 stream[_pendingAbortRequest] = undefined; | 395 stream[_pendingAbortRequest] = undefined; |
| 321 } | 396 } |
| 322 } | 397 } |
| 323 | 398 |
| 324 function WritableStreamRejectPromisesInReactionToError(stream) { | 399 function WritableStreamRejectPromisesInReactionToError(stream) { |
| 325 TEMP_ASSERT(stream[_state] === ERRORED, 'stream.[[state]] is "errored"'); | |
| 326 TEMP_ASSERT(stream[_pendingWriteRequest] === undefined, | |
| 327 'stream.[[pendingWriteRequest]] is undefined'); | |
| 328 | |
| 329 const storedError = stream[_storedError]; | 400 const storedError = stream[_storedError]; |
| 330 rejectPromises(stream[_writeRequests], storedError); | 401 rejectPromises(stream[_writeRequests], storedError); |
| 331 stream[_writeRequests] = new binding.SimpleQueue(); | 402 stream[_writeRequests] = new binding.SimpleQueue(); |
| 332 | 403 |
| 333 if (stream[_pendingCloseRequest] !== undefined) { | 404 if (stream[_closeRequest] !== undefined) { |
| 334 TEMP_ASSERT( | 405 TEMP_ASSERT(stream[_inFlightCloseRequest] === undefined, |
| 335 getDefaultControllerInCloseFlag(stream[_writableStreamController]) === | 406 '_stream_.[[inFlightCloseRequest]] is *undefined*.'); |
| 336 false, 'stream.[[writableStreamController]].[[inClose]] === false'); | 407 v8.rejectPromise(stream[_closeRequest], storedError); |
| 337 v8.rejectPromise(stream[_pendingCloseRequest], storedError); | 408 stream[_closeRequest] = undefined; |
| 338 stream[_pendingCloseRequest] = undefined; | |
| 339 } | 409 } |
| 340 | 410 |
| 411 WritableStreamRejectClosedPromiseInReactionToError(stream); | |
| 412 } | |
| 413 | |
| 414 function WritableStreamUpdateBackpressure(stream, backpressure) { | |
| 415 TEMP_ASSERT((stream[_stateAndFlags] & STATE_MASK) === WRITABLE, | |
| 416 'stream.[[state]] is "writable".'); | |
| 417 TEMP_ASSERT(!WritableStreamCloseQueuedOrInFlight(stream), | |
| 418 'WritableStreamCloseQueuedOrInFlight(_stream_) is *false*.'); | |
| 341 const writer = stream[_writer]; | 419 const writer = stream[_writer]; |
| 342 if (writer !== undefined) { | 420 if (writer !== undefined && |
| 343 v8.rejectPromise(writer[_closedPromise], storedError); | 421 backpressure !== Boolean(stream[_stateAndFlags] & BACKPRESSURE_FLAG)) { |
| 344 v8.markPromiseAsHandled(writer[_closedPromise]); | 422 if (backpressure) { |
| 423 writer[_readyPromise] = v8.createPromise(); | |
| 424 } else { | |
| 425 TEMP_ASSERT(!backpressure, '_backpressure_ is *false*.'); | |
| 426 v8.resolvePromise(writer[_readyPromise], undefined); | |
| 427 } | |
| 428 } | |
| 429 if (backpressure) { | |
| 430 stream[_stateAndFlags] |= BACKPRESSURE_FLAG; | |
| 431 } else { | |
| 432 stream[_stateAndFlags] &= ~BACKPRESSURE_FLAG; | |
| 345 } | 433 } |
| 346 } | 434 } |
| 347 | 435 |
| 348 function WritableStreamUpdateBackpressure(stream, backpressure) { | |
| 349 TEMP_ASSERT(stream[_state] === WRITABLE, | |
| 350 'stream.[[state]] is "writable".'); | |
| 351 const writer = stream[_writer]; | |
| 352 if (writer === undefined) { | |
| 353 return; | |
| 354 } | |
| 355 if (backpressure) { | |
| 356 writer[_readyPromise] = v8.createPromise(); | |
| 357 } else { | |
| 358 TEMP_ASSERT(backpressure === false, | |
| 359 'backpressure is false.'); | |
| 360 v8.resolvePromise(writer[_readyPromise], undefined); | |
| 361 } | |
| 362 } | |
| 363 | |
| 364 // Functions to expose internals for ReadableStream.pipeTo. These are not | 436 // Functions to expose internals for ReadableStream.pipeTo. These are not |
| 365 // part of the standard. | 437 // part of the standard. |
| 366 function isWritableStreamErrored(stream) { | 438 function isWritableStreamErrored(stream) { |
| 367 TEMP_ASSERT( | 439 TEMP_ASSERT( |
| 368 IsWritableStream(stream), '! IsWritableStream(stream) is true.'); | 440 IsWritableStream(stream), '! IsWritableStream(stream) is true.'); |
| 369 return stream[_state] === ERRORED; | 441 return (stream[_stateAndFlags] & STATE_MASK) === ERRORED; |
| 370 } | 442 } |
| 371 | 443 |
| 372 function isWritableStreamClosingOrClosed(stream) { | 444 function isWritableStreamClosingOrClosed(stream) { |
| 373 TEMP_ASSERT( | 445 TEMP_ASSERT( |
| 374 IsWritableStream(stream), '! IsWritableStream(stream) is true.'); | 446 IsWritableStream(stream), '! IsWritableStream(stream) is true.'); |
| 375 return stream[_state] === CLOSING || stream[_state] === CLOSED; | 447 return WritableStreamCloseQueuedOrInFlight(stream) || |
| 448 (stream[_stateAndFlags] & STATE_MASK) === CLOSED; | |
| 376 } | 449 } |
| 377 | 450 |
| 378 function getWritableStreamStoredError(stream) { | 451 function getWritableStreamStoredError(stream) { |
| 379 TEMP_ASSERT( | 452 TEMP_ASSERT( |
| 380 IsWritableStream(stream), '! IsWritableStream(stream) is true.'); | 453 IsWritableStream(stream), '! IsWritableStream(stream) is true.'); |
| 381 return stream[_storedError]; | 454 return stream[_storedError]; |
| 382 } | 455 } |
| 383 | 456 |
| 384 class WritableStreamDefaultWriter { | 457 class WritableStreamDefaultWriter { |
| 385 constructor(stream) { | 458 constructor(stream) { |
| 386 if (!IsWritableStream(stream)) { | 459 if (!IsWritableStream(stream)) { |
| 387 throw new TypeError(streamErrors.illegalConstructor); | 460 throw new TypeError(streamErrors.illegalConstructor); |
| 388 } | 461 } |
| 389 if (IsWritableStreamLocked(stream)) { | 462 if (IsWritableStreamLocked(stream)) { |
| 390 throw new TypeError(streamErrors.illegalConstructor); | 463 throw new TypeError(streamErrors.illegalConstructor); |
| 391 } | 464 } |
| 392 this[_ownerWritableStream] = stream; | 465 this[_ownerWritableStream] = stream; |
| 393 stream[_writer] = this; | 466 stream[_writer] = this; |
| 394 const state = stream[_state]; | 467 const state = stream[_stateAndFlags] & STATE_MASK; |
| 395 if (state === WRITABLE || state === CLOSING) { | 468 if (state === WRITABLE) { |
| 469 if (stream[_pendingAbortRequest] !== undefined) { | |
| 470 const error = new TypeError(errStreamAborting); | |
| 471 this[_readyPromise] = Promise_reject(error); | |
| 472 v8.markPromiseAsHandled(this[_readyPromise]); | |
| 473 } else if (!WritableStreamCloseQueuedOrInFlight(stream) && | |
| 474 stream[_stateAndFlags] & BACKPRESSURE_FLAG) { | |
| 475 this[_readyPromise] = v8.createPromise(); | |
| 476 } else { | |
| 477 this[_readyPromise] = Promise_resolve(undefined); | |
| 478 } | |
| 396 this[_closedPromise] = v8.createPromise(); | 479 this[_closedPromise] = v8.createPromise(); |
| 397 } else if (state === CLOSED) { | 480 } else if (state === CLOSED) { |
| 481 this[_readyPromise] = Promise_resolve(undefined); | |
| 398 this[_closedPromise] = Promise_resolve(undefined); | 482 this[_closedPromise] = Promise_resolve(undefined); |
| 399 } else { | 483 } else { |
| 400 TEMP_ASSERT(state === ERRORED, | 484 TEMP_ASSERT(state === ERRORED, '_state_ is `"errored"`.'); |
| 401 'state is "errored".'); | 485 const storedError = stream[_storedError]; |
| 402 this[_closedPromise] = Promise_reject(stream[_storedError]); | 486 this[_readyPromise] = Promise_reject(storedError); |
| 487 v8.markPromiseAsHandled(this[_readyPromise]); | |
| 488 this[_closedPromise] = Promise_reject(storedError); | |
| 403 v8.markPromiseAsHandled(this[_closedPromise]); | 489 v8.markPromiseAsHandled(this[_closedPromise]); |
| 404 } | 490 } |
| 405 if (state === WRITABLE && | |
| 406 WritableStreamDefaultControllerGetBackpressure( | |
| 407 stream[_writableStreamController])) { | |
| 408 this[_readyPromise] = v8.createPromise(); | |
| 409 } else { | |
| 410 this[_readyPromise] = Promise_resolve(undefined); | |
| 411 } | |
| 412 } | 491 } |
| 413 | 492 |
| 414 get closed() { | 493 get closed() { |
| 415 if (!IsWritableStreamDefaultWriter(this)) { | 494 if (!IsWritableStreamDefaultWriter(this)) { |
| 416 return Promise_reject(new TypeError(streamErrors.illegalInvocation)); | 495 return Promise_reject(new TypeError(streamErrors.illegalInvocation)); |
| 417 } | 496 } |
| 418 return this[_closedPromise]; | 497 return this[_closedPromise]; |
| 419 } | 498 } |
| 420 | 499 |
| 421 get desiredSize() { | 500 get desiredSize() { |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 446 } | 525 } |
| 447 | 526 |
| 448 close() { | 527 close() { |
| 449 if (!IsWritableStreamDefaultWriter(this)) { | 528 if (!IsWritableStreamDefaultWriter(this)) { |
| 450 return Promise_reject(new TypeError(streamErrors.illegalInvocation)); | 529 return Promise_reject(new TypeError(streamErrors.illegalInvocation)); |
| 451 } | 530 } |
| 452 const stream = this[_ownerWritableStream]; | 531 const stream = this[_ownerWritableStream]; |
| 453 if (stream === undefined) { | 532 if (stream === undefined) { |
| 454 return Promise_reject(createWriterLockReleasedError(verbClosed)); | 533 return Promise_reject(createWriterLockReleasedError(verbClosed)); |
| 455 } | 534 } |
| 456 if (stream[_state] === CLOSING) { | 535 if (WritableStreamCloseQueuedOrInFlight(stream)) { |
| 457 return Promise_reject(new TypeError(errCloseCloseRequestedStream)); | 536 return Promise_reject(new TypeError(errCloseCloseRequestedStream)); |
| 458 } | 537 } |
| 459 return WritableStreamDefaultWriterClose(this); | 538 return WritableStreamDefaultWriterClose(this); |
| 460 } | 539 } |
| 461 | 540 |
| 462 releaseLock() { | 541 releaseLock() { |
| 463 if (!IsWritableStreamDefaultWriter(this)) { | 542 if (!IsWritableStreamDefaultWriter(this)) { |
| 464 throw new TypeError(streamErrors.illegalInvocation); | 543 throw new TypeError(streamErrors.illegalInvocation); |
| 465 } | 544 } |
| 466 const stream = this[_ownerWritableStream]; | 545 const stream = this[_ownerWritableStream]; |
| 467 if (stream === undefined) { | 546 if (stream === undefined) { |
| 468 return; | 547 return; |
| 469 } | 548 } |
| 470 TEMP_ASSERT(stream[_writer] !== undefined, | 549 TEMP_ASSERT(stream[_writer] !== undefined, |
| 471 'stream.[[writer]] is not undefined.'); | 550 'stream.[[writer]] is not undefined.'); |
| 472 WritableStreamDefaultWriterRelease(this); | 551 WritableStreamDefaultWriterRelease(this); |
| 473 } | 552 } |
| 474 | 553 |
| 475 write(chunk) { | 554 write(chunk) { |
| 476 if (!IsWritableStreamDefaultWriter(this)) { | 555 if (!IsWritableStreamDefaultWriter(this)) { |
| 477 return Promise_reject(new TypeError(streamErrors.illegalInvocation)); | 556 return Promise_reject(new TypeError(streamErrors.illegalInvocation)); |
| 478 } | 557 } |
| 479 const stream = this[_ownerWritableStream]; | 558 if (this[_ownerWritableStream] === undefined) { |
| 480 if (stream === undefined) { | |
| 481 return Promise_reject(createWriterLockReleasedError(verbWrittenTo)); | 559 return Promise_reject(createWriterLockReleasedError(verbWrittenTo)); |
| 482 } | 560 } |
| 483 if (stream[_state] === CLOSING) { | |
| 484 return Promise_reject(new TypeError(errWriteCloseRequestedStream)); | |
| 485 } | |
| 486 return WritableStreamDefaultWriterWrite(this, chunk); | 561 return WritableStreamDefaultWriterWrite(this, chunk); |
| 487 } | 562 } |
| 488 } | 563 } |
| 489 | 564 |
| 490 // Writable Stream Writer Abstract Operations | 565 // Writable Stream Writer Abstract Operations |
| 491 | 566 |
| 492 function IsWritableStreamDefaultWriter(x) { | 567 function IsWritableStreamDefaultWriter(x) { |
| 493 return hasOwnProperty(x, _ownerWritableStream); | 568 return hasOwnProperty(x, _ownerWritableStream); |
| 494 } | 569 } |
| 495 | 570 |
| 496 function WritableStreamDefaultWriterAbort(writer, reason) { | 571 function WritableStreamDefaultWriterAbort(writer, reason) { |
| 497 const stream = writer[_ownerWritableStream]; | 572 const stream = writer[_ownerWritableStream]; |
| 498 TEMP_ASSERT(stream !== undefined, | 573 TEMP_ASSERT(stream !== undefined, |
| 499 'stream is not undefined.'); | 574 'stream is not undefined.'); |
| 500 return WritableStreamAbort(stream, reason); | 575 return WritableStreamAbort(stream, reason); |
| 501 } | 576 } |
| 502 | 577 |
| 503 function WritableStreamDefaultWriterClose(writer) { | 578 function WritableStreamDefaultWriterClose(writer) { |
| 504 const stream = writer[_ownerWritableStream]; | 579 const stream = writer[_ownerWritableStream]; |
| 505 TEMP_ASSERT(stream !== undefined, | 580 TEMP_ASSERT(stream !== undefined, 'stream is not undefined.'); |
| 506 'stream is not undefined.'); | 581 const state = stream[_stateAndFlags] & STATE_MASK; |
| 507 const state = stream[_state]; | |
| 508 if (state === CLOSED || state === ERRORED) { | 582 if (state === CLOSED || state === ERRORED) { |
| 509 return Promise_reject( | 583 return Promise_reject( |
| 510 createCannotActionOnStateStreamError('close', state)); | 584 createCannotActionOnStateStreamError('close', state)); |
| 511 } | 585 } |
| 512 TEMP_ASSERT(state === WRITABLE, | 586 if (stream[_pendingAbortRequest] !== undefined) { |
| 513 'state is "writable".'); | 587 return Promise_reject(new TypeError(errStreamAborting)); |
| 514 stream[_pendingCloseRequest] = v8.createPromise(); | 588 } |
| 515 if (WritableStreamDefaultControllerGetBackpressure( | 589 TEMP_ASSERT(state === WRITABLE, '_state_ is `"writable"`.'); |
| 516 stream[_writableStreamController])) { | 590 TEMP_ASSERT(!WritableStreamCloseQueuedOrInFlight(stream), |
| 591 '! WritableStreamCloseQueuedOrInFlight(_stream_) is *false*.'); | |
| 592 const promise = v8.createPromise(); | |
| 593 stream[_closeRequest] = promise; | |
| 594 if (stream[_stateAndFlags] & BACKPRESSURE_FLAG) { | |
| 517 v8.resolvePromise(writer[_readyPromise], undefined); | 595 v8.resolvePromise(writer[_readyPromise], undefined); |
| 518 } | 596 } |
| 519 stream[_state] = CLOSING; | |
| 520 WritableStreamDefaultControllerClose(stream[_writableStreamController]); | 597 WritableStreamDefaultControllerClose(stream[_writableStreamController]); |
| 521 return stream[_pendingCloseRequest]; | 598 return promise; |
| 522 } | 599 } |
| 523 | 600 |
| 524 function WritableStreamDefaultWriterCloseWithErrorPropagation(writer) { | 601 function WritableStreamDefaultWriterCloseWithErrorPropagation(writer) { |
| 525 const stream = writer[_ownerWritableStream]; | 602 const stream = writer[_ownerWritableStream]; |
| 526 TEMP_ASSERT(stream !== undefined, 'stream is not undefined.'); | 603 TEMP_ASSERT(stream !== undefined, 'stream is not undefined.'); |
| 527 const state = stream[_state]; | 604 const state = stream[_stateAndFlags] & STATE_MASK; |
| 528 if (state === CLOSING || state === CLOSED) { | 605 if (WritableStreamCloseQueuedOrInFlight(stream) || state === CLOSED) { |
| 529 return Promise_resolve(undefined); | 606 return Promise_resolve(undefined); |
| 530 } | 607 } |
| 531 if (state === ERRORED) { | 608 if (state === ERRORED) { |
| 532 return Promise_reject(stream[_storedError]); | 609 return Promise_reject(stream[_storedError]); |
| 533 } | 610 } |
| 534 TEMP_ASSERT(state === WRITABLE, 'state is "writable".'); | 611 TEMP_ASSERT(state === WRITABLE, 'state is "writable".'); |
| 535 return WritableStreamDefaultWriterClose(writer); | 612 return WritableStreamDefaultWriterClose(writer); |
| 536 } | 613 } |
| 537 | 614 |
| 615 function WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer, | |
| 616 error) { | |
| 617 if (v8.promiseState(writer[_readyPromise]) === v8.kPROMISE_PENDING) { | |
| 618 v8.rejectPromise(writer[_readyPromise], error); | |
| 619 } else { | |
| 620 writer[_readyPromise] = Promise_reject(error); | |
| 621 } | |
| 622 v8.markPromiseAsHandled(writer[_readyPromise]); | |
| 623 } | |
| 624 | |
| 538 function WritableStreamDefaultWriterGetDesiredSize(writer) { | 625 function WritableStreamDefaultWriterGetDesiredSize(writer) { |
| 539 const stream = writer[_ownerWritableStream]; | 626 const stream = writer[_ownerWritableStream]; |
| 540 const state = stream[_state]; | 627 const state = stream[_stateAndFlags] & STATE_MASK; |
| 541 if (state === ERRORED) { | 628 if (state === ERRORED || stream[_pendingAbortRequest] !== undefined) { |
| 542 return null; | 629 return null; |
| 543 } | 630 } |
| 544 if (state === CLOSED) { | 631 if (state === CLOSED) { |
| 545 return 0; | 632 return 0; |
| 546 } | 633 } |
| 547 return WritableStreamDefaultControllerGetDesiredSize( | 634 return WritableStreamDefaultControllerGetDesiredSize( |
| 548 stream[_writableStreamController]); | 635 stream[_writableStreamController]); |
| 549 } | 636 } |
| 550 | 637 |
| 551 function WritableStreamDefaultWriterRelease(writer) { | 638 function WritableStreamDefaultWriterRelease(writer) { |
| 552 const stream = writer[_ownerWritableStream]; | 639 const stream = writer[_ownerWritableStream]; |
| 553 TEMP_ASSERT(stream !== undefined, | 640 TEMP_ASSERT(stream !== undefined, |
| 554 'stream is not undefined.'); | 641 'stream is not undefined.'); |
| 555 TEMP_ASSERT(stream[_writer] === writer, | 642 TEMP_ASSERT(stream[_writer] === writer, |
| 556 'stream.[[writer]] is writer.'); | 643 'stream.[[writer]] is writer.'); |
| 557 const releasedError = new TypeError(errReleasedWriterClosedPromise); | 644 const releasedError = new TypeError(errReleasedWriterClosedPromise); |
| 558 const state = stream[_state]; | 645 const state = stream[_stateAndFlags] & STATE_MASK; |
| 559 if (state === WRITABLE || state === CLOSING || | 646 WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer, |
| 560 stream[_pendingAbortRequest] !== undefined) { | 647 releasedError); |
| 648 if (state === WRITABLE || | |
| 649 WritableStreamHasOperationMarkedInFlight(stream)) { | |
| 561 v8.rejectPromise(writer[_closedPromise], releasedError); | 650 v8.rejectPromise(writer[_closedPromise], releasedError); |
| 562 } else { | 651 } else { |
| 563 writer[_closedPromise] = Promise_reject(releasedError); | 652 writer[_closedPromise] = Promise_reject(releasedError); |
| 564 } | 653 } |
| 565 v8.markPromiseAsHandled(writer[_closedPromise]); | 654 v8.markPromiseAsHandled(writer[_closedPromise]); |
| 566 | |
| 567 if (state === WRITABLE && | |
| 568 WritableStreamDefaultControllerGetBackpressure( | |
| 569 stream[_writableStreamController])) { | |
| 570 v8.rejectPromise(writer[_readyPromise], releasedError); | |
| 571 } else { | |
| 572 writer[_readyPromise] = Promise_reject(releasedError); | |
| 573 } | |
| 574 v8.markPromiseAsHandled(writer[_readyPromise]); | |
| 575 | |
| 576 stream[_writer] = undefined; | 655 stream[_writer] = undefined; |
| 577 writer[_ownerWritableStream] = undefined; | 656 writer[_ownerWritableStream] = undefined; |
| 578 } | 657 } |
| 579 | 658 |
| 580 function WritableStreamDefaultWriterWrite(writer, chunk) { | 659 function WritableStreamDefaultWriterWrite(writer, chunk) { |
| 581 const stream = writer[_ownerWritableStream]; | 660 const stream = writer[_ownerWritableStream]; |
| 582 TEMP_ASSERT(stream !== undefined, | 661 TEMP_ASSERT(stream !== undefined, 'stream is not undefined.'); |
| 583 'stream is not undefined.'); | 662 const controller = stream[_writableStreamController]; |
| 584 const state = stream[_state]; | 663 const chunkSize = |
| 585 if (state === CLOSED || state === ERRORED) { | 664 WritableStreamDefaultControllerGetChunkSize(controller, chunk); |
| 665 if (stream !== writer[_ownerWritableStream]) { | |
| 666 return Promise_reject(createWriterLockReleasedError(verbWrittenTo)); | |
| 667 } | |
| 668 const state = stream[_stateAndFlags] & STATE_MASK; | |
| 669 if (state === ERRORED) { | |
| 670 return Promise_reject(stream[_storedError]); | |
| 671 } | |
| 672 if (WritableStreamCloseQueuedOrInFlight(stream)) { | |
| 673 return Promise_reject(new TypeError( | |
| 674 templateErrorCannotActionOnStateStream('write to', 'closing'))); | |
| 675 } | |
| 676 if (state === CLOSED) { | |
| 586 return Promise_reject( | 677 return Promise_reject( |
| 587 createCannotActionOnStateStreamError('write to', state)); | 678 createCannotActionOnStateStreamError('write to', CLOSED)); |
| 588 } | 679 } |
| 589 TEMP_ASSERT(state === WRITABLE, | 680 if (stream[_pendingAbortRequest] !== undefined) { |
| 590 'state is "writable".'); | 681 return Promise_reject(new TypeError(errStreamAborting)); |
| 682 } | |
| 591 const promise = WritableStreamAddWriteRequest(stream); | 683 const promise = WritableStreamAddWriteRequest(stream); |
| 592 WritableStreamDefaultControllerWrite(stream[_writableStreamController], | 684 WritableStreamDefaultControllerWrite(controller, chunk, chunkSize); |
| 593 chunk); | |
| 594 return promise; | 685 return promise; |
| 595 } | 686 } |
| 596 | 687 |
| 597 // Functions to expose internals for ReadableStream.pipeTo. These do not | 688 // Functions to expose internals for ReadableStream.pipeTo. These do not |
| 598 // appear in the standard. | 689 // appear in the standard. |
| 599 function getWritableStreamDefaultWriterClosedPromise(writer) { | 690 function getWritableStreamDefaultWriterClosedPromise(writer) { |
| 600 TEMP_ASSERT( | 691 TEMP_ASSERT( |
| 601 IsWritableStreamDefaultWriter(writer), | 692 IsWritableStreamDefaultWriter(writer), |
| 602 'writer is a WritableStreamDefaultWriter.'); | 693 'writer is a WritableStreamDefaultWriter.'); |
| 603 return writer[_closedPromise]; | 694 return writer[_closedPromise]; |
| 604 } | 695 } |
| 605 | 696 |
| 606 function getWritableStreamDefaultWriterReadyPromise(writer) { | 697 function getWritableStreamDefaultWriterReadyPromise(writer) { |
| 607 TEMP_ASSERT( | 698 TEMP_ASSERT( |
| 608 IsWritableStreamDefaultWriter(writer), | 699 IsWritableStreamDefaultWriter(writer), |
| 609 'writer is a WritableStreamDefaultWriter.'); | 700 'writer is a WritableStreamDefaultWriter.'); |
| 610 return writer[_readyPromise]; | 701 return writer[_readyPromise]; |
| 611 } | 702 } |
| 612 | 703 |
| 613 class WritableStreamDefaultController { | 704 class WritableStreamDefaultController { |
| 614 constructor(stream, underlyingSink, size, highWaterMark) { | 705 constructor(stream, underlyingSink, size, highWaterMark) { |
| 615 if (!IsWritableStream(stream)) { | 706 if (!IsWritableStream(stream)) { |
| 616 throw new TypeError(streamErrors.illegalConstructor); | 707 throw new TypeError(streamErrors.illegalConstructor); |
| 617 } | 708 } |
| 618 if (stream[_writableStreamController] !== undefined) { | 709 if (stream[_writableStreamController] !== undefined) { |
| 619 throw new TypeError(streamErrors.illegalConstructor); | 710 throw new TypeError(streamErrors.illegalConstructor); |
| 620 } | 711 } |
| 621 this[_controlledWritableStream] = stream; | 712 this[_controlledWritableStream] = stream; |
| 622 this[_underlyingSink] = underlyingSink; | 713 this[_underlyingSink] = underlyingSink; |
| 623 this[_queue] = new binding.SimpleQueue(); | 714 // These are just initialised to avoid triggering the assert() in |
| 624 this[_queueSize] = 0; | 715 // ResetQueue. They are overwritten by ResetQueue(). |
| 625 this[_defaultControllerFlags] = 0; | 716 this[_queue] = undefined; |
| 717 this[_queueTotalSize] = undefined; | |
| 718 ResetQueue(this); | |
| 719 this[_started] = false; | |
| 626 const normalizedStrategy = | 720 const normalizedStrategy = |
| 627 ValidateAndNormalizeQueuingStrategy(size, highWaterMark); | 721 ValidateAndNormalizeQueuingStrategy(size, highWaterMark); |
| 628 this[_strategySize] = normalizedStrategy.size; | 722 this[_strategySize] = normalizedStrategy.size; |
| 629 this[_strategyHWM] = normalizedStrategy.highWaterMark; | 723 this[_strategyHWM] = normalizedStrategy.highWaterMark; |
| 630 const backpressure = WritableStreamDefaultControllerGetBackpressure(this); | 724 const backpressure = WritableStreamDefaultControllerGetBackpressure(this); |
| 631 if (backpressure) { | 725 WritableStreamUpdateBackpressure(stream, backpressure); |
| 632 WritableStreamUpdateBackpressure(stream, backpressure); | |
| 633 } | |
| 634 const controller = this; | |
| 635 const startResult = InvokeOrNoop(underlyingSink, 'start', [this]); | |
| 636 const onFulfilled = () => { | |
| 637 setDefaultControllerStartedFlag(controller, true); | |
| 638 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller); | |
| 639 }; | |
| 640 const onRejected = r => { | |
| 641 WritableStreamDefaultControllerErrorIfNeeded(controller, r); | |
| 642 }; | |
| 643 thenPromise(Promise_resolve(startResult), onFulfilled, onRejected); | |
| 644 } | 726 } |
| 645 | 727 |
| 646 error(e) { | 728 error(e) { |
| 647 if (!IsWritableStreamDefaultController(this)) { | 729 if (!IsWritableStreamDefaultController(this)) { |
| 648 throw new TypeError(streamErrors.illegalInvocation); | 730 throw new TypeError(streamErrors.illegalInvocation); |
| 649 } | 731 } |
| 650 const state = this[_controlledWritableStream][_state]; | 732 const state = this[_controlledWritableStream][_stateAndFlags] & STATE_MASK ; |
| 651 if (state === CLOSED || state === ERRORED) { | 733 if (state === CLOSED || state === ERRORED) { |
| 652 throw createCannotActionOnStateStreamError('error', state); | 734 throw createCannotActionOnStateStreamError('error', state); |
| 653 } | 735 } |
| 654 WritableStreamDefaultControllerError(this, e); | 736 WritableStreamDefaultControllerError(this, e); |
| 655 } | 737 } |
| 656 } | 738 } |
| 657 | 739 |
| 740 // Writable Stream Default Controller Internal Methods | |
| 741 | |
| 742 // TODO(ricea): Virtual dispatch via V8 Private Symbols seems to be difficult | |
| 743 // or impossible, so use static dispatch for now. This will have to be fixed | |
| 744 // when adding a byte controller. | |
|
domenic
2017/04/05 07:02:42
In particular private symbol lookup doesn't go thr
Adam Rice
2017/04/10 01:04:04
One possibility would be to explicitly chase the p
| |
| 745 function WritableStreamDefaultControllerAbortSteps(controller, reason) { | |
| 746 const sinkAbortPromise = | |
| 747 PromiseInvokeOrNoop(controller[_underlyingSink], 'abort', [reason]); | |
| 748 return thenPromise(sinkAbortPromise, () => undefined); | |
| 749 } | |
| 750 | |
| 751 function WritableStreamDefaultControllerErrorSteps(controller) { | |
| 752 ResetQueue(controller); | |
| 753 } | |
| 754 | |
| 755 function WritableStreamDefaultControllerStartSteps(controller) { | |
| 756 const startResult = | |
| 757 InvokeOrNoop(controller[_underlyingSink], 'start', [controller]); | |
| 758 const stream = controller[_controlledWritableStream]; | |
| 759 const startPromise = Promise_resolve(startResult); | |
| 760 thenPromise( | |
| 761 startPromise, | |
| 762 () => { | |
| 763 controller[_started] = true; | |
| 764 if ((stream[_stateAndFlags] & STATE_MASK) === ERRORED) { | |
| 765 WritableStreamRejectAbortRequestIfPending(stream); | |
| 766 } else { | |
| 767 WritableStreamHandleAbortRequestIfPending(stream); | |
| 768 } | |
| 769 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller); | |
| 770 }, | |
| 771 r => { | |
| 772 TEMP_ASSERT( | |
| 773 (stream[_stateAndFlags] & STATE_MASK) === WRITABLE || | |
| 774 (stream[_stateAndFlags] & STATE_MASK) === ERRORED, | |
| 775 '_stream_.[[state]] is `"writable"` or `"errored"`.'); | |
| 776 WritableStreamDefaultControllerErrorIfNeeded(controller, r); | |
| 777 WritableStreamRejectAbortRequestIfPending(stream); | |
| 778 }); | |
| 779 } | |
| 780 | |
| 658 // Writable Stream Default Controller Abstract Operations | 781 // Writable Stream Default Controller Abstract Operations |
| 659 | 782 |
| 660 function IsWritableStreamDefaultController(x) { | 783 function IsWritableStreamDefaultController(x) { |
| 661 return hasOwnProperty(x, _underlyingSink); | 784 return hasOwnProperty(x, _underlyingSink); |
| 662 } | 785 } |
| 663 | 786 |
| 664 function WritableStreamDefaultControllerAbort(controller, reason) { | |
| 665 controller[_queue] = new binding.SimpleQueue(); | |
| 666 controller[_queueSize] = 0; | |
| 667 const sinkAbortPromise = | |
| 668 PromiseInvokeOrNoop(controller[_underlyingSink], 'abort', [reason]); | |
| 669 return thenPromise(sinkAbortPromise, () => undefined); | |
| 670 } | |
| 671 | |
| 672 function WritableStreamDefaultControllerClose(controller) { | 787 function WritableStreamDefaultControllerClose(controller) { |
| 673 EnqueueValueWithSizeForController(controller, 'close', 0); | 788 EnqueueValueWithSize(controller, 'close', 0); |
| 674 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller); | 789 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller); |
| 675 } | 790 } |
| 676 | 791 |
| 677 function WritableStreamDefaultControllerGetDesiredSize(controller) { | 792 function WritableStreamDefaultControllerGetChunkSize(controller, chunk) { |
| 678 const queueSize = GetTotalQueueSizeForController(controller); | 793 const strategySize = controller[_strategySize]; |
| 679 return controller[_strategyHWM] - queueSize; | 794 if (strategySize === undefined) { |
| 795 return 1; | |
| 796 } | |
| 797 let value; | |
| 798 try { | |
| 799 value = Function_apply(strategySize, undefined, [chunk]); | |
|
domenic
2017/04/05 07:02:42
Function_call so you don't need to create the temp
Adam Rice
2017/04/10 01:04:04
Done.
| |
| 800 } catch (e) { | |
| 801 WritableStreamDefaultControllerErrorIfNeeded(controller, e); | |
| 802 return 1; | |
| 803 } | |
| 804 return value; | |
| 680 } | 805 } |
| 681 | 806 |
| 682 function WritableStreamDefaultControllerWrite(controller, chunk) { | 807 function WritableStreamDefaultControllerGetDesiredSize(controller) { |
| 683 const stream = controller[_controlledWritableStream]; | 808 return controller[_strategyHWM] - controller[_queueTotalSize]; |
| 684 TEMP_ASSERT(stream[_state] === WRITABLE, | 809 } |
| 685 'stream.[[state]] is "writable".'); | 810 |
| 686 let chunkSize = 1; | 811 function WritableStreamDefaultControllerWrite(controller, chunk, chunkSize) { |
| 687 const strategySize = controller[_strategySize]; | |
| 688 if (strategySize !== undefined) { | |
| 689 try { | |
| 690 chunkSize = strategySize(chunk); | |
| 691 } catch (e) { | |
| 692 WritableStreamDefaultControllerErrorIfNeeded(controller, e); | |
| 693 return; | |
| 694 } | |
| 695 } | |
| 696 const writeRecord = {chunk}; | 812 const writeRecord = {chunk}; |
| 697 const lastBackpressure = | |
| 698 WritableStreamDefaultControllerGetBackpressure(controller); | |
| 699 try { | 813 try { |
| 700 EnqueueValueWithSizeForController(controller, writeRecord, chunkSize); | 814 EnqueueValueWithSize(controller, writeRecord, chunkSize); |
| 701 } catch (e) { | 815 } catch (e) { |
| 702 WritableStreamDefaultControllerErrorIfNeeded(controller, e); | 816 WritableStreamDefaultControllerErrorIfNeeded(controller, e); |
| 703 return; | 817 return; |
| 704 } | 818 } |
| 705 if (stream[_state] === WRITABLE) { | 819 const stream = controller[_controlledWritableStream]; |
| 820 if (!WritableStreamCloseQueuedOrInFlight(stream)) { | |
| 706 const backpressure = | 821 const backpressure = |
| 707 WritableStreamDefaultControllerGetBackpressure(controller); | 822 WritableStreamDefaultControllerGetBackpressure(controller); |
| 708 if (lastBackpressure !== backpressure) { | 823 WritableStreamUpdateBackpressure(stream, backpressure); |
| 709 WritableStreamUpdateBackpressure(stream, backpressure); | |
| 710 } | |
| 711 } | 824 } |
| 712 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller); | 825 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller); |
| 713 } | 826 } |
| 714 | 827 |
| 715 function WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller) { | 828 function WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller) { |
| 716 const state = controller[_controlledWritableStream][_state]; | 829 const stream = controller[_controlledWritableStream]; |
| 830 const state = stream[_stateAndFlags] & STATE_MASK; | |
| 717 if (state === CLOSED || state === ERRORED) { | 831 if (state === CLOSED || state === ERRORED) { |
| 718 return; | 832 return; |
| 719 } | 833 } |
| 720 if (!getDefaultControllerStartedFlag(controller)) { | 834 if (!controller[_started]) { |
| 721 return; | 835 return; |
| 722 } | 836 } |
| 723 if (getDefaultControllerWritingFlag(controller)) { | 837 if (stream[_inFlightWriteRequest] !== undefined) { |
| 724 return; | 838 return; |
| 725 } | 839 } |
| 726 if (controller[_queue].length === 0) { | 840 if (controller[_queue].length === 0) { |
| 727 return; | 841 return; |
| 728 } | 842 } |
| 729 const writeRecord = PeekQueueValue(controller[_queue]); | 843 const writeRecord = PeekQueueValue(controller); |
| 730 if (writeRecord === 'close') { | 844 if (writeRecord === 'close') { |
| 731 WritableStreamDefaultControllerProcessClose(controller); | 845 WritableStreamDefaultControllerProcessClose(controller); |
| 732 } else { | 846 } else { |
| 733 WritableStreamDefaultControllerProcessWrite(controller, | 847 WritableStreamDefaultControllerProcessWrite(controller, |
| 734 writeRecord.chunk); | 848 writeRecord.chunk); |
| 735 } | 849 } |
| 736 } | 850 } |
| 737 | 851 |
| 738 function WritableStreamDefaultControllerErrorIfNeeded(controller, e) { | 852 function WritableStreamDefaultControllerErrorIfNeeded(controller, error) { |
| 739 const state = controller[_controlledWritableStream][_state]; | 853 const state = controller[_controlledWritableStream][_stateAndFlags] & STATE_ MASK; |
| 740 if (state === WRITABLE || state === CLOSING) { | 854 if (state === WRITABLE) { |
| 741 WritableStreamDefaultControllerError(controller, e); | 855 WritableStreamDefaultControllerError(controller, error); |
| 742 } | 856 } |
| 743 } | 857 } |
| 744 | 858 |
| 745 function WritableStreamDefaultControllerProcessClose(controller) { | 859 function WritableStreamDefaultControllerProcessClose(controller) { |
| 746 const stream = controller[_controlledWritableStream]; | 860 const stream = controller[_controlledWritableStream]; |
| 747 TEMP_ASSERT(stream[_state] === CLOSING, | 861 WritableStreamMarkCloseRequestInFlight(stream); |
| 748 'stream.[[state]] is "closing".'); | 862 DequeueValue(controller); |
| 749 DequeueValueForController(controller); | |
| 750 TEMP_ASSERT(controller[_queue].length === 0, | 863 TEMP_ASSERT(controller[_queue].length === 0, |
| 751 'controller.[[queue]] is empty.'); | 864 'controller.[[queue]] is empty.'); |
| 752 setDefaultControllerInCloseFlag(controller, true); | |
| 753 const sinkClosePromise = PromiseInvokeOrNoop(controller[_underlyingSink], | 865 const sinkClosePromise = PromiseInvokeOrNoop(controller[_underlyingSink], |
| 754 'close', [controller]); | 866 'close', [controller]); |
| 755 thenPromise(sinkClosePromise, | 867 thenPromise( |
| 756 () => { | 868 sinkClosePromise, |
| 757 TEMP_ASSERT(getDefaultControllerInCloseFlag(controller) | 869 () => WritableStreamFinishInFlightClose(stream), |
| 758 === true, | 870 reason => WritableStreamFinishInFlightCloseWithError(stream, reason) |
| 759 'controller.[[inClose]] is true'); | 871 ); |
| 760 setDefaultControllerInCloseFlag(controller, false); | |
| 761 | |
| 762 if (stream[_state] !== CLOSING && | |
| 763 stream[_state] !== ERRORED) { | |
| 764 return; | |
| 765 } | |
| 766 | |
| 767 TEMP_ASSERT(stream[_pendingCloseRequest] !== undefined); | |
| 768 v8.resolvePromise(stream[_pendingCloseRequest], undefined); | |
| 769 stream[_pendingCloseRequest] = undefined; | |
| 770 | |
| 771 WritableStreamFinishClose(stream); | |
| 772 }, | |
| 773 r => { | |
| 774 TEMP_ASSERT(getDefaultControllerInCloseFlag(controller) | |
| 775 === true, | |
| 776 'controller.[[inClose]] is true'); | |
| 777 setDefaultControllerInCloseFlag(controller, false); | |
| 778 | |
| 779 TEMP_ASSERT(stream[_pendingCloseRequest] !== undefined); | |
| 780 v8.rejectPromise(stream[_pendingCloseRequest], r); | |
| 781 stream[_pendingCloseRequest] = undefined; | |
| 782 | |
| 783 if (stream[_pendingAbortRequest] !== undefined) { | |
| 784 v8.rejectPromise(stream[_pendingAbortRequest], r); | |
| 785 stream[_pendingAbortRequest] = undefined; | |
| 786 } | |
| 787 | |
| 788 WritableStreamDefaultControllerErrorIfNeeded(controller, r); | |
| 789 } | |
| 790 ); | |
| 791 } | 872 } |
| 792 | 873 |
| 793 function WritableStreamDefaultControllerProcessWrite(controller, chunk) { | 874 function WritableStreamDefaultControllerProcessWrite(controller, chunk) { |
| 794 setDefaultControllerWritingFlag(controller, true); | |
| 795 | |
| 796 const stream = controller[_controlledWritableStream]; | 875 const stream = controller[_controlledWritableStream]; |
| 797 | 876 WritableStreamMarkFirstWriteRequestInFlight(stream); |
| 798 TEMP_ASSERT(stream[_pendingWriteRequest] === undefined, | |
| 799 'stream.[[pendingWriteRequest]] is undefined'); | |
| 800 TEMP_ASSERT(stream[_writeRequests].length > 0, | |
| 801 'stream.[[writeRequests]] is not empty'); | |
| 802 stream[_pendingWriteRequest] = stream[_writeRequests].shift(); | |
| 803 | |
| 804 const sinkWritePromise = PromiseInvokeOrNoop(controller[_underlyingSink], | 877 const sinkWritePromise = PromiseInvokeOrNoop(controller[_underlyingSink], |
| 805 'write', [chunk, controller]); | 878 'write', [chunk, controller]); |
| 806 thenPromise( | 879 thenPromise( |
| 807 sinkWritePromise, | 880 sinkWritePromise, |
| 808 () => { | 881 () => { |
| 809 TEMP_ASSERT(getDefaultControllerWritingFlag(controller) === true, | 882 WritableStreamFinishInFlightWrite(stream); |
| 810 'controller.[[writing]] is true'); | 883 const state = stream[_stateAndFlags] & STATE_MASK; |
| 811 setDefaultControllerWritingFlag(controller, false); | |
| 812 | |
| 813 TEMP_ASSERT(stream[_pendingWriteRequest] !== undefined, | |
| 814 'stream.[[pendingWriteRequest]] is not undefined'); | |
| 815 v8.resolvePromise(stream[_pendingWriteRequest], undefined); | |
| 816 stream[_pendingWriteRequest] = undefined; | |
| 817 | |
| 818 const state = stream[_state]; | |
| 819 if (state === ERRORED) { | 884 if (state === ERRORED) { |
| 820 WritableStreamRejectPromisesInReactionToError(stream); | |
| 821 | |
| 822 if (stream[_pendingAbortRequest] !== undefined) { | |
| 823 v8.resolvePromise(stream[_pendingAbortRequest], undefined); | |
| 824 stream[_pendingAbortRequest] = undefined; | |
| 825 } | |
| 826 return; | 885 return; |
| 827 } | 886 } |
| 828 | 887 TEMP_ASSERT(state === WRITABLE, '_state_ is `"writable"`.'); |
| 829 const lastBackpressure = | 888 DequeueValue(controller); |
| 830 WritableStreamDefaultControllerGetBackpressure(controller); | 889 if (!WritableStreamCloseQueuedOrInFlight(stream)) { |
| 831 DequeueValueForController(controller); | |
| 832 | |
| 833 if (state !== CLOSING) { | |
| 834 const backpressure = | 890 const backpressure = |
| 835 WritableStreamDefaultControllerGetBackpressure(controller); | 891 WritableStreamDefaultControllerGetBackpressure(controller); |
| 836 if (lastBackpressure !== backpressure) { | 892 WritableStreamUpdateBackpressure(stream, backpressure); |
| 837 WritableStreamUpdateBackpressure( | |
| 838 controller[_controlledWritableStream], backpressure); | |
| 839 } | |
| 840 } | 893 } |
| 841 | |
| 842 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller); | 894 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller); |
| 843 }, | 895 }, |
| 844 r => { | 896 reason => { |
| 845 TEMP_ASSERT(getDefaultControllerWritingFlag(controller) === true, | 897 const wasErrored = (stream[_stateAndFlags] & STATE_MASK) === ERRORED; |
| 846 'controller.[[writing]] is true'); | 898 WritableStreamFinishInFlightWriteWithError(stream, reason); |
| 847 setDefaultControllerWritingFlag(controller, false); | 899 TEMP_ASSERT((stream[_stateAndFlags] & STATE_MASK) === ERRORED, |
| 848 | 900 '_stream_.[[state]] is `"errored"`.'); |
| 849 TEMP_ASSERT(stream[_pendingWriteRequest] !== undefined, | 901 if (!wasErrored) { |
| 850 'stream.[[pendingWriteRequest]] is not undefined'); | 902 ResetQueue(controller); |
| 851 v8.rejectPromise(stream[_pendingWriteRequest], r); | |
| 852 stream[_pendingWriteRequest] = undefined; | |
| 853 | |
| 854 if (stream[_state] === ERRORED) { | |
| 855 stream[_storedError] = r; | |
| 856 WritableStreamRejectPromisesInReactionToError(stream); | |
| 857 } | 903 } |
| 858 | 904 }); |
| 859 if (stream[_pendingAbortRequest] !== undefined) { | |
| 860 v8.rejectPromise(stream[_pendingAbortRequest], r); | |
| 861 stream[_pendingAbortRequest] = undefined; | |
| 862 } | |
| 863 | |
| 864 WritableStreamDefaultControllerErrorIfNeeded(controller, r); | |
| 865 } | |
| 866 ); | |
| 867 } | 905 } |
| 868 | 906 |
| 869 function WritableStreamDefaultControllerGetBackpressure(controller) { | 907 function WritableStreamDefaultControllerGetBackpressure(controller) { |
| 870 const desiredSize = | 908 const desiredSize = |
| 871 WritableStreamDefaultControllerGetDesiredSize(controller); | 909 WritableStreamDefaultControllerGetDesiredSize(controller); |
| 872 return desiredSize <= 0; | 910 return desiredSize <= 0; |
| 873 } | 911 } |
| 874 | 912 |
| 875 function WritableStreamDefaultControllerError(controller, e) { | 913 function WritableStreamDefaultControllerError(controller, error) { |
| 876 const stream = controller[_controlledWritableStream]; | 914 const stream = controller[_controlledWritableStream]; |
| 877 const state = stream[_state]; | 915 TEMP_ASSERT((stream[_stateAndFlags] & STATE_MASK) === WRITABLE, |
| 878 TEMP_ASSERT(state === WRITABLE || state === CLOSING, | 916 '_stream_.[[state]] is `"writable"`.'); |
| 879 'stream.[[state]] is "writable" or "closing".'); | 917 WritableStreamError(stream, error); |
| 880 WritableStreamError(stream, e); | |
| 881 controller[_queue] = new binding.SimpleQueue(); | |
| 882 controller[_queueSize] = 0; | |
| 883 } | 918 } |
| 884 | 919 |
| 885 // Queue-with-Sizes Operations | 920 // Queue-with-Sizes Operations |
| 886 // | 921 // |
| 887 // TODO(ricea): Share these operations with ReadableStream.js. | 922 // TODO(ricea): Share these operations with ReadableStream.js. |
| 888 function DequeueValueForController(controller) { | 923 function DequeueValue(container) { |
| 889 TEMP_ASSERT(controller[_queue].length !== 0, | 924 TEMP_ASSERT( |
| 890 'queue is not empty.'); | 925 hasOwnProperty(container, _queue) && |
| 891 const result = controller[_queue].shift(); | 926 hasOwnProperty(container, _queueTotalSize), |
| 892 controller[_queueSize] -= result.size; | 927 'Assert: _container_ has [[queue]] and [[queueTotalSize]] internal ' + |
| 893 return result.value; | 928 'slots.'); |
| 929 TEMP_ASSERT(container[_queue].length !== 0, | |
| 930 '_container_.[[queue]] is not empty.'); | |
| 931 const pair = container[_queue].shift(); | |
| 932 container[_queueTotalSize] -= pair.size; | |
| 933 if (container[_queueTotalSize] < 0) { | |
| 934 container[_queueTotalSize] = 0; | |
| 935 } | |
| 936 return pair.value; | |
| 894 } | 937 } |
| 895 | 938 |
| 896 function EnqueueValueWithSizeForController(controller, value, size) { | 939 function EnqueueValueWithSize(container, value, size) { |
| 940 TEMP_ASSERT( | |
| 941 hasOwnProperty(container, _queue) && | |
| 942 hasOwnProperty(container, _queueTotalSize), | |
| 943 'Assert: _container_ has [[queue]] and [[queueTotalSize]] internal ' + | |
| 944 'slots.'); | |
| 897 size = Number(size); | 945 size = Number(size); |
| 898 if (!IsFiniteNonNegativeNumber(size)) { | 946 if (!IsFiniteNonNegativeNumber(size)) { |
| 899 throw new RangeError(streamErrors.invalidSize); | 947 throw new RangeError(streamErrors.invalidSize); |
| 900 } | 948 } |
| 901 | 949 |
| 902 controller[_queueSize] += size; | 950 container[_queue].push({value, size}); |
| 903 controller[_queue].push({value, size}); | 951 container[_queueTotalSize] += size; |
| 904 } | 952 } |
| 905 | 953 |
| 906 function GetTotalQueueSizeForController(controller) { | 954 function PeekQueueValue(container) { |
| 907 return controller[_queueSize]; | 955 TEMP_ASSERT( |
| 956 hasOwnProperty(container, _queue) && | |
| 957 hasOwnProperty(container, _queueTotalSize), | |
| 958 'Assert: _container_ has [[queue]] and [[queueTotalSize]] internal ' + | |
| 959 'slots.'); | |
| 960 TEMP_ASSERT(container[_queue].length !== 0, | |
| 961 '_container_.[[queue]] is not empty.'); | |
| 962 const pair = container[_queue].peek(); | |
| 963 return pair.value; | |
| 908 } | 964 } |
| 909 | 965 |
| 910 function PeekQueueValue(queue) { | 966 function ResetQueue(container) { |
| 911 TEMP_ASSERT(queue.length !== 0, | 967 TEMP_ASSERT( |
| 912 'queue is not empty.'); | 968 hasOwnProperty(container, _queue) && |
| 913 return queue.peek().value; | 969 hasOwnProperty(container, _queueTotalSize), |
| 970 'Assert: _container_ has [[queue]] and [[queueTotalSize]] internal ' + | |
| 971 'slots.'); | |
| 972 container[_queue] = new binding.SimpleQueue(); | |
| 973 container[_queueTotalSize] = 0; | |
| 914 } | 974 } |
| 915 | 975 |
| 916 // Miscellaneous Operations | 976 // Miscellaneous Operations |
| 917 | 977 |
| 918 // This differs from "CallOrNoop" in the ReadableStream implementation in | 978 // This differs from "CallOrNoop" in the ReadableStream implementation in |
| 919 // that it takes the arguments as an array, so that multiple arguments can be | 979 // that it takes the arguments as an array, so that multiple arguments can be |
| 920 // passed. | 980 // passed. |
| 921 // | 981 // |
| 922 // TODO(ricea): Consolidate with ReadableStream implementation. | 982 // TODO(ricea): Consolidate with ReadableStream implementation. |
| 923 function InvokeOrNoop(O, P, args) { | 983 function InvokeOrNoop(O, P, args) { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 992 getWritableStreamDefaultWriterClosedPromise; | 1052 getWritableStreamDefaultWriterClosedPromise; |
| 993 binding.WritableStreamDefaultWriterGetDesiredSize = | 1053 binding.WritableStreamDefaultWriterGetDesiredSize = |
| 994 WritableStreamDefaultWriterGetDesiredSize; | 1054 WritableStreamDefaultWriterGetDesiredSize; |
| 995 binding.getWritableStreamDefaultWriterReadyPromise = | 1055 binding.getWritableStreamDefaultWriterReadyPromise = |
| 996 getWritableStreamDefaultWriterReadyPromise; | 1056 getWritableStreamDefaultWriterReadyPromise; |
| 997 binding.WritableStreamDefaultWriterRelease = | 1057 binding.WritableStreamDefaultWriterRelease = |
| 998 WritableStreamDefaultWriterRelease; | 1058 WritableStreamDefaultWriterRelease; |
| 999 binding.WritableStreamDefaultWriterWrite = WritableStreamDefaultWriterWrite; | 1059 binding.WritableStreamDefaultWriterWrite = WritableStreamDefaultWriterWrite; |
| 1000 binding.getWritableStreamStoredError = getWritableStreamStoredError; | 1060 binding.getWritableStreamStoredError = getWritableStreamStoredError; |
| 1001 }); | 1061 }); |
| OLD | NEW |