Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5 | // See: http://code.google.com/p/v8/issues/detail?id=3926 |
| 6 | |
| 7 | // Switch statements should disable hole check elimination |
| 8 | |
| 9 | // Ensure that both reads and writes encounter the hole check |
| 10 | // FullCodeGen had an issue on reads; TurboFan had an issue on writes |
| 11 | function f(x) { |
| 12 | var z; |
| 13 | switch (x) { |
| 14 | case 1: |
| 15 | let y = 1; |
| 16 | case 2: |
| 17 | y = 2; |
| 18 | case 3: |
| 19 | z = y; |
| 20 | } |
| 21 | return z; |
| 22 | } |
| 23 | assertEquals(2, f(1)); |
| 24 | assertThrows(function() {f(2)}, ReferenceError); |
| 25 | assertThrows(function() {f(3)}, ReferenceError); |
| 26 | |
| 27 | // Ensure that hole checks are done even in subordinate scopes |
| 28 | assertThrows(function() { |
| 29 | switch (1) { |
| 30 | case 0: |
| 31 | let x = 2; |
| 32 | case 1: |
| 33 | { // this block, plus the let below, adds another linear lexical scope |
| 34 | let y = 3; |
| 35 | x; |
| 36 | } |
| 37 | } |
| 38 | }, ReferenceError); |
| 39 | |
| 40 | // Ensure that inner functions and eval don't skip hole checks |
| 41 | |
| 42 | function g(x) { |
| 43 | switch (x) { |
| 44 | case 1: |
| 45 | let z; |
| 46 | case 2: |
| 47 | return function() { z = 1; } |
| 48 | case 3: |
| 49 | return function() { return z; } |
| 50 | case 4: |
| 51 | return eval("z = 1"); |
| 52 | case 5: |
| 53 | return eval("z"); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | assertEquals(undefined, g(1)()); |
| 58 | assertThrows(g(2), ReferenceError); |
| 59 | assertThrows(g(3), ReferenceError); |
| 60 | assertThrows(function () {g(4)}, ReferenceError); |
| 61 | assertThrows(function () {g(5)}, ReferenceError); |
| 62 | |
| 63 | // Ensure the same in strict mode, with different eval and function semantics |
| 64 | |
| 65 | function h(x) { |
| 66 | 'use strict' |
| 67 | switch (x) { |
| 68 | case 1: |
| 69 | let z; |
| 70 | case 2: |
| 71 | return function() { z = 1; } |
| 72 | case 3: |
| 73 | return function() { return z; } |
| 74 | case 4: |
| 75 | return eval("z = 1"); |
| 76 | case 5: |
| 77 | return eval("z"); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | assertEquals(undefined, h(1)()); |
| 82 | assertThrows(h(2), ReferenceError); |
| 83 | assertThrows(h(3), ReferenceError); |
| 84 | assertThrows(function () {h(4)}, ReferenceError); |
| 85 | assertThrows(function () {h(5)}, ReferenceError); |