Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 28 | // Flags: --allow-natives-syntax --legacy-const |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 29 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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;" + |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 39 | // Initialization redefines global 'x'. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 40 | "const x = 3; assertEquals(3, x);" + |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 41 | // Test constness of the global 'x'. |
| 42 | "x = 4; assertEquals(3, x);"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 43 | eval(source); |
| 44 | } |
| 45 | |
| 46 | testIntroduceGlobal(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 47 | assertEquals("undefined", typeof x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 48 | |
| 49 | function testAssignExistingGlobal() { |
| 50 | var source = |
| 51 | // Delete 'x' to remove the local const property. |
| 52 | "delete x;" + |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 53 | // Initialization redefines global 'x'. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 54 | "const x = 5; assertEquals(5, x);" + |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 55 | // Test constness of the global 'x'. |
| 56 | "x = 6; assertEquals(5, x);"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 57 | eval(source); |
| 58 | } |
| 59 | |
| 60 | testAssignExistingGlobal(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 61 | assertEquals("undefined", typeof x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 69 | assertEquals("undefined", typeof x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 72 | for (var i = 0; i < 5; i++) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 73 | testAssignmentArgument(); |
| 74 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 75 | %OptimizeFunctionOnNextCall(testAssignmentArgument); |
| 76 | testAssignmentArgument(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 77 | assertEquals("undefined", typeof x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 78 | |
| 79 | __defineSetter__('x', function() { throw 42; }); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 80 | var finished = false; |
| 81 | function testRedefineGlobal() { |
| 82 | // Initialization redefines global 'x'. |
| 83 | var source = "delete x; const x = 8; finished = true;"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 84 | eval(source); |
| 85 | } |
| 86 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 87 | testRedefineGlobal(); |
| 88 | assertTrue(finished); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 89 | |
| 90 | function testInitFastCaseExtension() { |
| 91 | var source = "const x = 9; assertEquals(9, x); x = 10; assertEquals(9, x)"; |
| 92 | eval(source); |
| 93 | } |
| 94 | |
| 95 | testInitFastCaseExtension(); |
| 96 | |
| 97 | function testInitSlowCaseExtension() { |
| 98 | var source = ""; |
| 99 | // Introduce 100 properties on the context extension object to force |
| 100 | // it in slow case. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 101 | for (var i = 0; i < 100; i++) source += ("var a" + i + " = " + i + ";"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 102 | source += "const x = 10; assertEquals(10, x); x = 11; assertEquals(10, x)"; |
| 103 | eval(source); |
| 104 | } |
| 105 | |
| 106 | testInitSlowCaseExtension(); |
| 107 | |
| 108 | function testAssignSurroundingContextSlot() { |
| 109 | var x = 12; |
| 110 | function local() { |
| 111 | var source = "delete x; const x = 13; assertEquals(13, x)"; |
| 112 | eval(source); |
| 113 | } |
| 114 | local(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 115 | assertEquals(12, x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | testAssignSurroundingContextSlot(); |