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

Side by Side Diff: src/js/messages.js

Issue 1510173002: Revert of Make Error.prototype.toString spec compliant; and fix various side-effect-free error printing metho… (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years 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/isolate.h ('k') | src/messages.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 (function(global, utils) { 7 (function(global, utils) {
8 8
9 %CheckIsBootstrapping(); 9 %CheckIsBootstrapping();
10 10
11 // ------------------------------------------------------------------- 11 // -------------------------------------------------------------------
12 // Imports 12 // Imports
13 13
14 var ArrayJoin; 14 var ArrayJoin;
15 var Bool16x8ToString; 15 var Bool16x8ToString;
16 var Bool32x4ToString; 16 var Bool32x4ToString;
17 var Bool8x16ToString; 17 var Bool8x16ToString;
18 var callSiteReceiverSymbol = 18 var callSiteReceiverSymbol =
19 utils.ImportNow("call_site_receiver_symbol"); 19 utils.ImportNow("call_site_receiver_symbol");
20 var callSiteFunctionSymbol = 20 var callSiteFunctionSymbol =
21 utils.ImportNow("call_site_function_symbol"); 21 utils.ImportNow("call_site_function_symbol");
22 var callSitePositionSymbol = 22 var callSitePositionSymbol =
23 utils.ImportNow("call_site_position_symbol"); 23 utils.ImportNow("call_site_position_symbol");
24 var callSiteStrictSymbol = 24 var callSiteStrictSymbol =
25 utils.ImportNow("call_site_strict_symbol"); 25 utils.ImportNow("call_site_strict_symbol");
26 var FLAG_harmony_tostring;
27 var Float32x4ToString; 26 var Float32x4ToString;
28 var formattedStackTraceSymbol = 27 var formattedStackTraceSymbol =
29 utils.ImportNow("formatted_stack_trace_symbol"); 28 utils.ImportNow("formatted_stack_trace_symbol");
30 var FunctionSourceString 29 var FunctionSourceString
31 var GlobalObject = global.Object; 30 var GlobalObject = global.Object;
32 var Int16x8ToString; 31 var Int16x8ToString;
33 var Int32x4ToString; 32 var Int32x4ToString;
34 var Int8x16ToString; 33 var Int8x16ToString;
35 var InternalArray = utils.InternalArray; 34 var InternalArray = utils.InternalArray;
36 var internalErrorSymbol = utils.ImportNow("internal_error_symbol"); 35 var internalErrorSymbol = utils.ImportNow("internal_error_symbol");
37 var ObjectDefineProperty; 36 var ObjectDefineProperty;
38 var ObjectToString; 37 var ObjectToString;
39 var Script = utils.ImportNow("Script"); 38 var Script = utils.ImportNow("Script");
40 var stackTraceSymbol = utils.ImportNow("stack_trace_symbol"); 39 var stackTraceSymbol = utils.ImportNow("stack_trace_symbol");
41 var StringCharAt; 40 var StringCharAt;
42 var StringIndexOf; 41 var StringIndexOf;
43 var StringSubstring; 42 var StringSubstring;
44 var SymbolToString; 43 var SymbolToString;
45 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
46 var Uint16x8ToString; 44 var Uint16x8ToString;
47 var Uint32x4ToString; 45 var Uint32x4ToString;
48 var Uint8x16ToString; 46 var Uint8x16ToString;
49 47
50 utils.Import(function(from) { 48 utils.Import(function(from) {
51 ArrayJoin = from.ArrayJoin; 49 ArrayJoin = from.ArrayJoin;
52 Bool16x8ToString = from.Bool16x8ToString; 50 Bool16x8ToString = from.Bool16x8ToString;
53 Bool32x4ToString = from.Bool32x4ToString; 51 Bool32x4ToString = from.Bool32x4ToString;
54 Bool8x16ToString = from.Bool8x16ToString; 52 Bool8x16ToString = from.Bool8x16ToString;
55 Float32x4ToString = from.Float32x4ToString; 53 Float32x4ToString = from.Float32x4ToString;
56 FunctionSourceString = from.FunctionSourceString; 54 FunctionSourceString = from.FunctionSourceString;
57 Int16x8ToString = from.Int16x8ToString; 55 Int16x8ToString = from.Int16x8ToString;
58 Int32x4ToString = from.Int32x4ToString; 56 Int32x4ToString = from.Int32x4ToString;
59 Int8x16ToString = from.Int8x16ToString; 57 Int8x16ToString = from.Int8x16ToString;
60 ObjectDefineProperty = from.ObjectDefineProperty; 58 ObjectDefineProperty = from.ObjectDefineProperty;
61 ObjectToString = from.ObjectToString; 59 ObjectToString = from.ObjectToString;
62 StringCharAt = from.StringCharAt; 60 StringCharAt = from.StringCharAt;
63 StringIndexOf = from.StringIndexOf; 61 StringIndexOf = from.StringIndexOf;
64 StringSubstring = from.StringSubstring; 62 StringSubstring = from.StringSubstring;
65 SymbolToString = from.SymbolToString; 63 SymbolToString = from.SymbolToString;
66 Uint16x8ToString = from.Uint16x8ToString; 64 Uint16x8ToString = from.Uint16x8ToString;
67 Uint32x4ToString = from.Uint32x4ToString; 65 Uint32x4ToString = from.Uint32x4ToString;
68 Uint8x16ToString = from.Uint8x16ToString; 66 Uint8x16ToString = from.Uint8x16ToString;
69 }); 67 });
70 68
71 utils.ImportFromExperimental(function(from) {
72 FLAG_harmony_tostring = from.FLAG_harmony_tostring;
73 });
74
75 // ------------------------------------------------------------------- 69 // -------------------------------------------------------------------
76 70
77 var GlobalError; 71 var GlobalError;
78 var GlobalTypeError; 72 var GlobalTypeError;
79 var GlobalRangeError; 73 var GlobalRangeError;
80 var GlobalURIError; 74 var GlobalURIError;
81 var GlobalSyntaxError; 75 var GlobalSyntaxError;
82 var GlobalReferenceError; 76 var GlobalReferenceError;
83 var GlobalEvalError; 77 var GlobalEvalError;
84 78
85 79
86 function NoSideEffectsObjectToString() { 80 function NoSideEffectsObjectToString() {
87 if (IS_UNDEFINED(this)) return "[object Undefined]"; 81 if (IS_UNDEFINED(this)) return "[object Undefined]";
88 if (IS_NULL(this)) return "[object Null]"; 82 if (IS_NULL(this)) return "[object Null]";
89 var O = TO_OBJECT(this); 83 return "[object " + %_ClassOf(TO_OBJECT(this)) + "]";
90 var builtinTag = %_ClassOf(O);
91 var tag;
92 if (FLAG_harmony_tostring) {
93 tag = %GetDataProperty(O, toStringTagSymbol);
94 if (!IS_STRING(tag)) {
95 tag = builtinTag;
96 }
97 } else {
98 tag = builtinTag;
99 }
100 return `[object ${tag}]`;
101 } 84 }
102 85
103 function IsErrorObject(obj) {
104 return HAS_PRIVATE(obj, stackTraceSymbol);
105 }
106 86
107 function NoSideEffectsErrorToString() { 87 function NoSideEffectToString(obj) {
108 var name = %GetDataProperty(this, "name");
109 var message = %GetDataProperty(this, "message");
110 name = IS_UNDEFINED(name) ? "Error" : NoSideEffectsToString(name);
111 message = IS_UNDEFINED(message) ? "" : NoSideEffectsToString(message);
112 if (name == "") return message;
113 if (message == "") return name;
114 return `${name}: ${message}`;
115 }
116
117 function NoSideEffectsToString(obj) {
118 if (IS_STRING(obj)) return obj; 88 if (IS_STRING(obj)) return obj;
119 if (IS_NUMBER(obj)) return %_NumberToString(obj); 89 if (IS_NUMBER(obj)) return %_NumberToString(obj);
120 if (IS_BOOLEAN(obj)) return obj ? 'true' : 'false'; 90 if (IS_BOOLEAN(obj)) return obj ? 'true' : 'false';
121 if (IS_UNDEFINED(obj)) return 'undefined'; 91 if (IS_UNDEFINED(obj)) return 'undefined';
122 if (IS_NULL(obj)) return 'null'; 92 if (IS_NULL(obj)) return 'null';
123 if (IS_FUNCTION(obj)) { 93 if (IS_FUNCTION(obj)) {
124 var str = %_Call(FunctionSourceString, obj, obj); 94 var str = %_Call(FunctionSourceString, obj, obj);
125 if (str.length > 128) { 95 if (str.length > 128) {
126 str = %_SubString(str, 0, 111) + "...<omitted>..." + 96 str = %_SubString(str, 0, 111) + "...<omitted>..." +
127 %_SubString(str, str.length - 2, str.length); 97 %_SubString(str, str.length - 2, str.length);
128 } 98 }
129 return str; 99 return str;
130 } 100 }
131 if (IS_SYMBOL(obj)) return %_Call(SymbolToString, obj); 101 if (IS_SYMBOL(obj)) return %_Call(SymbolToString, obj);
132 if (IS_SIMD_VALUE(obj)) { 102 if (IS_SIMD_VALUE(obj)) {
133 switch (typeof(obj)) { 103 switch (typeof(obj)) {
134 case 'float32x4': return %_Call(Float32x4ToString, obj); 104 case 'float32x4': return %_Call(Float32x4ToString, obj);
135 case 'int32x4': return %_Call(Int32x4ToString, obj); 105 case 'int32x4': return %_Call(Int32x4ToString, obj);
136 case 'int16x8': return %_Call(Int16x8ToString, obj); 106 case 'int16x8': return %_Call(Int16x8ToString, obj);
137 case 'int8x16': return %_Call(Int8x16ToString, obj); 107 case 'int8x16': return %_Call(Int8x16ToString, obj);
138 case 'uint32x4': return %_Call(Uint32x4ToString, obj); 108 case 'uint32x4': return %_Call(Uint32x4ToString, obj);
139 case 'uint16x8': return %_Call(Uint16x8ToString, obj); 109 case 'uint16x8': return %_Call(Uint16x8ToString, obj);
140 case 'uint8x16': return %_Call(Uint8x16ToString, obj); 110 case 'uint8x16': return %_Call(Uint8x16ToString, obj);
141 case 'bool32x4': return %_Call(Bool32x4ToString, obj); 111 case 'bool32x4': return %_Call(Bool32x4ToString, obj);
142 case 'bool16x8': return %_Call(Bool16x8ToString, obj); 112 case 'bool16x8': return %_Call(Bool16x8ToString, obj);
143 case 'bool8x16': return %_Call(Bool8x16ToString, obj); 113 case 'bool8x16': return %_Call(Bool8x16ToString, obj);
144 } 114 }
145 } 115 }
146 116 if (IS_OBJECT(obj)
147 if (IS_SPEC_OBJECT(obj)) { 117 && %GetDataProperty(obj, "toString") === ObjectToString) {
148 // When internally formatting error objects, use a side-effects-free version 118 var constructor = %GetDataProperty(obj, "constructor");
149 // of Error.prototype.toString independent of the actually installed 119 if (typeof constructor == "function") {
150 // toString method. 120 var constructorName = constructor.name;
151 if (IsErrorObject(obj) || 121 if (IS_STRING(constructorName) && constructorName !== "") {
152 %GetDataProperty(obj, "toString") === ErrorToString) { 122 return "#<" + constructorName + ">";
153 return %_Call(NoSideEffectsErrorToString, obj);
154 }
155
156 if (%GetDataProperty(obj, "toString") === ObjectToString) {
157 var constructor = %GetDataProperty(obj, "constructor");
158 if (IS_FUNCTION(constructor)) {
159 var constructor_name = %FunctionGetName(constructor);
160 if (constructor_name != "") return `#<${constructor_name}>`;
161 } 123 }
162 } 124 }
163 } 125 }
126 if (CanBeSafelyTreatedAsAnErrorObject(obj)) {
127 return %_Call(ErrorToString, obj);
128 }
164 129
165 return %_Call(NoSideEffectsObjectToString, obj); 130 return %_Call(NoSideEffectsObjectToString, obj);
166 } 131 }
167 132
133 // To determine whether we can safely stringify an object using ErrorToString
134 // without the risk of side-effects, we need to check whether the object is
135 // either an instance of a native error type (via '%_ClassOf'), or has Error
136 // in its prototype chain and hasn't overwritten 'toString' with something
137 // strange and unusual.
138 function CanBeSafelyTreatedAsAnErrorObject(obj) {
139 switch (%_ClassOf(obj)) {
140 case 'Error':
141 case 'EvalError':
142 case 'RangeError':
143 case 'ReferenceError':
144 case 'SyntaxError':
145 case 'TypeError':
146 case 'URIError':
147 return true;
148 }
168 149
150 var objToString = %GetDataProperty(obj, "toString");
151 return obj instanceof GlobalError && objToString === ErrorToString;
152 }
153
154
155 // When formatting internally created error messages, do not
156 // invoke overwritten error toString methods but explicitly use
157 // the error to string method. This is to avoid leaking error
158 // objects between script tags in a browser setting.
159 function ToStringCheckErrorObject(obj) {
160 if (CanBeSafelyTreatedAsAnErrorObject(obj)) {
161 return %_Call(ErrorToString, obj);
162 } else {
163 return TO_STRING(obj);
164 }
165 }
166
167
168 function ToDetailString(obj) {
169 if (obj != null && IS_OBJECT(obj) && obj.toString === ObjectToString) {
170 var constructor = obj.constructor;
171 if (typeof constructor == "function") {
172 var constructorName = constructor.name;
173 if (IS_STRING(constructorName) && constructorName !== "") {
174 return "#<" + constructorName + ">";
175 }
176 }
177 }
178 return ToStringCheckErrorObject(obj);
179 }
180
181
169 function MakeGenericError(constructor, type, arg0, arg1, arg2) { 182 function MakeGenericError(constructor, type, arg0, arg1, arg2) {
170 var error = new constructor(FormatMessage(type, arg0, arg1, arg2)); 183 var error = new constructor(FormatMessage(type, arg0, arg1, arg2));
171 error[internalErrorSymbol] = true; 184 error[internalErrorSymbol] = true;
172 return error; 185 return error;
173 } 186 }
174 187
175 188
176 /** 189 /**
177 * Set up the Script function and constructor. 190 * Set up the Script function and constructor.
178 */ 191 */
179 %FunctionSetInstanceClassName(Script, 'Script'); 192 %FunctionSetInstanceClassName(Script, 'Script');
180 %AddNamedProperty(Script.prototype, 'constructor', Script, 193 %AddNamedProperty(Script.prototype, 'constructor', Script,
181 DONT_ENUM | DONT_DELETE | READ_ONLY); 194 DONT_ENUM | DONT_DELETE | READ_ONLY);
182 %SetCode(Script, function(x) { 195 %SetCode(Script, function(x) {
183 // Script objects can only be created by the VM. 196 // Script objects can only be created by the VM.
184 throw MakeError(kUnsupported); 197 throw MakeError(kUnsupported);
185 }); 198 });
186 199
187 200
188 // Helper functions; called from the runtime system. 201 // Helper functions; called from the runtime system.
189 function FormatMessage(type, arg0, arg1, arg2) { 202 function FormatMessage(type, arg0, arg1, arg2) {
190 var arg0 = NoSideEffectsToString(arg0); 203 var arg0 = NoSideEffectToString(arg0);
191 var arg1 = NoSideEffectsToString(arg1); 204 var arg1 = NoSideEffectToString(arg1);
192 var arg2 = NoSideEffectsToString(arg2); 205 var arg2 = NoSideEffectToString(arg2);
193 try { 206 try {
194 return %FormatMessageString(type, arg0, arg1, arg2); 207 return %FormatMessageString(type, arg0, arg1, arg2);
195 } catch (e) { 208 } catch (e) {
196 return "<error>"; 209 return "<error>";
197 } 210 }
198 } 211 }
199 212
200 213
201 function GetLineNumber(message) { 214 function GetLineNumber(message) {
202 var start_position = %MessageGetStartPosition(message); 215 var start_position = %MessageGetStartPosition(message);
(...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after
829 } 842 }
830 } 843 }
831 lines.push(" at " + line); 844 lines.push(" at " + line);
832 } 845 }
833 return %_Call(ArrayJoin, lines, "\n"); 846 return %_Call(ArrayJoin, lines, "\n");
834 } 847 }
835 848
836 849
837 function GetTypeName(receiver, requireConstructor) { 850 function GetTypeName(receiver, requireConstructor) {
838 if (IS_NULL_OR_UNDEFINED(receiver)) return null; 851 if (IS_NULL_OR_UNDEFINED(receiver)) return null;
839 if (%_IsJSProxy(receiver)) return "Proxy"; 852 if (%_IsJSProxy(receiver)) {
840 853 return "Proxy";
841 var constructor = %GetDataProperty(TO_OBJECT(receiver), "constructor"); 854 };
842 if (!IS_FUNCTION(constructor)) { 855 var constructor = receiver.constructor;
843 return requireConstructor ? null : %_Call(NoSideEffectsToString, receiver); 856 if (!constructor) {
857 return requireConstructor ? null :
858 %_Call(NoSideEffectsObjectToString, receiver);
844 } 859 }
845 return %FunctionGetName(constructor); 860 var constructorName = constructor.name;
861 if (!constructorName) {
862 return requireConstructor ? null :
863 %_Call(NoSideEffectsObjectToString, receiver);
864 }
865 return constructorName;
846 } 866 }
847 867
848 868
849 // Format the stack trace if not yet done, and return it. 869 // Format the stack trace if not yet done, and return it.
850 // Cache the formatted stack trace on the holder. 870 // Cache the formatted stack trace on the holder.
851 var StackTraceGetter = function() { 871 var StackTraceGetter = function() {
852 var formatted_stack_trace = UNDEFINED; 872 var formatted_stack_trace = UNDEFINED;
853 var holder = this; 873 var holder = this;
854 while (holder) { 874 while (holder) {
855 var formatted_stack_trace = 875 var formatted_stack_trace =
(...skipping 13 matching lines...) Expand all
869 } 889 }
870 return formatted_stack_trace; 890 return formatted_stack_trace;
871 } 891 }
872 return UNDEFINED; 892 return UNDEFINED;
873 }; 893 };
874 894
875 895
876 // If the receiver equals the holder, set the formatted stack trace that the 896 // If the receiver equals the holder, set the formatted stack trace that the
877 // getter returns. 897 // getter returns.
878 var StackTraceSetter = function(v) { 898 var StackTraceSetter = function(v) {
879 if (IsErrorObject(this)) { 899 if (HAS_PRIVATE(this, stackTraceSymbol)) {
880 SET_PRIVATE(this, stackTraceSymbol, UNDEFINED); 900 SET_PRIVATE(this, stackTraceSymbol, UNDEFINED);
881 SET_PRIVATE(this, formattedStackTraceSymbol, v); 901 SET_PRIVATE(this, formattedStackTraceSymbol, v);
882 } 902 }
883 }; 903 };
884 904
885 905
886 // Use a dummy function since we do not actually want to capture a stack trace 906 // Use a dummy function since we do not actually want to capture a stack trace
887 // when constructing the initial Error prototytpes. 907 // when constructing the initial Error prototytpes.
888 var captureStackTrace = function() {}; 908 var captureStackTrace = function() {};
889 909
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
929 GlobalURIError = SetUpError(global.URIError); 949 GlobalURIError = SetUpError(global.URIError);
930 950
931 utils.InstallFunctions(GlobalError.prototype, DONT_ENUM, 951 utils.InstallFunctions(GlobalError.prototype, DONT_ENUM,
932 ['toString', ErrorToString]); 952 ['toString', ErrorToString]);
933 953
934 function ErrorToString() { 954 function ErrorToString() {
935 if (!IS_SPEC_OBJECT(this)) { 955 if (!IS_SPEC_OBJECT(this)) {
936 throw MakeTypeError(kCalledOnNonObject, "Error.prototype.toString"); 956 throw MakeTypeError(kCalledOnNonObject, "Error.prototype.toString");
937 } 957 }
938 958
939 var name = this.name; 959 return %ErrorToStringRT(this);
940 name = IS_UNDEFINED(name) ? "Error" : TO_STRING(name);
941
942 var message = this.message;
943 message = IS_UNDEFINED(message) ? "" : TO_STRING(message);
944
945 if (name == "") return message;
946 if (message == "") return name;
947 return `${name}: ${message}`
948 } 960 }
949 961
950 function MakeError(type, arg0, arg1, arg2) { 962 function MakeError(type, arg0, arg1, arg2) {
951 return MakeGenericError(GlobalError, type, arg0, arg1, arg2); 963 return MakeGenericError(GlobalError, type, arg0, arg1, arg2);
952 } 964 }
953 965
954 function MakeRangeError(type, arg0, arg1, arg2) { 966 function MakeRangeError(type, arg0, arg1, arg2) {
955 return MakeGenericError(GlobalRangeError, type, arg0, arg1, arg2); 967 return MakeGenericError(GlobalRangeError, type, arg0, arg1, arg2);
956 } 968 }
957 969
(...skipping 27 matching lines...) Expand all
985 GlobalError.captureStackTrace = captureStackTrace; 997 GlobalError.captureStackTrace = captureStackTrace;
986 998
987 %InstallToContext([ 999 %InstallToContext([
988 "get_stack_trace_line_fun", GetStackTraceLine, 1000 "get_stack_trace_line_fun", GetStackTraceLine,
989 "make_error_function", MakeGenericError, 1001 "make_error_function", MakeGenericError,
990 "make_range_error", MakeRangeError, 1002 "make_range_error", MakeRangeError,
991 "make_type_error", MakeTypeError, 1003 "make_type_error", MakeTypeError,
992 "message_get_column_number", GetColumnNumber, 1004 "message_get_column_number", GetColumnNumber,
993 "message_get_line_number", GetLineNumber, 1005 "message_get_line_number", GetLineNumber,
994 "message_get_source_line", GetSourceLine, 1006 "message_get_source_line", GetSourceLine,
995 "no_side_effects_to_string_fun", NoSideEffectsToString, 1007 "no_side_effect_to_string_fun", NoSideEffectToString,
996 "stack_overflow_boilerplate", StackOverflowBoilerplate, 1008 "stack_overflow_boilerplate", StackOverflowBoilerplate,
1009 "to_detail_string_fun", ToDetailString,
997 ]); 1010 ]);
998 1011
999 utils.Export(function(to) { 1012 utils.Export(function(to) {
1000 to.ErrorToString = ErrorToString; 1013 to.ErrorToString = ErrorToString;
1001 to.MakeError = MakeError; 1014 to.MakeError = MakeError;
1002 to.MakeRangeError = MakeRangeError; 1015 to.MakeRangeError = MakeRangeError;
1003 to.MakeSyntaxError = MakeSyntaxError; 1016 to.MakeSyntaxError = MakeSyntaxError;
1004 to.MakeTypeError = MakeTypeError; 1017 to.MakeTypeError = MakeTypeError;
1005 to.MakeURIError = MakeURIError; 1018 to.MakeURIError = MakeURIError;
1006 }); 1019 });
1007 1020
1008 }); 1021 });
OLDNEW
« no previous file with comments | « src/isolate.h ('k') | src/messages.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698