OLD | NEW |
(Empty) | |
| 1 // Copyright 2011 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: --harmony-block-scoping |
| 29 |
| 30 // Test for conflicting variable bindings. |
| 31 |
| 32 function CheckException(e) { |
| 33 var string = e.toString(); |
| 34 assertTrue(string.indexOf("has already been declared") >= 0 || |
| 35 string.indexOf("redeclaration") >= 0); return 'Conflict'; |
| 36 } |
| 37 |
| 38 |
| 39 function TestFunction(s,e) { |
| 40 try { |
| 41 return eval("(function(){" + s + ";return " + e + "})")(); |
| 42 } catch (x) { |
| 43 return CheckException(x); |
| 44 } |
| 45 } |
| 46 |
| 47 |
| 48 function TestBlock(s,e) { |
| 49 try { |
| 50 return eval("(function(){ if (true) { " + s + "; }; return " + e + "})")(); |
| 51 } catch (x) { |
| 52 return CheckException(x); |
| 53 } |
| 54 } |
| 55 |
| 56 function TestAll(expected,s,opt_e) { |
| 57 var e = ""; |
| 58 var msg = s; |
| 59 if (opt_e) { e = opt_e; msg += "; " + opt_e; } |
| 60 assertEquals(expected, TestFunction(s,e), "function:'" + msg + "'"); |
| 61 assertEquals(expected, TestBlock(s,e), "block:'" + msg + "'"); |
| 62 } |
| 63 |
| 64 |
| 65 function TestConflict(s) { |
| 66 TestAll('Conflict', s); |
| 67 TestAll('Conflict', 'eval("' + s + '")'); |
| 68 } |
| 69 |
| 70 |
| 71 function TestNoConflict(s) { |
| 72 TestAll('NoConflict', s, "'NoConflict'"); |
| 73 TestAll('NoConflict', 'eval("' + s + '")', "'NoConflict'"); |
| 74 } |
| 75 |
| 76 var letbinds = [ "let x", |
| 77 "let x = 0", |
| 78 "let x = undefined", |
| 79 "function x() { }", |
| 80 "let x = function() {}", |
| 81 "let x, y", |
| 82 "let y, x", |
| 83 ]; |
| 84 var varbinds = [ "var x", |
| 85 "var x = 0", |
| 86 "var x = undefined", |
| 87 "var x = function() {}", |
| 88 "var x, y", |
| 89 "var y, x", |
| 90 ]; |
| 91 |
| 92 |
| 93 for (var l = 0; l < letbinds.length; ++l) { |
| 94 // Test conflicting let/var bindings. |
| 95 for (var v = 0; v < varbinds.length; ++v) { |
| 96 // Same level. |
| 97 TestConflict(letbinds[l] +'; ' + varbinds[v]); |
| 98 TestConflict(varbinds[v] +'; ' + letbinds[l]); |
| 99 // Different level. |
| 100 TestConflict(letbinds[l] +'; {' + varbinds[v] + '; }'); |
| 101 TestConflict('{ ' + varbinds[v] +'; }' + letbinds[l]); |
| 102 } |
| 103 |
| 104 // Test conflicting let/let bindings. |
| 105 for (var k = 0; k < letbinds.length; ++k) { |
| 106 // Same level. |
| 107 TestConflict(letbinds[l] +'; ' + letbinds[k]); |
| 108 TestConflict(letbinds[k] +'; ' + letbinds[l]); |
| 109 // Different level. |
| 110 TestNoConflict(letbinds[l] +'; { ' + letbinds[k] + '; }'); |
| 111 TestNoConflict('{ ' + letbinds[k] +'; } ' + letbinds[l]); |
| 112 } |
| 113 |
| 114 // Test conflicting parameter/let bindings. |
| 115 TestConflict('(function (x) { ' + letbinds[l] + '; })()'); |
| 116 } |
| 117 |
| 118 // Test conflicting catch/var bindings. |
| 119 for (var v = 0; v < varbinds.length; ++v) { |
| 120 TestConflict('try {} catch (x) { ' + varbinds[v] + '; }'); |
| 121 } |
| 122 |
| 123 // Test conflicting parameter/var bindings. |
| 124 for (var v = 0; v < varbinds.length; ++v) { |
| 125 TestConflict('(function (x) { ' + varbinds[v] + '; })()'); |
| 126 } |
OLD | NEW |