OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-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 994 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1005 DefineError(function SyntaxError() { }); | 1005 DefineError(function SyntaxError() { }); |
1006 DefineError(function ReferenceError() { }); | 1006 DefineError(function ReferenceError() { }); |
1007 DefineError(function EvalError() { }); | 1007 DefineError(function EvalError() { }); |
1008 DefineError(function URIError() { }); | 1008 DefineError(function URIError() { }); |
1009 | 1009 |
1010 $Error.captureStackTrace = captureStackTrace; | 1010 $Error.captureStackTrace = captureStackTrace; |
1011 | 1011 |
1012 // Setup extra properties of the Error.prototype object. | 1012 // Setup extra properties of the Error.prototype object. |
1013 $Error.prototype.message = ''; | 1013 $Error.prototype.message = ''; |
1014 | 1014 |
| 1015 // Global list of error objects visited during errorToString. This is |
| 1016 // used to detect cycles in error toString formatting. |
| 1017 var visited_errors = new $Array(); |
| 1018 var cyclic_error_marker = new $Object(); |
| 1019 |
| 1020 function errorToStringDetectCycle() { |
| 1021 if (!%PushIfAbsent(visited_errors, this)) throw cyclic_error_marker; |
| 1022 try { |
| 1023 var type = this.type; |
| 1024 if (type && !this.hasOwnProperty("message")) { |
| 1025 var formatted = FormatMessage({ type: type, args: this.arguments }); |
| 1026 return this.name + ": " + formatted; |
| 1027 } |
| 1028 var message = this.hasOwnProperty("message") ? (": " + this.message) : ""; |
| 1029 return this.name + message; |
| 1030 } finally { |
| 1031 visited_errors.pop(); |
| 1032 } |
| 1033 } |
| 1034 |
1015 function errorToString() { | 1035 function errorToString() { |
1016 var type = this.type; | 1036 // These helper functions are needed because access to properties on |
1017 if (type && !this.hasOwnProperty("message")) { | 1037 // the builtins object do not work inside of a catch clause. |
1018 return this.name + ": " + FormatMessage({ type: type, args: this.arguments }
); | 1038 function isCyclicErrorMarker(o) { return o === cyclic_error_marker; } |
| 1039 function isVisitedErrorsEmpty() { return visited_errors.length === 0; } |
| 1040 |
| 1041 try { |
| 1042 return %_CallFunction(this, errorToStringDetectCycle); |
| 1043 } catch(e) { |
| 1044 // Propagate cyclic_error_marker exception until all error |
| 1045 // formatting is finished and then return the empty string. Safari |
| 1046 // and Firefox also returns the empty string when converting a |
| 1047 // cyclic error to a string. |
| 1048 if (isCyclicErrorMarker(e) && isVisitedErrorsEmpty()) return ''; |
| 1049 else throw e; |
1019 } | 1050 } |
1020 var message = this.hasOwnProperty("message") ? (": " + this.message) : ""; | |
1021 return this.name + message; | |
1022 } | 1051 } |
1023 | 1052 |
1024 %FunctionSetName(errorToString, 'toString'); | 1053 %FunctionSetName(errorToString, 'toString'); |
1025 %SetProperty($Error.prototype, 'toString', errorToString, DONT_ENUM); | 1054 %SetProperty($Error.prototype, 'toString', errorToString, DONT_ENUM); |
1026 | 1055 |
1027 | |
1028 // Boilerplate for exceptions for stack overflows. Used from | 1056 // Boilerplate for exceptions for stack overflows. Used from |
1029 // Top::StackOverflow(). | 1057 // Top::StackOverflow(). |
1030 const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []); | 1058 const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []); |
OLD | NEW |