Chromium Code Reviews| 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 |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 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 // Date toJSON | 28 // Date toJSON |
| 29 assertEquals("1970-01-01T00:00:00.000Z", new Date(0).toJSON()); | 29 assertEquals("1970-01-01T00:00:00.000Z", new Date(0).toJSON()); |
| 30 assertEquals("1979-01-11T08:00:00.000Z", new Date("1979-01-11 08:00 GMT").toJSON ()); | 30 assertEquals("1979-01-11T08:00:00.000Z", new Date("1979-01-11 08:00 GMT").toJSON ()); |
|
Rico
2011/12/08 10:24:37
long line + below
| |
| 31 assertEquals("2005-05-05T05:05:05.000Z", new Date("2005-05-05 05:05:05 GMT").toJ SON()); | 31 assertEquals("2005-05-05T05:05:05.000Z", new Date("2005-05-05 05:05:05 GMT").toJ SON()); |
| 32 var n1 = new Date(10000); | 32 var n1 = new Date(10000); |
| 33 n1.toISOString = function () { return "foo"; }; | 33 n1.toISOString = function () { return "foo"; }; |
| 34 assertEquals("foo", n1.toJSON()); | 34 assertEquals("foo", n1.toJSON()); |
| 35 var n2 = new Date(10001); | 35 var n2 = new Date(10001); |
| 36 n2.toISOString = null; | 36 n2.toISOString = null; |
| 37 assertThrows(function () { n2.toJSON(); }, TypeError); | 37 assertThrows(function () { n2.toJSON(); }, TypeError); |
| 38 var n4 = new Date(10003); | 38 var n4 = new Date(10003); |
| 39 n4.toISOString = function () { | 39 n4.toISOString = function () { |
| 40 assertEquals(0, arguments.length); | 40 assertEquals(0, arguments.length); |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 322 for (var i = 0; i < 65536; i++) { | 322 for (var i = 0; i < 65536; i++) { |
| 323 var string = String.fromCharCode(i); | 323 var string = String.fromCharCode(i); |
| 324 var encoded = JSON.stringify(string); | 324 var encoded = JSON.stringify(string); |
| 325 var expected = "uninitialized"; | 325 var expected = "uninitialized"; |
| 326 // Following the ES5 specification of the abstraction function Quote. | 326 // Following the ES5 specification of the abstraction function Quote. |
| 327 if (string == '"' || string == '\\') { | 327 if (string == '"' || string == '\\') { |
| 328 // Step 2.a | 328 // Step 2.a |
| 329 expected = '\\' + string; | 329 expected = '\\' + string; |
| 330 } else if ("\b\t\n\r\f".indexOf(string) >= 0) { | 330 } else if ("\b\t\n\r\f".indexOf(string) >= 0) { |
| 331 // Step 2.b | 331 // Step 2.b |
| 332 if (string == '\b') expected = '\\b'; | 332 if (string == '\b') { expected = '\\b'; |
| 333 else if (string == '\t') expected = '\\t'; | 333 } |
| 334 else if (string == '\n') expected = '\\n'; | 334 else if (string == '\t') { expected = '\\t'; |
| 335 else if (string == '\f') expected = '\\f'; | 335 } |
| 336 else if (string == '\n') { expected = '\\n'; | |
| 337 } | |
| 338 else if (string == '\f') { expected = '\\f'; | |
| 339 } | |
|
Rico
2011/12/08 10:24:37
above: move body down to individual lines + move e
| |
| 336 else if (string == '\r') expected = '\\r'; | 340 else if (string == '\r') expected = '\\r'; |
|
Rico
2011/12/08 10:24:37
also use {} for last else if and move up on previo
| |
| 337 } else if (i < 32) { | 341 } else if (i < 32) { |
| 338 // Step 2.c | 342 // Step 2.c |
| 339 if (i < 16) { | 343 if (i < 16) { |
| 340 expected = "\\u000" + i.toString(16); | 344 expected = "\\u000" + i.toString(16); |
| 341 } else { | 345 } else { |
| 342 expected = "\\u00" + i.toString(16); | 346 expected = "\\u00" + i.toString(16); |
| 343 } | 347 } |
| 344 } else { | 348 } else { |
| 345 expected = string; | 349 expected = string; |
| 346 } | 350 } |
| 347 assertEquals('"' + expected + '"', encoded, "Codepoint " + i); | 351 assertEquals('"' + expected + '"', encoded, "Codepoint " + i); |
| 348 } | 352 } |
| 349 | 353 |
| 350 | 354 |
| 351 // Ensure that wrappers and callables are handled correctly. | 355 // Ensure that wrappers and callables are handled correctly. |
| 352 var num37 = new Number(42); | 356 var num37 = new Number(42); |
| 353 num37.valueOf = function() { return 37; }; | 357 num37.valueOf = function() { return 37; }; |
| 354 | 358 |
| 355 var numFoo = new Number(42); | 359 var numFoo = new Number(42); |
| 356 numFoo.valueOf = "not callable"; | 360 numFoo.valueOf = "not callable"; |
| 357 numFoo.toString = function() { return "foo"; }; | 361 numFoo.toString = function() { return "foo"; }; |
| 358 | 362 |
| 359 var numTrue = new Number(42); | 363 var numTrue = new Number(42); |
| 360 numTrue.valueOf = function() { return true; } | 364 numTrue.valueOf = function() { return true; }; |
| 361 | 365 |
| 362 var strFoo = new String("bar"); | 366 var strFoo = new String("bar"); |
| 363 strFoo.toString = function() { return "foo"; }; | 367 strFoo.toString = function() { return "foo"; }; |
| 364 | 368 |
| 365 var str37 = new String("bar"); | 369 var str37 = new String("bar"); |
| 366 str37.toString = "not callable"; | 370 str37.toString = "not callable"; |
| 367 str37.valueOf = function() { return 37; }; | 371 str37.valueOf = function() { return 37; }; |
| 368 | 372 |
| 369 var strTrue = new String("bar"); | 373 var strTrue = new String("bar"); |
| 370 strTrue.toString = function() { return true; } | 374 strTrue.toString = function() { return true; }; |
| 371 | 375 |
| 372 var func = function() { /* Is callable */ }; | 376 var func = function() { /* Is callable */ }; |
| 373 | 377 |
| 374 var funcJSON = function() { /* Is callable */ }; | 378 var funcJSON = function() { /* Is callable */ }; |
| 375 funcJSON.toJSON = function() { return "has toJSON"; }; | 379 funcJSON.toJSON = function() { return "has toJSON"; }; |
| 376 | 380 |
| 377 var re = /Is callable/; | 381 var re = /Is callable/; |
| 378 | 382 |
| 379 var reJSON = /Is callable/; | 383 var reJSON = /Is callable/; |
| 380 reJSON.toJSON = function() { return "has toJSON"; }; | 384 reJSON.toJSON = function() { return "has toJSON"; }; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 401 assertEquals('42', JSON.stringify(counter)); | 405 assertEquals('42', JSON.stringify(counter)); |
| 402 assertEquals(1, getCount); | 406 assertEquals(1, getCount); |
| 403 assertEquals(1, callCount); | 407 assertEquals(1, callCount); |
| 404 | 408 |
| 405 var oddball2 = Object(42); | 409 var oddball2 = Object(42); |
| 406 var oddball3 = Object("foo"); | 410 var oddball3 = Object("foo"); |
| 407 oddball3.__proto__ = { __proto__: null, | 411 oddball3.__proto__ = { __proto__: null, |
| 408 toString: "not callable", | 412 toString: "not callable", |
| 409 valueOf: function() { return true; } }; | 413 valueOf: function() { return true; } }; |
| 410 oddball2.__proto__ = { __proto__: null, | 414 oddball2.__proto__ = { __proto__: null, |
| 411 toJSON: function () { return oddball3; } } | 415 toJSON: function () { return oddball3; } }; |
| 412 assertEquals('"true"', JSON.stringify(oddball2)); | 416 assertEquals('"true"', JSON.stringify(oddball2)); |
| 413 | 417 |
| 414 | 418 |
| 415 var falseNum = Object("37"); | 419 var falseNum = Object("37"); |
| 416 falseNum.__proto__ = Number.prototype; | 420 falseNum.__proto__ = Number.prototype; |
| 417 falseNum.toString = function() { return 42; }; | 421 falseNum.toString = function() { return 42; }; |
| 418 assertEquals('"42"', JSON.stringify(falseNum)); | 422 assertEquals('"42"', JSON.stringify(falseNum)); |
| 419 | 423 |
| 420 // We don't currently allow plain properties called __proto__ in JSON | 424 // We don't currently allow plain properties called __proto__ in JSON |
| 421 // objects in JSON.parse. Instead we read them as we would JS object | 425 // objects in JSON.parse. Instead we read them as we would JS object |
| 422 // literals. If we change that, this test should change with it. | 426 // literals. If we change that, this test should change with it. |
| 423 // | 427 // |
| 424 // Parse a non-object value as __proto__. This must not create a | 428 // Parse a non-object value as __proto__. This must not create a |
| 425 // __proto__ property different from the original, and should not | 429 // __proto__ property different from the original, and should not |
| 426 // change the original. | 430 // change the original. |
| 427 var o = JSON.parse('{"__proto__":5}'); | 431 var o = JSON.parse('{"__proto__":5}'); |
| 428 assertEquals(Object.prototype, o.__proto__); // __proto__ isn't changed. | 432 assertEquals(Object.prototype, o.__proto__); // __proto__ isn't changed. |
| 429 assertEquals(0, Object.keys(o).length); // __proto__ isn't added as enumerable. | 433 assertEquals(0, Object.keys(o).length); // __proto__ isn't added as enumerable. |
| 430 | 434 |
| 431 | 435 |
| 432 | 436 |
| OLD | NEW |