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 var $errorToString; | 7 var $errorToString; |
8 var $getStackTraceLine; | 8 var $getStackTraceLine; |
9 var $messageGetPositionInLine; | 9 var $messageGetPositionInLine; |
10 var $messageGetLineNumber; | 10 var $messageGetLineNumber; |
11 var $messageGetSourceLine; | 11 var $messageGetSourceLine; |
12 var $noSideEffectToString; | 12 var $noSideEffectToString; |
13 var $stackOverflowBoilerplate; | 13 var $stackOverflowBoilerplate; |
14 var $stackTraceSymbol; | 14 var $stackTraceSymbol; |
15 var $toDetailString; | 15 var $toDetailString; |
16 var $Error; | 16 var $Error; |
17 var $EvalError; | 17 var $EvalError; |
18 var $RangeError; | 18 var $RangeError; |
19 var $ReferenceError; | 19 var $ReferenceError; |
20 var $SyntaxError; | 20 var $SyntaxError; |
21 var $TypeError; | 21 var $TypeError; |
22 var $URIError; | 22 var $URIError; |
23 var MakeError; | 23 var MakeError; |
24 var MakeEvalError; | 24 var MakeEvalError; |
25 var MakeRangeError; | 25 var MakeRangeError; |
26 var MakeReferenceError; | 26 var MakeReferenceError; |
27 var MakeSyntaxError; | 27 var MakeSyntaxError; |
28 var MakeTypeError; | 28 var MakeTypeError; |
29 var MakeURIError; | 29 var MakeURIError; |
| 30 var MakeReferenceErrorEmbedded; |
| 31 var MakeSyntaxErrorEmbedded; |
| 32 var MakeTypeErrorEmbedded; |
30 | 33 |
31 (function(global, utils) { | 34 (function(global, utils) { |
32 | 35 |
33 %CheckIsBootstrapping(); | 36 %CheckIsBootstrapping(); |
34 | 37 |
35 // ------------------------------------------------------------------- | 38 // ------------------------------------------------------------------- |
36 // Imports | 39 // Imports |
37 | 40 |
38 var GlobalObject = global.Object; | 41 var GlobalObject = global.Object; |
39 var InternalArray = utils.InternalArray; | 42 var InternalArray = utils.InternalArray; |
40 var ObjectDefineProperty = utils.ObjectDefineProperty; | |
41 | 43 |
42 var ArrayJoin; | |
43 var ObjectToString; | |
44 var StringCharAt; | 44 var StringCharAt; |
45 var StringIndexOf; | 45 var StringIndexOf; |
46 var StringSubstring; | 46 var StringSubstring; |
47 | 47 |
48 utils.Import(function(from) { | 48 utils.Import(function(from) { |
49 ArrayJoin = from.ArrayJoin; | |
50 ObjectToString = from.ObjectToString; | |
51 StringCharAt = from.StringCharAt; | 49 StringCharAt = from.StringCharAt; |
52 StringIndexOf = from.StringIndexOf; | 50 StringIndexOf = from.StringIndexOf; |
53 StringSubstring = from.StringSubstring; | 51 StringSubstring = from.StringSubstring; |
54 }); | 52 }); |
55 | 53 |
56 // ------------------------------------------------------------------- | 54 // ------------------------------------------------------------------- |
57 | 55 |
58 var GlobalError; | 56 var GlobalError; |
59 var GlobalTypeError; | 57 var GlobalTypeError; |
60 var GlobalRangeError; | 58 var GlobalRangeError; |
(...skipping 19 matching lines...) Expand all Loading... |
80 if (IS_FUNCTION(obj)) { | 78 if (IS_FUNCTION(obj)) { |
81 var str = %_CallFunction(obj, obj, $functionSourceString); | 79 var str = %_CallFunction(obj, obj, $functionSourceString); |
82 if (str.length > 128) { | 80 if (str.length > 128) { |
83 str = %_SubString(str, 0, 111) + "...<omitted>..." + | 81 str = %_SubString(str, 0, 111) + "...<omitted>..." + |
84 %_SubString(str, str.length - 2, str.length); | 82 %_SubString(str, str.length - 2, str.length); |
85 } | 83 } |
86 return str; | 84 return str; |
87 } | 85 } |
88 if (IS_SYMBOL(obj)) return %_CallFunction(obj, $symbolToString); | 86 if (IS_SYMBOL(obj)) return %_CallFunction(obj, $symbolToString); |
89 if (IS_OBJECT(obj) | 87 if (IS_OBJECT(obj) |
90 && %GetDataProperty(obj, "toString") === ObjectToString) { | 88 && %GetDataProperty(obj, "toString") === $objectToString) { |
91 var constructor = %GetDataProperty(obj, "constructor"); | 89 var constructor = %GetDataProperty(obj, "constructor"); |
92 if (typeof constructor == "function") { | 90 if (typeof constructor == "function") { |
93 var constructorName = constructor.name; | 91 var constructorName = constructor.name; |
94 if (IS_STRING(constructorName) && constructorName !== "") { | 92 if (IS_STRING(constructorName) && constructorName !== "") { |
95 return "#<" + constructorName + ">"; | 93 return "#<" + constructorName + ">"; |
96 } | 94 } |
97 } | 95 } |
98 } | 96 } |
99 if (CanBeSafelyTreatedAsAnErrorObject(obj)) { | 97 if (CanBeSafelyTreatedAsAnErrorObject(obj)) { |
100 return %_CallFunction(obj, ErrorToString); | 98 return %_CallFunction(obj, ErrorToString); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 function ToStringCheckErrorObject(obj) { | 130 function ToStringCheckErrorObject(obj) { |
133 if (CanBeSafelyTreatedAsAnErrorObject(obj)) { | 131 if (CanBeSafelyTreatedAsAnErrorObject(obj)) { |
134 return %_CallFunction(obj, ErrorToString); | 132 return %_CallFunction(obj, ErrorToString); |
135 } else { | 133 } else { |
136 return $toString(obj); | 134 return $toString(obj); |
137 } | 135 } |
138 } | 136 } |
139 | 137 |
140 | 138 |
141 function ToDetailString(obj) { | 139 function ToDetailString(obj) { |
142 if (obj != null && IS_OBJECT(obj) && obj.toString === ObjectToString) { | 140 if (obj != null && IS_OBJECT(obj) && obj.toString === $objectToString) { |
143 var constructor = obj.constructor; | 141 var constructor = obj.constructor; |
144 if (typeof constructor == "function") { | 142 if (typeof constructor == "function") { |
145 var constructorName = constructor.name; | 143 var constructorName = constructor.name; |
146 if (IS_STRING(constructorName) && constructorName !== "") { | 144 if (IS_STRING(constructorName) && constructorName !== "") { |
147 return "#<" + constructorName + ">"; | 145 return "#<" + constructorName + ">"; |
148 } | 146 } |
149 } | 147 } |
150 } | 148 } |
151 return ToStringCheckErrorObject(obj); | 149 return ToStringCheckErrorObject(obj); |
152 } | 150 } |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 * | 402 * |
405 * @return {?string} script name if present, value for //# sourceURL or | 403 * @return {?string} script name if present, value for //# sourceURL or |
406 * deprecated //@ sourceURL comment otherwise. | 404 * deprecated //@ sourceURL comment otherwise. |
407 */ | 405 */ |
408 function ScriptNameOrSourceURL() { | 406 function ScriptNameOrSourceURL() { |
409 if (this.source_url) return this.source_url; | 407 if (this.source_url) return this.source_url; |
410 return this.name; | 408 return this.name; |
411 } | 409 } |
412 | 410 |
413 | 411 |
414 utils.SetUpLockedPrototype(Script, [ | 412 $setUpLockedPrototype(Script, [ |
415 "source", | 413 "source", |
416 "name", | 414 "name", |
417 "source_url", | 415 "source_url", |
418 "source_mapping_url", | 416 "source_mapping_url", |
419 "line_ends", | 417 "line_ends", |
420 "line_offset", | 418 "line_offset", |
421 "column_offset" | 419 "column_offset" |
422 ], [ | 420 ], [ |
423 "lineFromPosition", ScriptLineFromPosition, | 421 "lineFromPosition", ScriptLineFromPosition, |
424 "locationFromPosition", ScriptLocationFromPosition, | 422 "locationFromPosition", ScriptLocationFromPosition, |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 * Source text for this location. | 466 * Source text for this location. |
469 */ | 467 */ |
470 function SourceLocationSourceText() { | 468 function SourceLocationSourceText() { |
471 return %_CallFunction(this.script.source, | 469 return %_CallFunction(this.script.source, |
472 this.start, | 470 this.start, |
473 this.end, | 471 this.end, |
474 StringSubstring); | 472 StringSubstring); |
475 } | 473 } |
476 | 474 |
477 | 475 |
478 utils.SetUpLockedPrototype(SourceLocation, | 476 $setUpLockedPrototype(SourceLocation, |
479 ["script", "position", "line", "column", "start", "end"], | 477 ["script", "position", "line", "column", "start", "end"], |
480 ["sourceText", SourceLocationSourceText] | 478 ["sourceText", SourceLocationSourceText] |
481 ); | 479 ); |
482 | 480 |
483 | 481 |
484 /** | 482 /** |
485 * Class for a source slice. A source slice is a part of a script source with | 483 * Class for a source slice. A source slice is a part of a script source with |
486 * the following properties: | 484 * the following properties: |
487 * script : script object for the source | 485 * script : script object for the source |
488 * from_line : line number for the first line in the slice | 486 * from_line : line number for the first line in the slice |
(...skipping 23 matching lines...) Expand all Loading... |
512 * @return {String} Source text for this slice. The last line will include | 510 * @return {String} Source text for this slice. The last line will include |
513 * the line terminating characters (if any) | 511 * the line terminating characters (if any) |
514 */ | 512 */ |
515 function SourceSliceSourceText() { | 513 function SourceSliceSourceText() { |
516 return %_CallFunction(this.script.source, | 514 return %_CallFunction(this.script.source, |
517 this.from_position, | 515 this.from_position, |
518 this.to_position, | 516 this.to_position, |
519 StringSubstring); | 517 StringSubstring); |
520 } | 518 } |
521 | 519 |
522 utils.SetUpLockedPrototype(SourceSlice, | 520 $setUpLockedPrototype(SourceSlice, |
523 ["script", "from_line", "to_line", "from_position", "to_position"], | 521 ["script", "from_line", "to_line", "from_position", "to_position"], |
524 ["sourceText", SourceSliceSourceText] | 522 ["sourceText", SourceSliceSourceText] |
525 ); | 523 ); |
526 | 524 |
527 | 525 |
528 // Returns the offset of the given position within the containing | 526 // Returns the offset of the given position within the containing |
529 // line. | 527 // line. |
530 function GetPositionInLine(message) { | 528 function GetPositionInLine(message) { |
531 var script = %MessageGetScript(message); | 529 var script = %MessageGetScript(message); |
532 var start_position = %MessageGetStartPosition(message); | 530 var start_position = %MessageGetStartPosition(message); |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 } else { | 708 } else { |
711 line += fileLocation; | 709 line += fileLocation; |
712 addSuffix = false; | 710 addSuffix = false; |
713 } | 711 } |
714 if (addSuffix) { | 712 if (addSuffix) { |
715 line += " (" + fileLocation + ")"; | 713 line += " (" + fileLocation + ")"; |
716 } | 714 } |
717 return line; | 715 return line; |
718 } | 716 } |
719 | 717 |
720 utils.SetUpLockedPrototype(CallSite, ["receiver", "fun", "pos"], [ | 718 $setUpLockedPrototype(CallSite, ["receiver", "fun", "pos"], [ |
721 "getThis", CallSiteGetThis, | 719 "getThis", CallSiteGetThis, |
722 "getTypeName", CallSiteGetTypeName, | 720 "getTypeName", CallSiteGetTypeName, |
723 "isToplevel", CallSiteIsToplevel, | 721 "isToplevel", CallSiteIsToplevel, |
724 "isEval", CallSiteIsEval, | 722 "isEval", CallSiteIsEval, |
725 "getEvalOrigin", CallSiteGetEvalOrigin, | 723 "getEvalOrigin", CallSiteGetEvalOrigin, |
726 "getScriptNameOrSourceURL", CallSiteGetScriptNameOrSourceURL, | 724 "getScriptNameOrSourceURL", CallSiteGetScriptNameOrSourceURL, |
727 "getFunction", CallSiteGetFunction, | 725 "getFunction", CallSiteGetFunction, |
728 "getFunctionName", CallSiteGetFunctionName, | 726 "getFunctionName", CallSiteGetFunctionName, |
729 "getMethodName", CallSiteGetMethodName, | 727 "getMethodName", CallSiteGetMethodName, |
730 "getFileName", CallSiteGetFileName, | 728 "getFileName", CallSiteGetFileName, |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
837 } catch (e) { | 835 } catch (e) { |
838 try { | 836 try { |
839 line = "<error: " + e + ">"; | 837 line = "<error: " + e + ">"; |
840 } catch (ee) { | 838 } catch (ee) { |
841 // Any code that reaches this point is seriously nasty! | 839 // Any code that reaches this point is seriously nasty! |
842 line = "<error>"; | 840 line = "<error>"; |
843 } | 841 } |
844 } | 842 } |
845 lines.push(" at " + line); | 843 lines.push(" at " + line); |
846 } | 844 } |
847 return %_CallFunction(lines, "\n", ArrayJoin); | 845 return %_CallFunction(lines, "\n", $arrayJoin); |
848 } | 846 } |
849 | 847 |
850 | 848 |
851 function GetTypeName(receiver, requireConstructor) { | 849 function GetTypeName(receiver, requireConstructor) { |
852 var constructor = receiver.constructor; | 850 var constructor = receiver.constructor; |
853 if (!constructor) { | 851 if (!constructor) { |
854 return requireConstructor ? null : | 852 return requireConstructor ? null : |
855 %_CallFunction(receiver, NoSideEffectsObjectToString); | 853 %_CallFunction(receiver, NoSideEffectsObjectToString); |
856 } | 854 } |
857 var constructorName = constructor.name; | 855 var constructorName = constructor.name; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
897 var StackTraceSetter = function(v) { | 895 var StackTraceSetter = function(v) { |
898 if (HAS_PRIVATE(this, $stackTraceSymbol)) { | 896 if (HAS_PRIVATE(this, $stackTraceSymbol)) { |
899 SET_PRIVATE(this, $stackTraceSymbol, UNDEFINED); | 897 SET_PRIVATE(this, $stackTraceSymbol, UNDEFINED); |
900 SET_PRIVATE(this, formatted_stack_trace_symbol, v); | 898 SET_PRIVATE(this, formatted_stack_trace_symbol, v); |
901 } | 899 } |
902 }; | 900 }; |
903 | 901 |
904 | 902 |
905 // Use a dummy function since we do not actually want to capture a stack trace | 903 // Use a dummy function since we do not actually want to capture a stack trace |
906 // when constructing the initial Error prototytpes. | 904 // when constructing the initial Error prototytpes. |
907 var captureStackTrace = function() {}; | 905 var captureStackTrace = function captureStackTrace(obj, cons_opt) { |
| 906 // Define accessors first, as this may fail and throw. |
| 907 $objectDefineProperty(obj, 'stack', { get: StackTraceGetter, |
| 908 set: StackTraceSetter, |
| 909 configurable: true }); |
| 910 %CollectStackTrace(obj, cons_opt ? cons_opt : captureStackTrace); |
| 911 } |
908 | 912 |
909 | 913 |
910 // Define special error type constructors. | 914 // Define special error type constructors. |
911 function DefineError(global, f) { | 915 function DefineError(global, f) { |
912 // Store the error function in both the global object | 916 // Store the error function in both the global object |
913 // and the runtime object. The function is fetched | 917 // and the runtime object. The function is fetched |
914 // from the runtime object when throwing errors from | 918 // from the runtime object when throwing errors from |
915 // within the runtime system to avoid strange side | 919 // within the runtime system to avoid strange side |
916 // effects when overwriting the error functions from | 920 // effects when overwriting the error functions from |
917 // user code. | 921 // user code. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
952 }; | 956 }; |
953 | 957 |
954 GlobalError = DefineError(global, function Error() { }); | 958 GlobalError = DefineError(global, function Error() { }); |
955 GlobalEvalError = DefineError(global, function EvalError() { }); | 959 GlobalEvalError = DefineError(global, function EvalError() { }); |
956 GlobalRangeError = DefineError(global, function RangeError() { }); | 960 GlobalRangeError = DefineError(global, function RangeError() { }); |
957 GlobalReferenceError = DefineError(global, function ReferenceError() { }); | 961 GlobalReferenceError = DefineError(global, function ReferenceError() { }); |
958 GlobalSyntaxError = DefineError(global, function SyntaxError() { }); | 962 GlobalSyntaxError = DefineError(global, function SyntaxError() { }); |
959 GlobalTypeError = DefineError(global, function TypeError() { }); | 963 GlobalTypeError = DefineError(global, function TypeError() { }); |
960 GlobalURIError = DefineError(global, function URIError() { }); | 964 GlobalURIError = DefineError(global, function URIError() { }); |
961 | 965 |
| 966 |
| 967 GlobalError.captureStackTrace = captureStackTrace; |
| 968 |
962 %AddNamedProperty(GlobalError.prototype, 'message', '', DONT_ENUM); | 969 %AddNamedProperty(GlobalError.prototype, 'message', '', DONT_ENUM); |
963 | 970 |
964 // Global list of error objects visited during ErrorToString. This is | 971 // Global list of error objects visited during ErrorToString. This is |
965 // used to detect cycles in error toString formatting. | 972 // used to detect cycles in error toString formatting. |
966 var visited_errors = new InternalArray(); | 973 var visited_errors = new InternalArray(); |
967 var cyclic_error_marker = new GlobalObject(); | 974 var cyclic_error_marker = new GlobalObject(); |
968 | 975 |
969 function GetPropertyWithoutInvokingMonkeyGetters(error, name) { | 976 function GetPropertyWithoutInvokingMonkeyGetters(error, name) { |
970 var current = error; | 977 var current = error; |
971 // Climb the prototype chain until we find the holder. | 978 // Climb the prototype chain until we find the holder. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1016 } catch(e) { | 1023 } catch(e) { |
1017 // If this error message was encountered already return the empty | 1024 // If this error message was encountered already return the empty |
1018 // string for it instead of recursively formatting it. | 1025 // string for it instead of recursively formatting it. |
1019 if (e === cyclic_error_marker) { | 1026 if (e === cyclic_error_marker) { |
1020 return ''; | 1027 return ''; |
1021 } | 1028 } |
1022 throw e; | 1029 throw e; |
1023 } | 1030 } |
1024 } | 1031 } |
1025 | 1032 |
1026 utils.InstallFunctions(GlobalError.prototype, DONT_ENUM, | 1033 $installFunctions(GlobalError.prototype, DONT_ENUM, |
1027 ['toString', ErrorToString]); | 1034 ['toString', ErrorToString]); |
1028 | 1035 |
1029 $errorToString = ErrorToString; | 1036 $errorToString = ErrorToString; |
1030 $getStackTraceLine = GetStackTraceLine; | 1037 $getStackTraceLine = GetStackTraceLine; |
1031 $messageGetPositionInLine = GetPositionInLine; | 1038 $messageGetPositionInLine = GetPositionInLine; |
1032 $messageGetLineNumber = GetLineNumber; | 1039 $messageGetLineNumber = GetLineNumber; |
1033 $messageGetSourceLine = GetSourceLine; | 1040 $messageGetSourceLine = GetSourceLine; |
1034 $noSideEffectToString = NoSideEffectToString; | 1041 $noSideEffectToString = NoSideEffectToString; |
1035 $toDetailString = ToDetailString; | 1042 $toDetailString = ToDetailString; |
1036 | 1043 |
1037 $Error = GlobalError; | 1044 $Error = GlobalError; |
(...skipping 25 matching lines...) Expand all Loading... |
1063 } | 1070 } |
1064 | 1071 |
1065 MakeTypeError = function(type, arg0, arg1, arg2) { | 1072 MakeTypeError = function(type, arg0, arg1, arg2) { |
1066 return MakeGenericError(GlobalTypeError, type, arg0, arg1, arg2); | 1073 return MakeGenericError(GlobalTypeError, type, arg0, arg1, arg2); |
1067 } | 1074 } |
1068 | 1075 |
1069 MakeURIError = function() { | 1076 MakeURIError = function() { |
1070 return MakeGenericError(GlobalURIError, kURIMalformed); | 1077 return MakeGenericError(GlobalURIError, kURIMalformed); |
1071 } | 1078 } |
1072 | 1079 |
1073 // Boilerplate for exceptions for stack overflows. Used from | 1080 //Boilerplate for exceptions for stack overflows. Used from |
1074 // Isolate::StackOverflow(). | 1081 //Isolate::StackOverflow(). |
1075 $stackOverflowBoilerplate = MakeRangeError(kStackOverflow); | 1082 $stackOverflowBoilerplate = MakeRangeError(kStackOverflow); |
1076 %DefineAccessorPropertyUnchecked($stackOverflowBoilerplate, 'stack', | 1083 %DefineAccessorPropertyUnchecked($stackOverflowBoilerplate, 'stack', |
1077 StackTraceGetter, StackTraceSetter, | 1084 StackTraceGetter, StackTraceSetter, DONT_ENUM); |
1078 DONT_ENUM); | |
1079 | 1085 |
1080 // Define actual captureStackTrace function after everything has been set up. | 1086 }) |
1081 captureStackTrace = function captureStackTrace(obj, cons_opt) { | |
1082 // Define accessors first, as this may fail and throw. | |
1083 ObjectDefineProperty(obj, 'stack', { get: StackTraceGetter, | |
1084 set: StackTraceSetter, | |
1085 configurable: true }); | |
1086 %CollectStackTrace(obj, cons_opt ? cons_opt : captureStackTrace); | |
1087 }; | |
1088 | |
1089 GlobalError.captureStackTrace = captureStackTrace; | |
1090 | |
1091 }); | |
OLD | NEW |