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

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: fix 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
« no previous file with comments | « src/messages.cc ('k') | src/runtime/runtime.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 return "#<" + constructorName + ">"; 175 return "#<" + constructorName + ">";
175 } 176 }
176 } 177 }
177 } 178 }
178 return ToStringCheckErrorObject(obj); 179 return ToStringCheckErrorObject(obj);
179 } 180 }
180 181
181 182
182 function MakeGenericError(constructor, type, arg0, arg1, arg2) { 183 function MakeGenericError(constructor, type, arg0, arg1, arg2) {
183 if (IS_UNDEFINED(arg0) && IS_STRING(type)) arg0 = []; 184 if (IS_UNDEFINED(arg0) && IS_STRING(type)) arg0 = [];
184 return new constructor(FormatMessage(type, arg0, arg1, arg2)); 185 var error = new constructor(FormatMessage(type, arg0, arg1, arg2));
186 error[$internalErrorSymbol] = true;
187 return error;
185 } 188 }
186 189
187 190
188 /** 191 /**
189 * Set up the Script function and constructor. 192 * Set up the Script function and constructor.
190 */ 193 */
191 %FunctionSetInstanceClassName(Script, 'Script'); 194 %FunctionSetInstanceClassName(Script, 'Script');
192 %AddNamedProperty(Script.prototype, 'constructor', Script, 195 %AddNamedProperty(Script.prototype, 'constructor', Script,
193 DONT_ENUM | DONT_DELETE | READ_ONLY); 196 DONT_ENUM | DONT_DELETE | READ_ONLY);
194 %SetCode(Script, function(x) { 197 %SetCode(Script, function(x) {
(...skipping 798 matching lines...) Expand 10 before | Expand all | Expand 10 after
993 GlobalError = DefineError(global, function Error() { }); 996 GlobalError = DefineError(global, function Error() { });
994 GlobalEvalError = DefineError(global, function EvalError() { }); 997 GlobalEvalError = DefineError(global, function EvalError() { });
995 GlobalRangeError = DefineError(global, function RangeError() { }); 998 GlobalRangeError = DefineError(global, function RangeError() { });
996 GlobalReferenceError = DefineError(global, function ReferenceError() { }); 999 GlobalReferenceError = DefineError(global, function ReferenceError() { });
997 GlobalSyntaxError = DefineError(global, function SyntaxError() { }); 1000 GlobalSyntaxError = DefineError(global, function SyntaxError() { });
998 GlobalTypeError = DefineError(global, function TypeError() { }); 1001 GlobalTypeError = DefineError(global, function TypeError() { });
999 GlobalURIError = DefineError(global, function URIError() { }); 1002 GlobalURIError = DefineError(global, function URIError() { });
1000 1003
1001 %AddNamedProperty(GlobalError.prototype, 'message', '', DONT_ENUM); 1004 %AddNamedProperty(GlobalError.prototype, 'message', '', DONT_ENUM);
1002 1005
1003 // Global list of error objects visited during ErrorToString. This is
1004 // used to detect cycles in error toString formatting.
1005 var visited_errors = new InternalArray();
1006 var cyclic_error_marker = new GlobalObject();
1007
1008 function GetPropertyWithoutInvokingMonkeyGetters(error, name) {
1009 var current = error;
1010 // Climb the prototype chain until we find the holder.
1011 while (current && !%HasOwnProperty(current, name)) {
1012 current = %_GetPrototype(current);
1013 }
1014 if (IS_NULL(current)) return UNDEFINED;
1015 if (!IS_OBJECT(current)) return error[name];
1016 // If the property is an accessor on one of the predefined errors that can be
1017 // generated statically by the compiler, don't touch it. This is to address
1018 // http://code.google.com/p/chromium/issues/detail?id=69187
1019 var desc = %GetOwnProperty(current, name);
1020 if (desc && desc[IS_ACCESSOR_INDEX]) {
1021 var isName = name === "name";
1022 if (current === GlobalReferenceError.prototype)
1023 return isName ? "ReferenceError" : UNDEFINED;
1024 if (current === GlobalSyntaxError.prototype)
1025 return isName ? "SyntaxError" : UNDEFINED;
1026 if (current === GlobalTypeError.prototype)
1027 return isName ? "TypeError" : UNDEFINED;
1028 }
1029 // Otherwise, read normally.
1030 return error[name];
1031 }
1032
1033 function ErrorToStringDetectCycle(error) {
1034 if (!%PushIfAbsent(visited_errors, error)) throw cyclic_error_marker;
1035 try {
1036 var name = GetPropertyWithoutInvokingMonkeyGetters(error, "name");
1037 name = IS_UNDEFINED(name) ? "Error" : TO_STRING_INLINE(name);
1038 var message = GetPropertyWithoutInvokingMonkeyGetters(error, "message");
1039 message = IS_UNDEFINED(message) ? "" : TO_STRING_INLINE(message);
1040 if (name === "") return message;
1041 if (message === "") return name;
1042 return name + ": " + message;
1043 } finally {
1044 visited_errors.length = visited_errors.length - 1;
1045 }
1046 }
1047
1048 function ErrorToString() { 1006 function ErrorToString() {
1049 if (!IS_SPEC_OBJECT(this)) { 1007 if (!IS_SPEC_OBJECT(this)) {
1050 throw MakeTypeError(kCalledOnNonObject, "Error.prototype.toString"); 1008 throw MakeTypeError(kCalledOnNonObject, "Error.prototype.toString");
1051 } 1009 }
1052 1010
1053 try { 1011 return %ErrorToStringRT(this);
1054 return ErrorToStringDetectCycle(this);
1055 } catch(e) {
1056 // If this error message was encountered already return the empty
1057 // string for it instead of recursively formatting it.
1058 if (e === cyclic_error_marker) {
1059 return '';
1060 }
1061 throw e;
1062 }
1063 } 1012 }
1064 1013
1065 utils.InstallFunctions(GlobalError.prototype, DONT_ENUM, 1014 utils.InstallFunctions(GlobalError.prototype, DONT_ENUM,
1066 ['toString', ErrorToString]); 1015 ['toString', ErrorToString]);
1067 1016
1068 $errorToString = ErrorToString; 1017 $errorToString = ErrorToString;
1069 $getStackTraceLine = GetStackTraceLine; 1018 $getStackTraceLine = GetStackTraceLine;
1070 $messageGetPositionInLine = GetPositionInLine; 1019 $messageGetPositionInLine = GetPositionInLine;
1071 $messageGetLineNumber = GetLineNumber; 1020 $messageGetLineNumber = GetLineNumber;
1072 $messageGetSourceLine = GetSourceLine; 1021 $messageGetSourceLine = GetSourceLine;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1121 // Define accessors first, as this may fail and throw. 1070 // Define accessors first, as this may fail and throw.
1122 ObjectDefineProperty(obj, 'stack', { get: StackTraceGetter, 1071 ObjectDefineProperty(obj, 'stack', { get: StackTraceGetter,
1123 set: StackTraceSetter, 1072 set: StackTraceSetter,
1124 configurable: true }); 1073 configurable: true });
1125 %CollectStackTrace(obj, cons_opt ? cons_opt : captureStackTrace); 1074 %CollectStackTrace(obj, cons_opt ? cons_opt : captureStackTrace);
1126 }; 1075 };
1127 1076
1128 GlobalError.captureStackTrace = captureStackTrace; 1077 GlobalError.captureStackTrace = captureStackTrace;
1129 1078
1130 }); 1079 });
OLDNEW
« no previous file with comments | « src/messages.cc ('k') | src/runtime/runtime.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698