Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1 | // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Flags: --harmony-sloppy --harmony-sloppy-let --harmony-sloppy-function |
| 6 | // Flags: --legacy-const |
| 7 | |
| 8 | // Legacy-const-let conflict in a function throws, even if the legacy const |
| 9 | // is in an eval |
| 10 | |
| 11 | // Throws at the top level of a function |
| 12 | assertThrows(function() { |
| 13 | let x = 1; |
| 14 | eval('const x = 2'); |
| 15 | }, TypeError); |
| 16 | |
| 17 | // If the eval is in its own block scope, throws |
| 18 | assertThrows(function() { |
| 19 | let y = 1; |
| 20 | { eval('const y = 2'); } |
| 21 | }, TypeError); |
| 22 | |
| 23 | // If the let is in its own block scope, with the eval, throws |
| 24 | assertThrows(function() { |
| 25 | { |
| 26 | let x = 1; |
| 27 | eval('const x = 2'); |
| 28 | } |
| 29 | }, TypeError); |
| 30 | |
| 31 | // Legal if the let is no longer visible |
| 32 | assertDoesNotThrow(function() { |
| 33 | { |
| 34 | let x = 1; |
| 35 | } |
| 36 | eval('const x = 2'); |
| 37 | }); |
| 38 | |
| 39 | // In global scope |
| 40 | let caught = false; |
| 41 | try { |
| 42 | let z = 1; |
| 43 | eval('const z = 2'); |
| 44 | } catch (e) { |
| 45 | caught = true; |
| 46 | } |
| 47 | assertTrue(caught); |
| 48 | |
| 49 | // Let declarations beyond a function boundary don't conflict |
| 50 | caught = false; |
| 51 | try { |
| 52 | let a = 1; |
| 53 | (function() { |
| 54 | eval('const a'); |
| 55 | })(); |
| 56 | } catch (e) { |
| 57 | caught = true; |
| 58 | } |
| 59 | assertFalse(caught); |
| 60 | |
| 61 | // legacy const across with doesn't conflict |
| 62 | caught = false; |
| 63 | try { |
| 64 | (function() { |
| 65 | with ({x: 1}) { |
| 66 | eval("const x = 2;"); |
| 67 | } |
| 68 | })(); |
| 69 | } catch (e) { |
| 70 | caught = true; |
| 71 | } |
| 72 | assertFalse(caught); |
| 73 | |
| 74 | // legacy const can still conflict with let across a with |
| 75 | caught = false; |
| 76 | try { |
| 77 | (function() { |
| 78 | let x; |
| 79 | with ({x: 1}) { |
| 80 | eval("const x = 2;"); |
| 81 | } |
| 82 | })(); |
| 83 | } catch (e) { |
| 84 | caught = true; |
| 85 | } |
| 86 | assertTrue(caught); |