 Chromium Code Reviews
 Chromium Code Reviews Issue 553413002:
  Array.prototype.{every, filter, find, findIndex, forEach, map, some}: Use fresh primitive wrapper f…  (Closed) 
  Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
    
  
    Issue 553413002:
  Array.prototype.{every, filter, find, findIndex, forEach, map, some}: Use fresh primitive wrapper f…  (Closed) 
  Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge| 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 // When evaluating 'this', primitive values should be coerced to an object. | |
| 
wingo
2014/09/23 18:39:01
previous comment was more correct -- the wrapper h
 
Diego Pino
2014/09/24 10:13:01
Acknowledged.
 | |
| 72 // See ECMA-262, Annex C. | |
| 73 a = []; | |
| 74 [1, 2].filter(function() { a.push(this) }, ""); | |
| 75 assertTrue(a[0] !== a[1]); | |
| 76 | |
| 77 // When evaluating 'this', non-primitive values are not coerced to an object. | |
| 78 a = []; | |
| 79 [1, 2].filter(function() { a.push(this) }, {}); | |
| 80 assertFalse(a[0] !== a[1]); | |
| 81 | |
| 82 // In strict mode, when evaluating 'this', primitive values should not be | |
| 83 // coerced to an object. | |
| 84 a = []; | |
| 85 [1, 2].filter(function() { 'use strict'; a.push(this); }, ""); | |
| 
wingo
2014/09/23 18:39:01
great test.
 | |
| 86 assertTrue(a[0] === "" && a[0] === a[1]); | |
| 87 | |
| 71 })(); | 88 })(); | 
| 72 | 89 | 
| 73 | 90 | 
| 74 // | 91 // | 
| 75 // Array.prototype.forEach | 92 // Array.prototype.forEach | 
| 76 // | 93 // | 
| 77 (function() { | 94 (function() { | 
| 78 // Simple use. | 95 // Simple use. | 
| 79 var a = [0,1]; | 96 var a = [0,1]; | 
| 80 var count = 0; | 97 var count = 0; | 
| (...skipping 21 matching lines...) Expand all Loading... | |
| 102 assertEquals(2, count); | 119 assertEquals(2, count); | 
| 103 assertArrayEquals([1,1,2,2], a); | 120 assertArrayEquals([1,1,2,2], a); | 
| 104 | 121 | 
| 105 // Respect holes. | 122 // Respect holes. | 
| 106 a = new Array(20); | 123 a = new Array(20); | 
| 107 count = 0; | 124 count = 0; | 
| 108 a[15] = 2; | 125 a[15] = 2; | 
| 109 a.forEach(function(n) { count++; }); | 126 a.forEach(function(n) { count++; }); | 
| 110 assertEquals(1, count); | 127 assertEquals(1, count); | 
| 111 | 128 | 
| 129 // When evaluating 'this', primitive values should be coerced to an object. | |
| 130 // See ECMA-262, Annex C. | |
| 131 a = []; | |
| 132 [1, 2].forEach(function() { a.push(this) }, ""); | |
| 133 assertTrue(a[0] !== a[1]); | |
| 134 | |
| 135 // When evaluating 'this', non-primitive values are not coerced to an object. | |
| 136 a = []; | |
| 137 [1, 2].forEach(function() { a.push(this) }, {}); | |
| 138 assertFalse(a[0] !== a[1]); | |
| 139 | |
| 140 // In strict mode, when evaluating 'this', primitive values should not be | |
| 141 // coerced to an object. | |
| 142 a = []; | |
| 143 [1, 2].forEach(function() { 'use strict'; a.push(this); }, ""); | |
| 144 assertTrue(a[0] === "" && a[0] === a[1]); | |
| 145 | |
| 112 })(); | 146 })(); | 
| 113 | 147 | 
| 114 | 148 | 
| 115 // | 149 // | 
| 116 // Array.prototype.every | 150 // Array.prototype.every | 
| 117 // | 151 // | 
| 118 (function() { | 152 (function() { | 
| 119 // Simple use. | 153 // Simple use. | 
| 120 var a = [0,1]; | 154 var a = [0,1]; | 
| 121 assertFalse(a.every(function(n) { return n == 0 })); | 155 assertFalse(a.every(function(n) { return n == 0 })); | 
| (...skipping 20 matching lines...) Expand all Loading... | |
| 142 assertArrayEquals([1,1,2,2], a); | 176 assertArrayEquals([1,1,2,2], a); | 
| 143 | 177 | 
| 144 // Respect holes. | 178 // Respect holes. | 
| 145 a = new Array(20); | 179 a = new Array(20); | 
| 146 var count = 0; | 180 var count = 0; | 
| 147 a[2] = 2; | 181 a[2] = 2; | 
| 148 a[15] = 2; | 182 a[15] = 2; | 
| 149 assertTrue(a.every(function(n) { count++; return n == 2; })); | 183 assertTrue(a.every(function(n) { count++; return n == 2; })); | 
| 150 assertEquals(2, count); | 184 assertEquals(2, count); | 
| 151 | 185 | 
| 186 // When evaluating 'this', primitive values should be coerced to an object. | |
| 187 // See ECMA-262, Annex C. | |
| 188 a = []; | |
| 189 [1, 2].every(function() { a.push(this); return true; }, ""); | |
| 190 assertTrue(a[0] !== a[1]); | |
| 191 | |
| 192 // When evaluating 'this', non-primitive values are not coerced to an object. | |
| 193 a = []; | |
| 194 [1, 2].every(function() { a.push(this); return true; }, {}); | |
| 195 assertFalse(a[0] !== a[1]); | |
| 196 | |
| 197 // In strict mode, when evaluating 'this', primitive values should not be | |
| 198 // coerced to an object. | |
| 199 a = []; | |
| 200 [1, 2].every(function() { 'use strict'; a.push(this); return true; }, ""); | |
| 201 assertTrue(a[0] === "" && a[0] === a[1]); | |
| 202 | |
| 152 })(); | 203 })(); | 
| 153 | 204 | 
| 154 // | 205 // | 
| 155 // Array.prototype.map | 206 // Array.prototype.map | 
| 156 // | 207 // | 
| 157 (function() { | 208 (function() { | 
| 158 var a = [0,1,2,3,4]; | 209 var a = [0,1,2,3,4]; | 
| 159 | 210 | 
| 160 // Simple use. | 211 // Simple use. | 
| 161 var result = [1,2,3,4,5]; | 212 var result = [1,2,3,4,5]; | 
| (...skipping 17 matching lines...) Expand all Loading... | |
| 179 result = [1,2,3,4,5]; | 230 result = [1,2,3,4,5]; | 
| 180 assertArrayEquals(result, a.map(function(n, index, array) { array.push(n); ret urn n + 1;})); | 231 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); | 232 assertArrayEquals([0,1,2,3,4,0,1,2,3,4], a); | 
| 182 | 233 | 
| 183 // Respect holes. | 234 // Respect holes. | 
| 184 a = new Array(20); | 235 a = new Array(20); | 
| 185 a[15] = 2; | 236 a[15] = 2; | 
| 186 a = a.map(function(n) { return 2*n; }); | 237 a = a.map(function(n) { return 2*n; }); | 
| 187 for (var i in a) assertEquals(4, a[i]); | 238 for (var i in a) assertEquals(4, a[i]); | 
| 188 | 239 | 
| 240 // When evaluating 'this', primitive values should be coerced to an object. | |
| 241 // See ECMA-262, Annex C. | |
| 242 a = []; | |
| 243 [1, 2].map(function() { a.push(this) }, ""); | |
| 244 assertTrue(a[0] !== a[1]); | |
| 245 | |
| 246 // When evaluating 'this', non-primitive values are not coerced to an object. | |
| 247 a = []; | |
| 248 [1, 2].map(function() { a.push(this) }, {}); | |
| 249 assertFalse(a[0] !== a[1]); | |
| 250 | |
| 251 // In strict mode, when evaluating 'this', primitive values should not be | |
| 252 // coerced to an object. | |
| 253 a = []; | |
| 254 [1, 2].map(function() { 'use strict'; a.push(this); }, ""); | |
| 255 assertTrue(a[0] === "" && a[0] === a[1]); | |
| 256 | |
| 189 })(); | 257 })(); | 
| 190 | 258 | 
| 191 // | 259 // | 
| 192 // Array.prototype.some | 260 // Array.prototype.some | 
| 193 // | 261 // | 
| 194 (function() { | 262 (function() { | 
| 195 var a = [0,1,2,3,4]; | 263 var a = [0,1,2,3,4]; | 
| 196 | 264 | 
| 197 // Simple use. | 265 // Simple use. | 
| 198 assertTrue(a.some(function(n) { return n == 3})); | 266 assertTrue(a.some(function(n) { return n == 3})); | 
| (...skipping 18 matching lines...) Expand all Loading... | |
| 217 | 285 | 
| 218 // Respect holes. | 286 // Respect holes. | 
| 219 a = new Array(20); | 287 a = new Array(20); | 
| 220 var count = 0; | 288 var count = 0; | 
| 221 a[2] = 42; | 289 a[2] = 42; | 
| 222 a[10] = 2; | 290 a[10] = 2; | 
| 223 a[15] = 42; | 291 a[15] = 42; | 
| 224 assertTrue(a.some(function(n) { count++; return n == 2; })); | 292 assertTrue(a.some(function(n) { count++; return n == 2; })); | 
| 225 assertEquals(2, count); | 293 assertEquals(2, count); | 
| 226 | 294 | 
| 295 // When evaluating 'this', primitive values should be coerced to an object. | |
| 296 // See ECMA-262, Annex C. | |
| 297 a = []; | |
| 298 [1, 2].some(function() { a.push(this) }, ""); | |
| 299 assertTrue(a[0] !== a[1]); | |
| 300 | |
| 301 // When evaluating 'this', non-primitive values are not coerced to an object. | |
| 302 a = []; | |
| 303 [1, 2].some(function() { a.push(this) }, {}); | |
| 304 assertFalse(a[0] !== a[1]); | |
| 305 | |
| 306 // In strict mode, when evaluating 'this', primitive values should not be | |
| 307 // coerced to an object. | |
| 308 a = []; | |
| 309 [1, 2].some(function() { 'use strict'; a.push(this); }, ""); | |
| 310 assertTrue(a[0] === "" && a[0] === a[1]); | |
| 311 | |
| 227 })(); | 312 })(); | 
| OLD | NEW |