8066224: fixes for folding a constant-test ternary operator

Reviewed-by: hannesw, lagergren
diff --git a/nashorn/src/jdk/nashorn/internal/codegen/CodeGenerator.java b/nashorn/src/jdk/nashorn/internal/codegen/CodeGenerator.java
index 3ef1b08..8d6f980 100644
--- a/nashorn/src/jdk/nashorn/internal/codegen/CodeGenerator.java
+++ b/nashorn/src/jdk/nashorn/internal/codegen/CodeGenerator.java
@@ -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);
         }
 
diff --git a/nashorn/src/jdk/nashorn/internal/codegen/FoldConstants.java b/nashorn/src/jdk/nashorn/internal/codegen/FoldConstants.java
index 72f9fd1..092b788 100644
--- a/nashorn/src/jdk/nashorn/internal/codegen/FoldConstants.java
+++ b/nashorn/src/jdk/nashorn/internal/codegen/FoldConstants.java
@@ -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;
     }
diff --git a/nashorn/test/script/basic/JDK-8066224.js b/nashorn/test/script/basic/JDK-8066224.js
new file mode 100644
index 0000000..b29ba35
--- /dev/null
+++ b/nashorn/test/script/basic/JDK-8066224.js
@@ -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;
+})())
diff --git a/nashorn/test/script/basic/JDK-8066224.js.EXPECTED b/nashorn/test/script/basic/JDK-8066224.js.EXPECTED
new file mode 100644
index 0000000..27ba77d
--- /dev/null
+++ b/nashorn/test/script/basic/JDK-8066224.js.EXPECTED
@@ -0,0 +1 @@
+true