Reviewers: Toon Verwaest,

Description:
Fix spec violations in JSON.stringify wrt replacer array.


[email protected]
BUG=v8:3135
LOG=Y

Please review this at https://codereview.chromium.org/146623009/
Index: src/json.js
diff --git a/src/json.js b/src/json.js
index c21e6351d45a345ca887ef1ed3ac3a7b811f3fbf..3ef14e8967807123e8df985e2e70d763ee2897fe 100644
--- a/src/json.js
+++ b/src/json.js
@@ -210,6 +210,21 @@ function JSONStringify(value, replacer, space) {
   } else {
     gap = "";
   }
+  if (IS_ARRAY(replacer)) {
+    // Deduplicate replacer array items.
+    var property_list = new InternalArray();
+    var length = replacer.length;
+    // This simple algorithm is O(n^2).
+    // Replace with something better if this becomes a bottleneck.
+    for (var i = 0; i < length; i++) {
+      var item = replacer[i];
+      if (IS_NUMBER(item)) item = %_NumberToString(item);
+      if (IS_STRING(item) && property_list.indexOf(item) < 0) {
+        property_list.push(item);
+      }
+    }
+    replacer = property_list;
+  }
return JSONSerialize('', {'': value}, replacer, new InternalArray(), "", gap);
 }

Index: test/mjsunit/regress-3135.js
diff --git a/test/mjsunit/regress-3135.js b/test/mjsunit/regress-3135.js
new file mode 100644
index 0000000000000000000000000000000000000000..4f8f3868fd80308b111c23343dc52f886597a4bb
--- /dev/null
+++ b/test/mjsunit/regress-3135.js
@@ -0,0 +1,44 @@
+// Copyright 2014 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Properties are serialized once.
+assertEquals('{"x":1}', JSON.stringify({ x : 1 }, ["x", 1, "x", 1]));
+assertEquals('{"1":1}', JSON.stringify({ 1 : 1 }, ["x", 1, "x", 1]));
+
+// Properties are visited at most once.
+var fired = 0;
+var getter_obj = { get x() { fired++; return 2; } };
+assertEquals('{"x":2}', JSON.stringify(getter_obj, ["x", "y", "x"]));
+assertEquals(1, fired);
+
+// Order of the replacer array is followed.
+assertEquals('{"y":4,"x":3}', JSON.stringify({ x : 3, y : 4}, ["y", "x"]));
+
+// Arrays are not affected by the replacer array.
+assertEquals("[9,8,7]", JSON.stringify([9, 8, 7], [1, 1]));
+var mixed_arr = [11,12,13];
+mixed_arr.x = 10;
+assertEquals('[11,12,13]', JSON.stringify(mixed_arr, [1, 0, 1]));
+
+// Array elements of objects are affected.
+var mixed_obj = { x : 3 };
+mixed_obj[0] = 6;
+mixed_obj[1] = 5;
+assertEquals('{"1":5,"0":6}', JSON.stringify(mixed_obj, [1, 0, 1]));
+
+// Nested object.
+assertEquals('{"z":{"x":3},"x":1}',
+             JSON.stringify({ x: 1, y:2, z: {x:3, b:4}}, ["z","x"]));
+
+// Objects in the replacer array are ignored.
+assertEquals('{}',
+             JSON.stringify({ x : 1, "1": 1 }, [{}]));
+assertEquals('{}',
+             JSON.stringify({ x : 1, "1": 1 }, [true, undefined, null]));
+assertEquals('{}',
+             JSON.stringify({ x : 1, "1": 1 },
+                            [{ toString: function() { return "x";} }]));
+assertEquals('{}',
+             JSON.stringify({ x : 1, "1": 1 },
+                            [{ valueOf: function() { return 1;} }]));


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to