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 | |
| 5 | // Flags: --allow-natives-syntax |
| 6 | |
| 7 | var obj = %GetUndetectable(); |
| 8 | |
| 9 | function shouldNotBeTaken() { |
| 10 | fail("Undetectable branch should not be taken", "branch was taken"); |
| 11 | } |
| 12 | |
| 13 | function shouldBeTaken() { |
| 14 | fail("Inverted Undetectable branch should be taken", "branch was not taken"); |
| 15 | } |
| 16 | |
| 17 | function testCompares() { |
| 18 | assertTrue(!obj); |
| 19 | assertFalse(!!obj); |
| 20 | assertFalse(obj == true); |
| 21 | assertFalse(obj == false); |
| 22 | assertFalse(obj === true); |
| 23 | assertFalse(obj === false); |
| 24 | assertEquals(2, obj ? 1 : 2); |
| 25 | assertEquals(obj, true && obj); |
| 26 | assertEquals(obj, false || obj); |
| 27 | } |
| 28 | |
| 29 | function testIfs() { |
| 30 | if (obj) { |
| 31 | shouldNotBeTaken(); |
| 32 | } |
| 33 | |
| 34 | if (obj) { |
| 35 | shouldNotBeTaken(); |
| 36 | } else { |
| 37 | // do nothing |
| 38 | } |
| 39 | |
| 40 | if (!obj) { |
| 41 | // do nothing |
| 42 | } else { |
| 43 | shouldBeTaken(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | function testWhiles() { |
| 48 | while (obj) { |
| 49 | shouldNotBeTaken(); |
| 50 | } |
| 51 | |
| 52 | var i = 0; |
| 53 | while (!obj) { |
| 54 | i++; |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | assertEquals(1, i); |
| 59 | } |
| 60 | |
| 61 | function testFors() { |
| 62 | for (var i = 0; obj; i++) { |
| 63 | shouldNotBeTaken(); |
| 64 | } |
| 65 | |
| 66 | var j = 0; |
| 67 | for (var i = 0; !obj; i++) { |
| 68 | j++; |
| 69 | break; |
| 70 | } |
| 71 | |
| 72 | assertEquals(1, j); |
| 73 | } |
| 74 | |
| 75 | for (var j = 0; j < 5; j++) { |
| 76 | testCompares(); |
| 77 | testIfs(); |
| 78 | testWhiles(); |
| 79 | testFors(); |
| 80 | |
| 81 | if (j == 3) { |
| 82 | %OptimizeFunctionOnNextCall(testCompares); |
| 83 | %OptimizeFunctionOnNextCall(testIfs); |
| 84 | %OptimizeFunctionOnNextCall(testWhiles); |
| 85 | %OptimizeFunctionOnNextCall(testFors); |
| 86 | } |
| 87 | } |