OLD | NEW |
---|---|
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 20 matching lines...) Expand all Loading... | |
31 | 31 |
32 function f1(a) { | 32 function f1(a) { |
33 return a; | 33 return a; |
34 } | 34 } |
35 | 35 |
36 assertTrue(this === f0.apply(), "1-0"); | 36 assertTrue(this === f0.apply(), "1-0"); |
37 | 37 |
38 assertTrue(this === f0.apply(this), "2a"); | 38 assertTrue(this === f0.apply(this), "2a"); |
39 assertTrue(this === f0.apply(this, new Array(1)), "2b"); | 39 assertTrue(this === f0.apply(this, new Array(1)), "2b"); |
40 assertTrue(this === f0.apply(this, new Array(2)), "2c"); | 40 assertTrue(this === f0.apply(this, new Array(2)), "2c"); |
41 assertTrue(this === f0.apply(this, new Array(4242)), "2c"); | 41 assertTrue(this === f0.apply(this, new Array(4242)), "2d"); |
42 | 42 |
43 assertTrue(this === f0.apply(null), "3a"); | 43 assertTrue(this === f0.apply(null), "3a"); |
44 assertTrue(this === f0.apply(null, new Array(1)), "3b"); | 44 assertTrue(this === f0.apply(null, new Array(1)), "3b"); |
45 assertTrue(this === f0.apply(null, new Array(2)), "3c"); | 45 assertTrue(this === f0.apply(null, new Array(2)), "3c"); |
46 assertTrue(this === f0.apply(this, new Array(4242)), "2c"); | 46 assertTrue(this === f0.apply(this, new Array(4242)), "3d"); |
47 | 47 |
48 assertTrue(this === f0.apply(void 0), "4a"); | 48 assertTrue(this === f0.apply(void 0), "4a"); |
49 assertTrue(this === f0.apply(void 0, new Array(1)), "4b"); | 49 assertTrue(this === f0.apply(void 0, new Array(1)), "4b"); |
50 assertTrue(this === f0.apply(void 0, new Array(2)), "4c"); | 50 assertTrue(this === f0.apply(void 0, new Array(2)), "4c"); |
51 | 51 |
52 assertTrue(void 0 === f1.apply(), "1-1"); | 52 assertTrue(void 0 === f1.apply(), "1-1"); |
53 | 53 |
54 assertTrue(void 0 === f1.apply(this), "2a"); | 54 assertTrue(void 0 === f1.apply(this), "5a"); |
55 assertTrue(void 0 === f1.apply(this, new Array(1)), "2b"); | 55 assertTrue(void 0 === f1.apply(this, new Array(1)), "5b"); |
56 assertTrue(void 0 === f1.apply(this, new Array(2)), "2c"); | 56 assertTrue(void 0 === f1.apply(this, new Array(2)), "5c"); |
57 assertTrue(void 0 === f1.apply(this, new Array(4242)), "2c"); | 57 assertTrue(void 0 === f1.apply(this, new Array(4242)), "5d"); |
58 assertTrue(42 === f1.apply(this, new Array(42, 43)), "2c"); | 58 assertTrue(42 === f1.apply(this, new Array(42, 43)), "5e"); |
59 assertEquals("foo", f1.apply(this, new Array("foo", "bar", "baz", "boo")), "2c") ; | 59 assertEquals("foo", f1.apply(this, new Array("foo", "bar", "baz", "boo")), |
William Hesse
2009/07/09 07:54:36
Why not just shorten "bar", "baz" and "boo" by a c
| |
60 "5f"); | |
60 | 61 |
61 assertTrue(void 0 === f1.apply(null), "3a"); | 62 assertTrue(void 0 === f1.apply(null), "6a"); |
62 assertTrue(void 0 === f1.apply(null, new Array(1)), "3b"); | 63 assertTrue(void 0 === f1.apply(null, new Array(1)), "6b"); |
63 assertTrue(void 0 === f1.apply(null, new Array(2)), "3c"); | 64 assertTrue(void 0 === f1.apply(null, new Array(2)), "6c"); |
64 assertTrue(void 0 === f1.apply(null, new Array(4242)), "2c"); | 65 assertTrue(void 0 === f1.apply(null, new Array(4242)), "6d"); |
65 assertTrue(42 === f1.apply(null, new Array(42, 43)), "2c"); | 66 assertTrue(42 === f1.apply(null, new Array(42, 43)), "6e"); |
66 assertEquals("foo", f1.apply(null, new Array("foo", "bar", "baz", "boo")), "2c") ; | 67 assertEquals("foo", f1.apply(null, new Array("foo", "bar", "baz", "boo")), |
68 "6f"); | |
67 | 69 |
68 assertTrue(void 0 === f1.apply(void 0), "4a"); | 70 assertTrue(void 0 === f1.apply(void 0), "7a"); |
69 assertTrue(void 0 === f1.apply(void 0, new Array(1)), "4b"); | 71 assertTrue(void 0 === f1.apply(void 0, new Array(1)), "7b"); |
70 assertTrue(void 0 === f1.apply(void 0, new Array(2)), "4c"); | 72 assertTrue(void 0 === f1.apply(void 0, new Array(2)), "7c"); |
71 assertTrue(void 0 === f1.apply(void 0, new Array(4242)), "4c"); | 73 assertTrue(void 0 === f1.apply(void 0, new Array(4242)), "7d"); |
72 assertTrue(42 === f1.apply(void 0, new Array(42, 43)), "2c"); | 74 assertTrue(42 === f1.apply(void 0, new Array(42, 43)), "7e"); |
73 assertEquals("foo", f1.apply(void 0, new Array("foo", "bar", "baz", "boo")), "2c "); | 75 assertEquals("foo", f1.apply(void 0, new Array("foo", "bar", "baz", "boo")), |
76 "7f"); | |
74 | 77 |
75 var arr = new Array(42, "foo", "fish", "horse"); | 78 var arr = new Array(42, "foo", "fish", "horse"); |
76 function j(a, b, c, d, e, f, g, h, i, j, k, l) { | 79 function 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; | 80 return "" + a + b + c + d + e + f + g + h + i + j + k + l; |
78 } | 81 } |
79 | 82 |
80 | 83 |
81 var expect = "42foofishhorse"; | 84 var expect = "42foofishhorse"; |
82 for (var i = 0; i < 8; i++) | 85 for (var i = 0; i < 8; i++) |
83 expect += "undefined"; | 86 expect += "undefined"; |
84 assertEquals(expect, j.apply(undefined, arr)); | 87 assertEquals(expect, j.apply(undefined, arr), "apply to undefined"); |
85 | 88 |
86 assertThrows("f0.apply(this, 1);"); | 89 assertThrows("f0.apply(this, 1);"); |
87 assertThrows("f0.apply(this, 1, 2);"); | 90 assertThrows("f0.apply(this, 1, 2);"); |
88 assertThrows("f0.apply(this, 1, new Array(2));"); | 91 assertThrows("f0.apply(this, 1, new Array(2));"); |
89 | 92 |
90 function f() { | 93 function f() { |
91 var doo = ""; | 94 var doo = ""; |
92 for (var i = 0; i < arguments.length; i++) { | 95 for (var i = 0; i < arguments.length; i++) { |
93 doo += arguments[i]; | 96 doo += arguments[i]; |
94 } | 97 } |
95 return doo; | 98 return doo; |
96 } | 99 } |
97 | 100 |
98 assertEquals("42foofishhorse", f.apply(this, arr)); | 101 assertEquals("42foofishhorse", f.apply(this, arr), "apply to this"); |
99 | 102 |
100 function s() { | 103 function s() { |
101 var doo = this; | 104 var doo = this; |
102 for (var i = 0; i < arguments.length; i++) { | 105 for (var i = 0; i < arguments.length; i++) { |
103 doo += arguments[i]; | 106 doo += arguments[i]; |
104 } | 107 } |
105 return doo; | 108 return doo; |
106 } | 109 } |
107 | 110 |
108 assertEquals("bar42foofishhorse", s.apply("bar", arr)); | 111 assertEquals("bar42foofishhorse", s.apply("bar", arr), "apply to string"); |
109 | 112 |
110 function al() { | 113 function al() { |
111 assertEquals(345, this); | 114 assertEquals(345, this); |
112 return arguments.length + arguments[arguments.length - 1]; | 115 return arguments.length + arguments[arguments.length - 1]; |
113 } | 116 } |
114 | 117 |
115 for (var j = 1; j < 0x40000000; j <<= 1) { | 118 for (var j = 1; j < 0x40000000; j <<= 1) { |
116 try { | 119 try { |
117 var a = new Array(j); | 120 var a = new Array(j); |
118 a[j - 1] = 42; | 121 a[j - 1] = 42; |
119 assertEquals(42 + j, al.apply(345, a)); | 122 assertEquals(42 + j, al.apply(345, a)); |
120 } catch (e) { | 123 } catch (e) { |
121 assertTrue(e.toString().indexOf("Function.prototype.apply") != -1); | 124 assertTrue(e.toString().indexOf("Function.prototype.apply") != -1, |
125 "exception does not contain Function.prototype.apply: " + | |
126 e.toString()); | |
122 for (; j < 0x40000000; j <<= 1) { | 127 for (; j < 0x40000000; j <<= 1) { |
123 var caught = false; | 128 var caught = false; |
124 try { | 129 try { |
125 a = new Array(j); | 130 a = new Array(j); |
126 a[j - 1] = 42; | 131 a[j - 1] = 42; |
127 al.apply(345, a); | 132 al.apply(345, a); |
128 assertEquals("Shouldn't get", "here"); | 133 assertUnreachable("Apply of arrray with length " + a.length + |
134 " should have thrown"); | |
129 } catch (e) { | 135 } catch (e) { |
130 assertTrue(e.toString().indexOf("Function.prototype.apply") != -1); | 136 assertTrue(e.toString().indexOf("Function.prototype.apply") != -1, |
137 "exception does not contain Function.prototype.apply [" + | |
138 "length = " + j + "]: " + e.toString()); | |
131 caught = true; | 139 caught = true; |
132 } | 140 } |
133 assertTrue(caught); | 141 assertTrue(caught, "exception not caught"); |
134 } | 142 } |
135 break; | 143 break; |
136 } | 144 } |
137 } | 145 } |
138 | 146 |
139 var primes = new Array(0); | 147 var primes = new Array(0); |
140 | 148 |
141 function isPrime(possible_prime) { | 149 function isPrime(possible_prime) { |
142 for (var d = 0; d < primes.length; d++) { | 150 for (var d = 0; d < primes.length; d++) { |
143 var p = primes[d]; | 151 var p = primes[d]; |
144 if (possible_prime % p == 0) | 152 if (possible_prime % p == 0) |
145 return false; | 153 return false; |
146 if (p * p > possible_prime) | 154 if (p * p > possible_prime) |
147 return true; | 155 return true; |
148 } | 156 } |
149 return true; | 157 return true; |
150 } | 158 } |
151 | 159 |
152 for (var i = 2; i < 10000; i++) { | 160 for (var i = 2; i < 10000; i++) { |
153 if (isPrime(i)) { | 161 if (isPrime(i)) { |
154 primes.push(i); | 162 primes.push(i); |
155 } | 163 } |
156 } | 164 } |
157 | 165 |
158 assertEquals(1229, primes.length); | 166 assertEquals(1229, primes.length); |
159 | 167 |
160 var same_primes = Array.prototype.constructor.apply(Array, primes); | 168 var same_primes = Array.prototype.constructor.apply(Array, primes); |
161 | 169 |
162 for (var i = 0; i < primes.length; i++) | 170 for (var i = 0; i < primes.length; i++) |
163 assertEquals(primes[i], same_primes[i]); | 171 assertEquals(primes[i], same_primes[i], "prime" + primes[i]); |
164 assertEquals(primes.length, same_primes.length); | 172 assertEquals(primes.length, same_primes.length, "prime-length"); |
165 | 173 |
166 | 174 |
167 Array.prototype["1"] = "sep"; | 175 Array.prototype["1"] = "sep"; |
168 | 176 |
169 var holey = new Array(3); | 177 var holey = new Array(3); |
170 holey[0] = "mor"; | 178 holey[0] = "mor"; |
171 holey[2] = "er"; | 179 holey[2] = "er"; |
172 | 180 |
173 assertEquals("morseper", String.prototype.concat.apply("", holey)); | 181 assertEquals("morseper", String.prototype.concat.apply("", holey), |
174 assertEquals("morseper", String.prototype.concat.apply("", holey, 1)); | 182 "moreseper0"); |
175 assertEquals("morseper", String.prototype.concat.apply("", holey, 1, 2)); | 183 assertEquals("morseper", String.prototype.concat.apply("", holey, 1), |
176 assertEquals("morseper", String.prototype.concat.apply("", holey, 1, 2, 3)); | 184 "moreseper1"); |
177 assertEquals("morseper", String.prototype.concat.apply("", holey, 1, 2, 3, 4)); | 185 assertEquals("morseper", String.prototype.concat.apply("", holey, 1, 2), |
186 "moreseper2"); | |
187 assertEquals("morseper", String.prototype.concat.apply("", holey, 1, 2, 3), | |
188 "morseper3"); | |
189 assertEquals("morseper", String.prototype.concat.apply("", holey, 1, 2, 3, 4), | |
190 "morseper4"); | |
178 | 191 |
179 primes[0] = ""; | 192 primes[0] = ""; |
180 primes[1] = holey; | 193 primes[1] = holey; |
181 assertThrows("String.prototype.concat.apply.apply('foo', primes)"); | 194 assertThrows("String.prototype.concat.apply.apply('foo', primes)"); |
182 assertEquals("morseper", String.prototype.concat.apply.apply(String.prototype.co ncat, primes)); | 195 assertEquals( |
196 "morseper", | |
William Hesse
2009/07/09 07:54:36
I think you can put the first argument on the same
| |
197 String.prototype.concat.apply.apply(String.prototype.concat, primes), | |
198 "moreseper-prime"); | |
183 | 199 |
184 delete(Array.prototype["1"]); | 200 delete(Array.prototype["1"]); |
OLD | NEW |