<!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>[197918] 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/197918">197918</a></dd>
<dt>Author</dt> <dd>sbarati@apple.com</dd>
<dt>Date</dt> <dd>2016-03-09 18:41:34 -0800 (Wed, 09 Mar 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>Add proper JSON.stringify support for Proxy when the target is an array
https://bugs.webkit.org/show_bug.cgi?id=155180

Reviewed by Darin Adler.

This patch makes the following type of program true:
`JSON.stringify(new Proxy([25], {})) === &quot;[25]&quot;`

We need to change the JSON stringifier to use the IsArray test
in section 7.2.2 of ES6 spec instead of the JSC inherits(JSArray::info())
test.

This patch also adds tests for general JSON.stringify support
of Proxy.

* runtime/ArrayConstructor.cpp:
(JSC::arrayConstructorIsArray):
(JSC::arrayConstructorPrivateFuncIsArrayConstructor):
* runtime/ArrayConstructor.h:
(JSC::isArray):
* runtime/JSONObject.cpp:
(JSC::Stringifier::Holder::object):
(JSC::Stringifier::appendStringifiedValue):
(JSC::Stringifier::startNewLine):
(JSC::Stringifier::Holder::Holder):
* tests/es6.yaml:
* tests/stress/proxy-json.js: Added.
(assert):
(test):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeArrayConstructorcpp">trunk/Source/JavaScriptCore/runtime/ArrayConstructor.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeArrayConstructorh">trunk/Source/JavaScriptCore/runtime/ArrayConstructor.h</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeJSONObjectcpp">trunk/Source/JavaScriptCore/runtime/JSONObject.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoretestses6yaml">trunk/Source/JavaScriptCore/tests/es6.yaml</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoretestsstressproxyjsonjs">trunk/Source/JavaScriptCore/tests/stress/proxy-json.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (197917 => 197918)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2016-03-10 02:33:12 UTC (rev 197917)
+++ trunk/Source/JavaScriptCore/ChangeLog        2016-03-10 02:41:34 UTC (rev 197918)
</span><span class="lines">@@ -1,3 +1,35 @@
</span><ins>+2016-03-09  Saam barati  &lt;sbarati@apple.com&gt;
+
+        Add proper JSON.stringify support for Proxy when the target is an array
+        https://bugs.webkit.org/show_bug.cgi?id=155180
+
+        Reviewed by Darin Adler.
+
+        This patch makes the following type of program true:
+        `JSON.stringify(new Proxy([25], {})) === &quot;[25]&quot;`
+
+        We need to change the JSON stringifier to use the IsArray test
+        in section 7.2.2 of ES6 spec instead of the JSC inherits(JSArray::info())
+        test.
+
+        This patch also adds tests for general JSON.stringify support
+        of Proxy.
+
+        * runtime/ArrayConstructor.cpp:
+        (JSC::arrayConstructorIsArray):
+        (JSC::arrayConstructorPrivateFuncIsArrayConstructor):
+        * runtime/ArrayConstructor.h:
+        (JSC::isArray):
+        * runtime/JSONObject.cpp:
+        (JSC::Stringifier::Holder::object):
+        (JSC::Stringifier::appendStringifiedValue):
+        (JSC::Stringifier::startNewLine):
+        (JSC::Stringifier::Holder::Holder):
+        * tests/es6.yaml:
+        * tests/stress/proxy-json.js: Added.
+        (assert):
+        (test):
+
</ins><span class="cx"> 2016-03-09  Saam Barati  &lt;sbarati@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         ES6: Implement lexical scoping for function definitions in strict mode
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeArrayConstructorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/ArrayConstructor.cpp (197917 => 197918)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/ArrayConstructor.cpp        2016-03-10 02:33:12 UTC (rev 197917)
+++ trunk/Source/JavaScriptCore/runtime/ArrayConstructor.cpp        2016-03-10 02:41:34 UTC (rev 197918)
</span><span class="lines">@@ -130,26 +130,7 @@
</span><span class="cx"> // https://tc39.github.io/ecma262/#sec-isarray
</span><span class="cx"> EncodedJSValue JSC_HOST_CALL arrayConstructorIsArray(ExecState* exec)
</span><span class="cx"> {
</span><del>-    JSValue argumentValue = exec-&gt;argument(0);
-
-    if (!argumentValue.isObject())
-        return JSValue::encode(jsBoolean(false));
-
-    JSObject* argument = jsCast&lt;JSObject*&gt;(argumentValue);
-    while (true) {
-        if (argument-&gt;inherits(JSArray::info()))
-            return JSValue::encode(jsBoolean(true));
-
-        if (argument-&gt;type() != ProxyObjectType)
-            return JSValue::encode(jsBoolean(false));
-
-        ProxyObject* proxy = jsCast&lt;ProxyObject*&gt;(argument);
-        if (proxy-&gt;isRevoked())
-            return throwVMTypeError(exec, ASCIILiteral(&quot;Array.isArray can not be called on a Proxy that has been revoked.&quot;));
-        argument = proxy-&gt;target();
-    }
-
-    ASSERT_NOT_REACHED();
</del><ins>+    return JSValue::encode(jsBoolean(isArray(exec, exec-&gt;argument(0))));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> EncodedJSValue JSC_HOST_CALL arrayConstructorPrivateFuncIsArrayConstructor(ExecState* exec)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeArrayConstructorh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/ArrayConstructor.h (197917 => 197918)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/ArrayConstructor.h        2016-03-10 02:33:12 UTC (rev 197917)
+++ trunk/Source/JavaScriptCore/runtime/ArrayConstructor.h        2016-03-10 02:41:34 UTC (rev 197918)
</span><span class="lines">@@ -22,6 +22,7 @@
</span><span class="cx"> #define ArrayConstructor_h
</span><span class="cx"> 
</span><span class="cx"> #include &quot;InternalFunction.h&quot;
</span><ins>+#include &quot;ProxyObject.h&quot;
</ins><span class="cx"> 
</span><span class="cx"> namespace JSC {
</span><span class="cx"> 
</span><span class="lines">@@ -64,6 +65,32 @@
</span><span class="cx"> 
</span><span class="cx"> EncodedJSValue JSC_HOST_CALL arrayConstructorPrivateFuncIsArrayConstructor(ExecState*);
</span><span class="cx"> 
</span><ins>+// ES6 7.2.2
+// https://tc39.github.io/ecma262/#sec-isarray
+inline bool isArray(ExecState* exec, JSValue argumentValue)
+{
+    if (!argumentValue.isObject())
+        return false;
+
+    JSObject* argument = jsCast&lt;JSObject*&gt;(argumentValue);
+    while (true) {
+        if (argument-&gt;inherits(JSArray::info()))
+            return true;
+
+        if (argument-&gt;type() != ProxyObjectType)
+            return false;
+
+        ProxyObject* proxy = jsCast&lt;ProxyObject*&gt;(argument);
+        if (proxy-&gt;isRevoked()) {
+            throwTypeError(exec, ASCIILiteral(&quot;Array.isArray can not be called on a Proxy that has been revoked.&quot;));
+            return false;
+        }
+        argument = proxy-&gt;target();
+    }
+
+    ASSERT_NOT_REACHED();
+}
+
</ins><span class="cx"> } // namespace JSC
</span><span class="cx"> 
</span><span class="cx"> #endif // ArrayConstructor_h
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeJSONObjectcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/JSONObject.cpp (197917 => 197918)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/JSONObject.cpp        2016-03-10 02:33:12 UTC (rev 197917)
+++ trunk/Source/JavaScriptCore/runtime/JSONObject.cpp        2016-03-10 02:41:34 UTC (rev 197918)
</span><span class="lines">@@ -26,6 +26,7 @@
</span><span class="cx"> #include &quot;config.h&quot;
</span><span class="cx"> #include &quot;JSONObject.h&quot;
</span><span class="cx"> 
</span><ins>+#include &quot;ArrayConstructor.h&quot;
</ins><span class="cx"> #include &quot;BooleanObject.h&quot;
</span><span class="cx"> #include &quot;Error.h&quot;
</span><span class="cx"> #include &quot;ExceptionHelpers.h&quot;
</span><span class="lines">@@ -92,7 +93,7 @@
</span><span class="cx"> private:
</span><span class="cx">     class Holder {
</span><span class="cx">     public:
</span><del>-        Holder(VM&amp;, JSObject*);
</del><ins>+        Holder(VM&amp;, ExecState*, JSObject*);
</ins><span class="cx"> 
</span><span class="cx">         JSObject* object() const { return m_object.get(); }
</span><span class="cx"> 
</span><span class="lines">@@ -289,9 +290,10 @@
</span><span class="cx"> 
</span><span class="cx"> Stringifier::StringifyResult Stringifier::appendStringifiedValue(StringBuilder&amp; builder, JSValue value, JSObject* holder, const PropertyNameForFunctionCall&amp; propertyName)
</span><span class="cx"> {
</span><ins>+    VM&amp; vm = m_exec-&gt;vm();
</ins><span class="cx">     // Call the toJSON function.
</span><span class="cx">     value = toJSON(value, propertyName);
</span><del>-    if (m_exec-&gt;hadException())
</del><ins>+    if (vm.exception())
</ins><span class="cx">         return StringifyFailed;
</span><span class="cx"> 
</span><span class="cx">     // Call the replacer function.
</span><span class="lines">@@ -300,7 +302,7 @@
</span><span class="cx">         args.append(propertyName.value(m_exec));
</span><span class="cx">         args.append(value);
</span><span class="cx">         value = call(m_exec, m_replacer.get(), m_replacerCallType, m_replacerCallData, holder, args);
</span><del>-        if (m_exec-&gt;hadException())
</del><ins>+        if (vm.exception())
</ins><span class="cx">             return StringifyFailed;
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="lines">@@ -314,7 +316,7 @@
</span><span class="cx"> 
</span><span class="cx">     value = unwrapBoxedPrimitive(m_exec, value);
</span><span class="cx"> 
</span><del>-    if (m_exec-&gt;hadException())
</del><ins>+    if (vm.exception())
</ins><span class="cx">         return StringifyFailed;
</span><span class="cx"> 
</span><span class="cx">     if (value.isBoolean()) {
</span><span class="lines">@@ -364,14 +366,17 @@
</span><span class="cx">             return StringifyFailed;
</span><span class="cx">         }
</span><span class="cx">     }
</span><ins>+
</ins><span class="cx">     bool holderStackWasEmpty = m_holderStack.isEmpty();
</span><del>-    m_holderStack.append(Holder(m_exec-&gt;vm(), object));
</del><ins>+    m_holderStack.append(Holder(vm, m_exec, object));
+    if (UNLIKELY(vm.exception()))
+        return StringifyFailed;
</ins><span class="cx">     if (!holderStackWasEmpty)
</span><span class="cx">         return StringifySucceeded;
</span><span class="cx"> 
</span><span class="cx">     do {
</span><span class="cx">         while (m_holderStack.last().appendNextProperty(*this, builder)) {
</span><del>-            if (m_exec-&gt;hadException())
</del><ins>+            if (vm.exception())
</ins><span class="cx">                 return StringifyFailed;
</span><span class="cx">         }
</span><span class="cx">         m_holderStack.removeLast();
</span><span class="lines">@@ -408,9 +413,9 @@
</span><span class="cx">     builder.append(m_indent);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-inline Stringifier::Holder::Holder(VM&amp; vm, JSObject* object)
</del><ins>+inline Stringifier::Holder::Holder(VM&amp; vm, ExecState* exec, JSObject* object)
</ins><span class="cx">     : m_object(vm, object)
</span><del>-    , m_isArray(object-&gt;inherits(JSArray::info()))
</del><ins>+    , m_isArray(isArray(exec, object))
</ins><span class="cx">     , m_index(0)
</span><span class="cx"> #ifndef NDEBUG
</span><span class="cx">     , m_size(0)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestses6yaml"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/tests/es6.yaml (197917 => 197918)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/es6.yaml        2016-03-10 02:33:12 UTC (rev 197917)
+++ trunk/Source/JavaScriptCore/tests/es6.yaml        2016-03-10 02:41:34 UTC (rev 197918)
</span><span class="lines">@@ -1057,7 +1057,7 @@
</span><span class="cx"> - path: es6/Proxy_isExtensible_handler.js
</span><span class="cx">   cmd: runES6 :normal
</span><span class="cx"> - path: es6/Proxy_JSON.stringify_support.js
</span><del>-  cmd: runES6 :fail
</del><ins>+  cmd: runES6 :normal
</ins><span class="cx"> - path: es6/Proxy_ownKeys_handler.js
</span><span class="cx">   cmd: runES6 :normal
</span><span class="cx"> - path: es6/Proxy_preventExtensions_handler.js
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressproxyjsonjs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/proxy-json.js (0 => 197918)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/proxy-json.js                                (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/proxy-json.js        2016-03-10 02:41:34 UTC (rev 197918)
</span><span class="lines">@@ -0,0 +1,114 @@
</span><ins>+function assert(b) {
+    if (!b)
+        throw new Error(&quot;Bad assertion.&quot;);
+}
+
+function test(f) {
+    for (let i = 0; i &lt; 500; i++)
+        f();
+}
+
+test(function() {
+    let proxy = new Proxy([], {});
+    assert(JSON.stringify(proxy) === &quot;[]&quot;);
+});
+
+test(function() {
+    let target = [&quot;foo&quot;];
+    let proxy = new Proxy(target, {});
+    assert(JSON.stringify(proxy) === JSON.stringify(target));
+});
+
+test(function() {
+    let target = {
+        foo: 25,
+        bar: false,
+        0: 45
+    };
+    let proxy = new Proxy(target, {});
+    assert(JSON.stringify(proxy) === JSON.stringify(target));
+});
+
+test(function() {
+    let target = {
+        foo: [&quot;baz&quot;, {foo: 45}],
+        bar: false,
+        0: 45,
+        baz: &quot;hello world&quot;,
+        jaz: 4553.434
+    };
+    let proxy = new Proxy(target, {});
+    assert(JSON.stringify(proxy) === JSON.stringify(target));
+});
+
+test(function() {
+    let target = {
+        foo: [&quot;baz&quot;, {foo: 45}],
+        bar: false,
+        0: 45,
+        baz: &quot;hello world&quot;,
+        jaz: 4553.434
+    };
+    let proxy = new Proxy(target, {});
+    for (let i = 0; i &lt; 50; i++)
+        proxy = new Proxy(proxy, {});
+    assert(JSON.stringify(proxy) === JSON.stringify(target));
+});
+
+test(function() {
+    let target = [20, 30, &quot;foo&quot;, {hello: &quot;world&quot;}];
+    let proxy = new Proxy(target, {});
+    for (let i = 0; i &lt; 50; i++)
+        proxy = new Proxy(proxy, {});
+    assert(JSON.stringify(proxy) === JSON.stringify(target));
+});
+
+test(function() {
+    let target = {
+        foo: [&quot;baz&quot;, {foo: 45}],
+        bar: false,
+        0: 45,
+        baz: &quot;hello world&quot;,
+        jaz: 4553.434
+    };
+    let {proxy, revoke} = Proxy.revocable(target, {});
+    assert(JSON.stringify(proxy) === JSON.stringify(target));
+
+    revoke();
+    JSON.stringify(target); // Things are ok.
+    let threw = false;
+    try {
+        JSON.stringify(proxy); // Things are not ok.
+    } catch(e) {
+        threw = true;
+        assert(e.toString() === &quot;TypeError: Proxy has already been revoked. No more operations are allowed to be performed on it.&quot;);
+    }
+    assert(threw);
+});
+
+test(function() {
+    let target = [&quot;foo&quot;, &quot;bar&quot;, 25, false];
+    let proxy = new Proxy(target, {});
+    let revoke;
+    for (let i = 0; i &lt; 50; i++) {
+        if (i === 25) {
+            let result = Proxy.revocable(proxy, {});
+            proxy = result.proxy;
+            revoke = result.revoke;
+        } else {
+            proxy = new Proxy(proxy, {});
+        }
+    }
+    assert(JSON.stringify(proxy) === JSON.stringify(target));
+
+    revoke();
+    JSON.stringify(target); // Things are ok.
+    let threw = false;
+    try {
+        JSON.stringify(proxy); // Things are not ok.
+    } catch(e) {
+        threw = true;
+        assert(e.toString() === &quot;TypeError: Proxy has already been revoked. No more operations are allowed to be performed on it.&quot;);
+    }
+    assert(threw);
+});
</ins></span></pre>
</div>
</div>

</body>
</html>