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

Side by Side Diff: src/messages.js

Issue 6287041: Avoid callbacks to user code during error formatting in a couple of (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 9 years, 10 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 | Annotate | Revision Log
OLDNEW
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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 var kNoLineNumberInfo = 0; 46 var kNoLineNumberInfo = 0;
47 47
48 // If this object gets passed to an error constructor the error will 48 // If this object gets passed to an error constructor the error will
49 // get an accessor for .message that constructs a descriptive error 49 // get an accessor for .message that constructs a descriptive error
50 // message on access. 50 // message on access.
51 var kAddMessageAccessorsMarker = { }; 51 var kAddMessageAccessorsMarker = { };
52 52
53 var kMessages = 0; 53 var kMessages = 0;
54 54
55 var kReplacementMarkers = 55 var kReplacementMarkers =
56 [ "%0", "%1", "%2", "%3", "%4", "%5", "%6", "%7", "%8", "%9", "%10" ]; 56 [ "%0", "%1", "%2", "%3" ]
57 57
58 function FormatString(format, args) { 58 function FormatString(format, args) {
59 var result = format; 59 var result = "";
60 for (var i = 0; i < args.length; i++) { 60 var arg_num = 0;
61 var str; 61 for (var i = 0; i < format.length; i++) {
62 try { 62 var str = format[i];
63 str = ToDetailString(args[i]); 63 for (arg_num = 0; arg_num < kReplacementMarkers.length; arg_num++) {
64 } catch (e) { 64 if (format[i] !== kReplacementMarkers[arg_num]) continue;
65 str = "#<error>"; 65 try {
66 str = ToDetailString(args[arg_num]);
67 } catch (e) {
68 str = "#<error>";
69 }
66 } 70 }
67 var replacement_marker = kReplacementMarkers[i]; 71 result += str;
68 var split = %_CallFunction(result, replacement_marker, StringSplit);
69 result = %_CallFunction(split, str, ArrayJoin);
70 } 72 }
71 return result; 73 return result;
72 } 74 }
73 75
74 76
75 // To check if something is a native error we need to check the 77 // To check if something is a native error we need to check the
76 // concrete native error types. It is not enough to check "obj 78 // concrete native error types. It is not enough to check "obj
77 // instanceof $Error" because user code can replace 79 // instanceof $Error" because user code can replace
78 // NativeError.prototype.__proto__. User code cannot replace 80 // NativeError.prototype.__proto__. User code cannot replace
79 // NativeError.prototype though and therefore this is a safe test. 81 // NativeError.prototype though and therefore this is a safe test.
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 // Script objects can only be created by the VM. 138 // Script objects can only be created by the VM.
137 throw new $Error("Not supported"); 139 throw new $Error("Not supported");
138 }); 140 });
139 141
140 142
141 // Helper functions; called from the runtime system. 143 // Helper functions; called from the runtime system.
142 function FormatMessage(message) { 144 function FormatMessage(message) {
143 if (kMessages === 0) { 145 if (kMessages === 0) {
144 kMessages = { 146 kMessages = {
145 // Error 147 // Error
146 cyclic_proto: "Cyclic __proto__ value", 148 cyclic_proto: ["Cyclic __proto__ value"],
147 // TypeError 149 // TypeError
148 unexpected_token: "Unexpected token %0", 150 unexpected_token: ["Unexpected token ", "%0"],
149 unexpected_token_number: "Unexpected number", 151 unexpected_token_number: ["Unexpected number"],
150 unexpected_token_string: "Unexpected string", 152 unexpected_token_string: ["Unexpected string"],
151 unexpected_token_identifier: "Unexpected identifier", 153 unexpected_token_identifier: ["Unexpected identifier"],
152 unexpected_eos: "Unexpected end of input", 154 unexpected_eos: ["Unexpected end of input"],
153 malformed_regexp: "Invalid regular expression: /%0/: %1", 155 malformed_regexp: ["Invalid regular expression: /", "%0", "/: ", "%1"],
154 unterminated_regexp: "Invalid regular expression: missing /", 156 unterminated_regexp: ["Invalid regular expression: missing /"],
155 regexp_flags: "Cannot supply flags when constructing one R egExp from another", 157 regexp_flags: ["Cannot supply flags when constructing one RegExp from another"],
156 incompatible_method_receiver: "Method %0 called on incompatible receiver % 1", 158 incompatible_method_receiver: ["Method ", "%0", " called on incompatible r eceiver ", "%1"],
157 invalid_lhs_in_assignment: "Invalid left-hand side in assignment", 159 invalid_lhs_in_assignment: ["Invalid left-hand side in assignment"],
158 invalid_lhs_in_for_in: "Invalid left-hand side in for-in", 160 invalid_lhs_in_for_in: ["Invalid left-hand side in for-in"],
159 invalid_lhs_in_postfix_op: "Invalid left-hand side expression in postfi x operation", 161 invalid_lhs_in_postfix_op: ["Invalid left-hand side expression in postf ix operation"],
160 invalid_lhs_in_prefix_op: "Invalid left-hand side expression in prefix operation", 162 invalid_lhs_in_prefix_op: ["Invalid left-hand side expression in prefi x operation"],
161 multiple_defaults_in_switch: "More than one default clause in switch stat ement", 163 multiple_defaults_in_switch: ["More than one default clause in switch sta tement"],
162 newline_after_throw: "Illegal newline after throw", 164 newline_after_throw: ["Illegal newline after throw"],
163 redeclaration: "%0 '%1' has already been declared", 165 redeclaration: ["%0", " '", "%1", "' has already been decla red"],
164 no_catch_or_finally: "Missing catch or finally after try", 166 no_catch_or_finally: ["Missing catch or finally after try"],
165 unknown_label: "Undefined label '%0'", 167 unknown_label: ["Undefined label '", "%0", "'"],
166 uncaught_exception: "Uncaught %0", 168 uncaught_exception: ["Uncaught ", "%0"],
167 stack_trace: "Stack Trace:\n%0", 169 stack_trace: ["Stack Trace:\n", "%0"],
168 called_non_callable: "%0 is not a function", 170 called_non_callable: ["%0", " is not a function"],
169 undefined_method: "Object %1 has no method '%0'", 171 undefined_method: ["Object ", "%1", " has no method '", "%0", "'"],
170 property_not_function: "Property '%0' of object %1 is not a functio n", 172 property_not_function: ["Property '", "%0", "' of object ", "%1", " is not a function"],
171 cannot_convert_to_primitive: "Cannot convert object to primitive value", 173 cannot_convert_to_primitive: ["Cannot convert object to primitive value"] ,
172 not_constructor: "%0 is not a constructor", 174 not_constructor: ["%0", " is not a constructor"],
173 not_defined: "%0 is not defined", 175 not_defined: ["%0", " is not defined"],
174 non_object_property_load: "Cannot read property '%0' of %1", 176 non_object_property_load: ["Cannot read property '", "%0", "' of ", "% 1"],
175 non_object_property_store: "Cannot set property '%0' of %1", 177 non_object_property_store: ["Cannot set property '", "%0", "' of ", "%1 "],
176 non_object_property_call: "Cannot call method '%0' of %1", 178 non_object_property_call: ["Cannot call method '", "%0", "' of ", "%1" ],
177 with_expression: "%0 has no properties", 179 with_expression: ["%0", " has no properties"],
178 illegal_invocation: "Illegal invocation", 180 illegal_invocation: ["Illegal invocation"],
179 no_setter_in_callback: "Cannot set property %0 of %1 which has only a getter", 181 no_setter_in_callback: ["Cannot set property ", "%0", " of ", "%1", " which has only a getter"],
180 apply_non_function: "Function.prototype.apply was called on %0, which is a %1 and not a function", 182 apply_non_function: ["Function.prototype.apply was called on ", "%0", ", which is a ", "%1", " and not a function"],
181 apply_wrong_args: "Function.prototype.apply: Arguments list ha s wrong type", 183 apply_wrong_args: ["Function.prototype.apply: Arguments list h as wrong type"],
182 invalid_in_operator_use: "Cannot use 'in' operator to search for '%0' in %1", 184 invalid_in_operator_use: ["Cannot use 'in' operator to search for '", "%0", "' in ", "%1"],
183 instanceof_function_expected: "Expecting a function in instanceof check, b ut got %0", 185 instanceof_function_expected: ["Expecting a function in instanceof check, but got ", "%0"],
184 instanceof_nonobject_proto: "Function has non-object prototype '%0' in i nstanceof check", 186 instanceof_nonobject_proto: ["Function has non-object prototype '", "%0" , "' in instanceof check"],
185 null_to_object: "Cannot convert null to object", 187 null_to_object: ["Cannot convert null to object"],
186 reduce_no_initial: "Reduce of empty array with no initial value ", 188 reduce_no_initial: ["Reduce of empty array with no initial valu e"],
187 getter_must_be_callable: "Getter must be a function: %0", 189 getter_must_be_callable: ["Getter must be a function: ", "%0"],
188 setter_must_be_callable: "Setter must be a function: %0", 190 setter_must_be_callable: ["Setter must be a function: ", "%0"],
189 value_and_accessor: "Invalid property. A property cannot both h ave accessors and be writable or have a value: %0", 191 value_and_accessor: ["Invalid property. A property cannot both have accessors and be writable or have a value: ", "%0"],
190 proto_object_or_null: "Object prototype may only be an Object or n ull", 192 proto_object_or_null: ["Object prototype may only be an Object or null"],
191 property_desc_object: "Property description must be an object: %0" , 193 property_desc_object: ["Property description must be an object: ", "%0"],
192 redefine_disallowed: "Cannot redefine property: %0", 194 redefine_disallowed: ["Cannot redefine property: ", "%0"],
193 define_disallowed: "Cannot define property, object is not exten sible: %0", 195 define_disallowed: ["Cannot define property, object is not exte nsible: ", "%0"],
194 // RangeError 196 // RangeError
195 invalid_array_length: "Invalid array length", 197 invalid_array_length: ["Invalid array length"],
196 stack_overflow: "Maximum call stack size exceeded", 198 stack_overflow: ["Maximum call stack size exceeded"],
197 // SyntaxError 199 // SyntaxError
198 unable_to_parse: "Parse error", 200 unable_to_parse: ["Parse error"],
199 duplicate_regexp_flag: "Duplicate RegExp flag %0", 201 duplicate_regexp_flag: ["Duplicate RegExp flag ", "%0"],
200 invalid_regexp: "Invalid RegExp pattern /%0/", 202 invalid_regexp: ["Invalid RegExp pattern /", "%0", "/"],
201 illegal_break: "Illegal break statement", 203 illegal_break: ["Illegal break statement"],
202 illegal_continue: "Illegal continue statement", 204 illegal_continue: ["Illegal continue statement"],
203 illegal_return: "Illegal return statement", 205 illegal_return: ["Illegal return statement"],
204 error_loading_debugger: "Error loading debugger", 206 error_loading_debugger: ["Error loading debugger"],
205 no_input_to_regexp: "No input to %0", 207 no_input_to_regexp: ["No input to ", "%0"],
206 invalid_json: "String '%0' is not valid JSON", 208 invalid_json: ["String '", "%0", "' is not valid JSON"],
207 circular_structure: "Converting circular structure to JSON", 209 circular_structure: ["Converting circular structure to JSON"],
208 obj_ctor_property_non_object: "Object.%0 called on non-object", 210 obj_ctor_property_non_object: ["Object.", "%0", " called on non-object"],
209 array_indexof_not_defined: "Array.getIndexOf: Argument undefined", 211 array_indexof_not_defined: ["Array.getIndexOf: Argument undefined"],
210 object_not_extensible: "Can't add property %0, object is not extens ible", 212 object_not_extensible: ["Can't add property ", "%0", ", object is n ot extensible"],
211 illegal_access: "Illegal access", 213 illegal_access: ["Illegal access"],
212 invalid_preparser_data: "Invalid preparser data for function %0", 214 invalid_preparser_data: ["Invalid preparser data for function ", "%0 "],
213 strict_mode_with: "Strict mode code may not include a with sta tement", 215 strict_mode_with: ["Strict mode code may not include a with st atement"],
214 strict_catch_variable: "Catch variable may not be eval or arguments in strict mode", 216 strict_catch_variable: ["Catch variable may not be eval or argument s in strict mode"],
215 strict_param_name: "Parameter name eval or arguments is not all owed in strict mode", 217 strict_param_name: ["Parameter name eval or arguments is not al lowed in strict mode"],
216 strict_param_dupe: "Strict mode function may not have duplicate parameter names", 218 strict_param_dupe: ["Strict mode function may not have duplicat e parameter names"],
217 strict_var_name: "Variable name may not be eval or arguments in strict mode", 219 strict_var_name: ["Variable name may not be eval or arguments in strict mode"],
218 strict_function_name: "Function name may not be eval or arguments in strict mode", 220 strict_function_name: ["Function name may not be eval or arguments in strict mode"],
219 strict_octal_literal: "Octal literals are not allowed in strict mo de.", 221 strict_octal_literal: ["Octal literals are not allowed in strict m ode."],
220 strict_duplicate_property: "Duplicate data property in object literal n ot allowed in strict mode", 222 strict_duplicate_property: ["Duplicate data property in object literal not allowed in strict mode"],
221 accessor_data_property: "Object literal may not have data and access or property with the same name", 223 accessor_data_property: ["Object literal may not have data and acces sor property with the same name"],
222 accessor_get_set: "Object literal may not have multiple get/se t accessors with the same name", 224 accessor_get_set: ["Object literal may not have multiple get/s et accessors with the same name"],
223 strict_lhs_eval_assignment: "Assignment to eval or arguments is not allo wed in strict mode", 225 strict_lhs_eval_assignment: ["Assignment to eval or arguments is not all owed in strict mode"],
224 strict_lhs_postfix: "Postfix increment/decrement may not have ev al or arguments operand in strict mode", 226 strict_lhs_postfix: ["Postfix increment/decrement may not have e val or arguments operand in strict mode"],
225 strict_lhs_prefix: "Prefix increment/decrement may not have eva l or arguments operand in strict mode", 227 strict_lhs_prefix: ["Prefix increment/decrement may not have ev al or arguments operand in strict mode"],
226 }; 228 };
227 } 229 }
228 var format = kMessages[message.type]; 230 var format = kMessages[message.type];
229 if (!format) return "<unknown message " + message.type + ">"; 231 if (!format) return "<unknown message " + message.type + ">";
230 return FormatString(format, message.args); 232 return FormatString(format, message.args);
231 } 233 }
232 234
233 235
234 function GetLineNumber(message) { 236 function GetLineNumber(message) {
235 if (message.startPos == -1) return kNoLineNumberInfo; 237 if (message.startPos == -1) return kNoLineNumberInfo;
(...skipping 795 matching lines...) Expand 10 before | Expand all | Expand 10 after
1031 1033
1032 // Global list of error objects visited during errorToString. This is 1034 // Global list of error objects visited during errorToString. This is
1033 // used to detect cycles in error toString formatting. 1035 // used to detect cycles in error toString formatting.
1034 var visited_errors = new $Array(); 1036 var visited_errors = new $Array();
1035 var cyclic_error_marker = new $Object(); 1037 var cyclic_error_marker = new $Object();
1036 1038
1037 function errorToStringDetectCycle() { 1039 function errorToStringDetectCycle() {
1038 if (!%PushIfAbsent(visited_errors, this)) throw cyclic_error_marker; 1040 if (!%PushIfAbsent(visited_errors, this)) throw cyclic_error_marker;
1039 try { 1041 try {
1040 var type = this.type; 1042 var type = this.type;
1041 if (type && !this.hasOwnProperty("message")) { 1043 if (type && !%_CallFunction(this, "message", ObjectHasOwnProperty)) {
1042 var formatted = FormatMessage({ type: type, args: this.arguments }); 1044 var formatted = FormatMessage({ type: type, args: this.arguments });
1043 return this.name + ": " + formatted; 1045 return this.name + ": " + formatted;
1044 } 1046 }
1045 var message = this.hasOwnProperty("message") ? (": " + this.message) : ""; 1047 var message = %_CallFunction(this, "message", ObjectHasOwnProperty)
1048 ? (": " + this.message)
1049 : "";
1046 return this.name + message; 1050 return this.name + message;
1047 } finally { 1051 } finally {
1048 visited_errors.pop(); 1052 visited_errors.length = visited_errors.length - 1;
1049 } 1053 }
1050 } 1054 }
1051 1055
1052 function errorToString() { 1056 function errorToString() {
1053 // This helper function is needed because access to properties on 1057 // This helper function is needed because access to properties on
1054 // the builtins object do not work inside of a catch clause. 1058 // the builtins object do not work inside of a catch clause.
1055 function isCyclicErrorMarker(o) { return o === cyclic_error_marker; } 1059 function isCyclicErrorMarker(o) { return o === cyclic_error_marker; }
1056 1060
1057 try { 1061 try {
1058 return %_CallFunction(this, errorToStringDetectCycle); 1062 return %_CallFunction(this, errorToStringDetectCycle);
1059 } catch(e) { 1063 } catch(e) {
1060 // If this error message was encountered already return the empty 1064 // If this error message was encountered already return the empty
1061 // string for it instead of recursively formatting it. 1065 // string for it instead of recursively formatting it.
1062 if (isCyclicErrorMarker(e)) return ''; 1066 if (isCyclicErrorMarker(e)) return '';
1063 else throw e; 1067 else throw e;
1064 } 1068 }
1065 } 1069 }
1066 1070
1067 %FunctionSetName(errorToString, 'toString'); 1071 %FunctionSetName(errorToString, 'toString');
1068 %SetProperty($Error.prototype, 'toString', errorToString, DONT_ENUM); 1072 %SetProperty($Error.prototype, 'toString', errorToString, DONT_ENUM);
1069 1073
1070 // Boilerplate for exceptions for stack overflows. Used from 1074 // Boilerplate for exceptions for stack overflows. Used from
1071 // Top::StackOverflow(). 1075 // Top::StackOverflow().
1072 const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []); 1076 const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []);
OLDNEW
« src/array.js ('K') | « src/array.js ('k') | src/mirror-debugger.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698