undetectable.js 1.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --allow-natives-syntax

var obj = %GetUndetectable();

function shouldNotBeTaken() {
  fail("Undetectable branch should not be taken", "branch was taken");
}

function shouldBeTaken() {
  fail("Inverted Undetectable branch should be taken", "branch was not taken");
}

function testCompares() {
  assertTrue(!obj);
  assertFalse(!!obj);
  assertFalse(obj == true);
  assertFalse(obj == false);
  assertFalse(obj === true);
  assertFalse(obj === false);
  assertEquals(2, obj ? 1 : 2);
  assertEquals(obj, true && obj);
  assertEquals(obj, false || obj);
}

function testIfs() {
  if (obj) {
    shouldNotBeTaken();
  }

  if (obj) {
    shouldNotBeTaken();
  } else {
    // do nothing
  }

  if (!obj) {
    // do nothing
  } else {
    shouldBeTaken();
  }
}

function testWhiles() {
  while (obj) {
    shouldNotBeTaken();
  }

  var i = 0;
  while (!obj) {
    i++;
    break;
  }

  assertEquals(1, i);
}

function testFors() {
  for (var i = 0; obj; i++) {
    shouldNotBeTaken();
  }

  var j = 0;
  for (var i = 0; !obj; i++) {
    j++;
    break;
  }

  assertEquals(1, j);
}

75 76 77 78
function testCall() {
  obj();
}

79 80 81 82 83
%PrepareFunctionForOptimization(testCompares);
%PrepareFunctionForOptimization(testIfs);
%PrepareFunctionForOptimization(testWhiles);
%PrepareFunctionForOptimization(testFors);
%PrepareFunctionForOptimization(testCall);
84 85 86 87 88
for (var j = 0; j < 5; j++) {
  testCompares();
  testIfs();
  testWhiles();
  testFors();
89
  testCall();
90 91 92 93 94 95

  if (j == 3) {
    %OptimizeFunctionOnNextCall(testCompares);
    %OptimizeFunctionOnNextCall(testIfs);
    %OptimizeFunctionOnNextCall(testWhiles);
    %OptimizeFunctionOnNextCall(testFors);
96
    %OptimizeFunctionOnNextCall(testCall);
97 98
  }
}