1 // Copyright 2015 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Flags: --allow-natives-syntax --use-osr --turbo-osr 6 7 "use strict"; 8 9 function nest(body, name, depth) { 10 var header = ""; 11 for (var i = 0; i < depth; i++) { 12 var x = "x" + (i + 1); 13 header += " for(var " + x + " = 0; " + x + " < 2; " + x + " = " + x + " + 1 | 0) {\n"; 14 body = body + "}" 15 } 16 17 return body.replace(new RegExp("function " + name + "\\(\\) {"), 18 "function " + name + "_" + x + "() {\n" + header); 19 } 20 21 function test(expected, func, depth) { 22 assertEquals(expected, func()); 23 assertEquals(expected, func()); 24 assertEquals(expected, func()); 25 26 var orig = func.toString(); 27 var name = func.name; 28 for (var depth = 1; depth < 4; depth++) { 29 var body = nest(orig, name, depth); 30 func = eval("(" + body + ")"); 31 32 assertEquals(expected, func()); 33 assertEquals(expected, func()); 34 assertEquals(expected, func()); 35 } 36 } 37 38 function foo() { 39 var result; 40 { 41 let sum = 0; 42 for (var i = 0; i < 10; i++) { 43 %OptimizeOsr(); 44 sum += i; 45 } 46 result = sum; 47 } 48 return result; 49 } 50 51 test(45, foo); 52 53 function bar() { 54 let sum = 0; 55 for (var i = 0; i < 10; i++) { 56 %OptimizeOsr(); 57 sum += i; 58 } 59 return sum; 60 } 61 62 test(45, bar); 63 64 function bon() { 65 { 66 let sum = 0; 67 for (var i = 0; i < 10; i++) { 68 if (i == 5) %OptimizeOsr(); 69 sum += i; 70 } 71 return sum; 72 } 73 } 74 75 test(45, bon); 76 77 function row() { 78 var i = 0; 79 { 80 let sum = 0; 81 while (true) { 82 if (i == 8) return sum; 83 %OptimizeOsr(); 84 sum = i; 85 i = i + 1 | 0; 86 } 87 } 88 return 11; 89 } 90 91 test(7, row); 92 93 function nub() { 94 let i = 0; 95 while (i < 2) { 96 %OptimizeOsr(); 97 i++; 98 } 99 return i; 100 } 101 102 test(2, nub); 103 104 function kub() { 105 var result = 0; 106 let i = 0; 107 while (i < 2) { 108 let x = i; 109 %OptimizeOsr(); 110 i++; 111 result = x; 112 } 113 return result; 114 } 115 116 test(1, kub); 117