[Streams API] Rework promises to use @newPromiseCapability
authorcalvaris@igalia.com <calvaris@igalia.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 3 Nov 2015 14:52:04 +0000 (14:52 +0000)
committercalvaris@igalia.com <calvaris@igalia.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 3 Nov 2015 14:52:04 +0000 (14:52 +0000)
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: https://svn.webkit.org/repository/webkit/trunk@191950 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/Modules/streams/ReadableStreamInternals.js
Source/WebCore/Modules/streams/ReadableStreamReader.js
Source/WebCore/Modules/streams/StreamInternals.js
Source/WebCore/Modules/streams/WritableStream.js
Source/WebCore/Modules/streams/WritableStreamInternals.js
Source/WebCore/bindings/js/WebCoreBuiltinNames.h

index 13694e7fd395f1c0446074ad95e2018e5e415bf6..4b1aef660c9ce15f6b2f30dae2ba26bec8502129 100644 (file)
@@ -1,3 +1,45 @@
+2015-11-03  Xabier Rodriguez Calvar  <calvaris@igalia.com>
+
+        [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:
+
 2015-11-03  Youenn Fablet  <youenn.fablet@crf.canon.fr>
 
         Move webkitGetUserMedia to JS Builtin
index 9f3f7cd67a209534040dd83105e401a01fb913ec..c79d6a9b70c9602c44693ada4d5670ef420e5419 100644 (file)
@@ -42,19 +42,19 @@ function privateInitializeReadableStreamReader(stream)
         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 @@ function teeReadableStream(stream, shouldClone)
         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 @@ function teeReadableStreamBranch1CancelFunction(teeState, stream)
         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 @@ function teeReadableStreamBranch2CancelFunction(teeState, stream)
         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 @@ function errorReadableStream(stream, error)
 
     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 @@ function closeReadableStreamReader(reader)
 
     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 @@ function enqueueInReadableStream(stream, chunk)
     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 @@ function readFromReadableStreamReader(reader)
             @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;
 }
index 9baa49e328c75945f337c4736be3e872bb2770f3..815a2267e37cfd17d498b2440b4c278fa284247e 100644 (file)
@@ -76,5 +76,5 @@ function closed()
     if (!@isReadableStreamReader(this))
         return Promise.reject(new @TypeError("Callee of closed is not a ReadableStreamReader"));
 
-    return this.@closedPromise;
+    return this.@closedPromiseCapability.@promise;
 }
index cb23af3779d64c1f60a0b6dfd504cc7132e4980b..0bbff4efa38e46d81ba9a32ed933997eea31e793 100644 (file)
@@ -90,43 +90,6 @@ function validateAndNormalizeQueuingStrategy(size, highWaterMark)
     return normalizedStrategy;
 }
 
-function createNewStreamsPromise()
-{
-    "use strict";
-
-    var resolveFunction;
-    var rejectFunction;
-    var promise = new Promise(function(resolve, reject) {
-        resolveFunction = resolve;
-        rejectFunction = reject;
-    });
-    promise.@resolve = resolveFunction;
-    promise.@reject = rejectFunction;
-    return promise;
-}
-
-function resolveStreamsPromise(promise, value)
-{
-    "use strict";
-
-    if (promise && promise.@resolve) {
-        promise.@resolve(value);
-        promise.@resolve = undefined;
-        promise.@reject = undefined;
-    }
-}
-
-function rejectStreamsPromise(promise, value)
-{
-    "use strict";
-
-    if (promise && promise.@reject) {
-        promise.@reject(value);
-        promise.@resolve = undefined;
-        promise.@reject = undefined;
-    }
-}
-
 function newQueue()
 {
     return { content: [], size: 0 };
index 5dc8ab9dca4a3d3c7c052c99239241964bf8ab8c..5d5d4a85e5ed55c68bc8405115d790c8f98b480d 100644 (file)
@@ -42,8 +42,8 @@ function initializeWritableStream(underlyingSink, strategy)
         throw new @TypeError("WritableStream constructor takes an object as second argument, if any");
 
     this.@underlyingSink = underlyingSink;
-    this.@closedPromise = @createNewStreamsPromise();
-    this.@readyPromise = Promise.resolve(undefined);
+    this.@closedPromiseCapability = @newPromiseCapability(Promise);
+    this.@readyPromiseCapability = { @promise: Promise.resolve(undefined), @resolve: function() {}, @reject: function() {} };
     this.@queue = @newQueue();
     this.@state = @streamWritable;
     this.@started = false;
@@ -99,13 +99,13 @@ function close()
         return Promise.reject(this.@storedError);
 
     if (this.@state === @streamWaiting)
-        @resolveStreamsPromise(this.@readyPromise, undefined);
+        this.@readyPromiseCapability.@resolve.@call(undefined, undefined);
 
     this.@state = @streamClosing;
     @enqueueValueWithSize(this.@queue, "close", 0);
     @callOrScheduleWritableStreamAdvanceQueue(this);
 
-    return this.@closedPromise;
+    return this.@closedPromiseCapability.@promise;
 }
 
 function write(chunk)
@@ -134,9 +134,9 @@ function write(chunk)
         }
     }
 
-    const promise = @createNewStreamsPromise();
+    const promiseCapability = @newPromiseCapability(Promise);
     try {
-        @enqueueValueWithSize(this.@queue, { promise: promise, chunk: chunk }, chunkSize);
+        @enqueueValueWithSize(this.@queue, { promiseCapability: promiseCapability, chunk: chunk }, chunkSize);
     } catch (e) {
         @errorWritableStream.@call(this, e);
         return Promise.reject(e);
@@ -145,7 +145,7 @@ function write(chunk)
     @syncWritableStreamStateWithQueue(this);
     @callOrScheduleWritableStreamAdvanceQueue(this);
 
-    return promise;
+    return promiseCapability.@promise;
 }
 
 function closed()
@@ -155,7 +155,7 @@ function closed()
     if (!@isWritableStream(this))
         return Promise.reject(new @TypeError("The WritableStream.closed getter can only be used on instances of WritableStream"));
 
-    return this.@closedPromise;
+    return this.@closedPromiseCapability.@promise;
 }
 
 function ready()
@@ -165,7 +165,7 @@ function ready()
     if (!@isWritableStream(this))
         return Promise.reject(new @TypeError("The WritableStream.ready getter can only be used on instances of WritableStream"));
 
-    return this.@readyPromise;
+    return this.@readyPromiseCapability.@promise;
 }
 
 function state()
index eb77bf5df7eba7cc67852719718b6c9d7801c2ad..17df09e227d2bbb397a9686a7face56cf5772023 100644 (file)
@@ -46,10 +46,10 @@ function syncWritableStreamStateWithQueue(stream)
 
     if (stream.@queue.size > stream.@strategy.highWaterMark) {
         stream.@state = @streamWaiting;
-        stream.@readyPromise = @createNewStreamsPromise();
+        stream.@readyPromiseCapability = @newPromiseCapability(Promise);
     } else {
         stream.@state = @streamWritable;
-        @resolveStreamsPromise(stream.@readyPromise, undefined);
+        stream.@readyPromiseCapability.@resolve.@call(undefined, undefined);
     }
 
     return undefined;
@@ -63,13 +63,13 @@ function errorWritableStream(e)
         return undefined;
     while (this.@queue.content.length > 0) {
         var writeRecord = @dequeueValue(this.@queue);
-        if (writeRecord !== "close" && writeRecord.promise)
-            @rejectStreamsPromise(writeRecord.promise, e);
+        if (writeRecord !== "close")
+            writeRecord.promiseCapability.@reject.@call(undefined, e);
     }
     this.@storedError = e;
     if (this.@state === @streamWaiting)
-        @resolveStreamsPromise(this.@readyPromise, undefined);
-    @rejectStreamsPromise(this.@closedPromise, e);
+        this.@readyPromiseCapability.@resolve.@call(undefined, undefined);
+    this.@closedPromiseCapability.@reject.@call(undefined, e);
     this.@state = @streamErrored;
     return undefined;
 }
@@ -106,7 +106,7 @@ function writableStreamAdvanceQueue(stream)
             if (stream.@state === @streamErrored)
                 return;
             stream.@writing = false;
-            @resolveStreamsPromise(writeRecord.promise, undefined);
+            writeRecord.promiseCapability.@resolve.@call(undefined, undefined);
             @dequeueValue(stream.@queue);
             @syncWritableStreamStateWithQueue(stream);
             @writableStreamAdvanceQueue(stream);
@@ -129,7 +129,7 @@ function closeWritableStream(stream)
                 return;
             // FIXME
             // assert(stream.@state === @streamClosing);
-            @resolveStreamsPromise(stream.@closedPromise, undefined);
+            stream.@closedPromiseCapability.@resolve.@call(undefined, undefined);
             stream.@state = @streamClosed;
         },
         function(r) {
index b6d0618fff5e87c15e27a4aabfb679630312e5ac..694b317c69f9cf4a2e3cb67b04ae3838553b9a09 100644 (file)
@@ -33,7 +33,7 @@ namespace WebCore {
 
 #define WEBCORE_COMMON_PRIVATE_IDENTIFIERS_EACH_PROPERTY_NAME(macro)\
     macro(closeRequested) \
-    macro(closedPromise) \
+    macro(closedPromiseCapability) \
     macro(controlledReadableStream) \
     macro(controller) \
     macro(getUserMediaFromJS) \
@@ -43,7 +43,7 @@ namespace WebCore {
     macro(queue) \
     macro(reader) \
     macro(readRequests) \
-    macro(readyPromise) \
+    macro(readyPromiseCapability) \
     macro(state) \
     macro(started) \
     macro(startedPromise) \