<!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>[196956] trunk/Source/JavaScriptCore</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/196956">196956</a></dd>
<dt>Author</dt> <dd>keith_miller@apple.com</dd>
<dt>Date</dt> <dd>2016-02-22 14:07:16 -0800 (Mon, 22 Feb 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>Bound functions should use the prototype of the function being bound
https://bugs.webkit.org/show_bug.cgi?id=154195

Reviewed by Geoffrey Garen.

Per ES6, the result of Function.prototype.bind should have the same
prototype as the the function being bound. In order to avoid creating
a new structure each time a function is bound we store the new
structure in our structure map. However, we cannot currently store
structures that have a different GlobalObject than their prototype.
In the rare case that the GlobalObject differs or the prototype of
the bindee is null we create a new structure each time. To further
minimize new structures, as well as making structure lookup faster,
we also store the structure in the RareData of the function we
are binding.

* runtime/FunctionRareData.cpp:
(JSC::FunctionRareData::visitChildren):
* runtime/FunctionRareData.h:
(JSC::FunctionRareData::getBoundFunctionStructure):
(JSC::FunctionRareData::setBoundFunctionStructure):
* runtime/JSBoundFunction.cpp:
(JSC::getBoundFunctionStructure):
(JSC::JSBoundFunction::create):
* tests/es6.yaml:
* tests/stress/bound-function-uses-prototype.js: Added.
(testChangeProto.foo):
(testChangeProto):
(testBuiltins):
* tests/stress/class-subclassing-function.js:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeFunctionRareDatacpp">trunk/Source/JavaScriptCore/runtime/FunctionRareData.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeFunctionRareDatah">trunk/Source/JavaScriptCore/runtime/FunctionRareData.h</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeJSBoundFunctioncpp">trunk/Source/JavaScriptCore/runtime/JSBoundFunction.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoretestses6yaml">trunk/Source/JavaScriptCore/tests/es6.yaml</a></li>
<li><a href="#trunkSourceJavaScriptCoretestsstressclasssubclassingfunctionjs">trunk/Source/JavaScriptCore/tests/stress/class-subclassing-function.js</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoretestsstressboundfunctionusesprototypejs">trunk/Source/JavaScriptCore/tests/stress/bound-function-uses-prototype.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (196955 => 196956)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2016-02-22 22:02:24 UTC (rev 196955)
+++ trunk/Source/JavaScriptCore/ChangeLog        2016-02-22 22:07:16 UTC (rev 196956)
</span><span class="lines">@@ -1,5 +1,38 @@
</span><span class="cx"> 2016-02-22  Keith Miller  &lt;keith_miller@apple.com&gt;
</span><span class="cx"> 
</span><ins>+        Bound functions should use the prototype of the function being bound
+        https://bugs.webkit.org/show_bug.cgi?id=154195
+
+        Reviewed by Geoffrey Garen.
+
+        Per ES6, the result of Function.prototype.bind should have the same
+        prototype as the the function being bound. In order to avoid creating
+        a new structure each time a function is bound we store the new
+        structure in our structure map. However, we cannot currently store
+        structures that have a different GlobalObject than their prototype.
+        In the rare case that the GlobalObject differs or the prototype of
+        the bindee is null we create a new structure each time. To further
+        minimize new structures, as well as making structure lookup faster,
+        we also store the structure in the RareData of the function we
+        are binding.
+
+        * runtime/FunctionRareData.cpp:
+        (JSC::FunctionRareData::visitChildren):
+        * runtime/FunctionRareData.h:
+        (JSC::FunctionRareData::getBoundFunctionStructure):
+        (JSC::FunctionRareData::setBoundFunctionStructure):
+        * runtime/JSBoundFunction.cpp:
+        (JSC::getBoundFunctionStructure):
+        (JSC::JSBoundFunction::create):
+        * tests/es6.yaml:
+        * tests/stress/bound-function-uses-prototype.js: Added.
+        (testChangeProto.foo):
+        (testChangeProto):
+        (testBuiltins):
+        * tests/stress/class-subclassing-function.js:
+
+2016-02-22  Keith Miller  &lt;keith_miller@apple.com&gt;
+
</ins><span class="cx">         Unreviewed, fix stress test to not print on success.
</span><span class="cx"> 
</span><span class="cx">         * tests/stress/call-apply-builtin-functions-dont-use-iterators.js:
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeFunctionRareDatacpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/FunctionRareData.cpp (196955 => 196956)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/FunctionRareData.cpp        2016-02-22 22:02:24 UTC (rev 196955)
+++ trunk/Source/JavaScriptCore/runtime/FunctionRareData.cpp        2016-02-22 22:07:16 UTC (rev 196956)
</span><span class="lines">@@ -56,6 +56,7 @@
</span><span class="cx"> 
</span><span class="cx">     rareData-&gt;m_objectAllocationProfile.visitAggregate(visitor);
</span><span class="cx">     rareData-&gt;m_internalFunctionAllocationProfile.visitAggregate(visitor);
</span><ins>+    visitor.append(&amp;rareData-&gt;m_boundFunctionStructure);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> FunctionRareData::FunctionRareData(VM&amp; vm)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeFunctionRareDatah"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/FunctionRareData.h (196955 => 196956)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/FunctionRareData.h        2016-02-22 22:02:24 UTC (rev 196955)
+++ trunk/Source/JavaScriptCore/runtime/FunctionRareData.h        2016-02-22 22:07:16 UTC (rev 196956)
</span><span class="lines">@@ -90,6 +90,9 @@
</span><span class="cx">         return m_internalFunctionAllocationProfile.createAllocationStructureFromBase(vm, this, prototype, baseStructure);
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    Structure* getBoundFunctionStructure() { return m_boundFunctionStructure.get(); }
+    void setBoundFunctionStructure(VM&amp; vm, Structure* structure) { m_boundFunctionStructure.set(vm, this, structure); }
+
</ins><span class="cx"> protected:
</span><span class="cx">     FunctionRareData(VM&amp;);
</span><span class="cx">     ~FunctionRareData();
</span><span class="lines">@@ -114,6 +117,7 @@
</span><span class="cx">     ObjectAllocationProfile m_objectAllocationProfile;
</span><span class="cx">     InlineWatchpointSet m_objectAllocationProfileWatchpoint;
</span><span class="cx">     InternalFunctionAllocationProfile m_internalFunctionAllocationProfile;
</span><ins>+    WriteBarrier&lt;Structure&gt; m_boundFunctionStructure;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> } // namespace JSC
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeJSBoundFunctioncpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/JSBoundFunction.cpp (196955 => 196956)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/JSBoundFunction.cpp        2016-02-22 22:02:24 UTC (rev 196955)
+++ trunk/Source/JavaScriptCore/runtime/JSBoundFunction.cpp        2016-02-22 22:07:16 UTC (rev 196956)
</span><span class="lines">@@ -87,13 +87,44 @@
</span><span class="cx">     return JSValue::encode(jsBoolean(boundObject-&gt;targetFunction()-&gt;hasInstance(exec, value)));
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+inline Structure* getBoundFunctionStructure(VM&amp; vm, JSGlobalObject* globalObject, JSObject* targetFunction)
+{
+    JSValue prototype = targetFunction-&gt;structure(vm)-&gt;storedPrototype();
+    JSFunction* targetJSFunction = jsDynamicCast&lt;JSFunction*&gt;(targetFunction);
+
+    // We only cache the structure of the bound function if the bindee is a JSFunction since there
+    // isn't any good place to put the structure on Internal Functions.
+    if (targetJSFunction) {
+        Structure* structure = targetJSFunction-&gt;rareData(vm)-&gt;getBoundFunctionStructure();
+        if (structure &amp;&amp; structure-&gt;storedPrototype() == prototype &amp;&amp; structure-&gt;globalObject() == globalObject)
+            return structure;
+    }
+
+    Structure* result = globalObject-&gt;boundFunctionStructure();
+
+    // It would be nice if the structure map was keyed global objects in addition to the other things. Unfortunately, it is not
+    // currently. Whoever works on caching structure changes for prototype transistions should consider this problem as well.
+    // See: https://bugs.webkit.org/show_bug.cgi?id=152738
+    if (prototype.isObject() &amp;&amp; prototype.getObject()-&gt;globalObject() == globalObject) {
+        result = vm.prototypeMap.emptyStructureForPrototypeFromBaseStructure(prototype.getObject(), result);
+        ASSERT_WITH_SECURITY_IMPLICATION(result-&gt;globalObject() == globalObject);
+    } else
+        result = Structure::create(vm, globalObject, prototype, result-&gt;typeInfo(), result-&gt;classInfo());
+
+    if (targetJSFunction)
+        targetJSFunction-&gt;rareData(vm)-&gt;setBoundFunctionStructure(vm, result);
+
+    return result;
+}
+
</ins><span class="cx"> JSBoundFunction* JSBoundFunction::create(VM&amp; vm, JSGlobalObject* globalObject, JSObject* targetFunction, JSValue boundThis, JSValue boundArgs, int length, const String&amp; name)
</span><span class="cx"> {
</span><span class="cx">     ConstructData constructData;
</span><span class="cx">     ConstructType constructType = JSC::getConstructData(targetFunction, constructData);
</span><span class="cx">     bool canConstruct = constructType != ConstructTypeNone;
</span><span class="cx">     NativeExecutable* executable = vm.getHostFunction(boundFunctionCall, canConstruct ? boundFunctionConstruct : callHostFunctionAsConstructor, ASCIILiteral(&quot;Function.prototype.bind result&quot;));
</span><del>-    JSBoundFunction* function = new (NotNull, allocateCell&lt;JSBoundFunction&gt;(vm.heap)) JSBoundFunction(vm, globalObject, globalObject-&gt;boundFunctionStructure(), targetFunction, boundThis, boundArgs);
</del><ins>+    Structure* structure = getBoundFunctionStructure(vm, globalObject, targetFunction);
+    JSBoundFunction* function = new (NotNull, allocateCell&lt;JSBoundFunction&gt;(vm.heap)) JSBoundFunction(vm, globalObject, structure, targetFunction, boundThis, boundArgs);
</ins><span class="cx"> 
</span><span class="cx">     function-&gt;finishCreation(vm, executable, length, makeString(&quot;bound &quot;, name));
</span><span class="cx">     return function;
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestses6yaml"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/tests/es6.yaml (196955 => 196956)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/es6.yaml        2016-02-22 22:02:24 UTC (rev 196955)
+++ trunk/Source/JavaScriptCore/tests/es6.yaml        2016-02-22 22:07:16 UTC (rev 196956)
</span><span class="lines">@@ -787,7 +787,7 @@
</span><span class="cx"> - path: es6/Function_is_subclassable_Function.prototype.apply.js
</span><span class="cx">   cmd: runES6 :normal
</span><span class="cx"> - path: es6/Function_is_subclassable_Function.prototype.bind.js
</span><del>-  cmd: runES6 :fail
</del><ins>+  cmd: runES6 :normal
</ins><span class="cx"> - path: es6/Function_is_subclassable_Function.prototype.call.js
</span><span class="cx">   cmd: runES6 :normal
</span><span class="cx"> - path: es6/function_name_property_accessor_properties.js
</span><span class="lines">@@ -897,15 +897,15 @@
</span><span class="cx"> - path: es6/proper_tail_calls_tail_call_optimisation_mutual_recursion.js
</span><span class="cx">   cmd: runES6 :normal
</span><span class="cx"> - path: es6/prototype_of_bound_functions_arrow_functions.js
</span><del>-  cmd: runES6 :fail
</del><ins>+  cmd: runES6 :normal
</ins><span class="cx"> - path: es6/prototype_of_bound_functions_basic_functions.js
</span><del>-  cmd: runES6 :fail
</del><ins>+  cmd: runES6 :normal
</ins><span class="cx"> - path: es6/prototype_of_bound_functions_classes.js
</span><del>-  cmd: runES6 :fail
</del><ins>+  cmd: runES6 :normal
</ins><span class="cx"> - path: es6/prototype_of_bound_functions_generator_functions.js
</span><del>-  cmd: runES6 :fail
</del><ins>+  cmd: runES6 :normal
</ins><span class="cx"> - path: es6/prototype_of_bound_functions_subclasses.js
</span><del>-  cmd: runES6 :fail
</del><ins>+  cmd: runES6 :normal
</ins><span class="cx"> - path: es6/Proxy_apply_handler.js
</span><span class="cx">   cmd: runES6 :normal
</span><span class="cx"> - path: es6/Proxy_Array.isArray_support.js
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressboundfunctionusesprototypejs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/bound-function-uses-prototype.js (0 => 196956)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/bound-function-uses-prototype.js                                (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/bound-function-uses-prototype.js        2016-02-22 22:07:16 UTC (rev 196956)
</span><span class="lines">@@ -0,0 +1,31 @@
</span><ins>+// Test ES6 feature of using the bindee's prototype when binding a function.
+
+bind = Function.prototype.bind;
+
+function testChangeProto() {
+    function foo() { }
+
+    foo.__proto__ = Object.prototype;
+
+    let bar = bind.call(foo);
+    if (bar.__proto__ !== foo.__proto__)
+        throw &quot;incorrect prototype&quot;;
+
+    foo.__proto__ = null;
+    bar = bind.call(foo);
+    if (bar.__proto__ !== foo.__proto__)
+        throw &quot;cached prototype incorrectly&quot;
+}
+testChangeProto();
+
+function testBuiltins() {
+    let bar = bind.call(Array);
+    if (bar.__proto__ !== Array.__proto__)
+        throw &quot;builtin prototype incorrect&quot;;
+
+    Array.__proto__ = null;
+    bar = bind.call(Array);
+    if (bar.__proto__ !== Array.__proto__)
+        throw &quot;builtin prototype did not change correctly.&quot;;
+}
+testBuiltins();
</ins></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressclasssubclassingfunctionjs"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/tests/stress/class-subclassing-function.js (196955 => 196956)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/class-subclassing-function.js        2016-02-22 22:02:24 UTC (rev 196955)
+++ trunk/Source/JavaScriptCore/tests/stress/class-subclassing-function.js        2016-02-22 22:07:16 UTC (rev 196956)
</span><span class="lines">@@ -1,4 +1,5 @@
</span><span class="cx"> F = class extends Function { }
</span><ins>+N = class extends null { }
</ins><span class="cx"> 
</span><span class="cx"> function test(i) {
</span><span class="cx"> 
</span><span class="lines">@@ -20,12 +21,15 @@
</span><span class="cx">         throw &quot;function was not .callable&quot;;
</span><span class="cx"> 
</span><span class="cx">     let g2 = g.bind({foo:1}, 1);
</span><del>-    if (g2 instanceof F)
-        throw &quot;the binding of a subclass should not inherit from the original function&quot;;
</del><ins>+    if (!(g2 instanceof F))
+        throw &quot;the binding of a subclass should inherit from the bound function's class&quot;;
</ins><span class="cx"> 
</span><span class="cx">     if (g2(1) !== 3)
</span><span class="cx">         throw &quot;binding didn't work&quot;;
</span><span class="cx"> 
</span><ins>+    let bound = C.bind(null)
+    if (bound.__proto__ !== C.__proto__)
+        throw &quot;binding with null as prototype didn't work&quot;;
</ins><span class="cx"> }
</span><span class="cx"> noInline(test);
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>