Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2016 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 | // |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5 | // Flags: --harmony-sloppy-function |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6 | |
| 7 | // In sloppy mode we allow function redeclarations within blocks for webcompat. |
| 8 | (function() { |
| 9 | assertEquals(undefined, f); // Annex B |
| 10 | if (true) { |
| 11 | assertEquals(2, f()); |
| 12 | function f() { return 1 } |
| 13 | assertEquals(2, f()); |
| 14 | function f() { return 2 } |
| 15 | assertEquals(2, f()); |
| 16 | } |
| 17 | assertEquals(2, f()); // Annex B |
| 18 | })(); |
| 19 | |
| 20 | // Should still fail in strict mode |
| 21 | assertThrows(` |
| 22 | (function() { |
| 23 | "use strict"; |
| 24 | if (true) { |
| 25 | function f() { return 1 } |
| 26 | function f() { return 2 } |
| 27 | } |
| 28 | })(); |
| 29 | `, SyntaxError); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 30 | |
| 31 | // Conflicts between let and function still throw |
| 32 | assertThrows(` |
| 33 | (function() { |
| 34 | if (true) { |
| 35 | let f; |
| 36 | function f() { return 2 } |
| 37 | } |
| 38 | })(); |
| 39 | `, SyntaxError); |
| 40 | |
| 41 | assertThrows(` |
| 42 | (function() { |
| 43 | if (true) { |
| 44 | function f() { return 2 } |
| 45 | let f; |
| 46 | } |
| 47 | })(); |
| 48 | `, SyntaxError); |
| 49 | |
| 50 | // Conflicts between const and function still throw |
| 51 | assertThrows(` |
| 52 | (function() { |
| 53 | if (true) { |
| 54 | const f; |
| 55 | function f() { return 2 } |
| 56 | } |
| 57 | })(); |
| 58 | `, SyntaxError); |
| 59 | |
| 60 | assertThrows(` |
| 61 | (function() { |
| 62 | if (true) { |
| 63 | function f() { return 2 } |
| 64 | const f; |
| 65 | } |
| 66 | })(); |
| 67 | `, SyntaxError); |
| 68 | |
| 69 | // Annex B redefinition semantics still apply with more blocks |
| 70 | (function() { |
| 71 | assertEquals(undefined, f); // Annex B |
| 72 | if (true) { |
| 73 | assertEquals(undefined, f); |
| 74 | { function f() { return 1 } } |
| 75 | assertEquals(1, f()); |
| 76 | { function f() { return 2 } } |
| 77 | assertEquals(2, f()); |
| 78 | } |
| 79 | assertEquals(2, f()); // Annex B |
| 80 | })(); |