1// Copyright 2013 the V8 project authors. All rights reserved. 2// Redistribution and use in source and binary forms, with or without 3// modification, are permitted provided that the following conditions are 4// met: 5// 6// * Redistributions of source code must retain the above copyright 7// notice, this list of conditions and the following disclaimer. 8// * Redistributions in binary form must reproduce the above 9// copyright notice, this list of conditions and the following 10// disclaimer in the documentation and/or other materials provided 11// with the distribution. 12// * Neither the name of Google Inc. nor the names of its 13// contributors may be used to endorse or promote products derived 14// from this software without specific prior written permission. 15// 16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28// Flags: --allow-natives-syntax --use-escape-analysis --expose-gc 29 30 31// Simple test of capture 32(function testCapturedArguments() { 33 function h() { 34 return g.arguments[0]; 35 } 36 37 function g(x) { 38 return h(); 39 } 40 41 function f() { 42 var l = { y : { z : 4 }, x : 2 } 43 var r = g(l); 44 assertEquals(2, r.x); 45 assertEquals(2, l.x); 46 l.x = 3; 47 l.y.z = 5; 48 // Test that the arguments object is properly 49 // aliased 50 assertEquals(3, r.x); 51 assertEquals(3, l.x); 52 assertEquals(5, r.y.z); 53 } 54 55 f(); f(); f(); 56 %OptimizeFunctionOnNextCall(f); 57 f(); 58})(); 59 60 61// Get the arguments object twice, test aliasing 62(function testTwoCapturedArguments() { 63 function h() { 64 return g.arguments[0]; 65 } 66 67 function i() { 68 return g.arguments[0]; 69 } 70 71 function g(x) { 72 return {h : h() , i : i()}; 73 } 74 75 function f() { 76 var l = { y : { z : 4 }, x : 2 } 77 var r = g(l); 78 assertEquals(2, r.h.x) 79 l.y.z = 3; 80 assertEquals(3, r.h.y.z); 81 assertEquals(3, r.i.y.z); 82 } 83 84 f(); f(); f(); 85 %OptimizeFunctionOnNextCall(f); 86 f(); 87})(); 88 89 90// Nested arguments object test 91(function testTwoCapturedArgumentsNested() { 92 function i() { 93 return { gx : g.arguments[0], hx : h.arguments[0] }; 94 } 95 96 function h(x) { 97 return i(); 98 } 99 100 function g(x) { 101 return h(x.y); 102 } 103 104 function f() { 105 var l = { y : { z : 4 }, x : 2 } 106 var r = g(l); 107 assertEquals(2, r.gx.x) 108 assertEquals(4, r.gx.y.z) 109 assertEquals(4, r.hx.z) 110 l.y.z = 3; 111 assertEquals(3, r.gx.y.z) 112 assertEquals(3, r.hx.z) 113 assertEquals(3, l.y.z) 114 } 115 116 f(); f(); f(); 117 %OptimizeFunctionOnNextCall(f); 118 f(); f(); 119 %OptimizeFunctionOnNextCall(f); 120 f(); f(); 121})(); 122 123 124// Nested arguments object test with different inlining 125(function testTwoCapturedArgumentsNested2() { 126 function i() { 127 return { gx : g.arguments[0], hx : h.arguments[0] }; 128 } 129 130 function h(x) { 131 return i(); 132 } 133 134 function g(x) { 135 return h(x.y); 136 } 137 138 function f() { 139 var l = { y : { z : 4 }, x : 2 } 140 var r = g(l); 141 assertEquals(2, r.gx.x) 142 assertEquals(4, r.gx.y.z) 143 assertEquals(4, r.hx.z) 144 l.y.z = 3; 145 assertEquals(3, r.gx.y.z) 146 assertEquals(3, r.hx.z) 147 assertEquals(3, l.y.z) 148 } 149 150 %NeverOptimizeFunction(i); 151 f(); f(); f(); 152 %OptimizeFunctionOnNextCall(f); 153 f(); f(); 154 %OptimizeFunctionOnNextCall(f); 155 f(); f(); 156})(); 157 158 159// Multiple captured argument test 160(function testTwoArgumentsCapture() { 161 function h() { 162 return { a : g.arguments[1], b : g.arguments[0] }; 163 } 164 165 function g(x, y) { 166 return h(); 167 } 168 169 function f() { 170 var l = { y : { z : 4 }, x : 2 } 171 var k = { t : { u : 3 } }; 172 var r = g(k, l); 173 assertEquals(2, r.a.x) 174 assertEquals(4, r.a.y.z) 175 assertEquals(3, r.b.t.u) 176 l.y.z = 6; 177 r.b.t.u = 7; 178 assertEquals(6, r.a.y.z) 179 assertEquals(7, k.t.u) 180 } 181 182 f(); f(); f(); 183 %OptimizeFunctionOnNextCall(f); 184 f(); f(); 185 %OptimizeFunctionOnNextCall(f); 186 f(); f(); 187})(); 188