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