<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[185537] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/185537">185537</a></dd>
<dt>Author</dt> <dd>youenn.fablet@crf.canon.fr</dd>
<dt>Date</dt> <dd>2015-06-13 02:09:53 -0700 (Sat, 13 Jun 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>[Streams API] ReadableJSStream should handle promises returned by JS source start callback
https://bugs.webkit.org/show_bug.cgi?id=145792

Source/JavaScriptCore:

Reviewed by Darin Adler.

Added support for JSFunction implemented by std::function.

* runtime/JSFunction.cpp:
(JSC::getNativeExecutable): Refactored code to share it with the two JSFunction::create
(JSC::JSFunction::create):
(JSC::runStdFunction):
* runtime/JSFunction.h: Added std::function based JSFunction::create prototype.
* runtime.JSPromise.h:

Source/WebCore:

Reviewed by Darin Adler.

Covered by rebased tests.

When calling start callback, the returned value is checked.
If it is not a promise, we do as if it is a resolved promise.
If it is a promise, we call its then() method with two resolve/reject JS functions.

* Modules/streams/ReadableStream.cpp:
* bindings/js/ReadableJSStream.cpp:
(WebCore::ReadableJSStream::invoke): Returns a JSPromise* if any is returned by the JS source callback.
(WebCore::thenPromise): Utility method to call the promise.
(WebCore::createStartResultFulfilledFunction): The promise resolve callback.
(WebCore::ReadableJSStream::doStart): Calls thenPromise if a JSPromise* is returned by invoke.
(WebCore::ReadableJSStream::ReadableJSStream):
* bindings/js/ReadableJSStream.h:

LayoutTests:

Reviewed by Darin Adler.

Rebasing expectations, and removing timeouts for tests that no longer timeout.

* streams/reference-implementation/readable-stream-expected.txt:
* streams/reference-implementation/readable-stream.html:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkLayoutTestsstreamsreferenceimplementationreadablestreamexpectedtxt">trunk/LayoutTests/streams/reference-implementation/readable-stream-expected.txt</a></li>
<li><a href="#trunkLayoutTestsstreamsreferenceimplementationreadablestreamhtml">trunk/LayoutTests/streams/reference-implementation/readable-stream.html</a></li>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeJSFunctioncpp">trunk/Source/JavaScriptCore/runtime/JSFunction.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeJSFunctionh">trunk/Source/JavaScriptCore/runtime/JSFunction.h</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeJSPromiseh">trunk/Source/JavaScriptCore/runtime/JSPromise.h</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreModulesstreamsReadableStreamcpp">trunk/Source/WebCore/Modules/streams/ReadableStream.cpp</a></li>
<li><a href="#trunkSourceWebCorebindingsjsReadableJSStreamcpp">trunk/Source/WebCore/bindings/js/ReadableJSStream.cpp</a></li>
<li><a href="#trunkSourceWebCorebindingsjsReadableJSStreamh">trunk/Source/WebCore/bindings/js/ReadableJSStream.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (185536 => 185537)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2015-06-13 08:29:23 UTC (rev 185536)
+++ trunk/LayoutTests/ChangeLog        2015-06-13 09:09:53 UTC (rev 185537)
</span><span class="lines">@@ -1,3 +1,15 @@
</span><ins>+2015-06-13  Xabier Rodriguez Calvar  &lt;calvaris@igalia.com&gt; and Youenn Fablet &lt;youenn.fablet@crf.canon.fr&gt;
+
+        [Streams API] ReadableJSStream should handle promises returned by JS source start callback
+        https://bugs.webkit.org/show_bug.cgi?id=145792
+
+        Reviewed by Darin Adler.
+
+        Rebasing expectations, and removing timeouts for tests that no longer timeout.
+
+        * streams/reference-implementation/readable-stream-expected.txt:
+        * streams/reference-implementation/readable-stream.html:
+
</ins><span class="cx"> 2015-06-13  Andres Gonzalez  &lt;agonzalez334@nc.rr.com&gt;
</span><span class="cx"> 
</span><span class="cx">         AX: WebKit exposes all Ruby Text as Unknown (Japanese EPUB accessibility blocker)
</span></span></pre></div>
<a id="trunkLayoutTestsstreamsreferenceimplementationreadablestreamexpectedtxt"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/streams/reference-implementation/readable-stream-expected.txt (185536 => 185537)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/streams/reference-implementation/readable-stream-expected.txt        2015-06-13 08:29:23 UTC (rev 185536)
+++ trunk/LayoutTests/streams/reference-implementation/readable-stream-expected.txt        2015-06-13 09:09:53 UTC (rev 185537)
</span><span class="lines">@@ -5,8 +5,8 @@
</span><span class="cx"> PASS ReadableStream constructor can get initial garbage as cancel argument 
</span><span class="cx"> PASS ReadableStream constructor can get initial garbage as pull argument 
</span><span class="cx"> PASS ReadableStream constructor can get initial garbage as strategy argument 
</span><del>-TIMEOUT ReadableStream start should be able to return a promise Test timed out
-TIMEOUT ReadableStream start should be able to return a promise and reject it Test timed out
</del><ins>+PASS ReadableStream start should be able to return a promise 
+PASS ReadableStream start should be able to return a promise and reject it 
</ins><span class="cx"> PASS ReadableStream should be able to enqueue different objects. 
</span><span class="cx"> PASS ReadableStream: if start throws an error, it should be re-thrown 
</span><span class="cx"> TIMEOUT ReadableStream: if pull rejects, it should error the stream Test timed out
</span><span class="lines">@@ -20,6 +20,6 @@
</span><span class="cx"> FAIL ReadableStream: should call underlying source methods as methods releaseLock is not implemented
</span><span class="cx"> FAIL ReadableStream strategies: the default strategy should return false for all but the first enqueue call assert_equals: first enqueue should return true expected (boolean) true but got (undefined) undefined
</span><span class="cx"> FAIL ReadableStream strategies: the default strategy should continue returning true from enqueue if the chunks are read immediately assert_equals: first enqueue should return true expected (boolean) true but got (undefined) undefined
</span><del>-TIMEOUT ReadableStream integration test: adapting a random push source Test timed out
</del><ins>+PASS ReadableStream integration test: adapting a random push source 
</ins><span class="cx"> PASS ReadableStream integration test: adapting a sync pull source 
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkLayoutTestsstreamsreferenceimplementationreadablestreamhtml"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/streams/reference-implementation/readable-stream.html (185536 => 185537)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/streams/reference-implementation/readable-stream.html        2015-06-13 08:29:23 UTC (rev 185536)
+++ trunk/LayoutTests/streams/reference-implementation/readable-stream.html        2015-06-13 09:09:53 UTC (rev 185537)
</span><span class="lines">@@ -53,7 +53,7 @@
</span><span class="cx">     new ReadableStream({ strategy: 2 }); // Constructor should not throw when strategy is not an object.
</span><span class="cx"> }, 'ReadableStream constructor can get initial garbage as strategy argument');
</span><span class="cx"> 
</span><del>-var test1 = async_test('ReadableStream start should be able to return a promise', { timeout: 50 });
</del><ins>+var test1 = async_test('ReadableStream start should be able to return a promise');
</ins><span class="cx"> test1.step(function()
</span><span class="cx"> {
</span><span class="cx">     var readCalled = false;
</span><span class="lines">@@ -82,7 +82,7 @@
</span><span class="cx">     }));
</span><span class="cx"> });
</span><span class="cx"> 
</span><del>-var test2 = async_test('ReadableStream start should be able to return a promise and reject it', { timeout: 100 });
</del><ins>+var test2 = async_test('ReadableStream start should be able to return a promise and reject it');
</ins><span class="cx"> test2.step(function()
</span><span class="cx"> {
</span><span class="cx">     var theError = new Error('rejected!');
</span><span class="lines">@@ -658,7 +658,7 @@
</span><span class="cx">     })).catch(test17.step_func(function(e) { assert_unreached(e); } ));
</span><span class="cx"> });
</span><span class="cx"> 
</span><del>-var test18 = async_test('ReadableStream integration test: adapting a random push source', { timeout: 50 });
</del><ins>+var test18 = async_test('ReadableStream integration test: adapting a random push source');
</ins><span class="cx"> test18.step(function() {
</span><span class="cx">     var pullChecked = false;
</span><span class="cx">     var randomSource = new RandomPushSource(8);
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (185536 => 185537)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2015-06-13 08:29:23 UTC (rev 185536)
+++ trunk/Source/JavaScriptCore/ChangeLog        2015-06-13 09:09:53 UTC (rev 185537)
</span><span class="lines">@@ -1,3 +1,19 @@
</span><ins>+2015-06-13  Youenn Fablet  &lt;youenn.fablet@crf.canon.fr&gt;
+
+        [Streams API] ReadableJSStream should handle promises returned by JS source start callback
+        https://bugs.webkit.org/show_bug.cgi?id=145792
+
+        Reviewed by Darin Adler.
+
+        Added support for JSFunction implemented by std::function.
+
+        * runtime/JSFunction.cpp:
+        (JSC::getNativeExecutable): Refactored code to share it with the two JSFunction::create
+        (JSC::JSFunction::create):
+        (JSC::runStdFunction):
+        * runtime/JSFunction.h: Added std::function based JSFunction::create prototype.
+        * runtime.JSPromise.h:
+
</ins><span class="cx"> 2015-06-12  Gyuyoung Kim  &lt;gyuyoung.kim@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Purge PassRefPtr in JavaScriptCore - 2
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeJSFunctioncpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/JSFunction.cpp (185536 => 185537)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/JSFunction.cpp        2015-06-13 08:29:23 UTC (rev 185536)
+++ trunk/Source/JavaScriptCore/runtime/JSFunction.cpp        2015-06-13 09:09:53 UTC (rev 185537)
</span><span class="lines">@@ -4,6 +4,7 @@
</span><span class="cx">  *  Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2015 Apple Inc. All rights reserved.
</span><span class="cx">  *  Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
</span><span class="cx">  *  Copyright (C) 2007 Maks Orlovich
</span><ins>+ *  Copyright (C) 2015 Canon Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  *  This library is free software; you can redistribute it and/or
</span><span class="cx">  *  modify it under the terms of the GNU Library General Public
</span><span class="lines">@@ -66,25 +67,53 @@
</span><span class="cx">     return result;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-JSFunction* JSFunction::create(VM&amp; vm, JSGlobalObject* globalObject, int length, const String&amp; name, NativeFunction nativeFunction, Intrinsic intrinsic, NativeFunction nativeConstructor)
</del><ins>+static inline NativeExecutable* getNativeExecutable(VM&amp; vm, NativeFunction nativeFunction, Intrinsic intrinsic, NativeFunction nativeConstructor)
</ins><span class="cx"> {
</span><del>-    NativeExecutable* executable;
</del><span class="cx"> #if !ENABLE(JIT)
</span><span class="cx">     UNUSED_PARAM(intrinsic);
</span><span class="cx"> #else
</span><span class="cx">     if (intrinsic != NoIntrinsic &amp;&amp; vm.canUseJIT()) {
</span><span class="cx">         ASSERT(nativeConstructor == callHostFunctionAsConstructor);
</span><del>-        executable = vm.getHostFunction(nativeFunction, intrinsic);
-    } else
</del><ins>+        return vm.getHostFunction(nativeFunction, intrinsic);
+    }
</ins><span class="cx"> #endif
</span><del>-        executable = vm.getHostFunction(nativeFunction, nativeConstructor);
</del><ins>+    return vm.getHostFunction(nativeFunction, nativeConstructor);
+}
</ins><span class="cx"> 
</span><ins>+JSFunction* JSFunction::create(VM&amp; vm, JSGlobalObject* globalObject, int length, const String&amp; name, NativeFunction nativeFunction, Intrinsic intrinsic, NativeFunction nativeConstructor)
+{
+    NativeExecutable* executable = getNativeExecutable(vm, nativeFunction, intrinsic, nativeConstructor);
</ins><span class="cx">     JSFunction* function = new (NotNull, allocateCell&lt;JSFunction&gt;(vm.heap)) JSFunction(vm, globalObject, globalObject-&gt;functionStructure());
</span><span class="cx">     // Can't do this during initialization because getHostFunction might do a GC allocation.
</span><span class="cx">     function-&gt;finishCreation(vm, executable, length, name);
</span><span class="cx">     return function;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+class JSStdFunction : public JSFunction {
+public:
+    JSStdFunction(VM&amp; vm, JSGlobalObject* object, Structure* structure, NativeStdFunction&amp;&amp; function)
+        : JSFunction(vm, object, structure)
+        , stdFunction(WTF::move(function)) { }
+
+    NativeStdFunction stdFunction;
+};
+
+static EncodedJSValue JSC_HOST_CALL runStdFunction(ExecState* state)
+{
+    JSStdFunction* jsFunction = jsCast&lt;JSStdFunction*&gt;(state-&gt;callee());
+    ASSERT(jsFunction);
+    return jsFunction-&gt;stdFunction(state);
+}
+
+JSFunction* JSFunction::create(VM&amp; vm, JSGlobalObject* globalObject, int length, const String&amp; name, NativeStdFunction&amp;&amp; nativeStdFunction, Intrinsic intrinsic, NativeFunction nativeConstructor)
+{
+    NativeExecutable* executable = getNativeExecutable(vm, runStdFunction, intrinsic, nativeConstructor);
+    JSStdFunction* function = new (NotNull, allocateCell&lt;JSStdFunction&gt;(vm.heap)) JSStdFunction(vm, globalObject, globalObject-&gt;functionStructure(), WTF::move(nativeStdFunction));
+    // Can't do this during initialization because getHostFunction might do a GC allocation.
+    function-&gt;finishCreation(vm, executable, length, name);
+    return function;
+}
+
</ins><span class="cx"> JSFunction::JSFunction(VM&amp; vm, JSGlobalObject* globalObject, Structure* structure)
</span><span class="cx">     : Base(vm, globalObject, structure)
</span><span class="cx">     , m_executable()
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeJSFunctionh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/JSFunction.h (185536 => 185537)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/JSFunction.h        2015-06-13 08:29:23 UTC (rev 185536)
+++ trunk/Source/JavaScriptCore/runtime/JSFunction.h        2015-06-13 09:09:53 UTC (rev 185537)
</span><span class="lines">@@ -45,6 +45,8 @@
</span><span class="cx"> class JITCompiler;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+typedef std::function&lt;EncodedJSValue (ExecState*)&gt; NativeStdFunction;
+
</ins><span class="cx"> JS_EXPORT_PRIVATE EncodedJSValue JSC_HOST_CALL callHostFunctionAsConstructor(ExecState*);
</span><span class="cx"> 
</span><span class="cx"> JS_EXPORT_PRIVATE String getCalculatedDisplayName(CallFrame*, JSObject*);
</span><span class="lines">@@ -68,6 +70,7 @@
</span><span class="cx">     JS_EXPORT_PRIVATE static JSFunction* create(VM&amp;, JSGlobalObject*, int length, const String&amp; name, NativeFunction, Intrinsic = NoIntrinsic, NativeFunction nativeConstructor = callHostFunctionAsConstructor);
</span><span class="cx">     
</span><span class="cx">     static JSFunction* createWithInvalidatedReallocationWatchpoint(VM&amp;, FunctionExecutable*, JSScope*);
</span><ins>+    JS_EXPORT_PRIVATE static JSFunction* create(VM&amp;, JSGlobalObject*, int length, const String&amp; name, NativeStdFunction&amp;&amp;, Intrinsic = NoIntrinsic, NativeFunction nativeConstructor = callHostFunctionAsConstructor);
</ins><span class="cx"> 
</span><span class="cx">     static JSFunction* create(VM&amp;, FunctionExecutable*, JSScope*);
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeJSPromiseh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/JSPromise.h (185536 => 185537)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/JSPromise.h        2015-06-13 08:29:23 UTC (rev 185536)
+++ trunk/Source/JavaScriptCore/runtime/JSPromise.h        2015-06-13 09:09:53 UTC (rev 185537)
</span><span class="lines">@@ -42,7 +42,7 @@
</span><span class="cx">     static JSPromise* create(VM&amp;, JSGlobalObject*, JSPromiseConstructor*);
</span><span class="cx">     static Structure* createStructure(VM&amp;, JSGlobalObject*, JSValue);
</span><span class="cx"> 
</span><del>-    DECLARE_INFO;
</del><ins>+    DECLARE_EXPORT_INFO;
</ins><span class="cx"> 
</span><span class="cx">     enum class Status {
</span><span class="cx">         Unresolved,
</span></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (185536 => 185537)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2015-06-13 08:29:23 UTC (rev 185536)
+++ trunk/Source/WebCore/ChangeLog        2015-06-13 09:09:53 UTC (rev 185537)
</span><span class="lines">@@ -1,3 +1,25 @@
</span><ins>+2015-06-13  Xabier Rodriguez Calvar  &lt;calvaris@igalia.com&gt; and Youenn Fablet &lt;youenn.fablet@crf.canon.fr&gt;
+
+        [Streams API] ReadableJSStream should handle promises returned by JS source start callback
+        https://bugs.webkit.org/show_bug.cgi?id=145792
+
+        Reviewed by Darin Adler.
+
+        Covered by rebased tests.
+
+        When calling start callback, the returned value is checked.
+        If it is not a promise, we do as if it is a resolved promise.
+        If it is a promise, we call its then() method with two resolve/reject JS functions.
+
+        * Modules/streams/ReadableStream.cpp:
+        * bindings/js/ReadableJSStream.cpp:
+        (WebCore::ReadableJSStream::invoke): Returns a JSPromise* if any is returned by the JS source callback.
+        (WebCore::thenPromise): Utility method to call the promise.
+        (WebCore::createStartResultFulfilledFunction): The promise resolve callback.
+        (WebCore::ReadableJSStream::doStart): Calls thenPromise if a JSPromise* is returned by invoke.
+        (WebCore::ReadableJSStream::ReadableJSStream):
+        * bindings/js/ReadableJSStream.h:
+
</ins><span class="cx"> 2015-06-13  Andres Gonzalez  &lt;agonzalez334@nc.rr.com&gt;
</span><span class="cx"> 
</span><span class="cx">         AX: WebKit exposes all Ruby Text as Unknown (Japanese EPUB accessibility blocker)
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesstreamsReadableStreamcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/streams/ReadableStream.cpp (185536 => 185537)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/streams/ReadableStream.cpp        2015-06-13 08:29:23 UTC (rev 185536)
+++ trunk/Source/WebCore/Modules/streams/ReadableStream.cpp        2015-06-13 09:09:53 UTC (rev 185537)
</span><span class="lines">@@ -32,7 +32,6 @@
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(STREAMS_API)
</span><span class="cx"> 
</span><del>-#include &quot;NotImplemented.h&quot;
</del><span class="cx"> #include &quot;ReadableStreamReader.h&quot;
</span><span class="cx"> #include &lt;runtime/JSCJSValueInlines.h&gt;
</span><span class="cx"> #include &lt;wtf/RefCountedLeakCounter.h&gt;
</span></span></pre></div>
<a id="trunkSourceWebCorebindingsjsReadableJSStreamcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/bindings/js/ReadableJSStream.cpp (185536 => 185537)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/bindings/js/ReadableJSStream.cpp        2015-06-13 08:29:23 UTC (rev 185536)
+++ trunk/Source/WebCore/bindings/js/ReadableJSStream.cpp        2015-06-13 09:09:53 UTC (rev 185537)
</span><span class="lines">@@ -36,11 +36,11 @@
</span><span class="cx"> #include &quot;JSDOMPromise.h&quot;
</span><span class="cx"> #include &quot;JSReadableStream.h&quot;
</span><span class="cx"> #include &quot;JSReadableStreamController.h&quot;
</span><del>-#include &quot;NotImplemented.h&quot;
</del><span class="cx"> #include &quot;ScriptExecutionContext.h&quot;
</span><span class="cx"> #include &lt;runtime/Error.h&gt;
</span><span class="cx"> #include &lt;runtime/Exception.h&gt;
</span><span class="cx"> #include &lt;runtime/JSCJSValueInlines.h&gt;
</span><ins>+#include &lt;runtime/JSPromise.h&gt;
</ins><span class="cx"> #include &lt;runtime/JSString.h&gt;
</span><span class="cx"> #include &lt;runtime/StructureInlines.h&gt;
</span><span class="cx"> 
</span><span class="lines">@@ -60,31 +60,57 @@
</span><span class="cx">     return call(&amp;exec, jsFunction, callType, callData, thisValue, arguments);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-JSValue ReadableJSStream::invoke(ExecState&amp; exec, const char* propertyName)
</del><ins>+JSPromise* ReadableJSStream::invoke(ExecState&amp; state, const char* propertyName)
</ins><span class="cx"> {
</span><del>-    JSValue function = getPropertyFromObject(exec, m_source.get(), propertyName);
-    if (exec.hadException())
-        return jsUndefined();
</del><ins>+    JSValue function = getPropertyFromObject(state, m_source.get(), propertyName);
+    if (state.hadException())
+        return nullptr;
</ins><span class="cx"> 
</span><span class="cx">     if (!function.isFunction()) {
</span><span class="cx">         if (!function.isUndefined())
</span><del>-            throwVMError(&amp;exec, createTypeError(&amp;exec, ASCIILiteral(&quot;ReadableStream trying to call a property that is not callable&quot;)));
-        return jsUndefined();
</del><ins>+            throwVMError(&amp;state, createTypeError(&amp;state, ASCIILiteral(&quot;ReadableStream trying to call a property that is not callable&quot;)));
+        return nullptr;
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     MarkedArgumentBuffer arguments;
</span><del>-    arguments.append(jsController(exec, globalObject()));
-    return callFunction(exec, function, m_source.get(), arguments);
</del><ins>+    arguments.append(jsController(state, globalObject()));
+
+    JSPromise* promise = jsDynamicCast&lt;JSPromise*&gt;(callFunction(state, function, m_source.get(), arguments));
+
+    ASSERT(!(promise &amp;&amp; state.hadException()));
+    return promise;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static void thenPromise(ExecState&amp; state, JSPromise* deferredPromise, JSValue fullfilFunction, JSValue rejectFunction)
+{
+    JSValue thenValue = deferredPromise-&gt;get(&amp;state, state.vm().propertyNames-&gt;then);
+    if (state.hadException())
+        return;
+
+    MarkedArgumentBuffer arguments;
+    arguments.append(fullfilFunction);
+    arguments.append(rejectFunction);
+
+    callFunction(state, thenValue, deferredPromise, arguments);
+}
+
</ins><span class="cx"> JSDOMGlobalObject* ReadableJSStream::globalObject()
</span><span class="cx"> {
</span><span class="cx">     return jsDynamicCast&lt;JSDOMGlobalObject*&gt;(m_source-&gt;globalObject());
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static void startReadableStreamAsync(ReadableStream&amp; readableStream)
</del><ins>+static inline JSFunction* createStartResultFulfilledFunction(ExecState&amp; state, ReadableStream&amp; readableStream)
</ins><span class="cx"> {
</span><span class="cx">     RefPtr&lt;ReadableStream&gt; stream = &amp;readableStream;
</span><ins>+    return JSFunction::create(state.vm(), state.callee()-&gt;globalObject(), 1, String(), [stream](ExecState*) {
+        stream-&gt;start();
+        return JSValue::encode(jsUndefined());
+    });
+}
+
+static inline void startReadableStreamAsync(ReadableStream&amp; readableStream)
+{
+    RefPtr&lt;ReadableStream&gt; stream = &amp;readableStream;
</ins><span class="cx">     stream-&gt;scriptExecutionContext()-&gt;postTask([stream](ScriptExecutionContext&amp;) {
</span><span class="cx">         stream-&gt;start();
</span><span class="cx">     });
</span><span class="lines">@@ -94,13 +120,17 @@
</span><span class="cx"> {
</span><span class="cx">     JSLockHolder lock(&amp;exec);
</span><span class="cx"> 
</span><del>-    invoke(exec, &quot;start&quot;);
</del><ins>+    JSPromise* promise = invoke(exec, &quot;start&quot;);
</ins><span class="cx"> 
</span><span class="cx">     if (exec.hadException())
</span><span class="cx">         return;
</span><span class="cx"> 
</span><del>-    // FIXME: Implement handling promise as result of calling start function.
-    startReadableStreamAsync(*this);
</del><ins>+    if (!promise) {
+        startReadableStreamAsync(*this);
+        return;
+    }
+
+    thenPromise(exec, promise, createStartResultFulfilledFunction(exec, *this), m_errorFunction.get());
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void ReadableJSStream::doPull()
</span><span class="lines">@@ -139,10 +169,16 @@
</span><span class="cx">     return readableStream;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-ReadableJSStream::ReadableJSStream(ScriptExecutionContext&amp; scriptExecutionContext, ExecState&amp; exec, JSObject* source)
</del><ins>+ReadableJSStream::ReadableJSStream(ScriptExecutionContext&amp; scriptExecutionContext, ExecState&amp; state, JSObject* source)
</ins><span class="cx">     : ReadableStream(scriptExecutionContext)
</span><span class="cx"> {
</span><del>-    m_source.set(exec.vm(), source);
</del><ins>+    m_source.set(state.vm(), source);
+    // We do not take a Ref to the stream as this would cause a Ref cycle.
+    // The resolution callback used jointly with m_errorFunction as promise callbacks should protect the stream instead.
+    m_errorFunction.set(state.vm(), JSFunction::create(state.vm(), state.callee()-&gt;globalObject(), 1, String(), [this](ExecState* state) {
+        storeError(*state);
+        return JSValue::encode(jsUndefined());
+    }));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> JSValue ReadableJSStream::jsController(ExecState&amp; exec, JSDOMGlobalObject* globalObject)
</span></span></pre></div>
<a id="trunkSourceWebCorebindingsjsReadableJSStreamh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/bindings/js/ReadableJSStream.h (185536 => 185537)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/bindings/js/ReadableJSStream.h        2015-06-13 08:29:23 UTC (rev 185536)
+++ trunk/Source/WebCore/bindings/js/ReadableJSStream.h        2015-06-13 09:09:53 UTC (rev 185537)
</span><span class="lines">@@ -40,6 +40,11 @@
</span><span class="cx"> #include &lt;wtf/Deque.h&gt;
</span><span class="cx"> #include &lt;wtf/Ref.h&gt;
</span><span class="cx"> 
</span><ins>+namespace JSC {
+class JSFunction;
+class JSPromise;
+}
+    
</ins><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><span class="cx"> class JSDOMGlobalObject;
</span><span class="lines">@@ -61,7 +66,7 @@
</span><span class="cx"> 
</span><span class="cx">     void doStart(JSC::ExecState&amp;);
</span><span class="cx"> 
</span><del>-    JSC::JSValue invoke(JSC::ExecState&amp;, const char*);
</del><ins>+    JSC::JSPromise* invoke(JSC::ExecState&amp;, const char*);
</ins><span class="cx">     void storeException(JSC::ExecState&amp;);
</span><span class="cx">     void storeError(JSC::ExecState&amp;, JSC::JSValue);
</span><span class="cx"> 
</span><span class="lines">@@ -73,6 +78,7 @@
</span><span class="cx"> 
</span><span class="cx">     std::unique_ptr&lt;ReadableStreamController&gt; m_controller;
</span><span class="cx">     JSC::Strong&lt;JSC::Unknown&gt; m_error;
</span><ins>+    JSC::Strong&lt;JSC::JSFunction&gt; m_errorFunction;
</ins><span class="cx">     JSC::Strong&lt;JSC::JSObject&gt; m_source;
</span><span class="cx">     Deque&lt;JSC::Strong&lt;JSC::Unknown&gt;&gt; m_chunkQueue;
</span><span class="cx"> };
</span></span></pre>
</div>
</div>

</body>
</html>