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 38 matching lines...) Loading... |
49 | 49 |
50 if (true) { | 50 if (true) { |
51 let y; | 51 let y; |
52 assertEquals(undefined, y); | 52 assertEquals(undefined, y); |
53 } | 53 } |
54 | 54 |
55 // Invalid declarations are early errors in harmony mode and thus should trigger | 55 // Invalid declarations are early errors in harmony mode and thus should trigger |
56 // an exception in eval code during parsing, before even compiling or executing | 56 // an exception in eval code during parsing, before even compiling or executing |
57 // the code. Thus the generated function is not called here. | 57 // the code. Thus the generated function is not called here. |
58 function TestLocalThrows(str, expect) { | 58 function TestLocalThrows(str, expect) { |
59 assertThrows("(function(){ 'use strict'; " + str + "})", expect); | 59 assertThrows("(function(arg){ 'use strict'; " + str + "})", expect); |
60 } | 60 } |
61 | 61 |
62 function TestLocalDoesNotThrow(str) { | 62 function TestLocalDoesNotThrow(str) { |
63 assertDoesNotThrow("(function(){ 'use strict'; " + str + "})()"); | 63 assertDoesNotThrow("(function(arg){ 'use strict'; " + str + "})()"); |
64 } | 64 } |
65 | 65 |
66 // Test let declarations in statement positions. | 66 // Test let declarations in statement positions. |
67 TestLocalThrows("if (true) let x;", SyntaxError); | 67 TestLocalThrows("if (true) let x;", SyntaxError); |
68 TestLocalThrows("if (true) {} else let x;", SyntaxError); | 68 TestLocalThrows("if (true) {} else let x;", SyntaxError); |
69 TestLocalThrows("do let x; while (false)", SyntaxError); | 69 TestLocalThrows("do let x; while (false)", SyntaxError); |
70 TestLocalThrows("while (false) let x;", SyntaxError); | 70 TestLocalThrows("while (false) let x;", SyntaxError); |
71 TestLocalThrows("label: let x;", SyntaxError); | 71 TestLocalThrows("label: let x;", SyntaxError); |
72 TestLocalThrows("for (;false;) let x;", SyntaxError); | 72 TestLocalThrows("for (;false;) let x;", SyntaxError); |
73 TestLocalThrows("switch (true) { case true: let x; }", SyntaxError); | 73 TestLocalThrows("switch (true) { case true: let x; }", SyntaxError); |
(...skipping 27 matching lines...) Loading... |
101 // Test var declarations in statement positions. | 101 // Test var declarations in statement positions. |
102 TestLocalDoesNotThrow("if (true) var x;"); | 102 TestLocalDoesNotThrow("if (true) var x;"); |
103 TestLocalDoesNotThrow("if (true) {} else var x;"); | 103 TestLocalDoesNotThrow("if (true) {} else var x;"); |
104 TestLocalDoesNotThrow("do var x; while (false)"); | 104 TestLocalDoesNotThrow("do var x; while (false)"); |
105 TestLocalDoesNotThrow("while (false) var x;"); | 105 TestLocalDoesNotThrow("while (false) var x;"); |
106 TestLocalDoesNotThrow("label: var x;"); | 106 TestLocalDoesNotThrow("label: var x;"); |
107 TestLocalDoesNotThrow("for (;false;) var x;"); | 107 TestLocalDoesNotThrow("for (;false;) var x;"); |
108 TestLocalDoesNotThrow("switch (true) { case true: var x; }"); | 108 TestLocalDoesNotThrow("switch (true) { case true: var x; }"); |
109 TestLocalDoesNotThrow("switch (true) { default: var x; }"); | 109 TestLocalDoesNotThrow("switch (true) { default: var x; }"); |
110 | 110 |
| 111 // Test that redeclarations of functions are only allowed in outermost scope. |
| 112 TestLocalThrows("{ let f; var f; }"); |
| 113 TestLocalThrows("{ var f; let f; }"); |
| 114 TestLocalThrows("{ function f() {} let f; }"); |
| 115 TestLocalThrows("{ let f; function f() {} }"); |
| 116 TestLocalThrows("{ function f() {} var f; }"); |
| 117 TestLocalThrows("{ var f; function f() {} }"); |
| 118 TestLocalThrows("{ function f() {} function f() {} }"); |
| 119 TestLocalThrows("function f() {} let f;"); |
| 120 TestLocalThrows("let f; function f() {}"); |
| 121 TestLocalDoesNotThrow("function arg() {}"); |
| 122 TestLocalDoesNotThrow("function f() {} var f;"); |
| 123 TestLocalDoesNotThrow("var f; function f() {}"); |
| 124 TestLocalDoesNotThrow("function f() {} function f() {}"); |
| 125 |
| 126 function g(f) { |
| 127 function f() { return 1 } |
| 128 return f() |
| 129 } |
| 130 assertEquals(1, g(function() { return 2 })) |
| 131 |
| 132 |
111 // Test function declarations in source element and | 133 // Test function declarations in source element and |
112 // sloppy statement positions. | 134 // sloppy statement positions. |
113 function f() { | 135 function f() { |
114 // Sloppy source element positions. | 136 // Sloppy source element positions. |
115 function g0() { | 137 function g0() { |
116 "use strict"; | 138 "use strict"; |
117 // Strict source element positions. | 139 // Strict source element positions. |
118 function h() { } | 140 function h() { } |
119 { | 141 { |
120 function h1() { } | 142 function h1() { } |
121 } | 143 } |
122 } | 144 } |
123 { | 145 { |
124 function g1() { } | 146 function g1() { } |
125 } | 147 } |
126 } | 148 } |
127 f(); | 149 f(); |
128 | 150 |
129 // Test function declarations in statement position in strict mode. | 151 // Test function declarations in statement position in strict mode. |
130 TestLocalThrows("function f() { if (true) function g() {}", SyntaxError); | 152 TestLocalThrows("function f() { if (true) function g() {}", SyntaxError); |
131 TestLocalThrows("function f() { if (true) {} else function g() {}", SyntaxError)
; | 153 TestLocalThrows("function f() { if (true) {} else function g() {}", SyntaxError)
; |
132 TestLocalThrows("function f() { do function g() {} while (false)", SyntaxError); | 154 TestLocalThrows("function f() { do function g() {} while (false)", SyntaxError); |
133 TestLocalThrows("function f() { while (false) function g() {}", SyntaxError); | 155 TestLocalThrows("function f() { while (false) function g() {}", SyntaxError); |
134 TestLocalThrows("function f() { label: function g() {}", SyntaxError); | 156 TestLocalThrows("function f() { label: function g() {}", SyntaxError); |
135 TestLocalThrows("function f() { for (;false;) function g() {}", SyntaxError); | 157 TestLocalThrows("function f() { for (;false;) function g() {}", SyntaxError); |
136 TestLocalThrows("function f() { switch (true) { case true: function g() {} }", S
yntaxError); | 158 TestLocalThrows("function f() { switch (true) { case true: function g() {} }", S
yntaxError); |
137 TestLocalThrows("function f() { switch (true) { default: function g() {} }", Syn
taxError); | 159 TestLocalThrows("function f() { switch (true) { default: function g() {} }", Syn
taxError); |
OLD | NEW |