Wed, 09 Oct 2013 14:50:39 +0200
8026008: Constant folding removes var statement
Reviewed-by: sundar, jlaskey
1.1 --- a/src/jdk/nashorn/internal/codegen/FoldConstants.java Wed Oct 09 13:26:23 2013 +0200 1.2 +++ b/src/jdk/nashorn/internal/codegen/FoldConstants.java Wed Oct 09 14:50:39 2013 +0200 1.3 @@ -25,6 +25,8 @@ 1.4 1.5 package jdk.nashorn.internal.codegen; 1.6 1.7 +import java.util.ArrayList; 1.8 +import java.util.List; 1.9 import jdk.nashorn.internal.codegen.types.Type; 1.10 import jdk.nashorn.internal.ir.BinaryNode; 1.11 import jdk.nashorn.internal.ir.Block; 1.12 @@ -37,8 +39,10 @@ 1.13 import jdk.nashorn.internal.ir.LiteralNode; 1.14 import jdk.nashorn.internal.ir.LiteralNode.ArrayLiteralNode; 1.15 import jdk.nashorn.internal.ir.Node; 1.16 +import jdk.nashorn.internal.ir.Statement; 1.17 import jdk.nashorn.internal.ir.TernaryNode; 1.18 import jdk.nashorn.internal.ir.UnaryNode; 1.19 +import jdk.nashorn.internal.ir.VarNode; 1.20 import jdk.nashorn.internal.ir.visitor.NodeVisitor; 1.21 import jdk.nashorn.internal.runtime.DebugLogger; 1.22 import jdk.nashorn.internal.runtime.JSType; 1.23 @@ -89,11 +93,21 @@ 1.24 public Node leaveIfNode(final IfNode ifNode) { 1.25 final Node test = ifNode.getTest(); 1.26 if (test instanceof LiteralNode.PrimitiveLiteralNode) { 1.27 - final Block shortCut = ((LiteralNode.PrimitiveLiteralNode<?>)test).isTrue() ? ifNode.getPass() : ifNode.getFail(); 1.28 - if (shortCut != null) { 1.29 - return new BlockStatement(ifNode.getLineNumber(), shortCut); 1.30 + final boolean isTrue = ((LiteralNode.PrimitiveLiteralNode<?>)test).isTrue(); 1.31 + final Block executed = isTrue ? ifNode.getPass() : ifNode.getFail(); 1.32 + final Block dropped = isTrue ? ifNode.getFail() : ifNode.getPass(); 1.33 + final List<Statement> statements = new ArrayList<>(); 1.34 + 1.35 + if (executed != null) { 1.36 + statements.addAll(executed.getStatements()); // Get statements form executed branch 1.37 } 1.38 - return new EmptyNode(ifNode); 1.39 + if (dropped != null) { 1.40 + extractVarNodes(dropped, statements); // Get var-nodes from non-executed branch 1.41 + } 1.42 + if (statements.isEmpty()) { 1.43 + return new EmptyNode(ifNode); 1.44 + } 1.45 + return BlockStatement.createReplacement(ifNode, ifNode.getFinish(), statements); 1.46 } 1.47 return ifNode; 1.48 } 1.49 @@ -131,6 +145,17 @@ 1.50 protected abstract LiteralNode<?> eval(); 1.51 } 1.52 1.53 + private static void extractVarNodes(final Block block, final List<Statement> statements) { 1.54 + final LexicalContext lc = new LexicalContext(); 1.55 + block.accept(lc, new NodeVisitor<LexicalContext>(lc) { 1.56 + @Override 1.57 + public boolean enterVarNode(VarNode varNode) { 1.58 + statements.add(varNode.setInit(null)); 1.59 + return false; 1.60 + } 1.61 + }); 1.62 + } 1.63 + 1.64 private static class UnaryNodeConstantEvaluator extends ConstantEvaluator<UnaryNode> { 1.65 UnaryNodeConstantEvaluator(final UnaryNode parent) { 1.66 super(parent);
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/script/basic/JDK-8026008.js Wed Oct 09 14:50:39 2013 +0200 2.3 @@ -0,0 +1,55 @@ 2.4 +/* 2.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +/** 2.28 + * JDK-8026008: Constant folding removes var statement 2.29 + * 2.30 + * @test 2.31 + * @run 2.32 + */ 2.33 + 2.34 +if (false) { 2.35 + var x1 = 10; 2.36 + if (false) { 2.37 + var x2; 2.38 + } 2.39 +} else { 2.40 + print(x1, x2); 2.41 +} 2.42 + 2.43 +if (undefined) { 2.44 + var z1; 2.45 + if (null) { 2.46 + var z2; 2.47 + } 2.48 +} 2.49 + 2.50 +print(z1, z2); 2.51 + 2.52 +if (1) { 2.53 + print(y1, y2); 2.54 +} else if (0) { 2.55 + var y1 = 1; 2.56 +} else { 2.57 + var y2 = 2 2.58 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/script/basic/JDK-8026008.js.EXPECTED Wed Oct 09 14:50:39 2013 +0200 3.3 @@ -0,0 +1,3 @@ 3.4 +undefined undefined 3.5 +undefined undefined 3.6 +undefined undefined