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 String.prototype.toString, |
| 82 String.prototype.valueOf, |
| 83 Boolean.prototype.toString, |
| 84 Boolean.prototype.valueOf, |
| 85 Number.prototype.toString, |
| 86 Number.prototype.valueOf, |
| 87 Number.prototype.toFixed, |
| 88 Number.prototype.toExponential, |
| 89 Number.prototype.toPrecision, |
| 90 Date.prototype.toString, |
| 91 Date.prototype.toDateString, |
| 92 Date.prototype.toTimeString, |
| 93 Date.prototype.toLocaleString, |
| 94 Date.prototype.toLocaleDateString, |
| 95 Date.prototype.toLocaleTimeString, |
| 96 Date.prototype.valueOf, |
| 97 Date.prototype.getTime, |
| 98 Date.prototype.getFullYear, |
| 99 Date.prototype.getUTCFullYear, |
| 100 Date.prototype.getMonth, |
| 101 Date.prototype.getUTCMonth, |
| 102 Date.prototype.getDate, |
| 103 Date.prototype.getUTCDate, |
| 104 Date.prototype.getDay, |
| 105 Date.prototype.getUTCDay, |
| 106 Date.prototype.getHours, |
| 107 Date.prototype.getUTCHours, |
| 108 Date.prototype.getMinutes, |
| 109 Date.prototype.getUTCMinutes, |
| 110 Date.prototype.getSeconds, |
| 111 Date.prototype.getUTCSeconds, |
| 112 Date.prototype.getMilliseconds, |
| 113 Date.prototype.getUTCMilliseconds, |
| 114 Date.prototype.getTimezoneOffset, |
| 115 Date.prototype.setTime, |
| 116 Date.prototype.setMilliseconds, |
| 117 Date.prototype.setUTCMilliseconds, |
| 118 Date.prototype.setSeconds, |
| 119 Date.prototype.setUTCSeconds, |
| 120 Date.prototype.setMinutes, |
| 121 Date.prototype.setUTCMinutes, |
| 122 Date.prototype.setHours, |
| 123 Date.prototype.setUTCHours, |
| 124 Date.prototype.setDate, |
| 125 Date.prototype.setUTCDate, |
| 126 Date.prototype.setMonth, |
| 127 Date.prototype.setUTCMonth, |
| 128 Date.prototype.setFullYear, |
| 129 Date.prototype.setUTCFullYear, |
| 130 Date.prototype.toUTCString, |
| 131 Date.prototype.toISOString, |
| 132 Date.prototype.toJSON, |
| 133 RegExp.prototype.exec, |
| 134 RegExp.prototype.test, |
| 135 RegExp.prototype.toString]; |
| 136 |
| 137 |
| 138 // Mapping functions. |
| 139 var mapping_functions = |
| 140 [Array.prototype.every, |
| 141 Array.prototype.some, |
| 142 Array.prototype.forEach, |
| 143 Array.prototype.map, |
| 144 Array.prototype.filter]; |
| 145 |
| 146 // Reduce functions. |
| 147 var reducing_functions = |
| 148 [Array.prototype.reduce, |
| 149 Array.prototype.reduceRight]; |
| 150 |
| 151 // Test that all natives using the ToObject call throws the right exception. |
| 152 for (var i = 0; i < should_throw_on_null_and_undefined.length; i++) { |
| 153 // Sanity check that all functions are correct |
| 154 assertEquals(typeof(should_throw_on_null_and_undefined[i]), "function"); |
| 155 |
| 156 try { |
| 157 should_throw_on_null_and_undefined[i].call(null); |
| 158 assertUnreachable(); |
| 159 } catch (e) { |
| 160 assertTrue(/called on non-object/.test(e) || /Cannot convert/.test(e)); |
| 161 } |
| 162 |
| 163 try { |
| 164 should_throw_on_null_and_undefined[i].call(undefined); |
| 165 assertUnreachable(); |
| 166 } catch (e) { |
| 167 assertTrue(/called on non-object/.test(e) || /Cannot convert/.test(e)); |
| 168 } |
| 169 |
| 170 try { |
| 171 should_throw_on_null_and_undefined[i].apply(null); |
| 172 assertUnreachable(); |
| 173 } catch (e) { |
| 174 assertTrue(/called on non-object/.test(e) || /Cannot convert/.test(e)); |
| 175 } |
| 176 |
| 177 try { |
| 178 should_throw_on_null_and_undefined[i].apply(undefined); |
| 179 assertUnreachable(); |
| 180 } catch (e) { |
| 181 assertTrue(/called on non-object/.test(e) || /Cannot convert/.test(e)); |
| 182 } |
| 183 } |
| 184 |
| 185 // Test that all natives that are non generic throws on null and undefined. |
| 186 for (var i = 0; i < non_generic.length; i++) { |
| 187 // Sanity check that all functions are correct |
| 188 assertEquals(typeof(non_generic[i]), "function"); |
| 189 print(non_generic[i].toString()); |
| 190 try { |
| 191 non_generic[i].call(null); |
| 192 assertUnreachable(); |
| 193 } catch (e) { |
| 194 assertTrue(e instanceof TypeError); |
| 195 } |
| 196 |
| 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].apply(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 |
| 219 |
| 220 // Test that we still throw when calling with thisArg null or undefined |
| 221 // through an array mapping function. |
| 222 var array = [1,2,3,4,5]; |
| 223 for (var j = 0; j < mapping_functions.length; j++) { |
| 224 for (var i = 0; i < should_throw_on_null_and_undefined.length; i++) { |
| 225 try { |
| 226 mapping_functions[j].call(array, |
| 227 should_throw_on_null_and_undefined[i], |
| 228 null); |
| 229 assertUnreachable(); |
| 230 } catch (e) { |
| 231 assertTrue(/called on non-object/.test(e) || /Cannot convert/.test(e)); |
| 232 } |
| 233 |
| 234 try { |
| 235 mapping_functions[j].call(array, |
| 236 should_throw_on_null_and_undefined[i], |
| 237 undefined); |
| 238 assertUnreachable(); |
| 239 } catch (e) { |
| 240 assertTrue(/called on non-object/.test(e) || /Cannot convert/.test(e)); |
| 241 } |
| 242 } |
| 243 } |
| 244 |
| 245 for (var j = 0; j < mapping_functions.length; j++) { |
| 246 for (var i = 0; i < non_generic.length; i++) { |
| 247 try { |
| 248 mapping_functions[j].call(array, |
| 249 non_generic[i], |
| 250 null); |
| 251 assertUnreachable(); |
| 252 } catch (e) { |
| 253 assertTrue(e instanceof TypeError); |
| 254 } |
| 255 |
| 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 } |
| 266 |
| 267 |
| 268 // Reduce functions do a call with null as this argument. |
| 269 for (var j = 0; j < reducing_functions.length; j++) { |
| 270 for (var i = 0; i < should_throw_on_null_and_undefined.length; i++) { |
| 271 try { |
| 272 reducing_functions[j].call(array, |
| 273 should_throw_on_null_and_undefined[i]); |
| 274 assertUnreachable(); |
| 275 } catch (e) { |
| 276 assertTrue(/called on non-object/.test(e) || /Cannot convert/.test(e)); |
| 277 } |
| 278 |
| 279 try { |
| 280 reducing_functions[j].call(array, |
| 281 should_throw_on_null_and_undefined[i]); |
| 282 assertUnreachable(); |
| 283 } catch (e) { |
| 284 assertTrue(/called on non-object/.test(e) || /Cannot convert/.test(e)); |
| 285 } |
| 286 } |
| 287 } |
| 288 |
| 289 for (var j = 0; j < reducing_functions.length; j++) { |
| 290 for (var i = 0; i < non_generic.length; i++) { |
| 291 try { |
| 292 reducing_functions[j].call(array, |
| 293 non_generic[i]); |
| 294 assertUnreachable(); |
| 295 } catch (e) { |
| 296 assertTrue(e instanceof TypeError); |
| 297 } |
| 298 |
| 299 try { |
| 300 reducing_functions[j].call(array, |
| 301 non_generic[i]); |
| 302 assertUnreachable(); |
| 303 } catch (e) { |
| 304 assertTrue(e instanceof TypeError); |
| 305 } |
| 306 } |
| 307 } |
| 308 |
| 309 // Object.prototype.toString() |
| 310 assertEquals(Object.prototype.toString.call(null), |
| 311 '[object Null]') |
| 312 |
| 313 assertEquals(Object.prototype.toString.call(undefined), |
| 314 '[object Undefined]') |
OLD | NEW |