8066224: fixes for folding a constant-test ternary operator
authorattila
Wed, 10 Dec 2014 11:55:25 +0100
changeset 27971 bff4872beee5
parent 27970 7b0048b90967
child 27972 8ec664fdf8da
8066224: fixes for folding a constant-test ternary operator Reviewed-by: hannesw, lagergren
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/CodeGenerator.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/FoldConstants.java
nashorn/test/script/basic/JDK-8066224.js
nashorn/test/script/basic/JDK-8066224.js.EXPECTED
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/CodeGenerator.java	Wed Dec 10 11:55:04 2014 +0100
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/CodeGenerator.java	Wed Dec 10 11:55:25 2014 +0100
@@ -2021,6 +2021,19 @@
         final Expression test = ifNode.getTest();
         final Block pass = ifNode.getPass();
         final Block fail = ifNode.getFail();
+
+        if (Expression.isAlwaysTrue(test)) {
+            loadAndDiscard(test);
+            pass.accept(this);
+            return false;
+        } else if (Expression.isAlwaysFalse(test)) {
+            loadAndDiscard(test);
+            if (fail != null) {
+                fail.accept(this);
+            }
+            return false;
+        }
+
         final boolean hasFailConversion = LocalVariableConversion.hasLiveConversion(ifNode);
 
         final Label failLabel  = new Label("if_fail");
@@ -2040,7 +2053,7 @@
             method.beforeJoinPoint(ifNode);
         }
 
-        if(afterLabel != null) {
+        if(afterLabel != null && afterLabel.isReachable()) {
             method.label(afterLabel);
         }
 
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/FoldConstants.java	Wed Dec 10 11:55:04 2014 +0100
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/FoldConstants.java	Wed Dec 10 11:55:25 2014 +0100
@@ -131,7 +131,7 @@
     public Node leaveTernaryNode(final TernaryNode ternaryNode) {
         final Node test = ternaryNode.getTest();
         if (test instanceof LiteralNode.PrimitiveLiteralNode) {
-            return ((LiteralNode.PrimitiveLiteralNode<?>)test).isTrue() ? ternaryNode.getTrueExpression() : ternaryNode.getFalseExpression();
+            return (((LiteralNode.PrimitiveLiteralNode<?>)test).isTrue() ? ternaryNode.getTrueExpression() : ternaryNode.getFalseExpression()).getExpression();
         }
         return ternaryNode;
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8066224.js	Wed Dec 10 11:55:25 2014 +0100
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8066224: fixes for folding a constant-test ternary operator
+ *
+ * @test
+ * @run
+ */
+
+print((function(){ 
+    if(false ? 0 : '') {
+        throw false;
+    } else if (x = this) {
+        var x = x; 
+    }
+    return x === this;
+})())
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8066224.js.EXPECTED	Wed Dec 10 11:55:25 2014 +0100
@@ -0,0 +1,1 @@
+true