Title: [194035] trunk/Source/WebCore
Revision
194035
Author
[email protected]
Date
2015-12-14 09:27:27 -0800 (Mon, 14 Dec 2015)

Log Message

[Streams API] Directly use @then as much as possible
https://bugs.webkit.org/show_bug.cgi?id=151631

Reviewed by Darin Adler.

Moved from @Promise.prototype.@then.@call(promise,...) to promise.@then.(...)
for promise objects that are not exposed to user scripts.

Updated promiseInvokeXX stream utility functions to ensure that returned promise always has a @then.
This allows improving the readability of code calling promiseInvokeXX functions.
Changed invokeOrNoop to promiseInvokeOrNoopNoCatch as invokeOrNoop
result is always wrapped as a promise using Promise.resolve.

No change in behavior.

* Modules/streams/ReadableStream.js:
(initializeReadableStream):
* Modules/streams/ReadableStreamInternals.js:
(teeReadableStream):
(teeReadableStreamBranch2CancelFunction):
(cancelReadableStream):
* Modules/streams/StreamInternals.js:
(shieldingPromiseResolve): introduced this routine to ensure the returned promise has a @then property.
(promiseInvokeOrNoopNoCatch):
(promiseInvokeOrNoop):
(promiseInvokeOrFallbackOrNoop):
* Modules/streams/WritableStream.js:
(initializeWritableStream):
(abort):
* Modules/streams/WritableStreamInternals.js:
(callOrScheduleWritableStreamAdvanceQueue):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (194034 => 194035)


--- trunk/Source/WebCore/ChangeLog	2015-12-14 16:27:42 UTC (rev 194034)
+++ trunk/Source/WebCore/ChangeLog	2015-12-14 17:27:27 UTC (rev 194035)
@@ -1,3 +1,37 @@
+2015-12-14  Youenn Fablet  <[email protected]>
+
+        [Streams API] Directly use @then as much as possible
+        https://bugs.webkit.org/show_bug.cgi?id=151631
+
+        Reviewed by Darin Adler.
+
+        Moved from @Promise.prototype.@then.@call(promise,...) to promise.@then.(...)
+        for promise objects that are not exposed to user scripts.
+
+        Updated promiseInvokeXX stream utility functions to ensure that returned promise always has a @then.
+        This allows improving the readability of code calling promiseInvokeXX functions.
+        Changed invokeOrNoop to promiseInvokeOrNoopNoCatch as invokeOrNoop
+        result is always wrapped as a promise using Promise.resolve.
+
+        No change in behavior.
+
+        * Modules/streams/ReadableStream.js:
+        (initializeReadableStream):
+        * Modules/streams/ReadableStreamInternals.js:
+        (teeReadableStream):
+        (teeReadableStreamBranch2CancelFunction):
+        (cancelReadableStream):
+        * Modules/streams/StreamInternals.js:
+        (shieldingPromiseResolve): introduced this routine to ensure the returned promise has a @then property.
+        (promiseInvokeOrNoopNoCatch):
+        (promiseInvokeOrNoop):
+        (promiseInvokeOrFallbackOrNoop):
+        * Modules/streams/WritableStream.js:
+        (initializeWritableStream):
+        (abort):
+        * Modules/streams/WritableStreamInternals.js:
+        (callOrScheduleWritableStreamAdvanceQueue):
+
 2015-12-14  Xabier Rodriguez Calvar  <[email protected]> and Youenn Fablet  <[email protected]>
 
         [Streams API] Expose ReadableStream and relatives to Worker

Modified: trunk/Source/WebCore/Modules/streams/ReadableStream.js (194034 => 194035)


--- trunk/Source/WebCore/Modules/streams/ReadableStream.js	2015-12-14 16:27:42 UTC (rev 194034)
+++ trunk/Source/WebCore/Modules/streams/ReadableStream.js	2015-12-14 17:27:27 UTC (rev 194035)
@@ -55,8 +55,7 @@
     this.@controller = new @ReadableStreamController(this);
     this.@strategy = @validateAndNormalizeQueuingStrategy(strategy.size, strategy.highWaterMark);
 
-    const result = @invokeOrNoop(underlyingSource, "start", [this.@controller]);
-    @Promise.prototype.@then.@call(@Promise.@resolve(result), () => {
+    @promiseInvokeOrNoopNoCatch(underlyingSource, "start", [this.@controller]).@then(() => {
         this.@started = true;
         @requestReadableStreamPull(this);
     }, (error) => {
@@ -104,8 +103,9 @@
 {
     "use strict";
 
-    // We are not shielding against methods and attributes of the reader and destination as those objects don't have to
-    // be necessarily ReadableStreamReader and WritableStream.
+    // FIXME: rewrite pipeTo so as to require to have 'this' as a ReadableStream and destination be a WritableStream.
+    // See https://github.com/whatwg/streams/issues/407.
+    // We should shield the pipeTo implementation at the same time.
 
     const preventClose = @isObject(options) && !!options.preventClose;
     const preventAbort = @isObject(options) && !!options.preventAbort;

Modified: trunk/Source/WebCore/Modules/streams/ReadableStreamInternals.js (194034 => 194035)


--- trunk/Source/WebCore/Modules/streams/ReadableStreamInternals.js	2015-12-14 16:27:42 UTC (rev 194034)
+++ trunk/Source/WebCore/Modules/streams/ReadableStreamInternals.js	2015-12-14 17:27:27 UTC (rev 194035)
@@ -96,7 +96,7 @@
         "cancel": @teeReadableStreamBranch2CancelFunction(teeState, stream)
     });
 
-    @Promise.prototype.@then.@call(reader.closed, undefined, function(e) {
+    reader.@closedPromiseCapability.@promise.@then(undefined, function(e) {
         if (teeState.closedOrErrored)
             return;
         @errorReadableStream(branch1, e);
@@ -146,9 +146,9 @@
         teeState.canceled1 = true;
         teeState.reason1 = r;
         if (teeState.canceled2) {
-            @Promise.prototype.@then.@call(@cancelReadableStream(stream, [teeState.reason1, teeState.reason2]),
-                                           teeState.cancelPromiseCapability.@resolve,
-                                           teeState.cancelPromiseCapability.@reject);
+            @cancelReadableStream(stream, [teeState.reason1, teeState.reason2]).@then(
+                teeState.cancelPromiseCapability.@resolve,
+                teeState.cancelPromiseCapability.@reject);
         }
         return teeState.cancelPromiseCapability.@promise;
     }
@@ -162,9 +162,9 @@
         teeState.canceled2 = true;
         teeState.reason2 = r;
         if (teeState.canceled1) {
-            @Promise.prototype.@then.@call(@cancelReadableStream(stream, [teeState.reason1, teeState.reason2]),
-                                           teeState.cancelPromiseCapability.@resolve,
-                                           teeState.cancelPromiseCapability.@reject);
+            @cancelReadableStream(stream, [teeState.reason1, teeState.reason2]).@then(
+                teeState.cancelPromiseCapability.@resolve,
+                teeState.cancelPromiseCapability.@reject);
         }
         return teeState.cancelPromiseCapability.@promise;
     }
@@ -234,8 +234,7 @@
 
     stream.@pulling = true;
 
-    const promise = @promiseInvokeOrNoop(stream.@underlyingSource, "pull", [stream.@controller]);
-    @Promise.prototype.@then.@call(promise, function() {
+    @promiseInvokeOrNoop(stream.@underlyingSource, "pull", [stream.@controller]).@then(function() {
         stream.@pulling = false;
         if (stream.@pullAgain) {
             stream.@pullAgain = false;
@@ -273,7 +272,7 @@
         return @Promise.@reject(stream.@storedError);
     stream.@queue = @newQueue();
     @finishClosingReadableStream(stream);
-    return @Promise.prototype.@then.@call(@promiseInvokeOrNoop(stream.@underlyingSource, "cancel", [reason]), function() { });
+    return @promiseInvokeOrNoop(stream.@underlyingSource, "cancel", [reason]).@then(function() { });
 }
 
 function finishClosingReadableStream(stream)

Modified: trunk/Source/WebCore/Modules/streams/StreamInternals.js (194034 => 194035)


--- trunk/Source/WebCore/Modules/streams/StreamInternals.js	2015-12-14 16:27:42 UTC (rev 194034)
+++ trunk/Source/WebCore/Modules/streams/StreamInternals.js	2015-12-14 17:27:27 UTC (rev 194035)
@@ -27,14 +27,22 @@
 // @conditional=ENABLE(STREAMS_API)
 // @internal
 
-function invokeOrNoop(object, key, args)
+function shieldingPromiseResolve(result)
 {
+    const promise = @Promise.@resolve(result);
+    if (promise.@then === undefined)
+        promise.@then = @Promise.prototype.@then;
+    return promise;
+}
+
+function promiseInvokeOrNoopNoCatch(object, key, args)
+{
     "use strict";
 
     const method = object[key];
-    if (typeof method === "undefined")
-        return;
-    return method.@apply(object, args);
+    if (method === undefined)
+        return @Promise.@resolve();
+    return @shieldingPromiseResolve(method.@apply(object, args));
 }
 
 function promiseInvokeOrNoop(object, key, args)
@@ -42,10 +50,7 @@
     "use strict";
 
     try {
-        const method = object[key];
-        if (typeof method === "undefined")
-            return @Promise.@resolve();
-        return @Promise.@resolve(method.@apply(object, args));
+        return @promiseInvokeOrNoopNoCatch(object, key, args);
     }
     catch(error) {
         return @Promise.@reject(error);
@@ -59,9 +64,9 @@
 
     try {
         const method = object[key1];
-        if (typeof method === "undefined")
-            return @promiseInvokeOrNoop(object, key2, args2);
-        return @Promise.@resolve(method.@apply(object, args1));
+        if (method === undefined)
+            return @promiseInvokeOrNoopNoCatch(object, key2, args2);
+        return @shieldingPromiseResolve(method.@apply(object, args1));
     }
     catch(error) {
         return @Promise.@reject(error);

Modified: trunk/Source/WebCore/Modules/streams/WritableStream.js (194034 => 194035)


--- trunk/Source/WebCore/Modules/streams/WritableStream.js	2015-12-14 16:27:42 UTC (rev 194034)
+++ trunk/Source/WebCore/Modules/streams/WritableStream.js	2015-12-14 17:27:27 UTC (rev 194035)
@@ -56,8 +56,8 @@
     const errorFunction = (e) => {
         @errorWritableStream(this, e);
     };
-    this.@startedPromise = @Promise.@resolve(@invokeOrNoop(underlyingSink, "start", [errorFunction]));
-    @Promise.prototype.@then.@call(this.@startedPromise, () => {
+    this.@startedPromise = @promiseInvokeOrNoopNoCatch(underlyingSink, "start", [errorFunction]);
+    this.@startedPromise.@then(() => {
         this.@started = true;
         this.@startedPromise = undefined;
     }, errorFunction);
@@ -80,9 +80,7 @@
 
     @errorWritableStream(this, reason);
 
-    const sinkAbortPromise = @promiseInvokeOrFallbackOrNoop(this.@underlyingSink, "abort", [reason], "close", []);
-
-    return @Promise.prototype.@then.@call(sinkAbortPromise, function() { });
+    return @promiseInvokeOrFallbackOrNoop(this.@underlyingSink, "abort", [reason], "close", []).@then(function() { });
 }
 
 function close()

Modified: trunk/Source/WebCore/Modules/streams/WritableStreamInternals.js (194034 => 194035)


--- trunk/Source/WebCore/Modules/streams/WritableStreamInternals.js	2015-12-14 16:27:42 UTC (rev 194034)
+++ trunk/Source/WebCore/Modules/streams/WritableStreamInternals.js	2015-12-14 17:27:27 UTC (rev 194035)
@@ -77,7 +77,7 @@
     "use strict";
 
     if (!stream.@started)
-        @Promise.prototype.@then.@call(stream.@startedPromise, function() { @writableStreamAdvanceQueue(stream); });
+        stream.@startedPromise.@then(function() { @writableStreamAdvanceQueue(stream); });
     else
         @writableStreamAdvanceQueue(stream);
 }
@@ -99,7 +99,7 @@
     }
 
     stream.@writing = true;
-    @Promise.prototype.@then.@call(@promiseInvokeOrNoop(stream.@underlyingSink, "write", [writeRecord.chunk]),
+    @promiseInvokeOrNoop(stream.@underlyingSink, "write", [writeRecord.chunk]).@then(
         function() {
             if (stream.@state === @streamErrored)
                 return;
@@ -120,7 +120,7 @@
     "use strict";
 
     @assert(stream.@state === @streamClosing);
-    @Promise.prototype.@then.@call(@promiseInvokeOrNoop(stream.@underlyingSink, "close"),
+    @promiseInvokeOrNoop(stream.@underlyingSink, "close").@then(
         function() {
             if (stream.@state === @streamErrored)
                 return;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to