| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 (obj instanceof $URIError); | 76 (obj instanceof $URIError); |
| 77 } | 77 } |
| 78 | 78 |
| 79 | 79 |
| 80 // When formatting internally created error messages, do not | 80 // When formatting internally created error messages, do not |
| 81 // invoke overwritten error toString methods but explicitly use | 81 // invoke overwritten error toString methods but explicitly use |
| 82 // the error to string method. This is to avoid leaking error | 82 // the error to string method. This is to avoid leaking error |
| 83 // objects between script tags in a browser setting. | 83 // objects between script tags in a browser setting. |
| 84 function ToStringCheckErrorObject(obj) { | 84 function ToStringCheckErrorObject(obj) { |
| 85 if (IsNativeErrorObject(obj)) { | 85 if (IsNativeErrorObject(obj)) { |
| 86 return %_CallFunction(obj, errorToString); | 86 return %_CallFunction(obj, ErrorToString); |
| 87 } else { | 87 } else { |
| 88 return ToString(obj); | 88 return ToString(obj); |
| 89 } | 89 } |
| 90 } | 90 } |
| 91 | 91 |
| 92 | 92 |
| 93 function ToDetailString(obj) { | 93 function ToDetailString(obj) { |
| 94 if (obj != null && IS_OBJECT(obj) && obj.toString === ObjectToString) { | 94 if (obj != null && IS_OBJECT(obj) && obj.toString === ObjectToString) { |
| 95 var constructor = obj.constructor; | 95 var constructor = obj.constructor; |
| 96 if (typeof constructor == "function") { | 96 if (typeof constructor == "function") { |
| (...skipping 1042 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1139 DefineError(function EvalError() { }); | 1139 DefineError(function EvalError() { }); |
| 1140 DefineError(function URIError() { }); | 1140 DefineError(function URIError() { }); |
| 1141 } | 1141 } |
| 1142 | 1142 |
| 1143 SetUpError(); | 1143 SetUpError(); |
| 1144 | 1144 |
| 1145 $Error.captureStackTrace = captureStackTrace; | 1145 $Error.captureStackTrace = captureStackTrace; |
| 1146 | 1146 |
| 1147 %SetProperty($Error.prototype, 'message', '', DONT_ENUM); | 1147 %SetProperty($Error.prototype, 'message', '', DONT_ENUM); |
| 1148 | 1148 |
| 1149 // Global list of error objects visited during errorToString. This is | 1149 // Global list of error objects visited during ErrorToString. This is |
| 1150 // used to detect cycles in error toString formatting. | 1150 // used to detect cycles in error toString formatting. |
| 1151 const visited_errors = new InternalArray(); | 1151 const visited_errors = new InternalArray(); |
| 1152 const cyclic_error_marker = new $Object(); | 1152 const cyclic_error_marker = new $Object(); |
| 1153 | 1153 |
| 1154 function errorToStringDetectCycle(error) { | 1154 function ErrorToStringDetectCycle(error) { |
| 1155 if (!%PushIfAbsent(visited_errors, error)) throw cyclic_error_marker; | 1155 if (!%PushIfAbsent(visited_errors, error)) throw cyclic_error_marker; |
| 1156 try { | 1156 try { |
| 1157 var type = error.type; | 1157 var type = error.type; |
| 1158 var name = error.name |
| 1159 name = IS_UNDEFINED(name) ? "Error" : TO_STRING_INLINE(name); |
| 1160 var message = error.message; |
| 1158 var hasMessage = %_CallFunction(error, "message", ObjectHasOwnProperty); | 1161 var hasMessage = %_CallFunction(error, "message", ObjectHasOwnProperty); |
| 1159 if (type && !hasMessage) { | 1162 if (type && !hasMessage) { |
| 1160 var formatted = FormatMessage(%NewMessageObject(type, error.arguments)); | 1163 message = FormatMessage(%NewMessageObject(type, error.arguments)); |
| 1161 return error.name + ": " + formatted; | |
| 1162 } | 1164 } |
| 1163 var message = hasMessage ? (": " + error.message) : ""; | 1165 message = IS_UNDEFINED(message) ? "" : TO_STRING_INLINE(message); |
| 1164 return error.name + message; | 1166 if (name === "") return message; |
| 1167 if (message === "") return name; |
| 1168 return name + ": " + message; |
| 1165 } finally { | 1169 } finally { |
| 1166 visited_errors.length = visited_errors.length - 1; | 1170 visited_errors.length = visited_errors.length - 1; |
| 1167 } | 1171 } |
| 1168 } | 1172 } |
| 1169 | 1173 |
| 1170 function errorToString() { | 1174 function ErrorToString() { |
| 1171 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | 1175 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 1172 throw MakeTypeError("called_on_null_or_undefined", | 1176 throw MakeTypeError("called_on_null_or_undefined", |
| 1173 ["Error.prototype.toString"]); | 1177 ["Error.prototype.toString"]); |
| 1174 } | 1178 } |
| 1175 // This helper function is needed because access to properties on | |
| 1176 // the builtins object do not work inside of a catch clause. | |
| 1177 function isCyclicErrorMarker(o) { return o === cyclic_error_marker; } | |
| 1178 | 1179 |
| 1179 try { | 1180 try { |
| 1180 return errorToStringDetectCycle(this); | 1181 return ErrorToStringDetectCycle(this); |
| 1181 } catch(e) { | 1182 } catch(e) { |
| 1182 // If this error message was encountered already return the empty | 1183 // If this error message was encountered already return the empty |
| 1183 // string for it instead of recursively formatting it. | 1184 // string for it instead of recursively formatting it. |
| 1184 if (isCyclicErrorMarker(e)) { | 1185 if (e === cyclic_error_marker) { |
| 1185 return ''; | 1186 return ''; |
| 1186 } | 1187 } |
| 1187 throw e; | 1188 throw e; |
| 1188 } | 1189 } |
| 1189 } | 1190 } |
| 1190 | 1191 |
| 1191 | 1192 |
| 1192 InstallFunctions($Error.prototype, DONT_ENUM, ['toString', errorToString]); | 1193 InstallFunctions($Error.prototype, DONT_ENUM, ['toString', ErrorToString]); |
| 1193 | 1194 |
| 1194 // Boilerplate for exceptions for stack overflows. Used from | 1195 // Boilerplate for exceptions for stack overflows. Used from |
| 1195 // Isolate::StackOverflow(). | 1196 // Isolate::StackOverflow(). |
| 1196 const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []); | 1197 const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []); |
| OLD | NEW |