| 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 14 matching lines...) Expand all Loading... |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 // Flags: --harmony-scoping | 28 // Flags: --harmony-scoping |
| 29 | 29 |
| 30 // Test let declarations in various settings. | 30 // Test let declarations in various settings. |
| 31 | 31 |
| 32 // Global | 32 // Global |
| 33 let x; | 33 let x; |
| 34 let y = 2; | 34 let y = 2; |
| 35 const z = 4; |
| 35 | 36 |
| 36 // Block local | 37 // Block local |
| 37 { | 38 { |
| 38 let y; | 39 let y; |
| 39 let x = 3; | 40 let x = 3; |
| 41 const z = 5; |
| 40 } | 42 } |
| 41 | 43 |
| 42 assertEquals(undefined, x); | 44 assertEquals(undefined, x); |
| 43 assertEquals(2,y); | 45 assertEquals(2,y); |
| 46 assertEquals(4,z); |
| 44 | 47 |
| 45 if (true) { | 48 if (true) { |
| 46 let y; | 49 let y; |
| 47 assertEquals(undefined, y); | 50 assertEquals(undefined, y); |
| 48 } | 51 } |
| 49 | 52 |
| 50 // Invalid declarations are early errors in harmony mode and thus should trigger | 53 // Invalid declarations are early errors in harmony mode and thus should trigger |
| 51 // an exception in eval code during parsing, before even compiling or executing | 54 // an exception in eval code during parsing, before even compiling or executing |
| 52 // the code. Thus the generated function is not called here. | 55 // the code. Thus the generated function is not called here. |
| 53 function TestLocalThrows(str, expect) { | 56 function TestLocalThrows(str, expect) { |
| 54 assertThrows("(function(){" + str + "})", expect); | 57 assertThrows("(function(){" + str + "})", expect); |
| 55 } | 58 } |
| 56 | 59 |
| 57 function TestLocalDoesNotThrow(str) { | 60 function TestLocalDoesNotThrow(str) { |
| 58 assertDoesNotThrow("(function(){" + str + "})()"); | 61 assertDoesNotThrow("(function(){" + str + "})()"); |
| 59 } | 62 } |
| 60 | 63 |
| 61 // Test let declarations statement positions. | 64 // Test let declarations in statement positions. |
| 62 TestLocalThrows("if (true) let x;", SyntaxError); | 65 TestLocalThrows("if (true) let x;", SyntaxError); |
| 63 TestLocalThrows("if (true) {} else let x;", SyntaxError); | 66 TestLocalThrows("if (true) {} else let x;", SyntaxError); |
| 64 TestLocalThrows("do let x; while (false)", SyntaxError); | 67 TestLocalThrows("do let x; while (false)", SyntaxError); |
| 65 TestLocalThrows("while (false) let x;", SyntaxError); | 68 TestLocalThrows("while (false) let x;", SyntaxError); |
| 66 TestLocalThrows("label: let x;", SyntaxError); | 69 TestLocalThrows("label: let x;", SyntaxError); |
| 67 TestLocalThrows("for (;false;) let x;", SyntaxError); | 70 TestLocalThrows("for (;false;) let x;", SyntaxError); |
| 68 TestLocalThrows("switch (true) { case true: let x; }", SyntaxError); | 71 TestLocalThrows("switch (true) { case true: let x; }", SyntaxError); |
| 69 TestLocalThrows("switch (true) { default: let x; }", SyntaxError); | 72 TestLocalThrows("switch (true) { default: let x; }", SyntaxError); |
| 70 | 73 |
| 71 // Test var declarations statement positions. | 74 // Test const declarations with initialisers in statement positions. |
| 75 TestLocalThrows("if (true) const x = 1;", SyntaxError); |
| 76 TestLocalThrows("if (true) {} else const x = 1;", SyntaxError); |
| 77 TestLocalThrows("do const x = 1; while (false)", SyntaxError); |
| 78 TestLocalThrows("while (false) const x = 1;", SyntaxError); |
| 79 TestLocalThrows("label: const x = 1;", SyntaxError); |
| 80 TestLocalThrows("for (;false;) const x = 1;", SyntaxError); |
| 81 TestLocalThrows("switch (true) { case true: const x = 1; }", SyntaxError); |
| 82 TestLocalThrows("switch (true) { default: const x = 1; }", SyntaxError); |
| 83 |
| 84 // Test const declarations without initialisers. |
| 85 TestLocalThrows("const x;", SyntaxError); |
| 86 TestLocalThrows("const x = 1, y;", SyntaxError); |
| 87 TestLocalThrows("const x, y = 1;", SyntaxError); |
| 88 |
| 89 // Test const declarations without initialisers in statement positions. |
| 90 TestLocalThrows("if (true) const x;", SyntaxError); |
| 91 TestLocalThrows("if (true) {} else const x;", SyntaxError); |
| 92 TestLocalThrows("do const x; while (false)", SyntaxError); |
| 93 TestLocalThrows("while (false) const x;", SyntaxError); |
| 94 TestLocalThrows("label: const x;", SyntaxError); |
| 95 TestLocalThrows("for (;false;) const x;", SyntaxError); |
| 96 TestLocalThrows("switch (true) { case true: const x; }", SyntaxError); |
| 97 TestLocalThrows("switch (true) { default: const x; }", SyntaxError); |
| 98 |
| 99 // Test var declarations in statement positions. |
| 72 TestLocalDoesNotThrow("if (true) var x;"); | 100 TestLocalDoesNotThrow("if (true) var x;"); |
| 73 TestLocalDoesNotThrow("if (true) {} else var x;"); | 101 TestLocalDoesNotThrow("if (true) {} else var x;"); |
| 74 TestLocalDoesNotThrow("do var x; while (false)"); | 102 TestLocalDoesNotThrow("do var x; while (false)"); |
| 75 TestLocalDoesNotThrow("while (false) var x;"); | 103 TestLocalDoesNotThrow("while (false) var x;"); |
| 76 TestLocalDoesNotThrow("label: var x;"); | 104 TestLocalDoesNotThrow("label: var x;"); |
| 77 TestLocalDoesNotThrow("for (;false;) var x;"); | 105 TestLocalDoesNotThrow("for (;false;) var x;"); |
| 78 TestLocalDoesNotThrow("switch (true) { case true: var x; }"); | 106 TestLocalDoesNotThrow("switch (true) { case true: var x; }"); |
| 79 TestLocalDoesNotThrow("switch (true) { default: var x; }"); | 107 TestLocalDoesNotThrow("switch (true) { default: var x; }"); |
| 80 | 108 |
| 81 // Test function declarations in source element and | 109 // Test function declarations in source element and |
| 82 // non-strict statement positions. | 110 // non-strict statement positions. |
| 83 function f() { | 111 function f() { |
| 84 // Non-strict source element positions. | 112 // Non-strict source element positions. |
| 85 function g0() { | 113 function g0() { |
| 86 "use strict"; | 114 "use strict"; |
| 87 // Strict source element positions. | 115 // Strict source element positions. |
| 88 function h() { } | 116 function h() { } |
| 89 { | 117 { |
| 90 function h1() { } | 118 function h1() { } |
| 91 } | 119 } |
| 92 } | 120 } |
| 93 { | 121 { |
| 94 function g1() { } | 122 function g1() { } |
| 95 } | 123 } |
| 96 // Non-strict statement positions. | |
| 97 if (true) function g2() { } | |
| 98 if (true) {} else function g3() { } | |
| 99 do function g4() { } while (false) | |
| 100 while (false) function g5() { } | |
| 101 label: function g6() { } | |
| 102 for (;false;) function g7() { } | |
| 103 switch (true) { case true: function g8() { } } | |
| 104 switch (true) { default: function g9() { } } | |
| 105 } | 124 } |
| 106 f(); | 125 f(); |
| 107 | 126 |
| 108 // Test function declarations in statement position in strict mode. | 127 // Test function declarations in statement position in strict mode. |
| 109 TestLocalThrows("function f() { 'use strict'; if (true) function g() {}", Syntax
Error); | 128 TestLocalThrows("function f() { if (true) function g() {}", SyntaxError); |
| 110 TestLocalThrows("function f() { 'use strict'; if (true) {} else function g() {}"
, SyntaxError); | 129 TestLocalThrows("function f() { if (true) {} else function g() {}", SyntaxError)
; |
| 111 TestLocalThrows("function f() { 'use strict'; do function g() {} while (false)",
SyntaxError); | 130 TestLocalThrows("function f() { do function g() {} while (false)", SyntaxError); |
| 112 TestLocalThrows("function f() { 'use strict'; while (false) function g() {}", Sy
ntaxError); | 131 TestLocalThrows("function f() { while (false) function g() {}", SyntaxError); |
| 113 TestLocalThrows("function f() { 'use strict'; label: function g() {}", SyntaxErr
or); | 132 TestLocalThrows("function f() { label: function g() {}", SyntaxError); |
| 114 TestLocalThrows("function f() { 'use strict'; for (;false;) function g() {}", Sy
ntaxError); | 133 TestLocalThrows("function f() { for (;false;) function g() {}", SyntaxError); |
| 115 TestLocalThrows("function f() { 'use strict'; switch (true) { case true: functio
n g() {} }", SyntaxError); | 134 TestLocalThrows("function f() { switch (true) { case true: function g() {} }", S
yntaxError); |
| 116 TestLocalThrows("function f() { 'use strict'; switch (true) { default: function
g() {} }", SyntaxError); | 135 TestLocalThrows("function f() { switch (true) { default: function g() {} }", Syn
taxError); |
| OLD | NEW |