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

The following examples show how to use org.mozilla.javascript.ast.FunctionNode#getName() . 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: 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 3
Source File: ClassCompiler.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Compile JavaScript source into one or more Java class files.
 * The first compiled class will have name mainClassName.
 * If the results of {@link #getTargetExtends()} or
 * {@link #getTargetImplements()} are not null, then the first compiled
 * class will extend the specified super class and implement
 * specified interfaces.
 *
 * @return array where elements with even indexes specifies class name
 *         and the following odd index gives class file body as byte[]
 *         array. The initial element of the array always holds
 *         mainClassName and array[1] holds its byte code.
 */
public Object[] compileToClassFiles(String source,
                                    String sourceLocation,
                                    int lineno,
                                    String mainClassName)
{
    Parser p = new Parser(compilerEnv);
    AstRoot ast = p.parse(source, sourceLocation, lineno);
    IRFactory irf = new IRFactory(compilerEnv);
    ScriptNode tree = irf.transformTree(ast);

    // release reference to original parse tree & parser
    irf = null;
    ast = null;
    p = null;

    Class<?> superClass = getTargetExtends();
    Class<?>[] interfaces = getTargetImplements();
    String scriptClassName;
    boolean isPrimary = (interfaces == null && superClass == null);
    if (isPrimary) {
        scriptClassName = mainClassName;
    } else {
        scriptClassName = makeAuxiliaryClassName(mainClassName, "1");
    }

    Codegen codegen = new Codegen();
    codegen.setMainMethodClass(mainMethodClassName);
    byte[] scriptClassBytes
        = codegen.compileToClassFile(compilerEnv, scriptClassName,
                                     tree, tree.getEncodedSource(),
                                     false);

    if (isPrimary) {
        return new Object[] { scriptClassName, scriptClassBytes };
    }
    int functionCount = tree.getFunctionCount();
    ObjToIntMap functionNames = new ObjToIntMap(functionCount);
    for (int i = 0; i != functionCount; ++i) {
        FunctionNode ofn = tree.getFunctionNode(i);
        String name = ofn.getName();
        if (name != null && name.length() != 0) {
            functionNames.put(name, ofn.getParamCount());
        }
    }
    if (superClass == null) {
        superClass = ScriptRuntime.ObjectClass;
    }
    byte[] mainClassBytes
        = JavaAdapter.createAdapterCode(
            functionNames, mainClassName,
            superClass, interfaces, scriptClassName);

    return new Object[] { mainClassName, mainClassBytes,
                          scriptClassName, scriptClassBytes };
}
 
Example 4
Source File: ConstraintGenUtil.java    From SJS with Apache License 2.0 4 votes vote down vote up
/**
 * Determines if a FunctionNode is a constructor. This is done simply by
 * checking if its name starts with a capital letter.
 */
public static boolean isConstructor(FunctionNode fun){
	String funName = fun.getName();
	return (funName != null && funName.length() > 0 && funName.charAt(0) >= 'A' && funName.charAt(0) <= 'Z');
}
 
Example 5
Source File: ConstraintVisitor.java    From SJS with Apache License 2.0 4 votes vote down vote up
/**
 * assignment to the "prototype" property
 */
private void processAssignToPrototype(Assignment a, AstNode left, AstNode right, ITypeTerm expTerm) throws Error {
	PropertyGet pg = (PropertyGet)left;
	AstNode base = pg.getTarget();
	ITypeTerm pgTerm = findOrCreateExpressionTerm(pg);
	if (base instanceof Name){
		Name name = (Name)base;
		if (!validRHSForAssignToPrototype(right)) {
			error(
					"expression "
							+ right.toSource()
							+ " cannot be assigned to a constructor prototype (line "
							+ right.getLineno() + ")", a);
		}
		// can only write to prototype immediately after declaration of
		// constructor of the same name
		AstNode parent = a.getParent();
		if (!(parent instanceof ExpressionStatement)) {
			error(
					"assignment to prototype property not allowed here (line "
							+ a.getLineno() + ")", a);
			return;
		}
		Node prev = getPredecessorNode(parent);
		if (!(prev instanceof FunctionNode)) {
			error(
					"assignment to prototype property only allowed after constructor declaration (line "
							+ a.getLineno() + ")", a);
			return;
		}
		FunctionNode fn = (FunctionNode) prev;
		String functionName = fn.getName();
		String identifier = name.getIdentifier();
		if (!functionName.equals(identifier)) {
			error(
					"can only assign to prototype of function "
							+ functionName + " here (line " + a.getLineno()
							+ ")", a);
			return;
		}
		ITypeTerm baseTerm = findOrCreateExpressionTerm(base); // make term for expression
		ITypeTerm nameTerm = findOrCreateNameDeclarationTerm(name); // find unique representative for referenced Name
		addTypeEqualityConstraint(baseTerm, nameTerm, a.getLineno(), null); // equate them
		ITypeTerm protoTerm = findOrCreateProtoTerm(baseTerm, pg.getLineno());
		ITypeTerm rightTerm = processExpression(right);
		addTypeEqualityConstraint(pgTerm, protoTerm, a.getLineno(), null);
		addTypeEqualityConstraint(rightTerm, protoTerm, a.getLineno(), null);
		addTypeEqualityConstraint(expTerm, protoTerm, a.getLineno(), null);
	} else {
		error("processAssignToPrototype: unsupported case for receiver expression: " + base.getClass().getName(), base);
	}
}
 
Example 6
Source File: ConstraintVisitor.java    From SJS with Apache License 2.0 4 votes vote down vote up
private void checkForValidProtoPropAssign(Assignment a, PropertyGet pg,
		int assignLineNo, PropertyGet basePG) {
	AstNode baseTarget = basePG.getTarget();
	if (!(baseTarget instanceof Name)) {
		error("assignment to property of prototype not valid (line " + assignLineNo + ")", a);
		return;
	}
	Name baseName = (Name) baseTarget;
	AstNode parent = a.getParent();
	if (!(parent instanceof ExpressionStatement)) {
		error("assignment to property of prototype not valid (line " + assignLineNo + ")", a);
		return;
	}
	Node prev = getPredecessorNode(parent);
	if (prev instanceof FunctionNode) {
		FunctionNode fn = (FunctionNode) prev;
		String functionName = fn.getName();
		String identifier = baseName.getIdentifier();
		if (!functionName.equals(identifier)) {
			error("can only assign to prototype of function " + functionName + " here (line " + assignLineNo + ")", a);
			return;
		}

	} else if (prev instanceof ExpressionStatement) {
		// it needs to be an assignment either to C.prototype or C.prototype.foo
		// TODO clean up this gross code
		AstNode expression = ((ExpressionStatement)prev).getExpression();
		if (!(expression instanceof Assignment)) {
			error("assignment to property of prototype not valid (line " + assignLineNo + ")", a);
			return;
		}
		Assignment prevAssign = (Assignment) expression;
		AstNode prevLeft = prevAssign.getLeft();
		if (!(prevLeft instanceof PropertyGet)) {
			error("assignment to property of prototype not valid (line " + assignLineNo + ")", a);
			return;
		}
		PropertyGet prevPG = (PropertyGet) prevLeft;
		AstNode prevPGTarget = prevPG.getTarget();
		if (prevPG.getProperty().getIdentifier().equals("prototype")) {
			checkForSameName(assignLineNo, baseName, prevPGTarget);
		} else if (prevPGTarget instanceof PropertyGet) {
			PropertyGet prevPGBasePG = (PropertyGet) prevPGTarget;
			if (!prevPGBasePG.getProperty().getIdentifier().equals("prototype")) {
				error("assignment to property of prototype not valid (line " + assignLineNo + ")", a);
				return;
			}
			checkForSameName(assignLineNo, baseName, prevPGBasePG.getTarget());
		} else {
			error("assignment to property of prototype not valid (line " + assignLineNo + ")", a);
			return;
		}
	} else {
		error("assignment to property of prototype not valid (line " + assignLineNo + ")", a);
		return;
	}
}
 
Example 7
Source File: ClassCompiler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compile JavaScript source into one or more Java class files.
 * The first compiled class will have name mainClassName.
 * If the results of {@link #getTargetExtends()} or
 * {@link #getTargetImplements()} are not null, then the first compiled
 * class will extend the specified super class and implement
 * specified interfaces.
 *
 * @return array where elements with even indexes specifies class name
 *         and the following odd index gives class file body as byte[]
 *         array. The initial element of the array always holds
 *         mainClassName and array[1] holds its byte code.
 */
public Object[] compileToClassFiles(String source,
                                    String sourceLocation,
                                    int lineno,
                                    String mainClassName)
{
    Parser p = new Parser(compilerEnv);
    AstRoot ast = p.parse(source, sourceLocation, lineno);
    IRFactory irf = new IRFactory(compilerEnv);
    ScriptNode tree = irf.transformTree(ast);

    // release reference to original parse tree & parser
    irf = null;
    ast = null;
    p = null;

    Class<?> superClass = getTargetExtends();
    Class<?>[] interfaces = getTargetImplements();
    String scriptClassName;
    boolean isPrimary = (interfaces == null && superClass == null);
    if (isPrimary) {
        scriptClassName = mainClassName;
    } else {
        scriptClassName = makeAuxiliaryClassName(mainClassName, "1");
    }

    Codegen codegen = new Codegen();
    codegen.setMainMethodClass(mainMethodClassName);
    byte[] scriptClassBytes
        = codegen.compileToClassFile(compilerEnv, scriptClassName,
                                     tree, tree.getEncodedSource(),
                                     false);

    if (isPrimary) {
        return new Object[] { scriptClassName, scriptClassBytes };
    }
    int functionCount = tree.getFunctionCount();
    ObjToIntMap functionNames = new ObjToIntMap(functionCount);
    for (int i = 0; i != functionCount; ++i) {
        FunctionNode ofn = tree.getFunctionNode(i);
        String name = ofn.getName();
        if (name != null && name.length() != 0) {
            functionNames.put(name, ofn.getParamCount());
        }
    }
    if (superClass == null) {
        superClass = ScriptRuntime.ObjectClass;
    }
    byte[] mainClassBytes
        = JavaAdapter.createAdapterCode(
            functionNames, mainClassName,
            superClass, interfaces, scriptClassName);

    return new Object[] { mainClassName, mainClassBytes,
                          scriptClassName, scriptClassBytes };
}