The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / src / messages.js @ 40c0f755

History | View | Annotate | Download (23.8 KB)

1
// Copyright 2006-2008 the V8 project authors. All rights reserved.
2
// 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

    
31
const kVowelSounds = {a: true, e: true, i: true, o: true, u: true, y: true};
32
const 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

    
36
function 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;
51
}
52

    
53

    
54
const 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",
115
  no_input_to_regexp:           "No input to %0",
116
};
117

    
118

    
119
function 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;
128
}
129

    
130

    
131
function 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
  }
141
}
142

    
143

    
144
function 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
    }
152
  } else if (IS_UNDEFINED(args)) {
153
    args = [];
154
  }
155

    
156
  var e = new constructor(kAddMessageAccessorsMarker);
157
  e.type = type;
158
  e.arguments = args;
159
  return e;
160
}
161

    
162

    
163
/**
164
 * Setup the Script function and constructor.
165
 */
166
%FunctionSetInstanceClassName(Script, 'Script');
167
%SetProperty(Script.prototype, 'constructor', Script, DONT_ENUM);
168
%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.
175
function FormatMessage(message) {
176
  var format = kMessages[message.type];
177
  if (!format) return "<unknown message " + message.type + ">";
178
  return FormatString(format, message.args);
179
}
180

    
181

    
182
function GetLineNumber(message) {
183
  if (message.startPos == -1) return -1;
184
  var location = message.script.locationFromPosition(message.startPos, true);
185
  if (location == null) return -1;
186
  return location.line + 1;
187
}
188

    
189

    
190
// Returns the source code line containing the given source
191
// position, or the empty string if the position is invalid.
192
function GetSourceLine(message) {
193
  var location = message.script.locationFromPosition(message.startPos, true);
194
  if (location == null) return "";
195
  location.restrict();
196
  return location.sourceText();
197
}
198

    
199

    
200
function MakeTypeError(type, args) {
201
  return MakeGenericError($TypeError, type, args);
202
}
203

    
204

    
205
function MakeRangeError(type, args) {
206
  return MakeGenericError($RangeError, type, args);
207
}
208

    
209

    
210
function MakeSyntaxError(type, args) {
211
  return MakeGenericError($SyntaxError, type, args);
212
}
213

    
214

    
215
function MakeReferenceError(type, args) {
216
  return MakeGenericError($ReferenceError, type, args);
217
}
218

    
219

    
220
function MakeEvalError(type, args) {
221
  return MakeGenericError($EvalError, type, args);
222
}
223

    
224

    
225
function MakeError(type, args) {
226
  return MakeGenericError($Error, type, args);
227
}
228

    
229

    
230
/**
231
 * Get information on a specific source position.
232
 * @param {number} position The source position
233
 * @param {boolean} include_resource_offset Set to true to have the resource
234
 *     offset added to the location
235
 * @return {SourceLocation}
236
 *     If line is negative or not in the source null is returned.
237
 */
238
Script.prototype.locationFromPosition = function (position,
239
                                                  include_resource_offset) {
240
  var lineCount = this.lineCount();
241
  var line = -1;
242
  if (position <= this.line_ends[0]) {
243
    line = 0;
244
  } else {
245
    for (var i = 1; i < lineCount; i++) {
246
      if (this.line_ends[i - 1] < position && position <= this.line_ends[i]) {
247
        line = i;
248
        break;
249
      }
250
    }
251
  }
252

    
253
  if (line == -1) return null;
254

    
255
  // Determine start, end and column.
256
  var start = line == 0 ? 0 : this.line_ends[line - 1] + 1;
257
  var end = this.line_ends[line];
258
  if (end > 0 && this.source.charAt(end - 1) == '\r') end--;
259
  var column = position - start;
260

    
261
  // Adjust according to the offset within the resource.
262
  if (include_resource_offset) {
263
    line += this.line_offset;
264
    if (line == this.line_offset) {
265
      column += this.column_offset;
266
    }
267
  }
268

    
269
  return new SourceLocation(this, position, line, column, start, end);
270
};
271

    
272

    
273
/**
274
 * Get information on a specific source line and column possibly offset by a
275
 * fixed source position. This function is used to find a source position from
276
 * a line and column position. The fixed source position offset is typically
277
 * used to find a source position in a function based on a line and column in
278
 * the source for the function alone. The offset passed will then be the
279
 * start position of the source for the function within the full script source.
280
 * @param {number} opt_line The line within the source. Default value is 0
281
 * @param {number} opt_column The column in within the line. Default value is 0
282
 * @param {number} opt_offset_position The offset from the begining of the
283
 *     source from where the line and column calculation starts. Default value is 0
284
 * @return {SourceLocation}
285
 *     If line is negative or not in the source null is returned.
286
 */
287
Script.prototype.locationFromLine = function (opt_line, opt_column, opt_offset_position) {
288
  // Default is the first line in the script. Lines in the script is relative
289
  // to the offset within the resource.
290
  var line = 0;
291
  if (!IS_UNDEFINED(opt_line)) {
292
    line = opt_line - this.line_offset;
293
  }
294

    
295
  // Default is first column. If on the first line add the offset within the
296
  // resource.
297
  var column = opt_column || 0;
298
  if (line == 0) {
299
    column -= this.column_offset
300
  }
301

    
302
  var offset_position = opt_offset_position || 0;
303
  if (line < 0 || column < 0 || offset_position < 0) return null;
304
  if (line == 0) {
305
    return this.locationFromPosition(offset_position + column, false);
306
  } else {
307
    // Find the line where the offset position is located
308
    var lineCount = this.lineCount();
309
    var offset_line;
310
    for (var i = 0; i < lineCount; i++) {
311
      if (offset_position <= this.line_ends[i]) {
312
        offset_line = i;
313
        break;
314
      }
315
    }
316
    if (offset_line + line >= lineCount) return null;
317
    return this.locationFromPosition(this.line_ends[offset_line + line - 1] + 1 + column);  // line > 0 here.
318
  }
319
}
320

    
321

    
322
/**
323
 * Get a slice of source code from the script. The boundaries for the slice is
324
 * specified in lines.
325
 * @param {number} opt_from_line The first line (zero bound) in the slice.
326
 *     Default is 0
327
 * @param {number} opt_to_column The last line (zero bound) in the slice (non
328
 *     inclusive). Default is the number of lines in the script
329
 * @return {SourceSlice} The source slice or null of the parameters where
330
 *     invalid
331
 */
332
Script.prototype.sourceSlice = function (opt_from_line, opt_to_line) {
333
  var from_line = IS_UNDEFINED(opt_from_line) ? this.line_offset : opt_from_line;
334
  var to_line = IS_UNDEFINED(opt_to_line) ? this.line_offset + this.lineCount() : opt_to_line
335

    
336
  // Adjust according to the offset within the resource.
337
  from_line -= this.line_offset;
338
  to_line -= this.line_offset;
339
  if (from_line < 0) from_line = 0;
340
  if (to_line > this.lineCount()) to_line = this.lineCount();
341

    
342
  // Check parameters.
343
  if (from_line >= this.lineCount() ||
344
      to_line < 0 ||
345
      from_line > to_line) {
346
    return null;
347
  }
348

    
349
  var from_position = from_line == 0 ? 0 : this.line_ends[from_line - 1] + 1;
350
  var to_position = to_line == 0 ? 0 : this.line_ends[to_line - 1] + 1;
351

    
352
  // Return a source slice with line numbers re-adjusted to the resource.
353
  return new SourceSlice(this, from_line + this.line_offset, to_line + this.line_offset,
354
                         from_position, to_position);
355
}
356

    
357

    
358
Script.prototype.sourceLine = function (opt_line) {
359
  // Default is the first line in the script. Lines in the script are relative
360
  // to the offset within the resource.
361
  var line = 0;
362
  if (!IS_UNDEFINED(opt_line)) {
363
    line = opt_line - this.line_offset;
364
  }
365

    
366
  // Check parameter.
367
  if (line < 0 || this.lineCount() <= line) {
368
    return null;
369
  }
370

    
371
  // Return the source line.
372
  var start = line == 0 ? 0 : this.line_ends[line - 1] + 1;
373
  var end = this.line_ends[line];
374
  return this.source.substring(start, end);
375
}
376

    
377

    
378
/**
379
 * Returns the number of source lines.
380
 * @return {number}
381
 *     Number of source lines.
382
 */
383
Script.prototype.lineCount = function() {
384
  // Return number of source lines.
385
  return this.line_ends.length;
386
};
387

    
388

    
389
/**
390
 * Class for source location. A source location is a position within some
391
 * source with the following properties:
392
 *   script   : script object for the source
393
 *   line     : source line number
394
 *   column   : source column within the line
395
 *   position : position within the source
396
 *   start    : position of start of source context (inclusive)
397
 *   end      : position of end of source context (not inclusive)
398
 * Source text for the source context is the character interval [start, end[. In
399
 * most cases end will point to a newline character. It might point just past
400
 * the final position of the source if the last source line does not end with a
401
 * newline character.
402
 * @param {Script} script The Script object for which this is a location
403
 * @param {number} position Source position for the location
404
 * @param {number} line The line number for the location
405
 * @param {number} column The column within the line for the location
406
 * @param {number} start Source position for start of source context
407
 * @param {number} end Source position for end of source context
408
 * @constructor
409
 */
410
function SourceLocation(script, position, line, column, start, end) {
411
  this.script = script;
412
  this.position = position;
413
  this.line = line;
414
  this.column = column;
415
  this.start = start;
416
  this.end = end;
417
}
418

    
419

    
420
const kLineLengthLimit = 78;
421

    
422
/**
423
 * Restrict source location start and end positions to make the source slice
424
 * no more that a certain number of characters wide.
425
 * @param {number} opt_limit The with limit of the source text with a default
426
 *     of 78
427
 * @param {number} opt_before The number of characters to prefer before the
428
 *     position with a default value of 10 less that the limit
429
 */
430
SourceLocation.prototype.restrict = function (opt_limit, opt_before) {
431
  // Find the actual limit to use.
432
  var limit;
433
  var before;
434
  if (!IS_UNDEFINED(opt_limit)) {
435
    limit = opt_limit;
436
  } else {
437
    limit = kLineLengthLimit;
438
  }
439
  if (!IS_UNDEFINED(opt_before)) {
440
    before = opt_before;
441
  } else {
442
    // If no before is specified center for small limits and perfer more source
443
    // before the the position that after for longer limits.
444
    if (limit <= 20) {
445
      before = $floor(limit / 2);
446
    } else {
447
      before = limit - 10;
448
    }
449
  }
450
  if (before >= limit) {
451
    before = limit - 1;
452
  }
453

    
454
  // If the [start, end[ interval is too big we restrict
455
  // it in one or both ends. We make sure to always produce
456
  // restricted intervals of maximum allowed size.
457
  if (this.end - this.start > limit) {
458
    var start_limit = this.position - before;
459
    var end_limit = this.position + limit - before;
460
    if (this.start < start_limit && end_limit < this.end) {
461
      this.start = start_limit;
462
      this.end = end_limit;
463
    } else if (this.start < start_limit) {
464
      this.start = this.end - limit;
465
    } else {
466
      this.end = this.start + limit;
467
    }
468
  }
469
};
470

    
471

    
472
/**
473
 * Get the source text for a SourceLocation
474
 * @return {String}
475
 *     Source text for this location.
476
 */
477
SourceLocation.prototype.sourceText = function () {
478
  return this.script.source.substring(this.start, this.end);
479
};
480

    
481

    
482
/**
483
 * Class for a source slice. A source slice is a part of a script source with
484
 * the following properties:
485
 *   script        : script object for the source
486
 *   from_line     : line number for the first line in the slice
487
 *   to_line       : source line number for the last line in the slice
488
 *   from_position : position of the first character in the slice
489
 *   to_position   : position of the last character in the slice
490
 * The to_line and to_position are not included in the slice, that is the lines
491
 * in the slice are [from_line, to_line[. Likewise the characters in the slice
492
 * are [from_position, to_position[.
493
 * @param {Script} script The Script object for the source slice
494
 * @param {number} from_line
495
 * @param {number} to_line
496
 * @param {number} from_position
497
 * @param {number} to_position
498
 * @constructor
499
 */
500
function SourceSlice(script, from_line, to_line, from_position, to_position) {
501
  this.script = script;
502
  this.from_line = from_line;
503
  this.to_line = to_line;
504
  this.from_position = from_position;
505
  this.to_position = to_position;
506
}
507

    
508

    
509
/**
510
 * Get the source text for a SourceSlice
511
 * @return {String} Source text for this slice. The last line will include
512
 *     the line terminating characters (if any)
513
 */
514
SourceSlice.prototype.sourceText = function () {
515
  return this.script.source.substring(this.from_position, this.to_position);
516
};
517

    
518

    
519
// Returns the offset of the given position within the containing
520
// line.
521
function GetPositionInLine(message) {
522
  var location = message.script.locationFromPosition(message.startPos, false);
523
  if (location == null) return -1;
524
  location.restrict();
525
  return message.startPos - location.start;
526
}
527

    
528

    
529
function ErrorMessage(type, args, startPos, endPos, script, stackTrace) {
530
  this.startPos = startPos;
531
  this.endPos = endPos;
532
  this.type = type;
533
  this.args = args;
534
  this.script = script;
535
  this.stackTrace = stackTrace;
536
}
537

    
538

    
539
function MakeMessage(type, args, startPos, endPos, script, stackTrace) {
540
  return new ErrorMessage(type, args, startPos, endPos, script, stackTrace);
541
}
542

    
543

    
544
function GetStackTraceLine(recv, fun, pos, isGlobal) {
545
  try {
546
    return UnsafeGetStackTraceLine(recv, fun, pos, isGlobal);
547
  } catch (e) {
548
    return "<error: " + e + ">";
549
  }
550
}
551

    
552

    
553
function GetFunctionName(fun, recv) {
554
  var name = %FunctionGetName(fun);
555
  if (name) return name;
556
  for (var prop in recv) {
557
    if (recv[prop] === fun)
558
      return prop;
559
  }
560
  return "[anonymous]";
561
}
562

    
563

    
564
function UnsafeGetStackTraceLine(recv, fun, pos, isTopLevel) {
565
  var result = "";
566
  // The global frame has no meaningful function or receiver
567
  if (!isTopLevel) {
568
    // If the receiver is not the global object then prefix the
569
    // message send
570
    if (recv !== global)
571
      result += ToDetailString(recv) + ".";
572
    result += GetFunctionName(fun, recv);
573
  }
574
  if (pos != -1) {
575
    var script = %FunctionGetScript(fun);
576
    var file;
577
    if (script) {
578
      file = %FunctionGetScript(fun).data;
579
    }
580
    if (file) {
581
      var location = %FunctionGetScript(fun).locationFromPosition(pos, true);
582
      if (!isTopLevel) result += "(";
583
      result += file;
584
      if (location != null) {
585
        result += ":" + (location.line + 1) + ":" + (location.column + 1);
586
      }
587
      if (!isTopLevel) result += ")";
588
    }
589
  }
590
  return (result) ? "    at " + result : result;
591
}
592

    
593

    
594
// ----------------------------------------------------------------------------
595
// Error implementation
596

    
597
// If this object gets passed to an error constructor the error will
598
// get an accessor for .message that constructs a descriptive error
599
// message on access.
600
var kAddMessageAccessorsMarker = { };
601

    
602
// Defines accessors for a property that is calculated the first time
603
// the property is read and then replaces the accessor with the value.
604
// Also, setting the property causes the accessors to be deleted.
605
function DefineOneShotAccessor(obj, name, fun) {
606
  // Note that the accessors consistently operate on 'obj', not 'this'.
607
  // Since the object may occur in someone else's prototype chain we
608
  // can't rely on 'this' being the same as 'obj'.
609
  obj.__defineGetter__(name, function () {
610
    var value = fun(obj);
611
    obj[name] = value;
612
    return value;
613
  });
614
  obj.__defineSetter__(name, function (v) {
615
    delete obj[name];
616
    obj[name] = v;
617
  });
618
}
619

    
620
function DefineError(f) {
621
  // Store the error function in both the global object
622
  // and the runtime object. The function is fetched
623
  // from the runtime object when throwing errors from
624
  // within the runtime system to avoid strange side
625
  // effects when overwriting the error functions from
626
  // user code.
627
  var name = f.name;
628
  %SetProperty(global, name, f, DONT_ENUM);
629
  this['$' + name] = f;
630
  // Configure the error function.
631
  if (name == 'Error') {
632
    // The prototype of the Error object must itself be an error.
633
    // However, it can't be an instance of the Error object because
634
    // it hasn't been properly configured yet.  Instead we create a
635
    // special not-a-true-error-but-close-enough object.
636
    function ErrorPrototype() {}
637
    %FunctionSetPrototype(ErrorPrototype, $Object.prototype);
638
    %FunctionSetInstanceClassName(ErrorPrototype, 'Error');
639
    %FunctionSetPrototype(f, new ErrorPrototype());
640
  } else {
641
    %FunctionSetPrototype(f, new $Error());
642
  }
643
  %FunctionSetInstanceClassName(f, 'Error');
644
  %SetProperty(f.prototype, 'constructor', f, DONT_ENUM);
645
  f.prototype.name = name;
646
  %SetCode(f, function(m) {
647
    if (%IsConstructCall()) {
648
      if (m === kAddMessageAccessorsMarker) {
649
        DefineOneShotAccessor(this, 'message', function (obj) {
650
          return FormatMessage({type: obj.type, args: obj.arguments});
651
        });
652
      } else if (!IS_UNDEFINED(m)) {
653
        this.message = ToString(m);
654
      }
655
    } else {
656
      return new f(m);
657
    }
658
  });
659
}
660

    
661
$Math.__proto__ = global.Object.prototype;
662

    
663
DefineError(function Error() { });
664
DefineError(function TypeError() { });
665
DefineError(function RangeError() { });
666
DefineError(function SyntaxError() { });
667
DefineError(function ReferenceError() { });
668
DefineError(function EvalError() { });
669
DefineError(function URIError() { });
670

    
671
// Setup extra properties of the Error.prototype object.
672
$Error.prototype.message = '';
673

    
674
%SetProperty($Error.prototype, 'toString', function toString() {
675
  var type = this.type;
676
  if (type && !this.hasOwnProperty("message")) {
677
    return this.name + ": " + FormatMessage({ type: type, args: this.arguments });
678
  }
679
  var message = this.message;
680
  return this.name + (message ? (": " + message) : "");
681
}, DONT_ENUM);
682

    
683

    
684
// Boilerplate for exceptions for stack overflows. Used from
685
// Top::StackOverflow().
686
const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []);