OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 21 matching lines...) Expand all Loading... |
32 function f1() { | 32 function f1() { |
33 { | 33 { |
34 var x = 1; | 34 var x = 1; |
35 var y; | 35 var y; |
36 } | 36 } |
37 assertEquals(1, x) | 37 assertEquals(1, x) |
38 assertEquals(undefined, y) | 38 assertEquals(undefined, y) |
39 } | 39 } |
40 f1(); | 40 f1(); |
41 | 41 |
42 // Dynamic lookup through block scopes. | 42 |
| 43 // Dynamic lookup in and through block contexts. |
43 function f2(one) { | 44 function f2(one) { |
44 var x = one + 1; | 45 var x = one + 1; |
45 // TODO(keuchel): introduce let | 46 let y = one + 2; |
46 // let y = one + 2; | 47 { |
47 if (one == 1) { | 48 let z = one + 3; |
48 // Parameter | |
49 assertEquals(1, eval('one')); | 49 assertEquals(1, eval('one')); |
50 // Function local var variable | |
51 assertEquals(2, eval('x')); | 50 assertEquals(2, eval('x')); |
52 // Function local let variable | 51 assertEquals(3, eval('y')); |
53 // TODO(keuchel): introduce let | 52 assertEquals(4, eval('z')); |
54 // assertEquals(3, eval('y')); | |
55 } | 53 } |
56 } | 54 } |
57 f2(1); | 55 f2(1); |
58 | 56 |
| 57 |
| 58 // Lookup in and through block contexts. |
| 59 function f3(one) { |
| 60 var x = one + 1; |
| 61 let y = one + 2; |
| 62 { |
| 63 let z = one + 3; |
| 64 assertEquals(1, one); |
| 65 assertEquals(2, x); |
| 66 assertEquals(3, y); |
| 67 assertEquals(4, z); |
| 68 } |
| 69 } |
| 70 f3(1); |
| 71 |
| 72 |
| 73 // Dynamic lookup from closure. |
| 74 function f4(one) { |
| 75 var x = one + 1; |
| 76 let y = one + 2; |
| 77 { |
| 78 let z = one + 3; |
| 79 function f() { |
| 80 assertEquals(1, eval('one')); |
| 81 assertEquals(2, eval('x')); |
| 82 assertEquals(3, eval('y')); |
| 83 assertEquals(4, eval('z')); |
| 84 }; |
| 85 } |
| 86 } |
| 87 f4(1); |
| 88 |
| 89 |
| 90 // Lookup from closure. |
| 91 function f5(one) { |
| 92 var x = one + 1; |
| 93 let y = one + 2; |
| 94 { |
| 95 let z = one + 3; |
| 96 function f() { |
| 97 assertEquals(1, one); |
| 98 assertEquals(2, x); |
| 99 assertEquals(3, y); |
| 100 assertEquals(4, z); |
| 101 }; |
| 102 } |
| 103 } |
| 104 f5(1); |
| 105 |
| 106 |
| 107 // Return from block. |
| 108 function f6() { |
| 109 let x = 1; |
| 110 { |
| 111 let y = 2; |
| 112 return x + y; |
| 113 } |
| 114 } |
| 115 assertEquals(3, f6(6)); |
| 116 |
| 117 |
| 118 // Variable shadowing and lookup. |
| 119 function f7(a) { |
| 120 let b = 1; |
| 121 var c = 1; |
| 122 var d = 1; |
| 123 { // let variables shadowing argument, let and var variables |
| 124 let a = 2; |
| 125 let b = 2; |
| 126 let c = 2; |
| 127 assertEquals(2,a); |
| 128 assertEquals(2,b); |
| 129 assertEquals(2,c); |
| 130 } |
| 131 try { |
| 132 throw 'stuff1'; |
| 133 } catch (a) { |
| 134 assertEquals('stuff1',a); |
| 135 // catch variable shadowing argument |
| 136 a = 2; |
| 137 assertEquals(2,a); |
| 138 { |
| 139 // let variable shadowing catch variable |
| 140 let a = 3; |
| 141 assertEquals(3,a); |
| 142 try { |
| 143 throw 'stuff2'; |
| 144 } catch (a) { |
| 145 assertEquals('stuff2',a); |
| 146 // catch variable shadowing let variable |
| 147 a = 4; |
| 148 assertEquals(4,a); |
| 149 } |
| 150 assertEquals(3,a); |
| 151 } |
| 152 assertEquals(2,a); |
| 153 } |
| 154 try { |
| 155 throw 'stuff3'; |
| 156 } catch (c) { |
| 157 // catch variable shadowing var variable |
| 158 assertEquals('stuff3',c); |
| 159 try { |
| 160 throw 'stuff4'; |
| 161 } catch(c) { |
| 162 assertEquals('stuff4',c); |
| 163 // catch variable shadowing catch variable |
| 164 c = 3; |
| 165 assertEquals(3,c); |
| 166 } |
| 167 (function(c) { |
| 168 // argument shadowing catch variable |
| 169 c = 3; |
| 170 assertEquals(3,c); |
| 171 })(); |
| 172 assertEquals('stuff3', c); |
| 173 (function() { |
| 174 // var variable shadowing catch variable |
| 175 var c = 3; |
| 176 })(); |
| 177 assertEquals('stuff3', c); |
| 178 c = 2; |
| 179 } |
| 180 assertEquals(1,c); |
| 181 (function(a,b,c) { |
| 182 // arguments shadowing argument, let and var variable |
| 183 a = 2; |
| 184 b = 2; |
| 185 c = 2; |
| 186 assertEquals(2,a); |
| 187 assertEquals(2,b); |
| 188 assertEquals(2,c); |
| 189 // var variable shadowing var variable |
| 190 var d = 2; |
| 191 })(1,1); |
| 192 assertEquals(1,a); |
| 193 assertEquals(1,b); |
| 194 assertEquals(1,c); |
| 195 assertEquals(1,d); |
| 196 } |
| 197 f7(1); |
| 198 |
| 199 |
| 200 // Ensure let variables are block local and var variables function local. |
| 201 function f8() { |
| 202 var let_accessors = []; |
| 203 var var_accessors = []; |
| 204 for (var i = 0; i < 10; i++) { |
| 205 let x = i; |
| 206 var y = i; |
| 207 let_accessors[i] = function() { return x; } |
| 208 var_accessors[i] = function() { return y; } |
| 209 } |
| 210 for (var j = 0; j < 10; j++) { |
| 211 y = j + 10; |
| 212 assertEquals(j, let_accessors[j]()); |
| 213 assertEquals(y, var_accessors[j]()); |
| 214 } |
| 215 } |
| 216 f8(); |
OLD | NEW |