constructor-inlining.js 2.7 KB
Newer Older
1 2 3 4
// Copyright 2017 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 --stress-inline
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

var counter = 0;
var deopt_at = -1;

class Base {
  constructor(use, x){
    if (deopt_at-- == 0) {
        %_DeoptimizeNow();
        %DeoptimizeFunction(testConstructorInlining);
    }
    counter++;
    this.x = x;
    if (use) {
      return x;
    }
  }
}

class Derived extends Base {
  constructor(use, x, y, deopt = false) {
    super(use, x);
    counter++;
    if (deopt_at-- == 0) %_DeoptimizeNow();
    this.y = y;
    if (use) {
      return y;
    }
  }
}

var DerivedDeoptCreate =  new Proxy(Derived, {
  get: function(target, name) {
    if (name=='prototype') {
      counter++;
      if (deopt_at-- == 0) %DeoptimizeFunction(Derived);
    }
    return target[name];
  }
});

function Constr(use, x){
  counter++;
  if (deopt_at-- == 0) %_DeoptimizeNow();
  this.x = x;
  if (use) {
    return x;
  }
}


var a = {};
var b = {};

function testConstructorInlining(){
  assertEquals(a, new Constr(true, a));
  assertEquals(7, new Constr(false, 7).x);
  assertEquals(5, new Constr(true, 5).x);

  assertEquals(a, new Base(true, a));
  assertEquals(7, new Base(false, 7).x);
66
  assertEquals(5, new Base(true, 5).x);
67 68 69 70 71 72 73 74 75 76 77

  assertEquals(b, new Derived(true, a, b));
  assertEquals(a, new Derived(true, a, undefined));
  assertEquals(5, new Derived(false, 5, 7).x);
  assertEquals(7, new Derived(false, 5, 7).y);
  try {
    new Derived(true, a, 7)
    assertTrue(false);
  } catch (e) {
    if (!(e instanceof TypeError)) throw e;
  }
78
  assertEquals(a, new Derived(true, 5, a));
79

80
  %PrepareFunctionForOptimization(Derived);
81 82
  %OptimizeFunctionOnNextCall(Derived);
  assertEquals(b, new DerivedDeoptCreate(true, a, b));
83
  %PrepareFunctionForOptimization(Derived);
84 85
  %OptimizeFunctionOnNextCall(Derived);
  assertEquals(a, new DerivedDeoptCreate(true, a, undefined));
86
  %PrepareFunctionForOptimization(Derived);
87 88
  %OptimizeFunctionOnNextCall(Derived);
  assertEquals(5, new DerivedDeoptCreate(false, 5, 7).x);
89
  %PrepareFunctionForOptimization(Derived);
90 91 92 93 94
  %OptimizeFunctionOnNextCall(Derived);
  assertEquals(7, new DerivedDeoptCreate(false, 5, 7).y);
}

testConstructorInlining();
95
%PrepareFunctionForOptimization(testConstructorInlining);
96 97 98 99 100 101 102
%OptimizeFunctionOnNextCall(testConstructorInlining);
testConstructorInlining();

var last = undefined;
for(var i = 0; deopt_at < 0; ++i) {
  deopt_at = i;
  counter = 0;
103
  %PrepareFunctionForOptimization(testConstructorInlining);
104 105 106 107 108 109 110
  %OptimizeFunctionOnNextCall(testConstructorInlining);
  testConstructorInlining();
  if (last !== undefined) {
    assertEquals(counter, last)
  }
  last = counter;
}