OLD | NEW |
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 17 matching lines...) Expand all Loading... |
28 // Flags: --allow-natives-syntax --expose-externalize-string | 28 // Flags: --allow-natives-syntax --expose-externalize-string |
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 assertTrue(JSON.stringify(this, null, 0).indexOf('"a":12345') > 0); |
34 | 34 |
35 // Test JSON.stringify of array in dictionary mode. | 35 // Test JSON.stringify of array in dictionary mode. |
36 function TestStringify(expected, input) { | 36 function TestStringify(expected, input) { |
37 assertEquals(expected, JSON.stringify(input)); | 37 assertEquals(expected, JSON.stringify(input)); |
38 assertEquals(expected, JSON.stringify(input, null, 0)); | 38 assertEquals(expected, JSON.stringify(input, (key, value) => value)); |
| 39 assertEquals(JSON.stringify(input, null, "="), |
| 40 JSON.stringify(input, (key, value) => value, "=")); |
39 } | 41 } |
40 | 42 |
41 var array_1 = []; | 43 var array_1 = []; |
42 var array_2 = []; | 44 var array_2 = []; |
43 array_1[1<<17] = 1; | 45 array_1[1<<17] = 1; |
44 array_2[1<<17] = function() { return 1; }; | 46 array_2[1<<17] = function() { return 1; }; |
45 var nulls = "null,"; | 47 var nulls = "null,"; |
46 for (var i = 0; i < 17; i++) { | 48 for (var i = 0; i < 17; i++) { |
47 nulls += nulls; | 49 nulls += nulls; |
48 } | 50 } |
(...skipping 20 matching lines...) Expand all Loading... |
69 // Note that toString function is not evaluated here! | 71 // Note that toString function is not evaluated here! |
70 TestStringify('false', bool_wrapper); | 72 TestStringify('false', bool_wrapper); |
71 | 73 |
72 // Test getters. | 74 // Test getters. |
73 var counter = 0; | 75 var counter = 0; |
74 var getter_obj = { get getter() { | 76 var getter_obj = { get getter() { |
75 counter++; | 77 counter++; |
76 return 123; | 78 return 123; |
77 } }; | 79 } }; |
78 TestStringify('{"getter":123}', getter_obj); | 80 TestStringify('{"getter":123}', getter_obj); |
79 assertEquals(2, counter); | 81 assertEquals(4, counter); |
80 | 82 |
81 // Test toJSON function. | 83 // Test toJSON function. |
82 var tojson_obj = { toJSON: function() { | 84 var tojson_obj = { toJSON: function() { |
83 counter++; | 85 counter++; |
84 return [1, 2]; | 86 return [1, 2]; |
85 }, | 87 }, |
86 a: 1}; | 88 a: 1}; |
87 TestStringify('[1,2]', tojson_obj); | 89 TestStringify('[1,2]', tojson_obj); |
88 assertEquals(4, counter); | 90 assertEquals(8, counter); |
89 | 91 |
90 // Test that we don't recursively look for the toJSON function. | 92 // Test that we don't recursively look for the toJSON function. |
91 var tojson_proto_obj = { a: 'fail' }; | 93 var tojson_proto_obj = { a: 'fail' }; |
92 tojson_proto_obj.__proto__ = { toJSON: function() { | 94 tojson_proto_obj.__proto__ = { toJSON: function() { |
93 counter++; | 95 counter++; |
94 return tojson_obj; | 96 return tojson_obj; |
95 } }; | 97 } }; |
96 TestStringify('{"a":1}', tojson_proto_obj); | 98 TestStringify('{"a":1}', tojson_proto_obj); |
97 | 99 |
98 // Test toJSON produced by a getter. | 100 // Test toJSON produced by a getter. |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 var str = "external"; | 183 var str = "external"; |
182 try { | 184 try { |
183 externalizeString(str, true); | 185 externalizeString(str, true); |
184 } catch (e) { } | 186 } catch (e) { } |
185 TestStringify("\"external\"", str, null, 0); | 187 TestStringify("\"external\"", str, null, 0); |
186 | 188 |
187 var o = {}; | 189 var o = {}; |
188 o.somespecialproperty = 10; | 190 o.somespecialproperty = 10; |
189 o["\x19"] = 10; | 191 o["\x19"] = 10; |
190 assertThrows("JSON.parse('{\"somespecialproperty\":100, \"\x19\":10}')"); | 192 assertThrows("JSON.parse('{\"somespecialproperty\":100, \"\x19\":10}')"); |
OLD | NEW |