[Streams API] Rework promises to use @newPromiseCapability
https://bugs.webkit.org/show_bug.cgi?id=150627

Reviewed by Youenn Fablet.

We are removing the stream promises functions in favor of @newPromiseCapabity which basically provides the same
functionality (keeping the resolve and reject functions without external slots). Slots and variables were
renamed as *PromiseCapability to show that they no longer hold just a promise, but a promise capability.

Internal rework, no new tests needed.

* Modules/streams/ReadableStreamInternals.js:
(privateInitializeReadableStreamReader.this.closedPromiseCapability.resolve):
(privateInitializeReadableStreamReader.this.closedPromiseCapability.reject):
(privateInitializeReadableStreamReader):
(teeReadableStream):
(teeReadableStreamBranch2CancelFunction):
(errorReadableStream):
(closeReadableStreamReader):
(enqueueInReadableStream):
(readFromReadableStreamReader):
* Modules/streams/ReadableStreamReader.js:
(closed):
* Modules/streams/StreamInternals.js:
(createNewStreamsPromise): Deleted.
(resolveStreamsPromise): Deleted.
(rejectStreamsPromise): Deleted.
* Modules/streams/WritableStream.js:
(this.readyPromiseCapability.resolve):
(this.readyPromiseCapability.reject):
(initializeWritableStream):
(close):
(write):
(closed):
(ready):
* Modules/streams/WritableStreamInternals.js:
(syncWritableStreamStateWithQueue):
(errorWritableStream):
* bindings/js/WebCoreBuiltinNames.h:


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@191950 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/Modules/streams/ReadableStreamInternals.js b/Source/WebCore/Modules/streams/ReadableStreamInternals.js
index 9f3f7cd..c79d6a9 100644
--- a/Source/WebCore/Modules/streams/ReadableStreamInternals.js
+++ b/Source/WebCore/Modules/streams/ReadableStreamInternals.js
@@ -42,19 +42,19 @@
         this.@ownerReadableStream = stream;
         this.@storedError = undefined;
         stream.@reader = this;
-        this.@closedPromise = @createNewStreamsPromise();
+        this.@closedPromiseCapability = @newPromiseCapability(Promise);
         return this;
     }
     if (stream.@state === @streamClosed) {
         this.@ownerReadableStream = null;
         this.@storedError = undefined;
-        this.@closedPromise = Promise.resolve();
+        this.@closedPromiseCapability = { @promise: Promise.resolve(undefined), @resolve: function() {}, @reject: function() {} };
         return this;
     }
     // FIXME: ASSERT(stream.@state === @streamErrored);
     this.@ownerReadableStream = null;
     this.@storedError = stream.@storedError;
-    this.@closedPromise = Promise.reject(stream.@storedError);
+    this.@closedPromiseCapability = { @promise: Promise.reject(stream.@storedError), @resolve: function() {}, @reject: function() {} };
 
     return this;
 }
@@ -89,10 +89,7 @@
         reason: undefined,
     };
 
-    teeState.cancelPromise = new @InternalPromise(function(resolve, reject) {
-       teeState.resolvePromise = resolve;
-       teeState.rejectPromise = reject;
-    });
+    teeState.cancelPromiseCapability = @newPromiseCapability(@InternalPromise);
 
     let pullFunction = @teeReadableStreamPullFunction(teeState, reader, shouldClone);
 
@@ -153,9 +150,10 @@
         teeState.canceled1 = true;
         teeState.reason1 = r;
         if (teeState.canceled2) {
-            @cancelReadableStream(stream, [teeState.reason1, teeState.reason2]).then(teeState.resolvePromise, teeState.rejectPromise);
+            @cancelReadableStream(stream, [teeState.reason1, teeState.reason2]).then(teeState.cancelPromiseCapability.@resolve,
+                                                                                     teeState.cancelPromiseCapability.@reject);
         }
-        return teeState.cancelPromise;
+        return teeState.cancelPromiseCapability.@promise;
     }
 }
 
@@ -165,9 +163,10 @@
         teeState.canceled2 = true;
         teeState.reason2 = r;
         if (teeState.canceled1) {
-            @cancelReadableStream(stream, [teeState.reason1, teeState.reason2]).then(teeState.resolvePromise, teeState.rejectPromise);
+            @cancelReadableStream(stream, [teeState.reason1, teeState.reason2]).then(teeState.cancelPromiseCapability.@resolve,
+                                                                                     teeState.cancelPromiseCapability.@reject);
         }
-        return teeState.cancelPromise;
+        return teeState.cancelPromiseCapability.@promise;
     }
 }
 
@@ -207,14 +206,14 @@
 
     var requests = reader.@readRequests;
     for (var index = 0, length = requests.length; index < length; ++index)
-        @rejectStreamsPromise(requests[index], error);
+        requests[index].@reject.@call(undefined, error);
     reader.@readRequests = [];
 
     @releaseReadableStreamReader(reader);
     reader.@storedError = error;
     reader.@state = @streamErrored;
 
-    @rejectStreamsPromise(reader.@closedPromise, error);
+    reader.@closedPromiseCapability.@reject.@call(undefined, error);
 }
 
 function requestReadableStreamPull(stream)
@@ -314,11 +313,11 @@
 
     var requests = reader.@readRequests;
     for (var index = 0, length = requests.length; index < length; ++index)
-        @resolveStreamsPromise(requests[index], {value:undefined, done: true});
+        requests[index].@resolve.@call(undefined, {value:undefined, done: true});
     reader.@readRequests = [];
     @releaseReadableStreamReader(reader);
     reader.@state = @streamClosed;
-    @resolveStreamsPromise(reader.@closedPromise);
+    reader.@closedPromiseCapability.@resolve.@call(undefined);
 }
 
 function enqueueInReadableStream(stream, chunk)
@@ -330,7 +329,7 @@
     if (stream.@state === @streamClosed)
         return undefined;
     if (@isReadableStreamLocked(stream) && stream.@reader.@readRequests.length) {
-        @resolveStreamsPromise(stream.@reader.@readRequests.shift(), {value: chunk, done: false});
+        stream.@reader.@readRequests.shift().@resolve.@call(undefined, {value: chunk, done: false});
         @requestReadableStreamPull(stream);
         return;
     }
@@ -369,8 +368,8 @@
             @finishClosingReadableStream(stream);
         return Promise.resolve({value: chunk, done: false});
     }
-    var readPromise = @createNewStreamsPromise();
-    reader.@readRequests.push(readPromise);
+    var readPromiseCapability = @newPromiseCapability(Promise);
+    reader.@readRequests.push(readPromiseCapability);
     @requestReadableStreamPull(stream);
-    return readPromise;
+    return readPromiseCapability.@promise;
 }