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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 // Respect holes. | 61 // Respect holes. |
62 a = new Array(20); | 62 a = new Array(20); |
63 var count = 0; | 63 var count = 0; |
64 a[2] = 2; | 64 a[2] = 2; |
65 a[15] = 2; | 65 a[15] = 2; |
66 a[17] = 4; | 66 a[17] = 4; |
67 var a = a.filter(function(n) { count++; return n == 2; }); | 67 var a = a.filter(function(n) { count++; return n == 2; }); |
68 assertEquals(3, count); | 68 assertEquals(3, count); |
69 for (var i in a) assertEquals(2, a[i]); | 69 for (var i in a) assertEquals(2, a[i]); |
70 | 70 |
| 71 // Create a new object in each function call when receiver is a |
| 72 // primitive value. See ECMA-262, Annex C. |
| 73 a = []; |
| 74 [1, 2].filter(function() { a.push(this) }, ""); |
| 75 assertTrue(a[0] !== a[1]); |
| 76 |
| 77 // Do not create a new object otherwise. |
| 78 a = []; |
| 79 [1, 2].filter(function() { a.push(this) }, {}); |
| 80 assertFalse(a[0] !== a[1]); |
| 81 |
| 82 // In strict mode primitive values should not be coerced to an object. |
| 83 a = []; |
| 84 [1, 2].filter(function() { 'use strict'; a.push(this); }, ""); |
| 85 assertTrue(a[0] === "" && a[0] === a[1]); |
| 86 |
71 })(); | 87 })(); |
72 | 88 |
73 | 89 |
74 // | 90 // |
75 // Array.prototype.forEach | 91 // Array.prototype.forEach |
76 // | 92 // |
77 (function() { | 93 (function() { |
78 // Simple use. | 94 // Simple use. |
79 var a = [0,1]; | 95 var a = [0,1]; |
80 var count = 0; | 96 var count = 0; |
(...skipping 21 matching lines...) Expand all Loading... |
102 assertEquals(2, count); | 118 assertEquals(2, count); |
103 assertArrayEquals([1,1,2,2], a); | 119 assertArrayEquals([1,1,2,2], a); |
104 | 120 |
105 // Respect holes. | 121 // Respect holes. |
106 a = new Array(20); | 122 a = new Array(20); |
107 count = 0; | 123 count = 0; |
108 a[15] = 2; | 124 a[15] = 2; |
109 a.forEach(function(n) { count++; }); | 125 a.forEach(function(n) { count++; }); |
110 assertEquals(1, count); | 126 assertEquals(1, count); |
111 | 127 |
| 128 // Create a new object in each function call when receiver is a |
| 129 // primitive value. See ECMA-262, Annex C. |
| 130 a = []; |
| 131 [1, 2].forEach(function() { a.push(this) }, ""); |
| 132 assertTrue(a[0] !== a[1]); |
| 133 |
| 134 // Do not create a new object otherwise. |
| 135 a = []; |
| 136 [1, 2].forEach(function() { a.push(this) }, {}); |
| 137 assertFalse(a[0] !== a[1]); |
| 138 |
| 139 // In strict mode primitive values should not be coerced to an object. |
| 140 a = []; |
| 141 [1, 2].forEach(function() { 'use strict'; a.push(this); }, ""); |
| 142 assertTrue(a[0] === "" && a[0] === a[1]); |
| 143 |
112 })(); | 144 })(); |
113 | 145 |
114 | 146 |
115 // | 147 // |
116 // Array.prototype.every | 148 // Array.prototype.every |
117 // | 149 // |
118 (function() { | 150 (function() { |
119 // Simple use. | 151 // Simple use. |
120 var a = [0,1]; | 152 var a = [0,1]; |
121 assertFalse(a.every(function(n) { return n == 0 })); | 153 assertFalse(a.every(function(n) { return n == 0 })); |
(...skipping 20 matching lines...) Expand all Loading... |
142 assertArrayEquals([1,1,2,2], a); | 174 assertArrayEquals([1,1,2,2], a); |
143 | 175 |
144 // Respect holes. | 176 // Respect holes. |
145 a = new Array(20); | 177 a = new Array(20); |
146 var count = 0; | 178 var count = 0; |
147 a[2] = 2; | 179 a[2] = 2; |
148 a[15] = 2; | 180 a[15] = 2; |
149 assertTrue(a.every(function(n) { count++; return n == 2; })); | 181 assertTrue(a.every(function(n) { count++; return n == 2; })); |
150 assertEquals(2, count); | 182 assertEquals(2, count); |
151 | 183 |
| 184 // Create a new object in each function call when receiver is a |
| 185 // primitive value. See ECMA-262, Annex C. |
| 186 a = []; |
| 187 [1, 2].every(function() { a.push(this); return true; }, ""); |
| 188 assertTrue(a[0] !== a[1]); |
| 189 |
| 190 // Do not create a new object otherwise. |
| 191 a = []; |
| 192 [1, 2].every(function() { a.push(this); return true; }, {}); |
| 193 assertFalse(a[0] !== a[1]); |
| 194 |
| 195 // In strict mode primitive values should not be coerced to an object. |
| 196 a = []; |
| 197 [1, 2].every(function() { 'use strict'; a.push(this); return true; }, ""); |
| 198 assertTrue(a[0] === "" && a[0] === a[1]); |
| 199 |
152 })(); | 200 })(); |
153 | 201 |
154 // | 202 // |
155 // Array.prototype.map | 203 // Array.prototype.map |
156 // | 204 // |
157 (function() { | 205 (function() { |
158 var a = [0,1,2,3,4]; | 206 var a = [0,1,2,3,4]; |
159 | 207 |
160 // Simple use. | 208 // Simple use. |
161 var result = [1,2,3,4,5]; | 209 var result = [1,2,3,4,5]; |
(...skipping 17 matching lines...) Expand all Loading... |
179 result = [1,2,3,4,5]; | 227 result = [1,2,3,4,5]; |
180 assertArrayEquals(result, a.map(function(n, index, array) { array.push(n); ret
urn n + 1;})); | 228 assertArrayEquals(result, a.map(function(n, index, array) { array.push(n); ret
urn n + 1;})); |
181 assertArrayEquals([0,1,2,3,4,0,1,2,3,4], a); | 229 assertArrayEquals([0,1,2,3,4,0,1,2,3,4], a); |
182 | 230 |
183 // Respect holes. | 231 // Respect holes. |
184 a = new Array(20); | 232 a = new Array(20); |
185 a[15] = 2; | 233 a[15] = 2; |
186 a = a.map(function(n) { return 2*n; }); | 234 a = a.map(function(n) { return 2*n; }); |
187 for (var i in a) assertEquals(4, a[i]); | 235 for (var i in a) assertEquals(4, a[i]); |
188 | 236 |
| 237 // Create a new object in each function call when receiver is a |
| 238 // primitive value. See ECMA-262, Annex C. |
| 239 a = []; |
| 240 [1, 2].map(function() { a.push(this) }, ""); |
| 241 assertTrue(a[0] !== a[1]); |
| 242 |
| 243 // Do not create a new object otherwise. |
| 244 a = []; |
| 245 [1, 2].map(function() { a.push(this) }, {}); |
| 246 assertFalse(a[0] !== a[1]); |
| 247 |
| 248 // In strict mode primitive values should not be coerced to an object. |
| 249 a = []; |
| 250 [1, 2].map(function() { 'use strict'; a.push(this); }, ""); |
| 251 assertTrue(a[0] === "" && a[0] === a[1]); |
| 252 |
189 })(); | 253 })(); |
190 | 254 |
191 // | 255 // |
192 // Array.prototype.some | 256 // Array.prototype.some |
193 // | 257 // |
194 (function() { | 258 (function() { |
195 var a = [0,1,2,3,4]; | 259 var a = [0,1,2,3,4]; |
196 | 260 |
197 // Simple use. | 261 // Simple use. |
198 assertTrue(a.some(function(n) { return n == 3})); | 262 assertTrue(a.some(function(n) { return n == 3})); |
(...skipping 18 matching lines...) Expand all Loading... |
217 | 281 |
218 // Respect holes. | 282 // Respect holes. |
219 a = new Array(20); | 283 a = new Array(20); |
220 var count = 0; | 284 var count = 0; |
221 a[2] = 42; | 285 a[2] = 42; |
222 a[10] = 2; | 286 a[10] = 2; |
223 a[15] = 42; | 287 a[15] = 42; |
224 assertTrue(a.some(function(n) { count++; return n == 2; })); | 288 assertTrue(a.some(function(n) { count++; return n == 2; })); |
225 assertEquals(2, count); | 289 assertEquals(2, count); |
226 | 290 |
| 291 // Create a new object in each function call when receiver is a |
| 292 // primitive value. See ECMA-262, Annex C. |
| 293 a = []; |
| 294 [1, 2].some(function() { a.push(this) }, ""); |
| 295 assertTrue(a[0] !== a[1]); |
| 296 |
| 297 // Do not create a new object otherwise. |
| 298 a = []; |
| 299 [1, 2].some(function() { a.push(this) }, {}); |
| 300 assertFalse(a[0] !== a[1]); |
| 301 |
| 302 // In strict mode primitive values should not be coerced to an object. |
| 303 a = []; |
| 304 [1, 2].some(function() { 'use strict'; a.push(this); }, ""); |
| 305 assertTrue(a[0] === "" && a[0] === a[1]); |
| 306 |
227 })(); | 307 })(); |
OLD | NEW |