Java Code Examples for org.mozilla.javascript.ast.FunctionNode#getFunctionName()

The following examples show how to use org.mozilla.javascript.ast.FunctionNode#getFunctionName() . 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: CodeGenerator.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
private void generateFunctionICode()
{
    itsInFunctionFlag = true;

    FunctionNode theFunction = (FunctionNode)scriptOrFn;

    itsData.itsFunctionType = theFunction.getFunctionType();
    itsData.itsNeedsActivation = theFunction.requiresActivation();
    if (theFunction.getFunctionName() != null) {
        itsData.itsName = theFunction.getName();
    }
    if (theFunction.isGenerator()) {
      addIcode(Icode_GENERATOR);
      addUint16(theFunction.getBaseLineno() & 0xFFFF);
    }
    if (theFunction.isInStrictMode()) {
        itsData.isStrict = true;
    }

    generateICodeFromTree(theFunction.getLastChild());
}
 
Example 2
Source File: ConstraintVisitor.java    From SJS with Apache License 2.0 6 votes vote down vote up
/**
 * For a function definition (FunctionNode), create constraints equating its
 * parameters to param(.) variables, and equate the type of the function name
 * to the type of the entire function definition.
 */
private void createFunctionNodeConstraints(FunctionNode node, ITypeTerm funTerm){
	// if the function has a name, equate the types of the function node and the function name
	Name funName = node.getFunctionName();
	if (funName != null){
		ITypeTerm nameTerm = findOrCreateNameDeclarationTerm(funName);
		addSubTypeConstraint(funTerm, nameTerm, node.getLineno(), null);
	}

	// for a function f with params v1, v2, ... generate param(|f|,i) = |v_i|
	for (int i=0; i < node.getParamCount(); i++){
		AstNode param = node.getParams().get(i);
		if (param instanceof Name){
			Name name = (Name)param;
			ITypeTerm nameVar = findOrCreateNameDeclarationTerm(name);
			ITypeTerm paramVar = findOrCreateFunctionParamTerm(funTerm, i, node.getParamCount(), node.getLineno());
			addTypeEqualityConstraint(paramVar, nameVar, param.getLineno(), null);
		} else {
			error("createFunctionNodeConstraints: unexpected parameter type", node);
		}
	}

}
 
Example 3
Source File: CodeGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void generateFunctionICode()
{
    itsInFunctionFlag = true;

    FunctionNode theFunction = (FunctionNode)scriptOrFn;

    itsData.itsFunctionType = theFunction.getFunctionType();
    itsData.itsNeedsActivation = theFunction.requiresActivation();
    if (theFunction.getFunctionName() != null) {
        itsData.itsName = theFunction.getName();
    }
    if (theFunction.isGenerator()) {
      addIcode(Icode_GENERATOR);
      addUint16(theFunction.getBaseLineno() & 0xFFFF);
    }

    generateICodeFromTree(theFunction.getLastChild());
}
 
Example 4
Source File: IRFactory.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
Node decompileFunctionHeader(FunctionNode fn) {
    Node mexpr = null;
    if (fn.getFunctionName() != null) {
        decompiler.addName(fn.getName());
    } else if (fn.getMemberExprNode() != null) {
        mexpr = transform(fn.getMemberExprNode());
    }
    boolean isArrow = fn.getFunctionType() == FunctionNode.ARROW_FUNCTION;
    boolean noParen = isArrow && fn.getLp() == -1;
    if (!noParen) {
        decompiler.addToken(Token.LP);
    }
    List<AstNode> params = fn.getParams();
    for (int i = 0; i < params.size(); i++) {
        decompile(params.get(i));
        if (i < params.size() - 1) {
            decompiler.addToken(Token.COMMA);
        }
    }
    if (!noParen) {
        decompiler.addToken(Token.RP);
    }
    if (isArrow) {
        decompiler.addToken(Token.ARROW);
    }
    if (!fn.isExpressionClosure()) {
        decompiler.addEOL(Token.LC);
    }
    return mexpr;
}
 
Example 5
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
private ObjectProperty methodDefinition(int pos, AstNode propName, int entryKind)
    throws IOException
{
    FunctionNode fn = function(FunctionNode.FUNCTION_EXPRESSION);
    // We've already parsed the function name, so fn should be anonymous.
    Name name = fn.getFunctionName();
    if (name != null && name.length() != 0) {
        reportError("msg.bad.prop");
    }
    ObjectProperty pn = new ObjectProperty(pos);
    switch (entryKind) {
    case GET_ENTRY:
        pn.setIsGetterMethod();
        fn.setFunctionIsGetterMethod();
        break;
    case SET_ENTRY:
        pn.setIsSetterMethod();
        fn.setFunctionIsSetterMethod();
        break;
    case METHOD_ENTRY:
        pn.setIsNormalMethod();
        fn.setFunctionIsNormalMethod();
        break;
    }
    int end = getNodeEnd(fn);
    pn.setLeft(propName);
    pn.setRight(fn);
    pn.setLength(end - pos);
    return pn;
}
 
Example 6
Source File: IRFactory.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
private Node initFunction(FunctionNode fnNode, int functionIndex,
                          Node statements, int functionType) {
    fnNode.setFunctionType(functionType);
    fnNode.addChildToBack(statements);

    int functionCount = fnNode.getFunctionCount();
    if (functionCount != 0) {
        // Functions containing other functions require activation objects
        fnNode.setRequiresActivation();
    }

    if (functionType == FunctionNode.FUNCTION_EXPRESSION) {
        Name name = fnNode.getFunctionName();
        if (name != null && name.length() != 0
                && fnNode.getSymbol(name.getIdentifier()) == null) {
            // A function expression needs to have its name as a
            // variable (if it isn't already allocated as a variable).
            // See ECMA Ch. 13.  We add code to the beginning of the
            // function to initialize a local variable of the
            // function's name to the function value, but only if the
            // function doesn't already define a formal parameter, var,
            // or nested function with the same name.
            fnNode.putSymbol(new Symbol(Token.FUNCTION, name.getIdentifier()));
            Node setFn = new Node(Token.EXPR_VOID,
                             new Node(Token.SETNAME,
                                      Node.newString(Token.BINDNAME,
                                                     name.getIdentifier()),
                                 new Node(Token.THISFN)));
            statements.addChildrenToFront(setFn);
        }
    }

    // Add return to end if needed.
    Node lastStmt = statements.getLastChild();
    if (lastStmt == null || lastStmt.getType() != Token.RETURN) {
        statements.addChildToBack(new Node(Token.RETURN));
    }

    Node result = Node.newString(Token.FUNCTION, fnNode.getName());
    result.putIntProp(Node.FUNCTION_PROP, functionIndex);
    return result;
}