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