1 // Copyright 2013 the V8 project authors. All rights reserved. 2 // Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions 6 // are met: 7 // 1. Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // 2. Redistributions in binary form must reproduce the above copyright 10 // notice, this list of conditions and the following disclaimer in the 11 // documentation and/or other materials provided with the distribution. 12 // 13 // THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 14 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 // DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 24 description( 25 "This test checks that toString() round-trip on a function that has a array with elision does not remove a comma." 26 ); 27 28 function f1() { 29 return [,]; 30 } 31 32 function f2() { 33 return [1]; 34 } 35 36 function f3() { 37 return [1,]; 38 } 39 40 // this is the first testcase that proves that trailing 41 // elision comma is not removed 42 function f4() { 43 return [1,,]; 44 } 45 46 function f5() { 47 return [1,,,]; 48 } 49 function f6() { 50 return [1,,,4]; 51 } 52 53 function f7() { 54 return [,2,]; 55 } 56 57 function f8() { 58 return [,2,,]; 59 } 60 61 function f9() { 62 return [,2,,,5]; 63 } 64 65 function f10() { 66 return [,,3,,,]; 67 } 68 69 function f11() { 70 return [,,3,,,6]; 71 } 72 73 function f12() { 74 return [,undefined]; 75 } 76 77 function f13() { 78 return [,undefined,]; 79 } 80 81 function f14() { 82 return [,undefined,,]; 83 } 84 85 function f15() { 86 return [,,]; 87 } 88 89 function f16() { 90 return [,,,]; 91 } 92 93 shouldBe("typeof undefined", "'undefined'"); 94 95 unevalf = function(x) { return '(' + x.toString() + ')'; }; 96 97 function testToStringAndLength(fn, length, lastElement) 98 { 99 // check that array length is correct 100 shouldBe(""+ fn +"().length", "" + length); 101 102 // check that last element is what it is supposed to be 103 shouldBe(""+ fn +"()[" + length +"-1]", "" + lastElement); 104 105 // check that toString result evaluates to code that can be evaluated 106 // and that toString doesn't remove the trailing elision comma. 107 shouldBe("unevalf(eval(unevalf("+fn+")))", "unevalf(" + fn + ")"); 108 109 // check that toString()ed functions should retain semantics 110 111 shouldBe("eval(unevalf("+fn+"))().length", ""+length); 112 shouldBe("eval(unevalf("+fn+"))()[" + length +"-1]", ""+lastElement); 113 } 114 115 116 testToStringAndLength("f1", 1); 117 testToStringAndLength("f2", 1, 1); 118 testToStringAndLength("f3", 1,1); 119 testToStringAndLength("f4", 2); 120 testToStringAndLength("f5", 3); 121 testToStringAndLength("f6", 4, 4); 122 testToStringAndLength("f7", 2, 2); 123 testToStringAndLength("f8", 3); 124 testToStringAndLength("f9", 5, 5); 125 testToStringAndLength("f10", 5); 126 testToStringAndLength("f11", 6, 6); 127 testToStringAndLength("f12", 2); 128 testToStringAndLength("f13", 2); 129 testToStringAndLength("f14", 3); 130 testToStringAndLength("f15", 2); 131 testToStringAndLength("f16", 3); 132