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