OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 // If this object gets passed to an error constructor the error will | 48 // If this object gets passed to an error constructor the error will |
49 // get an accessor for .message that constructs a descriptive error | 49 // get an accessor for .message that constructs a descriptive error |
50 // message on access. | 50 // message on access. |
51 var kAddMessageAccessorsMarker = { }; | 51 var kAddMessageAccessorsMarker = { }; |
52 | 52 |
53 var kMessages = 0; | 53 var kMessages = 0; |
54 | 54 |
55 var kReplacementMarkers = | 55 var kReplacementMarkers = |
56 [ "%0", "%1", "%2", "%3" ] | 56 [ "%0", "%1", "%2", "%3" ] |
57 | 57 |
58 function FormatString(format, args) { | 58 function FormatString(format, message) { |
| 59 var args = %MessageGetArguments(message); |
59 var result = ""; | 60 var result = ""; |
60 var arg_num = 0; | 61 var arg_num = 0; |
61 for (var i = 0; i < format.length; i++) { | 62 for (var i = 0; i < format.length; i++) { |
62 var str = format[i]; | 63 var str = format[i]; |
63 for (arg_num = 0; arg_num < kReplacementMarkers.length; arg_num++) { | 64 for (arg_num = 0; arg_num < kReplacementMarkers.length; arg_num++) { |
64 if (format[i] !== kReplacementMarkers[arg_num]) continue; | 65 if (format[i] !== kReplacementMarkers[arg_num]) continue; |
65 try { | 66 try { |
66 str = ToDetailString(args[arg_num]); | 67 str = ToDetailString(args[arg_num]); |
67 } catch (e) { | 68 } catch (e) { |
68 str = "#<error>"; | 69 str = "#<error>"; |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 strict_function_name: ["Function name may not be eval or arguments
in strict mode"], | 221 strict_function_name: ["Function name may not be eval or arguments
in strict mode"], |
221 strict_octal_literal: ["Octal literals are not allowed in strict m
ode."], | 222 strict_octal_literal: ["Octal literals are not allowed in strict m
ode."], |
222 strict_duplicate_property: ["Duplicate data property in object literal
not allowed in strict mode"], | 223 strict_duplicate_property: ["Duplicate data property in object literal
not allowed in strict mode"], |
223 accessor_data_property: ["Object literal may not have data and acces
sor property with the same name"], | 224 accessor_data_property: ["Object literal may not have data and acces
sor property with the same name"], |
224 accessor_get_set: ["Object literal may not have multiple get/s
et accessors with the same name"], | 225 accessor_get_set: ["Object literal may not have multiple get/s
et accessors with the same name"], |
225 strict_lhs_assignment: ["Assignment to eval or arguments is not all
owed in strict mode"], | 226 strict_lhs_assignment: ["Assignment to eval or arguments is not all
owed in strict mode"], |
226 strict_lhs_postfix: ["Postfix increment/decrement may not have e
val or arguments operand in strict mode"], | 227 strict_lhs_postfix: ["Postfix increment/decrement may not have e
val or arguments operand in strict mode"], |
227 strict_lhs_prefix: ["Prefix increment/decrement may not have ev
al or arguments operand in strict mode"], | 228 strict_lhs_prefix: ["Prefix increment/decrement may not have ev
al or arguments operand in strict mode"], |
228 }; | 229 }; |
229 } | 230 } |
230 var format = kMessages[message.type]; | 231 var message_type = %MessageGetType(message); |
231 if (!format) return "<unknown message " + message.type + ">"; | 232 var format = kMessages[message_type]; |
232 return FormatString(format, message.args); | 233 if (!format) return "<unknown message " + message_type + ">"; |
| 234 return FormatString(format, message); |
233 } | 235 } |
234 | 236 |
235 | 237 |
236 function GetLineNumber(message) { | 238 function GetLineNumber(message) { |
237 if (message.startPos == -1) return kNoLineNumberInfo; | 239 var start_position = %MessageGetStartPosition(message); |
238 var location = message.script.locationFromPosition(message.startPos, true); | 240 if (start_position == -1) return kNoLineNumberInfo; |
| 241 var script = %MessageGetScript(message); |
| 242 var location = script.locationFromPosition(start_position, true); |
239 if (location == null) return kNoLineNumberInfo; | 243 if (location == null) return kNoLineNumberInfo; |
240 return location.line + 1; | 244 return location.line + 1; |
241 } | 245 } |
242 | 246 |
243 | 247 |
244 // Returns the source code line containing the given source | 248 // Returns the source code line containing the given source |
245 // position, or the empty string if the position is invalid. | 249 // position, or the empty string if the position is invalid. |
246 function GetSourceLine(message) { | 250 function GetSourceLine(message) { |
247 var location = message.script.locationFromPosition(message.startPos, true); | 251 var script = %MessageGetScript(message); |
| 252 var start_position = %MessageGetStartPosition(message); |
| 253 var location = script.locationFromPosition(start_position, true); |
248 if (location == null) return ""; | 254 if (location == null) return ""; |
249 location.restrict(); | 255 location.restrict(); |
250 return location.sourceText(); | 256 return location.sourceText(); |
251 } | 257 } |
252 | 258 |
253 | 259 |
254 function MakeTypeError(type, args) { | 260 function MakeTypeError(type, args) { |
255 return MakeGenericError($TypeError, type, args); | 261 return MakeGenericError($TypeError, type, args); |
256 } | 262 } |
257 | 263 |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
616 return %_CallFunction(this.script.source, | 622 return %_CallFunction(this.script.source, |
617 this.from_position, | 623 this.from_position, |
618 this.to_position, | 624 this.to_position, |
619 StringSubstring); | 625 StringSubstring); |
620 }; | 626 }; |
621 | 627 |
622 | 628 |
623 // Returns the offset of the given position within the containing | 629 // Returns the offset of the given position within the containing |
624 // line. | 630 // line. |
625 function GetPositionInLine(message) { | 631 function GetPositionInLine(message) { |
626 var location = message.script.locationFromPosition(message.startPos, false); | 632 var script = %MessageGetScript(message); |
| 633 var start_position = %MessageGetStartPosition(message); |
| 634 var location = script.locationFromPosition(start_position, false); |
627 if (location == null) return -1; | 635 if (location == null) return -1; |
628 location.restrict(); | 636 location.restrict(); |
629 return message.startPos - location.start; | 637 return start_position - location.start; |
630 } | 638 } |
631 | 639 |
632 | 640 |
633 function ErrorMessage(type, args, startPos, endPos, script, stackTrace, | |
634 stackFrames) { | |
635 this.startPos = startPos; | |
636 this.endPos = endPos; | |
637 this.type = type; | |
638 this.args = args; | |
639 this.script = script; | |
640 this.stackTrace = stackTrace; | |
641 this.stackFrames = stackFrames; | |
642 } | |
643 | |
644 | |
645 function MakeMessage(type, args, startPos, endPos, script, stackTrace, | |
646 stackFrames) { | |
647 return new ErrorMessage(type, args, startPos, endPos, script, stackTrace, | |
648 stackFrames); | |
649 } | |
650 | |
651 | |
652 function GetStackTraceLine(recv, fun, pos, isGlobal) { | 641 function GetStackTraceLine(recv, fun, pos, isGlobal) { |
653 return FormatSourcePosition(new CallSite(recv, fun, pos)); | 642 return FormatSourcePosition(new CallSite(recv, fun, pos)); |
654 } | 643 } |
655 | 644 |
656 // ---------------------------------------------------------------------------- | 645 // ---------------------------------------------------------------------------- |
657 // Error implementation | 646 // Error implementation |
658 | 647 |
659 // Defines accessors for a property that is calculated the first time | 648 // Defines accessors for a property that is calculated the first time |
660 // the property is read. | 649 // the property is read. |
661 function DefineOneShotAccessor(obj, name, fun) { | 650 function DefineOneShotAccessor(obj, name, fun) { |
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
985 // Define all the expected properties directly on the error | 974 // Define all the expected properties directly on the error |
986 // object. This avoids going through getters and setters defined | 975 // object. This avoids going through getters and setters defined |
987 // on prototype objects. | 976 // on prototype objects. |
988 %IgnoreAttributesAndSetProperty(this, 'stack', void 0); | 977 %IgnoreAttributesAndSetProperty(this, 'stack', void 0); |
989 %IgnoreAttributesAndSetProperty(this, 'arguments', void 0); | 978 %IgnoreAttributesAndSetProperty(this, 'arguments', void 0); |
990 %IgnoreAttributesAndSetProperty(this, 'type', void 0); | 979 %IgnoreAttributesAndSetProperty(this, 'type', void 0); |
991 if (m === kAddMessageAccessorsMarker) { | 980 if (m === kAddMessageAccessorsMarker) { |
992 // DefineOneShotAccessor always inserts a message property and | 981 // DefineOneShotAccessor always inserts a message property and |
993 // ignores setters. | 982 // ignores setters. |
994 DefineOneShotAccessor(this, 'message', function (obj) { | 983 DefineOneShotAccessor(this, 'message', function (obj) { |
995 return FormatMessage({type: obj.type, args: obj.arguments}); | 984 return FormatMessage(%NewMessageObject(obj.type, obj.arguments)); |
996 }); | 985 }); |
997 } else if (!IS_UNDEFINED(m)) { | 986 } else if (!IS_UNDEFINED(m)) { |
998 %IgnoreAttributesAndSetProperty(this, 'message', ToString(m)); | 987 %IgnoreAttributesAndSetProperty(this, 'message', ToString(m)); |
999 } | 988 } |
1000 captureStackTrace(this, f); | 989 captureStackTrace(this, f); |
1001 } else { | 990 } else { |
1002 return new f(m); | 991 return new f(m); |
1003 } | 992 } |
1004 }); | 993 }); |
1005 } | 994 } |
1006 | 995 |
1007 function captureStackTrace(obj, cons_opt) { | 996 function captureStackTrace(obj, cons_opt) { |
1008 var stackTraceLimit = $Error.stackTraceLimit; | 997 var stackTraceLimit = $Error.stackTraceLimit; |
1009 if (!stackTraceLimit) return; | 998 if (!stackTraceLimit || !IS_NUMBER(stackTraceLimit)) return; |
1010 if (stackTraceLimit < 0 || stackTraceLimit > 10000) | 999 if (stackTraceLimit < 0 || stackTraceLimit > 10000) |
1011 stackTraceLimit = 10000; | 1000 stackTraceLimit = 10000; |
1012 var raw_stack = %CollectStackTrace(cons_opt ? cons_opt : captureStackTrace, | 1001 var raw_stack = %CollectStackTrace(cons_opt |
1013 stackTraceLimit); | 1002 ? cons_opt |
| 1003 : captureStackTrace, stackTraceLimit); |
1014 DefineOneShotAccessor(obj, 'stack', function (obj) { | 1004 DefineOneShotAccessor(obj, 'stack', function (obj) { |
1015 return FormatRawStackTrace(obj, raw_stack); | 1005 return FormatRawStackTrace(obj, raw_stack); |
1016 }); | 1006 }); |
1017 }; | 1007 }; |
1018 | 1008 |
1019 $Math.__proto__ = global.Object.prototype; | 1009 $Math.__proto__ = global.Object.prototype; |
1020 | 1010 |
1021 DefineError(function Error() { }); | 1011 DefineError(function Error() { }); |
1022 DefineError(function TypeError() { }); | 1012 DefineError(function TypeError() { }); |
1023 DefineError(function RangeError() { }); | 1013 DefineError(function RangeError() { }); |
(...skipping 10 matching lines...) Expand all Loading... |
1034 // Global list of error objects visited during errorToString. This is | 1024 // Global list of error objects visited during errorToString. This is |
1035 // used to detect cycles in error toString formatting. | 1025 // used to detect cycles in error toString formatting. |
1036 var visited_errors = new $Array(); | 1026 var visited_errors = new $Array(); |
1037 var cyclic_error_marker = new $Object(); | 1027 var cyclic_error_marker = new $Object(); |
1038 | 1028 |
1039 function errorToStringDetectCycle() { | 1029 function errorToStringDetectCycle() { |
1040 if (!%PushIfAbsent(visited_errors, this)) throw cyclic_error_marker; | 1030 if (!%PushIfAbsent(visited_errors, this)) throw cyclic_error_marker; |
1041 try { | 1031 try { |
1042 var type = this.type; | 1032 var type = this.type; |
1043 if (type && !%_CallFunction(this, "message", ObjectHasOwnProperty)) { | 1033 if (type && !%_CallFunction(this, "message", ObjectHasOwnProperty)) { |
1044 var formatted = FormatMessage({ type: type, args: this.arguments }); | 1034 var formatted = FormatMessage(%NewMessageObject(type, this.arguments)); |
1045 return this.name + ": " + formatted; | 1035 return this.name + ": " + formatted; |
1046 } | 1036 } |
1047 var message = %_CallFunction(this, "message", ObjectHasOwnProperty) | 1037 var message = %_CallFunction(this, "message", ObjectHasOwnProperty) |
1048 ? (": " + this.message) | 1038 ? (": " + this.message) |
1049 : ""; | 1039 : ""; |
1050 return this.name + message; | 1040 return this.name + message; |
1051 } finally { | 1041 } finally { |
1052 visited_errors.length = visited_errors.length - 1; | 1042 visited_errors.length = visited_errors.length - 1; |
1053 } | 1043 } |
1054 } | 1044 } |
(...skipping 12 matching lines...) Expand all Loading... |
1067 else throw e; | 1057 else throw e; |
1068 } | 1058 } |
1069 } | 1059 } |
1070 | 1060 |
1071 %FunctionSetName(errorToString, 'toString'); | 1061 %FunctionSetName(errorToString, 'toString'); |
1072 %SetProperty($Error.prototype, 'toString', errorToString, DONT_ENUM); | 1062 %SetProperty($Error.prototype, 'toString', errorToString, DONT_ENUM); |
1073 | 1063 |
1074 // Boilerplate for exceptions for stack overflows. Used from | 1064 // Boilerplate for exceptions for stack overflows. Used from |
1075 // Top::StackOverflow(). | 1065 // Top::StackOverflow(). |
1076 const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []); | 1066 const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []); |
OLD | NEW |