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 | // ------------------------------------------------------------------- |
| 30 | |
| 31 | const kVowelSounds = {a: true, e: true, i: true, o: true, u: true, y: true}; |
| 32 | const kCapitalVowelSounds = {a: true, e: true, i: true, o: true, u: true, |
| 33 | h: true, f: true, l: true, m: true, n: true, r: true, s: true, x: true, |
| 34 | y: true}; |
| 35 | |
| 36 | function GetInstanceName(cons) { |
| 37 | if (cons.length == 0) { |
| 38 | return ""; |
| 39 | } |
| 40 | var first = cons.charAt(0).toLowerCase(); |
| 41 | var mapping = kVowelSounds; |
| 42 | if (cons.length > 1 && (cons.charAt(0) != first)) { |
| 43 | // First char is upper case |
| 44 | var second = cons.charAt(1).toLowerCase(); |
| 45 | // Second char is upper case |
| 46 | if (cons.charAt(1) != second) |
| 47 | mapping = kCapitalVowelSounds; |
| 48 | } |
| 49 | var s = mapping[first] ? "an " : "a "; |
| 50 | return s + cons; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 51 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 52 | |
| 53 | |
| 54 | const kMessages = { |
| 55 | // Error |
| 56 | cyclic_proto: "Cyclic __proto__ value", |
| 57 | // TypeError |
| 58 | unexpected_token: "Unexpected token %0", |
| 59 | unexpected_token_number: "Unexpected number", |
| 60 | unexpected_token_string: "Unexpected string", |
| 61 | unexpected_token_identifier: "Unexpected identifier", |
| 62 | unexpected_eos: "Unexpected end of input", |
| 63 | expected_label: "Expected label", |
| 64 | malformed_regexp: "Invalid regular expression: /%0/: %1", |
| 65 | unterminated_regexp: "Invalid regular expression: missing /", |
| 66 | pcre_error: "PCRE function %0, error code %1", |
| 67 | regexp_flags: "Cannot supply flags when constructing one RegExp from another", |
| 68 | invalid_lhs_in_assignment: "Invalid left-hand side in assignment", |
| 69 | invalid_lhs_in_for_in: "Invalid left-hand side in for-in", |
| 70 | invalid_lhs_in_postfix_op: "Invalid left-hand side expression in postfix operation", |
| 71 | invalid_lhs_in_prefix_op: "Invalid left-hand side expression in prefix operation", |
| 72 | multiple_defaults_in_switch: "More than one default clause in switch statement", |
| 73 | newline_after_throw: "Illegal newline after throw", |
| 74 | redeclaration: "%0 '%1' has already been declared", |
| 75 | no_catch_or_finally: "Missing catch or finally after try", |
| 76 | unknown_label: "Undefined label '%0'", |
| 77 | invalid_break: "Invalid break statement", |
| 78 | invalid_continue: "Invalid continue statement", |
| 79 | uncaught_exception: "Uncaught %0", |
| 80 | stack_trace: "Stack Trace:\n%0", |
| 81 | called_non_callable: "%0 is not a function", |
| 82 | undefined_method: "Object %1 has no method '%0'", |
| 83 | property_not_function: "Property '%0' of object %1 is not a function", |
| 84 | null_or_undefined: "Cannot access property of null or undefined", |
| 85 | cannot_convert_to_primitive: "Cannot convert object to primitive value", |
| 86 | not_constructor: "%0 is not a constructor", |
| 87 | not_defined: "%0 is not defined", |
| 88 | non_object_property_load: "Cannot read property '%0' of %1", |
| 89 | non_object_property_store: "Cannot set property '%0' of %1", |
| 90 | non_object_property_call: "Cannot call method '%0' of %1", |
| 91 | illegal_eval: "Unsupported indirect eval() call", |
| 92 | with_expression: "%0 has no properties", |
| 93 | illegal_invocation: "Illegal invocation", |
| 94 | no_setter_in_callback: "Cannot set property %0 of %1 which has only a getter", |
| 95 | apply_non_function: "Function.prototype.apply was called on %0, which is a %1 and not a function", |
| 96 | apply_wrong_args: "Function.prototype.apply: Arguments list has wrong type", |
| 97 | invalid_in_operator_use: "Cannot use 'in' operator to search for '%0' in %1", |
| 98 | instanceof_function_expected: "Expecting a function in instanceof check, but got %0", |
| 99 | instanceof_nonobject_proto: "Function has non-object prototype '%0' in instanceof check", |
| 100 | null_to_object: "Cannot convert null to object", |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 101 | reduce_no_initial: "Reduce of empty array with no initial value", |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 102 | // RangeError |
| 103 | invalid_array_length: "Invalid array length", |
| 104 | invalid_array_apply_length: "Function.prototype.apply supports only up to 1024 arguments", |
| 105 | stack_overflow: "Maximum call stack size exceeded", |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 106 | apply_overflow: "Function.prototype.apply cannot support %0 arguments", |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 107 | // SyntaxError |
| 108 | unable_to_parse: "Parse error", |
| 109 | duplicate_regexp_flag: "Duplicate RegExp flag %0", |
| 110 | unrecognized_regexp_flag: "Unrecognized RegExp flag %0", |
| 111 | invalid_regexp: "Invalid RegExp pattern /%0/", |
| 112 | illegal_break: "Illegal break statement", |
| 113 | illegal_continue: "Illegal continue statement", |
| 114 | illegal_return: "Illegal return statement", |
| 115 | error_loading_debugger: "Error loading debugger %0", |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 116 | no_input_to_regexp: "No input to %0", |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 117 | result_not_primitive: "Result of %0 must be a primitive, was %1", |
| 118 | invalid_json: "String '%0' is not valid JSON", |
| 119 | circular_structure: "Converting circular structure to JSON" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | |
| 123 | function FormatString(format, args) { |
| 124 | var result = format; |
| 125 | for (var i = 0; i < args.length; i++) { |
| 126 | var str; |
| 127 | try { str = ToDetailString(args[i]); } |
| 128 | catch (e) { str = "#<error>"; } |
| 129 | result = result.split("%" + i).join(str); |
| 130 | } |
| 131 | return result; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 132 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 133 | |
| 134 | |
| 135 | function ToDetailString(obj) { |
| 136 | if (obj != null && IS_OBJECT(obj) && obj.toString === $Object.prototype.toString) { |
| 137 | var constructor = obj.constructor; |
| 138 | if (!constructor) return ToString(obj); |
| 139 | var constructorName = constructor.name; |
| 140 | if (!constructorName) return ToString(obj); |
| 141 | return "#<" + GetInstanceName(constructorName) + ">"; |
| 142 | } else { |
| 143 | return ToString(obj); |
| 144 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 145 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 146 | |
| 147 | |
| 148 | function MakeGenericError(constructor, type, args) { |
| 149 | if (args instanceof $Array) { |
| 150 | for (var i = 0; i < args.length; i++) { |
| 151 | var elem = args[i]; |
| 152 | if (elem instanceof $Array && elem.length > 100) { // arbitrary limit, grab a reasonable slice to report |
| 153 | args[i] = elem.slice(0,20).concat("..."); |
| 154 | } |
| 155 | } |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 156 | } else if (IS_UNDEFINED(args)) { |
| 157 | args = []; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 158 | } |
| 159 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 160 | var e = new constructor(kAddMessageAccessorsMarker); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 161 | e.type = type; |
| 162 | e.arguments = args; |
| 163 | return e; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 164 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 165 | |
| 166 | |
| 167 | /** |
| 168 | * Setup the Script function and constructor. |
| 169 | */ |
| 170 | %FunctionSetInstanceClassName(Script, 'Script'); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 171 | %SetProperty(Script.prototype, 'constructor', Script, DONT_ENUM); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 172 | %SetCode(Script, function(x) { |
| 173 | // Script objects can only be created by the VM. |
| 174 | throw new $Error("Not supported"); |
| 175 | }); |
| 176 | |
| 177 | |
| 178 | // Helper functions; called from the runtime system. |
| 179 | function FormatMessage(message) { |
| 180 | var format = kMessages[message.type]; |
| 181 | if (!format) return "<unknown message " + message.type + ">"; |
| 182 | return FormatString(format, message.args); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 183 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 184 | |
| 185 | |
| 186 | function GetLineNumber(message) { |
| 187 | if (message.startPos == -1) return -1; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 188 | var location = message.script.locationFromPosition(message.startPos, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 189 | if (location == null) return -1; |
| 190 | return location.line + 1; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 191 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 192 | |
| 193 | |
| 194 | // Returns the source code line containing the given source |
| 195 | // position, or the empty string if the position is invalid. |
| 196 | function GetSourceLine(message) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 197 | var location = message.script.locationFromPosition(message.startPos, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 198 | if (location == null) return ""; |
| 199 | location.restrict(); |
| 200 | return location.sourceText(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 201 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 202 | |
| 203 | |
| 204 | function MakeTypeError(type, args) { |
| 205 | return MakeGenericError($TypeError, type, 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 MakeRangeError(type, args) { |
| 210 | return MakeGenericError($RangeError, type, args); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 211 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 212 | |
| 213 | |
| 214 | function MakeSyntaxError(type, args) { |
| 215 | return MakeGenericError($SyntaxError, type, args); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 216 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 217 | |
| 218 | |
| 219 | function MakeReferenceError(type, args) { |
| 220 | return MakeGenericError($ReferenceError, type, args); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 221 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 222 | |
| 223 | |
| 224 | function MakeEvalError(type, args) { |
| 225 | return MakeGenericError($EvalError, type, args); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 226 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 227 | |
| 228 | |
| 229 | function MakeError(type, args) { |
| 230 | return MakeGenericError($Error, type, args); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 231 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 232 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 233 | /** |
| 234 | * Find a line number given a specific source position. |
| 235 | * @param {number} position The source position. |
| 236 | * @return {number} 0 if input too small, -1 if input too large, |
| 237 | else the line number. |
| 238 | */ |
| 239 | Script.prototype.lineFromPosition = function(position) { |
| 240 | var lower = 0; |
| 241 | var upper = this.lineCount() - 1; |
| 242 | |
| 243 | // We'll never find invalid positions so bail right away. |
| 244 | if (position > this.line_ends[upper]) { |
| 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | // This means we don't have to safe-guard indexing line_ends[i - 1]. |
| 249 | if (position <= this.line_ends[0]) { |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | // Binary search to find line # from position range. |
| 254 | while (upper >= 1) { |
| 255 | var i = (lower + upper) >> 1; |
| 256 | |
| 257 | if (position > this.line_ends[i]) { |
| 258 | lower = i + 1; |
| 259 | } else if (position <= this.line_ends[i - 1]) { |
| 260 | upper = i - 1; |
| 261 | } else { |
| 262 | return i; |
| 263 | } |
| 264 | } |
| 265 | return -1; |
| 266 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 267 | |
| 268 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 269 | * Get information on a specific source position. |
| 270 | * @param {number} position The source position |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 271 | * @param {boolean} include_resource_offset Set to true to have the resource |
| 272 | * offset added to the location |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 273 | * @return {SourceLocation} |
| 274 | * If line is negative or not in the source null is returned. |
| 275 | */ |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 276 | Script.prototype.locationFromPosition = function (position, |
| 277 | include_resource_offset) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 278 | var line = this.lineFromPosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 279 | if (line == -1) return null; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 280 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 281 | // Determine start, end and column. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 282 | var start = line == 0 ? 0 : this.line_ends[line - 1] + 1; |
| 283 | var end = this.line_ends[line]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 284 | if (end > 0 && this.source.charAt(end - 1) == '\r') end--; |
| 285 | var column = position - start; |
| 286 | |
| 287 | // Adjust according to the offset within the resource. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 288 | if (include_resource_offset) { |
| 289 | line += this.line_offset; |
| 290 | if (line == this.line_offset) { |
| 291 | column += this.column_offset; |
| 292 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | return new SourceLocation(this, position, line, column, start, end); |
| 296 | }; |
| 297 | |
| 298 | |
| 299 | /** |
| 300 | * Get information on a specific source line and column possibly offset by a |
| 301 | * fixed source position. This function is used to find a source position from |
| 302 | * a line and column position. The fixed source position offset is typically |
| 303 | * used to find a source position in a function based on a line and column in |
| 304 | * the source for the function alone. The offset passed will then be the |
| 305 | * start position of the source for the function within the full script source. |
| 306 | * @param {number} opt_line The line within the source. Default value is 0 |
| 307 | * @param {number} opt_column The column in within the line. Default value is 0 |
| 308 | * @param {number} opt_offset_position The offset from the begining of the |
| 309 | * source from where the line and column calculation starts. Default value is 0 |
| 310 | * @return {SourceLocation} |
| 311 | * If line is negative or not in the source null is returned. |
| 312 | */ |
| 313 | Script.prototype.locationFromLine = function (opt_line, opt_column, opt_offset_position) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 314 | // Default is the first line in the script. Lines in the script is relative |
| 315 | // to the offset within the resource. |
| 316 | var line = 0; |
| 317 | if (!IS_UNDEFINED(opt_line)) { |
| 318 | line = opt_line - this.line_offset; |
| 319 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 320 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 321 | // Default is first column. If on the first line add the offset within the |
| 322 | // resource. |
| 323 | var column = opt_column || 0; |
| 324 | if (line == 0) { |
| 325 | column -= this.column_offset |
| 326 | } |
| 327 | |
| 328 | var offset_position = opt_offset_position || 0; |
| 329 | if (line < 0 || column < 0 || offset_position < 0) return null; |
| 330 | if (line == 0) { |
ager@chromium.org | 3a6061e | 2009-03-12 14:24:36 +0000 | [diff] [blame] | 331 | return this.locationFromPosition(offset_position + column, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 332 | } else { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 333 | // Find the line where the offset position is located. |
| 334 | var offset_line = this.lineFromPosition(offset_position); |
| 335 | |
| 336 | if (offset_line == -1 || offset_line + line >= this.lineCount()) { |
| 337 | return null; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 338 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 339 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 340 | 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] | 341 | } |
| 342 | } |
| 343 | |
| 344 | |
| 345 | /** |
| 346 | * Get a slice of source code from the script. The boundaries for the slice is |
| 347 | * specified in lines. |
| 348 | * @param {number} opt_from_line The first line (zero bound) in the slice. |
| 349 | * Default is 0 |
| 350 | * @param {number} opt_to_column The last line (zero bound) in the slice (non |
| 351 | * inclusive). Default is the number of lines in the script |
| 352 | * @return {SourceSlice} The source slice or null of the parameters where |
| 353 | * invalid |
| 354 | */ |
| 355 | Script.prototype.sourceSlice = function (opt_from_line, opt_to_line) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 356 | var from_line = IS_UNDEFINED(opt_from_line) ? this.line_offset : opt_from_line; |
| 357 | var to_line = IS_UNDEFINED(opt_to_line) ? this.line_offset + this.lineCount() : opt_to_line |
| 358 | |
| 359 | // Adjust according to the offset within the resource. |
| 360 | from_line -= this.line_offset; |
| 361 | to_line -= this.line_offset; |
| 362 | if (from_line < 0) from_line = 0; |
| 363 | if (to_line > this.lineCount()) to_line = this.lineCount(); |
| 364 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 365 | // Check parameters. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 366 | if (from_line >= this.lineCount() || |
| 367 | to_line < 0 || |
| 368 | from_line > to_line) { |
| 369 | return null; |
| 370 | } |
| 371 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 372 | var from_position = from_line == 0 ? 0 : this.line_ends[from_line - 1] + 1; |
| 373 | var to_position = to_line == 0 ? 0 : this.line_ends[to_line - 1] + 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 374 | |
| 375 | // Return a source slice with line numbers re-adjusted to the resource. |
| 376 | return new SourceSlice(this, from_line + this.line_offset, to_line + this.line_offset, |
| 377 | from_position, to_position); |
| 378 | } |
| 379 | |
| 380 | |
| 381 | Script.prototype.sourceLine = function (opt_line) { |
| 382 | // Default is the first line in the script. Lines in the script are relative |
| 383 | // to the offset within the resource. |
| 384 | var line = 0; |
| 385 | if (!IS_UNDEFINED(opt_line)) { |
| 386 | line = opt_line - this.line_offset; |
| 387 | } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 388 | |
| 389 | // Check parameter. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 390 | if (line < 0 || this.lineCount() <= line) { |
| 391 | return null; |
| 392 | } |
| 393 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 394 | // Return the source line. |
| 395 | var start = line == 0 ? 0 : this.line_ends[line - 1] + 1; |
| 396 | var end = this.line_ends[line]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 397 | return this.source.substring(start, end); |
| 398 | } |
| 399 | |
| 400 | |
| 401 | /** |
| 402 | * Returns the number of source lines. |
| 403 | * @return {number} |
| 404 | * Number of source lines. |
| 405 | */ |
| 406 | Script.prototype.lineCount = function() { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 407 | // Return number of source lines. |
| 408 | return this.line_ends.length; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 409 | }; |
| 410 | |
| 411 | |
| 412 | /** |
| 413 | * Class for source location. A source location is a position within some |
| 414 | * source with the following properties: |
| 415 | * script : script object for the source |
| 416 | * line : source line number |
| 417 | * column : source column within the line |
| 418 | * position : position within the source |
| 419 | * start : position of start of source context (inclusive) |
| 420 | * end : position of end of source context (not inclusive) |
| 421 | * Source text for the source context is the character interval [start, end[. In |
| 422 | * most cases end will point to a newline character. It might point just past |
| 423 | * 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] | 424 | * newline character. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 425 | * @param {Script} script The Script object for which this is a location |
| 426 | * @param {number} position Source position for the location |
| 427 | * @param {number} line The line number for the location |
| 428 | * @param {number} column The column within the line for the location |
| 429 | * @param {number} start Source position for start of source context |
| 430 | * @param {number} end Source position for end of source context |
| 431 | * @constructor |
| 432 | */ |
| 433 | function SourceLocation(script, position, line, column, start, end) { |
| 434 | this.script = script; |
| 435 | this.position = position; |
| 436 | this.line = line; |
| 437 | this.column = column; |
| 438 | this.start = start; |
| 439 | this.end = end; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 440 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 441 | |
| 442 | |
| 443 | const kLineLengthLimit = 78; |
| 444 | |
| 445 | /** |
| 446 | * Restrict source location start and end positions to make the source slice |
| 447 | * no more that a certain number of characters wide. |
| 448 | * @param {number} opt_limit The with limit of the source text with a default |
| 449 | * of 78 |
| 450 | * @param {number} opt_before The number of characters to prefer before the |
| 451 | * position with a default value of 10 less that the limit |
| 452 | */ |
| 453 | SourceLocation.prototype.restrict = function (opt_limit, opt_before) { |
| 454 | // Find the actual limit to use. |
| 455 | var limit; |
| 456 | var before; |
| 457 | if (!IS_UNDEFINED(opt_limit)) { |
| 458 | limit = opt_limit; |
| 459 | } else { |
| 460 | limit = kLineLengthLimit; |
| 461 | } |
| 462 | if (!IS_UNDEFINED(opt_before)) { |
| 463 | before = opt_before; |
| 464 | } else { |
| 465 | // If no before is specified center for small limits and perfer more source |
| 466 | // before the the position that after for longer limits. |
| 467 | if (limit <= 20) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 468 | before = $floor(limit / 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 469 | } else { |
| 470 | before = limit - 10; |
| 471 | } |
| 472 | } |
| 473 | if (before >= limit) { |
| 474 | before = limit - 1; |
| 475 | } |
| 476 | |
| 477 | // If the [start, end[ interval is too big we restrict |
| 478 | // it in one or both ends. We make sure to always produce |
| 479 | // restricted intervals of maximum allowed size. |
| 480 | if (this.end - this.start > limit) { |
| 481 | var start_limit = this.position - before; |
| 482 | var end_limit = this.position + limit - before; |
| 483 | if (this.start < start_limit && end_limit < this.end) { |
| 484 | this.start = start_limit; |
| 485 | this.end = end_limit; |
| 486 | } else if (this.start < start_limit) { |
| 487 | this.start = this.end - limit; |
| 488 | } else { |
| 489 | this.end = this.start + limit; |
| 490 | } |
| 491 | } |
| 492 | }; |
| 493 | |
| 494 | |
| 495 | /** |
| 496 | * Get the source text for a SourceLocation |
| 497 | * @return {String} |
| 498 | * Source text for this location. |
| 499 | */ |
| 500 | SourceLocation.prototype.sourceText = function () { |
| 501 | return this.script.source.substring(this.start, this.end); |
| 502 | }; |
| 503 | |
| 504 | |
| 505 | /** |
| 506 | * Class for a source slice. A source slice is a part of a script source with |
| 507 | * the following properties: |
| 508 | * script : script object for the source |
| 509 | * from_line : line number for the first line in the slice |
| 510 | * to_line : source line number for the last line in the slice |
| 511 | * from_position : position of the first character in the slice |
| 512 | * to_position : position of the last character in the slice |
| 513 | * The to_line and to_position are not included in the slice, that is the lines |
| 514 | * in the slice are [from_line, to_line[. Likewise the characters in the slice |
| 515 | * are [from_position, to_position[. |
| 516 | * @param {Script} script The Script object for the source slice |
| 517 | * @param {number} from_line |
| 518 | * @param {number} to_line |
| 519 | * @param {number} from_position |
| 520 | * @param {number} to_position |
| 521 | * @constructor |
| 522 | */ |
| 523 | function SourceSlice(script, from_line, to_line, from_position, to_position) { |
| 524 | this.script = script; |
| 525 | this.from_line = from_line; |
| 526 | this.to_line = to_line; |
| 527 | this.from_position = from_position; |
| 528 | this.to_position = to_position; |
| 529 | } |
| 530 | |
| 531 | |
| 532 | /** |
| 533 | * Get the source text for a SourceSlice |
| 534 | * @return {String} Source text for this slice. The last line will include |
| 535 | * the line terminating characters (if any) |
| 536 | */ |
| 537 | SourceSlice.prototype.sourceText = function () { |
| 538 | return this.script.source.substring(this.from_position, this.to_position); |
| 539 | }; |
| 540 | |
| 541 | |
| 542 | // Returns the offset of the given position within the containing |
| 543 | // line. |
| 544 | function GetPositionInLine(message) { |
ager@chromium.org | 3a6061e | 2009-03-12 14:24:36 +0000 | [diff] [blame] | 545 | var location = message.script.locationFromPosition(message.startPos, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 546 | if (location == null) return -1; |
| 547 | location.restrict(); |
| 548 | return message.startPos - location.start; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 549 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 550 | |
| 551 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 552 | function ErrorMessage(type, args, startPos, endPos, script, stackTrace) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 553 | this.startPos = startPos; |
| 554 | this.endPos = endPos; |
| 555 | this.type = type; |
| 556 | this.args = args; |
| 557 | this.script = script; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 558 | this.stackTrace = stackTrace; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 559 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 560 | |
| 561 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 562 | function MakeMessage(type, args, startPos, endPos, script, stackTrace) { |
| 563 | return new ErrorMessage(type, args, startPos, endPos, script, stackTrace); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 564 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 565 | |
| 566 | |
| 567 | function GetStackTraceLine(recv, fun, pos, isGlobal) { |
| 568 | try { |
| 569 | return UnsafeGetStackTraceLine(recv, fun, pos, isGlobal); |
| 570 | } catch (e) { |
| 571 | return "<error: " + e + ">"; |
| 572 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 573 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 574 | |
| 575 | |
| 576 | function GetFunctionName(fun, recv) { |
| 577 | var name = %FunctionGetName(fun); |
| 578 | if (name) return name; |
| 579 | for (var prop in recv) { |
| 580 | if (recv[prop] === fun) |
| 581 | return prop; |
| 582 | } |
| 583 | return "[anonymous]"; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 584 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 585 | |
| 586 | |
| 587 | function UnsafeGetStackTraceLine(recv, fun, pos, isTopLevel) { |
| 588 | var result = ""; |
| 589 | // The global frame has no meaningful function or receiver |
| 590 | if (!isTopLevel) { |
| 591 | // If the receiver is not the global object then prefix the |
| 592 | // message send |
| 593 | if (recv !== global) |
| 594 | result += ToDetailString(recv) + "."; |
| 595 | result += GetFunctionName(fun, recv); |
| 596 | } |
| 597 | if (pos != -1) { |
| 598 | var script = %FunctionGetScript(fun); |
| 599 | var file; |
| 600 | if (script) { |
| 601 | file = %FunctionGetScript(fun).data; |
| 602 | } |
| 603 | if (file) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 604 | var location = %FunctionGetScript(fun).locationFromPosition(pos, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 605 | if (!isTopLevel) result += "("; |
| 606 | result += file; |
| 607 | if (location != null) { |
| 608 | result += ":" + (location.line + 1) + ":" + (location.column + 1); |
| 609 | } |
| 610 | if (!isTopLevel) result += ")"; |
| 611 | } |
| 612 | } |
| 613 | return (result) ? " at " + result : result; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 614 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 615 | |
| 616 | |
| 617 | // ---------------------------------------------------------------------------- |
| 618 | // Error implementation |
| 619 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 620 | // If this object gets passed to an error constructor the error will |
| 621 | // get an accessor for .message that constructs a descriptive error |
| 622 | // message on access. |
| 623 | var kAddMessageAccessorsMarker = { }; |
| 624 | |
| 625 | // Defines accessors for a property that is calculated the first time |
| 626 | // the property is read and then replaces the accessor with the value. |
| 627 | // Also, setting the property causes the accessors to be deleted. |
| 628 | function DefineOneShotAccessor(obj, name, fun) { |
| 629 | // Note that the accessors consistently operate on 'obj', not 'this'. |
| 630 | // Since the object may occur in someone else's prototype chain we |
| 631 | // can't rely on 'this' being the same as 'obj'. |
| 632 | obj.__defineGetter__(name, function () { |
| 633 | var value = fun(obj); |
| 634 | obj[name] = value; |
| 635 | return value; |
| 636 | }); |
| 637 | obj.__defineSetter__(name, function (v) { |
| 638 | delete obj[name]; |
| 639 | obj[name] = v; |
| 640 | }); |
| 641 | } |
| 642 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 643 | function DefineError(f) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 644 | // Store the error function in both the global object |
| 645 | // and the runtime object. The function is fetched |
| 646 | // from the runtime object when throwing errors from |
| 647 | // within the runtime system to avoid strange side |
| 648 | // effects when overwriting the error functions from |
| 649 | // user code. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 650 | var name = f.name; |
| 651 | %SetProperty(global, name, f, DONT_ENUM); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 652 | this['$' + name] = f; |
| 653 | // Configure the error function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 654 | if (name == 'Error') { |
| 655 | // The prototype of the Error object must itself be an error. |
| 656 | // However, it can't be an instance of the Error object because |
| 657 | // it hasn't been properly configured yet. Instead we create a |
| 658 | // special not-a-true-error-but-close-enough object. |
| 659 | function ErrorPrototype() {} |
| 660 | %FunctionSetPrototype(ErrorPrototype, $Object.prototype); |
| 661 | %FunctionSetInstanceClassName(ErrorPrototype, 'Error'); |
| 662 | %FunctionSetPrototype(f, new ErrorPrototype()); |
| 663 | } else { |
| 664 | %FunctionSetPrototype(f, new $Error()); |
| 665 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 666 | %FunctionSetInstanceClassName(f, 'Error'); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 667 | %SetProperty(f.prototype, 'constructor', f, DONT_ENUM); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 668 | f.prototype.name = name; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 669 | %SetCode(f, function(m) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 670 | if (%IsConstructCall()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 671 | if (m === kAddMessageAccessorsMarker) { |
| 672 | DefineOneShotAccessor(this, 'message', function (obj) { |
| 673 | return FormatMessage({type: obj.type, args: obj.arguments}); |
| 674 | }); |
| 675 | } else if (!IS_UNDEFINED(m)) { |
| 676 | this.message = ToString(m); |
| 677 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 678 | } else { |
| 679 | return new f(m); |
| 680 | } |
| 681 | }); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 682 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 683 | |
| 684 | $Math.__proto__ = global.Object.prototype; |
| 685 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 686 | DefineError(function Error() { }); |
| 687 | DefineError(function TypeError() { }); |
| 688 | DefineError(function RangeError() { }); |
| 689 | DefineError(function SyntaxError() { }); |
| 690 | DefineError(function ReferenceError() { }); |
| 691 | DefineError(function EvalError() { }); |
| 692 | DefineError(function URIError() { }); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 693 | |
| 694 | // Setup extra properties of the Error.prototype object. |
| 695 | $Error.prototype.message = ''; |
| 696 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 697 | %SetProperty($Error.prototype, 'toString', function toString() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 698 | var type = this.type; |
| 699 | if (type && !this.hasOwnProperty("message")) { |
| 700 | return this.name + ": " + FormatMessage({ type: type, args: this.arguments }); |
| 701 | } |
| 702 | var message = this.message; |
| 703 | return this.name + (message ? (": " + message) : ""); |
| 704 | }, DONT_ENUM); |
| 705 | |
| 706 | |
| 707 | // Boilerplate for exceptions for stack overflows. Used from |
| 708 | // Top::StackOverflow(). |
| 709 | const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []); |