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 26 matching lines...) Expand all Loading... |
37 var COMPILATION_TYPE_HOST = 0; | 37 var COMPILATION_TYPE_HOST = 0; |
38 var COMPILATION_TYPE_EVAL = 1; | 38 var COMPILATION_TYPE_EVAL = 1; |
39 var COMPILATION_TYPE_JSON = 2; | 39 var COMPILATION_TYPE_JSON = 2; |
40 | 40 |
41 // Matches Messages::kNoLineNumberInfo from v8.h | 41 // Matches Messages::kNoLineNumberInfo from v8.h |
42 var kNoLineNumberInfo = 0; | 42 var kNoLineNumberInfo = 0; |
43 | 43 |
44 // If this object gets passed to an error constructor the error will | 44 // If this object gets passed to an error constructor the error will |
45 // get an accessor for .message that constructs a descriptive error | 45 // get an accessor for .message that constructs a descriptive error |
46 // message on access. | 46 // message on access. |
47 var kAddMessageAccessorsMarker = { }; | 47 const kAddMessageAccessorsMarker = { }; |
48 | 48 |
49 var kMessages = 0; | 49 // This will be lazily initialized when first needed (and forcibly |
50 | 50 // overwritten even though it's const). |
51 var kReplacementMarkers = [ "%0", "%1", "%2", "%3" ]; | 51 const kMessages = 0; |
52 | 52 |
53 function FormatString(format, message) { | 53 function FormatString(format, message) { |
54 var args = %MessageGetArguments(message); | 54 var args = %MessageGetArguments(message); |
55 var result = ""; | 55 var result = ""; |
56 var arg_num = 0; | 56 var arg_num = 0; |
57 for (var i = 0; i < format.length; i++) { | 57 for (var i = 0; i < format.length; i++) { |
58 var str = format[i]; | 58 var str = format[i]; |
59 for (arg_num = 0; arg_num < kReplacementMarkers.length; arg_num++) { | 59 if (str.length == 2 && %_StringCharCodeAt(str, 0) == 0x25) { |
60 if (str == kReplacementMarkers[arg_num]) { | 60 // Two-char string starts with "%". |
| 61 var arg_num = (%_StringCharCodeAt(str, 1) - 0x30) >>> 0; |
| 62 if (arg_num < 4) { |
| 63 // str is one of %0, %1, %2 or %3. |
61 try { | 64 try { |
62 str = ToDetailString(args[arg_num]); | 65 str = ToDetailString(args[arg_num]); |
63 } catch (e) { | 66 } catch (e) { |
64 str = "#<error>"; | 67 str = "#<error>"; |
65 } | 68 } |
66 break; | |
67 } | 69 } |
68 } | 70 } |
69 result += str; | 71 result += str; |
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 |
(...skipping 18 matching lines...) Expand all Loading... |
95 function ToStringCheckErrorObject(obj) { | 97 function ToStringCheckErrorObject(obj) { |
96 if (IsNativeErrorObject(obj)) { | 98 if (IsNativeErrorObject(obj)) { |
97 return %_CallFunction(obj, errorToString); | 99 return %_CallFunction(obj, errorToString); |
98 } else { | 100 } else { |
99 return ToString(obj); | 101 return ToString(obj); |
100 } | 102 } |
101 } | 103 } |
102 | 104 |
103 | 105 |
104 function ToDetailString(obj) { | 106 function ToDetailString(obj) { |
105 if (obj != null && IS_OBJECT(obj) && | 107 if (obj != null && IS_OBJECT(obj) && obj.toString === ObjectToString) { |
106 obj.toString === $Object.prototype.toString) { | |
107 var constructor = obj.constructor; | 108 var constructor = obj.constructor; |
108 if (!constructor) return ToStringCheckErrorObject(obj); | 109 if (typeof constructor == "function") { |
109 var constructorName = constructor.name; | 110 var constructorName = constructor.name; |
110 if (!constructorName || !IS_STRING(constructorName)) { | 111 if (IS_STRING(constructorName) && constructorName !== "") { |
111 return ToStringCheckErrorObject(obj); | 112 return "#<" + constructorName + ">"; |
| 113 } |
112 } | 114 } |
113 return "#<" + constructorName + ">"; | |
114 } else { | |
115 return ToStringCheckErrorObject(obj); | |
116 } | 115 } |
| 116 return ToStringCheckErrorObject(obj); |
117 } | 117 } |
118 | 118 |
119 | 119 |
120 function MakeGenericError(constructor, type, args) { | 120 function MakeGenericError(constructor, type, args) { |
121 if (IS_UNDEFINED(args)) { | 121 if (IS_UNDEFINED(args)) { |
122 args = []; | 122 args = []; |
123 } | 123 } |
124 var e = new constructor(kAddMessageAccessorsMarker); | 124 var e = new constructor(kAddMessageAccessorsMarker); |
125 e.type = type; | 125 e.type = type; |
126 e.arguments = args; | 126 e.arguments = args; |
127 return e; | 127 return e; |
128 } | 128 } |
129 | 129 |
130 | 130 |
131 /** | 131 /** |
132 * Setup the Script function and constructor. | 132 * Setup the Script function and constructor. |
133 */ | 133 */ |
134 %FunctionSetInstanceClassName(Script, 'Script'); | 134 %FunctionSetInstanceClassName(Script, 'Script'); |
135 %SetProperty(Script.prototype, 'constructor', Script, DONT_ENUM); | 135 %SetProperty(Script.prototype, 'constructor', Script, DONT_ENUM); |
136 %SetCode(Script, function(x) { | 136 %SetCode(Script, function(x) { |
137 // Script objects can only be created by the VM. | 137 // Script objects can only be created by the VM. |
138 throw new $Error("Not supported"); | 138 throw new $Error("Not supported"); |
139 }); | 139 }); |
140 | 140 |
141 | 141 |
142 // Helper functions; called from the runtime system. | 142 // Helper functions; called from the runtime system. |
143 function FormatMessage(message) { | 143 function FormatMessage(message) { |
144 if (kMessages === 0) { | 144 if (kMessages === 0) { |
145 kMessages = { | 145 var messagesDictionary = [ |
146 // Error | 146 // Error |
147 cyclic_proto: ["Cyclic __proto__ value"], | 147 "cyclic_proto", ["Cyclic __proto__ value"], |
148 code_gen_from_strings: ["Code generation from strings disallowed fo
r this context"], | 148 "code_gen_from_strings", ["Code generation from strings disallowed
for this context"], |
149 // TypeError | 149 // TypeError |
150 unexpected_token: ["Unexpected token ", "%0"], | 150 "unexpected_token", ["Unexpected token ", "%0"], |
151 unexpected_token_number: ["Unexpected number"], | 151 "unexpected_token_number", ["Unexpected number"], |
152 unexpected_token_string: ["Unexpected string"], | 152 "unexpected_token_string", ["Unexpected string"], |
153 unexpected_token_identifier: ["Unexpected identifier"], | 153 "unexpected_token_identifier", ["Unexpected identifier"], |
154 unexpected_reserved: ["Unexpected reserved word"], | 154 "unexpected_reserved", ["Unexpected reserved word"], |
155 unexpected_strict_reserved: ["Unexpected strict mode reserved word"], | 155 "unexpected_strict_reserved", ["Unexpected strict mode reserved word"], |
156 unexpected_eos: ["Unexpected end of input"], | 156 "unexpected_eos", ["Unexpected end of input"], |
157 malformed_regexp: ["Invalid regular expression: /", "%0", "/:
", "%1"], | 157 "malformed_regexp", ["Invalid regular expression: /", "%0", "/
: ", "%1"], |
158 unterminated_regexp: ["Invalid regular expression: missing /"], | 158 "unterminated_regexp", ["Invalid regular expression: missing /"], |
159 regexp_flags: ["Cannot supply flags when constructing one
RegExp from another"], | 159 "regexp_flags", ["Cannot supply flags when constructing on
e RegExp from another"], |
160 incompatible_method_receiver: ["Method ", "%0", " called on incompatible r
eceiver ", "%1"], | 160 "incompatible_method_receiver", ["Method ", "%0", " called on incompatible
receiver ", "%1"], |
161 invalid_lhs_in_assignment: ["Invalid left-hand side in assignment"], | 161 "invalid_lhs_in_assignment", ["Invalid left-hand side in assignment"], |
162 invalid_lhs_in_for_in: ["Invalid left-hand side in for-in"], | 162 "invalid_lhs_in_for_in", ["Invalid left-hand side in for-in"], |
163 invalid_lhs_in_postfix_op: ["Invalid left-hand side expression in postf
ix operation"], | 163 "invalid_lhs_in_postfix_op", ["Invalid left-hand side expression in pos
tfix operation"], |
164 invalid_lhs_in_prefix_op: ["Invalid left-hand side expression in prefi
x operation"], | 164 "invalid_lhs_in_prefix_op", ["Invalid left-hand side expression in pre
fix operation"], |
165 multiple_defaults_in_switch: ["More than one default clause in switch sta
tement"], | 165 "multiple_defaults_in_switch", ["More than one default clause in switch s
tatement"], |
166 newline_after_throw: ["Illegal newline after throw"], | 166 "newline_after_throw", ["Illegal newline after throw"], |
167 redeclaration: ["%0", " '", "%1", "' has already been decla
red"], | 167 "redeclaration", ["%0", " '", "%1", "' has already been dec
lared"], |
168 no_catch_or_finally: ["Missing catch or finally after try"], | 168 "no_catch_or_finally", ["Missing catch or finally after try"], |
169 unknown_label: ["Undefined label '", "%0", "'"], | 169 "unknown_label", ["Undefined label '", "%0", "'"], |
170 uncaught_exception: ["Uncaught ", "%0"], | 170 "uncaught_exception", ["Uncaught ", "%0"], |
171 stack_trace: ["Stack Trace:\n", "%0"], | 171 "stack_trace", ["Stack Trace:\n", "%0"], |
172 called_non_callable: ["%0", " is not a function"], | 172 "called_non_callable", ["%0", " is not a function"], |
173 undefined_method: ["Object ", "%1", " has no method '", "%0",
"'"], | 173 "undefined_method", ["Object ", "%1", " has no method '", "%0"
, "'"], |
174 property_not_function: ["Property '", "%0", "' of object ", "%1", "
is not a function"], | 174 "property_not_function", ["Property '", "%0", "' of object ", "%1",
" is not a function"], |
175 cannot_convert_to_primitive: ["Cannot convert object to primitive value"]
, | 175 "cannot_convert_to_primitive", ["Cannot convert object to primitive value
"], |
176 not_constructor: ["%0", " is not a constructor"], | 176 "not_constructor", ["%0", " is not a constructor"], |
177 not_defined: ["%0", " is not defined"], | 177 "not_defined", ["%0", " is not defined"], |
178 non_object_property_load: ["Cannot read property '", "%0", "' of ", "%
1"], | 178 "non_object_property_load", ["Cannot read property '", "%0", "' of ",
"%1"], |
179 non_object_property_store: ["Cannot set property '", "%0", "' of ", "%1
"], | 179 "non_object_property_store", ["Cannot set property '", "%0", "' of ", "
%1"], |
180 non_object_property_call: ["Cannot call method '", "%0", "' of ", "%1"
], | 180 "non_object_property_call", ["Cannot call method '", "%0", "' of ", "%
1"], |
181 with_expression: ["%0", " has no properties"], | 181 "with_expression", ["%0", " has no properties"], |
182 illegal_invocation: ["Illegal invocation"], | 182 "illegal_invocation", ["Illegal invocation"], |
183 no_setter_in_callback: ["Cannot set property ", "%0", " of ", "%1",
" which has only a getter"], | 183 "no_setter_in_callback", ["Cannot set property ", "%0", " of ", "%1
", " which has only a getter"], |
184 apply_non_function: ["Function.prototype.apply was called on ",
"%0", ", which is a ", "%1", " and not a function"], | 184 "apply_non_function", ["Function.prototype.apply was called on "
, "%0", ", which is a ", "%1", " and not a function"], |
185 apply_wrong_args: ["Function.prototype.apply: Arguments list h
as wrong type"], | 185 "apply_wrong_args", ["Function.prototype.apply: Arguments list
has wrong type"], |
186 invalid_in_operator_use: ["Cannot use 'in' operator to search for '",
"%0", "' in ", "%1"], | 186 "invalid_in_operator_use", ["Cannot use 'in' operator to search for '
", "%0", "' in ", "%1"], |
187 instanceof_function_expected: ["Expecting a function in instanceof check,
but got ", "%0"], | 187 "instanceof_function_expected", ["Expecting a function in instanceof check
, but got ", "%0"], |
188 instanceof_nonobject_proto: ["Function has non-object prototype '", "%0"
, "' in instanceof check"], | 188 "instanceof_nonobject_proto", ["Function has non-object prototype '", "%
0", "' in instanceof check"], |
189 null_to_object: ["Cannot convert null to object"], | 189 "null_to_object", ["Cannot convert null to object"], |
190 reduce_no_initial: ["Reduce of empty array with no initial valu
e"], | 190 "reduce_no_initial", ["Reduce of empty array with no initial va
lue"], |
191 getter_must_be_callable: ["Getter must be a function: ", "%0"], | 191 "getter_must_be_callable", ["Getter must be a function: ", "%0"], |
192 setter_must_be_callable: ["Setter must be a function: ", "%0"], | 192 "setter_must_be_callable", ["Setter must be a function: ", "%0"], |
193 value_and_accessor: ["Invalid property. A property cannot both
have accessors and be writable or have a value: ", "%0"], | 193 "value_and_accessor", ["Invalid property. A property cannot bot
h have accessors and be writable or have a value, ", "%0"], |
194 proto_object_or_null: ["Object prototype may only be an Object or
null"], | 194 "proto_object_or_null", ["Object prototype may only be an Object o
r null"], |
195 property_desc_object: ["Property description must be an object: ",
"%0"], | 195 "property_desc_object", ["Property description must be an object:
", "%0"], |
196 redefine_disallowed: ["Cannot redefine property: ", "%0"], | 196 "redefine_disallowed", ["Cannot redefine property: ", "%0"], |
197 define_disallowed: ["Cannot define property:", "%0", ", object
is not extensible."], | 197 "define_disallowed", ["Cannot define property:", "%0", ", objec
t is not extensible."], |
198 non_extensible_proto: ["%0", " is not extensible"], | 198 "non_extensible_proto", ["%0", " is not extensible"], |
199 handler_non_object: ["Proxy.", "%0", " called with non-object as
handler"], | 199 "handler_non_object", ["Proxy.", "%0", " called with non-object
as handler"], |
200 handler_trap_missing: ["Proxy handler ", "%0", " has no '", "%1",
"' trap"], | 200 "handler_trap_missing", ["Proxy handler ", "%0", " has no '", "%1"
, "' trap"], |
201 handler_trap_must_be_callable: ["Proxy handler ", "%0", " has non-callable
'", "%1", "' trap"], | 201 "handler_trap_must_be_callable", ["Proxy handler ", "%0", " has non-callab
le '", "%1", "' trap"], |
202 handler_returned_false: ["Proxy handler ", "%0", " returned false fo
r '", "%1", "' trap"], | 202 "handler_returned_false", ["Proxy handler ", "%0", " returned false
for '", "%1", "' trap"], |
203 handler_returned_undefined: ["Proxy handler ", "%0", " returned undefine
d for '", "%1", "' trap"], | 203 "handler_returned_undefined", ["Proxy handler ", "%0", " returned undefi
ned for '", "%1", "' trap"], |
204 proxy_prop_not_configurable: ["Trap ", "%1", " of proxy handler ", "%0",
" returned non-configurable descriptor for property ", "%2"], | 204 "proxy_prop_not_configurable", ["Trap ", "%1", " of proxy handler ", "%0"
, " returned non-configurable descriptor for property ", "%2"], |
205 proxy_non_object_prop_names: ["Trap ", "%1", " returned non-object ", "%0
"], | 205 "proxy_non_object_prop_names", ["Trap ", "%1", " returned non-object ", "
%0"], |
206 proxy_repeated_prop_name: ["Trap ", "%1", " returned repeated property
name ", "%2"], | 206 "proxy_repeated_prop_name", ["Trap ", "%1", " returned repeated proper
ty name ", "%2"], |
207 invalid_weakmap_key: ["Invalid value used as weak map key"], | 207 "invalid_weakmap_key", ["Invalid value used as weak map key"], |
208 // RangeError | 208 // RangeError |
209 invalid_array_length: ["Invalid array length"], | 209 "invalid_array_length", ["Invalid array length"], |
210 stack_overflow: ["Maximum call stack size exceeded"], | 210 "stack_overflow", ["Maximum call stack size exceeded"], |
211 // SyntaxError | 211 // SyntaxError |
212 unable_to_parse: ["Parse error"], | 212 "unable_to_parse", ["Parse error"], |
213 invalid_regexp_flags: ["Invalid flags supplied to RegExp construct
or '", "%0", "'"], | 213 "invalid_regexp_flags", ["Invalid flags supplied to RegExp constru
ctor '", "%0", "'"], |
214 invalid_regexp: ["Invalid RegExp pattern /", "%0", "/"], | 214 "invalid_regexp", ["Invalid RegExp pattern /", "%0", "/"], |
215 illegal_break: ["Illegal break statement"], | 215 "illegal_break", ["Illegal break statement"], |
216 illegal_continue: ["Illegal continue statement"], | 216 "illegal_continue", ["Illegal continue statement"], |
217 illegal_return: ["Illegal return statement"], | 217 "illegal_return", ["Illegal return statement"], |
218 error_loading_debugger: ["Error loading debugger"], | 218 "error_loading_debugger", ["Error loading debugger"], |
219 no_input_to_regexp: ["No input to ", "%0"], | 219 "no_input_to_regexp", ["No input to ", "%0"], |
220 invalid_json: ["String '", "%0", "' is not valid JSON"], | 220 "invalid_json", ["String '", "%0", "' is not valid JSON"], |
221 circular_structure: ["Converting circular structure to JSON"], | 221 "circular_structure", ["Converting circular structure to JSON"], |
222 obj_ctor_property_non_object: ["Object.", "%0", " called on non-object"], | 222 "obj_ctor_property_non_object", ["Object.", "%0", " called on non-object"]
, |
223 called_on_null_or_undefined: ["%0", " called on null or undefined"], | 223 "called_on_null_or_undefined", ["%0", " called on null or undefined"], |
224 array_indexof_not_defined: ["Array.getIndexOf: Argument undefined"], | 224 "array_indexof_not_defined", ["Array.getIndexOf: Argument undefined"], |
225 object_not_extensible: ["Can't add property ", "%0", ", object is n
ot extensible"], | 225 "object_not_extensible", ["Can't add property ", "%0", ", object is
not extensible"], |
226 illegal_access: ["Illegal access"], | 226 "illegal_access", ["Illegal access"], |
227 invalid_preparser_data: ["Invalid preparser data for function ", "%0
"], | 227 "invalid_preparser_data", ["Invalid preparser data for function ", "
%0"], |
228 strict_mode_with: ["Strict mode code may not include a with st
atement"], | 228 "strict_mode_with", ["Strict mode code may not include a with
statement"], |
229 strict_catch_variable: ["Catch variable may not be eval or argument
s in strict mode"], | 229 "strict_catch_variable", ["Catch variable may not be eval or argume
nts in strict mode"], |
230 too_many_arguments: ["Too many arguments in function call (only
32766 allowed)"], | 230 "too_many_arguments", ["Too many arguments in function call (onl
y 32766 allowed)"], |
231 too_many_parameters: ["Too many parameters in function definition
(only 32766 allowed)"], | 231 "too_many_parameters", ["Too many parameters in function definiti
on (only 32766 allowed)"], |
232 too_many_variables: ["Too many variables declared (only 32767 al
lowed)"], | 232 "too_many_variables", ["Too many variables declared (only 32767
allowed)"], |
233 strict_param_name: ["Parameter name eval or arguments is not al
lowed in strict mode"], | 233 "strict_param_name", ["Parameter name eval or arguments is not
allowed in strict mode"], |
234 strict_param_dupe: ["Strict mode function may not have duplicat
e parameter names"], | 234 "strict_param_dupe", ["Strict mode function may not have duplic
ate parameter names"], |
235 strict_var_name: ["Variable name may not be eval or arguments
in strict mode"], | 235 "strict_var_name", ["Variable name may not be eval or argumen
ts in strict mode"], |
236 strict_function_name: ["Function name may not be eval or arguments
in strict mode"], | 236 "strict_function_name", ["Function name may not be eval or argumen
ts in strict mode"], |
237 strict_octal_literal: ["Octal literals are not allowed in strict m
ode."], | 237 "strict_octal_literal", ["Octal literals are not allowed in strict
mode."], |
238 strict_duplicate_property: ["Duplicate data property in object literal
not allowed in strict mode"], | 238 "strict_duplicate_property", ["Duplicate data property in object litera
l not allowed in strict mode"], |
239 accessor_data_property: ["Object literal may not have data and acces
sor property with the same name"], | 239 "accessor_data_property", ["Object literal may not have data and acc
essor property with the same name"], |
240 accessor_get_set: ["Object literal may not have multiple get/s
et accessors with the same name"], | 240 "accessor_get_set", ["Object literal may not have multiple get
/set accessors with the same name"], |
241 strict_lhs_assignment: ["Assignment to eval or arguments is not all
owed in strict mode"], | 241 "strict_lhs_assignment", ["Assignment to eval or arguments is not a
llowed in strict mode"], |
242 strict_lhs_postfix: ["Postfix increment/decrement may not have e
val or arguments operand in strict mode"], | 242 "strict_lhs_postfix", ["Postfix increment/decrement may not have
eval or arguments operand in strict mode"], |
243 strict_lhs_prefix: ["Prefix increment/decrement may not have ev
al or arguments operand in strict mode"], | 243 "strict_lhs_prefix", ["Prefix increment/decrement may not have
eval or arguments operand in strict mode"], |
244 strict_reserved_word: ["Use of future reserved word in strict mode
"], | 244 "strict_reserved_word", ["Use of future reserved word in strict mo
de"], |
245 strict_delete: ["Delete of an unqualified identifier in str
ict mode."], | 245 "strict_delete", ["Delete of an unqualified identifier in s
trict mode."], |
246 strict_delete_property: ["Cannot delete property '", "%0", "' of ",
"%1"], | 246 "strict_delete_property", ["Cannot delete property '", "%0", "' of "
, "%1"], |
247 strict_const: ["Use of const in strict mode."], | 247 "strict_const", ["Use of const in strict mode."], |
248 strict_function: ["In strict mode code, functions can only be
declared at top level or immediately within another function." ], | 248 "strict_function", ["In strict mode code, functions can only
be declared at top level or immediately within another function." ], |
249 strict_read_only_property: ["Cannot assign to read only property '", "%
0", "' of ", "%1"], | 249 "strict_read_only_property", ["Cannot assign to read only property '",
"%0", "' of ", "%1"], |
250 strict_cannot_assign: ["Cannot assign to read only '", "%0", "' in
strict mode"], | 250 "strict_cannot_assign", ["Cannot assign to read only '", "%0", "'
in strict mode"], |
251 strict_poison_pill: ["'caller', 'callee', and 'arguments' proper
ties may not be accessed on strict mode functions or the arguments objects for c
alls to them"], | 251 "strict_poison_pill", ["'caller', 'callee', and 'arguments' prop
erties may not be accessed on strict mode functions or the arguments objects for
calls to them"], |
252 strict_caller: ["Illegal access to a strict mode caller fun
ction."], | 252 "strict_caller", ["Illegal access to a strict mode caller f
unction."], |
253 unprotected_let: ["Illegal let declaration in unprotected sta
tement context."], | 253 "unprotected_let", ["Illegal let declaration in unprotected s
tatement context."], |
254 }; | 254 ]; |
| 255 var messages = { __proto__ : null }; |
| 256 var desc = new PropertyDescriptor(); |
| 257 desc.setConfigurable(false); |
| 258 desc.setEnumerable(false); |
| 259 desc.setWritable(false); |
| 260 for (var i = 0; i < messagesDictionary.length; i += 2) { |
| 261 var key = messagesDictionary[i]; |
| 262 var format = messagesDictionary[i + 1]; |
| 263 ObjectFreeze(format); |
| 264 desc.setValue(format); |
| 265 DefineOwnProperty(messages, key, desc); |
| 266 } |
| 267 %PreventExtensions(messages); |
| 268 %IgnoreAttributesAndSetProperty(builtins, "kMessages", |
| 269 messages, |
| 270 DONT_DELETE | DONT_ENUM | READ_ONLY); |
255 } | 271 } |
256 var message_type = %MessageGetType(message); | 272 var message_type = %MessageGetType(message); |
257 var format = kMessages[message_type]; | 273 var format = kMessages[message_type]; |
258 if (!format) return "<unknown message " + message_type + ">"; | 274 if (!format) return "<unknown message " + message_type + ">"; |
259 return FormatString(format, message); | 275 return FormatString(format, message); |
260 } | 276 } |
261 | 277 |
262 | 278 |
263 function GetLineNumber(message) { | 279 function GetLineNumber(message) { |
264 var start_position = %MessageGetStartPosition(message); | 280 var start_position = %MessageGetStartPosition(message); |
(...skipping 726 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
991 var pos = %FunctionGetPositionForOffset(code, pc); | 1007 var pos = %FunctionGetPositionForOffset(code, pc); |
992 frames.push(new CallSite(recv, fun, pos)); | 1008 frames.push(new CallSite(recv, fun, pos)); |
993 } | 1009 } |
994 if (IS_FUNCTION($Error.prepareStackTrace)) { | 1010 if (IS_FUNCTION($Error.prepareStackTrace)) { |
995 return $Error.prepareStackTrace(error, frames); | 1011 return $Error.prepareStackTrace(error, frames); |
996 } else { | 1012 } else { |
997 return FormatStackTrace(error, frames); | 1013 return FormatStackTrace(error, frames); |
998 } | 1014 } |
999 } | 1015 } |
1000 | 1016 |
1001 function DefineError(f) { | |
1002 // Store the error function in both the global object | |
1003 // and the runtime object. The function is fetched | |
1004 // from the runtime object when throwing errors from | |
1005 // within the runtime system to avoid strange side | |
1006 // effects when overwriting the error functions from | |
1007 // user code. | |
1008 var name = f.name; | |
1009 %SetProperty(global, name, f, DONT_ENUM); | |
1010 this['$' + name] = f; | |
1011 // Configure the error function. | |
1012 if (name == 'Error') { | |
1013 // The prototype of the Error object must itself be an error. | |
1014 // However, it can't be an instance of the Error object because | |
1015 // it hasn't been properly configured yet. Instead we create a | |
1016 // special not-a-true-error-but-close-enough object. | |
1017 function ErrorPrototype() {} | |
1018 %FunctionSetPrototype(ErrorPrototype, $Object.prototype); | |
1019 %FunctionSetInstanceClassName(ErrorPrototype, 'Error'); | |
1020 %FunctionSetPrototype(f, new ErrorPrototype()); | |
1021 } else { | |
1022 %FunctionSetPrototype(f, new $Error()); | |
1023 } | |
1024 %FunctionSetInstanceClassName(f, 'Error'); | |
1025 %SetProperty(f.prototype, 'constructor', f, DONT_ENUM); | |
1026 // The name property on the prototype of error objects is not | |
1027 // specified as being read-one and dont-delete. However, allowing | |
1028 // overwriting allows leaks of error objects between script blocks | |
1029 // in the same context in a browser setting. Therefore we fix the | |
1030 // name. | |
1031 %SetProperty(f.prototype, "name", name, DONT_ENUM | DONT_DELETE | READ_ONLY); | |
1032 %SetCode(f, function(m) { | |
1033 if (%_IsConstructCall()) { | |
1034 // Define all the expected properties directly on the error | |
1035 // object. This avoids going through getters and setters defined | |
1036 // on prototype objects. | |
1037 %IgnoreAttributesAndSetProperty(this, 'stack', void 0, DONT_ENUM); | |
1038 %IgnoreAttributesAndSetProperty(this, 'arguments', void 0, DONT_ENUM); | |
1039 %IgnoreAttributesAndSetProperty(this, 'type', void 0, DONT_ENUM); | |
1040 if (m === kAddMessageAccessorsMarker) { | |
1041 // DefineOneShotAccessor always inserts a message property and | |
1042 // ignores setters. | |
1043 DefineOneShotAccessor(this, 'message', function (obj) { | |
1044 return FormatMessage(%NewMessageObject(obj.type, obj.arguments)); | |
1045 }); | |
1046 } else if (!IS_UNDEFINED(m)) { | |
1047 %IgnoreAttributesAndSetProperty(this, | |
1048 'message', | |
1049 ToString(m), | |
1050 DONT_ENUM); | |
1051 } | |
1052 captureStackTrace(this, f); | |
1053 } else { | |
1054 return new f(m); | |
1055 } | |
1056 }); | |
1057 } | |
1058 | |
1059 function captureStackTrace(obj, cons_opt) { | 1017 function captureStackTrace(obj, cons_opt) { |
1060 var stackTraceLimit = $Error.stackTraceLimit; | 1018 var stackTraceLimit = $Error.stackTraceLimit; |
1061 if (!stackTraceLimit || !IS_NUMBER(stackTraceLimit)) return; | 1019 if (!stackTraceLimit || !IS_NUMBER(stackTraceLimit)) return; |
1062 if (stackTraceLimit < 0 || stackTraceLimit > 10000) { | 1020 if (stackTraceLimit < 0 || stackTraceLimit > 10000) { |
1063 stackTraceLimit = 10000; | 1021 stackTraceLimit = 10000; |
1064 } | 1022 } |
1065 var raw_stack = %CollectStackTrace(cons_opt | 1023 var raw_stack = %CollectStackTrace(cons_opt |
1066 ? cons_opt | 1024 ? cons_opt |
1067 : captureStackTrace, stackTraceLimit); | 1025 : captureStackTrace, stackTraceLimit); |
1068 DefineOneShotAccessor(obj, 'stack', function (obj) { | 1026 DefineOneShotAccessor(obj, 'stack', function (obj) { |
1069 return FormatRawStackTrace(obj, raw_stack); | 1027 return FormatRawStackTrace(obj, raw_stack); |
1070 }); | 1028 }); |
1071 }; | 1029 }; |
1072 | 1030 |
1073 $Math.__proto__ = global.Object.prototype; | |
1074 | 1031 |
1075 // DefineError is a native function. Use explicit receiver. Otherwise | 1032 (function () { |
1076 // the receiver will be 'undefined'. | 1033 // Define special error type constructors. |
1077 this.DefineError(function Error() { }); | 1034 |
1078 this.DefineError(function TypeError() { }); | 1035 function DefineError(f) { |
1079 this.DefineError(function RangeError() { }); | 1036 // Store the error function in both the global object |
1080 this.DefineError(function SyntaxError() { }); | 1037 // and the runtime object. The function is fetched |
1081 this.DefineError(function ReferenceError() { }); | 1038 // from the runtime object when throwing errors from |
1082 this.DefineError(function EvalError() { }); | 1039 // within the runtime system to avoid strange side |
1083 this.DefineError(function URIError() { }); | 1040 // effects when overwriting the error functions from |
| 1041 // user code. |
| 1042 var name = f.name; |
| 1043 %SetProperty(global, name, f, DONT_ENUM); |
| 1044 builtins['$' + name] = f; |
| 1045 // Configure the error function. |
| 1046 if (name == 'Error') { |
| 1047 // The prototype of the Error object must itself be an error. |
| 1048 // However, it can't be an instance of the Error object because |
| 1049 // it hasn't been properly configured yet. Instead we create a |
| 1050 // special not-a-true-error-but-close-enough object. |
| 1051 function ErrorPrototype() {} |
| 1052 %FunctionSetPrototype(ErrorPrototype, $Object.prototype); |
| 1053 %FunctionSetInstanceClassName(ErrorPrototype, 'Error'); |
| 1054 %FunctionSetPrototype(f, new ErrorPrototype()); |
| 1055 } else { |
| 1056 %FunctionSetPrototype(f, new $Error()); |
| 1057 } |
| 1058 %FunctionSetInstanceClassName(f, 'Error'); |
| 1059 %SetProperty(f.prototype, 'constructor', f, DONT_ENUM); |
| 1060 // The name property on the prototype of error objects is not |
| 1061 // specified as being read-one and dont-delete. However, allowing |
| 1062 // overwriting allows leaks of error objects between script blocks |
| 1063 // in the same context in a browser setting. Therefore we fix the |
| 1064 // name. |
| 1065 %SetProperty(f.prototype, "name", name, |
| 1066 DONT_ENUM | DONT_DELETE | READ_ONLY) ; |
| 1067 %SetCode(f, function(m) { |
| 1068 if (%_IsConstructCall()) { |
| 1069 // Define all the expected properties directly on the error |
| 1070 // object. This avoids going through getters and setters defined |
| 1071 // on prototype objects. |
| 1072 %IgnoreAttributesAndSetProperty(this, 'stack', void 0, DONT_ENUM); |
| 1073 %IgnoreAttributesAndSetProperty(this, 'arguments', void 0, DONT_ENUM); |
| 1074 %IgnoreAttributesAndSetProperty(this, 'type', void 0, DONT_ENUM); |
| 1075 if (m === kAddMessageAccessorsMarker) { |
| 1076 // DefineOneShotAccessor always inserts a message property and |
| 1077 // ignores setters. |
| 1078 DefineOneShotAccessor(this, 'message', function (obj) { |
| 1079 return FormatMessage(%NewMessageObject(obj.type, obj.arguments)); |
| 1080 }); |
| 1081 } else if (!IS_UNDEFINED(m)) { |
| 1082 %IgnoreAttributesAndSetProperty(this, |
| 1083 'message', |
| 1084 ToString(m), |
| 1085 DONT_ENUM); |
| 1086 } |
| 1087 captureStackTrace(this, f); |
| 1088 } else { |
| 1089 return new f(m); |
| 1090 } |
| 1091 }); |
| 1092 } |
| 1093 |
| 1094 DefineError(function Error() { }); |
| 1095 DefineError(function TypeError() { }); |
| 1096 DefineError(function RangeError() { }); |
| 1097 DefineError(function SyntaxError() { }); |
| 1098 DefineError(function ReferenceError() { }); |
| 1099 DefineError(function EvalError() { }); |
| 1100 DefineError(function URIError() { }); |
| 1101 })(); |
1084 | 1102 |
1085 $Error.captureStackTrace = captureStackTrace; | 1103 $Error.captureStackTrace = captureStackTrace; |
1086 | 1104 |
1087 // Setup extra properties of the Error.prototype object. | 1105 %SetProperty($Error.prototype, 'message', '', DONT_ENUM); |
1088 function setErrorMessage() { | |
1089 var desc = {value: '', | |
1090 enumerable: false, | |
1091 configurable: true, | |
1092 writable: true }; | |
1093 DefineOwnProperty($Error.prototype, | |
1094 'message', | |
1095 ToPropertyDescriptor(desc), | |
1096 true); | |
1097 | |
1098 } | |
1099 | |
1100 setErrorMessage(); | |
1101 | 1106 |
1102 // Global list of error objects visited during errorToString. This is | 1107 // Global list of error objects visited during errorToString. This is |
1103 // used to detect cycles in error toString formatting. | 1108 // used to detect cycles in error toString formatting. |
1104 var visited_errors = new $Array(); | 1109 const visited_errors = new InternalArray(); |
1105 var cyclic_error_marker = new $Object(); | 1110 const cyclic_error_marker = new $Object(); |
1106 | 1111 |
1107 function errorToStringDetectCycle() { | 1112 function errorToStringDetectCycle(error) { |
1108 if (!%PushIfAbsent(visited_errors, this)) throw cyclic_error_marker; | 1113 if (!%PushIfAbsent(visited_errors, error)) throw cyclic_error_marker; |
1109 try { | 1114 try { |
1110 var type = this.type; | 1115 var type = error.type; |
1111 if (type && !%_CallFunction(this, "message", ObjectHasOwnProperty)) { | 1116 var hasMessage = %_CallFunction(error, "message", ObjectHasOwnProperty); |
1112 var formatted = FormatMessage(%NewMessageObject(type, this.arguments)); | 1117 if (type && !hasMessage) { |
1113 return this.name + ": " + formatted; | 1118 var formatted = FormatMessage(%NewMessageObject(type, error.arguments)); |
| 1119 return error.name + ": " + formatted; |
1114 } | 1120 } |
1115 var message = %_CallFunction(this, "message", ObjectHasOwnProperty) | 1121 var message = hasMessage ? (": " + error.message) : ""; |
1116 ? (": " + this.message) | 1122 return error.name + message; |
1117 : ""; | |
1118 return this.name + message; | |
1119 } finally { | 1123 } finally { |
1120 visited_errors.length = visited_errors.length - 1; | 1124 visited_errors.length = visited_errors.length - 1; |
1121 } | 1125 } |
1122 } | 1126 } |
1123 | 1127 |
1124 function errorToString() { | 1128 function errorToString() { |
1125 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | 1129 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
1126 throw MakeTypeError("called_on_null_or_undefined", | 1130 throw MakeTypeError("called_on_null_or_undefined", |
1127 ["Error.prototype.toString"]); | 1131 ["Error.prototype.toString"]); |
1128 } | 1132 } |
1129 // This helper function is needed because access to properties on | 1133 // This helper function is needed because access to properties on |
1130 // the builtins object do not work inside of a catch clause. | 1134 // the builtins object do not work inside of a catch clause. |
1131 function isCyclicErrorMarker(o) { return o === cyclic_error_marker; } | 1135 function isCyclicErrorMarker(o) { return o === cyclic_error_marker; } |
1132 | 1136 |
1133 try { | 1137 try { |
1134 return %_CallFunction(this, errorToStringDetectCycle); | 1138 return errorToStringDetectCycle(this); |
1135 } catch(e) { | 1139 } catch(e) { |
1136 // If this error message was encountered already return the empty | 1140 // If this error message was encountered already return the empty |
1137 // string for it instead of recursively formatting it. | 1141 // string for it instead of recursively formatting it. |
1138 if (isCyclicErrorMarker(e)) { | 1142 if (isCyclicErrorMarker(e)) { |
1139 return ''; | 1143 return ''; |
1140 } | 1144 } |
1141 throw e; | 1145 throw e; |
1142 } | 1146 } |
1143 } | 1147 } |
1144 | 1148 |
1145 | 1149 |
1146 InstallFunctions($Error.prototype, DONT_ENUM, ['toString', errorToString]); | 1150 InstallFunctions($Error.prototype, DONT_ENUM, ['toString', errorToString]); |
1147 | 1151 |
1148 // Boilerplate for exceptions for stack overflows. Used from | 1152 // Boilerplate for exceptions for stack overflows. Used from |
1149 // Isolate::StackOverflow(). | 1153 // Isolate::StackOverflow(). |
1150 const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []); | 1154 const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []); |
OLD | NEW |