Java Code Examples for jdk.nashorn.internal.ir.FunctionNode#isProgram()

The following examples show how to use jdk.nashorn.internal.ir.FunctionNode#isProgram() . 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: Lower.java    From openjdk-8 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 2
Source File: Lower.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Node leaveExpressionStatement(final ExpressionStatement expressionStatement) {
    final Expression expr = expressionStatement.getExpression();
    ExpressionStatement node = expressionStatement;

    final FunctionNode currentFunction = lc.getCurrentFunction();

    if (currentFunction.isProgram()) {
        if (!isInternalExpression(expr) && !isEvalResultAssignment(expr)) {
            node = expressionStatement.setExpression(
                new BinaryNode(
                    Token.recast(
                        expressionStatement.getToken(),
                        TokenType.ASSIGN),
                    compilerConstant(RETURN),
                expr));
        }
    }

    return addStatement(node);
}
 
Example 3
Source File: Lower.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Node leaveExpressionStatement(final ExpressionStatement expressionStatement) {
    final Expression expr = expressionStatement.getExpression();
    ExpressionStatement node = expressionStatement;

    final FunctionNode currentFunction = lc.getCurrentFunction();

    if (currentFunction.isProgram()) {
        if (!isInternalExpression(expr) && !isEvalResultAssignment(expr)) {
            node = expressionStatement.setExpression(
                new BinaryNode(
                    Token.recast(
                        expressionStatement.getToken(),
                        TokenType.ASSIGN),
                    compilerConstant(RETURN),
                expr));
        }
    }

    return addStatement(node);
}
 
Example 4
Source File: Lower.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Node leaveExpressionStatement(final ExpressionStatement expressionStatement) {
    final Expression expr = expressionStatement.getExpression();
    ExpressionStatement node = expressionStatement;

    final FunctionNode currentFunction = lc.getCurrentFunction();

    if (currentFunction.isProgram()) {
        if (!isInternalExpression(expr) && !isEvalResultAssignment(expr)) {
            node = expressionStatement.setExpression(
                new BinaryNode(
                    Token.recast(
                        expressionStatement.getToken(),
                        TokenType.ASSIGN),
                    compilerConstant(RETURN),
                expr));
        }
    }

    return addStatement(node);
}
 
Example 5
Source File: Lower.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Node leaveExpressionStatement(final ExpressionStatement expressionStatement) {
    final Expression expr = expressionStatement.getExpression();
    ExpressionStatement node = expressionStatement;

    final FunctionNode currentFunction = lc.getCurrentFunction();

    if (currentFunction.isProgram()) {
        if (!isInternalExpression(expr) && !isEvalResultAssignment(expr)) {
            node = expressionStatement.setExpression(
                new BinaryNode(
                    Token.recast(
                        expressionStatement.getToken(),
                        TokenType.ASSIGN),
                    compilerConstant(RETURN),
                expr));
        }
    }

    return addStatement(node);
}
 
Example 6
Source File: Lower.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Node leaveExpressionStatement(final ExpressionStatement expressionStatement) {
    final Expression expr = expressionStatement.getExpression();
    ExpressionStatement node = expressionStatement;

    final FunctionNode currentFunction = lc.getCurrentFunction();

    if (currentFunction.isProgram()) {
        if (!isInternalExpression(expr) && !isEvalResultAssignment(expr)) {
            node = expressionStatement.setExpression(
                new BinaryNode(
                    Token.recast(
                        expressionStatement.getToken(),
                        TokenType.ASSIGN),
                    compilerConstant(RETURN),
                expr));
        }
    }

    return addStatement(node);
}
 
Example 7
Source File: AssignSymbols.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private FunctionNode markProgramBlock(final FunctionNode functionNode) {
    if (isOnDemand || !functionNode.isProgram()) {
        return functionNode;
    }

    return functionNode.setBody(lc, functionNode.getBody().setFlag(lc, Block.IS_GLOBAL_SCOPE));
}
 
Example 8
Source File: LocalVariableTypesCalculator.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private void createSyntheticReturn(final Block body) {
    final FunctionNode functionNode = lc.getCurrentFunction();
    final long token = functionNode.getToken();
    final int finish = functionNode.getFinish();
    final List<Statement> statements = body.getStatements();
    final int lineNumber = statements.isEmpty() ? functionNode.getLineNumber() : statements.get(statements.size() - 1).getLineNumber();
    final IdentNode returnExpr;
    if(functionNode.isProgram()) {
        returnExpr = new IdentNode(token, finish, RETURN.symbolName()).setSymbol(getCompilerConstantSymbol(functionNode, RETURN));
    } else {
        returnExpr = null;
    }
    syntheticReturn = new ReturnNode(lineNumber, token, finish, returnExpr);
    syntheticReturn.accept(this);
}
 
Example 9
Source File: LocalVariableTypesCalculator.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void createSyntheticReturn(final Block body) {
    final FunctionNode functionNode = lc.getCurrentFunction();
    final long token = functionNode.getToken();
    final int finish = functionNode.getFinish();
    final List<Statement> statements = body.getStatements();
    final int lineNumber = statements.isEmpty() ? functionNode.getLineNumber() : statements.get(statements.size() - 1).getLineNumber();
    final IdentNode returnExpr;
    if(functionNode.isProgram()) {
        returnExpr = new IdentNode(token, finish, RETURN.symbolName()).setSymbol(getCompilerConstantSymbol(functionNode, RETURN));
    } else {
        returnExpr = null;
    }
    syntheticReturn = new ReturnNode(lineNumber, token, finish, returnExpr);
    syntheticReturn.accept(this);
}
 
Example 10
Source File: FunctionSignature.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a function signature given a function node, using as much
 * type information for parameters and return types that is available
 *
 * @param functionNode the function node
 */
public FunctionSignature(final FunctionNode functionNode) {
    this(
        true,
        functionNode.needsCallee(),
        functionNode.getReturnType(),
        (functionNode.isVarArg() && !functionNode.isProgram()) ?
            null :
            functionNode.getParameters());
}
 
Example 11
Source File: AssignSymbols.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determines if the symbol has to be a scope symbol. In general terms, it has to be a scope symbol if it can only
 * be reached from the current block by traversing a function node, a split node, or a with node.
 * @param symbol the symbol checked for needing to be a scope symbol
 * @return true if the symbol has to be a scope symbol.
 */
private boolean symbolNeedsToBeScope(final Symbol symbol) {
    if (symbol.isThis() || symbol.isInternal()) {
        return false;
    }

    final FunctionNode func = lc.getCurrentFunction();
    if ( func.allVarsInScope() || (!symbol.isBlockScoped() && func.isProgram())) {
        return true;
    }

    boolean previousWasBlock = false;
    for (final Iterator<LexicalContextNode> it = lc.getAllNodes(); it.hasNext();) {
        final LexicalContextNode node = it.next();
        if (node instanceof FunctionNode || isSplitLiteral(node)) {
            // We reached the function boundary or a splitting boundary without seeing a definition for the symbol.
            // It needs to be in scope.
            return true;
        } else if (node instanceof WithNode) {
            if (previousWasBlock) {
                // We reached a WithNode; the symbol must be scoped. Note that if the WithNode was not immediately
                // preceded by a block, this means we're currently processing its expression, not its body,
                // therefore it doesn't count.
                return true;
            }
            previousWasBlock = false;
        } else if (node instanceof Block) {
            if (((Block)node).getExistingSymbol(symbol.getName()) == symbol) {
                // We reached the block that defines the symbol without reaching either the function boundary, or a
                // WithNode. The symbol need not be scoped.
                return false;
            }
            previousWasBlock = true;
        } else {
            previousWasBlock = false;
        }
    }
    throw new AssertionError();
}
 
Example 12
Source File: AssignSymbols.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private FunctionNode markProgramBlock(final FunctionNode functionNode) {
    if (isOnDemand || !functionNode.isProgram()) {
        return functionNode;
    }

    return functionNode.setBody(lc, functionNode.getBody().setFlag(lc, Block.IS_GLOBAL_SCOPE));
}
 
Example 13
Source File: AssignSymbols.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private FunctionNode markProgramBlock(final FunctionNode functionNode) {
    if (isOnDemand || !functionNode.isProgram()) {
        return functionNode;
    }

    return functionNode.setBody(lc, functionNode.getBody().setFlag(lc, Block.IS_GLOBAL_SCOPE));
}
 
Example 14
Source File: LocalVariableTypesCalculator.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
private void createSyntheticReturn(final Block body) {
    final FunctionNode functionNode = lc.getCurrentFunction();
    final long token = functionNode.getToken();
    final int finish = functionNode.getFinish();
    final List<Statement> statements = body.getStatements();
    final int lineNumber = statements.isEmpty() ? functionNode.getLineNumber() : statements.get(statements.size() - 1).getLineNumber();
    final IdentNode returnExpr;
    if(functionNode.isProgram()) {
        returnExpr = new IdentNode(token, finish, RETURN.symbolName()).setSymbol(getCompilerConstantSymbol(functionNode, RETURN));
    } else {
        returnExpr = null;
    }
    syntheticReturn = new ReturnNode(lineNumber, token, finish, returnExpr);
    syntheticReturn.accept(this);
}
 
Example 15
Source File: Lower.java    From nashorn 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;
}
 
Example 16
Source File: LocalVariableTypesCalculator.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void createSyntheticReturn(final Block body) {
    final FunctionNode functionNode = lc.getCurrentFunction();
    final long token = functionNode.getToken();
    final int finish = functionNode.getFinish();
    final List<Statement> statements = body.getStatements();
    final int lineNumber = statements.isEmpty() ? functionNode.getLineNumber() : statements.get(statements.size() - 1).getLineNumber();
    final IdentNode returnExpr;
    if(functionNode.isProgram()) {
        returnExpr = new IdentNode(token, finish, RETURN.symbolName()).setSymbol(getCompilerConstantSymbol(functionNode, RETURN));
    } else {
        returnExpr = null;
    }
    syntheticReturn = new ReturnNode(lineNumber, token, finish, returnExpr);
    syntheticReturn.accept(this);
}
 
Example 17
Source File: AssignSymbols.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determines if the symbol has to be a scope symbol. In general terms, it has to be a scope symbol if it can only
 * be reached from the current block by traversing a function node, a split node, or a with node.
 * @param symbol the symbol checked for needing to be a scope symbol
 * @return true if the symbol has to be a scope symbol.
 */
private boolean symbolNeedsToBeScope(final Symbol symbol) {
    if (symbol.isThis() || symbol.isInternal()) {
        return false;
    }

    final FunctionNode func = lc.getCurrentFunction();
    if ( func.allVarsInScope() || (!symbol.isBlockScoped() && func.isProgram())) {
        return true;
    }

    boolean previousWasBlock = false;
    for (final Iterator<LexicalContextNode> it = lc.getAllNodes(); it.hasNext();) {
        final LexicalContextNode node = it.next();
        if (node instanceof FunctionNode || isSplitLiteral(node)) {
            // We reached the function boundary or a splitting boundary without seeing a definition for the symbol.
            // It needs to be in scope.
            return true;
        } else if (node instanceof WithNode) {
            if (previousWasBlock) {
                // We reached a WithNode; the symbol must be scoped. Note that if the WithNode was not immediately
                // preceded by a block, this means we're currently processing its expression, not its body,
                // therefore it doesn't count.
                return true;
            }
            previousWasBlock = false;
        } else if (node instanceof Block) {
            if (((Block)node).getExistingSymbol(symbol.getName()) == symbol) {
                // We reached the block that defines the symbol without reaching either the function boundary, or a
                // WithNode. The symbol need not be scoped.
                return false;
            }
            previousWasBlock = true;
        } else {
            previousWasBlock = false;
        }
    }
    throw new AssertionError();
}
 
Example 18
Source File: FunctionSignature.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a function signature given a function node, using as much
 * type information for parameters and return types that is available
 *
 * @param functionNode the function node
 */
public FunctionSignature(final FunctionNode functionNode) {
    this(
        true,
        functionNode.needsCallee(),
        functionNode.getReturnType(),
        (functionNode.isVarArg() && !functionNode.isProgram()) ?
            null :
            functionNode.getParameters());
}
 
Example 19
Source File: FunctionSignature.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a function signature given a function node, using as much
 * type information for parameters and return types that is available
 *
 * @param functionNode the function node
 */
public FunctionSignature(final FunctionNode functionNode) {
    this(
        true,
        functionNode.needsCallee(),
        functionNode.getReturnType(),
        (functionNode.isVarArg() && !functionNode.isProgram()) ?
            null :
            functionNode.getParameters());
}
 
Example 20
Source File: JSONWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean enterFunctionNode(final FunctionNode functionNode) {
    final boolean program = functionNode.isProgram();
    if (program) {
        return emitProgram(functionNode);
    }

    enterDefault(functionNode);
    final String name;
    if (functionNode.isDeclared()) {
        name = "FunctionDeclaration";
    } else {
        name = "FunctionExpression";
    }
    type(name);
    comma();

    property("id");
    final FunctionNode.Kind kind = functionNode.getKind();
    if (functionNode.isAnonymous() || kind == FunctionNode.Kind.GETTER || kind == FunctionNode.Kind.SETTER) {
        nullValue();
    } else {
        functionNode.getIdent().accept(this);
    }
    comma();

    array("params", functionNode.getParameters());
    comma();

    arrayStart("defaults");
    arrayEnd();
    comma();

    property("rest");
    nullValue();
    comma();

    property("body");
    functionNode.getBody().accept(this);
    comma();

    property("generator", false);
    comma();

    property("expression", false);

    return leave();
}