changeset 1318:bd783ddc4333

8086052: Script evaluation should not return last function declaration Reviewed-by: sundar, attila
author hannesw
date Mon, 15 Jun 2015 15:37:01 +0200
parents 5808c1886a90
children 0e28af5ee013
files samples/javahelp.js src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/Lower.java test/script/basic/evalreturn.js test/script/basic/evalreturn.js.EXPECTED test/src/jdk/nashorn/api/scripting/test/ScriptObjectMirrorTest.java
diffstat 5 files changed, 13 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/samples/javahelp.js	Fri Jun 12 16:55:20 2015 +0530
+++ b/samples/javahelp.js	Mon Jun 15 15:37:01 2015 +0200
@@ -63,5 +63,3 @@
         }
     }
 }
-
-undefined;
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/Lower.java	Fri Jun 12 16:55:20 2015 +0530
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/Lower.java	Mon Jun 15 15:37:01 2015 +0200
@@ -584,7 +584,9 @@
     @Override
     public Node leaveVarNode(final VarNode varNode) {
         addStatement(varNode);
-        if (varNode.getFlag(VarNode.IS_LAST_FUNCTION_DECLARATION) && lc.getCurrentFunction().isProgram()) {
+        if (varNode.getFlag(VarNode.IS_LAST_FUNCTION_DECLARATION)
+                && lc.getCurrentFunction().isProgram()
+                && ((FunctionNode) varNode.getInit()).isAnonymous()) {
             new ExpressionStatement(varNode.getLineNumber(), varNode.getToken(), varNode.getFinish(), new IdentNode(varNode.getName())).accept(this);
         }
         return varNode;
--- a/test/script/basic/evalreturn.js	Fri Jun 12 16:55:20 2015 +0530
+++ b/test/script/basic/evalreturn.js	Mon Jun 15 15:37:01 2015 +0200
@@ -59,10 +59,14 @@
 
 print("Scoping OK");
 
-var f = eval("function cookie() { print('sweet and crunchy!'); } function cake() { print('moist and delicious!'); }");
+// According to the spec, evaluation of function declarations should not return a value,
+// but we return values of anonymous function declarations (Nashorn extension).
+var e = eval("function cookie() { print('sweet and crunchy!'); } function cake() { print('moist and delicious!'); }");
+print(e);
+var f = eval("function cookie() { print('sweet and crunchy!'); } function() { print('moist and delicious!'); }");
 print(f);
 f();
-var g = eval("function cake() { print('moist and delicious!'); } function cookie() { print('sweet and crunchy!'); }");
+var g = eval("function cake() { print('moist and delicious!'); } function() { print('sweet and crunchy!'); }");
 print(g);
 g();
 
--- a/test/script/basic/evalreturn.js.EXPECTED	Fri Jun 12 16:55:20 2015 +0530
+++ b/test/script/basic/evalreturn.js.EXPECTED	Mon Jun 15 15:37:01 2015 +0200
@@ -5,7 +5,8 @@
 undefined
 hello
 Scoping OK
-function cake() { print('moist and delicious!'); }
+undefined
+function() { print('moist and delicious!'); }
 moist and delicious!
-function cookie() { print('sweet and crunchy!'); }
+function() { print('sweet and crunchy!'); }
 sweet and crunchy!
--- a/test/src/jdk/nashorn/api/scripting/test/ScriptObjectMirrorTest.java	Fri Jun 12 16:55:20 2015 +0530
+++ b/test/src/jdk/nashorn/api/scripting/test/ScriptObjectMirrorTest.java	Mon Jun 15 15:37:01 2015 +0200
@@ -204,7 +204,7 @@
         }
 
         try {
-            final Object obj = e.eval("function func() { print('hello'); }");
+            final Object obj = e.eval("(function func() { print('hello'); })");
             assertEquals(obj.toString(), "function func() { print('hello'); }", "toString returns wrong value");
         } catch (final Throwable t) {
             t.printStackTrace();