1a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// Copyright 2010 the V8 project authors. All rights reserved. 2a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// Redistribution and use in source and binary forms, with or without 3a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// modification, are permitted provided that the following conditions are 4a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// met: 5a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// 6a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// * Redistributions of source code must retain the above copyright 7a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// notice, this list of conditions and the following disclaimer. 8a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// * Redistributions in binary form must reproduce the above 9a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// copyright notice, this list of conditions and the following 10a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// disclaimer in the documentation and/or other materials provided 11a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// with the distribution. 12a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// * Neither the name of Google Inc. nor the names of its 13a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// contributors may be used to endorse or promote products derived 14a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// from this software without specific prior written permission. 15a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// 16a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org 2827bf28851c1fa362a3f7c709871c21dcc9c23ce7ricow@chromium.org// Flags: --allow-natives-syntax 2927bf28851c1fa362a3f7c709871c21dcc9c23ce7ricow@chromium.org 30a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// Test function.caller. 31a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgfunction A() {} 32a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org 33a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgfunction fun(x) { 34a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org if (x == 0) return fun.caller; 35a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org if (x == 1) return gee.caller; 36a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org return 42; 37a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org} 38a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgfunction gee(x) { return this.f(x); } 39a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org 40a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgA.prototype.f = fun; 41a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgA.prototype.g = gee; 42a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org 43a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgvar o = new A(); 44a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org 4527bf28851c1fa362a3f7c709871c21dcc9c23ce7ricow@chromium.orgfor (var i=0; i<5; i++) { 46a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org o.g(i); 47a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org} 4827bf28851c1fa362a3f7c709871c21dcc9c23ce7ricow@chromium.org%OptimizeFunctionOnNextCall(o.g); 49a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgassertEquals(gee, o.g(0)); 50a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgassertEquals(null, o.g(1)); 51a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org 52a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// Test when called from another function. 53a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgfunction hej(x) { 54a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org if (x == 0) return o.g(x); 55a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org if (x == 1) return o.g(x); 56a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org return o.g(x); 57a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org} 58a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org 5927bf28851c1fa362a3f7c709871c21dcc9c23ce7ricow@chromium.orgfor (var j=0; j<5; j++) { 60a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org hej(j); 61a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org} 6227bf28851c1fa362a3f7c709871c21dcc9c23ce7ricow@chromium.org%OptimizeFunctionOnNextCall(hej); 63a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgassertEquals(gee, hej(0)); 64a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgassertEquals(hej, hej(1)); 65a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org 66a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// Test when called from eval. 67a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgfunction from_eval(x) { 68a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org if (x == 0) return eval("o.g(x);"); 69a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org if (x == 1) return eval("o.g(x);"); 70a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org return o.g(x); 71a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org} 72a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org 7327bf28851c1fa362a3f7c709871c21dcc9c23ce7ricow@chromium.orgfor (var j=0; j<5; j++) { 74a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org from_eval(j); 75a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org} 7627bf28851c1fa362a3f7c709871c21dcc9c23ce7ricow@chromium.org%OptimizeFunctionOnNextCall(from_eval); 77a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgassertEquals(gee, from_eval(0)); 78a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgassertEquals(from_eval, from_eval(1)); 79