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 28a9aa5fa8ae2a2b43a94e6462ded2cd51445e9ee3ager@chromium.org// Flags: --allow-natives-syntax 29a9aa5fa8ae2a2b43a94e6462ded2cd51445e9ee3ager@chromium.org 30a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org// Test that we can inline a call with a parameter. 31a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgfunction TestInlineOneParam(o, p) { 32a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org // Effect context. 33a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org o.f(p); 34a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org // Value context. 35a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org var x = o.f(p); 36a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org assertEquals(42, x); 37a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org assertEquals(42, o.f(p)); 38a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org // Test context. 39a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org if (!o.f(p)) { 40a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org assertTrue(false); // Should not happen. 41a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org } 42a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org} 43a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org 44a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgvar obj = {x:42}; 45a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgvar o1 = {}; 46a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgo1.f = function(o) { return o.x; }; 47a9aa5fa8ae2a2b43a94e6462ded2cd51445e9ee3ager@chromium.orgfor (var i = 0; i < 5; i++) TestInlineOneParam(o1, obj); 48a9aa5fa8ae2a2b43a94e6462ded2cd51445e9ee3ager@chromium.org%OptimizeFunctionOnNextCall(TestInlineOneParam); 49a9aa5fa8ae2a2b43a94e6462ded2cd51445e9ee3ager@chromium.orgTestInlineOneParam(o1, obj); 50a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgTestInlineOneParam({f: o1.f}, {x:42}); 51a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org 52a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org 53a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgfunction TestInlineTwoParams(o, p) { 54a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org var y = 43; 55a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org // Effect context. 56a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org o.h(y, y); 57a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org // Value context. 58a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org var x = o.h(p, y); 59a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org assertEquals(true, x); 60a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org assertEquals(false, o.h(y, p)); 61a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org // Test context. 62a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org if (!o.h(p, y)) { 63a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org assertTrue(false); // Should not happen. 64a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org } 65a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org 66a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org // Perform the same tests again, but this time with non-trivial 67a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org // expressions as the parameters. 68a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org 69a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org // Effect context. 70a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org o.h(y + 1, y + 1); 71a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org // Value context. 72a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org var x = o.h(p + 1, y + 1); 73a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org assertEquals(true, x); 74a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org assertEquals(false, o.h(y + 1, p + 1)); 75a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org // Test context. 76a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org if (!o.h(p + 1, y + 1)) { 77a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org assertTrue(false); // Should not happen. 78a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org } 79a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org} 80a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org 81a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgvar o2 = {}; 82a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgo2.h = function(i, j) { return i < j; }; 83a9aa5fa8ae2a2b43a94e6462ded2cd51445e9ee3ager@chromium.orgfor (var i = 0; i < 5; i++) TestInlineTwoParams(o2, 42); 84a9aa5fa8ae2a2b43a94e6462ded2cd51445e9ee3ager@chromium.org%OptimizeFunctionOnNextCall(TestInlineTwoParams); 85a9aa5fa8ae2a2b43a94e6462ded2cd51445e9ee3ager@chromium.orgTestInlineTwoParams(o2, 42); 86a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.orgTestInlineTwoParams({h: o2.h}, 42); 87