OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Flags: --no-legacy-const --harmony-sloppy --harmony-sloppy-let --harmony-slop py-function | |
adamk
2015/09/11 15:42:56
You can have multiple "Flags:" lines to keep this
Dan Ehrenberg
2015/09/17 17:44:10
Done
| |
6 | |
7 // Test Annex B 3.3 semantics for functions declared in blocks in sloppy mode. | |
adamk
2015/09/11 15:42:56
Can you add tests for shadowing function params, b
Dan Ehrenberg
2015/09/17 18:10:27
Added tests. As you predicted, one of them fails,
| |
8 // http://www.ecma-international.org/ecma-262/6.0/#sec-block-level-function-decl arations-web-legacy-compatibility-semantics | |
9 | |
10 (function overridingLocalFunction() { | |
11 var x = []; | |
12 assertEquals('function', typeof f); | |
13 function f() { | |
14 x.push(1); | |
15 } | |
16 f(); | |
17 { | |
18 f(); | |
19 function f() { | |
20 x.push(2); | |
21 } | |
22 f(); | |
23 } | |
24 f(); | |
25 { | |
26 f(); | |
27 function f() { | |
28 x.push(3); | |
29 } | |
30 f(); | |
31 } | |
32 f(); | |
33 assertArrayEquals([1, 2, 2, 2, 3, 3, 3], x); | |
34 })(); | |
35 | |
36 (function newFunctionBinding() { | |
37 var x = []; | |
38 assertEquals('undefined', typeof f); | |
39 { | |
40 f(); | |
41 function f() { | |
42 x.push(2); | |
43 } | |
44 f(); | |
45 } | |
46 f(); | |
47 { | |
48 f(); | |
49 function f() { | |
50 x.push(3); | |
51 } | |
52 f(); | |
53 } | |
54 f(); | |
55 assertArrayEquals([2, 2, 2, 3, 3, 3], x); | |
56 })(); | |
57 | |
58 (function shadowingLetDoesntBind() { | |
59 let f = 1; | |
60 assertEquals(1, f); | |
61 { | |
62 let y = 3; | |
63 function f() { | |
64 y = 2; | |
65 } | |
66 f(); | |
67 assertEquals(2, y); | |
68 } | |
69 assertEquals(1, f); | |
70 })(); | |
71 | |
72 (function shadowingClassDoesntBind() { | |
73 class f { } | |
74 assertEquals('class f { }', f.toString()); | |
75 { | |
76 let y = 3; | |
77 function f() { | |
78 y = 2; | |
79 } | |
80 f(); | |
81 assertEquals(2, y); | |
82 } | |
83 assertEquals('class f { }', f.toString()); | |
84 })(); | |
85 | |
86 (function shadowingConstDoesntBind() { | |
87 const f = 1; | |
88 assertEquals(1, f); | |
89 { | |
90 let y = 3; | |
91 function f() { | |
92 y = 2; | |
93 } | |
94 f(); | |
95 assertEquals(2, y); | |
96 } | |
97 assertEquals(1, f); | |
98 })(); | |
99 | |
100 (function shadowingVarBinds() { | |
101 var f = 1; | |
102 assertEquals(1, f); | |
103 { | |
104 let y = 3; | |
105 function f() { | |
106 y = 2; | |
107 } | |
108 f(); | |
109 assertEquals(2, y); | |
110 } | |
111 assertEquals('function', typeof f); | |
112 })(); | |
113 | |
114 (function conditional() { | |
115 if (true) { | |
116 function f() { return 1; } | |
117 } else { | |
118 function f() { return 2; } | |
119 } | |
120 assertEquals(1, f()); | |
121 | |
122 if (false) { | |
123 function g() { return 1; } | |
124 } else { | |
125 function g() { return 2; } | |
126 } | |
127 assertEquals(2, g()); | |
128 })(); | |
129 | |
130 (function skipExecution() { | |
131 { | |
132 function f() { return 1; } | |
133 } | |
134 assertEquals(1, f()); | |
135 { | |
136 function f() { return 2; } | |
137 } | |
138 assertEquals(2, f()); | |
139 L: { | |
140 assertEquals(3, f()); | |
141 break L; | |
142 function f() { return 3; } | |
143 } | |
144 assertEquals(2, f()); | |
145 })(); | |
146 | |
147 // Test that hoisting from blocks doesn't happen in global scope | |
adamk
2015/09/11 15:42:56
How is this effect achieved?
Dan Ehrenberg
2015/09/17 17:44:10
By 'global' I meant 'not in a function'. This is t
| |
148 function globalUnhoisted() { return 0; } | |
149 { | |
150 function globalUnhoisted() { return 1; } | |
151 } | |
152 assertEquals(0, globalUnhoisted()); | |
OLD | NEW |