Chromium Code Reviews| 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. | |
|
rossberg
2011/08/16 09:49:43
What about shadowing between "var" and "let"?
Steven
2011/08/16 15:06:08
Done.
| |
| 119 function f7(a) { | |
| 120 let b = 1; | |
| 121 { // let vars shadowing arg and let var | |
| 122 let a = 2; | |
| 123 let b = 2; | |
|
rossberg
2011/08/16 09:49:43
You might want to put in more assertions (here and
Steven
2011/08/16 15:06:08
Done.
| |
| 124 } | |
| 125 try { | |
| 126 throw 'stuff1'; | |
| 127 } catch (a) { | |
| 128 // catch var shadowing arg | |
| 129 a = 2; | |
| 130 { | |
| 131 // let var shadowing catch var | |
| 132 let a = 3; | |
| 133 try { | |
| 134 throw 'stuff2'; | |
| 135 } catch (a) { | |
| 136 // catch var shadowing let var | |
| 137 a = 4; | |
| 138 } | |
| 139 assertEquals(3,a); | |
| 140 } | |
| 141 assertEquals(2,a); | |
| 142 } | |
| 143 try { | |
| 144 throw 'stuff3'; | |
| 145 } catch (c) { | |
| 146 try { | |
| 147 throw 'stuff4'; | |
| 148 } catch(c) { | |
| 149 // catch var shadowing catch var | |
| 150 c = 3; | |
| 151 } | |
| 152 (function(c) { | |
| 153 // arg shadowing catch | |
| 154 })(); | |
| 155 assertEquals('stuff3', c); | |
| 156 } | |
| 157 (function(a,b) { | |
| 158 // arg shadowing arg and let var | |
| 159 a = 2; | |
| 160 b = 2; | |
| 161 })(1,1); | |
| 162 assertEquals(1,a); | |
| 163 assertEquals(1,b); | |
| 164 } | |
| 165 f7(1); | |
| 166 | |
| 167 | |
| 168 // Ensure let variables are block local and var variables function local. | |
| 169 function f8() { | |
|
rossberg
2011/08/16 09:49:43
You never invoke this. ;-)
Steven
2011/08/16 15:06:08
Done.
| |
| 170 var let_accessors = []; | |
| 171 var var_accessors = []; | |
| 172 for (var i = 0; i < 10; i++) { | |
| 173 let x = i; | |
| 174 var y = i; | |
| 175 let_accessors[i] = function() { return x; } | |
| 176 var_accessors[i] = function() { return i; } | |
| 177 } | |
| 178 for (var i = 0; i < 10; i++) { | |
| 179 y = j + 10; | |
| 180 assertEquals(i, let_accessors[i]()); | |
| 181 assertEquals(y, var_accessors[i]()); | |
| 182 } | |
| 183 } | |
| OLD | NEW |