8030809: Anonymous functions should not be shown with internal names in script stack trace
authorsundar
Thu, 19 Dec 2013 21:53:27 +0530
changeset 22374 5231ab59e740
parent 22373 ca044992c73f
child 22375 1b3398bac257
8030809: Anonymous functions should not be shown with internal names in script stack trace Reviewed-by: lagergren, hannesw, jlaskey
nashorn/src/jdk/nashorn/api/scripting/NashornException.java
nashorn/src/jdk/nashorn/internal/codegen/CompilerConstants.java
nashorn/src/jdk/nashorn/internal/parser/Parser.java
nashorn/test/script/basic/JDK-8025515.js
nashorn/test/script/basic/JDK-8030809.js
nashorn/test/script/basic/JDK-8030809.js.EXPECTED
--- a/nashorn/src/jdk/nashorn/api/scripting/NashornException.java	Mon Dec 16 23:25:50 2013 +0530
+++ b/nashorn/src/jdk/nashorn/api/scripting/NashornException.java	Thu Dec 19 21:53:27 2013 +0530
@@ -158,6 +158,11 @@
                 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()));
             }
--- a/nashorn/src/jdk/nashorn/internal/codegen/CompilerConstants.java	Mon Dec 16 23:25:50 2013 +0530
+++ b/nashorn/src/jdk/nashorn/internal/codegen/CompilerConstants.java	Thu Dec 19 21:53:27 2013 +0530
@@ -76,7 +76,7 @@
     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"),
--- a/nashorn/src/jdk/nashorn/internal/parser/Parser.java	Mon Dec 16 23:25:50 2013 +0530
+++ b/nashorn/src/jdk/nashorn/internal/parser/Parser.java	Thu Dec 19 21:53:27 2013 +0530
@@ -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 @@
             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 @@
         // 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;
         }
--- a/nashorn/test/script/basic/JDK-8025515.js	Mon Dec 16 23:25:50 2013 +0530
+++ b/nashorn/test/script/basic/JDK-8025515.js	Thu Dec 19 21:53:27 2013 +0530
@@ -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 @@
 
 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");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8030809.js	Thu Dec 19 21:53:27 2013 +0530
@@ -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, '/'));
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8030809.js.EXPECTED	Thu Dec 19 21:53:27 2013 +0530
@@ -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)