ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | // ------------------------------------------------------------------- |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 30 | // |
| 31 | // Matches Script::Type from objects.h |
| 32 | var TYPE_NATIVE = 0; |
| 33 | var TYPE_EXTENSION = 1; |
| 34 | var TYPE_NORMAL = 2; |
| 35 | |
| 36 | // Matches Script::CompilationType from objects.h |
| 37 | var COMPILATION_TYPE_HOST = 0; |
| 38 | var COMPILATION_TYPE_EVAL = 1; |
| 39 | var COMPILATION_TYPE_JSON = 2; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 41 | // Matches Messages::kNoLineNumberInfo from v8.h |
| 42 | var kNoLineNumberInfo = 0; |
| 43 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 44 | // If this object gets passed to an error constructor the error will |
| 45 | // get an accessor for .message that constructs a descriptive error |
| 46 | // message on access. |
| 47 | var kAddMessageAccessorsMarker = { }; |
| 48 | |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 49 | var kMessages = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 50 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 51 | var kReplacementMarkers = [ "%0", "%1", "%2", "%3" ]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 52 | |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 53 | function FormatString(format, message) { |
| 54 | var args = %MessageGetArguments(message); |
| 55 | var result = ""; |
| 56 | var arg_num = 0; |
| 57 | for (var i = 0; i < format.length; i++) { |
| 58 | var str = format[i]; |
| 59 | for (arg_num = 0; arg_num < kReplacementMarkers.length; arg_num++) { |
| 60 | if (format[i] !== kReplacementMarkers[arg_num]) continue; |
| 61 | try { |
| 62 | str = ToDetailString(args[arg_num]); |
| 63 | } catch (e) { |
| 64 | str = "#<error>"; |
| 65 | } |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 66 | } |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 67 | result += str; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 68 | } |
| 69 | return result; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 70 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 71 | |
| 72 | |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 73 | // To check if something is a native error we need to check the |
| 74 | // concrete native error types. It is not enough to check "obj |
| 75 | // instanceof $Error" because user code can replace |
| 76 | // NativeError.prototype.__proto__. User code cannot replace |
| 77 | // NativeError.prototype though and therefore this is a safe test. |
| 78 | function IsNativeErrorObject(obj) { |
| 79 | return (obj instanceof $Error) || |
| 80 | (obj instanceof $EvalError) || |
| 81 | (obj instanceof $RangeError) || |
| 82 | (obj instanceof $ReferenceError) || |
| 83 | (obj instanceof $SyntaxError) || |
| 84 | (obj instanceof $TypeError) || |
| 85 | (obj instanceof $URIError); |
| 86 | } |
| 87 | |
| 88 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 89 | // When formatting internally created error messages, do not |
| 90 | // invoke overwritten error toString methods but explicitly use |
| 91 | // the error to string method. This is to avoid leaking error |
| 92 | // objects between script tags in a browser setting. |
| 93 | function ToStringCheckErrorObject(obj) { |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 94 | if (IsNativeErrorObject(obj)) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 95 | return %_CallFunction(obj, errorToString); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 96 | } else { |
| 97 | return ToString(obj); |
| 98 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 99 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 100 | |
| 101 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 102 | function ToDetailString(obj) { |
| 103 | if (obj != null && IS_OBJECT(obj) && obj.toString === $Object.prototype.toString) { |
| 104 | var constructor = obj.constructor; |
| 105 | if (!constructor) return ToStringCheckErrorObject(obj); |
| 106 | var constructorName = constructor.name; |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 107 | if (!constructorName || !IS_STRING(constructorName)) { |
| 108 | return ToStringCheckErrorObject(obj); |
| 109 | } |
whesse@chromium.org | 7a392b3 | 2011-01-31 11:30:36 +0000 | [diff] [blame] | 110 | return "#<" + constructorName + ">"; |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 111 | } else { |
| 112 | return ToStringCheckErrorObject(obj); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 117 | function MakeGenericError(constructor, type, args) { |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 118 | if (IS_UNDEFINED(args)) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 119 | args = []; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 120 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 121 | var e = new constructor(kAddMessageAccessorsMarker); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 122 | e.type = type; |
| 123 | e.arguments = args; |
| 124 | return e; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 125 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 126 | |
| 127 | |
| 128 | /** |
| 129 | * Setup the Script function and constructor. |
| 130 | */ |
| 131 | %FunctionSetInstanceClassName(Script, 'Script'); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 132 | %SetProperty(Script.prototype, 'constructor', Script, DONT_ENUM); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 133 | %SetCode(Script, function(x) { |
| 134 | // Script objects can only be created by the VM. |
| 135 | throw new $Error("Not supported"); |
| 136 | }); |
| 137 | |
| 138 | |
| 139 | // Helper functions; called from the runtime system. |
| 140 | function FormatMessage(message) { |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 141 | if (kMessages === 0) { |
| 142 | kMessages = { |
| 143 | // Error |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 144 | cyclic_proto: ["Cyclic __proto__ value"], |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 145 | code_gen_from_strings: ["Code generation from strings disallowed for this context"], |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 146 | // TypeError |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 147 | unexpected_token: ["Unexpected token ", "%0"], |
| 148 | unexpected_token_number: ["Unexpected number"], |
| 149 | unexpected_token_string: ["Unexpected string"], |
| 150 | unexpected_token_identifier: ["Unexpected identifier"], |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 151 | unexpected_reserved: ["Unexpected reserved word"], |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 152 | unexpected_strict_reserved: ["Unexpected strict mode reserved word"], |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 153 | unexpected_eos: ["Unexpected end of input"], |
| 154 | malformed_regexp: ["Invalid regular expression: /", "%0", "/: ", "%1"], |
| 155 | unterminated_regexp: ["Invalid regular expression: missing /"], |
| 156 | regexp_flags: ["Cannot supply flags when constructing one RegExp from another"], |
| 157 | incompatible_method_receiver: ["Method ", "%0", " called on incompatible receiver ", "%1"], |
| 158 | invalid_lhs_in_assignment: ["Invalid left-hand side in assignment"], |
| 159 | invalid_lhs_in_for_in: ["Invalid left-hand side in for-in"], |
| 160 | invalid_lhs_in_postfix_op: ["Invalid left-hand side expression in postfix operation"], |
| 161 | invalid_lhs_in_prefix_op: ["Invalid left-hand side expression in prefix operation"], |
| 162 | multiple_defaults_in_switch: ["More than one default clause in switch statement"], |
| 163 | newline_after_throw: ["Illegal newline after throw"], |
| 164 | redeclaration: ["%0", " '", "%1", "' has already been declared"], |
| 165 | no_catch_or_finally: ["Missing catch or finally after try"], |
| 166 | unknown_label: ["Undefined label '", "%0", "'"], |
| 167 | uncaught_exception: ["Uncaught ", "%0"], |
| 168 | stack_trace: ["Stack Trace:\n", "%0"], |
| 169 | called_non_callable: ["%0", " is not a function"], |
| 170 | undefined_method: ["Object ", "%1", " has no method '", "%0", "'"], |
| 171 | property_not_function: ["Property '", "%0", "' of object ", "%1", " is not a function"], |
| 172 | cannot_convert_to_primitive: ["Cannot convert object to primitive value"], |
| 173 | not_constructor: ["%0", " is not a constructor"], |
| 174 | not_defined: ["%0", " is not defined"], |
| 175 | non_object_property_load: ["Cannot read property '", "%0", "' of ", "%1"], |
| 176 | non_object_property_store: ["Cannot set property '", "%0", "' of ", "%1"], |
| 177 | non_object_property_call: ["Cannot call method '", "%0", "' of ", "%1"], |
| 178 | with_expression: ["%0", " has no properties"], |
| 179 | illegal_invocation: ["Illegal invocation"], |
| 180 | no_setter_in_callback: ["Cannot set property ", "%0", " of ", "%1", " which has only a getter"], |
| 181 | apply_non_function: ["Function.prototype.apply was called on ", "%0", ", which is a ", "%1", " and not a function"], |
| 182 | apply_wrong_args: ["Function.prototype.apply: Arguments list has wrong type"], |
| 183 | invalid_in_operator_use: ["Cannot use 'in' operator to search for '", "%0", "' in ", "%1"], |
| 184 | instanceof_function_expected: ["Expecting a function in instanceof check, but got ", "%0"], |
| 185 | instanceof_nonobject_proto: ["Function has non-object prototype '", "%0", "' in instanceof check"], |
| 186 | null_to_object: ["Cannot convert null to object"], |
| 187 | reduce_no_initial: ["Reduce of empty array with no initial value"], |
| 188 | getter_must_be_callable: ["Getter must be a function: ", "%0"], |
| 189 | setter_must_be_callable: ["Setter must be a function: ", "%0"], |
| 190 | value_and_accessor: ["Invalid property. A property cannot both have accessors and be writable or have a value: ", "%0"], |
| 191 | proto_object_or_null: ["Object prototype may only be an Object or null"], |
| 192 | property_desc_object: ["Property description must be an object: ", "%0"], |
| 193 | redefine_disallowed: ["Cannot redefine property: ", "%0"], |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 194 | define_disallowed: ["Cannot define property:", "%0", ", object is not extensible."], |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 195 | non_extensible_proto: ["%0", " is not extensible"], |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 196 | handler_non_object: ["Proxy.", "%0", " called with non-object as handler"], |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 197 | handler_trap_missing: ["Proxy handler ", "%0", " has no '", "%1", "' trap"], |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 198 | handler_trap_must_be_callable: ["Proxy handler ", "%0", " has non-callable '", "%1", "' trap"], |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 199 | handler_returned_false: ["Proxy handler ", "%0", " returned false for '", "%1", "' trap"], |
| 200 | handler_returned_undefined: ["Proxy handler ", "%0", " returned undefined for '", "%1", "' trap"], |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 201 | proxy_prop_not_configurable: ["Trap ", "%1", " of proxy handler ", "%0", " returned non-configurable descriptor for property ", "%2"], |
| 202 | proxy_non_object_prop_names: ["Trap ", "%1", " returned non-object ", "%0"], |
| 203 | proxy_repeated_prop_name: ["Trap ", "%1", " returned repeated property name ", "%2"], |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 204 | invalid_weakmap_key: ["Invalid value used as weak map key"], |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 205 | // RangeError |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 206 | invalid_array_length: ["Invalid array length"], |
| 207 | stack_overflow: ["Maximum call stack size exceeded"], |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 208 | // SyntaxError |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 209 | unable_to_parse: ["Parse error"], |
| 210 | duplicate_regexp_flag: ["Duplicate RegExp flag ", "%0"], |
| 211 | invalid_regexp: ["Invalid RegExp pattern /", "%0", "/"], |
| 212 | illegal_break: ["Illegal break statement"], |
| 213 | illegal_continue: ["Illegal continue statement"], |
| 214 | illegal_return: ["Illegal return statement"], |
| 215 | error_loading_debugger: ["Error loading debugger"], |
| 216 | no_input_to_regexp: ["No input to ", "%0"], |
| 217 | invalid_json: ["String '", "%0", "' is not valid JSON"], |
| 218 | circular_structure: ["Converting circular structure to JSON"], |
| 219 | obj_ctor_property_non_object: ["Object.", "%0", " called on non-object"], |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 220 | called_on_null_or_undefined: ["%0", " called on null or undefined"], |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 221 | array_indexof_not_defined: ["Array.getIndexOf: Argument undefined"], |
| 222 | object_not_extensible: ["Can't add property ", "%0", ", object is not extensible"], |
| 223 | illegal_access: ["Illegal access"], |
| 224 | invalid_preparser_data: ["Invalid preparser data for function ", "%0"], |
| 225 | strict_mode_with: ["Strict mode code may not include a with statement"], |
| 226 | strict_catch_variable: ["Catch variable may not be eval or arguments in strict mode"], |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 227 | too_many_arguments: ["Too many arguments in function call (only 32766 allowed)"], |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 228 | too_many_parameters: ["Too many parameters in function definition (only 32766 allowed)"], |
| 229 | too_many_variables: ["Too many variables declared (only 32767 allowed)"], |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 230 | strict_param_name: ["Parameter name eval or arguments is not allowed in strict mode"], |
| 231 | strict_param_dupe: ["Strict mode function may not have duplicate parameter names"], |
| 232 | strict_var_name: ["Variable name may not be eval or arguments in strict mode"], |
| 233 | strict_function_name: ["Function name may not be eval or arguments in strict mode"], |
| 234 | strict_octal_literal: ["Octal literals are not allowed in strict mode."], |
| 235 | strict_duplicate_property: ["Duplicate data property in object literal not allowed in strict mode"], |
| 236 | accessor_data_property: ["Object literal may not have data and accessor property with the same name"], |
| 237 | accessor_get_set: ["Object literal may not have multiple get/set accessors with the same name"], |
| 238 | strict_lhs_assignment: ["Assignment to eval or arguments is not allowed in strict mode"], |
| 239 | strict_lhs_postfix: ["Postfix increment/decrement may not have eval or arguments operand in strict mode"], |
| 240 | strict_lhs_prefix: ["Prefix increment/decrement may not have eval or arguments operand in strict mode"], |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 241 | strict_reserved_word: ["Use of future reserved word in strict mode"], |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 242 | strict_delete: ["Delete of an unqualified identifier in strict mode."], |
| 243 | strict_delete_property: ["Cannot delete property '", "%0", "' of ", "%1"], |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 244 | strict_const: ["Use of const in strict mode."], |
| 245 | strict_function: ["In strict mode code, functions can only be declared at top level or immediately within another function." ], |
| 246 | strict_read_only_property: ["Cannot assign to read only property '", "%0", "' of ", "%1"], |
| 247 | strict_cannot_assign: ["Cannot assign to read only '", "%0", "' in strict mode"], |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 248 | strict_poison_pill: ["'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them"], |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 249 | strict_caller: ["Illegal access to a strict mode caller function."], |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 250 | }; |
| 251 | } |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 252 | var message_type = %MessageGetType(message); |
| 253 | var format = kMessages[message_type]; |
| 254 | if (!format) return "<unknown message " + message_type + ">"; |
| 255 | return FormatString(format, message); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 256 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 257 | |
| 258 | |
| 259 | function GetLineNumber(message) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 260 | var start_position = %MessageGetStartPosition(message); |
| 261 | if (start_position == -1) return kNoLineNumberInfo; |
| 262 | var script = %MessageGetScript(message); |
| 263 | var location = script.locationFromPosition(start_position, true); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 264 | if (location == null) return kNoLineNumberInfo; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 265 | return location.line + 1; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 266 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 267 | |
| 268 | |
| 269 | // Returns the source code line containing the given source |
| 270 | // position, or the empty string if the position is invalid. |
| 271 | function GetSourceLine(message) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 272 | var script = %MessageGetScript(message); |
| 273 | var start_position = %MessageGetStartPosition(message); |
| 274 | var location = script.locationFromPosition(start_position, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 275 | if (location == null) return ""; |
| 276 | location.restrict(); |
| 277 | return location.sourceText(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 278 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 279 | |
| 280 | |
| 281 | function MakeTypeError(type, args) { |
| 282 | return MakeGenericError($TypeError, type, args); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 283 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 284 | |
| 285 | |
| 286 | function MakeRangeError(type, args) { |
| 287 | return MakeGenericError($RangeError, type, args); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 288 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 289 | |
| 290 | |
| 291 | function MakeSyntaxError(type, args) { |
| 292 | return MakeGenericError($SyntaxError, type, args); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 293 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 294 | |
| 295 | |
| 296 | function MakeReferenceError(type, args) { |
| 297 | return MakeGenericError($ReferenceError, type, args); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 298 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 299 | |
| 300 | |
| 301 | function MakeEvalError(type, args) { |
| 302 | return MakeGenericError($EvalError, type, args); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 303 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 304 | |
| 305 | |
| 306 | function MakeError(type, args) { |
| 307 | return MakeGenericError($Error, type, args); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 308 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 309 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 310 | /** |
| 311 | * Find a line number given a specific source position. |
| 312 | * @param {number} position The source position. |
| 313 | * @return {number} 0 if input too small, -1 if input too large, |
| 314 | else the line number. |
| 315 | */ |
| 316 | Script.prototype.lineFromPosition = function(position) { |
| 317 | var lower = 0; |
| 318 | var upper = this.lineCount() - 1; |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 319 | var line_ends = this.line_ends; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 320 | |
| 321 | // We'll never find invalid positions so bail right away. |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 322 | if (position > line_ends[upper]) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | // This means we don't have to safe-guard indexing line_ends[i - 1]. |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 327 | if (position <= line_ends[0]) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | // Binary search to find line # from position range. |
| 332 | while (upper >= 1) { |
| 333 | var i = (lower + upper) >> 1; |
| 334 | |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 335 | if (position > line_ends[i]) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 336 | lower = i + 1; |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 337 | } else if (position <= line_ends[i - 1]) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 338 | upper = i - 1; |
| 339 | } else { |
| 340 | return i; |
| 341 | } |
| 342 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 343 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 344 | return -1; |
| 345 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 346 | |
| 347 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 348 | * Get information on a specific source position. |
| 349 | * @param {number} position The source position |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 350 | * @param {boolean} include_resource_offset Set to true to have the resource |
| 351 | * offset added to the location |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 352 | * @return {SourceLocation} |
| 353 | * If line is negative or not in the source null is returned. |
| 354 | */ |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 355 | Script.prototype.locationFromPosition = function (position, |
| 356 | include_resource_offset) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 357 | var line = this.lineFromPosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 358 | if (line == -1) return null; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 359 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 360 | // Determine start, end and column. |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 361 | var line_ends = this.line_ends; |
| 362 | var start = line == 0 ? 0 : line_ends[line - 1] + 1; |
| 363 | var end = line_ends[line]; |
whesse@chromium.org | 7a392b3 | 2011-01-31 11:30:36 +0000 | [diff] [blame] | 364 | if (end > 0 && %_CallFunction(this.source, end - 1, StringCharAt) == '\r') end--; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 365 | var column = position - start; |
| 366 | |
| 367 | // Adjust according to the offset within the resource. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 368 | if (include_resource_offset) { |
| 369 | line += this.line_offset; |
| 370 | if (line == this.line_offset) { |
| 371 | column += this.column_offset; |
| 372 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | return new SourceLocation(this, position, line, column, start, end); |
| 376 | }; |
| 377 | |
| 378 | |
| 379 | /** |
| 380 | * Get information on a specific source line and column possibly offset by a |
| 381 | * fixed source position. This function is used to find a source position from |
| 382 | * a line and column position. The fixed source position offset is typically |
| 383 | * used to find a source position in a function based on a line and column in |
| 384 | * the source for the function alone. The offset passed will then be the |
| 385 | * start position of the source for the function within the full script source. |
| 386 | * @param {number} opt_line The line within the source. Default value is 0 |
| 387 | * @param {number} opt_column The column in within the line. Default value is 0 |
| 388 | * @param {number} opt_offset_position The offset from the begining of the |
| 389 | * source from where the line and column calculation starts. Default value is 0 |
| 390 | * @return {SourceLocation} |
| 391 | * If line is negative or not in the source null is returned. |
| 392 | */ |
| 393 | Script.prototype.locationFromLine = function (opt_line, opt_column, opt_offset_position) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 394 | // Default is the first line in the script. Lines in the script is relative |
| 395 | // to the offset within the resource. |
| 396 | var line = 0; |
| 397 | if (!IS_UNDEFINED(opt_line)) { |
| 398 | line = opt_line - this.line_offset; |
| 399 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 400 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 401 | // Default is first column. If on the first line add the offset within the |
| 402 | // resource. |
| 403 | var column = opt_column || 0; |
| 404 | if (line == 0) { |
| 405 | column -= this.column_offset |
| 406 | } |
| 407 | |
| 408 | var offset_position = opt_offset_position || 0; |
| 409 | if (line < 0 || column < 0 || offset_position < 0) return null; |
| 410 | if (line == 0) { |
ager@chromium.org | 3a6061e | 2009-03-12 14:24:36 +0000 | [diff] [blame] | 411 | return this.locationFromPosition(offset_position + column, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 412 | } else { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 413 | // Find the line where the offset position is located. |
| 414 | var offset_line = this.lineFromPosition(offset_position); |
| 415 | |
| 416 | if (offset_line == -1 || offset_line + line >= this.lineCount()) { |
| 417 | return null; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 418 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 419 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 420 | return this.locationFromPosition(this.line_ends[offset_line + line - 1] + 1 + column); // line > 0 here. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | |
| 425 | /** |
| 426 | * Get a slice of source code from the script. The boundaries for the slice is |
| 427 | * specified in lines. |
| 428 | * @param {number} opt_from_line The first line (zero bound) in the slice. |
| 429 | * Default is 0 |
| 430 | * @param {number} opt_to_column The last line (zero bound) in the slice (non |
| 431 | * inclusive). Default is the number of lines in the script |
| 432 | * @return {SourceSlice} The source slice or null of the parameters where |
| 433 | * invalid |
| 434 | */ |
| 435 | Script.prototype.sourceSlice = function (opt_from_line, opt_to_line) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 436 | var from_line = IS_UNDEFINED(opt_from_line) ? this.line_offset : opt_from_line; |
| 437 | var to_line = IS_UNDEFINED(opt_to_line) ? this.line_offset + this.lineCount() : opt_to_line |
| 438 | |
| 439 | // Adjust according to the offset within the resource. |
| 440 | from_line -= this.line_offset; |
| 441 | to_line -= this.line_offset; |
| 442 | if (from_line < 0) from_line = 0; |
| 443 | if (to_line > this.lineCount()) to_line = this.lineCount(); |
| 444 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 445 | // Check parameters. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 446 | if (from_line >= this.lineCount() || |
| 447 | to_line < 0 || |
| 448 | from_line > to_line) { |
| 449 | return null; |
| 450 | } |
| 451 | |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 452 | var line_ends = this.line_ends; |
| 453 | var from_position = from_line == 0 ? 0 : line_ends[from_line - 1] + 1; |
| 454 | var to_position = to_line == 0 ? 0 : line_ends[to_line - 1] + 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 455 | |
| 456 | // Return a source slice with line numbers re-adjusted to the resource. |
| 457 | return new SourceSlice(this, from_line + this.line_offset, to_line + this.line_offset, |
| 458 | from_position, to_position); |
| 459 | } |
| 460 | |
| 461 | |
| 462 | Script.prototype.sourceLine = function (opt_line) { |
| 463 | // Default is the first line in the script. Lines in the script are relative |
| 464 | // to the offset within the resource. |
| 465 | var line = 0; |
| 466 | if (!IS_UNDEFINED(opt_line)) { |
| 467 | line = opt_line - this.line_offset; |
| 468 | } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 469 | |
| 470 | // Check parameter. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 471 | if (line < 0 || this.lineCount() <= line) { |
| 472 | return null; |
| 473 | } |
| 474 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 475 | // Return the source line. |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 476 | var line_ends = this.line_ends; |
| 477 | var start = line == 0 ? 0 : line_ends[line - 1] + 1; |
| 478 | var end = line_ends[line]; |
whesse@chromium.org | 7a392b3 | 2011-01-31 11:30:36 +0000 | [diff] [blame] | 479 | return %_CallFunction(this.source, start, end, StringSubstring); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | |
| 483 | /** |
| 484 | * Returns the number of source lines. |
| 485 | * @return {number} |
| 486 | * Number of source lines. |
| 487 | */ |
| 488 | Script.prototype.lineCount = function() { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 489 | // Return number of source lines. |
| 490 | return this.line_ends.length; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 491 | }; |
| 492 | |
| 493 | |
| 494 | /** |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 495 | * Returns the name of script if available, contents of sourceURL comment |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 496 | * otherwise. See |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 497 | * http://fbug.googlecode.com/svn/branches/firebug1.1/docs/ReleaseNotes_1.1.txt |
| 498 | * for details on using //@ sourceURL comment to identify scritps that don't |
| 499 | * have name. |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 500 | * |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 501 | * @return {?string} script name if present, value for //@ sourceURL comment |
| 502 | * otherwise. |
| 503 | */ |
| 504 | Script.prototype.nameOrSourceURL = function() { |
| 505 | if (this.name) |
| 506 | return this.name; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 507 | // TODO(608): the spaces in a regexp below had to be escaped as \040 |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 508 | // because this file is being processed by js2c whose handling of spaces |
| 509 | // in regexps is broken. Also, ['"] are excluded from allowed URLs to |
| 510 | // avoid matches against sources that invoke evals with sourceURL. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 511 | // A better solution would be to detect these special comments in |
| 512 | // the scanner/parser. |
| 513 | var source = ToString(this.source); |
| 514 | var sourceUrlPos = %StringIndexOf(source, "sourceURL=", 0); |
| 515 | if (sourceUrlPos > 4) { |
| 516 | var sourceUrlPattern = |
| 517 | /\/\/@[\040\t]sourceURL=[\040\t]*([^\s\'\"]*)[\040\t]*$/gm; |
| 518 | // Don't reuse lastMatchInfo here, so we create a new array with room |
| 519 | // for four captures (array with length one longer than the index |
| 520 | // of the fourth capture, where the numbering is zero-based). |
| 521 | var matchInfo = new InternalArray(CAPTURE(3) + 1); |
| 522 | var match = |
| 523 | %_RegExpExec(sourceUrlPattern, source, sourceUrlPos - 4, matchInfo); |
| 524 | if (match) { |
| 525 | return SubString(source, matchInfo[CAPTURE(2)], matchInfo[CAPTURE(3)]); |
| 526 | } |
| 527 | } |
| 528 | return this.name; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | |
| 532 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 533 | * Class for source location. A source location is a position within some |
| 534 | * source with the following properties: |
| 535 | * script : script object for the source |
| 536 | * line : source line number |
| 537 | * column : source column within the line |
| 538 | * position : position within the source |
| 539 | * start : position of start of source context (inclusive) |
| 540 | * end : position of end of source context (not inclusive) |
| 541 | * Source text for the source context is the character interval [start, end[. In |
| 542 | * most cases end will point to a newline character. It might point just past |
| 543 | * the final position of the source if the last source line does not end with a |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 544 | * newline character. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 545 | * @param {Script} script The Script object for which this is a location |
| 546 | * @param {number} position Source position for the location |
| 547 | * @param {number} line The line number for the location |
| 548 | * @param {number} column The column within the line for the location |
| 549 | * @param {number} start Source position for start of source context |
| 550 | * @param {number} end Source position for end of source context |
| 551 | * @constructor |
| 552 | */ |
| 553 | function SourceLocation(script, position, line, column, start, end) { |
| 554 | this.script = script; |
| 555 | this.position = position; |
| 556 | this.line = line; |
| 557 | this.column = column; |
| 558 | this.start = start; |
| 559 | this.end = end; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 560 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 561 | |
| 562 | |
| 563 | const kLineLengthLimit = 78; |
| 564 | |
| 565 | /** |
| 566 | * Restrict source location start and end positions to make the source slice |
| 567 | * no more that a certain number of characters wide. |
| 568 | * @param {number} opt_limit The with limit of the source text with a default |
| 569 | * of 78 |
| 570 | * @param {number} opt_before The number of characters to prefer before the |
| 571 | * position with a default value of 10 less that the limit |
| 572 | */ |
| 573 | SourceLocation.prototype.restrict = function (opt_limit, opt_before) { |
| 574 | // Find the actual limit to use. |
| 575 | var limit; |
| 576 | var before; |
| 577 | if (!IS_UNDEFINED(opt_limit)) { |
| 578 | limit = opt_limit; |
| 579 | } else { |
| 580 | limit = kLineLengthLimit; |
| 581 | } |
| 582 | if (!IS_UNDEFINED(opt_before)) { |
| 583 | before = opt_before; |
| 584 | } else { |
| 585 | // If no before is specified center for small limits and perfer more source |
| 586 | // before the the position that after for longer limits. |
| 587 | if (limit <= 20) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 588 | before = $floor(limit / 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 589 | } else { |
| 590 | before = limit - 10; |
| 591 | } |
| 592 | } |
| 593 | if (before >= limit) { |
| 594 | before = limit - 1; |
| 595 | } |
| 596 | |
| 597 | // If the [start, end[ interval is too big we restrict |
| 598 | // it in one or both ends. We make sure to always produce |
| 599 | // restricted intervals of maximum allowed size. |
| 600 | if (this.end - this.start > limit) { |
| 601 | var start_limit = this.position - before; |
| 602 | var end_limit = this.position + limit - before; |
| 603 | if (this.start < start_limit && end_limit < this.end) { |
| 604 | this.start = start_limit; |
| 605 | this.end = end_limit; |
| 606 | } else if (this.start < start_limit) { |
| 607 | this.start = this.end - limit; |
| 608 | } else { |
| 609 | this.end = this.start + limit; |
| 610 | } |
| 611 | } |
| 612 | }; |
| 613 | |
| 614 | |
| 615 | /** |
| 616 | * Get the source text for a SourceLocation |
| 617 | * @return {String} |
| 618 | * Source text for this location. |
| 619 | */ |
| 620 | SourceLocation.prototype.sourceText = function () { |
whesse@chromium.org | 7a392b3 | 2011-01-31 11:30:36 +0000 | [diff] [blame] | 621 | return %_CallFunction(this.script.source, this.start, this.end, StringSubstring); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 622 | }; |
| 623 | |
| 624 | |
| 625 | /** |
| 626 | * Class for a source slice. A source slice is a part of a script source with |
| 627 | * the following properties: |
| 628 | * script : script object for the source |
| 629 | * from_line : line number for the first line in the slice |
| 630 | * to_line : source line number for the last line in the slice |
| 631 | * from_position : position of the first character in the slice |
| 632 | * to_position : position of the last character in the slice |
| 633 | * The to_line and to_position are not included in the slice, that is the lines |
| 634 | * in the slice are [from_line, to_line[. Likewise the characters in the slice |
| 635 | * are [from_position, to_position[. |
| 636 | * @param {Script} script The Script object for the source slice |
| 637 | * @param {number} from_line |
| 638 | * @param {number} to_line |
| 639 | * @param {number} from_position |
| 640 | * @param {number} to_position |
| 641 | * @constructor |
| 642 | */ |
| 643 | function SourceSlice(script, from_line, to_line, from_position, to_position) { |
| 644 | this.script = script; |
| 645 | this.from_line = from_line; |
| 646 | this.to_line = to_line; |
| 647 | this.from_position = from_position; |
| 648 | this.to_position = to_position; |
| 649 | } |
| 650 | |
| 651 | |
| 652 | /** |
| 653 | * Get the source text for a SourceSlice |
| 654 | * @return {String} Source text for this slice. The last line will include |
| 655 | * the line terminating characters (if any) |
| 656 | */ |
| 657 | SourceSlice.prototype.sourceText = function () { |
whesse@chromium.org | 7a392b3 | 2011-01-31 11:30:36 +0000 | [diff] [blame] | 658 | return %_CallFunction(this.script.source, |
| 659 | this.from_position, |
| 660 | this.to_position, |
| 661 | StringSubstring); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 662 | }; |
| 663 | |
| 664 | |
| 665 | // Returns the offset of the given position within the containing |
| 666 | // line. |
| 667 | function GetPositionInLine(message) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 668 | var script = %MessageGetScript(message); |
| 669 | var start_position = %MessageGetStartPosition(message); |
| 670 | var location = script.locationFromPosition(start_position, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 671 | if (location == null) return -1; |
| 672 | location.restrict(); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 673 | return start_position - location.start; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 674 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 675 | |
| 676 | |
| 677 | function GetStackTraceLine(recv, fun, pos, isGlobal) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 678 | return FormatSourcePosition(new CallSite(recv, fun, pos)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 679 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 680 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 681 | // ---------------------------------------------------------------------------- |
| 682 | // Error implementation |
| 683 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 684 | // Defines accessors for a property that is calculated the first time |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 685 | // the property is read. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 686 | function DefineOneShotAccessor(obj, name, fun) { |
| 687 | // Note that the accessors consistently operate on 'obj', not 'this'. |
| 688 | // Since the object may occur in someone else's prototype chain we |
| 689 | // can't rely on 'this' being the same as 'obj'. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 690 | var hasBeenSet = false; |
| 691 | var value; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 692 | function getter() { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 693 | if (hasBeenSet) { |
| 694 | return value; |
| 695 | } |
| 696 | hasBeenSet = true; |
| 697 | value = fun(obj); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 698 | return value; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 699 | } |
| 700 | function setter(v) { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 701 | hasBeenSet = true; |
| 702 | value = v; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 703 | } |
| 704 | var desc = { get: getter, |
| 705 | set: setter, |
| 706 | enumerable: false, |
| 707 | configurable: true }; |
| 708 | desc = ToPropertyDescriptor(desc); |
| 709 | DefineOwnProperty(obj, name, desc, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 710 | } |
| 711 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 712 | function CallSite(receiver, fun, pos) { |
| 713 | this.receiver = receiver; |
| 714 | this.fun = fun; |
| 715 | this.pos = pos; |
| 716 | } |
| 717 | |
| 718 | CallSite.prototype.getThis = function () { |
| 719 | return this.receiver; |
| 720 | }; |
| 721 | |
| 722 | CallSite.prototype.getTypeName = function () { |
| 723 | var constructor = this.receiver.constructor; |
| 724 | if (!constructor) |
whesse@chromium.org | 7a392b3 | 2011-01-31 11:30:36 +0000 | [diff] [blame] | 725 | return %_CallFunction(this.receiver, ObjectToString); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 726 | var constructorName = constructor.name; |
| 727 | if (!constructorName) |
whesse@chromium.org | 7a392b3 | 2011-01-31 11:30:36 +0000 | [diff] [blame] | 728 | return %_CallFunction(this.receiver, ObjectToString); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 729 | return constructorName; |
| 730 | }; |
| 731 | |
| 732 | CallSite.prototype.isToplevel = function () { |
| 733 | if (this.receiver == null) |
| 734 | return true; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 735 | return IS_GLOBAL(this.receiver); |
| 736 | }; |
| 737 | |
| 738 | CallSite.prototype.isEval = function () { |
| 739 | var script = %FunctionGetScript(this.fun); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 740 | return script && script.compilation_type == COMPILATION_TYPE_EVAL; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 741 | }; |
| 742 | |
| 743 | CallSite.prototype.getEvalOrigin = function () { |
| 744 | var script = %FunctionGetScript(this.fun); |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 745 | return FormatEvalOrigin(script); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 746 | }; |
| 747 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 748 | CallSite.prototype.getScriptNameOrSourceURL = function () { |
| 749 | var script = %FunctionGetScript(this.fun); |
| 750 | return script ? script.nameOrSourceURL() : null; |
| 751 | }; |
| 752 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 753 | CallSite.prototype.getFunction = function () { |
| 754 | return this.fun; |
| 755 | }; |
| 756 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 757 | CallSite.prototype.getFunctionName = function () { |
| 758 | // See if the function knows its own name |
| 759 | var name = this.fun.name; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 760 | if (name) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 761 | return name; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 762 | } else { |
| 763 | return %FunctionGetInferredName(this.fun); |
| 764 | } |
| 765 | // Maybe this is an evaluation? |
| 766 | var script = %FunctionGetScript(this.fun); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 767 | if (script && script.compilation_type == COMPILATION_TYPE_EVAL) |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 768 | return "eval"; |
| 769 | return null; |
| 770 | }; |
| 771 | |
| 772 | CallSite.prototype.getMethodName = function () { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 773 | // See if we can find a unique property on the receiver that holds |
| 774 | // this function. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 775 | var ownName = this.fun.name; |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 776 | if (ownName && this.receiver && |
whesse@chromium.org | 7a392b3 | 2011-01-31 11:30:36 +0000 | [diff] [blame] | 777 | (%_CallFunction(this.receiver, ownName, ObjectLookupGetter) === this.fun || |
| 778 | %_CallFunction(this.receiver, ownName, ObjectLookupSetter) === this.fun || |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 779 | this.receiver[ownName] === this.fun)) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 780 | // To handle DontEnum properties we guess that the method has |
| 781 | // the same name as the function. |
| 782 | return ownName; |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 783 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 784 | var name = null; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 785 | for (var prop in this.receiver) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 786 | if (this.receiver.__lookupGetter__(prop) === this.fun || |
| 787 | this.receiver.__lookupSetter__(prop) === this.fun || |
| 788 | (!this.receiver.__lookupGetter__(prop) && this.receiver[prop] === this.fun)) { |
| 789 | // If we find more than one match bail out to avoid confusion. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 790 | if (name) |
| 791 | return null; |
| 792 | name = prop; |
| 793 | } |
| 794 | } |
| 795 | if (name) |
| 796 | return name; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 797 | return null; |
| 798 | }; |
| 799 | |
| 800 | CallSite.prototype.getFileName = function () { |
| 801 | var script = %FunctionGetScript(this.fun); |
| 802 | return script ? script.name : null; |
| 803 | }; |
| 804 | |
| 805 | CallSite.prototype.getLineNumber = function () { |
| 806 | if (this.pos == -1) |
| 807 | return null; |
| 808 | var script = %FunctionGetScript(this.fun); |
| 809 | var location = null; |
| 810 | if (script) { |
| 811 | location = script.locationFromPosition(this.pos, true); |
| 812 | } |
| 813 | return location ? location.line + 1 : null; |
| 814 | }; |
| 815 | |
| 816 | CallSite.prototype.getColumnNumber = function () { |
| 817 | if (this.pos == -1) |
| 818 | return null; |
| 819 | var script = %FunctionGetScript(this.fun); |
| 820 | var location = null; |
| 821 | if (script) { |
| 822 | location = script.locationFromPosition(this.pos, true); |
| 823 | } |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 824 | return location ? location.column + 1: null; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 825 | }; |
| 826 | |
| 827 | CallSite.prototype.isNative = function () { |
| 828 | var script = %FunctionGetScript(this.fun); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 829 | return script ? (script.type == TYPE_NATIVE) : false; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 830 | }; |
| 831 | |
| 832 | CallSite.prototype.getPosition = function () { |
| 833 | return this.pos; |
| 834 | }; |
| 835 | |
| 836 | CallSite.prototype.isConstructor = function () { |
| 837 | var constructor = this.receiver ? this.receiver.constructor : null; |
| 838 | if (!constructor) |
| 839 | return false; |
| 840 | return this.fun === constructor; |
| 841 | }; |
| 842 | |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 843 | function FormatEvalOrigin(script) { |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 844 | var sourceURL = script.nameOrSourceURL(); |
| 845 | if (sourceURL) |
| 846 | return sourceURL; |
| 847 | |
| 848 | var eval_origin = "eval at "; |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 849 | if (script.eval_from_function_name) { |
| 850 | eval_origin += script.eval_from_function_name; |
| 851 | } else { |
| 852 | eval_origin += "<anonymous>"; |
| 853 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 854 | |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 855 | var eval_from_script = script.eval_from_script; |
| 856 | if (eval_from_script) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 857 | if (eval_from_script.compilation_type == COMPILATION_TYPE_EVAL) { |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 858 | // eval script originated from another eval. |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 859 | eval_origin += " (" + FormatEvalOrigin(eval_from_script) + ")"; |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 860 | } else { |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 861 | // eval script originated from "real" source. |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 862 | if (eval_from_script.name) { |
| 863 | eval_origin += " (" + eval_from_script.name; |
| 864 | var location = eval_from_script.locationFromPosition(script.eval_from_script_position, true); |
| 865 | if (location) { |
| 866 | eval_origin += ":" + (location.line + 1); |
| 867 | eval_origin += ":" + (location.column + 1); |
| 868 | } |
| 869 | eval_origin += ")" |
| 870 | } else { |
| 871 | eval_origin += " (unknown source)"; |
| 872 | } |
| 873 | } |
| 874 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 875 | |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 876 | return eval_origin; |
| 877 | }; |
| 878 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 879 | function FormatSourcePosition(frame) { |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 880 | var fileName; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 881 | var fileLocation = ""; |
| 882 | if (frame.isNative()) { |
| 883 | fileLocation = "native"; |
| 884 | } else if (frame.isEval()) { |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 885 | fileName = frame.getScriptNameOrSourceURL(); |
| 886 | if (!fileName) |
| 887 | fileLocation = frame.getEvalOrigin(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 888 | } else { |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 889 | fileName = frame.getFileName(); |
| 890 | } |
| 891 | |
| 892 | if (fileName) { |
| 893 | fileLocation += fileName; |
| 894 | var lineNumber = frame.getLineNumber(); |
| 895 | if (lineNumber != null) { |
| 896 | fileLocation += ":" + lineNumber; |
| 897 | var columnNumber = frame.getColumnNumber(); |
| 898 | if (columnNumber) { |
| 899 | fileLocation += ":" + columnNumber; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | } |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 903 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 904 | if (!fileLocation) { |
| 905 | fileLocation = "unknown source"; |
| 906 | } |
| 907 | var line = ""; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 908 | var functionName = frame.getFunction().name; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 909 | var addPrefix = true; |
| 910 | var isConstructor = frame.isConstructor(); |
| 911 | var isMethodCall = !(frame.isToplevel() || isConstructor); |
| 912 | if (isMethodCall) { |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 913 | var methodName = frame.getMethodName(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 914 | line += frame.getTypeName() + "."; |
| 915 | if (functionName) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 916 | line += functionName; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 917 | if (methodName && (methodName != functionName)) { |
| 918 | line += " [as " + methodName + "]"; |
| 919 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 920 | } else { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 921 | line += methodName || "<anonymous>"; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 922 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 923 | } else if (isConstructor) { |
| 924 | line += "new " + (functionName || "<anonymous>"); |
| 925 | } else if (functionName) { |
| 926 | line += functionName; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 927 | } else { |
| 928 | line += fileLocation; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 929 | addPrefix = false; |
| 930 | } |
| 931 | if (addPrefix) { |
| 932 | line += " (" + fileLocation + ")"; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 933 | } |
| 934 | return line; |
| 935 | } |
| 936 | |
| 937 | function FormatStackTrace(error, frames) { |
| 938 | var lines = []; |
| 939 | try { |
| 940 | lines.push(error.toString()); |
| 941 | } catch (e) { |
| 942 | try { |
| 943 | lines.push("<error: " + e + ">"); |
| 944 | } catch (ee) { |
| 945 | lines.push("<error>"); |
| 946 | } |
| 947 | } |
| 948 | for (var i = 0; i < frames.length; i++) { |
| 949 | var frame = frames[i]; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 950 | var line; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 951 | try { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 952 | line = FormatSourcePosition(frame); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 953 | } catch (e) { |
| 954 | try { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 955 | line = "<error: " + e + ">"; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 956 | } catch (ee) { |
| 957 | // Any code that reaches this point is seriously nasty! |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 958 | line = "<error>"; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 959 | } |
| 960 | } |
| 961 | lines.push(" at " + line); |
| 962 | } |
| 963 | return lines.join("\n"); |
| 964 | } |
| 965 | |
| 966 | function FormatRawStackTrace(error, raw_stack) { |
| 967 | var frames = [ ]; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 968 | for (var i = 0; i < raw_stack.length; i += 4) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 969 | var recv = raw_stack[i]; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 970 | var fun = raw_stack[i + 1]; |
| 971 | var code = raw_stack[i + 2]; |
| 972 | var pc = raw_stack[i + 3]; |
| 973 | var pos = %FunctionGetPositionForOffset(code, pc); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 974 | frames.push(new CallSite(recv, fun, pos)); |
| 975 | } |
| 976 | if (IS_FUNCTION($Error.prepareStackTrace)) { |
| 977 | return $Error.prepareStackTrace(error, frames); |
| 978 | } else { |
| 979 | return FormatStackTrace(error, frames); |
| 980 | } |
| 981 | } |
| 982 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 983 | function DefineError(f) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 984 | // Store the error function in both the global object |
| 985 | // and the runtime object. The function is fetched |
| 986 | // from the runtime object when throwing errors from |
| 987 | // within the runtime system to avoid strange side |
| 988 | // effects when overwriting the error functions from |
| 989 | // user code. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 990 | var name = f.name; |
| 991 | %SetProperty(global, name, f, DONT_ENUM); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 992 | this['$' + name] = f; |
| 993 | // Configure the error function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 994 | if (name == 'Error') { |
| 995 | // The prototype of the Error object must itself be an error. |
| 996 | // However, it can't be an instance of the Error object because |
| 997 | // it hasn't been properly configured yet. Instead we create a |
| 998 | // special not-a-true-error-but-close-enough object. |
| 999 | function ErrorPrototype() {} |
| 1000 | %FunctionSetPrototype(ErrorPrototype, $Object.prototype); |
| 1001 | %FunctionSetInstanceClassName(ErrorPrototype, 'Error'); |
| 1002 | %FunctionSetPrototype(f, new ErrorPrototype()); |
| 1003 | } else { |
| 1004 | %FunctionSetPrototype(f, new $Error()); |
| 1005 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1006 | %FunctionSetInstanceClassName(f, 'Error'); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1007 | %SetProperty(f.prototype, 'constructor', f, DONT_ENUM); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1008 | // The name property on the prototype of error objects is not |
| 1009 | // specified as being read-one and dont-delete. However, allowing |
| 1010 | // overwriting allows leaks of error objects between script blocks |
| 1011 | // in the same context in a browser setting. Therefore we fix the |
| 1012 | // name. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1013 | %SetProperty(f.prototype, "name", name, DONT_ENUM | DONT_DELETE | READ_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1014 | %SetCode(f, function(m) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1015 | if (%_IsConstructCall()) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1016 | // Define all the expected properties directly on the error |
| 1017 | // object. This avoids going through getters and setters defined |
| 1018 | // on prototype objects. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1019 | %IgnoreAttributesAndSetProperty(this, 'stack', void 0, DONT_ENUM); |
| 1020 | %IgnoreAttributesAndSetProperty(this, 'arguments', void 0, DONT_ENUM); |
| 1021 | %IgnoreAttributesAndSetProperty(this, 'type', void 0, DONT_ENUM); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1022 | if (m === kAddMessageAccessorsMarker) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1023 | // DefineOneShotAccessor always inserts a message property and |
| 1024 | // ignores setters. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1025 | DefineOneShotAccessor(this, 'message', function (obj) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 1026 | return FormatMessage(%NewMessageObject(obj.type, obj.arguments)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1027 | }); |
| 1028 | } else if (!IS_UNDEFINED(m)) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1029 | %IgnoreAttributesAndSetProperty(this, |
| 1030 | 'message', |
| 1031 | ToString(m), |
| 1032 | DONT_ENUM); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1033 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1034 | captureStackTrace(this, f); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1035 | } else { |
| 1036 | return new f(m); |
| 1037 | } |
| 1038 | }); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1039 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1040 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1041 | function captureStackTrace(obj, cons_opt) { |
| 1042 | var stackTraceLimit = $Error.stackTraceLimit; |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 1043 | if (!stackTraceLimit || !IS_NUMBER(stackTraceLimit)) return; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1044 | if (stackTraceLimit < 0 || stackTraceLimit > 10000) |
| 1045 | stackTraceLimit = 10000; |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 1046 | var raw_stack = %CollectStackTrace(cons_opt |
| 1047 | ? cons_opt |
| 1048 | : captureStackTrace, stackTraceLimit); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1049 | DefineOneShotAccessor(obj, 'stack', function (obj) { |
| 1050 | return FormatRawStackTrace(obj, raw_stack); |
| 1051 | }); |
| 1052 | }; |
| 1053 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1054 | $Math.__proto__ = global.Object.prototype; |
| 1055 | |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 1056 | // DefineError is a native function. Use explicit receiver. Otherwise |
| 1057 | // the receiver will be 'undefined'. |
| 1058 | this.DefineError(function Error() { }); |
| 1059 | this.DefineError(function TypeError() { }); |
| 1060 | this.DefineError(function RangeError() { }); |
| 1061 | this.DefineError(function SyntaxError() { }); |
| 1062 | this.DefineError(function ReferenceError() { }); |
| 1063 | this.DefineError(function EvalError() { }); |
| 1064 | this.DefineError(function URIError() { }); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1065 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1066 | $Error.captureStackTrace = captureStackTrace; |
| 1067 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1068 | // Setup extra properties of the Error.prototype object. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1069 | function setErrorMessage() { |
| 1070 | var desc = {value: '', |
| 1071 | enumerable: false, |
| 1072 | configurable: true, |
| 1073 | writable: true }; |
| 1074 | DefineOwnProperty($Error.prototype, |
| 1075 | 'message', |
| 1076 | ToPropertyDescriptor(desc), |
| 1077 | true); |
| 1078 | |
| 1079 | } |
| 1080 | |
| 1081 | setErrorMessage(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1082 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1083 | // Global list of error objects visited during errorToString. This is |
| 1084 | // used to detect cycles in error toString formatting. |
| 1085 | var visited_errors = new $Array(); |
| 1086 | var cyclic_error_marker = new $Object(); |
| 1087 | |
| 1088 | function errorToStringDetectCycle() { |
| 1089 | if (!%PushIfAbsent(visited_errors, this)) throw cyclic_error_marker; |
| 1090 | try { |
| 1091 | var type = this.type; |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 1092 | if (type && !%_CallFunction(this, "message", ObjectHasOwnProperty)) { |
| 1093 | var formatted = FormatMessage(%NewMessageObject(type, this.arguments)); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1094 | return this.name + ": " + formatted; |
| 1095 | } |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 1096 | var message = %_CallFunction(this, "message", ObjectHasOwnProperty) |
| 1097 | ? (": " + this.message) |
| 1098 | : ""; |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1099 | return this.name + message; |
| 1100 | } finally { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 1101 | visited_errors.length = visited_errors.length - 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1102 | } |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | function errorToString() { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 1106 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 1107 | throw MakeTypeError("called_on_null_or_undefined", |
| 1108 | ["Error.prototype.toString"]); |
| 1109 | } |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1110 | // This helper function is needed because access to properties on |
| 1111 | // the builtins object do not work inside of a catch clause. |
| 1112 | function isCyclicErrorMarker(o) { return o === cyclic_error_marker; } |
| 1113 | |
| 1114 | try { |
| 1115 | return %_CallFunction(this, errorToStringDetectCycle); |
| 1116 | } catch(e) { |
| 1117 | // If this error message was encountered already return the empty |
| 1118 | // string for it instead of recursively formatting it. |
| 1119 | if (isCyclicErrorMarker(e)) return ''; |
| 1120 | else throw e; |
| 1121 | } |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1124 | |
| 1125 | InstallFunctions($Error.prototype, DONT_ENUM, ['toString', errorToString]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1126 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1127 | // Boilerplate for exceptions for stack overflows. Used from |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1128 | // Isolate::StackOverflow(). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1129 | const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []); |