Tue, 08 Oct 2013 15:53:22 +0200
8026042: FoldConstants need to guard against ArrayLiteralNode
Reviewed-by: jlaskey, sundar
1.1 --- a/src/jdk/nashorn/internal/codegen/FoldConstants.java Tue Oct 08 14:57:31 2013 +0200 1.2 +++ b/src/jdk/nashorn/internal/codegen/FoldConstants.java Tue Oct 08 15:53:22 2013 +0200 1.3 @@ -88,8 +88,8 @@ 1.4 @Override 1.5 public Node leaveIfNode(final IfNode ifNode) { 1.6 final Node test = ifNode.getTest(); 1.7 - if (test instanceof LiteralNode) { 1.8 - final Block shortCut = ((LiteralNode<?>)test).isTrue() ? ifNode.getPass() : ifNode.getFail(); 1.9 + if (test instanceof LiteralNode.PrimitiveLiteralNode) { 1.10 + final Block shortCut = ((LiteralNode.PrimitiveLiteralNode<?>)test).isTrue() ? ifNode.getPass() : ifNode.getFail(); 1.11 if (shortCut != null) { 1.12 return new BlockStatement(ifNode.getLineNumber(), shortCut); 1.13 } 1.14 @@ -101,8 +101,8 @@ 1.15 @Override 1.16 public Node leaveTernaryNode(final TernaryNode ternaryNode) { 1.17 final Node test = ternaryNode.getTest(); 1.18 - if (test instanceof LiteralNode) { 1.19 - return ((LiteralNode<?>)test).isTrue() ? ternaryNode.getTrueExpression() : ternaryNode.getFalseExpression(); 1.20 + if (test instanceof LiteralNode.PrimitiveLiteralNode) { 1.21 + return ((LiteralNode.PrimitiveLiteralNode<?>)test).isTrue() ? ternaryNode.getTrueExpression() : ternaryNode.getFalseExpression(); 1.22 } 1.23 return ternaryNode; 1.24 }
2.1 --- a/src/jdk/nashorn/internal/ir/LiteralNode.java Tue Oct 08 14:57:31 2013 +0200 2.2 +++ b/src/jdk/nashorn/internal/ir/LiteralNode.java Tue Oct 08 15:53:22 2013 +0200 2.3 @@ -96,14 +96,6 @@ 2.4 return value == null; 2.5 } 2.6 2.7 - /** 2.8 - * Check if the literal value is boolean true 2.9 - * @return true if literal value is boolean true 2.10 - */ 2.11 - public boolean isTrue() { 2.12 - return JSType.toBoolean(value); 2.13 - } 2.14 - 2.15 @Override 2.16 public Type getType() { 2.17 return Type.typeFor(value.getClass()); 2.18 @@ -259,8 +251,31 @@ 2.19 return new NullLiteralNode(parent.getToken(), parent.getFinish()); 2.20 } 2.21 2.22 + /** 2.23 + * Super class for primitive (side-effect free) literals. 2.24 + * 2.25 + * @param <T> the literal type 2.26 + */ 2.27 + public static class PrimitiveLiteralNode<T> extends LiteralNode<T> { 2.28 + private PrimitiveLiteralNode(final long token, final int finish, final T value) { 2.29 + super(token, finish, value); 2.30 + } 2.31 + 2.32 + private PrimitiveLiteralNode(final PrimitiveLiteralNode<T> literalNode) { 2.33 + super(literalNode); 2.34 + } 2.35 + 2.36 + /** 2.37 + * Check if the literal value is boolean true 2.38 + * @return true if literal value is boolean true 2.39 + */ 2.40 + public boolean isTrue() { 2.41 + return JSType.toBoolean(value); 2.42 + } 2.43 + } 2.44 + 2.45 @Immutable 2.46 - private static final class BooleanLiteralNode extends LiteralNode<Boolean> { 2.47 + private static final class BooleanLiteralNode extends PrimitiveLiteralNode<Boolean> { 2.48 2.49 private BooleanLiteralNode(final long token, final int finish, final boolean value) { 2.50 super(Token.recast(token, value ? TokenType.TRUE : TokenType.FALSE), finish, value); 2.51 @@ -312,7 +327,7 @@ 2.52 } 2.53 2.54 @Immutable 2.55 - private static final class NumberLiteralNode extends LiteralNode<Number> { 2.56 + private static final class NumberLiteralNode extends PrimitiveLiteralNode<Number> { 2.57 2.58 private final Type type = numberGetType(value); 2.59 2.60 @@ -374,7 +389,7 @@ 2.61 return new NumberLiteralNode(parent.getToken(), parent.getFinish(), value); 2.62 } 2.63 2.64 - private static class UndefinedLiteralNode extends LiteralNode<Undefined> { 2.65 + private static class UndefinedLiteralNode extends PrimitiveLiteralNode<Undefined> { 2.66 private UndefinedLiteralNode(final long token, final int finish) { 2.67 super(Token.recast(token, TokenType.OBJECT), finish, ScriptRuntime.UNDEFINED); 2.68 } 2.69 @@ -410,7 +425,7 @@ 2.70 } 2.71 2.72 @Immutable 2.73 - private static class StringLiteralNode extends LiteralNode<String> { 2.74 + private static class StringLiteralNode extends PrimitiveLiteralNode<String> { 2.75 private StringLiteralNode(final long token, final int finish, final String value) { 2.76 super(Token.recast(token, TokenType.STRING), finish, value); 2.77 } 2.78 @@ -522,7 +537,7 @@ 2.79 return POSTSET_MARKER; 2.80 } 2.81 2.82 - private static final class NullLiteralNode extends LiteralNode<Object> { 2.83 + private static final class NullLiteralNode extends PrimitiveLiteralNode<Object> { 2.84 2.85 private NullLiteralNode(final long token, final int finish) { 2.86 super(Token.recast(token, TokenType.OBJECT), finish, null);
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/script/basic/JDK-8026042.js Tue Oct 08 15:53:22 2013 +0200 3.3 @@ -0,0 +1,43 @@ 3.4 +/* 3.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.23 + * or visit www.oracle.com if you need additional information or have any 3.24 + * questions. 3.25 + */ 3.26 + 3.27 +/** 3.28 + * JDK-8026042: FoldConstants need to guard against ArrayLiteralNode 3.29 + * 3.30 + * @test 3.31 + * @run 3.32 + */ 3.33 + 3.34 +try { 3.35 + if ([a]) { 3.36 + print("fail"); 3.37 + } 3.38 +} catch (e) { 3.39 + print(e); 3.40 +} 3.41 + 3.42 +try { 3.43 + [a] ? print(1) : print(2); 3.44 +} catch (e) { 3.45 + print(e); 3.46 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/script/basic/JDK-8026042.js.EXPECTED Tue Oct 08 15:53:22 2013 +0200 4.3 @@ -0,0 +1,2 @@ 4.4 +ReferenceError: "a" is not defined 4.5 +ReferenceError: "a" is not defined