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

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

Issue 7348008: Merge up to 8597 to experimental/gc from the bleeding edge. (Closed) Base URL: http://v8.googlecode.com/svn/branches/experimental/gc/
Patch Set: '' Created 9 years, 5 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/hex-parsing.js ('k') | test/mjsunit/keywords-and-reserved_words.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 2009 the V8 project authors. All rights reserved. 1 // Copyright 2009 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 valueOf: "not callable", 60 valueOf: "not callable",
61 toString: function() { return Infinity; }, 61 toString: function() { return Infinity; },
62 toISOString: function() { return 42; }}; 62 toISOString: function() { return 42; }};
63 63
64 assertEquals(null, d3.toJSON()); 64 assertEquals(null, d3.toJSON());
65 65
66 var d4 = {toJSON: Date.prototype.toJSON, 66 var d4 = {toJSON: Date.prototype.toJSON,
67 valueOf: "not callable", 67 valueOf: "not callable",
68 toString: "not callable either", 68 toString: "not callable either",
69 toISOString: function() { return 42; }}; 69 toISOString: function() { return 42; }};
70 assertThrows("d4.toJSON()", TypeError); // ToPrimitive throws. 70 assertThrows("d4.toJSON()", TypeError); // ToPrimitive throws.
71 71
72 var d5 = {toJSON: Date.prototype.toJSON, 72 var d5 = {toJSON: Date.prototype.toJSON,
73 valueOf: "not callable", 73 valueOf: "not callable",
74 toString: function() { return "Infinity"; }, 74 toString: function() { return "Infinity"; },
75 toISOString: function() { return 42; }}; 75 toISOString: function() { return 42; }};
76 assertEquals(42, d5.toJSON()); 76 assertEquals(42, d5.toJSON());
77 77
78 var d6 = {toJSON: Date.prototype.toJSON, 78 var d6 = {toJSON: Date.prototype.toJSON,
79 toISOString: function() { return ["not primitive"]; }}; 79 toISOString: function() { return ["not primitive"]; }};
80 assertEquals(["not primitive"], d6.toJSON()); 80 assertEquals(["not primitive"], d6.toJSON());
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 TestInvalid('"\\a invalid escape"'); 189 TestInvalid('"\\a invalid escape"');
190 TestInvalid('"\\v invalid escape"'); // Valid JavaScript 190 TestInvalid('"\\v invalid escape"'); // Valid JavaScript
191 TestInvalid('"\\\' invalid escape"'); // Valid JavaScript 191 TestInvalid('"\\\' invalid escape"'); // Valid JavaScript
192 TestInvalid('"\\x42 invalid escape"'); // Valid JavaScript 192 TestInvalid('"\\x42 invalid escape"'); // Valid JavaScript
193 TestInvalid('"\\u202 invalid escape"'); 193 TestInvalid('"\\u202 invalid escape"');
194 TestInvalid('"\\012 invalid escape"'); 194 TestInvalid('"\\012 invalid escape"');
195 TestInvalid('"Unterminated string'); 195 TestInvalid('"Unterminated string');
196 TestInvalid('"Unterminated string\\"'); 196 TestInvalid('"Unterminated string\\"');
197 TestInvalid('"Unterminated string\\\\\\"'); 197 TestInvalid('"Unterminated string\\\\\\"');
198 198
199 // JavaScript RegExp literals not valid in JSON.
200 TestInvalid('/true/');
201
202 // Test bad JSON that would be good JavaScript (ES5). 199 // Test bad JSON that would be good JavaScript (ES5).
203 TestInvalid("{true:42}"); 200 TestInvalid("{true:42}");
204 TestInvalid("{false:42}"); 201 TestInvalid("{false:42}");
205 TestInvalid("{null:42}"); 202 TestInvalid("{null:42}");
206 TestInvalid("{'foo':42}"); 203 TestInvalid("{'foo':42}");
207 TestInvalid("{42:42}"); 204 TestInvalid("{42:42}");
208 TestInvalid("{0:42}"); 205 TestInvalid("{0:42}");
209 TestInvalid("{-1:42}"); 206 TestInvalid("{-1:42}");
210 207
211 // Test for trailing garbage detection. 208 // Test for trailing garbage detection.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 assertEquals("[\n^1,\n^2,\n^3\n]", JSON.stringify([1, 2, 3], null, "^")); 249 assertEquals("[\n^1,\n^2,\n^3\n]", JSON.stringify([1, 2, 3], null, "^"));
253 assertEquals("[\n^1,\n^2,\n^3\n]", 250 assertEquals("[\n^1,\n^2,\n^3\n]",
254 JSON.stringify([1, 2, 3], null, new String("^"))); 251 JSON.stringify([1, 2, 3], null, new String("^")));
255 assertEquals("[\n 1,\n 2,\n [\n 3,\n [\n 4\n ],\n 5\n ],\n 6,\n 7\n]", 252 assertEquals("[\n 1,\n 2,\n [\n 3,\n [\n 4\n ],\n 5\n ],\n 6,\n 7\n]",
256 JSON.stringify([1, 2, [3, [4], 5], 6, 7], null, 1)); 253 JSON.stringify([1, 2, [3, [4], 5], 6, 7], null, 1));
257 assertEquals("[]", JSON.stringify([], null, 1)); 254 assertEquals("[]", JSON.stringify([], null, 1));
258 assertEquals("[1,2,[3,[4],5],6,7]", 255 assertEquals("[1,2,[3,[4],5],6,7]",
259 JSON.stringify([1, 2, [3, [4], 5], 6, 7], null)); 256 JSON.stringify([1, 2, [3, [4], 5], 6, 7], null));
260 assertEquals("[2,4,[6,[8],10],12,14]", 257 assertEquals("[2,4,[6,[8],10],12,14]",
261 JSON.stringify([1, 2, [3, [4], 5], 6, 7], DoubleNumbers)); 258 JSON.stringify([1, 2, [3, [4], 5], 6, 7], DoubleNumbers));
259 assertEquals('["a","ab","abc"]', JSON.stringify(["a","ab","abc"]));
262 260
263 var circular = [1, 2, 3]; 261 var circular = [1, 2, 3];
264 circular[2] = circular; 262 circular[2] = circular;
265 assertThrows(function () { JSON.stringify(circular); }, TypeError); 263 assertThrows(function () { JSON.stringify(circular); }, TypeError);
266 264
267 var singleton = []; 265 var singleton = [];
268 var multiOccurrence = [singleton, singleton, singleton]; 266 var multiOccurrence = [singleton, singleton, singleton];
269 assertEquals("[[],[],[]]", JSON.stringify(multiOccurrence)); 267 assertEquals("[[],[],[]]", JSON.stringify(multiOccurrence));
270 268
271 assertEquals('{"x":5,"y":6}', JSON.stringify({x:5,y:6})); 269 assertEquals('{"x":5,"y":6}', JSON.stringify({x:5,y:6}));
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 373
376 var funcJSON = function() { /* Is callable */ }; 374 var funcJSON = function() { /* Is callable */ };
377 funcJSON.toJSON = function() { return "has toJSON"; }; 375 funcJSON.toJSON = function() { return "has toJSON"; };
378 376
379 var re = /Is callable/; 377 var re = /Is callable/;
380 378
381 var reJSON = /Is callable/; 379 var reJSON = /Is callable/;
382 reJSON.toJSON = function() { return "has toJSON"; }; 380 reJSON.toJSON = function() { return "has toJSON"; };
383 381
384 assertEquals( 382 assertEquals(
385 '[37,null,1,"foo","37","true",null,"has toJSON",null,"has toJSON"]', 383 '[37,null,1,"foo","37","true",null,"has toJSON",{},"has toJSON"]',
386 JSON.stringify([num37, numFoo, numTrue, 384 JSON.stringify([num37, numFoo, numTrue,
387 strFoo, str37, strTrue, 385 strFoo, str37, strTrue,
388 func, funcJSON, re, reJSON])); 386 func, funcJSON, re, reJSON]));
389 387
390 388
391 var oddball = Object(42); 389 var oddball = Object(42);
392 oddball.__proto__ = { __proto__: null, toString: function() { return true; } }; 390 oddball.__proto__ = { __proto__: null, toString: function() { return true; } };
393 assertEquals('1', JSON.stringify(oddball)); 391 assertEquals('1', JSON.stringify(oddball));
394 392
395 var getCount = 0; 393 var getCount = 0;
396 var callCount = 0; 394 var callCount = 0;
397 var counter = { get toJSON() { getCount++; 395 var counter = { get toJSON() { getCount++;
398 return function() { callCount++; 396 return function() { callCount++;
399 return 42; }; } }; 397 return 42; }; } };
398
399 // RegExps are not callable, so they are stringified as objects.
400 assertEquals('{}', JSON.stringify(/regexp/));
400 assertEquals('42', JSON.stringify(counter)); 401 assertEquals('42', JSON.stringify(counter));
401 assertEquals(1, getCount); 402 assertEquals(1, getCount);
402 assertEquals(1, callCount); 403 assertEquals(1, callCount);
403 404
404 var oddball2 = Object(42); 405 var oddball2 = Object(42);
405 var oddball3 = Object("foo"); 406 var oddball3 = Object("foo");
406 oddball3.__proto__ = { __proto__: null, 407 oddball3.__proto__ = { __proto__: null,
407 toString: "not callable", 408 toString: "not callable",
408 valueOf: function() { return true; } }; 409 valueOf: function() { return true; } };
409 oddball2.__proto__ = { __proto__: null, 410 oddball2.__proto__ = { __proto__: null,
410 toJSON: function () { return oddball3; } } 411 toJSON: function () { return oddball3; } }
411 assertEquals('"true"', JSON.stringify(oddball2)); 412 assertEquals('"true"', JSON.stringify(oddball2));
412 413
413 414
414 var falseNum = Object("37"); 415 var falseNum = Object("37");
415 falseNum.__proto__ = Number.prototype; 416 falseNum.__proto__ = Number.prototype;
416 falseNum.toString = function() { return 42; }; 417 falseNum.toString = function() { return 42; };
417 assertEquals('"42"', JSON.stringify(falseNum)); 418 assertEquals('"42"', JSON.stringify(falseNum));
418 419
419 // We don't currently allow plain properties called __proto__ in JSON 420 // We don't currently allow plain properties called __proto__ in JSON
420 // objects in JSON.parse. Instead we read them as we would JS object 421 // objects in JSON.parse. Instead we read them as we would JS object
421 // literals. If we change that, this test should change with it. 422 // literals. If we change that, this test should change with it.
422 // 423 //
423 // Parse a non-object value as __proto__. This must not create a 424 // Parse a non-object value as __proto__. This must not create a
424 // __proto__ property different from the original, and should not 425 // __proto__ property different from the original, and should not
425 // change the original. 426 // change the original.
426 var o = JSON.parse('{"__proto__":5}'); 427 var o = JSON.parse('{"__proto__":5}');
427 assertEquals(Object.prototype, o.__proto__); // __proto__ isn't changed. 428 assertEquals(Object.prototype, o.__proto__); // __proto__ isn't changed.
428 assertEquals(0, Object.keys(o).length); // __proto__ isn't added as enumerable. 429 assertEquals(0, Object.keys(o).length); // __proto__ isn't added as enumerable.
429 430
430 431
431 432
OLDNEW
« no previous file with comments | « test/mjsunit/hex-parsing.js ('k') | test/mjsunit/keywords-and-reserved_words.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698