<!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>[201674] 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/201674">201674</a></dd>
<dt>Author</dt> <dd>commit-queue@webkit.org</dd>
<dt>Date</dt> <dd>2016-06-03 18:08:27 -0700 (Fri, 03 Jun 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>JSON.stringify replacer function calls with numeric array indices
https://bugs.webkit.org/show_bug.cgi?id=158262
Source/JavaScriptCore:

rdar://problem/26613876

Patch by Benjamin Poulain &lt;bpoulain@apple.com&gt; on 2016-06-03
Reviewed by Saam Barati.

The spec of SerializeJSONArray is pretty clear that the index
should be transformed into a string before calling SerializeJSONProperty.
See http://www.ecma-international.org/ecma-262/6.0/#sec-serializejsonarray

* runtime/JSONObject.cpp:
(JSC::PropertyNameForFunctionCall::value):

LayoutTests:

Patch by Benjamin Poulain &lt;bpoulain@apple.com&gt; on 2016-06-03
Reviewed by Saam Barati.

* js/JSON-stringify-exposes-array-indices-as-strings-expected.txt: Added.
* js/JSON-stringify-exposes-array-indices-as-strings.html: Added.
* js/script-tests/JSON-stringify-exposes-array-indices-as-strings.js: Added.
(replacer):
(testObject.toJSON):
(toJSONArrayHelper):
* js/resources/JSON-stringify.js:
* js/dom/JSON-stringify-expected.txt:
Update the test results for the native implementation.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkLayoutTestsjsdomJSONstringifyexpectedtxt">trunk/LayoutTests/js/dom/JSON-stringify-expected.txt</a></li>
<li><a href="#trunkLayoutTestsjsresourcesJSONstringifyjs">trunk/LayoutTests/js/resources/JSON-stringify.js</a></li>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeJSONObjectcpp">trunk/Source/JavaScriptCore/runtime/JSONObject.cpp</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsjsJSONstringifyexposesarrayindicesasstringsexpectedtxt">trunk/LayoutTests/js/JSON-stringify-exposes-array-indices-as-strings-expected.txt</a></li>
<li><a href="#trunkLayoutTestsjsJSONstringifyexposesarrayindicesasstringshtml">trunk/LayoutTests/js/JSON-stringify-exposes-array-indices-as-strings.html</a></li>
<li><a href="#trunkLayoutTestsjsscripttestsJSONstringifyexposesarrayindicesasstringsjs">trunk/LayoutTests/js/script-tests/JSON-stringify-exposes-array-indices-as-strings.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (201673 => 201674)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2016-06-04 00:56:57 UTC (rev 201673)
+++ trunk/LayoutTests/ChangeLog        2016-06-04 01:08:27 UTC (rev 201674)
</span><span class="lines">@@ -1,3 +1,20 @@
</span><ins>+2016-06-03  Benjamin Poulain  &lt;bpoulain@apple.com&gt;
+
+        JSON.stringify replacer function calls with numeric array indices
+        https://bugs.webkit.org/show_bug.cgi?id=158262
+
+        Reviewed by Saam Barati.
+
+        * js/JSON-stringify-exposes-array-indices-as-strings-expected.txt: Added.
+        * js/JSON-stringify-exposes-array-indices-as-strings.html: Added.
+        * js/script-tests/JSON-stringify-exposes-array-indices-as-strings.js: Added.
+        (replacer):
+        (testObject.toJSON):
+        (toJSONArrayHelper):
+        * js/resources/JSON-stringify.js:
+        * js/dom/JSON-stringify-expected.txt:
+        Update the test results for the native implementation.
+
</ins><span class="cx"> 2016-06-03  Ryosuke Niwa  &lt;rniwa@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Crash under VisibleSelection::firstRange()
</span></span></pre></div>
<a id="trunkLayoutTestsjsJSONstringifyexposesarrayindicesasstringsexpectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/js/JSON-stringify-exposes-array-indices-as-strings-expected.txt (0 => 201674)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/js/JSON-stringify-exposes-array-indices-as-strings-expected.txt                                (rev 0)
+++ trunk/LayoutTests/js/JSON-stringify-exposes-array-indices-as-strings-expected.txt        2016-06-04 01:08:27 UTC (rev 201674)
</span><span class="lines">@@ -0,0 +1,32 @@
</span><ins>+Verify that JSON.stringify passes keys as strings when calling toJSON or the replacer function.
+
+On success, you will see a series of &quot;PASS&quot; messages, followed by &quot;TEST COMPLETE&quot;.
+
+
+Test Replacer Function
+PASS typeof globalKey is &quot;string&quot;
+PASS globalKey is &quot;&quot;
+PASS typeof globalValue is &quot;object&quot;
+PASS globalValue instanceof Array is true
+PASS globalValue.length is 1
+PASS globalValue[0] is 42
+PASS typeof globalKey is &quot;string&quot;
+PASS globalKey is &quot;0&quot;
+PASS typeof globalValue is &quot;number&quot;
+PASS globalValue is 42
+PASS JSON.stringify([42], replacer) is &quot;[5]&quot;
+
+Test toJSON Function
+PASS toJSONArrayHelperCallCounter is 0
+PASS globalThis === testArray is true
+PASS typeof globalKey is &quot;string&quot;
+PASS globalKey is &quot;&quot;
+PASS toJSONArrayHelperCallCounter is 1
+PASS globalThis === testObject is true
+PASS typeof globalKey is &quot;string&quot;
+PASS globalKey is &quot;0&quot;
+PASS JSON.stringify(testArray) is &quot;[true]&quot;
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
</ins></span></pre></div>
<a id="trunkLayoutTestsjsJSONstringifyexposesarrayindicesasstringshtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/js/JSON-stringify-exposes-array-indices-as-strings.html (0 => 201674)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/js/JSON-stringify-exposes-array-indices-as-strings.html                                (rev 0)
+++ trunk/LayoutTests/js/JSON-stringify-exposes-array-indices-as-strings.html        2016-06-04 01:08:27 UTC (rev 201674)
</span><span class="lines">@@ -0,0 +1,10 @@
</span><ins>+&lt;!DOCTYPE&gt;
+&lt;html&gt;
+&lt;head&gt;
+&lt;script src=&quot;../resources/js-test-pre.js&quot;&gt;&lt;/script&gt;
+&lt;/head&gt;
+&lt;body&gt;
+&lt;script src=&quot;script-tests/JSON-stringify-exposes-array-indices-as-strings.js&quot;&gt;&lt;/script&gt;
+&lt;script src=&quot;../resources/js-test-post.js&quot;&gt;&lt;/script&gt;
+&lt;/body&gt;
+&lt;/html&gt;
</ins></span></pre></div>
<a id="trunkLayoutTestsjsdomJSONstringifyexpectedtxt"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/js/dom/JSON-stringify-expected.txt (201673 => 201674)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/js/dom/JSON-stringify-expected.txt        2016-06-04 00:56:57 UTC (rev 201673)
+++ trunk/LayoutTests/js/dom/JSON-stringify-expected.txt        2016-06-04 01:08:27 UTC (rev 201674)
</span><span class="lines">@@ -147,7 +147,7 @@
</span><span class="cx">         jsonObject.stringify([1,2,3,4,5], function(k,v){allString = allString &amp;&amp; (typeof k == &quot;string&quot;); return v});
</span><span class="cx">         return allString;
</span><span class="cx">     }
</span><del>-PASS tests[i](nativeJSON) is tests[i](JSON)
</del><ins>+PASS tests[i](nativeJSON) is tests[i].expected
</ins><span class="cx"> function (jsonObject){
</span><span class="cx">         var allString = true;
</span><span class="cx">         var array = [];
</span></span></pre></div>
<a id="trunkLayoutTestsjsresourcesJSONstringifyjs"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/js/resources/JSON-stringify.js (201673 => 201674)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/js/resources/JSON-stringify.js        2016-06-04 00:56:57 UTC (rev 201673)
+++ trunk/LayoutTests/js/resources/JSON-stringify.js        2016-06-04 01:08:27 UTC (rev 201674)
</span><span class="lines">@@ -144,6 +144,7 @@
</span><span class="cx">         jsonObject.stringify([1,2,3,4,5], function(k,v){allString = allString &amp;&amp; (typeof k == &quot;string&quot;); return v});
</span><span class="cx">         return allString;
</span><span class="cx">     });
</span><ins>+    result[result.length - 1].expected = true;
</ins><span class="cx">     result.push(function(jsonObject){
</span><span class="cx">         var allString = true;
</span><span class="cx">         var array = [];
</span><span class="lines">@@ -346,7 +347,7 @@
</span><span class="cx">         jsonObject.stringify([1,2,3,,,,4,5,6], replaceFunc);
</span><span class="cx">         return replaceTracker;
</span><span class="cx">     });
</span><del>-    result[result.length - 1].expected = '(string)[1,2,3,null,null,null,4,5,6];0(number)1;1(number)2;2(number)3;3(number)undefined;4(number)undefined;5(number)undefined;6(number)4;7(number)5;8(number)6;'
</del><ins>+    result[result.length - 1].expected = '(string)[1,2,3,null,null,null,4,5,6];0(string)1;1(string)2;2(string)3;3(string)undefined;4(string)undefined;5(string)undefined;6(string)4;7(string)5;8(string)6;'
</ins><span class="cx">     result.push(function(jsonObject){
</span><span class="cx">         replaceTracker = &quot;&quot;;
</span><span class="cx">         jsonObject.stringify({a:&quot;a&quot;, b:&quot;b&quot;, c:&quot;c&quot;, 3: &quot;d&quot;, 2: &quot;e&quot;, 1: &quot;f&quot;}, replaceFunc);
</span><span class="lines">@@ -427,7 +428,7 @@
</span><span class="cx">         try { jsonObject.stringify(cyclicArray); } catch(e) { cycleTracker += &quot; -&gt; exception&quot; }
</span><span class="cx">         return cycleTracker;
</span><span class="cx">     });
</span><del>-    result[result.length - 1].expected = &quot;0(number):[object Object]first, -&gt; exception&quot;;
</del><ins>+    result[result.length - 1].expected = &quot;0(string):[object Object]first, -&gt; exception&quot;;
</ins><span class="cx">     function createArray(len, o) { var r = []; for (var i = 0; i &lt; len; i++) r[i] = o; return r; }
</span><span class="cx">     var getterCalls;
</span><span class="cx">     var magicObject = createArray(10, {abcdefg: [1,2,5,&quot;ab&quot;, null, undefined, true, false,,], 
</span></span></pre></div>
<a id="trunkLayoutTestsjsscripttestsJSONstringifyexposesarrayindicesasstringsjs"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/js/script-tests/JSON-stringify-exposes-array-indices-as-strings.js (0 => 201674)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/js/script-tests/JSON-stringify-exposes-array-indices-as-strings.js                                (rev 0)
+++ trunk/LayoutTests/js/script-tests/JSON-stringify-exposes-array-indices-as-strings.js        2016-06-04 01:08:27 UTC (rev 201674)
</span><span class="lines">@@ -0,0 +1,64 @@
</span><ins>+&quot;use strict&quot;;
+
+description(&quot;Verify that JSON.stringify passes keys as strings when calling toJSON or the replacer function.&quot;);
+
+debug(&quot;Test Replacer Function&quot;);
+var globalKey;
+var globalValue;
+var globalIndex = 0;
+function replacer(key, value) {
+    globalKey = key;
+    globalValue = value;
+    if (globalIndex == 0) {
+        shouldBeEqualToString(&quot;typeof globalKey&quot;, &quot;string&quot;);
+        shouldBeEqualToString(&quot;globalKey&quot;, &quot;&quot;);
+        shouldBeEqualToString(&quot;typeof globalValue&quot;, &quot;object&quot;);
+        shouldBeTrue(&quot;globalValue instanceof Array&quot;);
+        shouldBe(&quot;globalValue.length&quot;, &quot;1&quot;);
+        shouldBe(&quot;globalValue[0]&quot;, &quot;42&quot;);
+    } else if (globalIndex == 1) {
+        globalKey = key;
+        shouldBeEqualToString(&quot;typeof globalKey&quot;, &quot;string&quot;);
+        shouldBeEqualToString(&quot;globalKey&quot;, &quot;0&quot;);
+        shouldBeEqualToString(&quot;typeof globalValue&quot;, &quot;number&quot;);
+        shouldBe(&quot;globalValue&quot;, &quot;42&quot;);
+        value = 5;
+    } else {
+        shouldBeTrue(false);
+    }
+    ++globalIndex;
+    return value;
+}
+
+shouldBeEqualToString(&quot;JSON.stringify([42], replacer)&quot;, &quot;[5]&quot;);
+
+debug(&quot;&quot;);
+debug(&quot;Test toJSON Function&quot;);
+var globalThis;
+var toJSONArrayHelperCallCounter = 0;
+var testObject = {
+    toJSON: function(key) {
+        globalThis = this;
+        globalKey = key;
+        shouldBe(&quot;toJSONArrayHelperCallCounter&quot;, &quot;1&quot;);
+        shouldBeTrue(&quot;globalThis === testObject&quot;);
+        shouldBeEqualToString(&quot;typeof globalKey&quot;, &quot;string&quot;);
+        shouldBeEqualToString(&quot;globalKey&quot;, &quot;0&quot;);
+        return true;
+    }
+}
+
+var testArray = [testObject];
+function toJSONArrayHelper(key) {
+    globalThis = this;
+    globalKey = key;
+    shouldBe(&quot;toJSONArrayHelperCallCounter&quot;, &quot;0&quot;);
+    shouldBeTrue(&quot;globalThis === testArray&quot;);
+    shouldBeEqualToString(&quot;typeof globalKey&quot;, &quot;string&quot;);
+    shouldBeEqualToString(&quot;globalKey&quot;, &quot;&quot;);
+    ++toJSONArrayHelperCallCounter;
+    return this;
+}
+
+Array.prototype.toJSON = toJSONArrayHelper;
+shouldBeEqualToString(&quot;JSON.stringify(testArray)&quot;, &quot;[true]&quot;);
</ins></span></pre></div>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (201673 => 201674)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2016-06-04 00:56:57 UTC (rev 201673)
+++ trunk/Source/JavaScriptCore/ChangeLog        2016-06-04 01:08:27 UTC (rev 201674)
</span><span class="lines">@@ -1,3 +1,18 @@
</span><ins>+2016-06-03  Benjamin Poulain  &lt;bpoulain@apple.com&gt;
+
+        JSON.stringify replacer function calls with numeric array indices
+        https://bugs.webkit.org/show_bug.cgi?id=158262
+        rdar://problem/26613876
+
+        Reviewed by Saam Barati.
+
+        The spec of SerializeJSONArray is pretty clear that the index
+        should be transformed into a string before calling SerializeJSONProperty.
+        See http://www.ecma-international.org/ecma-262/6.0/#sec-serializejsonarray
+
+        * runtime/JSONObject.cpp:
+        (JSC::PropertyNameForFunctionCall::value):
+
</ins><span class="cx"> 2016-06-03  Saam barati  &lt;sbarati@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Proxy.ownKeys should no longer throw an exception when duplicate keys are returned and the target is non-extensible
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeJSONObjectcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/JSONObject.cpp (201673 => 201674)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/JSONObject.cpp        2016-06-04 00:56:57 UTC (rev 201673)
+++ trunk/Source/JavaScriptCore/runtime/JSONObject.cpp        2016-06-04 01:08:27 UTC (rev 201674)
</span><span class="lines">@@ -200,8 +200,12 @@
</span><span class="cx">     if (!m_value) {
</span><span class="cx">         if (m_identifier)
</span><span class="cx">             m_value = jsString(exec, m_identifier-&gt;string());
</span><del>-        else
-            m_value = jsNumber(m_number);
</del><ins>+        else {
+            VM&amp; vm = exec-&gt;vm();
+            if (m_number &lt;= 9)
+                return vm.smallStrings.singleCharacterString(m_number + '0');
+            m_value = jsNontrivialString(&amp;vm, vm.numericStrings.add(m_number));
+        }
</ins><span class="cx">     }
</span><span class="cx">     return m_value;
</span><span class="cx"> }
</span></span></pre>
</div>
</div>

</body>
</html>