deopt-array-prototype-includes.js 2.36 KB
Newer Older
1 2 3 4
// Copyright 2018 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.

5
// Flags: --allow-natives-syntax --turbofan
6 7 8 9 10

/* Test deopt behaviors when the prototype has elements */

// includes

11 12 13 14 15 16 17 18
(function() {
  const iarr = [0,1,2];
  const darr = [0.0, 2.0, 3.3];

  function includes(arr, val) {
    return arr.includes(val);
  }

19
  %PrepareFunctionForOptimization(includes);
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  assertTrue(includes(iarr, 0)); assertTrue(includes(darr, 0));
  assertTrue(includes(iarr, 2)); assertTrue(includes(darr, 2));

  // JSCallReducer for includes not reduce because it only works with single map
  %OptimizeFunctionOnNextCall(includes);

  assertTrue(includes(iarr, 0));
  assertTrue(includes(darr, 0));
})();

(function() {
  const iarr = [0,1,2];

  function includes(arr, val) {
    return arr.includes(val);
  }

37
  %PrepareFunctionForOptimization(includes);
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  assertTrue(includes(iarr, 0));
  assertTrue(includes(iarr, 2));

  %OptimizeFunctionOnNextCall(includes);

  assertTrue(includes(iarr, 0));

  const darr = [0.0, 2.0, 3.3];
  // deopt because of map change
  assertTrue(includes(darr, 0));
})();

(function() {
  const iarr = [,3];

  function includes(arr, val) {
    return arr.includes(val);
  }

  iarr.__proto__ = [2];

  // get feedback
60
  %PrepareFunctionForOptimization(includes);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  assertFalse(includes(iarr, 0));
  assertTrue(includes(iarr, 2));

  %OptimizeFunctionOnNextCall(includes);

  assertFalse(includes(iarr, 0));

  assertTrue(includes(iarr, 2));
})();

(function() {
  const iarr = [,3];

  function includes(arr, val) {
    return arr.includes(val);
  }

78
  %PrepareFunctionForOptimization(includes);
79 80 81 82 83 84 85 86 87 88 89 90 91
  assertFalse(includes(iarr, 2));
  assertTrue(includes(iarr, 3));

  %OptimizeFunctionOnNextCall(includes);
  assertFalse(includes(iarr, 2));

  // deopt because of map change
  iarr.__proto__ = [2];
  assertTrue(includes(iarr, 2));
})();

// This pollutes the Array prototype, so we should not run more tests
// in the same environment after this.
92 93 94 95 96 97 98
(function () {
  var array = [,];

  function includes(val) {
    return array.includes(val);
  }

99
  %PrepareFunctionForOptimization(includes);
100 101 102
  includes(6); includes(6);

  %OptimizeFunctionOnNextCall(includes);
103
  assertFalse(includes(6));
104 105

  array.__proto__.push(6);
106 107
  // deopt because of no_elements_protector
  assertTrue(includes(6));
108
})();