mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8030809: Anonymous functions should not be shown with internal names in script stack trace
Reviewed-by: lagergren, hannesw, jlaskey
This commit is contained in:
parent
1bef37e215
commit
1b8c152090
6 changed files with 72 additions and 10 deletions
|
@ -158,6 +158,11 @@ public abstract class NashornException extends RuntimeException {
|
||||||
if (methodName.equals(CompilerConstants.RUN_SCRIPT.symbolName())) {
|
if (methodName.equals(CompilerConstants.RUN_SCRIPT.symbolName())) {
|
||||||
methodName = "<program>";
|
methodName = "<program>";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (methodName.contains(CompilerConstants.ANON_FUNCTION_PREFIX.symbolName())) {
|
||||||
|
methodName = "<anonymous>";
|
||||||
|
}
|
||||||
|
|
||||||
filtered.add(new StackTraceElement(className, methodName,
|
filtered.add(new StackTraceElement(className, methodName,
|
||||||
st.getFileName(), st.getLineNumber()));
|
st.getFileName(), st.getLineNumber()));
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,7 +76,7 @@ public enum CompilerConstants {
|
||||||
DEFAULT_SCRIPT_NAME("Script"),
|
DEFAULT_SCRIPT_NAME("Script"),
|
||||||
|
|
||||||
/** function prefix for anonymous functions */
|
/** function prefix for anonymous functions */
|
||||||
FUNCTION_PREFIX(":function$"),
|
ANON_FUNCTION_PREFIX("L:"),
|
||||||
|
|
||||||
/** method name for Java method that is script entry point */
|
/** method name for Java method that is script entry point */
|
||||||
RUN_SCRIPT("runScript"),
|
RUN_SCRIPT("runScript"),
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
package jdk.nashorn.internal.parser;
|
package jdk.nashorn.internal.parser;
|
||||||
|
|
||||||
import static jdk.nashorn.internal.codegen.CompilerConstants.EVAL;
|
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.codegen.CompilerConstants.RUN_SCRIPT;
|
||||||
import static jdk.nashorn.internal.parser.TokenType.ASSIGN;
|
import static jdk.nashorn.internal.parser.TokenType.ASSIGN;
|
||||||
import static jdk.nashorn.internal.parser.TokenType.CASE;
|
import static jdk.nashorn.internal.parser.TokenType.CASE;
|
||||||
|
@ -389,7 +389,9 @@ loop:
|
||||||
sb.append(parentFunction.getName()).append('$');
|
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());
|
final String name = namespace.uniqueName(sb.toString());
|
||||||
assert parentFunction != null || name.equals(RUN_SCRIPT.symbolName()) : "name = " + name;// must not rename runScript().
|
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
|
// name is null, generate anonymous name
|
||||||
boolean isAnonymous = false;
|
boolean isAnonymous = false;
|
||||||
if (name == null) {
|
if (name == null) {
|
||||||
final String tmpName = "_L" + functionLine;
|
final String tmpName = ANON_FUNCTION_PREFIX.symbolName() + functionLine;
|
||||||
name = new IdentNode(functionToken, Token.descPosition(functionToken), tmpName);
|
name = new IdentNode(functionToken, Token.descPosition(functionToken), tmpName);
|
||||||
isAnonymous = true;
|
isAnonymous = true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,13 +30,23 @@
|
||||||
|
|
||||||
// Make sure synthetic names of anonymous functions have correct line numbers
|
// 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) {
|
function testMethodName(f, expected) {
|
||||||
try {
|
try {
|
||||||
f();
|
f();
|
||||||
fail("expected error");
|
fail("expected error");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
var stack = e.getStackTrace();
|
var stack = e.nashornException.getStackTrace();
|
||||||
if (stack[0].methodName !== expected) {
|
var name = getFirstScriptFrame(stack).methodName;
|
||||||
|
if (name !== expected) {
|
||||||
fail("got " + stack[0].methodName + ", expected " + expected);
|
fail("got " + stack[0].methodName + ", expected " + expected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,15 +54,15 @@ function testMethodName(f, expected) {
|
||||||
|
|
||||||
testMethodName(function() {
|
testMethodName(function() {
|
||||||
return a.b.c;
|
return a.b.c;
|
||||||
}, "_L45");
|
}, "L:55");
|
||||||
|
|
||||||
testMethodName(function() { throw new Error() }, "_L49");
|
testMethodName(function() { throw new Error() }, "L:59");
|
||||||
|
|
||||||
var f = (function() {
|
var f = (function() {
|
||||||
return function() { a.b.c; };
|
return function() { a.b.c; };
|
||||||
})();
|
})();
|
||||||
testMethodName(f, "_L51$_L52");
|
testMethodName(f, "L:61$L:62");
|
||||||
|
|
||||||
testMethodName((function() {
|
testMethodName((function() {
|
||||||
return function() { return a.b.c; };
|
return function() { return a.b.c; };
|
||||||
})(), "_L56$_L57");
|
})(), "L:66$L:67");
|
||||||
|
|
41
nashorn/test/script/basic/JDK-8030809.js
Normal file
41
nashorn/test/script/basic/JDK-8030809.js
Normal 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, '/'));
|
||||||
|
}
|
4
nashorn/test/script/basic/JDK-8030809.js.EXPECTED
Normal file
4
nashorn/test/script/basic/JDK-8030809.js.EXPECTED
Normal 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)
|
Loading…
Add table
Add a link
Reference in a new issue