1 // Copyright 2009 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 29 30 // Test the handling of initialization of deleted const variables. 31 // This only makes sense in local scopes since the declaration and 32 // initialization of consts in the global scope happen at the same 33 // time. 34 35 function testIntroduceGlobal() { 36 var source = 37 // Deleting 'x' removes the local const property. 38 "delete x;" + 39 // Initialization turns into assignment to global 'x'. 40 "const x = 3; assertEquals(3, x);" + 41 // No constness of the global 'x'. 42 "x = 4; assertEquals(4, x);"; 43 eval(source); 44 } 45 46 testIntroduceGlobal(); 47 assertEquals(4, x); 48 49 function testAssignExistingGlobal() { 50 var source = 51 // Delete 'x' to remove the local const property. 52 "delete x;" + 53 // Initialization turns into assignment to global 'x'. 54 "const x = 5; assertEquals(5, x);" + 55 // No constness of the global 'x'. 56 "x = 6; assertEquals(6, x);"; 57 eval(source); 58 } 59 60 testAssignExistingGlobal(); 61 assertEquals(6, x); 62 63 function testAssignmentArgument(x) { 64 function local() { 65 var source = "delete x; const x = 7; assertEquals(7, x)"; 66 eval(source); 67 } 68 local(); 69 assertEquals(7, x); 70 } 71 72 for (var i = 0; i < 5; i++) { 73 testAssignmentArgument(); 74 } 75 %OptimizeFunctionOnNextCall(testAssignmentArgument); 76 testAssignmentArgument(); 77 assertEquals(6, x); 78 79 __defineSetter__('x', function() { throw 42; }); 80 function testAssignGlobalThrows() { 81 // Initialization turns into assignment to global 'x' which 82 // throws an exception. 83 var source = "delete x; const x = 8"; 84 eval(source); 85 } 86 87 assertThrows("testAssignGlobalThrows()"); 88 89 function testInitFastCaseExtension() { 90 var source = "const x = 9; assertEquals(9, x); x = 10; assertEquals(9, x)"; 91 eval(source); 92 } 93 94 testInitFastCaseExtension(); 95 96 function testInitSlowCaseExtension() { 97 var source = ""; 98 // Introduce 100 properties on the context extension object to force 99 // it in slow case. 100 for (var i = 0; i < 100; i++) source += ("var a" + i + " = " + i + ";"); 101 source += "const x = 10; assertEquals(10, x); x = 11; assertEquals(10, x)"; 102 eval(source); 103 } 104 105 testInitSlowCaseExtension(); 106 107 function testAssignSurroundingContextSlot() { 108 var x = 12; 109 function local() { 110 var source = "delete x; const x = 13; assertEquals(13, x)"; 111 eval(source); 112 } 113 local(); 114 assertEquals(13, x); 115 } 116 117 testAssignSurroundingContextSlot(); 118