8030809: Anonymous functions should not be shown with internal names in script stack trace

Reviewed-by: lagergren, hannesw, jlaskey
This commit is contained in:
Athijegannathan Sundararajan 2013-12-19 21:53:27 +05:30
parent 1bef37e215
commit 1b8c152090
6 changed files with 72 additions and 10 deletions

View file

@ -158,6 +158,11 @@ public abstract class NashornException extends RuntimeException {
if (methodName.equals(CompilerConstants.RUN_SCRIPT.symbolName())) {
methodName = "<program>";
}
if (methodName.contains(CompilerConstants.ANON_FUNCTION_PREFIX.symbolName())) {
methodName = "<anonymous>";
}
filtered.add(new StackTraceElement(className, methodName,
st.getFileName(), st.getLineNumber()));
}

View file

@ -76,7 +76,7 @@ public enum CompilerConstants {
DEFAULT_SCRIPT_NAME("Script"),
/** function prefix for anonymous functions */
FUNCTION_PREFIX(":function$"),
ANON_FUNCTION_PREFIX("L:"),
/** method name for Java method that is script entry point */
RUN_SCRIPT("runScript"),

View file

@ -26,7 +26,7 @@
package jdk.nashorn.internal.parser;
import static jdk.nashorn.internal.codegen.CompilerConstants.EVAL;
import static jdk.nashorn.internal.codegen.CompilerConstants.FUNCTION_PREFIX;
import static jdk.nashorn.internal.codegen.CompilerConstants.ANON_FUNCTION_PREFIX;
import static jdk.nashorn.internal.codegen.CompilerConstants.RUN_SCRIPT;
import static jdk.nashorn.internal.parser.TokenType.ASSIGN;
import static jdk.nashorn.internal.parser.TokenType.CASE;
@ -389,7 +389,9 @@ loop:
sb.append(parentFunction.getName()).append('$');
}
sb.append(ident != null ? ident.getName() : FUNCTION_PREFIX.symbolName());
assert ident.getName() != null;
sb.append(ident.getName());
final String name = namespace.uniqueName(sb.toString());
assert parentFunction != null || name.equals(RUN_SCRIPT.symbolName()) : "name = " + name;// must not rename runScript().
@ -2448,7 +2450,7 @@ loop:
// name is null, generate anonymous name
boolean isAnonymous = false;
if (name == null) {
final String tmpName = "_L" + functionLine;
final String tmpName = ANON_FUNCTION_PREFIX.symbolName() + functionLine;
name = new IdentNode(functionToken, Token.descPosition(functionToken), tmpName);
isAnonymous = true;
}

View file

@ -30,13 +30,23 @@
// Make sure synthetic names of anonymous functions have correct line numbers
function getFirstScriptFrame(stack) {
for (frameNum in stack) {
var frame = stack[frameNum];
if (frame.className.startsWith("jdk.nashorn.internal.scripts.Script$")) {
return frame;
}
}
}
function testMethodName(f, expected) {
try {
f();
fail("expected error");
} catch (e) {
var stack = e.getStackTrace();
if (stack[0].methodName !== expected) {
var stack = e.nashornException.getStackTrace();
var name = getFirstScriptFrame(stack).methodName;
if (name !== expected) {
fail("got " + stack[0].methodName + ", expected " + expected);
}
}
@ -44,15 +54,15 @@ function testMethodName(f, expected) {
testMethodName(function() {
return a.b.c;
}, "_L45");
}, "L:55");
testMethodName(function() { throw new Error() }, "_L49");
testMethodName(function() { throw new Error() }, "L:59");
var f = (function() {
return function() { a.b.c; };
})();
testMethodName(f, "_L51$_L52");
testMethodName(f, "L:61$L:62");
testMethodName((function() {
return function() { return a.b.c; };
})(), "_L56$_L57");
})(), "L:66$L:67");

View file

@ -0,0 +1,41 @@
/*
* 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-8030809: Anonymous functions should not be shown with internal names in script stack trace
*
* @test
* @run
*/
function func() {
(function() {
throw new Error();
})();
}
try {
func();
} catch (e) {
print(e.stack.replace(/\\/g, '/'));
}

View file

@ -0,0 +1,4 @@
Error
at <anonymous> (test/script/basic/JDK-8030809.js:33)
at func (test/script/basic/JDK-8030809.js:32)
at <program> (test/script/basic/JDK-8030809.js:38)