8026042: FoldConstants need to guard against ArrayLiteralNode

Reviewed-by: jlaskey, sundar
This commit is contained in:
Hannes Wallnöfer 2013-10-08 15:53:22 +02:00
parent 21a8fda433
commit 90e3c6f95e
4 changed files with 77 additions and 17 deletions

View File

@ -88,8 +88,8 @@ final class FoldConstants extends NodeVisitor<LexicalContext> {
@Override
public Node leaveIfNode(final IfNode ifNode) {
final Node test = ifNode.getTest();
if (test instanceof LiteralNode) {
final Block shortCut = ((LiteralNode<?>)test).isTrue() ? ifNode.getPass() : ifNode.getFail();
if (test instanceof LiteralNode.PrimitiveLiteralNode) {
final Block shortCut = ((LiteralNode.PrimitiveLiteralNode<?>)test).isTrue() ? ifNode.getPass() : ifNode.getFail();
if (shortCut != null) {
return new BlockStatement(ifNode.getLineNumber(), shortCut);
}
@ -101,8 +101,8 @@ final class FoldConstants extends NodeVisitor<LexicalContext> {
@Override
public Node leaveTernaryNode(final TernaryNode ternaryNode) {
final Node test = ternaryNode.getTest();
if (test instanceof LiteralNode) {
return ((LiteralNode<?>)test).isTrue() ? ternaryNode.getTrueExpression() : ternaryNode.getFalseExpression();
if (test instanceof LiteralNode.PrimitiveLiteralNode) {
return ((LiteralNode.PrimitiveLiteralNode<?>)test).isTrue() ? ternaryNode.getTrueExpression() : ternaryNode.getFalseExpression();
}
return ternaryNode;
}

View File

@ -96,14 +96,6 @@ public abstract class LiteralNode<T> extends Expression implements PropertyKey {
return value == null;
}
/**
* Check if the literal value is boolean true
* @return true if literal value is boolean true
*/
public boolean isTrue() {
return JSType.toBoolean(value);
}
@Override
public Type getType() {
return Type.typeFor(value.getClass());
@ -259,8 +251,31 @@ public abstract class LiteralNode<T> extends Expression implements PropertyKey {
return new NullLiteralNode(parent.getToken(), parent.getFinish());
}
/**
* Super class for primitive (side-effect free) literals.
*
* @param <T> the literal type
*/
public static class PrimitiveLiteralNode<T> extends LiteralNode<T> {
private PrimitiveLiteralNode(final long token, final int finish, final T value) {
super(token, finish, value);
}
private PrimitiveLiteralNode(final PrimitiveLiteralNode<T> literalNode) {
super(literalNode);
}
/**
* Check if the literal value is boolean true
* @return true if literal value is boolean true
*/
public boolean isTrue() {
return JSType.toBoolean(value);
}
}
@Immutable
private static final class BooleanLiteralNode extends LiteralNode<Boolean> {
private static final class BooleanLiteralNode extends PrimitiveLiteralNode<Boolean> {
private BooleanLiteralNode(final long token, final int finish, final boolean value) {
super(Token.recast(token, value ? TokenType.TRUE : TokenType.FALSE), finish, value);
@ -312,7 +327,7 @@ public abstract class LiteralNode<T> extends Expression implements PropertyKey {
}
@Immutable
private static final class NumberLiteralNode extends LiteralNode<Number> {
private static final class NumberLiteralNode extends PrimitiveLiteralNode<Number> {
private final Type type = numberGetType(value);
@ -374,7 +389,7 @@ public abstract class LiteralNode<T> extends Expression implements PropertyKey {
return new NumberLiteralNode(parent.getToken(), parent.getFinish(), value);
}
private static class UndefinedLiteralNode extends LiteralNode<Undefined> {
private static class UndefinedLiteralNode extends PrimitiveLiteralNode<Undefined> {
private UndefinedLiteralNode(final long token, final int finish) {
super(Token.recast(token, TokenType.OBJECT), finish, ScriptRuntime.UNDEFINED);
}
@ -410,7 +425,7 @@ public abstract class LiteralNode<T> extends Expression implements PropertyKey {
}
@Immutable
private static class StringLiteralNode extends LiteralNode<String> {
private static class StringLiteralNode extends PrimitiveLiteralNode<String> {
private StringLiteralNode(final long token, final int finish, final String value) {
super(Token.recast(token, TokenType.STRING), finish, value);
}
@ -522,7 +537,7 @@ public abstract class LiteralNode<T> extends Expression implements PropertyKey {
return POSTSET_MARKER;
}
private static final class NullLiteralNode extends LiteralNode<Object> {
private static final class NullLiteralNode extends PrimitiveLiteralNode<Object> {
private NullLiteralNode(final long token, final int finish) {
super(Token.recast(token, TokenType.OBJECT), finish, null);

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2010, 2013, 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-8026042: FoldConstants need to guard against ArrayLiteralNode
*
* @test
* @run
*/
try {
if ([a]) {
print("fail");
}
} catch (e) {
print(e);
}
try {
[a] ? print(1) : print(2);
} catch (e) {
print(e);
}

View File

@ -0,0 +1,2 @@
ReferenceError: "a" is not defined
ReferenceError: "a" is not defined