8086052: Script evaluation should not return last function declaration
Reviewed-by: sundar, attila
--- a/nashorn/samples/javahelp.js Fri Jun 12 16:55:20 2015 +0530
+++ b/nashorn/samples/javahelp.js Mon Jun 15 15:37:01 2015 +0200
@@ -63,5 +63,3 @@
}
}
}
-
-undefined;
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/Lower.java Fri Jun 12 16:55:20 2015 +0530
+++ b/nashorn/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/nashorn/test/script/basic/evalreturn.js Fri Jun 12 16:55:20 2015 +0530
+++ b/nashorn/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/nashorn/test/script/basic/evalreturn.js.EXPECTED Fri Jun 12 16:55:20 2015 +0530
+++ b/nashorn/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/nashorn/test/src/jdk/nashorn/api/scripting/test/ScriptObjectMirrorTest.java Fri Jun 12 16:55:20 2015 +0530
+++ b/nashorn/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();