8156896: Script stack trace should display function names

Reviewed-by: attila, sundar
This commit is contained in:
Hannes Wallnöfer 2016-05-13 14:30:54 +02:00
parent cb8de1cae1
commit 96e6ae9274
6 changed files with 88 additions and 9 deletions

View File

@ -174,10 +174,8 @@ public abstract class NashornException extends RuntimeException {
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 @@ public abstract class NashornException extends RuntimeException {
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'
*

View File

@ -78,6 +78,12 @@ public enum CompilerConstants {
/** 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"),

View File

@ -68,7 +68,7 @@ public class Namespace {
}
/**
* 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 @@ public class Namespace {
if (counter != null) {
final int count = counter + 1;
namespaceDirectory.put(truncatedBase, count);
return truncatedBase + '-' + count;
return truncatedBase + CompilerConstants.ID_FUNCTION_SEPARATOR.symbolName() + count;
}
}

View File

@ -534,7 +534,7 @@ public class Parser extends AbstractParser implements Loggable {
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;

View File

@ -61,8 +61,8 @@ testMethodName(function() { throw new Error() }, "L:59");
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");

View File

@ -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());