changeset 699:8c0524412634

8030809: Anonymous functions should not be shown with internal names in script stack trace Reviewed-by: lagergren, hannesw, jlaskey
author sundar
date Thu, 19 Dec 2013 21:53:27 +0530
parents cade47c36c6c
children 78cdf566535d
files src/jdk/nashorn/api/scripting/NashornException.java src/jdk/nashorn/internal/codegen/CompilerConstants.java src/jdk/nashorn/internal/parser/Parser.java test/script/basic/JDK-8025515.js test/script/basic/JDK-8030809.js test/script/basic/JDK-8030809.js.EXPECTED
diffstat 6 files changed, 72 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk/nashorn/api/scripting/NashornException.java	Mon Dec 16 23:25:50 2013 +0530
+++ b/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/src/jdk/nashorn/internal/codegen/CompilerConstants.java	Mon Dec 16 23:25:50 2013 +0530
+++ b/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/src/jdk/nashorn/internal/parser/Parser.java	Mon Dec 16 23:25:50 2013 +0530
+++ b/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/test/script/basic/JDK-8025515.js	Mon Dec 16 23:25:50 2013 +0530
+++ b/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/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/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)