Revision: 5919
Author: [email protected]
Date: Mon Dec  6 07:41:07 2010
Log: Improved JSON stringify.
Review URL: http://codereview.chromium.org/5578004
http://code.google.com/p/v8/source/detail?r=5919

Modified:
 /branches/bleeding_edge/src/array.js
 /branches/bleeding_edge/src/json.js
 /branches/bleeding_edge/test/mjsunit/json.js

=======================================
--- /branches/bleeding_edge/src/array.js        Fri Nov 19 01:25:46 2010
+++ /branches/bleeding_edge/src/array.js        Mon Dec  6 07:41:07 2010
@@ -148,6 +148,9 @@
         }
       }
     }
+    elements.length = elements_length;
+    var result = %_FastAsciiArrayJoin(elements, "");
+    if (!IS_UNDEFINED(result)) return result;
     return %StringBuilderConcat(elements, elements_length, '');
   } finally {
     // Make sure to pop the visited array no matter what happens.
@@ -366,7 +369,7 @@
   }

   var result = %_FastAsciiArrayJoin(this, separator);
-  if (typeof result != "undefined") return result;
+  if (!IS_UNDEFINED(result)) return result;

   var length = TO_UINT32(this.length);
   return Join(this, length, separator, ConvertToString);
=======================================
--- /branches/bleeding_edge/src/json.js Mon Dec  6 03:44:16 2010
+++ /branches/bleeding_edge/src/json.js Mon Dec  6 07:41:07 2010
@@ -205,7 +205,7 @@
   var len = value.length;
   for (var i = 0; i < len; i++) {
     var before = builder.length;
-    BasicJSONSerialize($String(i), value, stack, builder);
+    BasicJSONSerialize(i, value, stack, builder);
     if (before == builder.length) builder.push("null");
     builder.push(",");
   }
@@ -226,8 +226,9 @@
   stack.push(value);
   builder.push("{");
   for (var p in value) {
-    if (ObjectHasOwnProperty.call(value, p)) {
-      builder.push(%QuoteJSONString(p), ":");
+    if (%HasLocalProperty(value, p)) {
+      builder.push(%QuoteJSONString(p));
+      builder.push(":");
       var before = builder.length;
       BasicJSONSerialize(p, value, stack, builder);
       if (before == builder.length) {
@@ -251,7 +252,7 @@
   var value = holder[key];
   if (IS_OBJECT(value) && value) {
     var toJSON = value.toJSON;
-    if (IS_FUNCTION(toJSON)) value = toJSON.call(value, key);
+    if (IS_FUNCTION(toJSON)) value = toJSON.call(value, $String(key));
   }
   if (IS_STRING(value)) {
     builder.push(%QuoteJSONString(value));
=======================================
--- /branches/bleeding_edge/test/mjsunit/json.js        Thu Sep  2 00:09:28 2010
+++ /branches/bleeding_edge/test/mjsunit/json.js        Mon Dec  6 07:41:07 2010
@@ -278,6 +278,12 @@
              JSON.stringify({a:"b",c:"d"}, null, 1));
 assertEquals('{"y":6,"x":5}', JSON.stringify({x:5,y:6}, ['y', 'x']));

+// toJSON get string keys.
+var checker = {};
+var array = [checker];
+checker.toJSON = function(key) { return 1 + key; };
+assertEquals('["10"]', JSON.stringify(array));
+
 // The gap is capped at ten characters if specified as string.
 assertEquals('{\n          "a": "b",\n          "c": "d"\n}',
               JSON.stringify({a:"b",c:"d"}, null,

--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to