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