messages.js 47.7 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6

// -------------------------------------------------------------------

7 8 9 10
var kMessages = {
  // Error
  cyclic_proto:                  ["Cyclic __proto__ value"],
  code_gen_from_strings:         ["%0"],
11 12
  generator_running:             ["Generator is already running"],
  generator_finished:            ["Generator has already finished"],
13 14 15 16 17 18 19 20 21 22 23 24 25 26
  // TypeError
  unexpected_token:              ["Unexpected token ", "%0"],
  unexpected_token_number:       ["Unexpected number"],
  unexpected_token_string:       ["Unexpected string"],
  unexpected_token_identifier:   ["Unexpected identifier"],
  unexpected_reserved:           ["Unexpected reserved word"],
  unexpected_strict_reserved:    ["Unexpected strict mode reserved word"],
  unexpected_eos:                ["Unexpected end of input"],
  malformed_regexp:              ["Invalid regular expression: /", "%0", "/: ", "%1"],
  unterminated_regexp:           ["Invalid regular expression: missing /"],
  regexp_flags:                  ["Cannot supply flags when constructing one RegExp from another"],
  incompatible_method_receiver:  ["Method ", "%0", " called on incompatible receiver ", "%1"],
  multiple_defaults_in_switch:   ["More than one default clause in switch statement"],
  newline_after_throw:           ["Illegal newline after throw"],
27 28
  label_redeclaration:           ["Label '", "%0", "' has already been declared"],
  var_redeclaration:             ["Identifier '", "%0", "' has already been declared"],
29
  duplicate_template_property:   ["Object template has duplicate property '", "%0", "'"],
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  no_catch_or_finally:           ["Missing catch or finally after try"],
  unknown_label:                 ["Undefined label '", "%0", "'"],
  uncaught_exception:            ["Uncaught ", "%0"],
  stack_trace:                   ["Stack Trace:\n", "%0"],
  called_non_callable:           ["%0", " is not a function"],
  undefined_method:              ["Object ", "%1", " has no method '", "%0", "'"],
  property_not_function:         ["Property '", "%0", "' of object ", "%1", " is not a function"],
  cannot_convert_to_primitive:   ["Cannot convert object to primitive value"],
  not_constructor:               ["%0", " is not a constructor"],
  not_defined:                   ["%0", " is not defined"],
  non_object_property_load:      ["Cannot read property '", "%0", "' of ", "%1"],
  non_object_property_store:     ["Cannot set property '", "%0", "' of ", "%1"],
  with_expression:               ["%0", " has no properties"],
  illegal_invocation:            ["Illegal invocation"],
  no_setter_in_callback:         ["Cannot set property ", "%0", " of ", "%1", " which has only a getter"],
  apply_non_function:            ["Function.prototype.apply was called on ", "%0", ", which is a ", "%1", " and not a function"],
  apply_wrong_args:              ["Function.prototype.apply: Arguments list has wrong type"],
  invalid_in_operator_use:       ["Cannot use 'in' operator to search for '", "%0", "' in ", "%1"],
  instanceof_function_expected:  ["Expecting a function in instanceof check, but got ", "%0"],
  instanceof_nonobject_proto:    ["Function has non-object prototype '", "%0", "' in instanceof check"],
50
  undefined_or_null_to_object:   ["Cannot convert undefined or null to object"],
51 52 53 54
  reduce_no_initial:             ["Reduce of empty array with no initial value"],
  getter_must_be_callable:       ["Getter must be a function: ", "%0"],
  setter_must_be_callable:       ["Setter must be a function: ", "%0"],
  value_and_accessor:            ["Invalid property.  A property cannot both have accessors and be writable or have a value, ", "%0"],
55
  proto_object_or_null:          ["Object prototype may only be an Object or null: ", "%0"],
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  property_desc_object:          ["Property description must be an object: ", "%0"],
  redefine_disallowed:           ["Cannot redefine property: ", "%0"],
  define_disallowed:             ["Cannot define property:", "%0", ", object is not extensible."],
  non_extensible_proto:          ["%0", " is not extensible"],
  handler_non_object:            ["Proxy.", "%0", " called with non-object as handler"],
  proto_non_object:              ["Proxy.", "%0", " called with non-object as prototype"],
  trap_function_expected:        ["Proxy.", "%0", " called with non-function for '", "%1", "' trap"],
  handler_trap_missing:          ["Proxy handler ", "%0", " has no '", "%1", "' trap"],
  handler_trap_must_be_callable: ["Proxy handler ", "%0", " has non-callable '", "%1", "' trap"],
  handler_returned_false:        ["Proxy handler ", "%0", " returned false from '", "%1", "' trap"],
  handler_returned_undefined:    ["Proxy handler ", "%0", " returned undefined from '", "%1", "' trap"],
  proxy_prop_not_configurable:   ["Proxy handler ", "%0", " returned non-configurable descriptor for property '", "%2", "' from '", "%1", "' trap"],
  proxy_non_object_prop_names:   ["Trap '", "%1", "' returned non-object ", "%0"],
  proxy_repeated_prop_name:      ["Trap '", "%1", "' returned repeated property name '", "%2", "'"],
  invalid_weakmap_key:           ["Invalid value used as weak map key"],
71
  invalid_weakset_value:         ["Invalid value used in weak set"],
72 73 74 75
  not_date_object:               ["this is not a Date object."],
  observe_non_object:            ["Object.", "%0", " cannot ", "%0", " non-object"],
  observe_non_function:          ["Object.", "%0", " cannot deliver to non-function"],
  observe_callback_frozen:       ["Object.observe cannot deliver to a frozen function object"],
76
  observe_invalid_accept:        ["Object.observe accept must be an array of strings."],
77
  observe_type_non_string:       ["Invalid changeRecord with non-string 'type' property"],
78 79
  observe_perform_non_string:    ["Invalid non-string changeType"],
  observe_perform_non_function:  ["Cannot perform non-function"],
80
  observe_notify_non_notifier:   ["notify called on non-notifier object"],
81
  observe_global_proxy:          ["%0", " cannot be called on the global proxy object"],
82
  not_typed_array:               ["this is not a typed array."],
83
  invalid_argument:              ["invalid_argument"],
84
  data_view_not_array_buffer:    ["First argument to DataView constructor must be an ArrayBuffer"],
85
  constructor_not_function:      ["Constructor ", "%0", " requires 'new'"],
86
  not_a_symbol:                  ["%0", " is not a symbol"],
87 88 89
  not_a_promise:                 ["%0", " is not a promise"],
  resolver_not_a_function:       ["Promise resolver ", "%0", " is not a function"],
  promise_cyclic:                ["Chaining cycle detected for promise ", "%0"],
90 91
  array_functions_on_frozen:     ["Cannot modify frozen array elements"],
  array_functions_change_sealed: ["Cannot add/remove sealed array elements"],
92
  first_argument_not_regexp:     ["First argument to ", "%0", " must not be a regular expression"],
93 94 95 96
  not_iterable:                  ["%0", " is not iterable"],
  not_an_iterator:               ["%0", " is not an iterator"],
  iterator_result_not_an_object: ["Iterator result ", "%0", " is not an object"],
  iterator_value_not_an_object:  ["Iterator value ", "%0", " is not an entry object"],
97 98
  // RangeError
  invalid_array_length:          ["Invalid array length"],
99
  invalid_array_buffer_length:   ["Invalid array buffer length"],
100
  invalid_string_length:         ["Invalid string length"],
101 102
  invalid_typed_array_offset:    ["Start offset is too large:"],
  invalid_typed_array_length:    ["Invalid typed array length"],
103
  invalid_typed_array_alignment: ["%0", " of ", "%1", " should be a multiple of ", "%2"],
104 105
  typed_array_set_source_too_large:
                                 ["Source is too large"],
106 107
  typed_array_set_negative_offset:
                                 ["Start offset is negative"],
108
  invalid_data_view_offset:      ["Start offset is outside the bounds of the buffer"],
109
  invalid_data_view_length:      ["Invalid data view length"],
110 111 112
  invalid_data_view_accessor_offset:
                                 ["Offset is outside the bounds of the DataView"],

113 114
  stack_overflow:                ["Maximum call stack size exceeded"],
  invalid_time_value:            ["Invalid time value"],
115
  invalid_count_value:           ["Invalid count value"],
116
  invalid_code_point:            ["Invalid code point ", "%0"],
117 118 119 120 121
  // ReferenceError
  invalid_lhs_in_assignment:     ["Invalid left-hand side in assignment"],
  invalid_lhs_in_for:            ["Invalid left-hand side in for-loop"],
  invalid_lhs_in_postfix_op:     ["Invalid left-hand side expression in postfix operation"],
  invalid_lhs_in_prefix_op:      ["Invalid left-hand side expression in prefix operation"],
122
  // SyntaxError
123 124 125
  paren_in_arg_string:           ["Function arg string contains parenthesis"],
  not_isvar:                     ["builtin %IS_VAR: not a variable"],
  single_function_literal:       ["Single function literal required"],
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  invalid_regexp_flags:          ["Invalid flags supplied to RegExp constructor '", "%0", "'"],
  invalid_regexp:                ["Invalid RegExp pattern /", "%0", "/"],
  illegal_break:                 ["Illegal break statement"],
  illegal_continue:              ["Illegal continue statement"],
  illegal_return:                ["Illegal return statement"],
  error_loading_debugger:        ["Error loading debugger"],
  no_input_to_regexp:            ["No input to ", "%0"],
  invalid_json:                  ["String '", "%0", "' is not valid JSON"],
  circular_structure:            ["Converting circular structure to JSON"],
  called_on_non_object:          ["%0", " called on non-object"],
  called_on_null_or_undefined:   ["%0", " called on null or undefined"],
  array_indexof_not_defined:     ["Array.getIndexOf: Argument undefined"],
  object_not_extensible:         ["Can't add property ", "%0", ", object is not extensible"],
  illegal_access:                ["Illegal access"],
  strict_mode_with:              ["Strict mode code may not include a with statement"],
141
  strict_eval_arguments:         ["Unexpected eval or arguments in strict mode"],
142 143
  too_many_arguments:            ["Too many arguments in function call (only 65535 allowed)"],
  too_many_parameters:           ["Too many parameters in function definition (only 65535 allowed)"],
144
  too_many_variables:            ["Too many variables declared (only 4194303 allowed)"],
145 146 147 148 149 150 151 152 153 154 155 156 157
  strict_param_dupe:             ["Strict mode function may not have duplicate parameter names"],
  strict_octal_literal:          ["Octal literals are not allowed in strict mode."],
  strict_duplicate_property:     ["Duplicate data property in object literal not allowed in strict mode"],
  accessor_data_property:        ["Object literal may not have data and accessor property with the same name"],
  accessor_get_set:              ["Object literal may not have multiple get/set accessors with the same name"],
  strict_delete:                 ["Delete of an unqualified identifier in strict mode."],
  strict_delete_property:        ["Cannot delete property '", "%0", "' of ", "%1"],
  strict_const:                  ["Use of const in strict mode."],
  strict_function:               ["In strict mode code, functions can only be declared at top level or immediately within another function." ],
  strict_read_only_property:     ["Cannot assign to read only property '", "%0", "' of ", "%1"],
  strict_cannot_assign:          ["Cannot assign to read only '", "%0", "' in strict mode"],
  strict_poison_pill:            ["'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them"],
  strict_caller:                 ["Illegal access to a strict mode caller function."],
158
  malformed_arrow_function_parameter_list: ["Malformed arrow function parameter list"],
159
  generator_poison_pill:         ["'caller' and 'arguments' properties may not be accessed on generator functions."],
160 161 162 163 164
  unprotected_let:               ["Illegal let declaration in unprotected statement context."],
  unprotected_const:             ["Illegal const declaration in unprotected statement context."],
  cant_prevent_ext_external_array_elements: ["Cannot prevent extension of an object with external array elements"],
  redef_external_array_element:  ["Cannot redefine a property of an object with external array elements"],
  harmony_const_assign:          ["Assignment to constant variable."],
165 166
  symbol_to_string:              ["Cannot convert a Symbol value to a string"],
  symbol_to_primitive:           ["Cannot convert a Symbol wrapper object to a primitive value"],
167
  symbol_to_number:              ["Cannot convert a Symbol value to a number"],
168 169
  invalid_module_path:           ["Module does not export '", "%0", "', or export is not itself a module"],
  module_type_error:             ["Module '", "%0", "' used improperly"],
170 171
  module_export_undefined:       ["Export '", "%0", "' is not defined in module"],
  unexpected_super:              ["'super' keyword unexpected here"]
172 173 174 175
};


function FormatString(format, args) {
176 177 178 179
  var result = "";
  var arg_num = 0;
  for (var i = 0; i < format.length; i++) {
    var str = format[i];
180 181 182 183 184
    if (str.length == 2 && %_StringCharCodeAt(str, 0) == 0x25) {
      // Two-char string starts with "%".
      var arg_num = (%_StringCharCodeAt(str, 1) - 0x30) >>> 0;
      if (arg_num < 4) {
        // str is one of %0, %1, %2 or %3.
185
        try {
186
          str = NoSideEffectToString(args[arg_num]);
187
        } catch (e) {
188 189 190 191 192 193
          if (%IsJSModule(args[arg_num]))
            str = "module";
          else if (IS_SPEC_OBJECT(args[arg_num]))
            str = "object";
          else
            str = "#<error>";
194
        }
195
      }
196
    }
197
    result += str;
198 199
  }
  return result;
200
}
201 202


203 204 205
function NoSideEffectToString(obj) {
  if (IS_STRING(obj)) return obj;
  if (IS_NUMBER(obj)) return %_NumberToString(obj);
206
  if (IS_BOOLEAN(obj)) return obj ? 'true' : 'false';
207 208
  if (IS_UNDEFINED(obj)) return 'undefined';
  if (IS_NULL(obj)) return 'null';
209 210 211 212 213 214 215 216
  if (IS_FUNCTION(obj)) {
    var str = %_CallFunction(obj, FunctionToString);
    if (str.length > 128) {
      str = %_SubString(str, 0, 111) + "...<omitted>..." +
            %_SubString(str, str.length - 2, str.length);
    }
    return str;
  }
217
  if (IS_SYMBOL(obj)) return %_CallFunction(obj, SymbolToString);
218
  if (IS_OBJECT(obj) && %GetDataProperty(obj, "toString") === ObjectToString) {
219
    var constructor = %GetDataProperty(obj, "constructor");
220 221 222 223 224 225 226
    if (typeof constructor == "function") {
      var constructorName = constructor.name;
      if (IS_STRING(constructorName) && constructorName !== "") {
        return "#<" + constructorName + ">";
      }
    }
  }
227 228 229
  if (CanBeSafelyTreatedAsAnErrorObject(obj)) {
    return %_CallFunction(obj, ErrorToString);
  }
230 231 232
  return %_CallFunction(obj, ObjectToString);
}

233 234 235 236 237 238
// To determine whether we can safely stringify an object using ErrorToString
// without the risk of side-effects, we need to check whether the object is
// either an instance of a native error type (via '%_ClassOf'), or has $Error
// in its prototype chain and hasn't overwritten 'toString' with something
// strange and unusual.
function CanBeSafelyTreatedAsAnErrorObject(obj) {
239 240 241 242 243 244 245 246 247 248
  switch (%_ClassOf(obj)) {
    case 'Error':
    case 'EvalError':
    case 'RangeError':
    case 'ReferenceError':
    case 'SyntaxError':
    case 'TypeError':
    case 'URIError':
      return true;
  }
249 250 251

  var objToString = %GetDataProperty(obj, "toString");
  return obj instanceof $Error && objToString === ErrorToString;
252 253 254
}


255 256 257 258 259
// When formatting internally created error messages, do not
// invoke overwritten error toString methods but explicitly use
// the error to string method. This is to avoid leaking error
// objects between script tags in a browser setting.
function ToStringCheckErrorObject(obj) {
260
  if (CanBeSafelyTreatedAsAnErrorObject(obj)) {
261
    return %_CallFunction(obj, ErrorToString);
262 263 264 265 266 267
  } else {
    return ToString(obj);
  }
}


268
function ToDetailString(obj) {
269
  if (obj != null && IS_OBJECT(obj) && obj.toString === ObjectToString) {
270
    var constructor = obj.constructor;
271 272 273 274 275
    if (typeof constructor == "function") {
      var constructorName = constructor.name;
      if (IS_STRING(constructorName) && constructorName !== "") {
        return "#<" + constructorName + ">";
      }
276
    }
277
  }
278
  return ToStringCheckErrorObject(obj);
279
}
280 281 282


function MakeGenericError(constructor, type, args) {
283 284
  if (IS_UNDEFINED(args)) args = [];
  return new constructor(FormatMessage(type, args));
285
}
286 287 288


/**
289
 * Set up the Script function and constructor.
290 291
 */
%FunctionSetInstanceClassName(Script, 'Script');
292 293
%AddNamedProperty(Script.prototype, 'constructor', Script,
                  DONT_ENUM | DONT_DELETE | READ_ONLY);
294 295 296 297 298 299 300
%SetCode(Script, function(x) {
  // Script objects can only be created by the VM.
  throw new $Error("Not supported");
});


// Helper functions; called from the runtime system.
301 302 303 304
function FormatMessage(type, args) {
  var format = kMessages[type];
  if (!format) return "<unknown message " + type + ">";
  return FormatString(format, args);
305
}
306 307 308


function GetLineNumber(message) {
309 310 311 312
  var start_position = %MessageGetStartPosition(message);
  if (start_position == -1) return kNoLineNumberInfo;
  var script = %MessageGetScript(message);
  var location = script.locationFromPosition(start_position, true);
313
  if (location == null) return kNoLineNumberInfo;
314
  return location.line + 1;
315
}
316 317 318 319 320


// Returns the source code line containing the given source
// position, or the empty string if the position is invalid.
function GetSourceLine(message) {
321 322 323
  var script = %MessageGetScript(message);
  var start_position = %MessageGetStartPosition(message);
  var location = script.locationFromPosition(start_position, true);
324 325 326
  if (location == null) return "";
  location.restrict();
  return location.sourceText();
327
}
328 329 330 331


function MakeTypeError(type, args) {
  return MakeGenericError($TypeError, type, args);
332
}
333 334 335 336


function MakeRangeError(type, args) {
  return MakeGenericError($RangeError, type, args);
337
}
338 339 340 341


function MakeSyntaxError(type, args) {
  return MakeGenericError($SyntaxError, type, args);
342
}
343 344 345 346


function MakeReferenceError(type, args) {
  return MakeGenericError($ReferenceError, type, args);
347
}
348 349 350 351


function MakeEvalError(type, args) {
  return MakeGenericError($EvalError, type, args);
352
}
353 354 355 356


function MakeError(type, args) {
  return MakeGenericError($Error, type, args);
357
}
358

359 360 361 362 363 364
/**
 * Find a line number given a specific source position.
 * @param {number} position The source position.
 * @return {number} 0 if input too small, -1 if input too large,
       else the line number.
 */
365
function ScriptLineFromPosition(position) {
366 367
  var lower = 0;
  var upper = this.lineCount() - 1;
368
  var line_ends = this.line_ends;
369 370

  // We'll never find invalid positions so bail right away.
371
  if (position > line_ends[upper]) {
372 373 374 375
    return -1;
  }

  // This means we don't have to safe-guard indexing line_ends[i - 1].
376
  if (position <= line_ends[0]) {
377 378 379 380 381 382 383
    return 0;
  }

  // Binary search to find line # from position range.
  while (upper >= 1) {
    var i = (lower + upper) >> 1;

384
    if (position > line_ends[i]) {
385
      lower = i + 1;
386
    } else if (position <= line_ends[i - 1]) {
387 388 389 390 391
      upper = i - 1;
    } else {
      return i;
    }
  }
392

393 394
  return -1;
}
395 396 397 398

/**
 * Get information on a specific source position.
 * @param {number} position The source position
399 400
 * @param {boolean} include_resource_offset Set to true to have the resource
 *     offset added to the location
401 402 403
 * @return {SourceLocation}
 *     If line is negative or not in the source null is returned.
 */
404 405
function ScriptLocationFromPosition(position,
                                    include_resource_offset) {
406
  var line = this.lineFromPosition(position);
407
  if (line == -1) return null;
408

409
  // Determine start, end and column.
410 411 412
  var line_ends = this.line_ends;
  var start = line == 0 ? 0 : line_ends[line - 1] + 1;
  var end = line_ends[line];
413 414 415
  if (end > 0 && %_CallFunction(this.source, end - 1, StringCharAt) == '\r') {
    end--;
  }
416 417 418
  var column = position - start;

  // Adjust according to the offset within the resource.
419 420 421 422 423
  if (include_resource_offset) {
    line += this.line_offset;
    if (line == this.line_offset) {
      column += this.column_offset;
    }
424 425 426
  }

  return new SourceLocation(this, position, line, column, start, end);
427
}
428 429 430 431 432 433 434 435 436 437 438 439


/**
 * Get information on a specific source line and column possibly offset by a
 * fixed source position. This function is used to find a source position from
 * a line and column position. The fixed source position offset is typically
 * used to find a source position in a function based on a line and column in
 * the source for the function alone. The offset passed will then be the
 * start position of the source for the function within the full script source.
 * @param {number} opt_line The line within the source. Default value is 0
 * @param {number} opt_column The column in within the line. Default value is 0
 * @param {number} opt_offset_position The offset from the begining of the
440 441
 *     source from where the line and column calculation starts.
 *     Default value is 0
442 443 444
 * @return {SourceLocation}
 *     If line is negative or not in the source null is returned.
 */
445
function ScriptLocationFromLine(opt_line, opt_column, opt_offset_position) {
446 447 448 449 450 451
  // Default is the first line in the script. Lines in the script is relative
  // to the offset within the resource.
  var line = 0;
  if (!IS_UNDEFINED(opt_line)) {
    line = opt_line - this.line_offset;
  }
452

453 454 455 456
  // Default is first column. If on the first line add the offset within the
  // resource.
  var column = opt_column || 0;
  if (line == 0) {
457
    column -= this.column_offset;
458 459 460 461 462
  }

  var offset_position = opt_offset_position || 0;
  if (line < 0 || column < 0 || offset_position < 0) return null;
  if (line == 0) {
463
    return this.locationFromPosition(offset_position + column, false);
464
  } else {
465 466 467 468 469
    // Find the line where the offset position is located.
    var offset_line = this.lineFromPosition(offset_position);

    if (offset_line == -1 || offset_line + line >= this.lineCount()) {
      return null;
470
    }
471

472 473
    return this.locationFromPosition(
        this.line_ends[offset_line + line - 1] + 1 + column);  // line > 0 here.
474 475 476 477 478 479 480 481 482 483 484 485 486 487
  }
}


/**
 * Get a slice of source code from the script. The boundaries for the slice is
 * specified in lines.
 * @param {number} opt_from_line The first line (zero bound) in the slice.
 *     Default is 0
 * @param {number} opt_to_column The last line (zero bound) in the slice (non
 *     inclusive). Default is the number of lines in the script
 * @return {SourceSlice} The source slice or null of the parameters where
 *     invalid
 */
488
function ScriptSourceSlice(opt_from_line, opt_to_line) {
489 490 491 492
  var from_line = IS_UNDEFINED(opt_from_line) ? this.line_offset
                                              : opt_from_line;
  var to_line = IS_UNDEFINED(opt_to_line) ? this.line_offset + this.lineCount()
                                          : opt_to_line;
493 494 495 496 497 498 499

  // Adjust according to the offset within the resource.
  from_line -= this.line_offset;
  to_line -= this.line_offset;
  if (from_line < 0) from_line = 0;
  if (to_line > this.lineCount()) to_line = this.lineCount();

500
  // Check parameters.
501 502 503 504 505 506
  if (from_line >= this.lineCount() ||
      to_line < 0 ||
      from_line > to_line) {
    return null;
  }

507 508 509
  var line_ends = this.line_ends;
  var from_position = from_line == 0 ? 0 : line_ends[from_line - 1] + 1;
  var to_position = to_line == 0 ? 0 : line_ends[to_line - 1] + 1;
510 511

  // Return a source slice with line numbers re-adjusted to the resource.
512 513 514 515
  return new SourceSlice(this,
                         from_line + this.line_offset,
                         to_line + this.line_offset,
                          from_position, to_position);
516 517 518
}


519
function ScriptSourceLine(opt_line) {
520 521 522 523 524 525
  // Default is the first line in the script. Lines in the script are relative
  // to the offset within the resource.
  var line = 0;
  if (!IS_UNDEFINED(opt_line)) {
    line = opt_line - this.line_offset;
  }
526 527

  // Check parameter.
528 529 530 531
  if (line < 0 || this.lineCount() <= line) {
    return null;
  }

532
  // Return the source line.
533 534 535
  var line_ends = this.line_ends;
  var start = line == 0 ? 0 : line_ends[line - 1] + 1;
  var end = line_ends[line];
536
  return %_CallFunction(this.source, start, end, StringSubstring);
537 538 539 540 541 542 543 544
}


/**
 * Returns the number of source lines.
 * @return {number}
 *     Number of source lines.
 */
545
function ScriptLineCount() {
546 547
  // Return number of source lines.
  return this.line_ends.length;
548
}
549 550


551
/**
552 553
 * If sourceURL comment is available and script starts at zero returns sourceURL
 * comment contents. Otherwise, script name is returned. See
554
 * http://fbug.googlecode.com/svn/branches/firebug1.1/docs/ReleaseNotes_1.1.txt
555 556
 * and Source Map Revision 3 proposal for details on using //# sourceURL and
 * deprecated //@ sourceURL comment to identify scripts that don't have name.
557
 *
558 559
 * @return {?string} script name if present, value for //# sourceURL or
 * deprecated //@ sourceURL comment otherwise.
560
 */
561
function ScriptNameOrSourceURL() {
562
  if (this.line_offset > 0 || this.column_offset > 0) {
563
    return this.name;
564
  }
565 566
  if (this.source_url) {
    return this.source_url;
567
  }
568
  return this.name;
569 570 571
}


572
SetUpLockedPrototype(Script,
573 574
  $Array("source", "name", "source_url", "source_mapping_url", "line_ends",
         "line_offset", "column_offset"),
575 576 577 578 579 580 581 582 583 584 585 586
  $Array(
    "lineFromPosition", ScriptLineFromPosition,
    "locationFromPosition", ScriptLocationFromPosition,
    "locationFromLine", ScriptLocationFromLine,
    "sourceSlice", ScriptSourceSlice,
    "sourceLine", ScriptSourceLine,
    "lineCount", ScriptLineCount,
    "nameOrSourceURL", ScriptNameOrSourceURL
  )
);


587 588 589 590 591 592 593 594 595
/**
 * Class for source location. A source location is a position within some
 * source with the following properties:
 *   script   : script object for the source
 *   line     : source line number
 *   column   : source column within the line
 *   position : position within the source
 *   start    : position of start of source context (inclusive)
 *   end      : position of end of source context (not inclusive)
596 597 598 599
 * Source text for the source context is the character interval
 * [start, end[. In most cases end will point to a newline character.
 * It might point just past the final position of the source if the last
 * source line does not end with a newline character.
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
 * @param {Script} script The Script object for which this is a location
 * @param {number} position Source position for the location
 * @param {number} line The line number for the location
 * @param {number} column The column within the line for the location
 * @param {number} start Source position for start of source context
 * @param {number} end Source position for end of source context
 * @constructor
 */
function SourceLocation(script, position, line, column, start, end) {
  this.script = script;
  this.position = position;
  this.line = line;
  this.column = column;
  this.start = start;
  this.end = end;
615
}
616

617
var kLineLengthLimit = 78;
618 619 620 621 622 623 624 625 626

/**
 * Restrict source location start and end positions to make the source slice
 * no more that a certain number of characters wide.
 * @param {number} opt_limit The with limit of the source text with a default
 *     of 78
 * @param {number} opt_before The number of characters to prefer before the
 *     position with a default value of 10 less that the limit
 */
627
function SourceLocationRestrict(opt_limit, opt_before) {
628 629 630 631 632 633 634 635 636 637 638 639 640 641
  // Find the actual limit to use.
  var limit;
  var before;
  if (!IS_UNDEFINED(opt_limit)) {
    limit = opt_limit;
  } else {
    limit = kLineLengthLimit;
  }
  if (!IS_UNDEFINED(opt_before)) {
    before = opt_before;
  } else {
    // If no before is specified center for small limits and perfer more source
    // before the the position that after for longer limits.
    if (limit <= 20) {
642
      before = $floor(limit / 2);
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
    } else {
      before = limit - 10;
    }
  }
  if (before >= limit) {
    before = limit - 1;
  }

  // If the [start, end[ interval is too big we restrict
  // it in one or both ends. We make sure to always produce
  // restricted intervals of maximum allowed size.
  if (this.end - this.start > limit) {
    var start_limit = this.position - before;
    var end_limit = this.position + limit - before;
    if (this.start < start_limit && end_limit < this.end) {
      this.start = start_limit;
      this.end = end_limit;
    } else if (this.start < start_limit) {
      this.start = this.end - limit;
    } else {
      this.end = this.start + limit;
    }
  }
666
}
667 668 669 670 671 672 673


/**
 * Get the source text for a SourceLocation
 * @return {String}
 *     Source text for this location.
 */
674
function SourceLocationSourceText() {
675 676 677 678 679
  return %_CallFunction(this.script.source,
                        this.start,
                        this.end,
                        StringSubstring);
}
680 681


682 683 684 685 686
SetUpLockedPrototype(SourceLocation,
  $Array("script", "position", "line", "column", "start", "end"),
  $Array(
    "restrict", SourceLocationRestrict,
    "sourceText", SourceLocationSourceText
687
 )
688 689 690
);


691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
/**
 * Class for a source slice. A source slice is a part of a script source with
 * the following properties:
 *   script        : script object for the source
 *   from_line     : line number for the first line in the slice
 *   to_line       : source line number for the last line in the slice
 *   from_position : position of the first character in the slice
 *   to_position   : position of the last character in the slice
 * The to_line and to_position are not included in the slice, that is the lines
 * in the slice are [from_line, to_line[. Likewise the characters in the slice
 * are [from_position, to_position[.
 * @param {Script} script The Script object for the source slice
 * @param {number} from_line
 * @param {number} to_line
 * @param {number} from_position
 * @param {number} to_position
 * @constructor
 */
function SourceSlice(script, from_line, to_line, from_position, to_position) {
  this.script = script;
  this.from_line = from_line;
  this.to_line = to_line;
  this.from_position = from_position;
  this.to_position = to_position;
}

/**
 * Get the source text for a SourceSlice
 * @return {String} Source text for this slice. The last line will include
 *     the line terminating characters (if any)
 */
722
function SourceSliceSourceText() {
723 724 725 726
  return %_CallFunction(this.script.source,
                        this.from_position,
                        this.to_position,
                        StringSubstring);
727
}
728

729 730 731 732 733
SetUpLockedPrototype(SourceSlice,
  $Array("script", "from_line", "to_line", "from_position", "to_position"),
  $Array("sourceText", SourceSliceSourceText)
);

734 735 736 737

// Returns the offset of the given position within the containing
// line.
function GetPositionInLine(message) {
738 739 740
  var script = %MessageGetScript(message);
  var start_position = %MessageGetStartPosition(message);
  var location = script.locationFromPosition(start_position, false);
741 742
  if (location == null) return -1;
  location.restrict();
743
  return start_position - location.start;
744
}
745 746 747


function GetStackTraceLine(recv, fun, pos, isGlobal) {
748
  return new CallSite(recv, fun, pos, false).toString();
749
}
750 751 752 753

// ----------------------------------------------------------------------------
// Error implementation

754 755 756 757
var CallSiteReceiverKey = NEW_PRIVATE("CallSite#receiver");
var CallSiteFunctionKey = NEW_PRIVATE("CallSite#function");
var CallSitePositionKey = NEW_PRIVATE("CallSite#position");
var CallSiteStrictModeKey = NEW_PRIVATE("CallSite#strict_mode");
758 759

function CallSite(receiver, fun, pos, strict_mode) {
760 761 762 763
  SET_PRIVATE(this, CallSiteReceiverKey, receiver);
  SET_PRIVATE(this, CallSiteFunctionKey, fun);
  SET_PRIVATE(this, CallSitePositionKey, pos);
  SET_PRIVATE(this, CallSiteStrictModeKey, strict_mode);
764 765
}

766
function CallSiteGetThis() {
767 768
  return GET_PRIVATE(this, CallSiteStrictModeKey)
      ? UNDEFINED : GET_PRIVATE(this, CallSiteReceiverKey);
769
}
770

771
function CallSiteGetTypeName() {
772
  return GetTypeName(GET_PRIVATE(this, CallSiteReceiverKey), false);
773
}
774

775
function CallSiteIsToplevel() {
776
  if (GET_PRIVATE(this, CallSiteReceiverKey) == null) {
777
    return true;
778
  }
779
  return IS_GLOBAL(GET_PRIVATE(this, CallSiteReceiverKey));
780
}
781

782
function CallSiteIsEval() {
783
  var script = %FunctionGetScript(GET_PRIVATE(this, CallSiteFunctionKey));
784
  return script && script.compilation_type == COMPILATION_TYPE_EVAL;
785
}
786

787
function CallSiteGetEvalOrigin() {
788
  var script = %FunctionGetScript(GET_PRIVATE(this, CallSiteFunctionKey));
789
  return FormatEvalOrigin(script);
790
}
791

792
function CallSiteGetScriptNameOrSourceURL() {
793
  var script = %FunctionGetScript(GET_PRIVATE(this, CallSiteFunctionKey));
794
  return script ? script.nameOrSourceURL() : null;
795
}
796

797
function CallSiteGetFunction() {
798 799
  return GET_PRIVATE(this, CallSiteStrictModeKey)
      ? UNDEFINED : GET_PRIVATE(this, CallSiteFunctionKey);
800
}
801

802
function CallSiteGetFunctionName() {
803
  // See if the function knows its own name
804
  var name = GET_PRIVATE(this, CallSiteFunctionKey).name;
805
  if (name) {
806
    return name;
807
  }
808
  name = %FunctionGetInferredName(GET_PRIVATE(this, CallSiteFunctionKey));
809 810
  if (name) {
    return name;
811 812
  }
  // Maybe this is an evaluation?
813
  var script = %FunctionGetScript(GET_PRIVATE(this, CallSiteFunctionKey));
814
  if (script && script.compilation_type == COMPILATION_TYPE_EVAL) {
815
    return "eval";
816
  }
817
  return null;
818
}
819

820
function CallSiteGetMethodName() {
821 822
  // See if we can find a unique property on the receiver that holds
  // this function.
823 824
  var receiver = GET_PRIVATE(this, CallSiteReceiverKey);
  var fun = GET_PRIVATE(this, CallSiteFunctionKey);
825 826 827 828 829
  var ownName = fun.name;
  if (ownName && receiver &&
      (%_CallFunction(receiver, ownName, ObjectLookupGetter) === fun ||
       %_CallFunction(receiver, ownName, ObjectLookupSetter) === fun ||
       (IS_OBJECT(receiver) && %GetDataProperty(receiver, ownName) === fun))) {
830 831 832
    // To handle DontEnum properties we guess that the method has
    // the same name as the function.
    return ownName;
833
  }
834
  var name = null;
835 836 837 838
  for (var prop in receiver) {
    if (%_CallFunction(receiver, prop, ObjectLookupGetter) === fun ||
        %_CallFunction(receiver, prop, ObjectLookupSetter) === fun ||
        (IS_OBJECT(receiver) && %GetDataProperty(receiver, prop) === fun)) {
839
      // If we find more than one match bail out to avoid confusion.
840
      if (name) {
841
        return null;
842
      }
843 844 845
      name = prop;
    }
  }
846
  if (name) {
847
    return name;
848
  }
849
  return null;
850
}
851

852
function CallSiteGetFileName() {
853
  var script = %FunctionGetScript(GET_PRIVATE(this, CallSiteFunctionKey));
854
  return script ? script.name : null;
855
}
856

857
function CallSiteGetLineNumber() {
858
  if (GET_PRIVATE(this, CallSitePositionKey) == -1) {
859
    return null;
860
  }
861
  var script = %FunctionGetScript(GET_PRIVATE(this, CallSiteFunctionKey));
862 863
  var location = null;
  if (script) {
864 865
    location = script.locationFromPosition(
        GET_PRIVATE(this, CallSitePositionKey), true);
866 867
  }
  return location ? location.line + 1 : null;
868
}
869

870
function CallSiteGetColumnNumber() {
871
  if (GET_PRIVATE(this, CallSitePositionKey) == -1) {
872
    return null;
873
  }
874
  var script = %FunctionGetScript(GET_PRIVATE(this, CallSiteFunctionKey));
875 876
  var location = null;
  if (script) {
877 878
    location = script.locationFromPosition(
      GET_PRIVATE(this, CallSitePositionKey), true);
879
  }
880
  return location ? location.column + 1: null;
881
}
882

883
function CallSiteIsNative() {
884
  var script = %FunctionGetScript(GET_PRIVATE(this, CallSiteFunctionKey));
885
  return script ? (script.type == TYPE_NATIVE) : false;
886
}
887

888
function CallSiteGetPosition() {
889
  return GET_PRIVATE(this, CallSitePositionKey);
890
}
891

892
function CallSiteIsConstructor() {
893
  var receiver = GET_PRIVATE(this, CallSiteReceiverKey);
894 895
  var constructor = (receiver != null && IS_OBJECT(receiver))
                        ? %GetDataProperty(receiver, "constructor") : null;
896
  if (!constructor) return false;
897
  return GET_PRIVATE(this, CallSiteFunctionKey) === constructor;
898
}
899

900 901 902 903 904 905
function CallSiteToString() {
  var fileName;
  var fileLocation = "";
  if (this.isNative()) {
    fileLocation = "native";
  } else {
906 907 908 909
    fileName = this.getScriptNameOrSourceURL();
    if (!fileName && this.isEval()) {
      fileLocation = this.getEvalOrigin();
      fileLocation += ", ";  // Expecting source position to follow.
910
    }
911

912 913 914 915 916 917 918 919
    if (fileName) {
      fileLocation += fileName;
    } else {
      // Source code does not originate from a file and is not native, but we
      // can still get the source position inside the source string, e.g. in
      // an eval string.
      fileLocation += "<anonymous>";
    }
920 921 922 923 924 925 926 927 928 929 930
    var lineNumber = this.getLineNumber();
    if (lineNumber != null) {
      fileLocation += ":" + lineNumber;
      var columnNumber = this.getColumnNumber();
      if (columnNumber) {
        fileLocation += ":" + columnNumber;
      }
    }
  }

  var line = "";
931 932
  var functionName = this.getFunctionName();
  var addSuffix = true;
933 934 935
  var isConstructor = this.isConstructor();
  var isMethodCall = !(this.isToplevel() || isConstructor);
  if (isMethodCall) {
936
    var typeName = GetTypeName(GET_PRIVATE(this, CallSiteReceiverKey), true);
937 938
    var methodName = this.getMethodName();
    if (functionName) {
939
      if (typeName &&
940
          %_CallFunction(functionName, typeName, StringIndexOfJS) != 0) {
941 942
        line += typeName + ".";
      }
943
      line += functionName;
944
      if (methodName &&
945
          (%_CallFunction(functionName, "." + methodName, StringIndexOfJS) !=
946
           functionName.length - methodName.length - 1)) {
947 948 949
        line += " [as " + methodName + "]";
      }
    } else {
950
      line += typeName + "." + (methodName || "<anonymous>");
951 952 953 954 955 956 957
    }
  } else if (isConstructor) {
    line += "new " + (functionName || "<anonymous>");
  } else if (functionName) {
    line += functionName;
  } else {
    line += fileLocation;
958
    addSuffix = false;
959
  }
960
  if (addSuffix) {
961 962 963 964 965
    line += " (" + fileLocation + ")";
  }
  return line;
}

966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
SetUpLockedPrototype(CallSite, $Array("receiver", "fun", "pos"), $Array(
  "getThis", CallSiteGetThis,
  "getTypeName", CallSiteGetTypeName,
  "isToplevel", CallSiteIsToplevel,
  "isEval", CallSiteIsEval,
  "getEvalOrigin", CallSiteGetEvalOrigin,
  "getScriptNameOrSourceURL", CallSiteGetScriptNameOrSourceURL,
  "getFunction", CallSiteGetFunction,
  "getFunctionName", CallSiteGetFunctionName,
  "getMethodName", CallSiteGetMethodName,
  "getFileName", CallSiteGetFileName,
  "getLineNumber", CallSiteGetLineNumber,
  "getColumnNumber", CallSiteGetColumnNumber,
  "isNative", CallSiteIsNative,
  "getPosition", CallSiteGetPosition,
981 982
  "isConstructor", CallSiteIsConstructor,
  "toString", CallSiteToString
983 984 985
));


986
function FormatEvalOrigin(script) {
987
  var sourceURL = script.nameOrSourceURL();
988
  if (sourceURL) {
989
    return sourceURL;
990
  }
991 992

  var eval_origin = "eval at ";
993 994 995 996 997
  if (script.eval_from_function_name) {
    eval_origin += script.eval_from_function_name;
  } else {
    eval_origin +=  "<anonymous>";
  }
998

999 1000
  var eval_from_script = script.eval_from_script;
  if (eval_from_script) {
1001
    if (eval_from_script.compilation_type == COMPILATION_TYPE_EVAL) {
1002
      // eval script originated from another eval.
1003
      eval_origin += " (" + FormatEvalOrigin(eval_from_script) + ")";
1004
    } else {
1005
      // eval script originated from "real" source.
1006 1007
      if (eval_from_script.name) {
        eval_origin += " (" + eval_from_script.name;
1008 1009
        var location = eval_from_script.locationFromPosition(
            script.eval_from_script_position, true);
1010 1011 1012 1013
        if (location) {
          eval_origin += ":" + (location.line + 1);
          eval_origin += ":" + (location.column + 1);
        }
1014
        eval_origin += ")";
1015 1016 1017 1018 1019
      } else {
        eval_origin += " (unknown source)";
      }
    }
  }
1020

1021
  return eval_origin;
1022
}
1023

1024 1025

function FormatErrorString(error) {
1026
  try {
1027
    return %_CallFunction(error, ErrorToString);
1028 1029
  } catch (e) {
    try {
1030
      return "<error: " + e + ">";
1031
    } catch (ee) {
1032
      return "<error>";
1033 1034
    }
  }
1035 1036 1037 1038
}


function GetStackFrames(raw_stack) {
1039
  var frames = new InternalArray();
1040
  var sloppy_frames = raw_stack[0];
1041
  for (var i = 1; i < raw_stack.length; i += 4) {
1042 1043 1044 1045 1046
    var recv = raw_stack[i];
    var fun = raw_stack[i + 1];
    var code = raw_stack[i + 2];
    var pc = raw_stack[i + 3];
    var pos = %FunctionGetPositionForOffset(code, pc);
1047 1048
    sloppy_frames--;
    frames.push(new CallSite(recv, fun, pos, (sloppy_frames < 0)));
1049 1050 1051 1052 1053
  }
  return frames;
}


1054 1055 1056 1057
// Flag to prevent recursive call of Error.prepareStackTrace.
var formatting_custom_stack_trace = false;


1058 1059
function FormatStackTrace(obj, raw_stack) {
  var frames = GetStackFrames(raw_stack);
1060 1061 1062 1063
  if (IS_FUNCTION($Error.prepareStackTrace) && !formatting_custom_stack_trace) {
    var array = [];
    %MoveArrayContents(frames, array);
    formatting_custom_stack_trace = true;
1064
    var stack_trace = UNDEFINED;
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
    try {
      stack_trace = $Error.prepareStackTrace(obj, array);
    } catch (e) {
      throw e;  // The custom formatting function threw.  Rethrow.
    } finally {
      formatting_custom_stack_trace = false;
    }
    return stack_trace;
  }

1075
  var lines = new InternalArray();
1076
  lines.push(FormatErrorString(obj));
1077 1078
  for (var i = 0; i < frames.length; i++) {
    var frame = frames[i];
1079
    var line;
1080
    try {
1081
      line = frame.toString();
1082 1083
    } catch (e) {
      try {
1084
        line = "<error: " + e + ">";
1085 1086
      } catch (ee) {
        // Any code that reaches this point is seriously nasty!
1087
        line = "<error>";
1088 1089 1090 1091
      }
    }
    lines.push("    at " + line);
  }
1092
  return %_CallFunction(lines, "\n", ArrayJoin);
1093 1094 1095
}


1096 1097
function GetTypeName(receiver, requireConstructor) {
  var constructor = receiver.constructor;
1098 1099
  if (!constructor) {
    return requireConstructor ? null :
1100
        %_CallFunction(receiver, ObjectToString);
1101 1102 1103 1104
  }
  var constructorName = constructor.name;
  if (!constructorName) {
    return requireConstructor ? null :
1105
        %_CallFunction(receiver, ObjectToString);
1106 1107 1108
  }
  return constructorName;
}
1109

1110

1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
var stack_trace_symbol;  // Set during bootstrapping.
var formatted_stack_trace_symbol = NEW_PRIVATE("formatted stack trace");


// Format the stack trace if not yet done, and return it.
// Cache the formatted stack trace on the holder.
var StackTraceGetter = function() {
  var formatted_stack_trace = GET_PRIVATE(this, formatted_stack_trace_symbol);
  if (IS_UNDEFINED(formatted_stack_trace)) {
    var holder = this;
    while (!HAS_PRIVATE(holder, stack_trace_symbol)) {
      holder = %GetPrototype(holder);
      if (!holder) return UNDEFINED;
1124
    }
1125 1126 1127 1128 1129 1130 1131 1132
    var stack_trace = GET_PRIVATE(holder, stack_trace_symbol);
    if (IS_UNDEFINED(stack_trace)) return UNDEFINED;
    formatted_stack_trace = FormatStackTrace(holder, stack_trace);
    SET_PRIVATE(holder, stack_trace_symbol, UNDEFINED);
    SET_PRIVATE(holder, formatted_stack_trace_symbol, formatted_stack_trace);
  }
  return formatted_stack_trace;
};
1133 1134


1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
// If the receiver equals the holder, set the formatted stack trace that the
// getter returns.
var StackTraceSetter = function(v) {
  if (HAS_PRIVATE(this, stack_trace_symbol)) {
    SET_PRIVATE(this, stack_trace_symbol, UNDEFINED);
    SET_PRIVATE(this, formatted_stack_trace_symbol, v);
  }
};


// Use a dummy function since we do not actually want to capture a stack trace
// when constructing the initial Error prototytpes.
var captureStackTrace = function captureStackTrace(obj, cons_opt) {
  // Define accessors first, as this may fail and throw.
  ObjectDefineProperty(obj, 'stack', { get: StackTraceGetter,
1150 1151
                                       set: StackTraceSetter,
                                       configurable: true });
1152
  %CollectStackTrace(obj, cons_opt ? cons_opt : captureStackTrace);
1153
}
1154

1155

1156
function SetUpError() {
1157 1158
  // Define special error type constructors.

1159
  var DefineError = function(f) {
1160 1161 1162 1163 1164 1165 1166
    // Store the error function in both the global object
    // and the runtime object. The function is fetched
    // from the runtime object when throwing errors from
    // within the runtime system to avoid strange side
    // effects when overwriting the error functions from
    // user code.
    var name = f.name;
1167 1168 1169
    %AddNamedProperty(global, name, f, DONT_ENUM);
    %AddNamedProperty(builtins, '$' + name, f,
                      DONT_ENUM | DONT_DELETE | READ_ONLY);
1170 1171 1172 1173 1174 1175
    // Configure the error function.
    if (name == 'Error') {
      // The prototype of the Error object must itself be an error.
      // However, it can't be an instance of the Error object because
      // it hasn't been properly configured yet.  Instead we create a
      // special not-a-true-error-but-close-enough object.
1176
      var ErrorPrototype = function() {};
1177 1178 1179 1180 1181 1182 1183
      %FunctionSetPrototype(ErrorPrototype, $Object.prototype);
      %FunctionSetInstanceClassName(ErrorPrototype, 'Error');
      %FunctionSetPrototype(f, new ErrorPrototype());
    } else {
      %FunctionSetPrototype(f, new $Error());
    }
    %FunctionSetInstanceClassName(f, 'Error');
1184 1185
    %AddNamedProperty(f.prototype, 'constructor', f, DONT_ENUM);
    %AddNamedProperty(f.prototype, "name", name, DONT_ENUM);
1186 1187 1188 1189 1190
    %SetCode(f, function(m) {
      if (%_IsConstructCall()) {
        // Define all the expected properties directly on the error
        // object. This avoids going through getters and setters defined
        // on prototype objects.
1191
        %AddNamedProperty(this, 'stack', UNDEFINED, DONT_ENUM);
1192
        if (!IS_UNDEFINED(m)) {
1193
          %AddNamedProperty(this, 'message', ToString(m), DONT_ENUM);
1194
        }
1195
        try { captureStackTrace(this, f); } catch (e) { }
1196 1197 1198 1199
      } else {
        return new f(m);
      }
    });
1200
    %SetNativeFlag(f);
1201
  };
1202

1203 1204 1205 1206 1207 1208 1209
  DefineError(function Error() { });
  DefineError(function TypeError() { });
  DefineError(function RangeError() { });
  DefineError(function SyntaxError() { });
  DefineError(function ReferenceError() { });
  DefineError(function EvalError() { });
  DefineError(function URIError() { });
1210 1211 1212
}

SetUpError();
1213

1214
$Error.captureStackTrace = captureStackTrace;
1215

1216
%AddNamedProperty($Error.prototype, 'message', '', DONT_ENUM);
1217

1218
// Global list of error objects visited during ErrorToString. This is
1219
// used to detect cycles in error toString formatting.
1220 1221
var visited_errors = new InternalArray();
var cyclic_error_marker = new $Object();
1222

1223
function GetPropertyWithoutInvokingMonkeyGetters(error, name) {
1224
  var current = error;
1225
  // Climb the prototype chain until we find the holder.
1226
  while (current && !%HasOwnProperty(current, name)) {
1227
    current = %GetPrototype(current);
1228
  }
1229 1230
  if (IS_NULL(current)) return UNDEFINED;
  if (!IS_OBJECT(current)) return error[name];
1231 1232 1233
  // If the property is an accessor on one of the predefined errors that can be
  // generated statically by the compiler, don't touch it. This is to address
  // http://code.google.com/p/chromium/issues/detail?id=69187
1234
  var desc = %GetOwnProperty(current, name);
1235 1236
  if (desc && desc[IS_ACCESSOR_INDEX]) {
    var isName = name === "name";
1237
    if (current === $ReferenceError.prototype)
1238
      return isName ? "ReferenceError" : UNDEFINED;
1239
    if (current === $SyntaxError.prototype)
1240
      return isName ? "SyntaxError" : UNDEFINED;
1241
    if (current === $TypeError.prototype)
1242
      return isName ? "TypeError" : UNDEFINED;
1243 1244 1245 1246 1247
  }
  // Otherwise, read normally.
  return error[name];
}

1248
function ErrorToStringDetectCycle(error) {
1249
  if (!%PushIfAbsent(visited_errors, error)) throw cyclic_error_marker;
1250
  try {
1251
    var name = GetPropertyWithoutInvokingMonkeyGetters(error, "name");
1252
    name = IS_UNDEFINED(name) ? "Error" : TO_STRING_INLINE(name);
1253
    var message = GetPropertyWithoutInvokingMonkeyGetters(error, "message");
1254 1255 1256 1257
    message = IS_UNDEFINED(message) ? "" : TO_STRING_INLINE(message);
    if (name === "") return message;
    if (message === "") return name;
    return name + ": " + message;
1258
  } finally {
1259
    visited_errors.length = visited_errors.length - 1;
1260 1261 1262
  }
}

1263
function ErrorToString() {
1264 1265
  if (!IS_SPEC_OBJECT(this)) {
    throw MakeTypeError("called_on_non_object", ["Error.prototype.toString"]);
1266
  }
1267 1268

  try {
1269
    return ErrorToStringDetectCycle(this);
1270
  } catch(e) {
1271 1272
    // If this error message was encountered already return the empty
    // string for it instead of recursively formatting it.
1273
    if (e === cyclic_error_marker) {
1274 1275 1276
      return '';
    }
    throw e;
1277
  }
1278
}
1279

1280

1281
InstallFunctions($Error.prototype, DONT_ENUM, ['toString', ErrorToString]);
1282 1283

// Boilerplate for exceptions for stack overflows. Used from
1284
// Isolate::StackOverflow().
1285 1286 1287
function SetUpStackOverflowBoilerplate() {
  var boilerplate = MakeRangeError('stack_overflow', []);

1288
  %DefineAccessorPropertyUnchecked(
1289
      boilerplate, 'stack', StackTraceGetter, StackTraceSetter, DONT_ENUM);
1290 1291 1292 1293 1294

  return boilerplate;
}

var kStackOverflowBoilerplate = SetUpStackOverflowBoilerplate();