blob: 75e25f89242414ebc976cb768d750b7b604d53e1 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
27
28// Flags: --allow-natives-syntax --expose-externalize-string
29
30// Test JSON.stringify on the global object.
31var a = 12345;
32assertTrue(JSON.stringify(this).indexOf('"a":12345') > 0);
33assertTrue(JSON.stringify(this, null, 0).indexOf('"a":12345') > 0);
34
35// Test JSON.stringify of array in dictionary mode.
36function TestStringify(expected, input) {
37 assertEquals(expected, JSON.stringify(input));
Ben Murdoch61f157c2016-09-16 13:49:30 +010038 assertEquals(expected, JSON.stringify(input, (key, value) => value));
39 assertEquals(JSON.stringify(input, null, "="),
40 JSON.stringify(input, (key, value) => value, "="));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041}
42
43var array_1 = [];
44var array_2 = [];
45array_1[1<<17] = 1;
46array_2[1<<17] = function() { return 1; };
47var nulls = "null,";
48for (var i = 0; i < 17; i++) {
49 nulls += nulls;
50}
51
52expected_1 = '[' + nulls + '1]';
53expected_2 = '[' + nulls + 'null]';
54TestStringify(expected_1, array_1);
55TestStringify(expected_2, array_2);
56
57// Test JSValue with custom prototype.
58var num_wrapper = Object(42);
59num_wrapper.__proto__ = { __proto__: null,
60 toString: function() { return true; } };
61TestStringify('1', num_wrapper);
62
63var str_wrapper = Object('2');
64str_wrapper.__proto__ = { __proto__: null,
65 toString: function() { return true; } };
66TestStringify('"true"', str_wrapper);
67
68var bool_wrapper = Object(false);
69bool_wrapper.__proto__ = { __proto__: null,
70 toString: function() { return true; } };
71// Note that toString function is not evaluated here!
72TestStringify('false', bool_wrapper);
73
74// Test getters.
75var counter = 0;
76var getter_obj = { get getter() {
77 counter++;
78 return 123;
79 } };
80TestStringify('{"getter":123}', getter_obj);
Ben Murdoch61f157c2016-09-16 13:49:30 +010081assertEquals(4, counter);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082
83// Test toJSON function.
84var tojson_obj = { toJSON: function() {
85 counter++;
86 return [1, 2];
87 },
88 a: 1};
89TestStringify('[1,2]', tojson_obj);
Ben Murdoch61f157c2016-09-16 13:49:30 +010090assertEquals(8, counter);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000091
92// Test that we don't recursively look for the toJSON function.
93var tojson_proto_obj = { a: 'fail' };
94tojson_proto_obj.__proto__ = { toJSON: function() {
95 counter++;
96 return tojson_obj;
97 } };
98TestStringify('{"a":1}', tojson_proto_obj);
99
100// Test toJSON produced by a getter.
101var tojson_via_getter = { get toJSON() {
102 return function(x) {
103 counter++;
104 return 321;
105 };
106 },
107 a: 1 };
108TestStringify('321', tojson_via_getter);
109
110assertThrows(function() {
111 JSON.stringify({ get toJSON() { throw "error"; } });
112});
113
114// Test toJSON with key.
115tojson_obj = { toJSON: function(key) { return key + key; } };
116var tojson_with_key_1 = { a: tojson_obj, b: tojson_obj };
117TestStringify('{"a":"aa","b":"bb"}', tojson_with_key_1);
118var tojson_with_key_2 = [ tojson_obj, tojson_obj ];
119TestStringify('["00","11"]', tojson_with_key_2);
120
121// Test toJSON with exception.
122var tojson_ex = { toJSON: function(key) { throw "123" } };
123assertThrows(function() { JSON.stringify(tojson_ex); });
124assertThrows(function() { JSON.stringify(tojson_ex, null, 0); });
125
126// Test toJSON with access to this.
127var obj = { toJSON: function(key) { return this.a + key; }, a: "x" };
128TestStringify('{"y":"xy"}', {y: obj});
129
130// Test holes in arrays.
131var fast_smi = [1, 2, 3, 4];
132fast_smi.__proto__ = [7, 7, 7, 7];
133delete fast_smi[2];
134assertTrue(%HasFastSmiElements(fast_smi));
135TestStringify("[1,2,7,4]", fast_smi);
136
137var fast_double = [1.1, 2, 3, 4];
138fast_double.__proto__ = [7, 7, 7, 7];
139
140delete fast_double[2];
141assertTrue(%HasFastDoubleElements(fast_double));
142TestStringify("[1.1,2,7,4]", fast_double);
143
144var fast_obj = [1, 2, {}, {}];
145fast_obj.__proto__ = [7, 7, 7, 7];
146
147delete fast_obj[2];
148assertTrue(%HasFastObjectElements(fast_obj));
149TestStringify("[1,2,7,{}]", fast_obj);
150
151var getter_side_effect = { a: 1,
152 get b() {
153 delete this.a;
154 delete this.c;
155 this.e = 5;
156 return 2;
157 },
158 c: 3,
159 d: 4 };
160assertEquals('{"a":1,"b":2,"d":4}', JSON.stringify(getter_side_effect));
161assertEquals('{"b":2,"d":4,"e":5}', JSON.stringify(getter_side_effect));
162
163getter_side_effect = { a: 1,
164 get b() {
165 delete this.a;
166 delete this.c;
167 this.e = 5;
168 return 2;
169 },
170 c: 3,
171 d: 4 };
172assertEquals('{"a":1,"b":2,"d":4}',
173 JSON.stringify(getter_side_effect, null, 0));
174assertEquals('{"b":2,"d":4,"e":5}',
175 JSON.stringify(getter_side_effect, null, 0));
176
177var non_enum = {};
178non_enum.a = 1;
179Object.defineProperty(non_enum, "b", { value: 2, enumerable: false });
180non_enum.c = 3;
181TestStringify('{"a":1,"c":3}', non_enum);
182
183var str = "external";
184try {
185 externalizeString(str, true);
186} catch (e) { }
187TestStringify("\"external\"", str, null, 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000188
189var o = {};
190o.somespecialproperty = 10;
191o["\x19"] = 10;
192assertThrows("JSON.parse('{\"somespecialproperty\":100, \"\x19\":10}')");