Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: test/mjsunit/json2.js

Issue 11315009: Handle edge cases in basic JSON.stringify. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: rebased Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/json-stringifier.h ('k') | test/mjsunit/regress/regress-json-stringify-gc.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution. 11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its 12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived 13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission. 14 // from this software without specific prior written permission.
15 // 15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 // Flags: --allow-natives-syntax 28 // Flags: --allow-natives-syntax
29 29
30 // Test JSON.stringify on the global object.
31 var a = 12345;
32 assertTrue(JSON.stringify(this).indexOf('"a":12345') > 0);
33
34 // Test JSON.stringify of array in dictionary mode.
35 var array_1 = [];
36 var array_2 = [];
37 array_1[100000] = 1;
38 array_2[100000] = function() { return 1; };
39 var nulls = "";
40 for (var i = 0; i < 100000; i++) {
41 nulls += 'null,';
42 }
43 expected_1 = '[' + nulls + '1]';
44 expected_2 = '[' + nulls + 'null]';
45 assertEquals(expected_1, JSON.stringify(array_1));
46 assertEquals(expected_2, JSON.stringify(array_2));
47
48 // Test JSValue with custom prototype.
49 var num_wrapper = Object(42);
50 num_wrapper.__proto__ = { __proto__: null,
51 toString: function() { return true; } };
52 assertEquals('1', JSON.stringify(num_wrapper));
53
54 var str_wrapper = Object('2');
55 str_wrapper.__proto__ = { __proto__: null,
56 toString: function() { return true; } };
57 assertEquals('"true"', JSON.stringify(str_wrapper));
58
59 var bool_wrapper = Object(false);
60 bool_wrapper.__proto__ = { __proto__: null,
61 toString: function() { return true; } };
62 // Note that toString function is not evaluated here!
63 assertEquals('false', JSON.stringify(bool_wrapper));
64
65 // Test getters.
66 var counter = 0;
67 var getter_obj = { get getter() {
68 counter++;
69 return 123;
70 } };
71 assertEquals('{"getter":123}', JSON.stringify(getter_obj));
72 assertEquals(1, counter);
73
74 // Test toJSON function.
75 var tojson_obj = { toJSON: function() {
76 counter++;
77 return [1, 2];
78 },
79 a: 1};
80 assertEquals('[1,2]', JSON.stringify(tojson_obj));
81 assertEquals(2, counter);
82
83 // Test that we don't recursively look for the toJSON function.
84 var tojson_proto_obj = { a: 'fail' };
85 tojson_proto_obj.__proto__ = { toJSON: function() {
86 counter++;
87 return tojson_obj;
88 } };
89 assertEquals('{"a":1}', JSON.stringify(tojson_proto_obj));
90
91 // Test toJSON produced by a getter.
92 var tojson_via_getter = { get toJSON() {
93 return function(x) {
94 counter++;
95 return 321;
96 };
97 },
98 a: 1 };
99 assertEquals('321', JSON.stringify(tojson_via_getter));
100
101 // Test toJSON with key.
102 tojson_obj = { toJSON: function(key) { return key + key; } };
103 var tojson_with_key_1 = { a: tojson_obj, b: tojson_obj };
104 assertEquals('{"a":"aa","b":"bb"}', JSON.stringify(tojson_with_key_1));
105 var tojson_with_key_2 = [ tojson_obj, tojson_obj ];
106 assertEquals('["00","11"]', JSON.stringify(tojson_with_key_2));
107
108 // Test toJSON with exception.
109 var tojson_ex = { toJSON: function(key) { throw "123" } };
110 assertThrows(function() { JSON.stringify(tojson_ex); });
111
112 // Test holes in arrays.
30 var fast_smi = [1, 2, 3, 4]; 113 var fast_smi = [1, 2, 3, 4];
31 fast_smi.__proto__ = [7, 7, 7, 7]; 114 fast_smi.__proto__ = [7, 7, 7, 7];
32 delete fast_smi[2]; 115 delete fast_smi[2];
33 assertTrue(%HasFastSmiElements(fast_smi)); 116 assertTrue(%HasFastSmiElements(fast_smi));
34 assertEquals("[1,2,7,4]", JSON.stringify(fast_smi)); 117 assertEquals("[1,2,7,4]", JSON.stringify(fast_smi));
35 118
36 var fast_double = [1.1, 2, 3, 4]; 119 var fast_double = [1.1, 2, 3, 4];
37 fast_double.__proto__ = [7, 7, 7, 7]; 120 fast_double.__proto__ = [7, 7, 7, 7];
38 121
39 delete fast_double[2]; 122 delete fast_double[2];
40 assertTrue(%HasFastDoubleElements(fast_double)); 123 assertTrue(%HasFastDoubleElements(fast_double));
41 assertEquals("[1.1,2,7,4]", JSON.stringify(fast_double)); 124 assertEquals("[1.1,2,7,4]", JSON.stringify(fast_double));
42 125
43 var fast_obj = [1, 2, {}, {}]; 126 var fast_obj = [1, 2, {}, {}];
44 fast_obj.__proto__ = [7, 7, 7, 7]; 127 fast_obj.__proto__ = [7, 7, 7, 7];
45 128
46 delete fast_obj[2]; 129 delete fast_obj[2];
47 assertTrue(%HasFastObjectElements(fast_obj)); 130 assertTrue(%HasFastObjectElements(fast_obj));
48 assertEquals("[1,2,7,{}]", JSON.stringify(fast_obj)); 131 assertEquals("[1,2,7,{}]", JSON.stringify(fast_obj));
OLDNEW
« no previous file with comments | « src/json-stringifier.h ('k') | test/mjsunit/regress/regress-json-stringify-gc.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698