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

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

Issue 12702009: Extend test coverage for JSON.stringify's slow path. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 9 months 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 | « test/mjsunit/json.js ('k') | test/mjsunit/regress/regress-2374.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
(...skipping 12 matching lines...) Expand all
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. 30 // Test JSON.stringify on the global object.
31 var a = 12345; 31 var a = 12345;
32 assertTrue(JSON.stringify(this).indexOf('"a":12345') > 0); 32 assertTrue(JSON.stringify(this).indexOf('"a":12345') > 0);
33 assertTrue(JSON.stringify(this, null, 0).indexOf('"a":12345') > 0);
33 34
34 // Test JSON.stringify of array in dictionary mode. 35 // Test JSON.stringify of array in dictionary mode.
36 function TestStringify(expected, input) {
37 assertEquals(expected, JSON.stringify(input));
38 assertEquals(expected, JSON.stringify(input, null, 0));
39 }
40
35 var array_1 = []; 41 var array_1 = [];
36 var array_2 = []; 42 var array_2 = [];
37 array_1[100000] = 1; 43 array_1[100000] = 1;
38 array_2[100000] = function() { return 1; }; 44 array_2[100000] = function() { return 1; };
39 var nulls = ""; 45 var nulls = "";
40 for (var i = 0; i < 100000; i++) { 46 for (var i = 0; i < 100000; i++) {
41 nulls += 'null,'; 47 nulls += 'null,';
42 } 48 }
43 expected_1 = '[' + nulls + '1]'; 49 expected_1 = '[' + nulls + '1]';
44 expected_2 = '[' + nulls + 'null]'; 50 expected_2 = '[' + nulls + 'null]';
45 assertEquals(expected_1, JSON.stringify(array_1)); 51 TestStringify(expected_1, array_1);
46 assertEquals(expected_2, JSON.stringify(array_2)); 52 TestStringify(expected_2, array_2);
47 53
48 // Test JSValue with custom prototype. 54 // Test JSValue with custom prototype.
49 var num_wrapper = Object(42); 55 var num_wrapper = Object(42);
50 num_wrapper.__proto__ = { __proto__: null, 56 num_wrapper.__proto__ = { __proto__: null,
51 toString: function() { return true; } }; 57 toString: function() { return true; } };
52 assertEquals('1', JSON.stringify(num_wrapper)); 58 TestStringify('1', num_wrapper);
53 59
54 var str_wrapper = Object('2'); 60 var str_wrapper = Object('2');
55 str_wrapper.__proto__ = { __proto__: null, 61 str_wrapper.__proto__ = { __proto__: null,
56 toString: function() { return true; } }; 62 toString: function() { return true; } };
57 assertEquals('"true"', JSON.stringify(str_wrapper)); 63 TestStringify('"true"', str_wrapper);
58 64
59 var bool_wrapper = Object(false); 65 var bool_wrapper = Object(false);
60 bool_wrapper.__proto__ = { __proto__: null, 66 bool_wrapper.__proto__ = { __proto__: null,
61 toString: function() { return true; } }; 67 toString: function() { return true; } };
62 // Note that toString function is not evaluated here! 68 // Note that toString function is not evaluated here!
63 assertEquals('false', JSON.stringify(bool_wrapper)); 69 TestStringify('false', bool_wrapper);
64 70
65 // Test getters. 71 // Test getters.
66 var counter = 0; 72 var counter = 0;
67 var getter_obj = { get getter() { 73 var getter_obj = { get getter() {
68 counter++; 74 counter++;
69 return 123; 75 return 123;
70 } }; 76 } };
71 assertEquals('{"getter":123}', JSON.stringify(getter_obj)); 77 TestStringify('{"getter":123}', getter_obj);
72 assertEquals(1, counter); 78 assertEquals(2, counter);
73 79
74 // Test toJSON function. 80 // Test toJSON function.
75 var tojson_obj = { toJSON: function() { 81 var tojson_obj = { toJSON: function() {
76 counter++; 82 counter++;
77 return [1, 2]; 83 return [1, 2];
78 }, 84 },
79 a: 1}; 85 a: 1};
80 assertEquals('[1,2]', JSON.stringify(tojson_obj)); 86 TestStringify('[1,2]', tojson_obj);
81 assertEquals(2, counter); 87 assertEquals(4, counter);
82 88
83 // Test that we don't recursively look for the toJSON function. 89 // Test that we don't recursively look for the toJSON function.
84 var tojson_proto_obj = { a: 'fail' }; 90 var tojson_proto_obj = { a: 'fail' };
85 tojson_proto_obj.__proto__ = { toJSON: function() { 91 tojson_proto_obj.__proto__ = { toJSON: function() {
86 counter++; 92 counter++;
87 return tojson_obj; 93 return tojson_obj;
88 } }; 94 } };
89 assertEquals('{"a":1}', JSON.stringify(tojson_proto_obj)); 95 TestStringify('{"a":1}', tojson_proto_obj);
90 96
91 // Test toJSON produced by a getter. 97 // Test toJSON produced by a getter.
92 var tojson_via_getter = { get toJSON() { 98 var tojson_via_getter = { get toJSON() {
93 return function(x) { 99 return function(x) {
94 counter++; 100 counter++;
95 return 321; 101 return 321;
96 }; 102 };
97 }, 103 },
98 a: 1 }; 104 a: 1 };
99 assertEquals('321', JSON.stringify(tojson_via_getter)); 105 TestStringify('321', tojson_via_getter);
100 106
101 // Test toJSON with key. 107 // Test toJSON with key.
102 tojson_obj = { toJSON: function(key) { return key + key; } }; 108 tojson_obj = { toJSON: function(key) { return key + key; } };
103 var tojson_with_key_1 = { a: tojson_obj, b: tojson_obj }; 109 var tojson_with_key_1 = { a: tojson_obj, b: tojson_obj };
104 assertEquals('{"a":"aa","b":"bb"}', JSON.stringify(tojson_with_key_1)); 110 TestStringify('{"a":"aa","b":"bb"}', tojson_with_key_1);
105 var tojson_with_key_2 = [ tojson_obj, tojson_obj ]; 111 var tojson_with_key_2 = [ tojson_obj, tojson_obj ];
106 assertEquals('["00","11"]', JSON.stringify(tojson_with_key_2)); 112 TestStringify('["00","11"]', tojson_with_key_2);
107 113
108 // Test toJSON with exception. 114 // Test toJSON with exception.
109 var tojson_ex = { toJSON: function(key) { throw "123" } }; 115 var tojson_ex = { toJSON: function(key) { throw "123" } };
110 assertThrows(function() { JSON.stringify(tojson_ex); }); 116 assertThrows(function() { JSON.stringify(tojson_ex); });
117 assertThrows(function() { JSON.stringify(tojson_ex, null, 0); });
111 118
112 // Test toJSON with access to this. 119 // Test toJSON with access to this.
113 var obj = { toJSON: function(key) { return this.a + key; }, a: "x" }; 120 var obj = { toJSON: function(key) { return this.a + key; }, a: "x" };
114 assertEquals('{"y":"xy"}', JSON.stringify({y: obj})); 121 TestStringify('{"y":"xy"}', {y: obj});
115 122
116 // Test holes in arrays. 123 // Test holes in arrays.
117 var fast_smi = [1, 2, 3, 4]; 124 var fast_smi = [1, 2, 3, 4];
118 fast_smi.__proto__ = [7, 7, 7, 7]; 125 fast_smi.__proto__ = [7, 7, 7, 7];
119 delete fast_smi[2]; 126 delete fast_smi[2];
120 assertTrue(%HasFastSmiElements(fast_smi)); 127 assertTrue(%HasFastSmiElements(fast_smi));
121 assertEquals("[1,2,7,4]", JSON.stringify(fast_smi)); 128 TestStringify("[1,2,7,4]", fast_smi);
122 129
123 var fast_double = [1.1, 2, 3, 4]; 130 var fast_double = [1.1, 2, 3, 4];
124 fast_double.__proto__ = [7, 7, 7, 7]; 131 fast_double.__proto__ = [7, 7, 7, 7];
125 132
126 delete fast_double[2]; 133 delete fast_double[2];
127 assertTrue(%HasFastDoubleElements(fast_double)); 134 assertTrue(%HasFastDoubleElements(fast_double));
128 assertEquals("[1.1,2,7,4]", JSON.stringify(fast_double)); 135 TestStringify("[1.1,2,7,4]", fast_double);
129 136
130 var fast_obj = [1, 2, {}, {}]; 137 var fast_obj = [1, 2, {}, {}];
131 fast_obj.__proto__ = [7, 7, 7, 7]; 138 fast_obj.__proto__ = [7, 7, 7, 7];
132 139
133 delete fast_obj[2]; 140 delete fast_obj[2];
134 assertTrue(%HasFastObjectElements(fast_obj)); 141 assertTrue(%HasFastObjectElements(fast_obj));
135 assertEquals("[1,2,7,{}]", JSON.stringify(fast_obj)); 142 TestStringify("[1,2,7,{}]", fast_obj);
136 143
137 var getter_side_effect = { a: 1, 144 var getter_side_effect = { a: 1,
138 get b() { 145 get b() {
139 delete this.a; 146 delete this.a;
140 delete this.c; 147 delete this.c;
141 this.e = 5; 148 this.e = 5;
142 return 2; 149 return 2;
143 }, 150 },
144 c: 3, 151 c: 3,
145 d: 4 }; 152 d: 4 };
146 assertEquals('{"a":1,"b":2,"d":4}', JSON.stringify(getter_side_effect)); 153 assertEquals('{"a":1,"b":2,"d":4}', JSON.stringify(getter_side_effect));
147 assertEquals('{"b":2,"d":4,"e":5}', JSON.stringify(getter_side_effect)); 154 assertEquals('{"b":2,"d":4,"e":5}', JSON.stringify(getter_side_effect));
148 155
156 getter_side_effect = { a: 1,
157 get b() {
158 delete this.a;
159 delete this.c;
160 this.e = 5;
161 return 2;
162 },
163 c: 3,
164 d: 4 };
165 assertEquals('{"a":1,"b":2,"d":4}',
166 JSON.stringify(getter_side_effect, null, 0));
167 assertEquals('{"b":2,"d":4,"e":5}',
168 JSON.stringify(getter_side_effect, null, 0));
169
149 var non_enum = {}; 170 var non_enum = {};
150 non_enum.a = 1; 171 non_enum.a = 1;
151 Object.defineProperty(non_enum, "b", { value: 2, enumerable: false }); 172 Object.defineProperty(non_enum, "b", { value: 2, enumerable: false });
152 non_enum.c = 3; 173 non_enum.c = 3;
153 assertEquals('{"a":1,"c":3}', JSON.stringify(non_enum)); 174 TestStringify('{"a":1,"c":3}', non_enum);
OLDNEW
« no previous file with comments | « test/mjsunit/json.js ('k') | test/mjsunit/regress/regress-2374.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698