8156896: Script stack trace should display function names
Reviewed-by: attila, sundar
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/scripting/NashornException.java Thu May 12 18:46:54 2016 +0000
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/scripting/NashornException.java Fri May 13 14:30:54 2016 +0200
@@ -174,10 +174,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,
@@ -187,6 +185,22 @@
return filtered.toArray(new StackTraceElement[0]);
}
+ 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'
*
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/CompilerConstants.java Thu May 12 18:46:54 2016 +0000
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/CompilerConstants.java Fri May 13 14:30:54 2016 +0200
@@ -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 numeric ids */
+ ID_FUNCTION_SEPARATOR("-"),
+
/** method name for Java method that is the program entry point */
PROGRAM(":program"),
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/Namespace.java Thu May 12 18:46:54 2016 +0000
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/Namespace.java Fri May 13 14:30:54 2016 +0200
@@ -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;
}
}
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/parser/Parser.java Thu May 12 18:46:54 2016 +0000
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/parser/Parser.java Fri May 13 14:30:54 2016 +0200
@@ -534,7 +534,7 @@
final ParserContextFunctionNode 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;
--- a/nashorn/test/script/basic/JDK-8025515.js Thu May 12 18:46:54 2016 +0000
+++ b/nashorn/test/script/basic/JDK-8025515.js Fri May 13 14:30:54 2016 +0200
@@ -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");
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8156896.js Fri May 13 14:30:54 2016 +0200
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2016, 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 stack trace should display function names
+ *
+ * @test
+ * @run
+ */
+
+function checkNamedFunction(stack) {
+ Assert.assertTrue(stack.indexOf("Error\n\tat bar (") === 0);
+}
+
+function checkAnonymousFunction(stack) {
+ Assert.assertTrue(stack.indexOf("Error\n\tat <anonymous> (") === 0);
+}
+
+// Named functions
+function bar() { try { throw new Error(); } catch(e) { return e.stack; } }
+checkNamedFunction(bar());
+
+bar = function() { try { throw new Error(); } catch(e) { return e.stack; } };
+checkNamedFunction(bar());
+
+f = (function() {return function bar() { try { throw new Error(); } catch(e) { return e.stack; } } })();
+checkNamedFunction(f());
+
+f = new Function("return function bar() { try { throw new Error(); } catch(e) { return e.stack; } }")();
+checkNamedFunction(f());
+
+// Anonymous functions
+checkAnonymousFunction((function() { try { throw new Error(); } catch(e) { return e.stack; } })());
+
+f = (function() {return function() { try { throw new Error(); } catch(e) { return e.stack; } } })();
+checkAnonymousFunction(f());
+
+f = new Function("return function() { try { throw new Error(); } catch(e) { return e.stack; } }")();
+checkAnonymousFunction(f());