1// Copyright 2008 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 28function f0() { 29 return this; 30} 31 32assertTrue(this === f0.call(), "1"); 33 34assertTrue(this === f0.call(this), "w"); 35assertTrue(this === f0.call(this, 1), "w"); 36assertTrue(this === f0.call(this, 1, 2), "w"); 37 38assertTrue(this === f0.call(null), "3a"); 39assertTrue(this === f0.call(null, 1), "3b"); 40assertTrue(this === f0.call(null, 1, 2), "3c"); 41 42assertTrue(this === f0.call(void 0), "4a"); 43assertTrue(this === f0.call(void 0, 1), "4b"); 44assertTrue(this === f0.call(void 0, 1, 2), "4c"); 45 46var x = {}; 47assertTrue(x === f0.call(x)); 48assertTrue(x === f0.call(x, 1)); 49assertTrue(x === f0.call(x, 1, 2)); 50 51 52function f1(a) { 53 a = a || 'i'; 54 return this[a]; 55} 56 57assertEquals(1, f1.call({i:1})); 58assertEquals(42, f1.call({i:42}, 'i')); 59assertEquals(87, f1.call({j:87}, 'j', 1)); 60assertEquals(99, f1.call({k:99}, 'k', 1, 2)); 61 62 63function f2(a, b) { 64 a = a || 'n'; 65 b = b || 2; 66 return this[a] + b; 67} 68 69var x = {n: 1}; 70assertEquals(3, f2.call(x)); 71assertEquals(14, f2.call({i:12}, 'i')); 72assertEquals(42, f2.call(x, 'n', 41)); 73assertEquals(87, f2.call(x, 'n', 86, 1)); 74assertEquals(99, f2.call(x, 'n', 98, 1, 2)); 75 76 77function fn() { 78 return arguments.length; 79} 80 81assertEquals(0, fn.call()); 82assertEquals(0, fn.call(this)); 83assertEquals(0, fn.call(null)); 84assertEquals(0, fn.call(void 0)); 85assertEquals(1, fn.call(this, 1)); 86assertEquals(2, fn.call(this, 1, 2)); 87assertEquals(3, fn.call(this, 1, 2, 3)); 88