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 // Flags: --allow-natives-syntax
     29 
     30 function f0() {
     31   return this;
     32 }
     33 
     34 function f1(a) {
     35   return a;
     36 }
     37 
     38 assertSame(this, f0.apply(), "1-0");
     39 
     40 assertSame(this, f0.apply(this), "2a");
     41 assertSame(this, f0.apply(this, new Array(1)), "2b");
     42 assertSame(this, f0.apply(this, new Array(2)), "2c");
     43 assertSame(this, f0.apply(this, new Array(4242)), "2d");
     44 
     45 assertSame(this, f0.apply(null), "3a");
     46 assertSame(this, f0.apply(null, new Array(1)), "3b");
     47 assertSame(this, f0.apply(null, new Array(2)), "3c");
     48 assertSame(this, f0.apply(this, new Array(4242)), "3d");
     49 
     50 assertSame(this, f0.apply(void 0), "4a");
     51 assertSame(this, f0.apply(void 0, new Array(1)), "4b");
     52 assertSame(this, f0.apply(void 0, new Array(2)), "4c");
     53 
     54 assertEquals(void 0, f1.apply(), "1-1");
     55 
     56 assertEquals(void 0, f1.apply(this), "5a");
     57 assertEquals(void 0, f1.apply(this, new Array(1)), "5b");
     58 assertEquals(void 0, f1.apply(this, new Array(2)), "5c");
     59 assertEquals(void 0, f1.apply(this, new Array(4242)), "5d");
     60 assertEquals(42, f1.apply(this, new Array(42, 43)), "5e");
     61 assertEquals("foo", f1.apply(this, new Array("foo", "bar", "baz", "bo")), "5f");
     62 
     63 assertEquals(void 0, f1.apply(null), "6a");
     64 assertEquals(void 0, f1.apply(null, new Array(1)), "6b");
     65 assertEquals(void 0, f1.apply(null, new Array(2)), "6c");
     66 assertEquals(void 0, f1.apply(null, new Array(4242)), "6d");
     67 assertEquals(42, f1.apply(null, new Array(42, 43)), "6e");
     68 assertEquals("foo", f1.apply(null, new Array("foo", "bar", "baz", "bo")), "6f");
     69 
     70 assertEquals(void 0, f1.apply(void 0), "7a");
     71 assertEquals(void 0, f1.apply(void 0, new Array(1)), "7b");
     72 assertEquals(void 0, f1.apply(void 0, new Array(2)), "7c");
     73 assertEquals(void 0, f1.apply(void 0, new Array(4242)), "7d");
     74 assertEquals(42, f1.apply(void 0, new Array(42, 43)), "7e");
     75 assertEquals("foo", f1.apply(void 0, new Array("foo", "bar", "ba", "b")), "7f");
     76 
     77 var arr = new Array(42, "foo", "fish", "horse");
     78 function j(a, b, c, d, e, f, g, h, i, j, k, l) {
     79   return "" + a + b + c + d + e + f + g + h + i + j + k + l;
     80 }
     81 
     82 
     83 var expect = "42foofishhorse";
     84 for (var i = 0; i < 8; i++)
     85   expect += "undefined";
     86 assertEquals(expect, j.apply(undefined, arr), "apply to undefined");
     87 
     88 assertThrows("f0.apply(this, 1);");
     89 assertThrows("f0.apply(this, 1, 2);");
     90 assertThrows("f0.apply(this, 1, new Array(2));");
     91 
     92 function f() {
     93   var doo = "";
     94   for (var i = 0; i < arguments.length; i++) {
     95     doo += arguments[i];
     96   }
     97   return doo;
     98 }
     99 
    100 assertEquals("42foofishhorse", f.apply(this, arr), "apply to this");
    101 
    102 function s() {
    103   var doo = this;
    104   for (var i = 0; i < arguments.length; i++) {
    105     doo += arguments[i];
    106   }
    107   return doo;
    108 }
    109 
    110 assertEquals("bar42foofishhorse", s.apply("bar", arr), "apply to string");
    111 
    112 function al() {
    113   assertEquals(Object(345), this);
    114   return arguments.length + arguments[arguments.length - 1];
    115 }
    116 
    117 for (var j = 1; j < 0x4000000; j <<= 1) {
    118   try {
    119     var a = %NormalizeElements([]);
    120     a.length = j;
    121     a[j - 1] = 42;
    122     assertEquals(42 + j, al.apply(345, a));
    123   } catch (e) {
    124     assertTrue(e.toString().indexOf("Maximum call stack size exceeded") != -1);
    125     for (; j < 0x4000000; j <<= 1) {
    126       var caught = false;
    127       try {
    128         a = %NormalizeElements([]);
    129         a.length = j;
    130         a[j - 1] = 42;
    131         al.apply(345, a);
    132         assertUnreachable("Apply of array with length " + a.length +
    133                           " should have thrown");
    134       } catch (e) {
    135         assertTrue(e.toString().indexOf("Maximum call stack size exceeded") != -1);
    136         caught = true;
    137       }
    138       assertTrue(caught, "exception not caught");
    139     }
    140     break;
    141   }
    142 }
    143 
    144 var primes = new Array(0);
    145 
    146 function isPrime(possible_prime) {
    147   for (var d = 0; d < primes.length; d++) {
    148     var p = primes[d];
    149     if (possible_prime % p == 0)
    150       return false;
    151     if (p * p > possible_prime)
    152       return true;
    153   }
    154   return true;
    155 }
    156 
    157 for (var i = 2; i < 10000; i++) {
    158   if (isPrime(i)) {
    159     primes.push(i);
    160   }
    161 }
    162 
    163 assertEquals(1229, primes.length);
    164 
    165 var same_primes = Array.prototype.constructor.apply(Array, primes);
    166 
    167 for (var i = 0; i < primes.length; i++)
    168   assertEquals(primes[i], same_primes[i], "prime" + primes[i]);
    169 assertEquals(primes.length, same_primes.length, "prime-length");
    170 
    171 
    172 Array.prototype["1"] = "sep";
    173 
    174 var holey = new Array(3);
    175 holey[0] = "mor";
    176 holey[2] = "er";
    177 
    178 assertEquals("morseper", String.prototype.concat.apply("", holey),
    179              "moreseper0");
    180 assertEquals("morseper", String.prototype.concat.apply("", holey, 1),
    181              "moreseper1");
    182 assertEquals("morseper", String.prototype.concat.apply("", holey, 1, 2),
    183              "moreseper2");
    184 assertEquals("morseper", String.prototype.concat.apply("", holey, 1, 2, 3),
    185              "morseper3");
    186 assertEquals("morseper", String.prototype.concat.apply("", holey, 1, 2, 3, 4),
    187              "morseper4");
    188 
    189 primes[0] = "";
    190 primes[1] = holey;
    191 assertThrows("String.prototype.concat.apply.apply('foo', primes)");
    192 assertEquals("morseper",
    193     String.prototype.concat.apply.apply(String.prototype.concat, primes),
    194     "moreseper-prime");
    195 
    196 delete(Array.prototype["1"]);
    197 
    198 // Check correct handling of non-array argument lists.
    199 assertSame(this, f0.apply(this, {}), "non-array-1");
    200 assertSame(this, f0.apply(this, { length:1 }), "non-array-2");
    201 assertEquals(void 0, f1.apply(this, { length:1 }), "non-array-3");
    202 assertEquals(void 0, f1.apply(this, { 0:"foo" }), "non-array-4");
    203 assertEquals("foo", f1.apply(this, { length:1, 0:"foo" }), "non-array-5");
    204