blob: 7f9c0f8d363459b564b90a93d849cfcf8caa186c [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
29// -------------------------------------------------------------------
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000030//
31// Matches Script::Type from objects.h
32var TYPE_NATIVE = 0;
33var TYPE_EXTENSION = 1;
34var TYPE_NORMAL = 2;
35
36// Matches Script::CompilationType from objects.h
37var COMPILATION_TYPE_HOST = 0;
38var COMPILATION_TYPE_EVAL = 1;
39var COMPILATION_TYPE_JSON = 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
ager@chromium.orgadd848f2009-08-13 12:44:13 +000041// Lazily initialized.
42var kVowelSounds = 0;
43var kCapitalVowelSounds = 0;
44
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000045// Matches Messages::kNoLineNumberInfo from v8.h
46var kNoLineNumberInfo = 0;
47
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000048// If this object gets passed to an error constructor the error will
49// get an accessor for .message that constructs a descriptive error
50// message on access.
51var kAddMessageAccessorsMarker = { };
52
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053
54function GetInstanceName(cons) {
55 if (cons.length == 0) {
56 return "";
57 }
ager@chromium.org3e875802009-06-29 08:26:34 +000058 var first = %StringToLowerCase(StringCharAt.call(cons, 0));
ager@chromium.orgadd848f2009-08-13 12:44:13 +000059 if (kVowelSounds === 0) {
60 kVowelSounds = {a: true, e: true, i: true, o: true, u: true, y: true};
61 kCapitalVowelSounds = {a: true, e: true, i: true, o: true, u: true, h: true,
62 f: true, l: true, m: true, n: true, r: true, s: true, x: true, y: true};
63 }
64 var vowel_mapping = kVowelSounds;
ager@chromium.org3e875802009-06-29 08:26:34 +000065 if (cons.length > 1 && (StringCharAt.call(cons, 0) != first)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000066 // First char is upper case
ager@chromium.org3e875802009-06-29 08:26:34 +000067 var second = %StringToLowerCase(StringCharAt.call(cons, 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000068 // Second char is upper case
ager@chromium.orgadd848f2009-08-13 12:44:13 +000069 if (StringCharAt.call(cons, 1) != second) {
70 vowel_mapping = kCapitalVowelSounds;
71 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000072 }
ager@chromium.orgadd848f2009-08-13 12:44:13 +000073 var s = vowel_mapping[first] ? "an " : "a ";
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000074 return s + cons;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000075}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000076
77
ager@chromium.orgadd848f2009-08-13 12:44:13 +000078var kMessages = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000079
80
81function FormatString(format, args) {
82 var result = format;
83 for (var i = 0; i < args.length; i++) {
84 var str;
85 try { str = ToDetailString(args[i]); }
86 catch (e) { str = "#<error>"; }
ager@chromium.org3e875802009-06-29 08:26:34 +000087 result = ArrayJoin.call(StringSplit.call(result, "%" + i), str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000088 }
89 return result;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000090}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091
92
93function ToDetailString(obj) {
94 if (obj != null && IS_OBJECT(obj) && obj.toString === $Object.prototype.toString) {
95 var constructor = obj.constructor;
96 if (!constructor) return ToString(obj);
97 var constructorName = constructor.name;
98 if (!constructorName) return ToString(obj);
99 return "#<" + GetInstanceName(constructorName) + ">";
100 } else {
101 return ToString(obj);
102 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000103}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104
105
106function MakeGenericError(constructor, type, args) {
ager@chromium.org3e875802009-06-29 08:26:34 +0000107 if (IS_UNDEFINED(args)) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000108 args = [];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000109 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000110 var e = new constructor(kAddMessageAccessorsMarker);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000111 e.type = type;
112 e.arguments = args;
113 return e;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000114}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000115
116
117/**
118 * Setup the Script function and constructor.
119 */
120%FunctionSetInstanceClassName(Script, 'Script');
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000121%SetProperty(Script.prototype, 'constructor', Script, DONT_ENUM);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000122%SetCode(Script, function(x) {
123 // Script objects can only be created by the VM.
124 throw new $Error("Not supported");
125});
126
127
128// Helper functions; called from the runtime system.
129function FormatMessage(message) {
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000130 if (kMessages === 0) {
131 kMessages = {
132 // Error
133 cyclic_proto: "Cyclic __proto__ value",
134 // TypeError
135 unexpected_token: "Unexpected token %0",
136 unexpected_token_number: "Unexpected number",
137 unexpected_token_string: "Unexpected string",
138 unexpected_token_identifier: "Unexpected identifier",
139 unexpected_eos: "Unexpected end of input",
140 malformed_regexp: "Invalid regular expression: /%0/: %1",
141 unterminated_regexp: "Invalid regular expression: missing /",
142 regexp_flags: "Cannot supply flags when constructing one RegExp from another",
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000143 incompatible_method_receiver: "Method %0 called on incompatible receiver %1",
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000144 invalid_lhs_in_assignment: "Invalid left-hand side in assignment",
145 invalid_lhs_in_for_in: "Invalid left-hand side in for-in",
146 invalid_lhs_in_postfix_op: "Invalid left-hand side expression in postfix operation",
147 invalid_lhs_in_prefix_op: "Invalid left-hand side expression in prefix operation",
148 multiple_defaults_in_switch: "More than one default clause in switch statement",
149 newline_after_throw: "Illegal newline after throw",
150 redeclaration: "%0 '%1' has already been declared",
151 no_catch_or_finally: "Missing catch or finally after try",
152 unknown_label: "Undefined label '%0'",
153 uncaught_exception: "Uncaught %0",
154 stack_trace: "Stack Trace:\n%0",
155 called_non_callable: "%0 is not a function",
156 undefined_method: "Object %1 has no method '%0'",
157 property_not_function: "Property '%0' of object %1 is not a function",
158 cannot_convert_to_primitive: "Cannot convert object to primitive value",
159 not_constructor: "%0 is not a constructor",
160 not_defined: "%0 is not defined",
161 non_object_property_load: "Cannot read property '%0' of %1",
162 non_object_property_store: "Cannot set property '%0' of %1",
163 non_object_property_call: "Cannot call method '%0' of %1",
164 with_expression: "%0 has no properties",
165 illegal_invocation: "Illegal invocation",
166 no_setter_in_callback: "Cannot set property %0 of %1 which has only a getter",
167 apply_non_function: "Function.prototype.apply was called on %0, which is a %1 and not a function",
168 apply_wrong_args: "Function.prototype.apply: Arguments list has wrong type",
169 invalid_in_operator_use: "Cannot use 'in' operator to search for '%0' in %1",
170 instanceof_function_expected: "Expecting a function in instanceof check, but got %0",
171 instanceof_nonobject_proto: "Function has non-object prototype '%0' in instanceof check",
172 null_to_object: "Cannot convert null to object",
173 reduce_no_initial: "Reduce of empty array with no initial value",
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000174 getter_must_be_callable: "Getter must be a function: %0",
175 setter_must_be_callable: "Setter must be a function: %0",
176 value_and_accessor: "Invalid property. A property cannot both have accessors and be writable or have a value: %0",
177 proto_object_or_null: "Object prototype may only be an Object or null",
178 property_desc_object: "Property description must be an object: %0",
ager@chromium.org5c838252010-02-19 08:53:10 +0000179 redefine_disallowed: "Cannot redefine property: %0",
180 define_disallowed: "Cannot define property, object is not extensible: %0",
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000181 // RangeError
182 invalid_array_length: "Invalid array length",
183 stack_overflow: "Maximum call stack size exceeded",
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000184 // SyntaxError
185 unable_to_parse: "Parse error",
186 duplicate_regexp_flag: "Duplicate RegExp flag %0",
187 invalid_regexp: "Invalid RegExp pattern /%0/",
188 illegal_break: "Illegal break statement",
189 illegal_continue: "Illegal continue statement",
190 illegal_return: "Illegal return statement",
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000191 error_loading_debugger: "Error loading debugger",
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000192 no_input_to_regexp: "No input to %0",
193 result_not_primitive: "Result of %0 must be a primitive, was %1",
194 invalid_json: "String '%0' is not valid JSON",
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000195 circular_structure: "Converting circular structure to JSON",
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000196 obj_ctor_property_non_object: "Object.%0 called on non-object",
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000197 array_indexof_not_defined: "Array.getIndexOf: Argument undefined",
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000198 object_not_extensible: "Can't add property %0, object is not extensible",
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +0000199 illegal_access: "Illegal access",
200 invalid_preparser_data: "Invalid preparser data for function %0"
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000201 };
202 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203 var format = kMessages[message.type];
204 if (!format) return "<unknown message " + message.type + ">";
205 return FormatString(format, message.args);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000206}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207
208
209function GetLineNumber(message) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000210 if (message.startPos == -1) return kNoLineNumberInfo;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000211 var location = message.script.locationFromPosition(message.startPos, true);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000212 if (location == null) return kNoLineNumberInfo;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213 return location.line + 1;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000214}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215
216
217// Returns the source code line containing the given source
218// position, or the empty string if the position is invalid.
219function GetSourceLine(message) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000220 var location = message.script.locationFromPosition(message.startPos, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221 if (location == null) return "";
222 location.restrict();
223 return location.sourceText();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000224}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225
226
227function MakeTypeError(type, args) {
228 return MakeGenericError($TypeError, type, args);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000229}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000230
231
232function MakeRangeError(type, args) {
233 return MakeGenericError($RangeError, type, args);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000234}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235
236
237function MakeSyntaxError(type, args) {
238 return MakeGenericError($SyntaxError, type, args);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000239}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240
241
242function MakeReferenceError(type, args) {
243 return MakeGenericError($ReferenceError, type, args);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000244}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000245
246
247function MakeEvalError(type, args) {
248 return MakeGenericError($EvalError, type, args);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000249}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250
251
252function MakeError(type, args) {
253 return MakeGenericError($Error, type, args);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000254}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000255
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000256/**
257 * Find a line number given a specific source position.
258 * @param {number} position The source position.
259 * @return {number} 0 if input too small, -1 if input too large,
260 else the line number.
261 */
262Script.prototype.lineFromPosition = function(position) {
263 var lower = 0;
264 var upper = this.lineCount() - 1;
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000265 var line_ends = this.line_ends;
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000266
267 // We'll never find invalid positions so bail right away.
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000268 if (position > line_ends[upper]) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000269 return -1;
270 }
271
272 // This means we don't have to safe-guard indexing line_ends[i - 1].
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000273 if (position <= line_ends[0]) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000274 return 0;
275 }
276
277 // Binary search to find line # from position range.
278 while (upper >= 1) {
279 var i = (lower + upper) >> 1;
280
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000281 if (position > line_ends[i]) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000282 lower = i + 1;
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000283 } else if (position <= line_ends[i - 1]) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000284 upper = i - 1;
285 } else {
286 return i;
287 }
288 }
289 return -1;
290}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291
292/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293 * Get information on a specific source position.
294 * @param {number} position The source position
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000295 * @param {boolean} include_resource_offset Set to true to have the resource
296 * offset added to the location
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000297 * @return {SourceLocation}
298 * If line is negative or not in the source null is returned.
299 */
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000300Script.prototype.locationFromPosition = function (position,
301 include_resource_offset) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000302 var line = this.lineFromPosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303 if (line == -1) return null;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000304
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305 // Determine start, end and column.
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000306 var line_ends = this.line_ends;
307 var start = line == 0 ? 0 : line_ends[line - 1] + 1;
308 var end = line_ends[line];
ager@chromium.org3e875802009-06-29 08:26:34 +0000309 if (end > 0 && StringCharAt.call(this.source, end - 1) == '\r') end--;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000310 var column = position - start;
311
312 // Adjust according to the offset within the resource.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000313 if (include_resource_offset) {
314 line += this.line_offset;
315 if (line == this.line_offset) {
316 column += this.column_offset;
317 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000318 }
319
320 return new SourceLocation(this, position, line, column, start, end);
321};
322
323
324/**
325 * Get information on a specific source line and column possibly offset by a
326 * fixed source position. This function is used to find a source position from
327 * a line and column position. The fixed source position offset is typically
328 * used to find a source position in a function based on a line and column in
329 * the source for the function alone. The offset passed will then be the
330 * start position of the source for the function within the full script source.
331 * @param {number} opt_line The line within the source. Default value is 0
332 * @param {number} opt_column The column in within the line. Default value is 0
333 * @param {number} opt_offset_position The offset from the begining of the
334 * source from where the line and column calculation starts. Default value is 0
335 * @return {SourceLocation}
336 * If line is negative or not in the source null is returned.
337 */
338Script.prototype.locationFromLine = function (opt_line, opt_column, opt_offset_position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000339 // Default is the first line in the script. Lines in the script is relative
340 // to the offset within the resource.
341 var line = 0;
342 if (!IS_UNDEFINED(opt_line)) {
343 line = opt_line - this.line_offset;
344 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000345
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346 // Default is first column. If on the first line add the offset within the
347 // resource.
348 var column = opt_column || 0;
349 if (line == 0) {
350 column -= this.column_offset
351 }
352
353 var offset_position = opt_offset_position || 0;
354 if (line < 0 || column < 0 || offset_position < 0) return null;
355 if (line == 0) {
ager@chromium.org3a6061e2009-03-12 14:24:36 +0000356 return this.locationFromPosition(offset_position + column, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357 } else {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000358 // Find the line where the offset position is located.
359 var offset_line = this.lineFromPosition(offset_position);
360
361 if (offset_line == -1 || offset_line + line >= this.lineCount()) {
362 return null;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000363 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000364
iposva@chromium.org245aa852009-02-10 00:49:54 +0000365 return this.locationFromPosition(this.line_ends[offset_line + line - 1] + 1 + column); // line > 0 here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000366 }
367}
368
369
370/**
371 * Get a slice of source code from the script. The boundaries for the slice is
372 * specified in lines.
373 * @param {number} opt_from_line The first line (zero bound) in the slice.
374 * Default is 0
375 * @param {number} opt_to_column The last line (zero bound) in the slice (non
376 * inclusive). Default is the number of lines in the script
377 * @return {SourceSlice} The source slice or null of the parameters where
378 * invalid
379 */
380Script.prototype.sourceSlice = function (opt_from_line, opt_to_line) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381 var from_line = IS_UNDEFINED(opt_from_line) ? this.line_offset : opt_from_line;
382 var to_line = IS_UNDEFINED(opt_to_line) ? this.line_offset + this.lineCount() : opt_to_line
383
384 // Adjust according to the offset within the resource.
385 from_line -= this.line_offset;
386 to_line -= this.line_offset;
387 if (from_line < 0) from_line = 0;
388 if (to_line > this.lineCount()) to_line = this.lineCount();
389
iposva@chromium.org245aa852009-02-10 00:49:54 +0000390 // Check parameters.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000391 if (from_line >= this.lineCount() ||
392 to_line < 0 ||
393 from_line > to_line) {
394 return null;
395 }
396
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000397 var line_ends = this.line_ends;
398 var from_position = from_line == 0 ? 0 : line_ends[from_line - 1] + 1;
399 var to_position = to_line == 0 ? 0 : line_ends[to_line - 1] + 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400
401 // Return a source slice with line numbers re-adjusted to the resource.
402 return new SourceSlice(this, from_line + this.line_offset, to_line + this.line_offset,
403 from_position, to_position);
404}
405
406
407Script.prototype.sourceLine = function (opt_line) {
408 // Default is the first line in the script. Lines in the script are relative
409 // to the offset within the resource.
410 var line = 0;
411 if (!IS_UNDEFINED(opt_line)) {
412 line = opt_line - this.line_offset;
413 }
iposva@chromium.org245aa852009-02-10 00:49:54 +0000414
415 // Check parameter.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000416 if (line < 0 || this.lineCount() <= line) {
417 return null;
418 }
419
iposva@chromium.org245aa852009-02-10 00:49:54 +0000420 // Return the source line.
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000421 var line_ends = this.line_ends;
422 var start = line == 0 ? 0 : line_ends[line - 1] + 1;
423 var end = line_ends[line];
ager@chromium.org3e875802009-06-29 08:26:34 +0000424 return StringSubstring.call(this.source, start, end);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000425}
426
427
428/**
429 * Returns the number of source lines.
430 * @return {number}
431 * Number of source lines.
432 */
433Script.prototype.lineCount = function() {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000434 // Return number of source lines.
435 return this.line_ends.length;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000436};
437
438
439/**
lrn@chromium.org25156de2010-04-06 13:10:27 +0000440 * Returns the name of script if available, contents of sourceURL comment
vegorov@chromium.org42841962010-10-18 11:18:59 +0000441 * otherwise. See
lrn@chromium.org25156de2010-04-06 13:10:27 +0000442 * http://fbug.googlecode.com/svn/branches/firebug1.1/docs/ReleaseNotes_1.1.txt
443 * for details on using //@ sourceURL comment to identify scritps that don't
444 * have name.
vegorov@chromium.org42841962010-10-18 11:18:59 +0000445 *
lrn@chromium.org25156de2010-04-06 13:10:27 +0000446 * @return {?string} script name if present, value for //@ sourceURL comment
447 * otherwise.
448 */
449Script.prototype.nameOrSourceURL = function() {
450 if (this.name)
451 return this.name;
vegorov@chromium.org42841962010-10-18 11:18:59 +0000452 // TODO(608): the spaces in a regexp below had to be escaped as \040
lrn@chromium.org25156de2010-04-06 13:10:27 +0000453 // because this file is being processed by js2c whose handling of spaces
454 // in regexps is broken. Also, ['"] are excluded from allowed URLs to
455 // avoid matches against sources that invoke evals with sourceURL.
456 var sourceUrlPattern =
457 /\/\/@[\040\t]sourceURL=[\040\t]*([^\s'"]*)[\040\t]*$/m;
458 var match = sourceUrlPattern.exec(this.source);
459 return match ? match[1] : this.name;
460}
461
462
463/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000464 * Class for source location. A source location is a position within some
465 * source with the following properties:
466 * script : script object for the source
467 * line : source line number
468 * column : source column within the line
469 * position : position within the source
470 * start : position of start of source context (inclusive)
471 * end : position of end of source context (not inclusive)
472 * Source text for the source context is the character interval [start, end[. In
473 * most cases end will point to a newline character. It might point just past
474 * the final position of the source if the last source line does not end with a
iposva@chromium.org245aa852009-02-10 00:49:54 +0000475 * newline character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476 * @param {Script} script The Script object for which this is a location
477 * @param {number} position Source position for the location
478 * @param {number} line The line number for the location
479 * @param {number} column The column within the line for the location
480 * @param {number} start Source position for start of source context
481 * @param {number} end Source position for end of source context
482 * @constructor
483 */
484function SourceLocation(script, position, line, column, start, end) {
485 this.script = script;
486 this.position = position;
487 this.line = line;
488 this.column = column;
489 this.start = start;
490 this.end = end;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000491}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492
493
494const kLineLengthLimit = 78;
495
496/**
497 * Restrict source location start and end positions to make the source slice
498 * no more that a certain number of characters wide.
499 * @param {number} opt_limit The with limit of the source text with a default
500 * of 78
501 * @param {number} opt_before The number of characters to prefer before the
502 * position with a default value of 10 less that the limit
503 */
504SourceLocation.prototype.restrict = function (opt_limit, opt_before) {
505 // Find the actual limit to use.
506 var limit;
507 var before;
508 if (!IS_UNDEFINED(opt_limit)) {
509 limit = opt_limit;
510 } else {
511 limit = kLineLengthLimit;
512 }
513 if (!IS_UNDEFINED(opt_before)) {
514 before = opt_before;
515 } else {
516 // If no before is specified center for small limits and perfer more source
517 // before the the position that after for longer limits.
518 if (limit <= 20) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000519 before = $floor(limit / 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520 } else {
521 before = limit - 10;
522 }
523 }
524 if (before >= limit) {
525 before = limit - 1;
526 }
527
528 // If the [start, end[ interval is too big we restrict
529 // it in one or both ends. We make sure to always produce
530 // restricted intervals of maximum allowed size.
531 if (this.end - this.start > limit) {
532 var start_limit = this.position - before;
533 var end_limit = this.position + limit - before;
534 if (this.start < start_limit && end_limit < this.end) {
535 this.start = start_limit;
536 this.end = end_limit;
537 } else if (this.start < start_limit) {
538 this.start = this.end - limit;
539 } else {
540 this.end = this.start + limit;
541 }
542 }
543};
544
545
546/**
547 * Get the source text for a SourceLocation
548 * @return {String}
549 * Source text for this location.
550 */
551SourceLocation.prototype.sourceText = function () {
ager@chromium.org3e875802009-06-29 08:26:34 +0000552 return StringSubstring.call(this.script.source, this.start, this.end);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553};
554
555
556/**
557 * Class for a source slice. A source slice is a part of a script source with
558 * the following properties:
559 * script : script object for the source
560 * from_line : line number for the first line in the slice
561 * to_line : source line number for the last line in the slice
562 * from_position : position of the first character in the slice
563 * to_position : position of the last character in the slice
564 * The to_line and to_position are not included in the slice, that is the lines
565 * in the slice are [from_line, to_line[. Likewise the characters in the slice
566 * are [from_position, to_position[.
567 * @param {Script} script The Script object for the source slice
568 * @param {number} from_line
569 * @param {number} to_line
570 * @param {number} from_position
571 * @param {number} to_position
572 * @constructor
573 */
574function SourceSlice(script, from_line, to_line, from_position, to_position) {
575 this.script = script;
576 this.from_line = from_line;
577 this.to_line = to_line;
578 this.from_position = from_position;
579 this.to_position = to_position;
580}
581
582
583/**
584 * Get the source text for a SourceSlice
585 * @return {String} Source text for this slice. The last line will include
586 * the line terminating characters (if any)
587 */
588SourceSlice.prototype.sourceText = function () {
ager@chromium.org3e875802009-06-29 08:26:34 +0000589 return StringSubstring.call(this.script.source, this.from_position, this.to_position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000590};
591
592
593// Returns the offset of the given position within the containing
594// line.
595function GetPositionInLine(message) {
ager@chromium.org3a6061e2009-03-12 14:24:36 +0000596 var location = message.script.locationFromPosition(message.startPos, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000597 if (location == null) return -1;
598 location.restrict();
599 return message.startPos - location.start;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000600}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601
602
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000603function ErrorMessage(type, args, startPos, endPos, script, stackTrace,
604 stackFrames) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000605 this.startPos = startPos;
606 this.endPos = endPos;
607 this.type = type;
608 this.args = args;
609 this.script = script;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000610 this.stackTrace = stackTrace;
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000611 this.stackFrames = stackFrames;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000612}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000613
614
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000615function MakeMessage(type, args, startPos, endPos, script, stackTrace,
616 stackFrames) {
617 return new ErrorMessage(type, args, startPos, endPos, script, stackTrace,
618 stackFrames);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000619}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000620
621
622function GetStackTraceLine(recv, fun, pos, isGlobal) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000623 return FormatSourcePosition(new CallSite(recv, fun, pos));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000624}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000625
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000626// ----------------------------------------------------------------------------
627// Error implementation
628
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000629// Defines accessors for a property that is calculated the first time
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000630// the property is read.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000631function DefineOneShotAccessor(obj, name, fun) {
632 // Note that the accessors consistently operate on 'obj', not 'this'.
633 // Since the object may occur in someone else's prototype chain we
634 // can't rely on 'this' being the same as 'obj'.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000635 var hasBeenSet = false;
636 var value;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000637 obj.__defineGetter__(name, function () {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000638 if (hasBeenSet) {
639 return value;
640 }
641 hasBeenSet = true;
642 value = fun(obj);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000643 return value;
644 });
645 obj.__defineSetter__(name, function (v) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000646 hasBeenSet = true;
647 value = v;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000648 });
649}
650
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000651function CallSite(receiver, fun, pos) {
652 this.receiver = receiver;
653 this.fun = fun;
654 this.pos = pos;
655}
656
657CallSite.prototype.getThis = function () {
658 return this.receiver;
659};
660
661CallSite.prototype.getTypeName = function () {
662 var constructor = this.receiver.constructor;
663 if (!constructor)
664 return $Object.prototype.toString.call(this.receiver);
665 var constructorName = constructor.name;
666 if (!constructorName)
667 return $Object.prototype.toString.call(this.receiver);
668 return constructorName;
669};
670
671CallSite.prototype.isToplevel = function () {
672 if (this.receiver == null)
673 return true;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000674 return IS_GLOBAL(this.receiver);
675};
676
677CallSite.prototype.isEval = function () {
678 var script = %FunctionGetScript(this.fun);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000679 return script && script.compilation_type == COMPILATION_TYPE_EVAL;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000680};
681
682CallSite.prototype.getEvalOrigin = function () {
683 var script = %FunctionGetScript(this.fun);
sgjesse@chromium.org98180592009-12-02 08:17:28 +0000684 return FormatEvalOrigin(script);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000685};
686
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000687CallSite.prototype.getScriptNameOrSourceURL = function () {
688 var script = %FunctionGetScript(this.fun);
689 return script ? script.nameOrSourceURL() : null;
690};
691
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000692CallSite.prototype.getFunction = function () {
693 return this.fun;
694};
695
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000696CallSite.prototype.getFunctionName = function () {
697 // See if the function knows its own name
698 var name = this.fun.name;
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000699 if (name) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000700 return name;
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000701 } else {
702 return %FunctionGetInferredName(this.fun);
703 }
704 // Maybe this is an evaluation?
705 var script = %FunctionGetScript(this.fun);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000706 if (script && script.compilation_type == COMPILATION_TYPE_EVAL)
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000707 return "eval";
708 return null;
709};
710
711CallSite.prototype.getMethodName = function () {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000712 // See if we can find a unique property on the receiver that holds
713 // this function.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000714 var ownName = this.fun.name;
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000715 if (ownName && this.receiver &&
716 (ObjectLookupGetter.call(this.receiver, ownName) === this.fun ||
717 ObjectLookupSetter.call(this.receiver, ownName) === this.fun ||
718 this.receiver[ownName] === this.fun)) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000719 // To handle DontEnum properties we guess that the method has
720 // the same name as the function.
721 return ownName;
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000722 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000723 var name = null;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000724 for (var prop in this.receiver) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000725 if (this.receiver.__lookupGetter__(prop) === this.fun ||
726 this.receiver.__lookupSetter__(prop) === this.fun ||
727 (!this.receiver.__lookupGetter__(prop) && this.receiver[prop] === this.fun)) {
728 // If we find more than one match bail out to avoid confusion.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000729 if (name)
730 return null;
731 name = prop;
732 }
733 }
734 if (name)
735 return name;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000736 return null;
737};
738
739CallSite.prototype.getFileName = function () {
740 var script = %FunctionGetScript(this.fun);
741 return script ? script.name : null;
742};
743
744CallSite.prototype.getLineNumber = function () {
745 if (this.pos == -1)
746 return null;
747 var script = %FunctionGetScript(this.fun);
748 var location = null;
749 if (script) {
750 location = script.locationFromPosition(this.pos, true);
751 }
752 return location ? location.line + 1 : null;
753};
754
755CallSite.prototype.getColumnNumber = function () {
756 if (this.pos == -1)
757 return null;
758 var script = %FunctionGetScript(this.fun);
759 var location = null;
760 if (script) {
761 location = script.locationFromPosition(this.pos, true);
762 }
sgjesse@chromium.org98180592009-12-02 08:17:28 +0000763 return location ? location.column + 1: null;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000764};
765
766CallSite.prototype.isNative = function () {
767 var script = %FunctionGetScript(this.fun);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000768 return script ? (script.type == TYPE_NATIVE) : false;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000769};
770
771CallSite.prototype.getPosition = function () {
772 return this.pos;
773};
774
775CallSite.prototype.isConstructor = function () {
776 var constructor = this.receiver ? this.receiver.constructor : null;
777 if (!constructor)
778 return false;
779 return this.fun === constructor;
780};
781
sgjesse@chromium.org98180592009-12-02 08:17:28 +0000782function FormatEvalOrigin(script) {
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000783 var sourceURL = script.nameOrSourceURL();
784 if (sourceURL)
785 return sourceURL;
786
787 var eval_origin = "eval at ";
sgjesse@chromium.org98180592009-12-02 08:17:28 +0000788 if (script.eval_from_function_name) {
789 eval_origin += script.eval_from_function_name;
790 } else {
791 eval_origin += "<anonymous>";
792 }
lrn@chromium.org25156de2010-04-06 13:10:27 +0000793
sgjesse@chromium.org98180592009-12-02 08:17:28 +0000794 var eval_from_script = script.eval_from_script;
795 if (eval_from_script) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000796 if (eval_from_script.compilation_type == COMPILATION_TYPE_EVAL) {
sgjesse@chromium.org98180592009-12-02 08:17:28 +0000797 // eval script originated from another eval.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000798 eval_origin += " (" + FormatEvalOrigin(eval_from_script) + ")";
sgjesse@chromium.org98180592009-12-02 08:17:28 +0000799 } else {
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000800 // eval script originated from "real" source.
sgjesse@chromium.org98180592009-12-02 08:17:28 +0000801 if (eval_from_script.name) {
802 eval_origin += " (" + eval_from_script.name;
803 var location = eval_from_script.locationFromPosition(script.eval_from_script_position, true);
804 if (location) {
805 eval_origin += ":" + (location.line + 1);
806 eval_origin += ":" + (location.column + 1);
807 }
808 eval_origin += ")"
809 } else {
810 eval_origin += " (unknown source)";
811 }
812 }
813 }
lrn@chromium.org25156de2010-04-06 13:10:27 +0000814
sgjesse@chromium.org98180592009-12-02 08:17:28 +0000815 return eval_origin;
816};
817
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000818function FormatSourcePosition(frame) {
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000819 var fileName;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000820 var fileLocation = "";
821 if (frame.isNative()) {
822 fileLocation = "native";
823 } else if (frame.isEval()) {
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000824 fileName = frame.getScriptNameOrSourceURL();
825 if (!fileName)
826 fileLocation = frame.getEvalOrigin();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000827 } else {
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000828 fileName = frame.getFileName();
829 }
830
831 if (fileName) {
832 fileLocation += fileName;
833 var lineNumber = frame.getLineNumber();
834 if (lineNumber != null) {
835 fileLocation += ":" + lineNumber;
836 var columnNumber = frame.getColumnNumber();
837 if (columnNumber) {
838 fileLocation += ":" + columnNumber;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000839 }
840 }
841 }
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000842
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000843 if (!fileLocation) {
844 fileLocation = "unknown source";
845 }
846 var line = "";
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000847 var functionName = frame.getFunction().name;
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000848 var addPrefix = true;
849 var isConstructor = frame.isConstructor();
850 var isMethodCall = !(frame.isToplevel() || isConstructor);
851 if (isMethodCall) {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000852 var methodName = frame.getMethodName();
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000853 line += frame.getTypeName() + ".";
854 if (functionName) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000855 line += functionName;
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000856 if (methodName && (methodName != functionName)) {
857 line += " [as " + methodName + "]";
858 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000859 } else {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000860 line += methodName || "<anonymous>";
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000861 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000862 } else if (isConstructor) {
863 line += "new " + (functionName || "<anonymous>");
864 } else if (functionName) {
865 line += functionName;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000866 } else {
867 line += fileLocation;
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000868 addPrefix = false;
869 }
870 if (addPrefix) {
871 line += " (" + fileLocation + ")";
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000872 }
873 return line;
874}
875
876function FormatStackTrace(error, frames) {
877 var lines = [];
878 try {
879 lines.push(error.toString());
880 } catch (e) {
881 try {
882 lines.push("<error: " + e + ">");
883 } catch (ee) {
884 lines.push("<error>");
885 }
886 }
887 for (var i = 0; i < frames.length; i++) {
888 var frame = frames[i];
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000889 var line;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000890 try {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000891 line = FormatSourcePosition(frame);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000892 } catch (e) {
893 try {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000894 line = "<error: " + e + ">";
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000895 } catch (ee) {
896 // Any code that reaches this point is seriously nasty!
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000897 line = "<error>";
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000898 }
899 }
900 lines.push(" at " + line);
901 }
902 return lines.join("\n");
903}
904
905function FormatRawStackTrace(error, raw_stack) {
906 var frames = [ ];
907 for (var i = 0; i < raw_stack.length; i += 3) {
908 var recv = raw_stack[i];
909 var fun = raw_stack[i+1];
910 var pc = raw_stack[i+2];
911 var pos = %FunctionGetPositionForOffset(fun, pc);
912 frames.push(new CallSite(recv, fun, pos));
913 }
914 if (IS_FUNCTION($Error.prepareStackTrace)) {
915 return $Error.prepareStackTrace(error, frames);
916 } else {
917 return FormatStackTrace(error, frames);
918 }
919}
920
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000921function DefineError(f) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000922 // Store the error function in both the global object
923 // and the runtime object. The function is fetched
924 // from the runtime object when throwing errors from
925 // within the runtime system to avoid strange side
926 // effects when overwriting the error functions from
927 // user code.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000928 var name = f.name;
929 %SetProperty(global, name, f, DONT_ENUM);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000930 this['$' + name] = f;
931 // Configure the error function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000932 if (name == 'Error') {
933 // The prototype of the Error object must itself be an error.
934 // However, it can't be an instance of the Error object because
935 // it hasn't been properly configured yet. Instead we create a
936 // special not-a-true-error-but-close-enough object.
937 function ErrorPrototype() {}
938 %FunctionSetPrototype(ErrorPrototype, $Object.prototype);
939 %FunctionSetInstanceClassName(ErrorPrototype, 'Error');
940 %FunctionSetPrototype(f, new ErrorPrototype());
941 } else {
942 %FunctionSetPrototype(f, new $Error());
943 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944 %FunctionSetInstanceClassName(f, 'Error');
ager@chromium.org7c537e22008-10-16 08:43:32 +0000945 %SetProperty(f.prototype, 'constructor', f, DONT_ENUM);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000946 f.prototype.name = name;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947 %SetCode(f, function(m) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000948 if (%_IsConstructCall()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000949 if (m === kAddMessageAccessorsMarker) {
950 DefineOneShotAccessor(this, 'message', function (obj) {
951 return FormatMessage({type: obj.type, args: obj.arguments});
952 });
953 } else if (!IS_UNDEFINED(m)) {
954 this.message = ToString(m);
955 }
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000956 captureStackTrace(this, f);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000957 } else {
958 return new f(m);
959 }
960 });
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000961}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000963function captureStackTrace(obj, cons_opt) {
964 var stackTraceLimit = $Error.stackTraceLimit;
965 if (!stackTraceLimit) return;
966 if (stackTraceLimit < 0 || stackTraceLimit > 10000)
967 stackTraceLimit = 10000;
968 var raw_stack = %CollectStackTrace(cons_opt ? cons_opt : captureStackTrace,
969 stackTraceLimit);
970 DefineOneShotAccessor(obj, 'stack', function (obj) {
971 return FormatRawStackTrace(obj, raw_stack);
972 });
973};
974
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975$Math.__proto__ = global.Object.prototype;
976
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000977DefineError(function Error() { });
978DefineError(function TypeError() { });
979DefineError(function RangeError() { });
980DefineError(function SyntaxError() { });
981DefineError(function ReferenceError() { });
982DefineError(function EvalError() { });
983DefineError(function URIError() { });
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000985$Error.captureStackTrace = captureStackTrace;
986
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000987// Setup extra properties of the Error.prototype object.
988$Error.prototype.message = '';
989
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000990%SetProperty($Error.prototype, 'toString', function toString() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000991 var type = this.type;
992 if (type && !this.hasOwnProperty("message")) {
993 return this.name + ": " + FormatMessage({ type: type, args: this.arguments });
994 }
995 var message = this.message;
996 return this.name + (message ? (": " + message) : "");
997}, DONT_ENUM);
998
999
1000// Boilerplate for exceptions for stack overflows. Used from
1001// Top::StackOverflow().
1002const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []);