<!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>[180564] 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/180564">180564</a></dd>
<dt>Author</dt> <dd>fpizlo@apple.com</dd>
<dt>Date</dt> <dd>2015-02-24 09:08:06 -0800 (Tue, 24 Feb 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Our bizarre behavior on Arguments::defineOwnProperty should be deliberate rather than a spaghetti incident
https://bugs.webkit.org/show_bug.cgi?id=141951

Reviewed by Benjamin Poulain.
        
This patch has no behavioral change, but it simplifies a bunch of wrong code. The code is
still wrong in exactly the same way, but at least it's obvious what's going on. The wrongness
is covered by this bug: https://bugs.webkit.org/show_bug.cgi?id=141952.

* runtime/Arguments.cpp:
(JSC::Arguments::copyBackingStore): We should only see the arguments token; assert otherwise. This works because if the GC sees the butterfly token it calls the JSObject::copyBackingStore method directly.
(JSC::Arguments::defineOwnProperty): Make our bizarre behavior deliberate rather than an accident of a decade of patches.
* tests/stress/arguments-bizarre-behavior.js: Added.
(foo):
* tests/stress/arguments-bizarre-behaviour-disable-enumerability.js: Added. My choice of spellings of the word &quot;behavio[u]r&quot; is almost as consistent as our implementation of arguments.
(foo):
* tests/stress/arguments-custom-properties-gc.js: Added. I added this test because at first I was unsure if we GCd arguments correctly.
(makeBaseArguments):
(makeArray):
(cons):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeArgumentscpp">trunk/Source/JavaScriptCore/runtime/Arguments.cpp</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoretestsstressargumentsbizarrebehaviorjs">trunk/Source/JavaScriptCore/tests/stress/arguments-bizarre-behavior.js</a></li>
<li><a href="#trunkSourceJavaScriptCoretestsstressargumentsbizarrebehaviourdisableenumerabilityjs">trunk/Source/JavaScriptCore/tests/stress/arguments-bizarre-behaviour-disable-enumerability.js</a></li>
<li><a href="#trunkSourceJavaScriptCoretestsstressargumentscustompropertiesgcjs">trunk/Source/JavaScriptCore/tests/stress/arguments-custom-properties-gc.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (180563 => 180564)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2015-02-24 17:07:46 UTC (rev 180563)
+++ trunk/Source/JavaScriptCore/ChangeLog        2015-02-24 17:08:06 UTC (rev 180564)
</span><span class="lines">@@ -1,3 +1,26 @@
</span><ins>+2015-02-23  Filip Pizlo  &lt;fpizlo@apple.com&gt;
+
+        Our bizarre behavior on Arguments::defineOwnProperty should be deliberate rather than a spaghetti incident
+        https://bugs.webkit.org/show_bug.cgi?id=141951
+
+        Reviewed by Benjamin Poulain.
+        
+        This patch has no behavioral change, but it simplifies a bunch of wrong code. The code is
+        still wrong in exactly the same way, but at least it's obvious what's going on. The wrongness
+        is covered by this bug: https://bugs.webkit.org/show_bug.cgi?id=141952.
+
+        * runtime/Arguments.cpp:
+        (JSC::Arguments::copyBackingStore): We should only see the arguments token; assert otherwise. This works because if the GC sees the butterfly token it calls the JSObject::copyBackingStore method directly.
+        (JSC::Arguments::defineOwnProperty): Make our bizarre behavior deliberate rather than an accident of a decade of patches.
+        * tests/stress/arguments-bizarre-behavior.js: Added.
+        (foo):
+        * tests/stress/arguments-bizarre-behaviour-disable-enumerability.js: Added. My choice of spellings of the word &quot;behavio[u]r&quot; is almost as consistent as our implementation of arguments.
+        (foo):
+        * tests/stress/arguments-custom-properties-gc.js: Added. I added this test because at first I was unsure if we GCd arguments correctly.
+        (makeBaseArguments):
+        (makeArray):
+        (cons):
+
</ins><span class="cx"> 2015-02-23  Commit Queue  &lt;commit-queue@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Unreviewed, rolling out r180547 and r180550.
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeArgumentscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/Arguments.cpp (180563 => 180564)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/Arguments.cpp        2015-02-24 17:07:46 UTC (rev 180563)
+++ trunk/Source/JavaScriptCore/runtime/Arguments.cpp        2015-02-24 17:08:06 UTC (rev 180564)
</span><span class="lines">@@ -81,7 +81,7 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     default:
</span><del>-        return;
</del><ins>+        RELEASE_ASSERT_NOT_REACHED();
</ins><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx">     
</span><span class="lines">@@ -295,35 +295,33 @@
</span><span class="cx">     unsigned i = propertyName.asIndex();
</span><span class="cx">     if (i &lt; thisObject-&gt;m_numArguments) {
</span><span class="cx">         RELEASE_ASSERT(i &lt; PropertyName::NotAnIndex);
</span><del>-        // If the property is not yet present on the object, and is not yet marked as deleted, then add it now.
-        PropertySlot slot(thisObject);
-        if (!thisObject-&gt;isDeletedArgument(i) &amp;&amp; !JSObject::getOwnPropertySlot(thisObject, exec, propertyName, slot)) {
-            JSValue value = thisObject-&gt;tryGetArgument(i);
-            ASSERT(value);
-            object-&gt;putDirectMayBeIndex(exec, propertyName, value);
-        }
-        if (!Base::defineOwnProperty(object, exec, propertyName, descriptor, shouldThrow))
-            return false;
-
-        // From ES 5.1, 10.6 Arguments Object
-        // 5. If the value of isMapped is not undefined, then
</del><ins>+        
</ins><span class="cx">         if (thisObject-&gt;isArgument(i)) {
</span><del>-            // a. If IsAccessorDescriptor(Desc) is true, then
-            if (descriptor.isAccessorDescriptor()) {
-                // i. Call the [[Delete]] internal method of map passing P, and false as the arguments.
-                thisObject-&gt;tryDeleteArgument(exec-&gt;vm(), i);
-            } else { // b. Else
-                // i. If Desc.[[Value]] is present, then
-                // 1. Call the [[Put]] internal method of map passing P, Desc.[[Value]], and Throw as the arguments.
</del><ins>+            if (!descriptor.isAccessorDescriptor()) {
+                // If the property is not deleted and we are using a non-accessor descriptor, then
+                // make sure that the aliased argument sees the value.
</ins><span class="cx">                 if (descriptor.value())
</span><span class="cx">                     thisObject-&gt;trySetArgument(exec-&gt;vm(), i, descriptor.value());
</span><del>-                // ii. If Desc.[[Writable]] is present and its value is false, then
-                // 1. Call the [[Delete]] internal method of map passing P and false as arguments.
-                if (descriptor.writablePresent() &amp;&amp; !descriptor.writable())
-                    thisObject-&gt;tryDeleteArgument(exec-&gt;vm(), i);
</del><ins>+            
+                // If the property is not deleted and we are using a non-accessor, writable
+                // descriptor, then we are done. The argument continues to be aliased. Note that we
+                // ignore the request to change enumerability. We appear to have always done so, in
+                // cases where the argument was still aliased.
+                // FIXME: https://bugs.webkit.org/show_bug.cgi?id=141952
+                if (descriptor.writable())
+                    return true;
</ins><span class="cx">             }
</span><ins>+            
+            // If the property is a non-deleted argument, then move it into the base object and then
+            // delete it.
+            JSValue value = thisObject-&gt;tryGetArgument(i);
+            ASSERT(value);
+            object-&gt;putDirectMayBeIndex(exec, propertyName, value);
+            thisObject-&gt;tryDeleteArgument(exec-&gt;vm(), i);
</ins><span class="cx">         }
</span><del>-        return true;
</del><ins>+        
+        // Now just let the normal object machinery do its thing.
+        return Base::defineOwnProperty(object, exec, propertyName, descriptor, shouldThrow);
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     if (propertyName == exec-&gt;propertyNames().length &amp;&amp; !thisObject-&gt;m_overrodeLength) {
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressargumentsbizarrebehaviorjs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/arguments-bizarre-behavior.js (0 => 180564)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/arguments-bizarre-behavior.js                                (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/arguments-bizarre-behavior.js        2015-02-24 17:08:06 UTC (rev 180564)
</span><span class="lines">@@ -0,0 +1,25 @@
</span><ins>+function foo(x) {
+    Object.defineProperty(arguments, 0, {configurable: true, enumerable: true, writable:true, value:42});
+    return [x, arguments[0], arguments]
+}
+
+var result = foo(1);
+
+if (result[0] !== 42)
+    throw new Error();
+
+if (result[1] !== 42)
+    throw new Error();
+
+if (Array.prototype.join.call(result[2], &quot;,&quot;) != &quot;42&quot;)
+    throw new Error();
+
+var array = [];
+for (var s in result[2])
+    array.push(s);
+
+if (array.join(&quot;,&quot;) != &quot;0&quot;)
+    throw new Error();
+
+if (Object.keys(result[2]).join(&quot;,&quot;) != &quot;0&quot;)
+    throw new Error();
</ins></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressargumentsbizarrebehaviourdisableenumerabilityjs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/arguments-bizarre-behaviour-disable-enumerability.js (0 => 180564)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/arguments-bizarre-behaviour-disable-enumerability.js                                (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/arguments-bizarre-behaviour-disable-enumerability.js        2015-02-24 17:08:06 UTC (rev 180564)
</span><span class="lines">@@ -0,0 +1,30 @@
</span><ins>+function foo(x) {
+    Object.defineProperty(arguments, 0, {configurable: true, enumerable: false, writable:true, value:42});
+    return [x, arguments[0], arguments]
+}
+
+var result = foo(1);
+
+if (result[0] !== 42)
+    throw new Error();
+
+if (result[1] !== 42)
+    throw new Error();
+
+if (Array.prototype.join.call(result[2], &quot;,&quot;) != &quot;42&quot;)
+    throw new Error();
+
+var array = [];
+for (var s in result[2])
+    array.push(s);
+
+if (array.join(&quot;,&quot;) != &quot;0&quot;)
+    throw new Error();
+
+if (Object.keys(result[2]).join(&quot;,&quot;) != &quot;0&quot;)
+    throw new Error();
+
+// FIXME: This is totally weird!
+// https://bugs.webkit.org/show_bug.cgi?id=141952
+if (Object.getOwnPropertyDescriptor(result[2], 0).enumerable !== true)
+    throw new Error();
</ins></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressargumentscustompropertiesgcjs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/arguments-custom-properties-gc.js (0 => 180564)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/arguments-custom-properties-gc.js                                (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/arguments-custom-properties-gc.js        2015-02-24 17:08:06 UTC (rev 180564)
</span><span class="lines">@@ -0,0 +1,36 @@
</span><ins>+function makeBaseArguments() {
+    return arguments;
+}
+
+noInline(makeBaseArguments);
+
+function makeArray(length) {
+    var array = new Array(length);
+    for (var i = 0; i &lt; length; ++i)
+        array[i] = 99999;
+    return array;
+}
+
+noInline(makeArray);
+
+function cons(f) {
+    var result = makeBaseArguments();
+    result.f = f;
+    return result;
+}
+
+var array = [];
+for (var i = 0; i &lt; 100000; ++i)
+    array.push(cons(i));
+
+for (var i = 0; i &lt; 1000000; ++i) {
+    var j = (i * 3) % array.length;
+    array[j] = cons(j);
+    
+    makeArray(i % 7);
+}
+
+for (var i = 0; i &lt; array.length; ++i) {
+    if (array[i].f != i)
+        throw &quot;Error: bad value of f at &quot; + i + &quot;: &quot; + array[i].f;
+}
</ins></span></pre>
</div>
</div>

</body>
</html>