Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2011 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 | |
| 29 var should_throw_on_null_and_undefined = | |
| 30 [Object.prototype.toLocaleString, | |
| 31 Object.prototype.valueOf, | |
| 32 Object.prototype.hasOwnProperty, | |
| 33 Object.prototype.isPrototypeOf, | |
| 34 Object.prototype.propertyIsEnumerable, | |
| 35 Array.prototype.toString, | |
| 36 Array.prototype.toLocaleString, | |
| 37 Array.prototype.concat, | |
| 38 Array.prototype.join, | |
| 39 Array.prototype.pop, | |
| 40 Array.prototype.push, | |
| 41 Array.prototype.reverse, | |
| 42 Array.prototype.shift, | |
| 43 Array.prototype.slice, | |
| 44 Array.prototype.sort, | |
| 45 Array.prototype.splice, | |
| 46 Array.prototype.unshift, | |
| 47 Array.prototype.indexOf, | |
| 48 Array.prototype.lastIndexOf, | |
| 49 Array.prototype.every, | |
| 50 Array.prototype.some, | |
| 51 Array.prototype.forEach, | |
| 52 Array.prototype.map, | |
| 53 Array.prototype.filter, | |
| 54 Array.prototype.reduce, | |
| 55 Array.prototype.reduceRight, | |
| 56 String.prototype.charAt, | |
| 57 String.prototype.charCodeAt, | |
| 58 String.prototype.concat, | |
| 59 String.prototype.indexOf, | |
| 60 String.prototype.lastIndexOf, | |
| 61 String.prototype.localeCompare, | |
| 62 String.prototype.match, | |
| 63 String.prototype.replace, | |
| 64 String.prototype.search, | |
| 65 String.prototype.slice, | |
| 66 String.prototype.split, | |
| 67 String.prototype.substring, | |
| 68 String.prototype.toLowerCase, | |
| 69 String.prototype.toLocaleLowerCase, | |
| 70 String.prototype.toUpperCase, | |
| 71 String.prototype.toLocaleUpperCase, | |
| 72 String.prototype.trim, | |
| 73 Number.prototype.toLocaleString, | |
| 74 Error.prototype.toString]; | |
| 75 | |
| 76 // Non generic natives does not work on any input other than the specific | |
| 77 // type, but since this change will allow call to be invoked with undefined | |
| 78 // or null as this we still explicitly test that we throw on these here. | |
| 79 var non_generic = | |
| 80 [Function.prototype.toString, | |
| 81 Function.prototype.call, | |
| 82 Function.prototype.apply, | |
| 83 String.prototype.toString, | |
| 84 String.prototype.valueOf, | |
| 85 Boolean.prototype.toString, | |
| 86 Boolean.prototype.valueOf, | |
| 87 Number.prototype.toString, | |
| 88 Number.prototype.valueOf, | |
| 89 Number.prototype.toFixed, | |
| 90 Number.prototype.toExponential, | |
| 91 Number.prototype.toPrecision, | |
| 92 Date.prototype.toString, | |
| 93 Date.prototype.toDateString, | |
| 94 Date.prototype.toTimeString, | |
| 95 Date.prototype.toLocaleString, | |
| 96 Date.prototype.toLocaleDateString, | |
| 97 Date.prototype.toLocaleTimeString, | |
| 98 Date.prototype.valueOf, | |
| 99 Date.prototype.getTime, | |
| 100 Date.prototype.getFullYear, | |
| 101 Date.prototype.getUTCFullYear, | |
| 102 Date.prototype.getMonth, | |
| 103 Date.prototype.getUTCMonth, | |
| 104 Date.prototype.getDate, | |
| 105 Date.prototype.getUTCDate, | |
| 106 Date.prototype.getDay, | |
| 107 Date.prototype.getUTCDay, | |
| 108 Date.prototype.getHours, | |
| 109 Date.prototype.getUTCHours, | |
| 110 Date.prototype.getMinutes, | |
| 111 Date.prototype.getUTCMinutes, | |
| 112 Date.prototype.getSeconds, | |
| 113 Date.prototype.getUTCSeconds, | |
| 114 Date.prototype.getMilliseconds, | |
| 115 Date.prototype.getUTCMilliseconds, | |
| 116 Date.prototype.getTimezoneOffset, | |
| 117 Date.prototype.setTime, | |
| 118 Date.prototype.setMilliseconds, | |
| 119 Date.prototype.setUTCMilliseconds, | |
| 120 Date.prototype.setSeconds, | |
| 121 Date.prototype.setUTCSeconds, | |
| 122 Date.prototype.setMinutes, | |
| 123 Date.prototype.setUTCMinutes, | |
| 124 Date.prototype.setHours, | |
| 125 Date.prototype.setUTCHours, | |
| 126 Date.prototype.setDate, | |
| 127 Date.prototype.setUTCDate, | |
| 128 Date.prototype.setMonth, | |
| 129 Date.prototype.setUTCMonth, | |
| 130 Date.prototype.setFullYear, | |
| 131 Date.prototype.setUTCFullYear, | |
| 132 Date.prototype.toUTCString, | |
| 133 Date.prototype.toISOString, | |
| 134 Date.prototype.toJSON, | |
| 135 RegExp.prototype.exec, | |
| 136 RegExp.prototype.test, | |
| 137 RegExp.prototype.toString]; | |
| 138 | |
| 139 | |
| 140 // Mapping functions. | |
| 141 var mapping_functions = | |
| 142 [Array.prototype.every, | |
| 143 Array.prototype.some, | |
| 144 Array.prototype.forEach, | |
| 145 Array.prototype.map, | |
| 146 Array.prototype.filter]; | |
| 147 | |
| 148 // Reduce functions. | |
| 149 var reducing_functions = | |
| 150 [Array.prototype.reduce, | |
| 151 Array.prototype.reduceRight]; | |
| 152 | |
| 153 // Test that all natives using the ToObject call throws the right exception. | |
| 154 for (var i = 0; i < should_throw_on_null_and_undefined.length; i++) { | |
| 155 // Sanity check that all functions are correct | |
| 156 assertEquals(typeof(should_throw_on_null_and_undefined[i]), "function"); | |
| 157 | |
| 158 try { | |
| 159 // We call all functions with undefined as additional parameters. | |
| 160 // The test for null or undefined on this is first. | |
|
Lasse Reichstein
2011/05/03 09:01:03
We call all functions with no parameters, which me
Rico
2011/05/03 12:41:02
Done.
| |
| 161 should_throw_on_null_and_undefined[i].call(null); | |
| 162 assertUnreachable(); | |
| 163 } catch (e) { | |
| 164 assertTrue(/called on null or undefined/.test(e) || | |
| 165 /Cannot convert/.test(e)); | |
| 166 } | |
| 167 | |
| 168 try { | |
| 169 should_throw_on_null_and_undefined[i].call(undefined); | |
| 170 assertUnreachable(); | |
| 171 } catch (e) { | |
| 172 assertTrue(/called on null or undefined/.test(e) || | |
| 173 /Cannot convert/.test(e)); | |
| 174 } | |
| 175 | |
| 176 try { | |
| 177 should_throw_on_null_and_undefined[i].apply(null); | |
| 178 assertUnreachable(); | |
| 179 } catch (e) { | |
| 180 assertTrue(/called on null or undefined/.test(e) || | |
| 181 /Cannot convert/.test(e)); | |
| 182 } | |
| 183 | |
| 184 try { | |
| 185 should_throw_on_null_and_undefined[i].apply(undefined); | |
| 186 assertUnreachable(); | |
| 187 } catch (e) { | |
| 188 assertTrue(/called on null or undefined/.test(e) || | |
| 189 /Cannot convert/.test(e)); | |
| 190 } | |
| 191 } | |
| 192 | |
| 193 // Test that all natives that are non generic throws on null and undefined. | |
| 194 for (var i = 0; i < non_generic.length; i++) { | |
| 195 // Sanity check that all functions are correct | |
| 196 assertEquals(typeof(non_generic[i]), "function"); | |
| 197 try { | |
| 198 non_generic[i].call(null); | |
| 199 assertUnreachable(); | |
| 200 } catch (e) { | |
| 201 assertTrue(e instanceof TypeError); | |
| 202 } | |
| 203 | |
| 204 try { | |
| 205 non_generic[i].call(null); | |
| 206 assertUnreachable(); | |
| 207 } catch (e) { | |
| 208 assertTrue(e instanceof TypeError); | |
| 209 } | |
| 210 | |
| 211 try { | |
| 212 non_generic[i].apply(null); | |
| 213 assertUnreachable(); | |
| 214 } catch (e) { | |
| 215 assertTrue(e instanceof TypeError); | |
| 216 } | |
| 217 | |
| 218 try { | |
| 219 non_generic[i].apply(null); | |
| 220 assertUnreachable(); | |
| 221 } catch (e) { | |
| 222 assertTrue(e instanceof TypeError); | |
| 223 } | |
| 224 } | |
| 225 | |
| 226 | |
| 227 // Test that we still throw when calling with thisArg null or undefined | |
| 228 // through an array mapping function. | |
| 229 var array = [1,2,3,4,5]; | |
| 230 for (var j = 0; j < mapping_functions.length; j++) { | |
| 231 for (var i = 0; i < should_throw_on_null_and_undefined.length; i++) { | |
| 232 try { | |
| 233 mapping_functions[j].call(array, | |
| 234 should_throw_on_null_and_undefined[i], | |
| 235 null); | |
| 236 assertUnreachable(); | |
| 237 } catch (e) { | |
| 238 assertTrue(/called on null or undefined/.test(e) || | |
|
Lasse Reichstein
2011/05/03 09:01:03
How about explicitly converting e to a string befo
Rico
2011/05/03 12:41:02
Done.
| |
| 239 /Cannot convert/.test(e)); | |
| 240 } | |
| 241 | |
| 242 try { | |
| 243 mapping_functions[j].call(array, | |
| 244 should_throw_on_null_and_undefined[i], | |
| 245 undefined); | |
| 246 assertUnreachable(); | |
| 247 } catch (e) { | |
| 248 assertTrue(/called on null or undefined/.test(e) || | |
| 249 /Cannot convert/.test(e)); | |
| 250 } | |
| 251 } | |
| 252 } | |
| 253 | |
| 254 for (var j = 0; j < mapping_functions.length; j++) { | |
| 255 for (var i = 0; i < non_generic.length; i++) { | |
| 256 try { | |
| 257 mapping_functions[j].call(array, | |
| 258 non_generic[i], | |
| 259 null); | |
| 260 assertUnreachable(); | |
| 261 } catch (e) { | |
| 262 assertTrue(e instanceof TypeError); | |
| 263 } | |
| 264 | |
| 265 try { | |
| 266 mapping_functions[j].call(array, | |
| 267 non_generic[i], | |
| 268 undefined); | |
| 269 assertUnreachable(); | |
| 270 } catch (e) { | |
| 271 assertTrue(e instanceof TypeError); | |
| 272 } | |
| 273 } | |
| 274 } | |
| 275 | |
| 276 | |
| 277 // Reduce functions do a call with null as this argument. | |
| 278 for (var j = 0; j < reducing_functions.length; j++) { | |
| 279 for (var i = 0; i < should_throw_on_null_and_undefined.length; i++) { | |
| 280 try { | |
| 281 reducing_functions[j].call(array, should_throw_on_null_and_undefined[i]); | |
| 282 assertUnreachable(); | |
| 283 } catch (e) { | |
| 284 assertTrue(/called on null or undefined/.test(e) || | |
| 285 /Cannot convert/.test(e)); | |
| 286 } | |
| 287 | |
| 288 try { | |
| 289 reducing_functions[j].call(array, should_throw_on_null_and_undefined[i]); | |
| 290 assertUnreachable(); | |
| 291 } catch (e) { | |
| 292 assertTrue(/called on null or undefined/.test(e) || | |
| 293 /Cannot convert/.test(e)); | |
| 294 } | |
| 295 } | |
| 296 } | |
| 297 | |
| 298 for (var j = 0; j < reducing_functions.length; j++) { | |
| 299 for (var i = 0; i < non_generic.length; i++) { | |
| 300 try { | |
| 301 reducing_functions[j].call(array, non_generic[i]); | |
| 302 assertUnreachable(); | |
| 303 } catch (e) { | |
| 304 assertTrue(e instanceof TypeError); | |
| 305 } | |
| 306 | |
| 307 try { | |
| 308 reducing_functions[j].call(array, non_generic[i]); | |
| 309 assertUnreachable(); | |
| 310 } catch (e) { | |
| 311 assertTrue(e instanceof TypeError); | |
| 312 } | |
| 313 } | |
| 314 } | |
| 315 | |
| 316 | |
| 317 // Object.prototype.toString() | |
| 318 assertEquals(Object.prototype.toString.call(null), | |
| 319 '[object Null]') | |
| 320 | |
| 321 assertEquals(Object.prototype.toString.call(undefined), | |
| 322 '[object Undefined]') | |
| OLD | NEW |