OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 if (IS_FUNCTION(obj)) return %_CallFunction(obj, FunctionToString); | 221 if (IS_FUNCTION(obj)) return %_CallFunction(obj, FunctionToString); |
222 if (IS_OBJECT(obj) && %GetDataProperty(obj, "toString") === ObjectToString) { | 222 if (IS_OBJECT(obj) && %GetDataProperty(obj, "toString") === ObjectToString) { |
223 var constructor = %GetDataProperty(obj, "constructor"); | 223 var constructor = %GetDataProperty(obj, "constructor"); |
224 if (typeof constructor == "function") { | 224 if (typeof constructor == "function") { |
225 var constructorName = constructor.name; | 225 var constructorName = constructor.name; |
226 if (IS_STRING(constructorName) && constructorName !== "") { | 226 if (IS_STRING(constructorName) && constructorName !== "") { |
227 return "#<" + constructorName + ">"; | 227 return "#<" + constructorName + ">"; |
228 } | 228 } |
229 } | 229 } |
230 } | 230 } |
231 if (IsNativeErrorObject(obj)) return %_CallFunction(obj, ErrorToString); | 231 if (CanBeSafelyTreatedAsAnErrorObject(obj)) { |
| 232 return %_CallFunction(obj, ErrorToString); |
| 233 } |
232 return %_CallFunction(obj, ObjectToString); | 234 return %_CallFunction(obj, ObjectToString); |
233 } | 235 } |
234 | 236 |
235 | 237 // To determine whether we can safely stringify an object using ErrorToString |
236 // To check if something is a native error we need to check the | 238 // without the risk of side-effects, we need to check whether the object is |
237 // concrete native error types. It is not sufficient to use instanceof | 239 // either an instance of a native error type (via '%_ClassOf'), or has $Error |
238 // since it possible to create an object that has Error.prototype on | 240 // in its prototype chain and hasn't overwritten 'toString' with something |
239 // its prototype chain. This is the case for DOMException for example. | 241 // strange and unusual. |
240 function IsNativeErrorObject(obj) { | 242 function CanBeSafelyTreatedAsAnErrorObject(obj) { |
241 switch (%_ClassOf(obj)) { | 243 switch (%_ClassOf(obj)) { |
242 case 'Error': | 244 case 'Error': |
243 case 'EvalError': | 245 case 'EvalError': |
244 case 'RangeError': | 246 case 'RangeError': |
245 case 'ReferenceError': | 247 case 'ReferenceError': |
246 case 'SyntaxError': | 248 case 'SyntaxError': |
247 case 'TypeError': | 249 case 'TypeError': |
248 case 'URIError': | 250 case 'URIError': |
249 return true; | 251 return true; |
250 } | 252 } |
251 return false; | 253 |
| 254 var objToString = %GetDataProperty(obj, "toString"); |
| 255 return obj instanceof $Error && objToString === ErrorToString; |
252 } | 256 } |
253 | 257 |
254 | 258 |
255 // When formatting internally created error messages, do not | 259 // When formatting internally created error messages, do not |
256 // invoke overwritten error toString methods but explicitly use | 260 // invoke overwritten error toString methods but explicitly use |
257 // the error to string method. This is to avoid leaking error | 261 // the error to string method. This is to avoid leaking error |
258 // objects between script tags in a browser setting. | 262 // objects between script tags in a browser setting. |
259 function ToStringCheckErrorObject(obj) { | 263 function ToStringCheckErrorObject(obj) { |
260 if (IsNativeErrorObject(obj)) { | 264 if (CanBeSafelyTreatedAsAnErrorObject(obj)) { |
261 return %_CallFunction(obj, ErrorToString); | 265 return %_CallFunction(obj, ErrorToString); |
262 } else { | 266 } else { |
263 return ToString(obj); | 267 return ToString(obj); |
264 } | 268 } |
265 } | 269 } |
266 | 270 |
267 | 271 |
268 function ToDetailString(obj) { | 272 function ToDetailString(obj) { |
269 if (obj != null && IS_OBJECT(obj) && obj.toString === ObjectToString) { | 273 if (obj != null && IS_OBJECT(obj) && obj.toString === ObjectToString) { |
270 var constructor = obj.constructor; | 274 var constructor = obj.constructor; |
(...skipping 1066 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1337 %GetAndClearOverflowedStackTrace(this); | 1341 %GetAndClearOverflowedStackTrace(this); |
1338 }; | 1342 }; |
1339 | 1343 |
1340 %DefineOrRedefineAccessorProperty( | 1344 %DefineOrRedefineAccessorProperty( |
1341 boilerplate, 'stack', getter, setter, DONT_ENUM); | 1345 boilerplate, 'stack', getter, setter, DONT_ENUM); |
1342 | 1346 |
1343 return boilerplate; | 1347 return boilerplate; |
1344 } | 1348 } |
1345 | 1349 |
1346 var kStackOverflowBoilerplate = SetUpStackOverflowBoilerplate(); | 1350 var kStackOverflowBoilerplate = SetUpStackOverflowBoilerplate(); |
OLD | NEW |