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