strong-rooted-literals.js 1.52 KB
Newer Older
1 2 3 4
// Copyright 2016 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 --expose-gc --opt
6 7 8 9 10 11 12 13 14 15

// Make sure literals are strongly rooted and safe from weak-code deopts.

(function() {
  function foo() {
    var a = {y: 0};
    a.y = 1;
    return a;
  }

16
  %PrepareFunctionForOptimization(foo);
17 18
  foo();
  foo();
19
  %OptimizeFunctionOnNextCall(foo);
20 21 22 23 24 25 26 27 28 29
  foo();
  gc();
  assertOptimized(foo);
})();

(function() {
  function hot(o) {
    return o.x + o.y;
  }
  function mapPlus(a, y) {
30 31 32
    var f = (x => hot({x, y}));
    %EnsureFeedbackVectorForFunction(f);
    return a.map(f);
33 34
  }

35 36
  %EnsureFeedbackVectorForFunction(mapPlus);
  %PrepareFunctionForOptimization(hot);
37 38 39
  var a = [1, 2, 3];
  print(mapPlus(a, 1));
  print(mapPlus(a, 2));
40
  %OptimizeFunctionOnNextCall(hot);
41 42 43 44 45 46 47 48 49 50 51
  print(mapPlus(a, 3));
  gc();  // BOOOM!
  assertOptimized(hot);
  print(mapPlus(a, 4));
})();

// Verify that we can handle the creation of a new script, where the
// code is cached and the feedback vector has to be re-created.
(function() {
  var sopen = 'function wrapper() {  ';
  var s1 = 'function foo() { return bar(5); } ';
52 53
  var s2 = '%PrepareFunctionForOptimization(foo); ';
  var s3 = 'foo(); foo(); %OptimizeFunctionOnNextCall(foo); foo(); ';
54
  var sclose = '}  wrapper(); ';
55
  var s = sopen + s1 + s2 + s3 + sclose;
56
  function bar(i){return i + 3};
57
  %EnsureFeedbackVectorForFunction(bar);
58 59 60 61 62

  for (var i = 0; i < 4; i++) {
    eval(s);
  }
})();