| OLD | NEW |
| 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 Loading... |
| 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 Loading... |
| 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 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 | 372 |
| 376 var funcJSON = function() { /* Is callable */ }; | 373 var funcJSON = function() { /* Is callable */ }; |
| 377 funcJSON.toJSON = function() { return "has toJSON"; }; | 374 funcJSON.toJSON = function() { return "has toJSON"; }; |
| 378 | 375 |
| 379 var re = /Is callable/; | 376 var re = /Is callable/; |
| 380 | 377 |
| 381 var reJSON = /Is callable/; | 378 var reJSON = /Is callable/; |
| 382 reJSON.toJSON = function() { return "has toJSON"; }; | 379 reJSON.toJSON = function() { return "has toJSON"; }; |
| 383 | 380 |
| 384 assertEquals( | 381 assertEquals( |
| 385 '[37,null,1,"foo","37","true",null,"has toJSON",null,"has toJSON"]', | 382 '[37,null,1,"foo","37","true",null,"has toJSON",{},"has toJSON"]', |
| 386 JSON.stringify([num37, numFoo, numTrue, | 383 JSON.stringify([num37, numFoo, numTrue, |
| 387 strFoo, str37, strTrue, | 384 strFoo, str37, strTrue, |
| 388 func, funcJSON, re, reJSON])); | 385 func, funcJSON, re, reJSON])); |
| 389 | 386 |
| 390 | 387 |
| 391 var oddball = Object(42); | 388 var oddball = Object(42); |
| 392 oddball.__proto__ = { __proto__: null, toString: function() { return true; } }; | 389 oddball.__proto__ = { __proto__: null, toString: function() { return true; } }; |
| 393 assertEquals('1', JSON.stringify(oddball)); | 390 assertEquals('1', JSON.stringify(oddball)); |
| 394 | 391 |
| 395 var getCount = 0; | 392 var getCount = 0; |
| 396 var callCount = 0; | 393 var callCount = 0; |
| 397 var counter = { get toJSON() { getCount++; | 394 var counter = { get toJSON() { getCount++; |
| 398 return function() { callCount++; | 395 return function() { callCount++; |
| 399 return 42; }; } }; | 396 return 42; }; } }; |
| 397 |
| 398 // RegExps are not callable, so they are stringified as objects. |
| 399 assertEquals('{}', JSON.stringify(/regexp/)); |
| 400 assertEquals('42', JSON.stringify(counter)); | 400 assertEquals('42', JSON.stringify(counter)); |
| 401 assertEquals(1, getCount); | 401 assertEquals(1, getCount); |
| 402 assertEquals(1, callCount); | 402 assertEquals(1, callCount); |
| 403 | 403 |
| 404 var oddball2 = Object(42); | 404 var oddball2 = Object(42); |
| 405 var oddball3 = Object("foo"); | 405 var oddball3 = Object("foo"); |
| 406 oddball3.__proto__ = { __proto__: null, | 406 oddball3.__proto__ = { __proto__: null, |
| 407 toString: "not callable", | 407 toString: "not callable", |
| 408 valueOf: function() { return true; } }; | 408 valueOf: function() { return true; } }; |
| 409 oddball2.__proto__ = { __proto__: null, | 409 oddball2.__proto__ = { __proto__: null, |
| 410 toJSON: function () { return oddball3; } } | 410 toJSON: function () { return oddball3; } } |
| 411 assertEquals('"true"', JSON.stringify(oddball2)); | 411 assertEquals('"true"', JSON.stringify(oddball2)); |
| 412 | 412 |
| 413 | 413 |
| 414 var falseNum = Object("37"); | 414 var falseNum = Object("37"); |
| 415 falseNum.__proto__ = Number.prototype; | 415 falseNum.__proto__ = Number.prototype; |
| 416 falseNum.toString = function() { return 42; }; | 416 falseNum.toString = function() { return 42; }; |
| 417 assertEquals('"42"', JSON.stringify(falseNum)); | 417 assertEquals('"42"', JSON.stringify(falseNum)); |
| 418 | 418 |
| 419 // We don't currently allow plain properties called __proto__ in JSON | 419 // 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 | 420 // 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. | 421 // literals. If we change that, this test should change with it. |
| 422 // | 422 // |
| 423 // Parse a non-object value as __proto__. This must not create a | 423 // Parse a non-object value as __proto__. This must not create a |
| 424 // __proto__ property different from the original, and should not | 424 // __proto__ property different from the original, and should not |
| 425 // change the original. | 425 // change the original. |
| 426 var o = JSON.parse('{"__proto__":5}'); | 426 var o = JSON.parse('{"__proto__":5}'); |
| 427 assertEquals(Object.prototype, o.__proto__); // __proto__ isn't changed. | 427 assertEquals(Object.prototype, o.__proto__); // __proto__ isn't changed. |
| 428 assertEquals(0, Object.keys(o).length); // __proto__ isn't added as enumerable. | 428 assertEquals(0, Object.keys(o).length); // __proto__ isn't added as enumerable. |
| 429 | 429 |
| 430 | 430 |
| 431 | 431 |
| OLD | NEW |