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

View file

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

View file

@ -584,7 +584,9 @@ final class Lower extends NodeOperatorVisitor<BlockLexicalContext> implements Lo
@Override @Override
public Node leaveVarNode(final VarNode varNode) { public Node leaveVarNode(final VarNode varNode) {
addStatement(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); new ExpressionStatement(varNode.getLineNumber(), varNode.getToken(), varNode.getFinish(), new IdentNode(varNode.getName())).accept(this);
} }
return varNode; return varNode;

View file

@ -59,10 +59,14 @@ if (!(str1 === str2)){
print("Scoping OK"); 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); print(f);
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); print(g);
g(); g();

View file

@ -5,7 +5,8 @@ undefined
undefined undefined
hello hello
Scoping OK Scoping OK
function cake() { print('moist and delicious!'); } undefined
function() { print('moist and delicious!'); }
moist and delicious! moist and delicious!
function cookie() { print('sweet and crunchy!'); } function() { print('sweet and crunchy!'); }
sweet and crunchy! sweet and crunchy!

View file

@ -204,7 +204,7 @@ public class ScriptObjectMirrorTest {
} }
try { 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"); assertEquals(obj.toString(), "function func() { print('hello'); }", "toString returns wrong value");
} catch (final Throwable t) { } catch (final Throwable t) {
t.printStackTrace(); t.printStackTrace();