Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: src/messages.js

Issue 1281833002: Rewrite Error.prototype.toString in C++. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: add test case Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // ------------------------------------------------------------------- 5 // -------------------------------------------------------------------
6 6
7 var $errorToString; 7 var $errorToString;
8 var $getStackTraceLine; 8 var $getStackTraceLine;
9 var $internalErrorSymbol;
9 var $messageGetPositionInLine; 10 var $messageGetPositionInLine;
10 var $messageGetLineNumber; 11 var $messageGetLineNumber;
11 var $messageGetSourceLine; 12 var $messageGetSourceLine;
12 var $noSideEffectToString; 13 var $noSideEffectToString;
13 var $stackOverflowBoilerplate; 14 var $stackOverflowBoilerplate;
14 var $stackTraceSymbol; 15 var $stackTraceSymbol;
15 var $toDetailString; 16 var $toDetailString;
16 var $Error; 17 var $Error;
17 var $EvalError; 18 var $EvalError;
18 var $RangeError; 19 var $RangeError;
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 return "#<" + constructorName + ">"; 159 return "#<" + constructorName + ">";
159 } 160 }
160 } 161 }
161 } 162 }
162 return ToStringCheckErrorObject(obj); 163 return ToStringCheckErrorObject(obj);
163 } 164 }
164 165
165 166
166 function MakeGenericError(constructor, type, arg0, arg1, arg2) { 167 function MakeGenericError(constructor, type, arg0, arg1, arg2) {
167 if (IS_UNDEFINED(arg0) && IS_STRING(type)) arg0 = []; 168 if (IS_UNDEFINED(arg0) && IS_STRING(type)) arg0 = [];
168 return new constructor(FormatMessage(type, arg0, arg1, arg2)); 169 var error = new constructor(FormatMessage(type, arg0, arg1, arg2));
170 error[$internalErrorSymbol] = true;
171 return error;
169 } 172 }
170 173
171 174
172 /** 175 /**
173 * Set up the Script function and constructor. 176 * Set up the Script function and constructor.
174 */ 177 */
175 %FunctionSetInstanceClassName(Script, 'Script'); 178 %FunctionSetInstanceClassName(Script, 'Script');
176 %AddNamedProperty(Script.prototype, 'constructor', Script, 179 %AddNamedProperty(Script.prototype, 'constructor', Script,
177 DONT_ENUM | DONT_DELETE | READ_ONLY); 180 DONT_ENUM | DONT_DELETE | READ_ONLY);
178 %SetCode(Script, function(x) { 181 %SetCode(Script, function(x) {
(...skipping 798 matching lines...) Expand 10 before | Expand all | Expand 10 after
977 GlobalError = DefineError(global, function Error() { }); 980 GlobalError = DefineError(global, function Error() { });
978 GlobalEvalError = DefineError(global, function EvalError() { }); 981 GlobalEvalError = DefineError(global, function EvalError() { });
979 GlobalRangeError = DefineError(global, function RangeError() { }); 982 GlobalRangeError = DefineError(global, function RangeError() { });
980 GlobalReferenceError = DefineError(global, function ReferenceError() { }); 983 GlobalReferenceError = DefineError(global, function ReferenceError() { });
981 GlobalSyntaxError = DefineError(global, function SyntaxError() { }); 984 GlobalSyntaxError = DefineError(global, function SyntaxError() { });
982 GlobalTypeError = DefineError(global, function TypeError() { }); 985 GlobalTypeError = DefineError(global, function TypeError() { });
983 GlobalURIError = DefineError(global, function URIError() { }); 986 GlobalURIError = DefineError(global, function URIError() { });
984 987
985 %AddNamedProperty(GlobalError.prototype, 'message', '', DONT_ENUM); 988 %AddNamedProperty(GlobalError.prototype, 'message', '', DONT_ENUM);
986 989
987 // Global list of error objects visited during ErrorToString. This is
988 // used to detect cycles in error toString formatting.
989 var visited_errors = new InternalArray();
990 var cyclic_error_marker = new GlobalObject();
991
992 function GetPropertyWithoutInvokingMonkeyGetters(error, name) {
993 var current = error;
994 // Climb the prototype chain until we find the holder.
995 while (current && !%HasOwnProperty(current, name)) {
996 current = %_GetPrototype(current);
997 }
998 if (IS_NULL(current)) return UNDEFINED;
999 if (!IS_OBJECT(current)) return error[name];
1000 // If the property is an accessor on one of the predefined errors that can be
1001 // generated statically by the compiler, don't touch it. This is to address
1002 // http://code.google.com/p/chromium/issues/detail?id=69187
1003 var desc = %GetOwnProperty(current, name);
1004 if (desc && desc[IS_ACCESSOR_INDEX]) {
1005 var isName = name === "name";
1006 if (current === GlobalReferenceError.prototype)
1007 return isName ? "ReferenceError" : UNDEFINED;
1008 if (current === GlobalSyntaxError.prototype)
1009 return isName ? "SyntaxError" : UNDEFINED;
1010 if (current === GlobalTypeError.prototype)
1011 return isName ? "TypeError" : UNDEFINED;
1012 }
1013 // Otherwise, read normally.
1014 return error[name];
1015 }
1016
1017 function ErrorToStringDetectCycle(error) {
1018 if (!%PushIfAbsent(visited_errors, error)) throw cyclic_error_marker;
1019 try {
1020 var name = GetPropertyWithoutInvokingMonkeyGetters(error, "name");
1021 name = IS_UNDEFINED(name) ? "Error" : TO_STRING_INLINE(name);
1022 var message = GetPropertyWithoutInvokingMonkeyGetters(error, "message");
1023 message = IS_UNDEFINED(message) ? "" : TO_STRING_INLINE(message);
1024 if (name === "") return message;
1025 if (message === "") return name;
1026 return name + ": " + message;
1027 } finally {
1028 visited_errors.length = visited_errors.length - 1;
1029 }
1030 }
1031
1032 function ErrorToString() { 990 function ErrorToString() {
1033 if (!IS_SPEC_OBJECT(this)) { 991 if (!IS_SPEC_OBJECT(this)) {
1034 throw MakeTypeError(kCalledOnNonObject, "Error.prototype.toString"); 992 throw MakeTypeError(kCalledOnNonObject, "Error.prototype.toString");
1035 } 993 }
1036 994
1037 try { 995 return %ErrorToStringRT(this);
1038 return ErrorToStringDetectCycle(this);
1039 } catch(e) {
1040 // If this error message was encountered already return the empty
1041 // string for it instead of recursively formatting it.
1042 if (e === cyclic_error_marker) {
1043 return '';
1044 }
1045 throw e;
1046 }
1047 } 996 }
1048 997
1049 utils.InstallFunctions(GlobalError.prototype, DONT_ENUM, 998 utils.InstallFunctions(GlobalError.prototype, DONT_ENUM,
1050 ['toString', ErrorToString]); 999 ['toString', ErrorToString]);
1051 1000
1052 $errorToString = ErrorToString; 1001 $errorToString = ErrorToString;
1053 $getStackTraceLine = GetStackTraceLine; 1002 $getStackTraceLine = GetStackTraceLine;
1054 $messageGetPositionInLine = GetPositionInLine; 1003 $messageGetPositionInLine = GetPositionInLine;
1055 $messageGetLineNumber = GetLineNumber; 1004 $messageGetLineNumber = GetLineNumber;
1056 $messageGetSourceLine = GetSourceLine; 1005 $messageGetSourceLine = GetSourceLine;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1105 // Define accessors first, as this may fail and throw. 1054 // Define accessors first, as this may fail and throw.
1106 ObjectDefineProperty(obj, 'stack', { get: StackTraceGetter, 1055 ObjectDefineProperty(obj, 'stack', { get: StackTraceGetter,
1107 set: StackTraceSetter, 1056 set: StackTraceSetter,
1108 configurable: true }); 1057 configurable: true });
1109 %CollectStackTrace(obj, cons_opt ? cons_opt : captureStackTrace); 1058 %CollectStackTrace(obj, cons_opt ? cons_opt : captureStackTrace);
1110 }; 1059 };
1111 1060
1112 GlobalError.captureStackTrace = captureStackTrace; 1061 GlobalError.captureStackTrace = captureStackTrace;
1113 1062
1114 }); 1063 });
OLDNEW
« src/messages.cc ('K') | « src/messages.cc ('k') | src/runtime/runtime.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698