<!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>[185407] trunk/Source/WebCore</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/185407">185407</a></dd>
<dt>Author</dt> <dd>youenn.fablet@crf.canon.fr</dd>
<dt>Date</dt> <dd>2015-06-10 01:10:44 -0700 (Wed, 10 Jun 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Refactor AudioContext implementation to enable automatic binding generation of promise-based methods
https://bugs.webkit.org/show_bug.cgi?id=145223

Reviewed by Darin Adler.

Introducing DOMPromise as a typed wrapper above DeferredWrapper.
This DOMPromise allows constraining Promise resolution/rejection to a single resolution type and rejection type.
This might be useful for typed callback usage of promise based APIs, like getUserMedia() or ReadableStreamReader.read().

Applying DOMPromise to AudioContext close, suspend and resume.
Changed binding code to represent what could be automatically generated by binding generator.
Fixing unneeded copies of std::function callbacks.

Disabling DOMPromise copy constructors to lower chances that resolution/rejection is done twice on the same object.

Covered by existing tests.

* Modules/webaudio/AudioContext.cpp:
(WebCore::AudioContext::addReaction):
(WebCore::AudioContext::setState):
(WebCore::AudioContext::suspend):
(WebCore::AudioContext::resume):
(WebCore::AudioContext::close):
* Modules/webaudio/AudioContext.h:
* bindings/js/JSAudioContextCustom.cpp:
(WebCore::JSAudioContext::suspend):
(WebCore::JSAudioContext::resume):
(WebCore::JSAudioContext::close):
* bindings/js/JSDOMPromise.cpp:
(WebCore::DeferredWrapper::callFunction):
* bindings/js/JSDOMPromise.h:
(WebCore::DeferredWrapper::resolve):
(WebCore::DeferredWrapper::reject):
(WebCore::DOMPromise::DOMPromise):
(WebCore::DOMPromise::resolve):
(WebCore::DOMPromise::reject):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreModuleswebaudioAudioContextcpp">trunk/Source/WebCore/Modules/webaudio/AudioContext.cpp</a></li>
<li><a href="#trunkSourceWebCoreModuleswebaudioAudioContexth">trunk/Source/WebCore/Modules/webaudio/AudioContext.h</a></li>
<li><a href="#trunkSourceWebCorebindingsjsJSAudioContextCustomcpp">trunk/Source/WebCore/bindings/js/JSAudioContextCustom.cpp</a></li>
<li><a href="#trunkSourceWebCorebindingsjsJSDOMPromisecpp">trunk/Source/WebCore/bindings/js/JSDOMPromise.cpp</a></li>
<li><a href="#trunkSourceWebCorebindingsjsJSDOMPromiseh">trunk/Source/WebCore/bindings/js/JSDOMPromise.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (185406 => 185407)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2015-06-10 08:09:02 UTC (rev 185406)
+++ trunk/Source/WebCore/ChangeLog        2015-06-10 08:10:44 UTC (rev 185407)
</span><span class="lines">@@ -1,3 +1,42 @@
</span><ins>+2015-06-10  Youenn Fablet  &lt;youenn.fablet@crf.canon.fr&gt;
+
+        Refactor AudioContext implementation to enable automatic binding generation of promise-based methods
+        https://bugs.webkit.org/show_bug.cgi?id=145223
+
+        Reviewed by Darin Adler.
+
+        Introducing DOMPromise as a typed wrapper above DeferredWrapper.
+        This DOMPromise allows constraining Promise resolution/rejection to a single resolution type and rejection type. 
+        This might be useful for typed callback usage of promise based APIs, like getUserMedia() or ReadableStreamReader.read().
+
+        Applying DOMPromise to AudioContext close, suspend and resume.
+        Changed binding code to represent what could be automatically generated by binding generator.
+        Fixing unneeded copies of std::function callbacks.
+
+        Disabling DOMPromise copy constructors to lower chances that resolution/rejection is done twice on the same object.
+
+        Covered by existing tests.
+
+        * Modules/webaudio/AudioContext.cpp:
+        (WebCore::AudioContext::addReaction):
+        (WebCore::AudioContext::setState):
+        (WebCore::AudioContext::suspend):
+        (WebCore::AudioContext::resume):
+        (WebCore::AudioContext::close):
+        * Modules/webaudio/AudioContext.h:
+        * bindings/js/JSAudioContextCustom.cpp:
+        (WebCore::JSAudioContext::suspend):
+        (WebCore::JSAudioContext::resume):
+        (WebCore::JSAudioContext::close):
+        * bindings/js/JSDOMPromise.cpp:
+        (WebCore::DeferredWrapper::callFunction):
+        * bindings/js/JSDOMPromise.h:
+        (WebCore::DeferredWrapper::resolve):
+        (WebCore::DeferredWrapper::reject):
+        (WebCore::DOMPromise::DOMPromise):
+        (WebCore::DOMPromise::resolve):
+        (WebCore::DOMPromise::reject):
+
</ins><span class="cx"> 2015-06-10  Xabier Rodriguez Calvar  &lt;calvaris@igalia.com&gt; and Youenn Fablet &lt;youenn.fablet@crf.canon.fr&gt;
</span><span class="cx"> 
</span><span class="cx">         [Streams API] Implement pulling of a source by a ReadableStream
</span></span></pre></div>
<a id="trunkSourceWebCoreModuleswebaudioAudioContextcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/webaudio/AudioContext.cpp (185406 => 185407)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/webaudio/AudioContext.cpp        2015-06-10 08:09:02 UTC (rev 185406)
+++ trunk/Source/WebCore/Modules/webaudio/AudioContext.cpp        2015-06-10 08:10:44 UTC (rev 185407)
</span><span class="lines">@@ -51,6 +51,7 @@
</span><span class="cx"> #include &quot;GenericEventQueue.h&quot;
</span><span class="cx"> #include &quot;HRTFDatabaseLoader.h&quot;
</span><span class="cx"> #include &quot;HRTFPanner.h&quot;
</span><ins>+#include &quot;JSDOMPromise.h&quot;
</ins><span class="cx"> #include &quot;OfflineAudioCompletionEvent.h&quot;
</span><span class="cx"> #include &quot;OfflineAudioDestinationNode.h&quot;
</span><span class="cx"> #include &quot;OscillatorNode.h&quot;
</span><span class="lines">@@ -277,13 +278,13 @@
</span><span class="cx">     return m_isInitialized;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void AudioContext::addReaction(State state, std::function&lt;void()&gt; reaction)
</del><ins>+void AudioContext::addReaction(State state, Promise&amp;&amp; promise)
</ins><span class="cx"> {
</span><span class="cx">     size_t stateIndex = static_cast&lt;size_t&gt;(state);
</span><span class="cx">     if (stateIndex &gt;= m_stateReactions.size())
</span><span class="cx">         m_stateReactions.resize(stateIndex + 1);
</span><span class="cx"> 
</span><del>-    m_stateReactions[stateIndex].append(reaction);
</del><ins>+    m_stateReactions[stateIndex].append(WTF::move(promise));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void AudioContext::setState(State state)
</span><span class="lines">@@ -298,11 +299,11 @@
</span><span class="cx">     if (stateIndex &gt;= m_stateReactions.size())
</span><span class="cx">         return;
</span><span class="cx"> 
</span><del>-    Vector&lt;std::function&lt;void()&gt;&gt; reactions;
</del><ins>+    Vector&lt;Promise&gt; reactions;
</ins><span class="cx">     m_stateReactions[stateIndex].swap(reactions);
</span><span class="cx"> 
</span><del>-    for (auto&amp; reaction : reactions)
-        reaction();
</del><ins>+    for (auto&amp; promise : reactions)
+        promise.resolve(nullptr);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> const AtomicString&amp; AudioContext::state() const
</span><span class="lines">@@ -1096,27 +1097,24 @@
</span><span class="cx">     --m_activeSourceCount;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void AudioContext::suspendContext(std::function&lt;void()&gt; successCallback, FailureCallback failureCallback)
</del><ins>+void AudioContext::suspend(Promise&amp;&amp; promise)
</ins><span class="cx"> {
</span><del>-    ASSERT(successCallback);
-    ASSERT(failureCallback);
-
</del><span class="cx">     if (isOfflineContext()) {
</span><del>-        failureCallback(INVALID_STATE_ERR);
</del><ins>+        promise.reject(INVALID_STATE_ERR);
</ins><span class="cx">         return;
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     if (m_state == State::Suspended) {
</span><del>-        successCallback();
</del><ins>+        promise.resolve(nullptr);
</ins><span class="cx">         return;
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     if (m_state == State::Closed || m_state == State::Interrupted || !m_destinationNode) {
</span><del>-        failureCallback(0);
</del><ins>+        promise.reject(0);
</ins><span class="cx">         return;
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    addReaction(State::Suspended, successCallback);
</del><ins>+    addReaction(State::Suspended, WTF::move(promise));
</ins><span class="cx"> 
</span><span class="cx">     if (!willPausePlayback())
</span><span class="cx">         return;
</span><span class="lines">@@ -1129,27 +1127,24 @@
</span><span class="cx">     });
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void AudioContext::resumeContext(std::function&lt;void()&gt; successCallback, FailureCallback failureCallback)
</del><ins>+void AudioContext::resume(Promise&amp;&amp; promise)
</ins><span class="cx"> {
</span><del>-    ASSERT(successCallback);
-    ASSERT(failureCallback);
-
</del><span class="cx">     if (isOfflineContext()) {
</span><del>-        failureCallback(INVALID_STATE_ERR);
</del><ins>+        promise.reject(INVALID_STATE_ERR);
</ins><span class="cx">         return;
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     if (m_state == State::Running) {
</span><del>-        successCallback();
</del><ins>+        promise.resolve(nullptr);
</ins><span class="cx">         return;
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     if (m_state == State::Closed || !m_destinationNode) {
</span><del>-        failureCallback(0);
</del><ins>+        promise.reject(0);
</ins><span class="cx">         return;
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    addReaction(State::Running, successCallback);
</del><ins>+    addReaction(State::Running, WTF::move(promise));
</ins><span class="cx"> 
</span><span class="cx">     if (!willBeginPlayback())
</span><span class="cx">         return;
</span><span class="lines">@@ -1162,27 +1157,24 @@
</span><span class="cx">     });
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void AudioContext::closeContext(std::function&lt;void()&gt; successCallback, FailureCallback failureCallback)
</del><ins>+void AudioContext::close(Promise&amp;&amp; promise)
</ins><span class="cx"> {
</span><del>-    ASSERT(successCallback);
-    ASSERT(failureCallback);
-
</del><span class="cx">     if (isOfflineContext()) {
</span><del>-        failureCallback(INVALID_STATE_ERR);
</del><ins>+        promise.reject(INVALID_STATE_ERR);
</ins><span class="cx">         return;
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     if (m_state == State::Closed || !m_destinationNode) {
</span><del>-        successCallback();
</del><ins>+        promise.resolve(nullptr);
</ins><span class="cx">         return;
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    addReaction(State::Closed, successCallback);
</del><ins>+    addReaction(State::Closed, WTF::move(promise));
</ins><span class="cx"> 
</span><span class="cx">     lazyInitialize();
</span><span class="cx"> 
</span><span class="cx">     RefPtr&lt;AudioContext&gt; strongThis(this);
</span><del>-    m_destinationNode-&gt;close([strongThis, successCallback] {
</del><ins>+    m_destinationNode-&gt;close([strongThis] {
</ins><span class="cx">         strongThis-&gt;setState(State::Closed);
</span><span class="cx">         strongThis-&gt;uninitialize();
</span><span class="cx">     });
</span></span></pre></div>
<a id="trunkSourceWebCoreModuleswebaudioAudioContexth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/webaudio/AudioContext.h (185406 => 185407)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/webaudio/AudioContext.h        2015-06-10 08:09:02 UTC (rev 185406)
+++ trunk/Source/WebCore/Modules/webaudio/AudioContext.h        2015-06-10 08:10:44 UTC (rev 185407)
</span><span class="lines">@@ -31,6 +31,7 @@
</span><span class="cx"> #include &quot;AudioDestinationNode.h&quot;
</span><span class="cx"> #include &quot;EventListener.h&quot;
</span><span class="cx"> #include &quot;EventTarget.h&quot;
</span><ins>+#include &quot;JSDOMPromise.h&quot;
</ins><span class="cx"> #include &quot;MediaCanStartListener.h&quot;
</span><span class="cx"> #include &quot;MediaProducer.h&quot;
</span><span class="cx"> #include &quot;PlatformMediaSession.h&quot;
</span><span class="lines">@@ -106,11 +107,15 @@
</span><span class="cx"> 
</span><span class="cx">     AudioListener* listener() { return m_listener.get(); }
</span><span class="cx"> 
</span><del>-    typedef std::function&lt;void(ExceptionCode)&gt; FailureCallback;
</del><ins>+    using ActiveDOMObject::suspend;
+    using ActiveDOMObject::resume;
</ins><span class="cx"> 
</span><del>-    void suspendContext(std::function&lt;void()&gt;, FailureCallback);
-    void resumeContext(std::function&lt;void()&gt;, FailureCallback);
-    void closeContext(std::function&lt;void()&gt;, FailureCallback);
</del><ins>+    typedef DOMPromise&lt;std::nullptr_t, ExceptionCode&gt; Promise;
+
+    void suspend(Promise&amp;&amp;);
+    void resume(Promise&amp;&amp;);
+    void close(Promise&amp;&amp;);
+
</ins><span class="cx">     const AtomicString&amp; state() const;
</span><span class="cx"> 
</span><span class="cx">     // The AudioNode create methods are called on the main thread (from JavaScript).
</span><span class="lines">@@ -324,7 +329,7 @@
</span><span class="cx">     void handleDirtyAudioSummingJunctions();
</span><span class="cx">     void handleDirtyAudioNodeOutputs();
</span><span class="cx"> 
</span><del>-    void addReaction(State, std::function&lt;void()&gt;);
</del><ins>+    void addReaction(State, Promise&amp;&amp;);
</ins><span class="cx">     void updateAutomaticPullNodes();
</span><span class="cx"> 
</span><span class="cx">     // Only accessed in the audio thread.
</span><span class="lines">@@ -361,7 +366,7 @@
</span><span class="cx">     Vector&lt;AudioNode*&gt; m_renderingAutomaticPullNodes;
</span><span class="cx">     // Only accessed in the audio thread.
</span><span class="cx">     Vector&lt;AudioNode*&gt; m_deferredFinishDerefList;
</span><del>-    Vector&lt;Vector&lt;std::function&lt;void()&gt;&gt;&gt; m_stateReactions;
</del><ins>+    Vector&lt;Vector&lt;Promise&gt;&gt; m_stateReactions;
</ins><span class="cx"> 
</span><span class="cx">     std::unique_ptr&lt;PlatformMediaSession&gt; m_mediaSession;
</span><span class="cx">     std::unique_ptr&lt;GenericEventQueue&gt; m_eventQueue;
</span></span></pre></div>
<a id="trunkSourceWebCorebindingsjsJSAudioContextCustomcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/bindings/js/JSAudioContextCustom.cpp (185406 => 185407)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/bindings/js/JSAudioContextCustom.cpp        2015-06-10 08:09:02 UTC (rev 185406)
+++ trunk/Source/WebCore/bindings/js/JSAudioContextCustom.cpp        2015-06-10 08:10:44 UTC (rev 185407)
</span><span class="lines">@@ -111,50 +111,29 @@
</span><span class="cx">     return JSValue::encode(CREATE_DOM_WRAPPER(jsConstructor-&gt;globalObject(), AudioContext, audioContext.get()));
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-JSValue JSAudioContext::suspend(ExecState* exec)
</del><ins>+JSValue JSAudioContext::suspend(ExecState* state)
</ins><span class="cx"> {
</span><del>-    JSPromiseDeferred* promiseDeferred = JSPromiseDeferred::create(exec, globalObject());
-    DeferredWrapper wrapper(exec, globalObject(), promiseDeferred);
-    auto successCallback = [wrapper]() mutable {
-        wrapper.resolve(nullptr);
-    };
-    auto failureCallback = [wrapper](ExceptionCode value) mutable {
-        wrapper.reject(value);
-    };
</del><ins>+    JSPromiseDeferred* promiseDeferred = JSPromiseDeferred::create(state, globalObject());
</ins><span class="cx"> 
</span><del>-    impl().suspendContext(WTF::move(successCallback), WTF::move(failureCallback));
</del><ins>+    impl().suspend(DeferredWrapper(state, globalObject(), promiseDeferred));
</ins><span class="cx"> 
</span><span class="cx">     return promiseDeferred-&gt;promise();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-JSValue JSAudioContext::resume(ExecState* exec)
</del><ins>+JSValue JSAudioContext::resume(ExecState* state)
</ins><span class="cx"> {
</span><del>-    JSPromiseDeferred* promiseDeferred = JSPromiseDeferred::create(exec, globalObject());
-    DeferredWrapper wrapper(exec, globalObject(), promiseDeferred);
-    auto successCallback = [wrapper]() mutable {
-        wrapper.resolve(nullptr);
-    };
-    auto failureCallback = [wrapper](ExceptionCode value) mutable {
-        wrapper.reject(value);
-    };
</del><ins>+    JSPromiseDeferred* promiseDeferred = JSPromiseDeferred::create(state, globalObject());
</ins><span class="cx"> 
</span><del>-    impl().resumeContext(WTF::move(successCallback), WTF::move(failureCallback));
</del><ins>+    impl().resume(DeferredWrapper(state, globalObject(), promiseDeferred));
</ins><span class="cx"> 
</span><span class="cx">     return promiseDeferred-&gt;promise();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-JSValue JSAudioContext::close(ExecState* exec)
</del><ins>+JSValue JSAudioContext::close(ExecState* state)
</ins><span class="cx"> {
</span><del>-    JSPromiseDeferred* promiseDeferred = JSPromiseDeferred::create(exec, globalObject());
-    DeferredWrapper wrapper(exec, globalObject(), promiseDeferred);
-    auto successCallback = [wrapper]() mutable {
-        wrapper.resolve(nullptr);
-    };
-    auto failureCallback = [wrapper](ExceptionCode value) mutable {
-        wrapper.reject(value);
-    };
</del><ins>+    JSPromiseDeferred* promiseDeferred = JSPromiseDeferred::create(state, globalObject());
</ins><span class="cx"> 
</span><del>-    impl().closeContext(WTF::move(successCallback), WTF::move(failureCallback));
</del><ins>+    impl().close(DeferredWrapper(state, globalObject(), promiseDeferred));
</ins><span class="cx"> 
</span><span class="cx">     return promiseDeferred-&gt;promise();
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebCorebindingsjsJSDOMPromisecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/bindings/js/JSDOMPromise.cpp (185406 => 185407)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/bindings/js/JSDOMPromise.cpp        2015-06-10 08:09:02 UTC (rev 185406)
+++ trunk/Source/WebCore/bindings/js/JSDOMPromise.cpp        2015-06-10 08:10:44 UTC (rev 185407)
</span><span class="lines">@@ -28,6 +28,8 @@
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(PROMISES)
</span><span class="cx"> 
</span><ins>+#include &quot;ExceptionCode.h&quot;
+
</ins><span class="cx"> using namespace JSC;
</span><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="lines">@@ -45,40 +47,21 @@
</span><span class="cx">     return m_deferred-&gt;promise();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void DeferredWrapper::resolve(ExecState* exec, JSValue resolution)
</del><ins>+void DeferredWrapper::callFunction(ExecState&amp; exec, JSValue function, JSValue resolution)
</ins><span class="cx"> {
</span><del>-    JSValue deferredResolve = m_deferred-&gt;resolve();
</del><ins>+    CallData callData;
+    CallType callType = getCallData(function, callData);
+    ASSERT(callType != CallTypeNone);
</ins><span class="cx"> 
</span><del>-    CallData resolveCallData;
-    CallType resolveCallType = getCallData(deferredResolve, resolveCallData);
-    ASSERT(resolveCallType != CallTypeNone);
-
</del><span class="cx">     MarkedArgumentBuffer arguments;
</span><span class="cx">     arguments.append(resolution);
</span><span class="cx"> 
</span><del>-    call(exec, deferredResolve, resolveCallType, resolveCallData, jsUndefined(), arguments);
</del><ins>+    call(&amp;exec, function, callType, callData, jsUndefined(), arguments);
</ins><span class="cx"> 
</span><span class="cx">     m_globalObject.clear();
</span><span class="cx">     m_deferred.clear();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void DeferredWrapper::reject(ExecState* exec, JSValue reason)
-{
-    JSValue deferredReject = m_deferred-&gt;reject();
-
-    CallData rejectCallData;
-    CallType rejectCallType = getCallData(deferredReject, rejectCallData);
-    ASSERT(rejectCallType != CallTypeNone);
-
-    MarkedArgumentBuffer arguments;
-    arguments.append(reason);
-
-    call(exec, deferredReject, rejectCallType, rejectCallData, jsUndefined(), arguments);
-
-    m_globalObject.clear();
-    m_deferred.clear();
</del><span class="cx"> }
</span><span class="cx"> 
</span><del>-}
-
</del><span class="cx"> #endif // ENABLE(PROMISES)
</span></span></pre></div>
<a id="trunkSourceWebCorebindingsjsJSDOMPromiseh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/bindings/js/JSDOMPromise.h (185406 => 185407)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/bindings/js/JSDOMPromise.h        2015-06-10 08:09:02 UTC (rev 185406)
+++ trunk/Source/WebCore/bindings/js/JSDOMPromise.h        2015-06-10 08:10:44 UTC (rev 185407)
</span><span class="lines">@@ -49,13 +49,30 @@
</span><span class="cx">     JSC::JSObject* promise() const;
</span><span class="cx"> 
</span><span class="cx"> private:
</span><del>-    void resolve(JSC::ExecState*, JSC::JSValue);
-    void reject(JSC::ExecState*, JSC::JSValue);
</del><ins>+    void callFunction(JSC::ExecState&amp;, JSC::JSValue function, JSC::JSValue resolution);
+    void resolve(JSC::ExecState&amp; state, JSC::JSValue resolution) { callFunction(state, m_deferred-&gt;resolve(), resolution); }
+    void reject(JSC::ExecState&amp; state, JSC::JSValue resolution) { callFunction(state, m_deferred-&gt;reject(), resolution); }
</ins><span class="cx"> 
</span><span class="cx">     JSC::Strong&lt;JSDOMGlobalObject&gt; m_globalObject;
</span><span class="cx">     JSC::Strong&lt;JSC::JSPromiseDeferred&gt; m_deferred;
</span><span class="cx"> };
</span><span class="cx"> 
</span><ins>+template &lt;typename Value, typename Error&gt;
+class DOMPromise {
+public:
+    DOMPromise(DeferredWrapper&amp;&amp; wrapper) : m_wrapper(WTF::move(wrapper)) { }
+    DOMPromise(DOMPromise&amp;&amp; promise) : m_wrapper(WTF::move(promise.m_wrapper)) { }
+
+    DOMPromise(const DOMPromise&amp;)= delete;
+    DOMPromise&amp; operator=(DOMPromise const&amp;) = delete;
+
+    void resolve(const Value&amp; value) { m_wrapper.resolve&lt;Value&gt;(value); }
+    void reject(const Error&amp; error) { m_wrapper.reject&lt;Error&gt;(error); }
+
+private:
+    DeferredWrapper m_wrapper;
+};
+
</ins><span class="cx"> template&lt;class ResolveResultType&gt;
</span><span class="cx"> inline void DeferredWrapper::resolve(const ResolveResultType&amp; result)
</span><span class="cx"> {
</span><span class="lines">@@ -63,7 +80,7 @@
</span><span class="cx">     ASSERT(m_globalObject);
</span><span class="cx">     JSC::ExecState* exec = m_globalObject-&gt;globalExec();
</span><span class="cx">     JSC::JSLockHolder locker(exec);
</span><del>-    resolve(exec, toJS(exec, m_globalObject.get(), result));
</del><ins>+    resolve(*exec, toJS(exec, m_globalObject.get(), result));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template&lt;class RejectResultType&gt;
</span><span class="lines">@@ -73,7 +90,7 @@
</span><span class="cx">     ASSERT(m_globalObject);
</span><span class="cx">     JSC::ExecState* exec = m_globalObject-&gt;globalExec();
</span><span class="cx">     JSC::JSLockHolder locker(exec);
</span><del>-    reject(exec, toJS(exec, m_globalObject.get(), result));
</del><ins>+    reject(*exec, toJS(exec, m_globalObject.get(), result));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template&lt;&gt;
</span><span class="lines">@@ -83,7 +100,7 @@
</span><span class="cx">     ASSERT(m_globalObject);
</span><span class="cx">     JSC::ExecState* exec = m_globalObject-&gt;globalExec();
</span><span class="cx">     JSC::JSLockHolder locker(exec);
</span><del>-    reject(exec, JSC::jsNull());
</del><ins>+    reject(*exec, JSC::jsNull());
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template&lt;&gt;
</span><span class="lines">@@ -93,7 +110,7 @@
</span><span class="cx">     ASSERT(m_globalObject);
</span><span class="cx">     JSC::ExecState* exec = m_globalObject-&gt;globalExec();
</span><span class="cx">     JSC::JSLockHolder locker(exec);
</span><del>-    reject(exec, value);
</del><ins>+    reject(*exec, value);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template&lt;&gt;
</span><span class="lines">@@ -103,7 +120,7 @@
</span><span class="cx">     ASSERT(m_globalObject);
</span><span class="cx">     JSC::ExecState* exec = m_globalObject-&gt;globalExec();
</span><span class="cx">     JSC::JSLockHolder locker(exec);
</span><del>-    reject(exec, createDOMException(exec, ec));
</del><ins>+    reject(*exec, createDOMException(exec, ec));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template&lt;&gt;
</span><span class="lines">@@ -113,7 +130,7 @@
</span><span class="cx">     ASSERT(m_globalObject);
</span><span class="cx">     JSC::ExecState* exec = m_globalObject-&gt;globalExec();
</span><span class="cx">     JSC::JSLockHolder locker(exec);
</span><del>-    resolve(exec, jsString(exec, result));
</del><ins>+    resolve(*exec, jsString(exec, result));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template&lt;&gt;
</span><span class="lines">@@ -123,7 +140,7 @@
</span><span class="cx">     ASSERT(m_globalObject);
</span><span class="cx">     JSC::ExecState* exec = m_globalObject-&gt;globalExec();
</span><span class="cx">     JSC::JSLockHolder locker(exec);
</span><del>-    resolve(exec, JSC::jsBoolean(result));
</del><ins>+    resolve(*exec, JSC::jsBoolean(result));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template&lt;&gt;
</span><span class="lines">@@ -133,7 +150,7 @@
</span><span class="cx">     ASSERT(m_globalObject);
</span><span class="cx">     JSC::ExecState* exec = m_globalObject-&gt;globalExec();
</span><span class="cx">     JSC::JSLockHolder locker(exec);
</span><del>-    resolve(exec, value);
</del><ins>+    resolve(*exec, value);
</ins><span class="cx"> }
</span><span class="cx"> template&lt;&gt;
</span><span class="cx"> inline void DeferredWrapper::resolve&lt;Vector&lt;unsigned char&gt;&gt;(const Vector&lt;unsigned char&gt;&amp; result)
</span><span class="lines">@@ -143,7 +160,7 @@
</span><span class="cx">     JSC::ExecState* exec = m_globalObject-&gt;globalExec();
</span><span class="cx">     JSC::JSLockHolder locker(exec);
</span><span class="cx">     RefPtr&lt;ArrayBuffer&gt; buffer = ArrayBuffer::create(result.data(), result.size());
</span><del>-    resolve(exec, toJS(exec, m_globalObject.get(), buffer.get()));
</del><ins>+    resolve(*exec, toJS(exec, m_globalObject.get(), buffer.get()));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template&lt;&gt;
</span><span class="lines">@@ -153,7 +170,7 @@
</span><span class="cx">     ASSERT(m_globalObject);
</span><span class="cx">     JSC::ExecState* exec = m_globalObject-&gt;globalExec();
</span><span class="cx">     JSC::JSLockHolder locker(exec);
</span><del>-    resolve(exec, JSC::jsUndefined());
</del><ins>+    resolve(*exec, JSC::jsUndefined());
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template&lt;&gt;
</span><span class="lines">@@ -163,7 +180,7 @@
</span><span class="cx">     ASSERT(m_globalObject);
</span><span class="cx">     JSC::ExecState* exec = m_globalObject-&gt;globalExec();
</span><span class="cx">     JSC::JSLockHolder locker(exec);
</span><del>-    reject(exec, jsString(exec, result));
</del><ins>+    reject(*exec, jsString(exec, result));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> }
</span></span></pre>
</div>
</div>

</body>
</html>