Revision: 11652
Author:   [email protected]
Date:     Thu May 24 04:00:05 2012
Log:      messages.js: Get better function names in stack traces.

CallSite.getFunctionName() is able to retrieve names for functions better than
getFunction().name.  Use it in CallSite.toString().

Code by [email protected].

BUG=NONE
TEST=stack-traces.js: Added testClassNames.

Review URL: https://chromiumcodereview.appspot.com/10384196
http://code.google.com/p/v8/source/detail?r=11652

Modified:
 /branches/bleeding_edge/src/messages.js
 /branches/bleeding_edge/test/mjsunit/stack-traces.js

=======================================
--- /branches/bleeding_edge/src/messages.js     Fri May 18 02:45:10 2012
+++ /branches/bleeding_edge/src/messages.js     Thu May 24 04:00:05 2012
@@ -788,15 +788,7 @@
 }

 function CallSiteGetTypeName() {
-  var constructor = this.receiver.constructor;
-  if (!constructor) {
-    return %_CallFunction(this.receiver, ObjectToString);
-  }
-  var constructorName = constructor.name;
-  if (!constructorName) {
-    return %_CallFunction(this.receiver, ObjectToString);
-  }
-  return constructorName;
+  return GetTypeName(this, false);
 }

 function CallSiteIsToplevel() {
@@ -830,8 +822,10 @@
   var name = this.fun.name;
   if (name) {
     return name;
-  } else {
-    return %FunctionGetInferredName(this.fun);
+  }
+  name = %FunctionGetInferredName(this.fun);
+  if (name) {
+    return name;
   }
   // Maybe this is an evaluation?
   var script = %FunctionGetScript(this.fun);
@@ -952,20 +946,24 @@
     fileLocation = "unknown source";
   }
   var line = "";
-  var functionName = this.getFunction().name;
-  var addPrefix = true;
+  var functionName = this.getFunctionName();
+  var addSuffix = true;
   var isConstructor = this.isConstructor();
   var isMethodCall = !(this.isToplevel() || isConstructor);
   if (isMethodCall) {
+    var typeName = GetTypeName(this, true);
     var methodName = this.getMethodName();
-    line += this.getTypeName() + ".";
     if (functionName) {
+      if (typeName && functionName.indexOf(typeName) != 0) {
+        line += typeName + ".";
+      }
       line += functionName;
-      if (methodName && (methodName != functionName)) {
+      if (methodName && functionName.lastIndexOf("." + methodName) !=
+          functionName.length - methodName.length - 1) {
         line += " [as " + methodName + "]";
       }
     } else {
-      line += methodName || "<anonymous>";
+      line += typeName + "." + (methodName || "<anonymous>");
     }
   } else if (isConstructor) {
     line += "new " + (functionName || "<anonymous>");
@@ -973,9 +971,9 @@
     line += functionName;
   } else {
     line += fileLocation;
-    addPrefix = false;
-  }
-  if (addPrefix) {
+    addSuffix = false;
+  }
+  if (addSuffix) {
     line += " (" + fileLocation + ")";
   }
   return line;
@@ -1085,6 +1083,19 @@
   }
 }

+function GetTypeName(obj, requireConstructor) {
+  var constructor = obj.receiver.constructor;
+  if (!constructor) {
+    return requireConstructor ? null :
+        %_CallFunction(obj.receiver, ObjectToString);
+  }
+  var constructorName = constructor.name;
+  if (!constructorName) {
+    return requireConstructor ? null :
+        %_CallFunction(obj.receiver, ObjectToString);
+  }
+  return constructorName;
+}

 function captureStackTrace(obj, cons_opt) {
   var stackTraceLimit = $Error.stackTraceLimit;
=======================================
--- /branches/bleeding_edge/test/mjsunit/stack-traces.js Thu Oct 20 05:31:33 2011 +++ /branches/bleeding_edge/test/mjsunit/stack-traces.js Thu May 24 04:00:05 2012
@@ -110,6 +110,18 @@
 function testStrippedCustomError() {
   throw new CustomError("hep-hey", CustomError);
 }
+
+MyObj = function() { FAIL; }
+
+MyObjCreator = function() {}
+
+MyObjCreator.prototype.Create = function() {
+  return new MyObj();
+}
+
+function testClassNames() {
+  (new MyObjCreator).Create();
+}

 // Utility function for testing that the expected strings occur
 // in the stack trace produced when running the given function.
@@ -254,6 +266,8 @@
     ["collectStackTrace"]);
 testTrace("testStrippedCustomError", testStrippedCustomError, ["hep-hey"],
     ["new CustomError", "collectStackTrace"]);
+testTrace("testClassNames", testClassNames,
+          ["new MyObj", "MyObjCreator.Create"], ["as Create"]);
 testCallerCensorship();
 testUnintendedCallerCensorship();
 testErrorsDuringFormatting();

--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to