ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | |
| 29 | // ------------------------------------------------------------------- |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 30 | // |
| 31 | // Matches Script::Type from objects.h |
| 32 | var TYPE_NATIVE = 0; |
| 33 | var TYPE_EXTENSION = 1; |
| 34 | var TYPE_NORMAL = 2; |
| 35 | |
| 36 | // Matches Script::CompilationType from objects.h |
| 37 | var COMPILATION_TYPE_HOST = 0; |
| 38 | var COMPILATION_TYPE_EVAL = 1; |
| 39 | var COMPILATION_TYPE_JSON = 2; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 41 | // Lazily initialized. |
| 42 | var kVowelSounds = 0; |
| 43 | var kCapitalVowelSounds = 0; |
| 44 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 45 | // Matches Messages::kNoLineNumberInfo from v8.h |
| 46 | var kNoLineNumberInfo = 0; |
| 47 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 48 | // If this object gets passed to an error constructor the error will |
| 49 | // get an accessor for .message that constructs a descriptive error |
| 50 | // message on access. |
| 51 | var kAddMessageAccessorsMarker = { }; |
| 52 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 53 | |
| 54 | function GetInstanceName(cons) { |
| 55 | if (cons.length == 0) { |
| 56 | return ""; |
| 57 | } |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 58 | var first = %StringToLowerCase(StringCharAt.call(cons, 0)); |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 59 | if (kVowelSounds === 0) { |
| 60 | kVowelSounds = {a: true, e: true, i: true, o: true, u: true, y: true}; |
| 61 | kCapitalVowelSounds = {a: true, e: true, i: true, o: true, u: true, h: true, |
| 62 | f: true, l: true, m: true, n: true, r: true, s: true, x: true, y: true}; |
| 63 | } |
| 64 | var vowel_mapping = kVowelSounds; |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 65 | if (cons.length > 1 && (StringCharAt.call(cons, 0) != first)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 66 | // First char is upper case |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 67 | var second = %StringToLowerCase(StringCharAt.call(cons, 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 68 | // Second char is upper case |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 69 | if (StringCharAt.call(cons, 1) != second) { |
| 70 | vowel_mapping = kCapitalVowelSounds; |
| 71 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 72 | } |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 73 | var s = vowel_mapping[first] ? "an " : "a "; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 74 | return s + cons; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 75 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 76 | |
| 77 | |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 78 | var kMessages = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | function FormatString(format, args) { |
| 82 | var result = format; |
| 83 | for (var i = 0; i < args.length; i++) { |
| 84 | var str; |
| 85 | try { str = ToDetailString(args[i]); } |
| 86 | catch (e) { str = "#<error>"; } |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 87 | result = ArrayJoin.call(StringSplit.call(result, "%" + i), str); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 88 | } |
| 89 | return result; |
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 | |
| 93 | function ToDetailString(obj) { |
| 94 | if (obj != null && IS_OBJECT(obj) && obj.toString === $Object.prototype.toString) { |
| 95 | var constructor = obj.constructor; |
| 96 | if (!constructor) return ToString(obj); |
| 97 | var constructorName = constructor.name; |
| 98 | if (!constructorName) return ToString(obj); |
| 99 | return "#<" + GetInstanceName(constructorName) + ">"; |
| 100 | } else { |
| 101 | return ToString(obj); |
| 102 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 103 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 104 | |
| 105 | |
| 106 | function MakeGenericError(constructor, type, args) { |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 107 | if (IS_UNDEFINED(args)) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 108 | args = []; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 109 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 110 | var e = new constructor(kAddMessageAccessorsMarker); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 111 | e.type = type; |
| 112 | e.arguments = args; |
| 113 | return e; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 114 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 115 | |
| 116 | |
| 117 | /** |
| 118 | * Setup the Script function and constructor. |
| 119 | */ |
| 120 | %FunctionSetInstanceClassName(Script, 'Script'); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 121 | %SetProperty(Script.prototype, 'constructor', Script, DONT_ENUM); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 122 | %SetCode(Script, function(x) { |
| 123 | // Script objects can only be created by the VM. |
| 124 | throw new $Error("Not supported"); |
| 125 | }); |
| 126 | |
| 127 | |
| 128 | // Helper functions; called from the runtime system. |
| 129 | function FormatMessage(message) { |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 130 | if (kMessages === 0) { |
| 131 | kMessages = { |
| 132 | // Error |
| 133 | cyclic_proto: "Cyclic __proto__ value", |
| 134 | // TypeError |
| 135 | unexpected_token: "Unexpected token %0", |
| 136 | unexpected_token_number: "Unexpected number", |
| 137 | unexpected_token_string: "Unexpected string", |
| 138 | unexpected_token_identifier: "Unexpected identifier", |
| 139 | unexpected_eos: "Unexpected end of input", |
| 140 | malformed_regexp: "Invalid regular expression: /%0/: %1", |
| 141 | unterminated_regexp: "Invalid regular expression: missing /", |
| 142 | regexp_flags: "Cannot supply flags when constructing one RegExp from another", |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 143 | incompatible_method_receiver: "Method %0 called on incompatible receiver %1", |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 144 | invalid_lhs_in_assignment: "Invalid left-hand side in assignment", |
| 145 | invalid_lhs_in_for_in: "Invalid left-hand side in for-in", |
| 146 | invalid_lhs_in_postfix_op: "Invalid left-hand side expression in postfix operation", |
| 147 | invalid_lhs_in_prefix_op: "Invalid left-hand side expression in prefix operation", |
| 148 | multiple_defaults_in_switch: "More than one default clause in switch statement", |
| 149 | newline_after_throw: "Illegal newline after throw", |
| 150 | redeclaration: "%0 '%1' has already been declared", |
| 151 | no_catch_or_finally: "Missing catch or finally after try", |
| 152 | unknown_label: "Undefined label '%0'", |
| 153 | uncaught_exception: "Uncaught %0", |
| 154 | stack_trace: "Stack Trace:\n%0", |
| 155 | called_non_callable: "%0 is not a function", |
| 156 | undefined_method: "Object %1 has no method '%0'", |
| 157 | property_not_function: "Property '%0' of object %1 is not a function", |
| 158 | cannot_convert_to_primitive: "Cannot convert object to primitive value", |
| 159 | not_constructor: "%0 is not a constructor", |
| 160 | not_defined: "%0 is not defined", |
| 161 | non_object_property_load: "Cannot read property '%0' of %1", |
| 162 | non_object_property_store: "Cannot set property '%0' of %1", |
| 163 | non_object_property_call: "Cannot call method '%0' of %1", |
| 164 | with_expression: "%0 has no properties", |
| 165 | illegal_invocation: "Illegal invocation", |
| 166 | no_setter_in_callback: "Cannot set property %0 of %1 which has only a getter", |
| 167 | apply_non_function: "Function.prototype.apply was called on %0, which is a %1 and not a function", |
| 168 | apply_wrong_args: "Function.prototype.apply: Arguments list has wrong type", |
| 169 | invalid_in_operator_use: "Cannot use 'in' operator to search for '%0' in %1", |
| 170 | instanceof_function_expected: "Expecting a function in instanceof check, but got %0", |
| 171 | instanceof_nonobject_proto: "Function has non-object prototype '%0' in instanceof check", |
| 172 | null_to_object: "Cannot convert null to object", |
| 173 | reduce_no_initial: "Reduce of empty array with no initial value", |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 174 | getter_must_be_callable: "Getter must be a function: %0", |
| 175 | setter_must_be_callable: "Setter must be a function: %0", |
| 176 | value_and_accessor: "Invalid property. A property cannot both have accessors and be writable or have a value: %0", |
| 177 | proto_object_or_null: "Object prototype may only be an Object or null", |
| 178 | property_desc_object: "Property description must be an object: %0", |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 179 | redefine_disallowed: "Cannot redefine property: %0", |
| 180 | define_disallowed: "Cannot define property, object is not extensible: %0", |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 181 | // RangeError |
| 182 | invalid_array_length: "Invalid array length", |
| 183 | stack_overflow: "Maximum call stack size exceeded", |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 184 | // SyntaxError |
| 185 | unable_to_parse: "Parse error", |
| 186 | duplicate_regexp_flag: "Duplicate RegExp flag %0", |
| 187 | invalid_regexp: "Invalid RegExp pattern /%0/", |
| 188 | illegal_break: "Illegal break statement", |
| 189 | illegal_continue: "Illegal continue statement", |
| 190 | illegal_return: "Illegal return statement", |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 191 | error_loading_debugger: "Error loading debugger", |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 192 | no_input_to_regexp: "No input to %0", |
| 193 | result_not_primitive: "Result of %0 must be a primitive, was %1", |
| 194 | invalid_json: "String '%0' is not valid JSON", |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 195 | circular_structure: "Converting circular structure to JSON", |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 196 | obj_ctor_property_non_object: "Object.%0 called on non-object", |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 197 | array_indexof_not_defined: "Array.getIndexOf: Argument undefined", |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 198 | object_not_extensible: "Can't add property %0, object is not extensible", |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 199 | illegal_access: "Illegal access", |
| 200 | invalid_preparser_data: "Invalid preparser data for function %0" |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 201 | }; |
| 202 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 203 | var format = kMessages[message.type]; |
| 204 | if (!format) return "<unknown message " + message.type + ">"; |
| 205 | return FormatString(format, message.args); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 206 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 207 | |
| 208 | |
| 209 | function GetLineNumber(message) { |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 210 | if (message.startPos == -1) return kNoLineNumberInfo; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 211 | var location = message.script.locationFromPosition(message.startPos, true); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 212 | if (location == null) return kNoLineNumberInfo; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 213 | return location.line + 1; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 214 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 215 | |
| 216 | |
| 217 | // Returns the source code line containing the given source |
| 218 | // position, or the empty string if the position is invalid. |
| 219 | function GetSourceLine(message) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 220 | var location = message.script.locationFromPosition(message.startPos, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 221 | if (location == null) return ""; |
| 222 | location.restrict(); |
| 223 | return location.sourceText(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 224 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 225 | |
| 226 | |
| 227 | function MakeTypeError(type, args) { |
| 228 | return MakeGenericError($TypeError, type, args); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 229 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 230 | |
| 231 | |
| 232 | function MakeRangeError(type, args) { |
| 233 | return MakeGenericError($RangeError, type, args); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 234 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 235 | |
| 236 | |
| 237 | function MakeSyntaxError(type, args) { |
| 238 | return MakeGenericError($SyntaxError, type, args); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 239 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 240 | |
| 241 | |
| 242 | function MakeReferenceError(type, args) { |
| 243 | return MakeGenericError($ReferenceError, type, args); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 244 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 245 | |
| 246 | |
| 247 | function MakeEvalError(type, args) { |
| 248 | return MakeGenericError($EvalError, type, args); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 249 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 250 | |
| 251 | |
| 252 | function MakeError(type, args) { |
| 253 | return MakeGenericError($Error, type, args); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 254 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 255 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 256 | /** |
| 257 | * Find a line number given a specific source position. |
| 258 | * @param {number} position The source position. |
| 259 | * @return {number} 0 if input too small, -1 if input too large, |
| 260 | else the line number. |
| 261 | */ |
| 262 | Script.prototype.lineFromPosition = function(position) { |
| 263 | var lower = 0; |
| 264 | var upper = this.lineCount() - 1; |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 265 | var line_ends = this.line_ends; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 266 | |
| 267 | // We'll never find invalid positions so bail right away. |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 268 | if (position > line_ends[upper]) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 269 | return -1; |
| 270 | } |
| 271 | |
| 272 | // 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] | 273 | if (position <= line_ends[0]) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | // Binary search to find line # from position range. |
| 278 | while (upper >= 1) { |
| 279 | var i = (lower + upper) >> 1; |
| 280 | |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 281 | if (position > line_ends[i]) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 282 | lower = i + 1; |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 283 | } else if (position <= line_ends[i - 1]) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 284 | upper = i - 1; |
| 285 | } else { |
| 286 | return i; |
| 287 | } |
| 288 | } |
| 289 | return -1; |
| 290 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 291 | |
| 292 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 293 | * Get information on a specific source position. |
| 294 | * @param {number} position The source position |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 295 | * @param {boolean} include_resource_offset Set to true to have the resource |
| 296 | * offset added to the location |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 297 | * @return {SourceLocation} |
| 298 | * If line is negative or not in the source null is returned. |
| 299 | */ |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 300 | Script.prototype.locationFromPosition = function (position, |
| 301 | include_resource_offset) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 302 | var line = this.lineFromPosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 303 | if (line == -1) return null; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 304 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 305 | // Determine start, end and column. |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 306 | var line_ends = this.line_ends; |
| 307 | var start = line == 0 ? 0 : line_ends[line - 1] + 1; |
| 308 | var end = line_ends[line]; |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 309 | if (end > 0 && StringCharAt.call(this.source, end - 1) == '\r') end--; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 310 | var column = position - start; |
| 311 | |
| 312 | // Adjust according to the offset within the resource. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 313 | if (include_resource_offset) { |
| 314 | line += this.line_offset; |
| 315 | if (line == this.line_offset) { |
| 316 | column += this.column_offset; |
| 317 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | return new SourceLocation(this, position, line, column, start, end); |
| 321 | }; |
| 322 | |
| 323 | |
| 324 | /** |
| 325 | * Get information on a specific source line and column possibly offset by a |
| 326 | * fixed source position. This function is used to find a source position from |
| 327 | * a line and column position. The fixed source position offset is typically |
| 328 | * used to find a source position in a function based on a line and column in |
| 329 | * the source for the function alone. The offset passed will then be the |
| 330 | * start position of the source for the function within the full script source. |
| 331 | * @param {number} opt_line The line within the source. Default value is 0 |
| 332 | * @param {number} opt_column The column in within the line. Default value is 0 |
| 333 | * @param {number} opt_offset_position The offset from the begining of the |
| 334 | * source from where the line and column calculation starts. Default value is 0 |
| 335 | * @return {SourceLocation} |
| 336 | * If line is negative or not in the source null is returned. |
| 337 | */ |
| 338 | Script.prototype.locationFromLine = function (opt_line, opt_column, opt_offset_position) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 339 | // Default is the first line in the script. Lines in the script is relative |
| 340 | // to the offset within the resource. |
| 341 | var line = 0; |
| 342 | if (!IS_UNDEFINED(opt_line)) { |
| 343 | line = opt_line - this.line_offset; |
| 344 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 345 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 346 | // Default is first column. If on the first line add the offset within the |
| 347 | // resource. |
| 348 | var column = opt_column || 0; |
| 349 | if (line == 0) { |
| 350 | column -= this.column_offset |
| 351 | } |
| 352 | |
| 353 | var offset_position = opt_offset_position || 0; |
| 354 | if (line < 0 || column < 0 || offset_position < 0) return null; |
| 355 | if (line == 0) { |
ager@chromium.org | 3a6061e | 2009-03-12 14:24:36 +0000 | [diff] [blame] | 356 | return this.locationFromPosition(offset_position + column, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 357 | } else { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 358 | // Find the line where the offset position is located. |
| 359 | var offset_line = this.lineFromPosition(offset_position); |
| 360 | |
| 361 | if (offset_line == -1 || offset_line + line >= this.lineCount()) { |
| 362 | return null; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 363 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 364 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 365 | 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] | 366 | } |
| 367 | } |
| 368 | |
| 369 | |
| 370 | /** |
| 371 | * Get a slice of source code from the script. The boundaries for the slice is |
| 372 | * specified in lines. |
| 373 | * @param {number} opt_from_line The first line (zero bound) in the slice. |
| 374 | * Default is 0 |
| 375 | * @param {number} opt_to_column The last line (zero bound) in the slice (non |
| 376 | * inclusive). Default is the number of lines in the script |
| 377 | * @return {SourceSlice} The source slice or null of the parameters where |
| 378 | * invalid |
| 379 | */ |
| 380 | Script.prototype.sourceSlice = function (opt_from_line, opt_to_line) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 381 | var from_line = IS_UNDEFINED(opt_from_line) ? this.line_offset : opt_from_line; |
| 382 | var to_line = IS_UNDEFINED(opt_to_line) ? this.line_offset + this.lineCount() : opt_to_line |
| 383 | |
| 384 | // Adjust according to the offset within the resource. |
| 385 | from_line -= this.line_offset; |
| 386 | to_line -= this.line_offset; |
| 387 | if (from_line < 0) from_line = 0; |
| 388 | if (to_line > this.lineCount()) to_line = this.lineCount(); |
| 389 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 390 | // Check parameters. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 391 | if (from_line >= this.lineCount() || |
| 392 | to_line < 0 || |
| 393 | from_line > to_line) { |
| 394 | return null; |
| 395 | } |
| 396 | |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 397 | var line_ends = this.line_ends; |
| 398 | var from_position = from_line == 0 ? 0 : line_ends[from_line - 1] + 1; |
| 399 | 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] | 400 | |
| 401 | // Return a source slice with line numbers re-adjusted to the resource. |
| 402 | return new SourceSlice(this, from_line + this.line_offset, to_line + this.line_offset, |
| 403 | from_position, to_position); |
| 404 | } |
| 405 | |
| 406 | |
| 407 | Script.prototype.sourceLine = function (opt_line) { |
| 408 | // Default is the first line in the script. Lines in the script are relative |
| 409 | // to the offset within the resource. |
| 410 | var line = 0; |
| 411 | if (!IS_UNDEFINED(opt_line)) { |
| 412 | line = opt_line - this.line_offset; |
| 413 | } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 414 | |
| 415 | // Check parameter. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 416 | if (line < 0 || this.lineCount() <= line) { |
| 417 | return null; |
| 418 | } |
| 419 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 420 | // Return the source line. |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 421 | var line_ends = this.line_ends; |
| 422 | var start = line == 0 ? 0 : line_ends[line - 1] + 1; |
| 423 | var end = line_ends[line]; |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 424 | return StringSubstring.call(this.source, start, end); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | |
| 428 | /** |
| 429 | * Returns the number of source lines. |
| 430 | * @return {number} |
| 431 | * Number of source lines. |
| 432 | */ |
| 433 | Script.prototype.lineCount = function() { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 434 | // Return number of source lines. |
| 435 | return this.line_ends.length; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 436 | }; |
| 437 | |
| 438 | |
| 439 | /** |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 440 | * Returns the name of script if available, contents of sourceURL comment |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 441 | * otherwise. See |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 442 | * http://fbug.googlecode.com/svn/branches/firebug1.1/docs/ReleaseNotes_1.1.txt |
| 443 | * for details on using //@ sourceURL comment to identify scritps that don't |
| 444 | * have name. |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 445 | * |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 446 | * @return {?string} script name if present, value for //@ sourceURL comment |
| 447 | * otherwise. |
| 448 | */ |
| 449 | Script.prototype.nameOrSourceURL = function() { |
| 450 | if (this.name) |
| 451 | return this.name; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 452 | // 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] | 453 | // because this file is being processed by js2c whose handling of spaces |
| 454 | // in regexps is broken. Also, ['"] are excluded from allowed URLs to |
| 455 | // avoid matches against sources that invoke evals with sourceURL. |
| 456 | var sourceUrlPattern = |
| 457 | /\/\/@[\040\t]sourceURL=[\040\t]*([^\s'"]*)[\040\t]*$/m; |
| 458 | var match = sourceUrlPattern.exec(this.source); |
| 459 | return match ? match[1] : this.name; |
| 460 | } |
| 461 | |
| 462 | |
| 463 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 464 | * Class for source location. A source location is a position within some |
| 465 | * source with the following properties: |
| 466 | * script : script object for the source |
| 467 | * line : source line number |
| 468 | * column : source column within the line |
| 469 | * position : position within the source |
| 470 | * start : position of start of source context (inclusive) |
| 471 | * end : position of end of source context (not inclusive) |
| 472 | * Source text for the source context is the character interval [start, end[. In |
| 473 | * most cases end will point to a newline character. It might point just past |
| 474 | * 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] | 475 | * newline character. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 476 | * @param {Script} script The Script object for which this is a location |
| 477 | * @param {number} position Source position for the location |
| 478 | * @param {number} line The line number for the location |
| 479 | * @param {number} column The column within the line for the location |
| 480 | * @param {number} start Source position for start of source context |
| 481 | * @param {number} end Source position for end of source context |
| 482 | * @constructor |
| 483 | */ |
| 484 | function SourceLocation(script, position, line, column, start, end) { |
| 485 | this.script = script; |
| 486 | this.position = position; |
| 487 | this.line = line; |
| 488 | this.column = column; |
| 489 | this.start = start; |
| 490 | this.end = end; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 491 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 492 | |
| 493 | |
| 494 | const kLineLengthLimit = 78; |
| 495 | |
| 496 | /** |
| 497 | * Restrict source location start and end positions to make the source slice |
| 498 | * no more that a certain number of characters wide. |
| 499 | * @param {number} opt_limit The with limit of the source text with a default |
| 500 | * of 78 |
| 501 | * @param {number} opt_before The number of characters to prefer before the |
| 502 | * position with a default value of 10 less that the limit |
| 503 | */ |
| 504 | SourceLocation.prototype.restrict = function (opt_limit, opt_before) { |
| 505 | // Find the actual limit to use. |
| 506 | var limit; |
| 507 | var before; |
| 508 | if (!IS_UNDEFINED(opt_limit)) { |
| 509 | limit = opt_limit; |
| 510 | } else { |
| 511 | limit = kLineLengthLimit; |
| 512 | } |
| 513 | if (!IS_UNDEFINED(opt_before)) { |
| 514 | before = opt_before; |
| 515 | } else { |
| 516 | // If no before is specified center for small limits and perfer more source |
| 517 | // before the the position that after for longer limits. |
| 518 | if (limit <= 20) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 519 | before = $floor(limit / 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 520 | } else { |
| 521 | before = limit - 10; |
| 522 | } |
| 523 | } |
| 524 | if (before >= limit) { |
| 525 | before = limit - 1; |
| 526 | } |
| 527 | |
| 528 | // If the [start, end[ interval is too big we restrict |
| 529 | // it in one or both ends. We make sure to always produce |
| 530 | // restricted intervals of maximum allowed size. |
| 531 | if (this.end - this.start > limit) { |
| 532 | var start_limit = this.position - before; |
| 533 | var end_limit = this.position + limit - before; |
| 534 | if (this.start < start_limit && end_limit < this.end) { |
| 535 | this.start = start_limit; |
| 536 | this.end = end_limit; |
| 537 | } else if (this.start < start_limit) { |
| 538 | this.start = this.end - limit; |
| 539 | } else { |
| 540 | this.end = this.start + limit; |
| 541 | } |
| 542 | } |
| 543 | }; |
| 544 | |
| 545 | |
| 546 | /** |
| 547 | * Get the source text for a SourceLocation |
| 548 | * @return {String} |
| 549 | * Source text for this location. |
| 550 | */ |
| 551 | SourceLocation.prototype.sourceText = function () { |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 552 | return StringSubstring.call(this.script.source, this.start, this.end); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 553 | }; |
| 554 | |
| 555 | |
| 556 | /** |
| 557 | * Class for a source slice. A source slice is a part of a script source with |
| 558 | * the following properties: |
| 559 | * script : script object for the source |
| 560 | * from_line : line number for the first line in the slice |
| 561 | * to_line : source line number for the last line in the slice |
| 562 | * from_position : position of the first character in the slice |
| 563 | * to_position : position of the last character in the slice |
| 564 | * The to_line and to_position are not included in the slice, that is the lines |
| 565 | * in the slice are [from_line, to_line[. Likewise the characters in the slice |
| 566 | * are [from_position, to_position[. |
| 567 | * @param {Script} script The Script object for the source slice |
| 568 | * @param {number} from_line |
| 569 | * @param {number} to_line |
| 570 | * @param {number} from_position |
| 571 | * @param {number} to_position |
| 572 | * @constructor |
| 573 | */ |
| 574 | function SourceSlice(script, from_line, to_line, from_position, to_position) { |
| 575 | this.script = script; |
| 576 | this.from_line = from_line; |
| 577 | this.to_line = to_line; |
| 578 | this.from_position = from_position; |
| 579 | this.to_position = to_position; |
| 580 | } |
| 581 | |
| 582 | |
| 583 | /** |
| 584 | * Get the source text for a SourceSlice |
| 585 | * @return {String} Source text for this slice. The last line will include |
| 586 | * the line terminating characters (if any) |
| 587 | */ |
| 588 | SourceSlice.prototype.sourceText = function () { |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 589 | return StringSubstring.call(this.script.source, this.from_position, this.to_position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 590 | }; |
| 591 | |
| 592 | |
| 593 | // Returns the offset of the given position within the containing |
| 594 | // line. |
| 595 | function GetPositionInLine(message) { |
ager@chromium.org | 3a6061e | 2009-03-12 14:24:36 +0000 | [diff] [blame] | 596 | var location = message.script.locationFromPosition(message.startPos, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 597 | if (location == null) return -1; |
| 598 | location.restrict(); |
| 599 | return message.startPos - location.start; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 600 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 601 | |
| 602 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 603 | function ErrorMessage(type, args, startPos, endPos, script, stackTrace, |
| 604 | stackFrames) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 605 | this.startPos = startPos; |
| 606 | this.endPos = endPos; |
| 607 | this.type = type; |
| 608 | this.args = args; |
| 609 | this.script = script; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 610 | this.stackTrace = stackTrace; |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 611 | this.stackFrames = stackFrames; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 612 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 613 | |
| 614 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 615 | function MakeMessage(type, args, startPos, endPos, script, stackTrace, |
| 616 | stackFrames) { |
| 617 | return new ErrorMessage(type, args, startPos, endPos, script, stackTrace, |
| 618 | stackFrames); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 619 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 620 | |
| 621 | |
| 622 | function GetStackTraceLine(recv, fun, pos, isGlobal) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 623 | return FormatSourcePosition(new CallSite(recv, fun, pos)); |
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 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 626 | // ---------------------------------------------------------------------------- |
| 627 | // Error implementation |
| 628 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 629 | // Defines accessors for a property that is calculated the first time |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 630 | // the property is read. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 631 | function DefineOneShotAccessor(obj, name, fun) { |
| 632 | // Note that the accessors consistently operate on 'obj', not 'this'. |
| 633 | // Since the object may occur in someone else's prototype chain we |
| 634 | // can't rely on 'this' being the same as 'obj'. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 635 | var hasBeenSet = false; |
| 636 | var value; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 637 | obj.__defineGetter__(name, function () { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 638 | if (hasBeenSet) { |
| 639 | return value; |
| 640 | } |
| 641 | hasBeenSet = true; |
| 642 | value = fun(obj); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 643 | return value; |
| 644 | }); |
| 645 | obj.__defineSetter__(name, function (v) { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 646 | hasBeenSet = true; |
| 647 | value = v; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 648 | }); |
| 649 | } |
| 650 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 651 | function CallSite(receiver, fun, pos) { |
| 652 | this.receiver = receiver; |
| 653 | this.fun = fun; |
| 654 | this.pos = pos; |
| 655 | } |
| 656 | |
| 657 | CallSite.prototype.getThis = function () { |
| 658 | return this.receiver; |
| 659 | }; |
| 660 | |
| 661 | CallSite.prototype.getTypeName = function () { |
| 662 | var constructor = this.receiver.constructor; |
| 663 | if (!constructor) |
| 664 | return $Object.prototype.toString.call(this.receiver); |
| 665 | var constructorName = constructor.name; |
| 666 | if (!constructorName) |
| 667 | return $Object.prototype.toString.call(this.receiver); |
| 668 | return constructorName; |
| 669 | }; |
| 670 | |
| 671 | CallSite.prototype.isToplevel = function () { |
| 672 | if (this.receiver == null) |
| 673 | return true; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 674 | return IS_GLOBAL(this.receiver); |
| 675 | }; |
| 676 | |
| 677 | CallSite.prototype.isEval = function () { |
| 678 | var script = %FunctionGetScript(this.fun); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 679 | return script && script.compilation_type == COMPILATION_TYPE_EVAL; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 680 | }; |
| 681 | |
| 682 | CallSite.prototype.getEvalOrigin = function () { |
| 683 | var script = %FunctionGetScript(this.fun); |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 684 | return FormatEvalOrigin(script); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 685 | }; |
| 686 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 687 | CallSite.prototype.getScriptNameOrSourceURL = function () { |
| 688 | var script = %FunctionGetScript(this.fun); |
| 689 | return script ? script.nameOrSourceURL() : null; |
| 690 | }; |
| 691 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 692 | CallSite.prototype.getFunction = function () { |
| 693 | return this.fun; |
| 694 | }; |
| 695 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 696 | CallSite.prototype.getFunctionName = function () { |
| 697 | // See if the function knows its own name |
| 698 | var name = this.fun.name; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 699 | if (name) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 700 | return name; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 701 | } else { |
| 702 | return %FunctionGetInferredName(this.fun); |
| 703 | } |
| 704 | // Maybe this is an evaluation? |
| 705 | var script = %FunctionGetScript(this.fun); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 706 | if (script && script.compilation_type == COMPILATION_TYPE_EVAL) |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 707 | return "eval"; |
| 708 | return null; |
| 709 | }; |
| 710 | |
| 711 | CallSite.prototype.getMethodName = function () { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 712 | // See if we can find a unique property on the receiver that holds |
| 713 | // this function. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 714 | var ownName = this.fun.name; |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 715 | if (ownName && this.receiver && |
| 716 | (ObjectLookupGetter.call(this.receiver, ownName) === this.fun || |
| 717 | ObjectLookupSetter.call(this.receiver, ownName) === this.fun || |
| 718 | this.receiver[ownName] === this.fun)) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 719 | // To handle DontEnum properties we guess that the method has |
| 720 | // the same name as the function. |
| 721 | return ownName; |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 722 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 723 | var name = null; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 724 | for (var prop in this.receiver) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 725 | if (this.receiver.__lookupGetter__(prop) === this.fun || |
| 726 | this.receiver.__lookupSetter__(prop) === this.fun || |
| 727 | (!this.receiver.__lookupGetter__(prop) && this.receiver[prop] === this.fun)) { |
| 728 | // If we find more than one match bail out to avoid confusion. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 729 | if (name) |
| 730 | return null; |
| 731 | name = prop; |
| 732 | } |
| 733 | } |
| 734 | if (name) |
| 735 | return name; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 736 | return null; |
| 737 | }; |
| 738 | |
| 739 | CallSite.prototype.getFileName = function () { |
| 740 | var script = %FunctionGetScript(this.fun); |
| 741 | return script ? script.name : null; |
| 742 | }; |
| 743 | |
| 744 | CallSite.prototype.getLineNumber = function () { |
| 745 | if (this.pos == -1) |
| 746 | return null; |
| 747 | var script = %FunctionGetScript(this.fun); |
| 748 | var location = null; |
| 749 | if (script) { |
| 750 | location = script.locationFromPosition(this.pos, true); |
| 751 | } |
| 752 | return location ? location.line + 1 : null; |
| 753 | }; |
| 754 | |
| 755 | CallSite.prototype.getColumnNumber = function () { |
| 756 | if (this.pos == -1) |
| 757 | return null; |
| 758 | var script = %FunctionGetScript(this.fun); |
| 759 | var location = null; |
| 760 | if (script) { |
| 761 | location = script.locationFromPosition(this.pos, true); |
| 762 | } |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 763 | return location ? location.column + 1: null; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 764 | }; |
| 765 | |
| 766 | CallSite.prototype.isNative = function () { |
| 767 | var script = %FunctionGetScript(this.fun); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 768 | return script ? (script.type == TYPE_NATIVE) : false; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 769 | }; |
| 770 | |
| 771 | CallSite.prototype.getPosition = function () { |
| 772 | return this.pos; |
| 773 | }; |
| 774 | |
| 775 | CallSite.prototype.isConstructor = function () { |
| 776 | var constructor = this.receiver ? this.receiver.constructor : null; |
| 777 | if (!constructor) |
| 778 | return false; |
| 779 | return this.fun === constructor; |
| 780 | }; |
| 781 | |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 782 | function FormatEvalOrigin(script) { |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 783 | var sourceURL = script.nameOrSourceURL(); |
| 784 | if (sourceURL) |
| 785 | return sourceURL; |
| 786 | |
| 787 | var eval_origin = "eval at "; |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 788 | if (script.eval_from_function_name) { |
| 789 | eval_origin += script.eval_from_function_name; |
| 790 | } else { |
| 791 | eval_origin += "<anonymous>"; |
| 792 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 793 | |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 794 | var eval_from_script = script.eval_from_script; |
| 795 | if (eval_from_script) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 796 | if (eval_from_script.compilation_type == COMPILATION_TYPE_EVAL) { |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 797 | // eval script originated from another eval. |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 798 | eval_origin += " (" + FormatEvalOrigin(eval_from_script) + ")"; |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 799 | } else { |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 800 | // eval script originated from "real" source. |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 801 | if (eval_from_script.name) { |
| 802 | eval_origin += " (" + eval_from_script.name; |
| 803 | var location = eval_from_script.locationFromPosition(script.eval_from_script_position, true); |
| 804 | if (location) { |
| 805 | eval_origin += ":" + (location.line + 1); |
| 806 | eval_origin += ":" + (location.column + 1); |
| 807 | } |
| 808 | eval_origin += ")" |
| 809 | } else { |
| 810 | eval_origin += " (unknown source)"; |
| 811 | } |
| 812 | } |
| 813 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 814 | |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 815 | return eval_origin; |
| 816 | }; |
| 817 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 818 | function FormatSourcePosition(frame) { |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 819 | var fileName; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 820 | var fileLocation = ""; |
| 821 | if (frame.isNative()) { |
| 822 | fileLocation = "native"; |
| 823 | } else if (frame.isEval()) { |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 824 | fileName = frame.getScriptNameOrSourceURL(); |
| 825 | if (!fileName) |
| 826 | fileLocation = frame.getEvalOrigin(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 827 | } else { |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 828 | fileName = frame.getFileName(); |
| 829 | } |
| 830 | |
| 831 | if (fileName) { |
| 832 | fileLocation += fileName; |
| 833 | var lineNumber = frame.getLineNumber(); |
| 834 | if (lineNumber != null) { |
| 835 | fileLocation += ":" + lineNumber; |
| 836 | var columnNumber = frame.getColumnNumber(); |
| 837 | if (columnNumber) { |
| 838 | fileLocation += ":" + columnNumber; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | } |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 842 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 843 | if (!fileLocation) { |
| 844 | fileLocation = "unknown source"; |
| 845 | } |
| 846 | var line = ""; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 847 | var functionName = frame.getFunction().name; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 848 | var addPrefix = true; |
| 849 | var isConstructor = frame.isConstructor(); |
| 850 | var isMethodCall = !(frame.isToplevel() || isConstructor); |
| 851 | if (isMethodCall) { |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 852 | var methodName = frame.getMethodName(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 853 | line += frame.getTypeName() + "."; |
| 854 | if (functionName) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 855 | line += functionName; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 856 | if (methodName && (methodName != functionName)) { |
| 857 | line += " [as " + methodName + "]"; |
| 858 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 859 | } else { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 860 | line += methodName || "<anonymous>"; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 861 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 862 | } else if (isConstructor) { |
| 863 | line += "new " + (functionName || "<anonymous>"); |
| 864 | } else if (functionName) { |
| 865 | line += functionName; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 866 | } else { |
| 867 | line += fileLocation; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 868 | addPrefix = false; |
| 869 | } |
| 870 | if (addPrefix) { |
| 871 | line += " (" + fileLocation + ")"; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 872 | } |
| 873 | return line; |
| 874 | } |
| 875 | |
| 876 | function FormatStackTrace(error, frames) { |
| 877 | var lines = []; |
| 878 | try { |
| 879 | lines.push(error.toString()); |
| 880 | } catch (e) { |
| 881 | try { |
| 882 | lines.push("<error: " + e + ">"); |
| 883 | } catch (ee) { |
| 884 | lines.push("<error>"); |
| 885 | } |
| 886 | } |
| 887 | for (var i = 0; i < frames.length; i++) { |
| 888 | var frame = frames[i]; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 889 | var line; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 890 | try { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 891 | line = FormatSourcePosition(frame); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 892 | } catch (e) { |
| 893 | try { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 894 | line = "<error: " + e + ">"; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 895 | } catch (ee) { |
| 896 | // Any code that reaches this point is seriously nasty! |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 897 | line = "<error>"; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | lines.push(" at " + line); |
| 901 | } |
| 902 | return lines.join("\n"); |
| 903 | } |
| 904 | |
| 905 | function FormatRawStackTrace(error, raw_stack) { |
| 906 | var frames = [ ]; |
| 907 | for (var i = 0; i < raw_stack.length; i += 3) { |
| 908 | var recv = raw_stack[i]; |
| 909 | var fun = raw_stack[i+1]; |
| 910 | var pc = raw_stack[i+2]; |
| 911 | var pos = %FunctionGetPositionForOffset(fun, pc); |
| 912 | frames.push(new CallSite(recv, fun, pos)); |
| 913 | } |
| 914 | if (IS_FUNCTION($Error.prepareStackTrace)) { |
| 915 | return $Error.prepareStackTrace(error, frames); |
| 916 | } else { |
| 917 | return FormatStackTrace(error, frames); |
| 918 | } |
| 919 | } |
| 920 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 921 | function DefineError(f) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 922 | // Store the error function in both the global object |
| 923 | // and the runtime object. The function is fetched |
| 924 | // from the runtime object when throwing errors from |
| 925 | // within the runtime system to avoid strange side |
| 926 | // effects when overwriting the error functions from |
| 927 | // user code. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 928 | var name = f.name; |
| 929 | %SetProperty(global, name, f, DONT_ENUM); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 930 | this['$' + name] = f; |
| 931 | // Configure the error function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 932 | if (name == 'Error') { |
| 933 | // The prototype of the Error object must itself be an error. |
| 934 | // However, it can't be an instance of the Error object because |
| 935 | // it hasn't been properly configured yet. Instead we create a |
| 936 | // special not-a-true-error-but-close-enough object. |
| 937 | function ErrorPrototype() {} |
| 938 | %FunctionSetPrototype(ErrorPrototype, $Object.prototype); |
| 939 | %FunctionSetInstanceClassName(ErrorPrototype, 'Error'); |
| 940 | %FunctionSetPrototype(f, new ErrorPrototype()); |
| 941 | } else { |
| 942 | %FunctionSetPrototype(f, new $Error()); |
| 943 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 944 | %FunctionSetInstanceClassName(f, 'Error'); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 945 | %SetProperty(f.prototype, 'constructor', f, DONT_ENUM); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 946 | f.prototype.name = name; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 947 | %SetCode(f, function(m) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 948 | if (%_IsConstructCall()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 949 | if (m === kAddMessageAccessorsMarker) { |
| 950 | DefineOneShotAccessor(this, 'message', function (obj) { |
| 951 | return FormatMessage({type: obj.type, args: obj.arguments}); |
| 952 | }); |
| 953 | } else if (!IS_UNDEFINED(m)) { |
| 954 | this.message = ToString(m); |
| 955 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 956 | captureStackTrace(this, f); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 957 | } else { |
| 958 | return new f(m); |
| 959 | } |
| 960 | }); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 961 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 962 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 963 | function captureStackTrace(obj, cons_opt) { |
| 964 | var stackTraceLimit = $Error.stackTraceLimit; |
| 965 | if (!stackTraceLimit) return; |
| 966 | if (stackTraceLimit < 0 || stackTraceLimit > 10000) |
| 967 | stackTraceLimit = 10000; |
| 968 | var raw_stack = %CollectStackTrace(cons_opt ? cons_opt : captureStackTrace, |
| 969 | stackTraceLimit); |
| 970 | DefineOneShotAccessor(obj, 'stack', function (obj) { |
| 971 | return FormatRawStackTrace(obj, raw_stack); |
| 972 | }); |
| 973 | }; |
| 974 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 975 | $Math.__proto__ = global.Object.prototype; |
| 976 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 977 | DefineError(function Error() { }); |
| 978 | DefineError(function TypeError() { }); |
| 979 | DefineError(function RangeError() { }); |
| 980 | DefineError(function SyntaxError() { }); |
| 981 | DefineError(function ReferenceError() { }); |
| 982 | DefineError(function EvalError() { }); |
| 983 | DefineError(function URIError() { }); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 984 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 985 | $Error.captureStackTrace = captureStackTrace; |
| 986 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 987 | // Setup extra properties of the Error.prototype object. |
| 988 | $Error.prototype.message = ''; |
| 989 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 990 | %SetProperty($Error.prototype, 'toString', function toString() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 991 | var type = this.type; |
| 992 | if (type && !this.hasOwnProperty("message")) { |
| 993 | return this.name + ": " + FormatMessage({ type: type, args: this.arguments }); |
| 994 | } |
| 995 | var message = this.message; |
| 996 | return this.name + (message ? (": " + message) : ""); |
| 997 | }, DONT_ENUM); |
| 998 | |
| 999 | |
| 1000 | // Boilerplate for exceptions for stack overflows. Used from |
| 1001 | // Top::StackOverflow(). |
| 1002 | const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []); |