[PATCH] 8156896: Script stack trace should display function names

Vivin Suresh Paliath vivin.paliath at gmail.com
Fri May 13 18:37:35 UTC 2016


This is Hannes' fix backported to jdk8. I see that the original issue has
been closed, so I am not sure if I should be using the same id or not.

I did try running the tests, but JDK-814421.js fails because there seems to
be some issue with arguments when they are passed along with the shebang
(everything after 19 characters is chopped off).

I have added a test for 8156896, but I wasn't able to run it because of the
failing test. I had a quick look at the build.xml file but couldn't tell
how the tests in script/basic get run.

-- 
*[vivin.net <http://vivin.net> :: github <http://github.com/vivin> ::
linkedin <https://www.linkedin.com/in/vivin>]*
-------------- next part --------------
diff --git a/src/jdk/nashorn/api/scripting/NashornException.java b/src/jdk/nashorn/api/scripting/NashornException.java
--- a/src/jdk/nashorn/api/scripting/NashornException.java
+++ b/src/jdk/nashorn/api/scripting/NashornException.java
@@ -175,10 +175,8 @@
                 String methodName = st.getMethodName();
                 if (methodName.equals(CompilerConstants.PROGRAM.symbolName())) {
                     methodName = "<program>";
-                }
-
-                if (methodName.contains(CompilerConstants.ANON_FUNCTION_PREFIX.symbolName())) {
-                    methodName = "<anonymous>";
+                } else {
+                    methodName = stripMethodName(methodName);
                 }
 
                 filtered.add(new StackTraceElement(className, methodName,
@@ -188,6 +186,22 @@
         return filtered.toArray(new StackTraceElement[filtered.size()]);
     }
 
+    private static String stripMethodName(final String methodName) {
+        String name = methodName;
+
+        final int nestedSeparator = name.lastIndexOf(CompilerConstants.NESTED_FUNCTION_SEPARATOR.symbolName());
+        if (nestedSeparator >= 0) {
+            name = name.substring(nestedSeparator + 1);
+        }
+
+        final int idSeparator = name.indexOf(CompilerConstants.ID_FUNCTION_SEPARATOR.symbolName());
+        if (idSeparator >= 0) {
+            name = name.substring(0, idSeparator);
+        }
+
+        return name.contains(CompilerConstants.ANON_FUNCTION_PREFIX.symbolName()) ? "<anonymous>" : name;
+    }
+
     /**
      * Return a formatted script stack trace string with frames information separated by '\n'
      *
diff --git a/src/jdk/nashorn/internal/codegen/CompilerConstants.java b/src/jdk/nashorn/internal/codegen/CompilerConstants.java
--- a/src/jdk/nashorn/internal/codegen/CompilerConstants.java
+++ b/src/jdk/nashorn/internal/codegen/CompilerConstants.java
@@ -78,6 +78,12 @@
     /** function prefix for anonymous functions */
     ANON_FUNCTION_PREFIX("L:"),
 
+    /** separator for method names of nested functions */
+    NESTED_FUNCTION_SEPARATOR("#"),
+
+    /** separator for making method names unique by appending numberic ids */
+    ID_FUNCTION_SEPARATOR("-"),
+
     /** method name for Java method that is the program entry point */
     PROGRAM(":program"),
 
diff --git a/src/jdk/nashorn/internal/codegen/Namespace.java b/src/jdk/nashorn/internal/codegen/Namespace.java
--- a/src/jdk/nashorn/internal/codegen/Namespace.java
+++ b/src/jdk/nashorn/internal/codegen/Namespace.java
@@ -68,7 +68,7 @@
     }
 
     /**
-     * Create a uniqueName name in the namespace in the form base$n where n varies.
+     * Create a uniqueName name in the namespace in the form base-n where n varies.
      * Also truncates very long names that would otherwise break ASM.
      *
      * @param base Base of name.  Base will be returned if uniqueName.
@@ -83,7 +83,7 @@
             if (counter != null) {
                 final int count = counter + 1;
                 namespaceDirectory.put(truncatedBase, count);
-                return truncatedBase + '-' + count;
+                return truncatedBase + CompilerConstants.ID_FUNCTION_SEPARATOR.symbolName() + count;
             }
         }
 
diff --git a/src/jdk/nashorn/internal/parser/Parser.java b/src/jdk/nashorn/internal/parser/Parser.java
--- a/src/jdk/nashorn/internal/parser/Parser.java
+++ b/src/jdk/nashorn/internal/parser/Parser.java
@@ -459,7 +459,7 @@
 
         final FunctionNode parentFunction = lc.getCurrentFunction();
         if (parentFunction != null && !parentFunction.isProgram()) {
-            sb.append(parentFunction.getName()).append('$');
+            sb.append(parentFunction.getName()).append(CompilerConstants.NESTED_FUNCTION_SEPARATOR.symbolName());
         }
 
         assert ident.getName() != null;
diff --git a/test/script/basic/JDK-8025515.js b/test/script/basic/JDK-8025515.js
--- a/test/script/basic/JDK-8025515.js
+++ b/test/script/basic/JDK-8025515.js
@@ -61,8 +61,8 @@
 var f = (function() {
     return function() { a.b.c; };
 })();
-testMethodName(f, "f$L:62");
+testMethodName(f, "f#L:62");
 
 testMethodName((function() {
     return function() { return a.b.c; };
-})(), "L:66$L:67");
+})(), "L:66#L:67");
diff --git a/test/script/basic/JDK-8156896.js b/test/script/basic/JDK-8156896.js
new file mode 100644
--- /dev/null
+++ b/test/script/basic/JDK-8156896.js
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8156896: Script tack trace should display function names
+ *
+ * @test
+ * @run
+ */
+
+(function() {
+    return function foo() {
+        try {
+            throw new Error();
+        } catch(e) {
+            print(e.stack);
+        }
+    }
+})()();
+
+new Function([], "return function foo() { try { throw new Error(); } catch(e) { print(e.stack); } }")()();
+
+var f = (function() {
+    return function foo() {
+        try {
+            throw new Error();
+        } catch(e) {
+            print(e.stack);
+        }
+    }
+})();
+
+f();
+
diff --git a/test/script/basic/JDK-8156896.js.EXPECTED b/test/script/basic/JDK-8156896.js.EXPECTED
new file mode 100644
--- /dev/null
+++ b/test/script/basic/JDK-8156896.js.EXPECTED
@@ -0,0 +1,9 @@
+Error
+	at foo (test.js:4)
+	at <program> (test.js:1)
+Error
+	at foo (<function>:2)
+	at <program> (test.js:11)
+Error
+	at foo (test.js:16)
+	at <program> (test.js:23)


More information about the nashorn-dev mailing list