jdk.nashorn.internal.ir.LiteralNode Java Examples

The following examples show how to use jdk.nashorn.internal.ir.LiteralNode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FoldConstants.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Node leaveIfNode(final IfNode ifNode) {
    final Node test = ifNode.getTest();
    if (test instanceof LiteralNode.PrimitiveLiteralNode) {
        final boolean isTrue = ((LiteralNode.PrimitiveLiteralNode<?>)test).isTrue();
        final Block executed = isTrue ? ifNode.getPass() : ifNode.getFail();
        final Block dropped  = isTrue ? ifNode.getFail() : ifNode.getPass();
        final List<Statement> statements = new ArrayList<>();

        if (executed != null) {
            statements.addAll(executed.getStatements()); // Get statements form executed branch
        }
        if (dropped != null) {
            extractVarNodesFromDeadCode(dropped, statements); // Get var-nodes from non-executed branch
        }
        if (statements.isEmpty()) {
            return new EmptyNode(ifNode);
        }
        return BlockStatement.createReplacement(ifNode, ifNode.getFinish(), statements);
    }
    return ifNode;
}
 
Example #2
Source File: FoldConstants.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected LiteralNode<?> eval() {
    LiteralNode<?> result;

    result = reduceTwoLiterals();
    if (result != null) {
        return result;
    }

    result = reduceOneLiteral();
    if (result != null) {
        return result;
    }

    return null;
}
 
Example #3
Source File: Parser.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Directive value or null if statement is not a directive.
 *
 * @param stmt Statement to be checked
 * @return Directive value if the given statement is a directive
 */
private String getDirective(final Node stmt) {
    if (stmt instanceof ExpressionStatement) {
        final Node expr = ((ExpressionStatement)stmt).getExpression();
        if (expr instanceof LiteralNode) {
            final LiteralNode<?> lit = (LiteralNode<?>)expr;
            final long litToken = lit.getToken();
            final TokenType tt = Token.descType(litToken);
            // A directive is either a string or an escape string
            if (tt == TokenType.STRING || tt == TokenType.ESCSTRING) {
                // Make sure that we don't unescape anything. Return as seen in source!
                return source.getString(lit.getStart(), Token.descLength(litToken));
            }
        }
    }

    return null;
}
 
Example #4
Source File: FoldConstants.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Node leaveIfNode(final IfNode ifNode) {
    final Node test = ifNode.getTest();
    if (test instanceof LiteralNode.PrimitiveLiteralNode) {
        final boolean isTrue = ((LiteralNode.PrimitiveLiteralNode<?>)test).isTrue();
        final Block executed = isTrue ? ifNode.getPass() : ifNode.getFail();
        final Block dropped  = isTrue ? ifNode.getFail() : ifNode.getPass();
        final List<Statement> statements = new ArrayList<>();

        if (executed != null) {
            statements.addAll(executed.getStatements()); // Get statements form executed branch
        }
        if (dropped != null) {
            extractVarNodesFromDeadCode(dropped, statements); // Get var-nodes from non-executed branch
        }
        if (statements.isEmpty()) {
            return new EmptyNode(ifNode);
        }
        return BlockStatement.createReplacement(ifNode, ifNode.getFinish(), statements);
    }
    return ifNode;
}
 
Example #5
Source File: FoldConstants.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected LiteralNode<?> eval() {
    LiteralNode<?> result;

    result = reduceTwoLiterals();
    if (result != null) {
        return result;
    }

    result = reduceOneLiteral();
    if (result != null) {
        return result;
    }

    return null;
}
 
Example #6
Source File: Lower.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Node leaveBlock(final Block block) {
    //now we have committed the entire statement list to the block, but we need to truncate
    //whatever is after the last terminal. block append won't append past it


    if (lc.isFunctionBody()) {
        final FunctionNode currentFunction = lc.getCurrentFunction();
        final boolean isProgram = currentFunction.isProgram();
        final Statement last = lc.getLastStatement();
        final ReturnNode returnNode = new ReturnNode(
            last == null ? currentFunction.getLineNumber() : last.getLineNumber(), //TODO?
            currentFunction.getToken(),
            currentFunction.getFinish(),
            isProgram ?
                compilerConstant(RETURN) :
                LiteralNode.newInstance(block, ScriptRuntime.UNDEFINED));

        returnNode.accept(this);
    }

    return block;
}
 
Example #7
Source File: FoldConstants.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Node leaveIfNode(final IfNode ifNode) {
    final Node test = ifNode.getTest();
    if (test instanceof LiteralNode.PrimitiveLiteralNode) {
        final boolean isTrue = ((LiteralNode.PrimitiveLiteralNode<?>)test).isTrue();
        final Block executed = isTrue ? ifNode.getPass() : ifNode.getFail();
        final Block dropped  = isTrue ? ifNode.getFail() : ifNode.getPass();
        final List<Statement> statements = new ArrayList<>();

        if (executed != null) {
            statements.addAll(executed.getStatements()); // Get statements form executed branch
        }
        if (dropped != null) {
            extractVarNodesFromDeadCode(dropped, statements); // Get var-nodes from non-executed branch
        }
        if (statements.isEmpty()) {
            return new EmptyNode(ifNode);
        }
        return BlockStatement.createReplacement(ifNode, ifNode.getFinish(), statements);
    }
    return ifNode;
}
 
Example #8
Source File: AssignSymbols.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private Node leaveTYPEOF(final UnaryNode unaryNode) {
    final Expression rhs = unaryNode.getExpression();

    final List<Expression> args = new ArrayList<>();
    if (rhs instanceof IdentNode && !isParamOrVar((IdentNode)rhs)) {
        args.add(compilerConstantIdentifier(SCOPE));
        args.add(LiteralNode.newInstance(rhs, ((IdentNode)rhs).getName())); //null
    } else {
        args.add(rhs);
        args.add(LiteralNode.newInstance(unaryNode)); //null, do not reuse token of identifier rhs, it can be e.g. 'this'
    }

    final Node runtimeNode = new RuntimeNode(unaryNode, Request.TYPEOF, args);

    end(unaryNode);

    return runtimeNode;
}
 
Example #9
Source File: FoldConstants.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected LiteralNode<?> eval() {
    LiteralNode<?> result;

    result = reduceTwoLiterals();
    if (result != null) {
        return result;
    }

    result = reduceOneLiteral();
    if (result != null) {
        return result;
    }

    return null;
}
 
Example #10
Source File: ReplaceCompileUnits.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Node leaveLiteralNode(final LiteralNode<?> node) {
    if (node instanceof ArrayLiteralNode) {
        final ArrayLiteralNode aln = (ArrayLiteralNode)node;
        if (aln.getSplitRanges() == null) {
            return node;
        }
        final List<Splittable.SplitRange> newArrayUnits = new ArrayList<>();
        for (final Splittable.SplitRange au : aln.getSplitRanges()) {
            newArrayUnits.add(new Splittable.SplitRange(getExistingReplacement(au), au.getLow(), au.getHigh()));
        }
        return aln.setSplitRanges(lc, newArrayUnits);
    }
    return node;
}
 
Example #11
Source File: ReplaceCompileUnits.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Node leaveLiteralNode(final LiteralNode<?> node) {
    if (node instanceof ArrayLiteralNode) {
        final ArrayLiteralNode aln = (ArrayLiteralNode)node;
        if (aln.getUnits() == null) {
            return node;
        }
        final List<ArrayUnit> newArrayUnits = new ArrayList<>();
        for (final ArrayUnit au : aln.getUnits()) {
            newArrayUnits.add(new ArrayUnit(getExistingReplacement(au), au.getLo(), au.getHi()));
        }
        return aln.setUnits(lc, newArrayUnits);
    }
    return node;
}
 
Example #12
Source File: AbstractParser.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a LiteralNode from the current token
 *
 * @return LiteralNode representing the current token
 * @throws ParserException if any literals fails to parse
 */
protected final LiteralNode<?> getLiteral() throws ParserException {
    // Capture LITERAL token.
    final long literalToken = token;

    // Create literal node.
    final Object value = getValue();
    // Advance to have a correct finish
    next();

    LiteralNode<?> node = null;

    if (value == null) {
        node = LiteralNode.newInstance(literalToken, finish);
    } else if (value instanceof Number) {
        node = LiteralNode.newInstance(literalToken, finish, (Number)value);
    } else if (value instanceof String) {
        node = LiteralNode.newInstance(literalToken, finish, (String)value);
    } else if (value instanceof LexerToken) {
        if (value instanceof RegexToken) {
            final RegexToken regex = (RegexToken)value;
            try {
                RegExpFactory.validate(regex.getExpression(), regex.getOptions());
            } catch (final ParserException e) {
                throw error(e.getMessage());
            }
        }
        node = LiteralNode.newInstance(literalToken, finish, (LexerToken)value);
    } else {
        assert false : "unknown type for LiteralNode: " + value.getClass();
    }

    return node;
}
 
Example #13
Source File: WeighNodes.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public boolean enterLiteralNode(final LiteralNode literalNode) {
    weight += LITERAL_WEIGHT;

    if (literalNode instanceof ArrayLiteralNode) {
        final ArrayLiteralNode arrayLiteralNode = (ArrayLiteralNode)literalNode;
        final Node[]           value            = arrayLiteralNode.getValue();
        final int[]            postsets         = arrayLiteralNode.getPostsets();
        final List<Splittable.SplitRange>  units            = arrayLiteralNode.getSplitRanges();

        if (units == null) {
            for (final int postset : postsets) {
                weight += AASTORE_WEIGHT;
                final Node element = value[postset];

                if (element != null) {
                    element.accept(this);
                }
            }
        }

        return false;
    }

    return true;
}
 
Example #14
Source File: LocalVariableTypesCalculator.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Node leaveLiteralNode(final LiteralNode<?> literalNode) {
    //for e.g. ArrayLiteralNodes the initial types may have been narrowed due to the
    //introduction of optimistic behavior - hence ensure that all literal nodes are
    //reinitialized
    return literalNode.initialize(lc);
}
 
Example #15
Source File: WeighNodes.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public boolean enterLiteralNode(final LiteralNode literalNode) {
    weight += LITERAL_WEIGHT;

    if (literalNode instanceof ArrayLiteralNode) {
        final ArrayLiteralNode arrayLiteralNode = (ArrayLiteralNode)literalNode;
        final Node[]           value            = arrayLiteralNode.getValue();
        final int[]            postsets         = arrayLiteralNode.getPostsets();
        final List<Splittable.SplitRange>  units            = arrayLiteralNode.getSplitRanges();

        if (units == null) {
            for (final int postset : postsets) {
                weight += AASTORE_WEIGHT;
                final Node element = value[postset];

                if (element != null) {
                    element.accept(this);
                }
            }
        }

        return false;
    }

    return true;
}
 
Example #16
Source File: CodeGenerator.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
private MethodEmitter load(final LiteralNode<?> node) {
    final Object value = node.getValue();

    if (value == null) {
        method.loadNull();
    } else if (value instanceof Undefined) {
        method.loadUndefined(Type.OBJECT);
    } else if (value instanceof String) {
        final String string = (String)value;

        if (string.length() > (MethodEmitter.LARGE_STRING_THRESHOLD / 3)) { // 3 == max bytes per encoded char
            loadConstant(string);
        } else {
            method.load(string);
        }
    } else if (value instanceof RegexToken) {
        loadRegex((RegexToken)value);
    } else if (value instanceof Boolean) {
        method.load((Boolean)value);
    } else if (value instanceof Integer) {
        method.load((Integer)value);
    } else if (value instanceof Long) {
        method.load((Long)value);
    } else if (value instanceof Double) {
        method.load((Double)value);
    } else if (node instanceof ArrayLiteralNode) {
        final ArrayType type = (ArrayType)node.getType();
        loadArray((ArrayLiteralNode)node, type);
        globalAllocateArray(type);
    } else {
        assert false : "Unknown literal for " + node.getClass() + " " + value.getClass() + " " + value;
    }

    return method;
}
 
Example #17
Source File: LocalVariableTypesCalculator.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Node leaveLiteralNode(final LiteralNode<?> literalNode) {
    //for e.g. ArrayLiteralNodes the initial types may have been narrowed due to the
    //introduction of optimistic behavior - hence ensure that all literal nodes are
    //reinitialized
    return literalNode.initialize(lc);
}
 
Example #18
Source File: AbstractParser.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a LiteralNode from the current token
 *
 * @return LiteralNode representing the current token
 * @throws ParserException if any literals fails to parse
 */
protected final LiteralNode<?> getLiteral() throws ParserException {
    // Capture LITERAL token.
    final long literalToken = token;

    // Create literal node.
    final Object value = getValue();
    // Advance to have a correct finish
    next();

    LiteralNode<?> node = null;

    if (value == null) {
        node = LiteralNode.newInstance(literalToken, finish);
    } else if (value instanceof Number) {
        node = LiteralNode.newInstance(literalToken, finish, (Number)value);
    } else if (value instanceof String) {
        node = LiteralNode.newInstance(literalToken, finish, (String)value);
    } else if (value instanceof LexerToken) {
        if (value instanceof RegexToken) {
            final RegexToken regex = (RegexToken)value;
            try {
                RegExpFactory.validate(regex.getExpression(), regex.getOptions());
            } catch (final ParserException e) {
                throw error(e.getMessage());
            }
        }
        node = LiteralNode.newInstance(literalToken, finish, (LexerToken)value);
    } else {
        assert false : "unknown type for LiteralNode: " + value.getClass();
    }

    return node;
}
 
Example #19
Source File: FoldConstants.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Node leaveUnaryNode(final UnaryNode unaryNode) {
    final LiteralNode<?> literalNode = new UnaryNodeConstantEvaluator(unaryNode).eval();
    if (literalNode != null) {
        log.info("Unary constant folded ", unaryNode, " to ", literalNode);
        return literalNode;
    }
    return unaryNode;
}
 
Example #20
Source File: FoldConstants.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isUniqueIntegerLiteral(final Expression expr, final Set<Integer> alreadySeen) {
    if (expr instanceof LiteralNode) {
        final Object value = ((LiteralNode<?>)expr).getValue();
        if (value instanceof Integer) {
            return alreadySeen.add((Integer)value);
        }
    }
    return false;
}
 
Example #21
Source File: ReplaceCompileUnits.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Node leaveLiteralNode(final LiteralNode<?> node) {
    if (node instanceof ArrayLiteralNode) {
        final ArrayLiteralNode aln = (ArrayLiteralNode)node;
        if (aln.getSplitRanges() == null) {
            return node;
        }
        final List<Splittable.SplitRange> newArrayUnits = new ArrayList<>();
        for (final Splittable.SplitRange au : aln.getSplitRanges()) {
            newArrayUnits.add(new Splittable.SplitRange(getExistingReplacement(au), au.getLow(), au.getHigh()));
        }
        return aln.setSplitRanges(lc, newArrayUnits);
    }
    return node;
}
 
Example #22
Source File: Parser.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
private RuntimeNode referenceError(final Expression lhs, final Expression rhs, final boolean earlyError) {
    if (earlyError) {
        throw error(JSErrorType.REFERENCE_ERROR, AbstractParser.message("invalid.lvalue"), lhs.getToken());
    }
    final ArrayList<Expression> args = new ArrayList<>();
    args.add(lhs);
    if (rhs == null) {
        args.add(LiteralNode.newInstance(lhs.getToken(), lhs.getFinish()));
    } else {
        args.add(rhs);
    }
    args.add(LiteralNode.newInstance(lhs.getToken(), lhs.getFinish(), lhs.toString()));
    return new RuntimeNode(lhs.getToken(), lhs.getFinish(), RuntimeNode.Request.REFERENCE_ERROR, args);
}
 
Example #23
Source File: JSONFunctions.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isNumericArray(final Node[] values) {
    for (final Node node : values) {
        if (node instanceof LiteralNode && ((LiteralNode<?>)node).getValue() instanceof Number) {
            continue;
        }
        return false;
    }
    return true;
}
 
Example #24
Source File: FoldConstants.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
@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 (shortCut != null) {
            return new BlockStatement(ifNode.getLineNumber(), shortCut);
        }
        return new EmptyNode(ifNode);
    }
    return ifNode;
}
 
Example #25
Source File: WeighNodes.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public boolean enterLiteralNode(final LiteralNode literalNode) {
    weight += LITERAL_WEIGHT;

    if (literalNode instanceof ArrayLiteralNode) {
        final ArrayLiteralNode arrayLiteralNode = (ArrayLiteralNode)literalNode;
        final Node[]           value            = arrayLiteralNode.getValue();
        final int[]            postsets         = arrayLiteralNode.getPostsets();
        final List<ArrayUnit>  units            = arrayLiteralNode.getUnits();

        if (units == null) {
            for (final int postset : postsets) {
                weight += AASTORE_WEIGHT;
                final Node element = value[postset];

                if (element != null) {
                    element.accept(this);
                }
            }
        }

        return false;
    }

    return true;
}
 
Example #26
Source File: Lower.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static String getConstantPropertyName(final Expression expression) {
    if (expression instanceof LiteralNode.PrimitiveLiteralNode) {
        final Object value = ((LiteralNode) expression).getValue();
        if (value instanceof String && SAFE_PROPERTY_NAME.matcher((String) value).matches()) {
            return (String) value;
        }
    }
    return null;
}
 
Example #27
Source File: AbstractParser.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a LiteralNode from the current token
 *
 * @return LiteralNode representing the current token
 * @throws ParserException if any literals fails to parse
 */
protected final LiteralNode<?> getLiteral() throws ParserException {
    // Capture LITERAL token.
    final long literalToken = token;

    // Create literal node.
    final Object value = getValue();
    // Advance to have a correct finish
    next();

    LiteralNode<?> node = null;

    if (value == null) {
        node = LiteralNode.newInstance(literalToken, finish);
    } else if (value instanceof Number) {
        node = LiteralNode.newInstance(literalToken, finish, (Number)value);
    } else if (value instanceof String) {
        node = LiteralNode.newInstance(literalToken, finish, (String)value);
    } else if (value instanceof LexerToken) {
        if (value instanceof RegexToken) {
            final RegexToken regex = (RegexToken)value;
            try {
                RegExpFactory.validate(regex.getExpression(), regex.getOptions());
            } catch (final ParserException e) {
                throw error(e.getMessage());
            }
        }
        node = LiteralNode.newInstance(literalToken, finish, (LexerToken)value);
    } else {
        assert false : "unknown type for LiteralNode: " + value.getClass();
    }

    return node;
}
 
Example #28
Source File: LocalVariableTypesCalculator.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean enterLiteralNode(final LiteralNode<?> literalNode) {
    if (literalNode instanceof ArrayLiteralNode) {
        final List<Expression> expressions = ((ArrayLiteralNode)literalNode).getElementExpressions();
        if (expressions != null) {
            visitExpressions(expressions);
        }
    }
    pushExpressionType(literalNode);
    return false;
}
 
Example #29
Source File: LocalVariableTypesCalculator.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Node leaveLiteralNode(final LiteralNode<?> literalNode) {
    //for e.g. ArrayLiteralNodes the initial types may have been narrowed due to the
    //introduction of optimistic behavior - hence ensure that all literal nodes are
    //reinitialized
    return literalNode.initialize(lc);
}
 
Example #30
Source File: Lower.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean enterBlock(final Block block) {
    final FunctionNode   function = lc.getCurrentFunction();
    if (lc.isFunctionBody() && function.isProgram() && !function.hasDeclaredFunctions()) {
        new ExpressionStatement(function.getLineNumber(), block.getToken(), block.getFinish(), LiteralNode.newInstance(block, ScriptRuntime.UNDEFINED)).accept(this);
    }
    return true;
}