8086052: Script evaluation should not return last function declaration

Reviewed-by: sundar, attila
This commit is contained in:
Hannes Wallnöfer 2015-06-15 15:37:01 +02:00
parent 907f7f2c7c
commit 21c3399f5e
5 changed files with 13 additions and 8 deletions
nashorn
samples
src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen
test
script/basic
src/jdk/nashorn/api/scripting/test

@ -63,5 +63,3 @@ function fields(jobj) {
}
}
}
undefined;

@ -584,7 +584,9 @@ final class Lower extends NodeOperatorVisitor<BlockLexicalContext> implements Lo
@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;

@ -59,10 +59,14 @@ if (!(str1 === str2)){
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();

@ -5,7 +5,8 @@ undefined
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!

@ -204,7 +204,7 @@ public class ScriptObjectMirrorTest {
}
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();