osr-from-generator.js 1.9 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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.

// Flags: --allow-natives-syntax

(function TestGeneratorOSRSimple() {
  function* gen1() {
    for (var i = 0; i < 3; ++i) {
10
      if (i == 1) %OptimizeOsr();
11 12 13
    }
    return 23;
  }
14
  %PrepareFunctionForOptimization(gen1);
15 16 17 18
  var g = gen1();
  assertEquals({ value:23, done:true }, g.next());
})();

19
(function TestGeneratorOSRYieldAfterArming() {
20 21
  function* gen2() {
    for (var i = 0; i < 3; ++i) {
22
      if (i == 1) %OptimizeOsr();
23 24 25 26
      yield i;
    }
    return 23;
  }
27
  %PrepareFunctionForOptimization(gen2);
28 29 30 31 32 33 34
  var g = gen2();
  assertEquals({ value:0, done:false }, g.next());
  assertEquals({ value:1, done:false }, g.next());
  assertEquals({ value:2, done:false }, g.next());
  assertEquals({ value:23, done:true }, g.next());
})();

35
(function TestGeneratorOSRYieldBeforeArming() {
36
  function* gen3() {
37 38
    for (var i = 0; i < 3; ++i) {
      yield i;
39
      if (i == 1) %OptimizeOsr();
40 41 42
    }
    return 23;
  }
43
  %PrepareFunctionForOptimization(gen3);
44 45 46 47 48 49 50 51 52
  var g = gen3();
  assertEquals({ value:0, done:false }, g.next());
  assertEquals({ value:1, done:false }, g.next());
  assertEquals({ value:2, done:false }, g.next());
  assertEquals({ value:23, done:true }, g.next());
})();

(function TestGeneratorOSRNested() {
  function* gen4() {
53 54 55
    for (var i = 0; i < 3; ++i) {
      for (var j = 0; j < 3; ++j) {
        for (var k = 0; k < 10; ++k) {
56
          if (k == 5) %OptimizeOsr();
57
        }
58
        %PrepareFunctionForOptimization(gen4);
59 60 61 62 63
      }
      yield i;
    }
    return 23;
  }
64
  %PrepareFunctionForOptimization(gen4);
65
  var g = gen4();
66 67 68 69 70
  assertEquals({ value:0, done:false }, g.next());
  assertEquals({ value:1, done:false }, g.next());
  assertEquals({ value:2, done:false }, g.next());
  assertEquals({ value:23, done:true }, g.next());
})();