| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // ------------------------------------------------------------------- | 5 // ------------------------------------------------------------------- |
| 6 | 6 |
| 7 (function(global, utils) { | 7 (function(global, utils) { |
| 8 | 8 |
| 9 %CheckIsBootstrapping(); | 9 %CheckIsBootstrapping(); |
| 10 | 10 |
| 11 // ------------------------------------------------------------------- | 11 // ------------------------------------------------------------------- |
| 12 // Imports | 12 // Imports |
| 13 | 13 |
| 14 var MakeGenericError = utils.ImportNow("make_generic_error"); | |
| 15 var GlobalError = global.Error; | |
| 16 var GlobalRangeError = global.RangeError; | |
| 17 var GlobalSyntaxError = global.SyntaxError; | |
| 18 var GlobalTypeError = global.TypeError; | |
| 19 var GlobalURIError = global.URIError; | |
| 20 var Script = utils.ImportNow("Script"); | 14 var Script = utils.ImportNow("Script"); |
| 21 | 15 |
| 22 // ------------------------------------------------------------------- | 16 // ------------------------------------------------------------------- |
| 17 // Script |
| 23 | 18 |
| 24 /** | 19 /** |
| 25 * Set up the Script function and constructor. | 20 * Set up the Script function and constructor. |
| 26 */ | 21 */ |
| 27 %FunctionSetInstanceClassName(Script, 'Script'); | 22 %FunctionSetInstanceClassName(Script, 'Script'); |
| 28 %AddNamedProperty(Script.prototype, 'constructor', Script, | 23 %AddNamedProperty(Script.prototype, 'constructor', Script, |
| 29 DONT_ENUM | DONT_DELETE | READ_ONLY); | 24 DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 30 %SetCode(Script, function(x) { | |
| 31 // Script objects can only be created by the VM. | |
| 32 throw MakeError(kUnsupported); | |
| 33 }); | |
| 34 | |
| 35 function GetLineNumber(message) { | |
| 36 var start_position = %MessageGetStartPosition(message); | |
| 37 if (start_position == -1) return kNoLineNumberInfo; | |
| 38 var script = %MessageGetScript(message); | |
| 39 var location = script.locationFromPosition(start_position, true); | |
| 40 if (location == null) return kNoLineNumberInfo; | |
| 41 return location.line + 1; | |
| 42 } | |
| 43 | |
| 44 | |
| 45 //Returns the offset of the given position within the containing line. | |
| 46 function GetColumnNumber(message) { | |
| 47 var script = %MessageGetScript(message); | |
| 48 var start_position = %MessageGetStartPosition(message); | |
| 49 var location = script.locationFromPosition(start_position, true); | |
| 50 if (location == null) return -1; | |
| 51 return location.column; | |
| 52 } | |
| 53 | |
| 54 | |
| 55 // Returns the source code line containing the given source | |
| 56 // position, or the empty string if the position is invalid. | |
| 57 function GetSourceLine(message) { | |
| 58 var script = %MessageGetScript(message); | |
| 59 var start_position = %MessageGetStartPosition(message); | |
| 60 var location = script.locationFromPosition(start_position, true); | |
| 61 if (location == null) return ""; | |
| 62 return location.sourceText; | |
| 63 } | |
| 64 | 25 |
| 65 | 26 |
| 66 /** | 27 /** |
| 67 * Get information on a specific source position. | 28 * Get information on a specific source position. |
| 68 * Returns an object with the following following properties: | 29 * Returns an object with the following following properties: |
| 69 * script : script object for the source | 30 * script : script object for the source |
| 70 * line : source line number | 31 * line : source line number |
| 71 * column : source column within the line | 32 * column : source column within the line |
| 72 * position : position within the source | 33 * position : position within the source |
| 73 * sourceText : a string containing the current line | 34 * sourceText : a string containing the current line |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 "source_url", | 66 "source_url", |
| 106 "source_mapping_url", | 67 "source_mapping_url", |
| 107 "line_offset", | 68 "line_offset", |
| 108 "column_offset" | 69 "column_offset" |
| 109 ], [ | 70 ], [ |
| 110 "locationFromPosition", ScriptLocationFromPosition, | 71 "locationFromPosition", ScriptLocationFromPosition, |
| 111 "nameOrSourceURL", ScriptNameOrSourceURL, | 72 "nameOrSourceURL", ScriptNameOrSourceURL, |
| 112 ] | 73 ] |
| 113 ); | 74 ); |
| 114 | 75 |
| 115 // ---------------------------------------------------------------------------- | 76 // ------------------------------------------------------------------- |
| 116 // Error implementation | 77 // Message |
| 117 | 78 |
| 118 function MakeError(type, arg0, arg1, arg2) { | 79 function GetLineNumber(message) { |
| 119 return MakeGenericError(GlobalError, type, arg0, arg1, arg2); | 80 var start_position = %MessageGetStartPosition(message); |
| 81 if (start_position == -1) return kNoLineNumberInfo; |
| 82 var script = %MessageGetScript(message); |
| 83 var location = script.locationFromPosition(start_position, true); |
| 84 if (location == null) return kNoLineNumberInfo; |
| 85 return location.line + 1; |
| 120 } | 86 } |
| 121 | 87 |
| 122 function MakeRangeError(type, arg0, arg1, arg2) { | 88 |
| 123 return MakeGenericError(GlobalRangeError, type, arg0, arg1, arg2); | 89 //Returns the offset of the given position within the containing line. |
| 90 function GetColumnNumber(message) { |
| 91 var script = %MessageGetScript(message); |
| 92 var start_position = %MessageGetStartPosition(message); |
| 93 var location = script.locationFromPosition(start_position, true); |
| 94 if (location == null) return -1; |
| 95 return location.column; |
| 124 } | 96 } |
| 125 | 97 |
| 126 function MakeSyntaxError(type, arg0, arg1, arg2) { | |
| 127 return MakeGenericError(GlobalSyntaxError, type, arg0, arg1, arg2); | |
| 128 } | |
| 129 | 98 |
| 130 function MakeTypeError(type, arg0, arg1, arg2) { | 99 // Returns the source code line containing the given source |
| 131 return MakeGenericError(GlobalTypeError, type, arg0, arg1, arg2); | 100 // position, or the empty string if the position is invalid. |
| 132 } | 101 function GetSourceLine(message) { |
| 133 | 102 var script = %MessageGetScript(message); |
| 134 function MakeURIError() { | 103 var start_position = %MessageGetStartPosition(message); |
| 135 return MakeGenericError(GlobalURIError, kURIMalformed); | 104 var location = script.locationFromPosition(start_position, true); |
| 105 if (location == null) return ""; |
| 106 return location.sourceText; |
| 136 } | 107 } |
| 137 | 108 |
| 138 %InstallToContext([ | 109 %InstallToContext([ |
| 139 "make_range_error", MakeRangeError, | |
| 140 "make_type_error", MakeTypeError, | |
| 141 "message_get_column_number", GetColumnNumber, | 110 "message_get_column_number", GetColumnNumber, |
| 142 "message_get_line_number", GetLineNumber, | 111 "message_get_line_number", GetLineNumber, |
| 143 "message_get_source_line", GetSourceLine, | 112 "message_get_source_line", GetSourceLine, |
| 144 ]); | 113 ]); |
| 145 | 114 |
| 146 utils.Export(function(to) { | |
| 147 to.MakeError = MakeError; | |
| 148 to.MakeRangeError = MakeRangeError; | |
| 149 to.MakeSyntaxError = MakeSyntaxError; | |
| 150 to.MakeTypeError = MakeTypeError; | |
| 151 to.MakeURIError = MakeURIError; | |
| 152 }); | 115 }); |
| 153 | |
| 154 }); | |
| OLD | NEW |