blob: 8aa2914fd08eebeb95d055fd7faf66d1b890c663 [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// -------------------------------------------------------------------
30
31const kVowelSounds = {a: true, e: true, i: true, o: true, u: true, y: true};
32const kCapitalVowelSounds = {a: true, e: true, i: true, o: true, u: true,
33 h: true, f: true, l: true, m: true, n: true, r: true, s: true, x: true,
34 y: true};
35
36function GetInstanceName(cons) {
37 if (cons.length == 0) {
38 return "";
39 }
40 var first = cons.charAt(0).toLowerCase();
41 var mapping = kVowelSounds;
42 if (cons.length > 1 && (cons.charAt(0) != first)) {
43 // First char is upper case
44 var second = cons.charAt(1).toLowerCase();
45 // Second char is upper case
46 if (cons.charAt(1) != second)
47 mapping = kCapitalVowelSounds;
48 }
49 var s = mapping[first] ? "an " : "a ";
50 return s + cons;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000051}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052
53
54const kMessages = {
55 // Error
56 cyclic_proto: "Cyclic __proto__ value",
57 // TypeError
58 unexpected_token: "Unexpected token %0",
59 unexpected_token_number: "Unexpected number",
60 unexpected_token_string: "Unexpected string",
61 unexpected_token_identifier: "Unexpected identifier",
62 unexpected_eos: "Unexpected end of input",
63 expected_label: "Expected label",
64 malformed_regexp: "Invalid regular expression: /%0/: %1",
65 unterminated_regexp: "Invalid regular expression: missing /",
66 pcre_error: "PCRE function %0, error code %1",
67 regexp_flags: "Cannot supply flags when constructing one RegExp from another",
68 invalid_lhs_in_assignment: "Invalid left-hand side in assignment",
69 invalid_lhs_in_for_in: "Invalid left-hand side in for-in",
70 invalid_lhs_in_postfix_op: "Invalid left-hand side expression in postfix operation",
71 invalid_lhs_in_prefix_op: "Invalid left-hand side expression in prefix operation",
72 multiple_defaults_in_switch: "More than one default clause in switch statement",
73 newline_after_throw: "Illegal newline after throw",
74 redeclaration: "%0 '%1' has already been declared",
75 no_catch_or_finally: "Missing catch or finally after try",
76 unknown_label: "Undefined label '%0'",
77 invalid_break: "Invalid break statement",
78 invalid_continue: "Invalid continue statement",
79 uncaught_exception: "Uncaught %0",
80 stack_trace: "Stack Trace:\n%0",
81 called_non_callable: "%0 is not a function",
82 undefined_method: "Object %1 has no method '%0'",
83 property_not_function: "Property '%0' of object %1 is not a function",
84 null_or_undefined: "Cannot access property of null or undefined",
85 cannot_convert_to_primitive: "Cannot convert object to primitive value",
86 not_constructor: "%0 is not a constructor",
87 not_defined: "%0 is not defined",
88 non_object_property_load: "Cannot read property '%0' of %1",
89 non_object_property_store: "Cannot set property '%0' of %1",
90 non_object_property_call: "Cannot call method '%0' of %1",
91 illegal_eval: "Unsupported indirect eval() call",
92 with_expression: "%0 has no properties",
93 illegal_invocation: "Illegal invocation",
94 no_setter_in_callback: "Cannot set property %0 of %1 which has only a getter",
95 apply_non_function: "Function.prototype.apply was called on %0, which is a %1 and not a function",
96 apply_wrong_args: "Function.prototype.apply: Arguments list has wrong type",
97 invalid_in_operator_use: "Cannot use 'in' operator to search for '%0' in %1",
98 instanceof_function_expected: "Expecting a function in instanceof check, but got %0",
99 instanceof_nonobject_proto: "Function has non-object prototype '%0' in instanceof check",
100 null_to_object: "Cannot convert null to object",
101 // RangeError
102 invalid_array_length: "Invalid array length",
103 invalid_array_apply_length: "Function.prototype.apply supports only up to 1024 arguments",
104 stack_overflow: "Maximum call stack size exceeded",
105 apply_overflow: "Function.prototype.apply cannot support %0 arguments",
106 // SyntaxError
107 unable_to_parse: "Parse error",
108 duplicate_regexp_flag: "Duplicate RegExp flag %0",
109 unrecognized_regexp_flag: "Unrecognized RegExp flag %0",
110 invalid_regexp: "Invalid RegExp pattern /%0/",
111 illegal_break: "Illegal break statement",
112 illegal_continue: "Illegal continue statement",
113 illegal_return: "Illegal return statement",
114 error_loading_debugger: "Error loading debugger %0",
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000115 no_input_to_regexp: "No input to %0",
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000116};
117
118
119function FormatString(format, args) {
120 var result = format;
121 for (var i = 0; i < args.length; i++) {
122 var str;
123 try { str = ToDetailString(args[i]); }
124 catch (e) { str = "#<error>"; }
125 result = result.split("%" + i).join(str);
126 }
127 return result;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000128}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000129
130
131function ToDetailString(obj) {
132 if (obj != null && IS_OBJECT(obj) && obj.toString === $Object.prototype.toString) {
133 var constructor = obj.constructor;
134 if (!constructor) return ToString(obj);
135 var constructorName = constructor.name;
136 if (!constructorName) return ToString(obj);
137 return "#<" + GetInstanceName(constructorName) + ">";
138 } else {
139 return ToString(obj);
140 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000141}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142
143
144function MakeGenericError(constructor, type, args) {
145 if (args instanceof $Array) {
146 for (var i = 0; i < args.length; i++) {
147 var elem = args[i];
148 if (elem instanceof $Array && elem.length > 100) { // arbitrary limit, grab a reasonable slice to report
149 args[i] = elem.slice(0,20).concat("...");
150 }
151 }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000152 } else if (IS_UNDEFINED(args)) {
153 args = [];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154 }
155
156 var e = new constructor();
157 e.type = type;
158 e.arguments = args;
159 return e;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000160}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161
162
163/**
164 * Setup the Script function and constructor.
165 */
166%FunctionSetInstanceClassName(Script, 'Script');
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000167%SetProperty(Script.prototype, 'constructor', Script, DONT_ENUM);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168%SetCode(Script, function(x) {
169 // Script objects can only be created by the VM.
170 throw new $Error("Not supported");
171});
172
173
174// Helper functions; called from the runtime system.
175function FormatMessage(message) {
176 var format = kMessages[message.type];
177 if (!format) return "<unknown message " + message.type + ">";
178 return FormatString(format, message.args);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000179}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000180
181
182function GetLineNumber(message) {
183 if (message.startPos == -1) return -1;
184 var location = message.script.locationFromPosition(message.startPos);
185 if (location == null) return -1;
186 return location.line + 1;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000187}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188
189
190// Returns the source code line containing the given source
191// position, or the empty string if the position is invalid.
192function GetSourceLine(message) {
193 var location = message.script.locationFromPosition(message.startPos);
194 if (location == null) return "";
195 location.restrict();
196 return location.sourceText();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000197}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000198
199
200function MakeTypeError(type, args) {
201 return MakeGenericError($TypeError, type, args);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000202}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203
204
205function MakeRangeError(type, args) {
206 return MakeGenericError($RangeError, type, args);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000207}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208
209
210function MakeSyntaxError(type, args) {
211 return MakeGenericError($SyntaxError, type, args);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000212}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213
214
215function MakeReferenceError(type, args) {
216 return MakeGenericError($ReferenceError, type, args);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000217}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000218
219
220function MakeEvalError(type, args) {
221 return MakeGenericError($EvalError, type, args);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000222}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223
224
225function MakeError(type, args) {
226 return MakeGenericError($Error, type, args);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000227}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000228
229
230/**
231 * Initialize the cached source information in a script. Currently all line
232 * end positions are cached.
233 */
234Script.prototype.initSourceInfo_ = function () {
235 // Just return if initialized.
236 if (this.lineEnds_) return;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000237
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000238 // Collect all line endings.
239 this.lineEnds_ = [];
240 for (var i = 0; i < this.source.length; i++) {
241 var current = this.source.charAt(i);
242 if (current == '\n') {
243 this.lineEnds_.push(i);
244 }
245 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000246
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000247 // If the script does not end with a line ending add the final end position
248 // as just past the last line ending.
249 if (this.lineEnds_[this.lineEnds_.length - 1] != this.source.length - 1) {
250 this.lineEnds_.push(this.source.length);
251 }
252};
253
254
255/**
256 * Get information on a specific source position.
257 * @param {number} position The source position
258 * @return {SourceLocation}
259 * If line is negative or not in the source null is returned.
260 */
261Script.prototype.locationFromPosition = function (position) {
262 // Make sure source info has been initialized.
263 this.initSourceInfo_();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000264
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265 var lineCount = this.lineCount();
266 var line = -1;
267 if (position <= this.lineEnds_[0]) {
268 line = 0;
269 } else {
270 for (var i = 1; i < lineCount; i++) {
271 if (this.lineEnds_[i - 1] < position && position <= this.lineEnds_[i]) {
272 line = i;
273 break;
274 }
275 }
276 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000277
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278 if (line == -1) return null;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000279
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280 // Determine start, end and column.
281 var start = line == 0 ? 0 : this.lineEnds_[line - 1] + 1;
282 var end = this.lineEnds_[line];
283 if (end > 0 && this.source.charAt(end - 1) == '\r') end--;
284 var column = position - start;
285
286 // Adjust according to the offset within the resource.
287 line += this.line_offset;
288 if (line == this.line_offset) {
289 column += this.column_offset;
290 }
291
292 return new SourceLocation(this, position, line, column, start, end);
293};
294
295
296/**
297 * Get information on a specific source line and column possibly offset by a
298 * fixed source position. This function is used to find a source position from
299 * a line and column position. The fixed source position offset is typically
300 * used to find a source position in a function based on a line and column in
301 * the source for the function alone. The offset passed will then be the
302 * start position of the source for the function within the full script source.
303 * @param {number} opt_line The line within the source. Default value is 0
304 * @param {number} opt_column The column in within the line. Default value is 0
305 * @param {number} opt_offset_position The offset from the begining of the
306 * source from where the line and column calculation starts. Default value is 0
307 * @return {SourceLocation}
308 * If line is negative or not in the source null is returned.
309 */
310Script.prototype.locationFromLine = function (opt_line, opt_column, opt_offset_position) {
311 // Make soure source info has been initialized.
312 this.initSourceInfo_();
313
314 // Default is the first line in the script. Lines in the script is relative
315 // to the offset within the resource.
316 var line = 0;
317 if (!IS_UNDEFINED(opt_line)) {
318 line = opt_line - this.line_offset;
319 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000320
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321 // Default is first column. If on the first line add the offset within the
322 // resource.
323 var column = opt_column || 0;
324 if (line == 0) {
325 column -= this.column_offset
326 }
327
328 var offset_position = opt_offset_position || 0;
329 if (line < 0 || column < 0 || offset_position < 0) return null;
330 if (line == 0) {
331 return this.locationFromPosition(offset_position + column);
332 } else {
333 // Find the line where the offset position is located
334 var lineCount = this.lineCount();
335 var offset_line;
336 for (var i = 0; i < lineCount; i++) {
337 if (offset_position <= this.lineEnds_[i]) {
338 offset_line = i;
339 break;
340 }
341 }
342 if (offset_line + line >= lineCount) return null;
343 return this.locationFromPosition(this.lineEnds_[offset_line + line - 1] + 1 + column); // line > 0 here.
344 }
345}
346
347
348/**
349 * Get a slice of source code from the script. The boundaries for the slice is
350 * specified in lines.
351 * @param {number} opt_from_line The first line (zero bound) in the slice.
352 * Default is 0
353 * @param {number} opt_to_column The last line (zero bound) in the slice (non
354 * inclusive). Default is the number of lines in the script
355 * @return {SourceSlice} The source slice or null of the parameters where
356 * invalid
357 */
358Script.prototype.sourceSlice = function (opt_from_line, opt_to_line) {
359 // Make soure source info has been initialized.
360 this.initSourceInfo_();
361
362 var from_line = IS_UNDEFINED(opt_from_line) ? this.line_offset : opt_from_line;
363 var to_line = IS_UNDEFINED(opt_to_line) ? this.line_offset + this.lineCount() : opt_to_line
364
365 // Adjust according to the offset within the resource.
366 from_line -= this.line_offset;
367 to_line -= this.line_offset;
368 if (from_line < 0) from_line = 0;
369 if (to_line > this.lineCount()) to_line = this.lineCount();
370
371 // Check parameters.
372 if (from_line >= this.lineCount() ||
373 to_line < 0 ||
374 from_line > to_line) {
375 return null;
376 }
377
378 var from_position = from_line == 0 ? 0 : this.lineEnds_[from_line - 1] + 1;
379 var to_position = to_line == 0 ? 0 : this.lineEnds_[to_line - 1] + 1;
380
381 // Return a source slice with line numbers re-adjusted to the resource.
382 return new SourceSlice(this, from_line + this.line_offset, to_line + this.line_offset,
383 from_position, to_position);
384}
385
386
387Script.prototype.sourceLine = function (opt_line) {
388 // Default is the first line in the script. Lines in the script are relative
389 // to the offset within the resource.
390 var line = 0;
391 if (!IS_UNDEFINED(opt_line)) {
392 line = opt_line - this.line_offset;
393 }
394
395 // Check parameter.
396 if (line < 0 || this.lineCount() <= line) {
397 return null;
398 }
399
400 // Return the source line.
401 var start = line == 0 ? 0 : this.lineEnds_[line - 1] + 1;
402 var end = this.lineEnds_[line];
403 return this.source.substring(start, end);
404}
405
406
407/**
408 * Returns the number of source lines.
409 * @return {number}
410 * Number of source lines.
411 */
412Script.prototype.lineCount = function() {
413 // Make soure source info has been initialized.
414 this.initSourceInfo_();
415
416 // Return number of source lines.
417 return this.lineEnds_.length;
418};
419
420
421/**
422 * Class for source location. A source location is a position within some
423 * source with the following properties:
424 * script : script object for the source
425 * line : source line number
426 * column : source column within the line
427 * position : position within the source
428 * start : position of start of source context (inclusive)
429 * end : position of end of source context (not inclusive)
430 * Source text for the source context is the character interval [start, end[. In
431 * most cases end will point to a newline character. It might point just past
432 * the final position of the source if the last source line does not end with a
433 * newline character.
434 * @param {Script} script The Script object for which this is a location
435 * @param {number} position Source position for the location
436 * @param {number} line The line number for the location
437 * @param {number} column The column within the line for the location
438 * @param {number} start Source position for start of source context
439 * @param {number} end Source position for end of source context
440 * @constructor
441 */
442function SourceLocation(script, position, line, column, start, end) {
443 this.script = script;
444 this.position = position;
445 this.line = line;
446 this.column = column;
447 this.start = start;
448 this.end = end;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000449}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000450
451
452const kLineLengthLimit = 78;
453
454/**
455 * Restrict source location start and end positions to make the source slice
456 * no more that a certain number of characters wide.
457 * @param {number} opt_limit The with limit of the source text with a default
458 * of 78
459 * @param {number} opt_before The number of characters to prefer before the
460 * position with a default value of 10 less that the limit
461 */
462SourceLocation.prototype.restrict = function (opt_limit, opt_before) {
463 // Find the actual limit to use.
464 var limit;
465 var before;
466 if (!IS_UNDEFINED(opt_limit)) {
467 limit = opt_limit;
468 } else {
469 limit = kLineLengthLimit;
470 }
471 if (!IS_UNDEFINED(opt_before)) {
472 before = opt_before;
473 } else {
474 // If no before is specified center for small limits and perfer more source
475 // before the the position that after for longer limits.
476 if (limit <= 20) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000477 before = $floor(limit / 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000478 } else {
479 before = limit - 10;
480 }
481 }
482 if (before >= limit) {
483 before = limit - 1;
484 }
485
486 // If the [start, end[ interval is too big we restrict
487 // it in one or both ends. We make sure to always produce
488 // restricted intervals of maximum allowed size.
489 if (this.end - this.start > limit) {
490 var start_limit = this.position - before;
491 var end_limit = this.position + limit - before;
492 if (this.start < start_limit && end_limit < this.end) {
493 this.start = start_limit;
494 this.end = end_limit;
495 } else if (this.start < start_limit) {
496 this.start = this.end - limit;
497 } else {
498 this.end = this.start + limit;
499 }
500 }
501};
502
503
504/**
505 * Get the source text for a SourceLocation
506 * @return {String}
507 * Source text for this location.
508 */
509SourceLocation.prototype.sourceText = function () {
510 return this.script.source.substring(this.start, this.end);
511};
512
513
514/**
515 * Class for a source slice. A source slice is a part of a script source with
516 * the following properties:
517 * script : script object for the source
518 * from_line : line number for the first line in the slice
519 * to_line : source line number for the last line in the slice
520 * from_position : position of the first character in the slice
521 * to_position : position of the last character in the slice
522 * The to_line and to_position are not included in the slice, that is the lines
523 * in the slice are [from_line, to_line[. Likewise the characters in the slice
524 * are [from_position, to_position[.
525 * @param {Script} script The Script object for the source slice
526 * @param {number} from_line
527 * @param {number} to_line
528 * @param {number} from_position
529 * @param {number} to_position
530 * @constructor
531 */
532function SourceSlice(script, from_line, to_line, from_position, to_position) {
533 this.script = script;
534 this.from_line = from_line;
535 this.to_line = to_line;
536 this.from_position = from_position;
537 this.to_position = to_position;
538}
539
540
541/**
542 * Get the source text for a SourceSlice
543 * @return {String} Source text for this slice. The last line will include
544 * the line terminating characters (if any)
545 */
546SourceSlice.prototype.sourceText = function () {
547 return this.script.source.substring(this.from_position, this.to_position);
548};
549
550
551// Returns the offset of the given position within the containing
552// line.
553function GetPositionInLine(message) {
554 var location = message.script.locationFromPosition(message.startPos);
555 if (location == null) return -1;
556 location.restrict();
557 return message.startPos - location.start;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000558}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559
560
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000561function ErrorMessage(type, args, startPos, endPos, script, stackTrace) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000562 this.startPos = startPos;
563 this.endPos = endPos;
564 this.type = type;
565 this.args = args;
566 this.script = script;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000567 this.stackTrace = stackTrace;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000568}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569
570
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000571function MakeMessage(type, args, startPos, endPos, script, stackTrace) {
572 return new ErrorMessage(type, args, startPos, endPos, script, stackTrace);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000573}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000574
575
576function GetStackTraceLine(recv, fun, pos, isGlobal) {
577 try {
578 return UnsafeGetStackTraceLine(recv, fun, pos, isGlobal);
579 } catch (e) {
580 return "<error: " + e + ">";
581 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000582}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583
584
585function GetFunctionName(fun, recv) {
586 var name = %FunctionGetName(fun);
587 if (name) return name;
588 for (var prop in recv) {
589 if (recv[prop] === fun)
590 return prop;
591 }
592 return "[anonymous]";
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000593}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594
595
596function UnsafeGetStackTraceLine(recv, fun, pos, isTopLevel) {
597 var result = "";
598 // The global frame has no meaningful function or receiver
599 if (!isTopLevel) {
600 // If the receiver is not the global object then prefix the
601 // message send
602 if (recv !== global)
603 result += ToDetailString(recv) + ".";
604 result += GetFunctionName(fun, recv);
605 }
606 if (pos != -1) {
607 var script = %FunctionGetScript(fun);
608 var file;
609 if (script) {
610 file = %FunctionGetScript(fun).data;
611 }
612 if (file) {
613 var location = %FunctionGetScript(fun).locationFromPosition(pos);
614 if (!isTopLevel) result += "(";
615 result += file;
616 if (location != null) {
617 result += ":" + (location.line + 1) + ":" + (location.column + 1);
618 }
619 if (!isTopLevel) result += ")";
620 }
621 }
622 return (result) ? " at " + result : result;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000623}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000624
625
626// ----------------------------------------------------------------------------
627// Error implementation
628
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000629function DefineError(f) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630 // Store the error function in both the global object
631 // and the runtime object. The function is fetched
632 // from the runtime object when throwing errors from
633 // within the runtime system to avoid strange side
634 // effects when overwriting the error functions from
635 // user code.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000636 var name = f.name;
637 %SetProperty(global, name, f, DONT_ENUM);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000638 this['$' + name] = f;
639 // Configure the error function.
640 // prototype of 'Error' must be as default: new Object().
641 if (name != 'Error') %FunctionSetPrototype(f, new $Error());
642 %FunctionSetInstanceClassName(f, 'Error');
ager@chromium.org7c537e22008-10-16 08:43:32 +0000643 %SetProperty(f.prototype, 'constructor', f, DONT_ENUM);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000644 f.prototype.name = name;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000645 %SetCode(f, function(m) {
mads.s.ager31e71382008-08-13 09:32:07 +0000646 if (%IsConstructCall()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000647 if (!IS_UNDEFINED(m)) this.message = ToString(m);
648 } else {
649 return new f(m);
650 }
651 });
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000652}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653
654$Math.__proto__ = global.Object.prototype;
655
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000656DefineError(function Error() { });
657DefineError(function TypeError() { });
658DefineError(function RangeError() { });
659DefineError(function SyntaxError() { });
660DefineError(function ReferenceError() { });
661DefineError(function EvalError() { });
662DefineError(function URIError() { });
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000663
664// Setup extra properties of the Error.prototype object.
665$Error.prototype.message = '';
666
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000667%SetProperty($Error.prototype, 'toString', function toString() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668 var type = this.type;
669 if (type && !this.hasOwnProperty("message")) {
670 return this.name + ": " + FormatMessage({ type: type, args: this.arguments });
671 }
672 var message = this.message;
673 return this.name + (message ? (": " + message) : "");
674}, DONT_ENUM);
675
676
677// Boilerplate for exceptions for stack overflows. Used from
678// Top::StackOverflow().
679const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []);