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

The following examples show how to use jdk.nashorn.internal.ir.FunctionNode#setBody() . 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: Parser.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Program :
 *      SourceElements?
 *
 * See 14
 *
 * Parse the top level script.
 */
private FunctionNode program(final String scriptName, final boolean allowPropertyFunction) {
    // Make a pseudo-token for the script holding its start and length.
    final long functionToken = Token.toDesc(FUNCTION, Token.descPosition(Token.withDelimiter(token)), source.getLength());
    final int  functionLine  = line;
    // Set up the script to append elements.

    FunctionNode script = newFunctionNode(
        functionToken,
        new IdentNode(functionToken, Token.descPosition(functionToken), scriptName),
        new ArrayList<IdentNode>(),
        FunctionNode.Kind.SCRIPT,
        functionLine);

    functionDeclarations = new ArrayList<>();
    sourceElements(allowPropertyFunction);
    addFunctionDeclarations(script);
    functionDeclarations = null;

    expect(EOF);

    script.setFinish(source.getLength() - 1);

    script = restoreFunctionNode(script, token); //commit code
    script = script.setBody(lc, script.getBody().setNeedsScope(lc));

    return script;
}
 
Example 2
Source File: AssignSymbols.java    From jdk8u_nashorn 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 3
Source File: CacheAst.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Node leaveFunctionNode(final FunctionNode functionNode) {
    final RecompilableScriptFunctionData data = dataStack.pop();
    if (functionNode.isSplit()) {
        // NOTE: cache only split function ASTs from eager pass. Caching non-split functions would require
        // some additional work, namely creating the concept of "uncacheable" function and reworking
        // ApplySpecialization to ensure that functions undergoing apply-to-call transformations are not
        // cacheable as well as recomputing Symbol.useCount when caching the eagerly parsed AST.
        // Recomputing Symbol.useCount would be needed so it will only reflect uses from within the
        // function being cached (and not reflect uses from its own nested functions or functions it is
        // nested in). This is consistent with the count an on-demand recompilation of the function would
        // produce. This is important as the decision to emit shared scope calls is based on this count,
        // and if it is not matched between a previous version of the code and its deoptimizing rest-of
        // compilation, it can result in rest-of not emitting a shared scope call where a previous version
        // of the code (compiled from a cached eager pre-pass seeing higher (global) useCount) would emit
        // it, causing a mismatch in stack shapes between previous code and its rest-of.
        data.setCachedAst(functionNode);
    }

    if (!dataStack.isEmpty() && ((dataStack.peek().getFunctionFlags() & FunctionNode.IS_SPLIT) != 0)) {
        // Return a function node with no body so that caching outer functions doesn't hold on to nested
        // functions' bodies. Note we're doing this only for functions directly nested inside split
        // functions, since we're only caching the split ones. It is not necessary to limit body removal
        // to just these functions, but it's a cheap way to prevent unnecessary AST mutations.
        return functionNode.setBody(lc, functionNode.getBody().setStatements(null, Collections.<Statement>emptyList()));
    }
    return functionNode;
}
 
Example 4
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 5
Source File: AssignSymbols.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private FunctionNode createSyntheticInitializers(final FunctionNode functionNode) {
    final List<VarNode> syntheticInitializers = new ArrayList<>(2);

    // Must visit the new var nodes in the context of the body. We could also just set the new statements into the
    // block and then revisit the entire block, but that seems to be too much double work.
    final Block body = functionNode.getBody();
    lc.push(body);
    try {
        if (functionNode.usesSelfSymbol()) {
            // "var fn = :callee"
            syntheticInitializers.add(createSyntheticInitializer(functionNode.getIdent(), CALLEE, functionNode));
        }

        if (functionNode.needsArguments()) {
            // "var arguments = :arguments"
            syntheticInitializers.add(createSyntheticInitializer(createImplicitIdentifier(ARGUMENTS_VAR.symbolName()),
                    ARGUMENTS, functionNode));
        }

        if (syntheticInitializers.isEmpty()) {
            return functionNode;
        }

        for(final ListIterator<VarNode> it = syntheticInitializers.listIterator(); it.hasNext();) {
            it.set((VarNode)it.next().accept(this));
        }
    } finally {
        lc.pop(body);
    }

    final List<Statement> stmts = body.getStatements();
    final List<Statement> newStatements = new ArrayList<>(stmts.size() + syntheticInitializers.size());
    newStatements.addAll(syntheticInitializers);
    newStatements.addAll(stmts);
    return functionNode.setBody(lc, body.setStatements(lc, newStatements));
}
 
Example 6
Source File: Parser.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Program :
 *      SourceElements?
 *
 * See 14
 *
 * Parse the top level script.
 */
private FunctionNode program(final String scriptName, final boolean allowPropertyFunction) {
    // Make a pseudo-token for the script holding its start and length.
    final long functionToken = Token.toDesc(FUNCTION, Token.descPosition(Token.withDelimiter(token)), source.getLength());
    final int  functionLine  = line;
    // Set up the script to append elements.

    FunctionNode script = newFunctionNode(
        functionToken,
        new IdentNode(functionToken, Token.descPosition(functionToken), scriptName),
        new ArrayList<IdentNode>(),
        FunctionNode.Kind.SCRIPT,
        functionLine);

    functionDeclarations = new ArrayList<>();
    sourceElements(allowPropertyFunction);
    addFunctionDeclarations(script);
    functionDeclarations = null;

    expect(EOF);

    script.setFinish(source.getLength() - 1);

    script = restoreFunctionNode(script, token); //commit code
    script = script.setBody(lc, script.getBody().setNeedsScope(lc));

    return script;
}
 
Example 7
Source File: Parser.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Execute parse and return the resulting function node.
 * Errors will be thrown and the error manager will contain information
 * if parsing should fail. This method is used to check if code String
 * passed to "Function" constructor is a valid function body or not.
 *
 * @return function node resulting from successful parse
 */
public FunctionNode parseFunctionBody() {
    try {
        stream = new TokenStream();
        lexer  = new Lexer(source, stream, scripting && !env._no_syntax_extensions);
        final int functionLine = line;

        // Set up first token (skips opening EOL.)
        k = -1;
        next();

        // Make a fake token for the function.
        final long functionToken = Token.toDesc(FUNCTION, 0, source.getLength());
        // Set up the function to append elements.

        FunctionNode function = newFunctionNode(
            functionToken,
            new IdentNode(functionToken, Token.descPosition(functionToken), PROGRAM.symbolName()),
            new ArrayList<IdentNode>(),
            FunctionNode.Kind.NORMAL,
            functionLine);

        functionDeclarations = new ArrayList<>();
        sourceElements(false);
        addFunctionDeclarations(function);
        functionDeclarations = null;

        expect(EOF);

        function.setFinish(source.getLength() - 1);
        function = restoreFunctionNode(function, token); //commit code
        function = function.setBody(lc, function.getBody().setNeedsScope(lc));

        printAST(function);
        return function;
    } catch (final Exception e) {
        handleParseException(e);
        return null;
    }
}
 
Example 8
Source File: CacheAst.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Node leaveFunctionNode(final FunctionNode functionNode) {
    final RecompilableScriptFunctionData data = dataStack.pop();
    if (functionNode.isSplit()) {
        // NOTE: cache only split function ASTs from eager pass. Caching non-split functions would require
        // some additional work, namely creating the concept of "uncacheable" function and reworking
        // ApplySpecialization to ensure that functions undergoing apply-to-call transformations are not
        // cacheable as well as recomputing Symbol.useCount when caching the eagerly parsed AST.
        // Recomputing Symbol.useCount would be needed so it will only reflect uses from within the
        // function being cached (and not reflect uses from its own nested functions or functions it is
        // nested in). This is consistent with the count an on-demand recompilation of the function would
        // produce. This is important as the decision to emit shared scope calls is based on this count,
        // and if it is not matched between a previous version of the code and its deoptimizing rest-of
        // compilation, it can result in rest-of not emitting a shared scope call where a previous version
        // of the code (compiled from a cached eager pre-pass seeing higher (global) useCount) would emit
        // it, causing a mismatch in stack shapes between previous code and its rest-of.
        data.setCachedAst(functionNode);
    }

    if (!dataStack.isEmpty() && ((dataStack.peek().getFunctionFlags() & FunctionNode.IS_SPLIT) != 0)) {
        // Return a function node with no body so that caching outer functions doesn't hold on to nested
        // functions' bodies. Note we're doing this only for functions directly nested inside split
        // functions, since we're only caching the split ones. It is not necessary to limit body removal
        // to just these functions, but it's a cheap way to prevent unnecessary AST mutations.
        return functionNode.setBody(lc, functionNode.getBody().setStatements(null, Collections.<Statement>emptyList()));
    }
    return functionNode;
}
 
Example 9
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 10
Source File: AssignSymbols.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private FunctionNode createSyntheticInitializers(final FunctionNode functionNode) {
    final List<VarNode> syntheticInitializers = new ArrayList<>(2);

    // Must visit the new var nodes in the context of the body. We could also just set the new statements into the
    // block and then revisit the entire block, but that seems to be too much double work.
    final Block body = functionNode.getBody();
    lc.push(body);
    try {
        if (functionNode.usesSelfSymbol()) {
            // "var fn = :callee"
            syntheticInitializers.add(createSyntheticInitializer(functionNode.getIdent(), CALLEE, functionNode));
        }

        if (functionNode.needsArguments()) {
            // "var arguments = :arguments"
            syntheticInitializers.add(createSyntheticInitializer(createImplicitIdentifier(ARGUMENTS_VAR.symbolName()),
                    ARGUMENTS, functionNode));
        }

        if (syntheticInitializers.isEmpty()) {
            return functionNode;
        }

        for(final ListIterator<VarNode> it = syntheticInitializers.listIterator(); it.hasNext();) {
            it.set((VarNode)it.next().accept(this));
        }
    } finally {
        lc.pop(body);
    }

    final List<Statement> stmts = body.getStatements();
    final List<Statement> newStatements = new ArrayList<>(stmts.size() + syntheticInitializers.size());
    newStatements.addAll(syntheticInitializers);
    newStatements.addAll(stmts);
    return functionNode.setBody(lc, body.setStatements(lc, newStatements));
}
 
Example 11
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 12
Source File: Parser.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Execute parse and return the resulting function node.
 * Errors will be thrown and the error manager will contain information
 * if parsing should fail. This method is used to check if code String
 * passed to "Function" constructor is a valid function body or not.
 *
 * @return function node resulting from successful parse
 */
public FunctionNode parseFunctionBody() {
    try {
        stream = new TokenStream();
        lexer  = new Lexer(source, stream, scripting && !env._no_syntax_extensions);
        final int functionLine = line;

        // Set up first token (skips opening EOL.)
        k = -1;
        next();

        // Make a fake token for the function.
        final long functionToken = Token.toDesc(FUNCTION, 0, source.getLength());
        // Set up the function to append elements.

        FunctionNode function = newFunctionNode(
            functionToken,
            new IdentNode(functionToken, Token.descPosition(functionToken), PROGRAM.symbolName()),
            new ArrayList<IdentNode>(),
            FunctionNode.Kind.NORMAL,
            functionLine);

        functionDeclarations = new ArrayList<>();
        sourceElements(false);
        addFunctionDeclarations(function);
        functionDeclarations = null;

        expect(EOF);

        function.setFinish(source.getLength() - 1);
        function = restoreFunctionNode(function, token); //commit code
        function = function.setBody(lc, function.getBody().setNeedsScope(lc));

        printAST(function);
        return function;
    } catch (final Exception e) {
        handleParseException(e);
        return null;
    }
}
 
Example 13
Source File: AssignSymbols.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private FunctionNode markProgramBlock(final FunctionNode functionNode) {
    if (compiler.isOnDemandCompilation() || !functionNode.isProgram()) {
        return functionNode;
    }

    return functionNode.setBody(lc, functionNode.getBody().setFlag(lc, Block.IS_GLOBAL_SCOPE));
}
 
Example 14
Source File: Parser.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Execute parse and return the resulting function node.
 * Errors will be thrown and the error manager will contain information
 * if parsing should fail. This method is used to check if code String
 * passed to "Function" constructor is a valid function body or not.
 *
 * @return function node resulting from successful parse
 */
public FunctionNode parseFunctionBody() {
    try {
        stream = new TokenStream();
        lexer  = new Lexer(source, stream, scripting && !env._no_syntax_extensions);

        // Set up first token (skips opening EOL.)
        k = -1;
        next();

        // Make a fake token for the function.
        final long functionToken = Token.toDesc(FUNCTION, 0, source.getLength());
        // Set up the function to append elements.

        FunctionNode function = newFunctionNode(
            functionToken,
            new IdentNode(functionToken, Token.descPosition(functionToken), RUN_SCRIPT.symbolName()),
            new ArrayList<IdentNode>(),
            FunctionNode.Kind.NORMAL);

        functionDeclarations = new ArrayList<>();
        sourceElements();
        addFunctionDeclarations(function);
        functionDeclarations = null;

        expect(EOF);

        function.setFinish(source.getLength() - 1);

        function = restoreFunctionNode(function, token); //commit code
        function = function.setBody(lc, function.getBody().setNeedsScope(lc));
        return function;
    } catch (final Exception e) {
        handleParseException(e);
        return null;
    }
}
 
Example 15
Source File: CacheAst.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Node leaveFunctionNode(final FunctionNode functionNode) {
    final RecompilableScriptFunctionData data = dataStack.pop();
    if (functionNode.isSplit()) {
        // NOTE: cache only split function ASTs from eager pass. Caching non-split functions would require
        // some additional work, namely creating the concept of "uncacheable" function and reworking
        // ApplySpecialization to ensure that functions undergoing apply-to-call transformations are not
        // cacheable as well as recomputing Symbol.useCount when caching the eagerly parsed AST.
        // Recomputing Symbol.useCount would be needed so it will only reflect uses from within the
        // function being cached (and not reflect uses from its own nested functions or functions it is
        // nested in). This is consistent with the count an on-demand recompilation of the function would
        // produce. This is important as the decision to emit shared scope calls is based on this count,
        // and if it is not matched between a previous version of the code and its deoptimizing rest-of
        // compilation, it can result in rest-of not emitting a shared scope call where a previous version
        // of the code (compiled from a cached eager pre-pass seeing higher (global) useCount) would emit
        // it, causing a mismatch in stack shapes between previous code and its rest-of.
        data.setCachedAst(functionNode);
    }

    if (!dataStack.isEmpty() && ((dataStack.peek().getFunctionFlags() & FunctionNode.IS_SPLIT) != 0)) {
        // Return a function node with no body so that caching outer functions doesn't hold on to nested
        // functions' bodies. Note we're doing this only for functions directly nested inside split
        // functions, since we're only caching the split ones. It is not necessary to limit body removal
        // to just these functions, but it's a cheap way to prevent unnecessary AST mutations.
        return functionNode.setBody(lc, functionNode.getBody().setStatements(null, Collections.<Statement>emptyList()));
    }
    return functionNode;
}
 
Example 16
Source File: Parser.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Execute parse and return the resulting function node.
 * Errors will be thrown and the error manager will contain information
 * if parsing should fail. This method is used to check if code String
 * passed to "Function" constructor is a valid function body or not.
 *
 * @return function node resulting from successful parse
 */
public FunctionNode parseFunctionBody() {
    try {
        stream = new TokenStream();
        lexer  = new Lexer(source, stream, scripting && !env._no_syntax_extensions);
        final int functionLine = line;

        // Set up first token (skips opening EOL.)
        k = -1;
        next();

        // Make a fake token for the function.
        final long functionToken = Token.toDesc(FUNCTION, 0, source.getLength());
        // Set up the function to append elements.

        FunctionNode function = newFunctionNode(
            functionToken,
            new IdentNode(functionToken, Token.descPosition(functionToken), PROGRAM.symbolName()),
            new ArrayList<IdentNode>(),
            FunctionNode.Kind.NORMAL,
            functionLine);

        functionDeclarations = new ArrayList<>();
        sourceElements(false);
        addFunctionDeclarations(function);
        functionDeclarations = null;

        expect(EOF);

        function.setFinish(source.getLength() - 1);
        function = restoreFunctionNode(function, token); //commit code
        function = function.setBody(lc, function.getBody().setNeedsScope(lc));

        printAST(function);
        return function;
    } catch (final Exception e) {
        handleParseException(e);
        return null;
    }
}
 
Example 17
Source File: CacheAst.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Node leaveFunctionNode(final FunctionNode functionNode) {
    final RecompilableScriptFunctionData data = dataStack.pop();
    if (functionNode.isSplit()) {
        // NOTE: cache only split function ASTs from eager pass. Caching non-split functions would require
        // some additional work, namely creating the concept of "uncacheable" function and reworking
        // ApplySpecialization to ensure that functions undergoing apply-to-call transformations are not
        // cacheable as well as recomputing Symbol.useCount when caching the eagerly parsed AST.
        // Recomputing Symbol.useCount would be needed so it will only reflect uses from within the
        // function being cached (and not reflect uses from its own nested functions or functions it is
        // nested in). This is consistent with the count an on-demand recompilation of the function would
        // produce. This is important as the decision to emit shared scope calls is based on this count,
        // and if it is not matched between a previous version of the code and its deoptimizing rest-of
        // compilation, it can result in rest-of not emitting a shared scope call where a previous version
        // of the code (compiled from a cached eager pre-pass seeing higher (global) useCount) would emit
        // it, causing a mismatch in stack shapes between previous code and its rest-of.
        data.setCachedAst(functionNode);
    }

    if (!dataStack.isEmpty() && ((dataStack.peek().getFunctionFlags() & FunctionNode.IS_SPLIT) != 0)) {
        // Return a function node with no body so that caching outer functions doesn't hold on to nested
        // functions' bodies. Note we're doing this only for functions directly nested inside split
        // functions, since we're only caching the split ones. It is not necessary to limit body removal
        // to just these functions, but it's a cheap way to prevent unnecessary AST mutations.
        return functionNode.setBody(lc, functionNode.getBody().setStatements(null, Collections.<Statement>emptyList()));
    }
    return functionNode;
}
 
Example 18
Source File: Parser.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Execute parse and return the resulting function node.
 * Errors will be thrown and the error manager will contain information
 * if parsing should fail. This method is used to check if code String
 * passed to "Function" constructor is a valid function body or not.
 *
 * @return function node resulting from successful parse
 */
public FunctionNode parseFunctionBody() {
    try {
        stream = new TokenStream();
        lexer  = new Lexer(source, stream, scripting && !env._no_syntax_extensions);

        // Set up first token (skips opening EOL.)
        k = -1;
        next();

        // Make a fake token for the function.
        final long functionToken = Token.toDesc(FUNCTION, 0, source.getLength());
        // Set up the function to append elements.

        FunctionNode function = newFunctionNode(
            functionToken,
            new IdentNode(functionToken, Token.descPosition(functionToken), RUN_SCRIPT.symbolName()),
            new ArrayList<IdentNode>(),
            FunctionNode.Kind.NORMAL);

        functionDeclarations = new ArrayList<>();
        sourceElements();
        addFunctionDeclarations(function);
        functionDeclarations = null;

        expect(EOF);

        function.setFinish(source.getLength() - 1);

        function = restoreFunctionNode(function, token); //commit code
        function = function.setBody(lc, function.getBody().setNeedsScope(lc));
        return function;
    } catch (final Exception e) {
        handleParseException(e);
        return null;
    }
}
 
Example 19
Source File: Parser.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Execute parse and return the resulting function node.
 * Errors will be thrown and the error manager will contain information
 * if parsing should fail. This method is used to check if code String
 * passed to "Function" constructor is a valid function body or not.
 *
 * @return function node resulting from successful parse
 */
public FunctionNode parseFunctionBody() {
    try {
        stream = new TokenStream();
        lexer  = new Lexer(source, stream, scripting && !env._no_syntax_extensions);

        // Set up first token (skips opening EOL.)
        k = -1;
        next();

        // Make a fake token for the function.
        final long functionToken = Token.toDesc(FUNCTION, 0, source.getLength());
        // Set up the function to append elements.

        FunctionNode function = newFunctionNode(
            functionToken,
            new IdentNode(functionToken, Token.descPosition(functionToken), RUN_SCRIPT.symbolName()),
            new ArrayList<IdentNode>(),
            FunctionNode.Kind.NORMAL);

        functionDeclarations = new ArrayList<>();
        sourceElements();
        addFunctionDeclarations(function);
        functionDeclarations = null;

        expect(EOF);

        function.setFinish(source.getLength() - 1);

        function = restoreFunctionNode(function, token); //commit code
        function = function.setBody(lc, function.getBody().setNeedsScope(lc));
        return function;
    } catch (final Exception e) {
        handleParseException(e);
        return null;
    }
}
 
Example 20
Source File: AssignSymbols.java    From hottub 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));
}