Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(250)

Side by Side Diff: src/messages.js

Issue 11377158: Fire 'stack' getter of error objects after GC. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/mark-compact.cc ('k') | src/runtime.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 802 matching lines...) Expand 10 before | Expand all | Expand 10 after
813 // See if we can find a unique property on the receiver that holds 813 // See if we can find a unique property on the receiver that holds
814 // this function. 814 // this function.
815 var ownName = this.fun.name; 815 var ownName = this.fun.name;
816 if (ownName && this.receiver && 816 if (ownName && this.receiver &&
817 (%_CallFunction(this.receiver, 817 (%_CallFunction(this.receiver,
818 ownName, 818 ownName,
819 ObjectLookupGetter) === this.fun || 819 ObjectLookupGetter) === this.fun ||
820 %_CallFunction(this.receiver, 820 %_CallFunction(this.receiver,
821 ownName, 821 ownName,
822 ObjectLookupSetter) === this.fun || 822 ObjectLookupSetter) === this.fun ||
823 this.receiver[ownName] === this.fun)) { 823 %GetDataProperty(this.receiver, ownName) === this.fun)) {
824 // To handle DontEnum properties we guess that the method has 824 // To handle DontEnum properties we guess that the method has
825 // the same name as the function. 825 // the same name as the function.
826 return ownName; 826 return ownName;
827 } 827 }
828 var name = null; 828 var name = null;
829 for (var prop in this.receiver) { 829 for (var prop in this.receiver) {
830 if (%_CallFunction(this.receiver, prop, ObjectLookupGetter) === this.fun || 830 if (%_CallFunction(this.receiver, prop, ObjectLookupGetter) === this.fun ||
831 %_CallFunction(this.receiver, prop, ObjectLookupSetter) === this.fun || 831 %_CallFunction(this.receiver, prop, ObjectLookupSetter) === this.fun ||
832 (!%_CallFunction(this.receiver, prop, ObjectLookupGetter) && 832 %GetDataProperty(this.receiver, prop) === this.fun) {
833 this.receiver[prop] === this.fun)) {
834 // If we find more than one match bail out to avoid confusion. 833 // If we find more than one match bail out to avoid confusion.
835 if (name) { 834 if (name) {
836 return null; 835 return null;
837 } 836 }
838 name = prop; 837 name = prop;
839 } 838 }
840 } 839 }
841 if (name) { 840 if (name) {
842 return name; 841 return name;
843 } 842 }
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
926 925
927 var line = ""; 926 var line = "";
928 var functionName = this.getFunctionName(); 927 var functionName = this.getFunctionName();
929 var addSuffix = true; 928 var addSuffix = true;
930 var isConstructor = this.isConstructor(); 929 var isConstructor = this.isConstructor();
931 var isMethodCall = !(this.isToplevel() || isConstructor); 930 var isMethodCall = !(this.isToplevel() || isConstructor);
932 if (isMethodCall) { 931 if (isMethodCall) {
933 var typeName = GetTypeName(this, true); 932 var typeName = GetTypeName(this, true);
934 var methodName = this.getMethodName(); 933 var methodName = this.getMethodName();
935 if (functionName) { 934 if (functionName) {
936 if (typeName && functionName.indexOf(typeName) != 0) { 935 if (typeName &&
936 %_CallFunction(functionName, typeName, StringIndexOf) != 0) {
937 line += typeName + "."; 937 line += typeName + ".";
938 } 938 }
939 line += functionName; 939 line += functionName;
940 if (methodName && functionName.lastIndexOf("." + methodName) != 940 if (methodName &&
941 functionName.length - methodName.length - 1) { 941 (%_CallFunction(functionName, "." + methodName, StringIndexOf) !=
942 functionName.length - methodName.length - 1)) {
942 line += " [as " + methodName + "]"; 943 line += " [as " + methodName + "]";
943 } 944 }
944 } else { 945 } else {
945 line += typeName + "." + (methodName || "<anonymous>"); 946 line += typeName + "." + (methodName || "<anonymous>");
946 } 947 }
947 } else if (isConstructor) { 948 } else if (isConstructor) {
948 line += "new " + (functionName || "<anonymous>"); 949 line += "new " + (functionName || "<anonymous>");
949 } else if (functionName) { 950 } else if (functionName) {
950 line += functionName; 951 line += functionName;
951 } else { 952 } else {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
1009 eval_origin += ")"; 1010 eval_origin += ")";
1010 } else { 1011 } else {
1011 eval_origin += " (unknown source)"; 1012 eval_origin += " (unknown source)";
1012 } 1013 }
1013 } 1014 }
1014 } 1015 }
1015 1016
1016 return eval_origin; 1017 return eval_origin;
1017 } 1018 }
1018 1019
1019 function FormatStackTrace(error, frames) { 1020
1020 var lines = []; 1021 function FormatErrorString(error) {
1021 try { 1022 try {
1022 lines.push(error.toString()); 1023 return %_CallFunction(error, ErrorToString);
1023 } catch (e) { 1024 } catch (e) {
1024 try { 1025 try {
1025 lines.push("<error: " + e + ">"); 1026 return "<error: " + e + ">";
1026 } catch (ee) { 1027 } catch (ee) {
1027 lines.push("<error>"); 1028 return "<error>";
1028 } 1029 }
1029 } 1030 }
1031 }
1032
1033
1034 function GetStackFrames(raw_stack) {
1035 var frames = new InternalArray();
1036 for (var i = 0; i < raw_stack.length; i += 4) {
1037 var recv = raw_stack[i];
1038 var fun = raw_stack[i + 1];
1039 var code = raw_stack[i + 2];
1040 var pc = raw_stack[i + 3];
1041 var pos = %FunctionGetPositionForOffset(code, pc);
1042 frames.push(new CallSite(recv, fun, pos));
1043 }
1044 return frames;
1045 }
1046
1047
1048 function FormatStackTrace(error_string, frames) {
1049 var lines = new InternalArray();
1050 lines.push(error_string);
1030 for (var i = 0; i < frames.length; i++) { 1051 for (var i = 0; i < frames.length; i++) {
1031 var frame = frames[i]; 1052 var frame = frames[i];
1032 var line; 1053 var line;
1033 try { 1054 try {
1034 line = frame.toString(); 1055 line = frame.toString();
1035 } catch (e) { 1056 } catch (e) {
1036 try { 1057 try {
1037 line = "<error: " + e + ">"; 1058 line = "<error: " + e + ">";
1038 } catch (ee) { 1059 } catch (ee) {
1039 // Any code that reaches this point is seriously nasty! 1060 // Any code that reaches this point is seriously nasty!
1040 line = "<error>"; 1061 line = "<error>";
1041 } 1062 }
1042 } 1063 }
1043 lines.push(" at " + line); 1064 lines.push(" at " + line);
1044 } 1065 }
1045 return lines.join("\n"); 1066 return %_CallFunction(lines, "\n", ArrayJoin);
1046 } 1067 }
1047 1068
1048 function FormatRawStackTrace(error, raw_stack) {
1049 var frames = [ ];
1050 for (var i = 0; i < raw_stack.length; i += 4) {
1051 var recv = raw_stack[i];
1052 var fun = raw_stack[i + 1];
1053 var code = raw_stack[i + 2];
1054 var pc = raw_stack[i + 3];
1055 var pos = %FunctionGetPositionForOffset(code, pc);
1056 frames.push(new CallSite(recv, fun, pos));
1057 }
1058 if (IS_FUNCTION($Error.prepareStackTrace)) {
1059 return $Error.prepareStackTrace(error, frames);
1060 } else {
1061 return FormatStackTrace(error, frames);
1062 }
1063 }
1064 1069
1065 function GetTypeName(obj, requireConstructor) { 1070 function GetTypeName(obj, requireConstructor) {
1066 var constructor = obj.receiver.constructor; 1071 var constructor = obj.receiver.constructor;
1067 if (!constructor) { 1072 if (!constructor) {
1068 return requireConstructor ? null : 1073 return requireConstructor ? null :
1069 %_CallFunction(obj.receiver, ObjectToString); 1074 %_CallFunction(obj.receiver, ObjectToString);
1070 } 1075 }
1071 var constructorName = constructor.name; 1076 var constructorName = constructor.name;
1072 if (!constructorName) { 1077 if (!constructorName) {
1073 return requireConstructor ? null : 1078 return requireConstructor ? null :
1074 %_CallFunction(obj.receiver, ObjectToString); 1079 %_CallFunction(obj.receiver, ObjectToString);
1075 } 1080 }
1076 return constructorName; 1081 return constructorName;
1077 } 1082 }
1078 1083
1084
1079 function captureStackTrace(obj, cons_opt) { 1085 function captureStackTrace(obj, cons_opt) {
1080 var stackTraceLimit = $Error.stackTraceLimit; 1086 var stackTraceLimit = $Error.stackTraceLimit;
1081 if (!stackTraceLimit || !IS_NUMBER(stackTraceLimit)) return; 1087 if (!stackTraceLimit || !IS_NUMBER(stackTraceLimit)) return;
1082 if (stackTraceLimit < 0 || stackTraceLimit > 10000) { 1088 if (stackTraceLimit < 0 || stackTraceLimit > 10000) {
1083 stackTraceLimit = 10000; 1089 stackTraceLimit = 10000;
1084 } 1090 }
1085 var raw_stack = %CollectStackTrace(obj, 1091 var raw_stack = %CollectStackTrace(obj,
1086 cons_opt ? cons_opt : captureStackTrace, 1092 cons_opt ? cons_opt : captureStackTrace,
1087 stackTraceLimit); 1093 stackTraceLimit);
1094
1095 // Don't be lazy if the error stack formatting is custom (observable).
1096 if (IS_FUNCTION($Error.prepareStackTrace)) {
1097 var custom_stacktrace_fun = $Error.prepareStackTrace;
1098 // Use default error formatting for the case that custom formatting throws.
1099 $Error.prepareStackTrace = null;
1100 var array = [];
1101 %MoveArrayContents(GetStackFrames(raw_stack), array);
1102 obj.stack = custom_stacktrace_fun(obj, array);
1103 $Error.prepareStackTrace = custom_stacktrace_fun;
1104 return;
1105 }
1106
1107 var error_string = FormatErrorString(obj);
1088 // Note that 'obj' and 'this' maybe different when called on objects that 1108 // Note that 'obj' and 'this' maybe different when called on objects that
1089 // have the error object on its prototype chain. The getter replaces itself 1109 // have the error object on its prototype chain. The getter replaces itself
1090 // with a data property as soon as the stack trace has been formatted. 1110 // with a data property as soon as the stack trace has been formatted.
1091 var getter = function() { 1111 var getter = function() {
1092 var value = FormatRawStackTrace(obj, raw_stack); 1112 var value = FormatStackTrace(error_string, GetStackFrames(raw_stack));
1093 %DefineOrRedefineDataProperty(obj, 'stack', value, NONE); 1113 %DefineOrRedefineDataProperty(obj, 'stack', value, NONE);
1094 return value; 1114 return value;
1095 }; 1115 };
1116 %MarkOneShotGetter(getter);
1117
1096 // The 'stack' property of the receiver is set as data property. If 1118 // The 'stack' property of the receiver is set as data property. If
1097 // the receiver is the same as holder, this accessor pair is replaced. 1119 // the receiver is the same as holder, this accessor pair is replaced.
1098 var setter = function(v) { 1120 var setter = function(v) {
1099 %DefineOrRedefineDataProperty(this, 'stack', v, NONE); 1121 %DefineOrRedefineDataProperty(this, 'stack', v, NONE);
1100 }; 1122 };
1101 1123
1102 %DefineOrRedefineAccessorProperty(obj, 'stack', getter, setter, DONT_ENUM); 1124 %DefineOrRedefineAccessorProperty(obj, 'stack', getter, setter, DONT_ENUM);
1103 } 1125 }
1104 1126
1105 1127
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
1232 // Boilerplate for exceptions for stack overflows. Used from 1254 // Boilerplate for exceptions for stack overflows. Used from
1233 // Isolate::StackOverflow(). 1255 // Isolate::StackOverflow().
1234 function SetUpStackOverflowBoilerplate() { 1256 function SetUpStackOverflowBoilerplate() {
1235 var boilerplate = MakeRangeError('stack_overflow', []); 1257 var boilerplate = MakeRangeError('stack_overflow', []);
1236 1258
1237 // The raw stack trace is stored as hidden property of the copy of this 1259 // The raw stack trace is stored as hidden property of the copy of this
1238 // boilerplate error object. Note that the receiver 'this' may not be that 1260 // boilerplate error object. Note that the receiver 'this' may not be that
1239 // error object copy, but can be found on the prototype chain of 'this'. 1261 // error object copy, but can be found on the prototype chain of 'this'.
1240 // When the stack trace is formatted, this accessor property is replaced by 1262 // When the stack trace is formatted, this accessor property is replaced by
1241 // a data property. 1263 // a data property.
1264 var error_string = boilerplate.name + ": " + boilerplate.message;
1265
1242 function getter() { 1266 function getter() {
1243 var holder = this; 1267 var holder = this;
1244 while (!IS_ERROR(holder)) { 1268 while (!IS_ERROR(holder)) {
1245 holder = %GetPrototype(holder); 1269 holder = %GetPrototype(holder);
1246 if (holder == null) return MakeSyntaxError('illegal_access', []); 1270 if (holder == null) return MakeSyntaxError('illegal_access', []);
1247 } 1271 }
1248 var raw_stack = %GetOverflowedRawStackTrace(holder); 1272 var raw_stack = %GetOverflowedRawStackTrace(holder);
1249 var result = IS_ARRAY(raw_stack) ? FormatRawStackTrace(holder, raw_stack) 1273 var result = IS_ARRAY(raw_stack)
1250 : void 0; 1274 ? FormatStackTrace(error_string, GetStackFrames(raw_stack))
1275 : void 0;
1251 %DefineOrRedefineDataProperty(holder, 'stack', result, NONE); 1276 %DefineOrRedefineDataProperty(holder, 'stack', result, NONE);
1252 return result; 1277 return result;
1253 } 1278 }
1279 %MarkOneShotGetter(getter);
1254 1280
1255 // The 'stack' property of the receiver is set as data property. If 1281 // The 'stack' property of the receiver is set as data property. If
1256 // the receiver is the same as holder, this accessor pair is replaced. 1282 // the receiver is the same as holder, this accessor pair is replaced.
1257 function setter(v) { 1283 function setter(v) {
1258 %DefineOrRedefineDataProperty(this, 'stack', v, NONE); 1284 %DefineOrRedefineDataProperty(this, 'stack', v, NONE);
1259 } 1285 }
1260 1286
1261 %DefineOrRedefineAccessorProperty( 1287 %DefineOrRedefineAccessorProperty(
1262 boilerplate, 'stack', getter, setter, DONT_ENUM); 1288 boilerplate, 'stack', getter, setter, DONT_ENUM);
1263 1289
1264 return boilerplate; 1290 return boilerplate;
1265 } 1291 }
1266 1292
1267 var kStackOverflowBoilerplate = SetUpStackOverflowBoilerplate(); 1293 var kStackOverflowBoilerplate = SetUpStackOverflowBoilerplate();
OLDNEW
« no previous file with comments | « src/mark-compact.cc ('k') | src/runtime.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698