org.mozilla.javascript.ast.ObjectProperty Java Examples

The following examples show how to use org.mozilla.javascript.ast.ObjectProperty. 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: IRFactory.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
void decompileObjectLiteral(ObjectLiteral node) {
    decompiler.addToken(Token.LC);
    List<ObjectProperty> props = node.getElements();
    int size = props.size();
    for (int i = 0; i < size; i++) {
        ObjectProperty prop = props.get(i);
        boolean destructuringShorthand =
                Boolean.TRUE.equals(prop.getProp(Node.DESTRUCTURING_SHORTHAND));
        decompile(prop.getLeft());
        if (!destructuringShorthand) {
            decompiler.addToken(Token.COLON);
            decompile(prop.getRight());
        }
        if (i < size - 1) {
            decompiler.addToken(Token.COMMA);
        }
    }
    decompiler.addToken(Token.RC);
}
 
Example #2
Source File: MapLiteralTerm.java    From SJS with Apache License 2.0 6 votes vote down vote up
public List<String> getPropertyNames(){
	List<String> propNames = new ArrayList<String>();
	ObjectLiteral ol = (ObjectLiteral)this.getNode();
	for (ObjectProperty op : ol.getElements()){
		AstNode left = op.getLeft();
		if (left instanceof Name){
			propNames.add(((Name)left).getIdentifier());
		} else if (left instanceof StringLiteral){
			String identifier = ConstraintGenUtil.removeQuotes(((StringLiteral)left).toSource());
			propNames.add(identifier);
		} else {
			System.err.println(left.getClass().getName() + " " + left.toSource());
			throw new Error("unsupported case in getPropertyNames()");
		}
	}
	return propNames;
}
 
Example #3
Source File: ObjectLiteralTerm.java    From SJS with Apache License 2.0 6 votes vote down vote up
public List<String> getPropertyNames(){
	List<String> propNames = new ArrayList<String>();
	ObjectLiteral ol = (ObjectLiteral)this.getNode();
	for (ObjectProperty op : ol.getElements()){
		AstNode left = op.getLeft();
		if (left instanceof Name){
			propNames.add(((Name)left).getIdentifier());
		} else if (left instanceof StringLiteral){
			String identifier = ConstraintGenUtil.removeQuotes(((StringLiteral)left).toSource());
			propNames.add(identifier);
		} else {
			System.err.println(left.getClass().getName() + " " + left.toSource());
			throw new Error("unsupported case in getPropertyNames()");
		}
	}
	return propNames;
}
 
Example #4
Source File: ConstraintGenUtil.java    From SJS with Apache License 2.0 6 votes vote down vote up
public static List<String> getPropertyNames(ObjectLiteral ol){
	List<String> propNames = new ArrayList<String>();
	for (ObjectProperty op : ol.getElements()){
		AstNode left = op.getLeft();
		if (left instanceof Name){
			propNames.add(((Name)left).getIdentifier());
		} else if (left instanceof StringLiteral){
			String identifier = ConstraintGenUtil.removeQuotes(((StringLiteral)left).toSource());
			propNames.add(identifier);
		} else {
			System.err.println(left.getClass().getName() + " " + left.toSource());
			throw new Error("unsupported case in getPropertyNames()");
		}
	}
	return propNames;
}
 
Example #5
Source File: ConstraintVisitor.java    From SJS with Apache License 2.0 6 votes vote down vote up
/**
 * Create constraints for an object literal.
 */
private ITypeTerm processObjectLiteralForObject(ObjectLiteral n) {
	ITypeTerm expTerm = findOrCreateObjectLiteralTerm(n);
	ObjectLiteral o = (ObjectLiteral)n;
	for (ObjectProperty prop : o.getElements()){
		AstNode left = prop.getLeft();
		AstNode right = prop.getRight();
		if (left instanceof Name){
			String identifier = ((Name)left).getIdentifier();

			// for object literal o = { name_1 : exp_1, ..., name_k : exp_k } generate
			// a constraint |exp_i| <: prop(|o|, name_i)

			ITypeTerm propTerm = findOrCreatePropertyAccessTerm(expTerm, identifier, null);
			ITypeTerm valTerm = processExpression(right);
			processCopy(right, valTerm, propTerm, n.getLineno(), null);
		}
	}
	return expTerm;
}
 
Example #6
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 #7
Source File: ConstraintGenUtil.java    From SJS with Apache License 2.0 5 votes vote down vote up
/**
 * Tests if an object literal is an object by checking that all
 * properties are unquoted.
 */
static boolean isObject(ObjectLiteral o){
	boolean result = (o.getElements().size() > 0);
	for (ObjectProperty prop : o.getElements()){
		AstNode left = prop.getLeft();
		result = result && (left instanceof Name);
	}
	return result;
}
 
Example #8
Source File: ConstraintGenUtil.java    From SJS with Apache License 2.0 5 votes vote down vote up
/**
 * Tests if an object literal is a map by checking that
 * all properties are quoted.
 * In JavaScript, both double quotes and single quotes are
 * supported but for now we assume double quotes are used.
 *
 * Empty object literals are assumed to be maps.
 */
static boolean isMap(ObjectLiteral o){
	boolean result = true;
	for (ObjectProperty prop : o.getElements()){
		AstNode left = prop.getLeft();
		result = result && (left instanceof StringLiteral);
	}
	return result;
}
 
Example #9
Source File: IRFactory.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
private Node transformObjectLiteral(ObjectLiteral node) {
    if (node.isDestructuring()) {
        return node;
    }
    // createObjectLiteral rewrites its argument as object
    // creation plus object property entries, so later compiler
    // stages don't need to know about object literals.
    decompiler.addToken(Token.LC);
    List<ObjectProperty> elems = node.getElements();
    Node object = new Node(Token.OBJECTLIT);
    Object[] properties;
    if (elems.isEmpty()) {
        properties = ScriptRuntime.emptyArgs;
    } else {
        int size = elems.size(), i = 0;
        properties = new Object[size];
        for (ObjectProperty prop : elems) {
            if (prop.isGetterMethod()) {
                decompiler.addToken(Token.GET);
            } else if (prop.isSetterMethod()) {
                decompiler.addToken(Token.SET);
            } else if (prop.isNormalMethod()) {
                decompiler.addToken(Token.METHOD);
            }

            properties[i++] = getPropKey(prop.getLeft());

            // OBJECTLIT is used as ':' in object literal for
            // decompilation to solve spacing ambiguity.
            if (!(prop.isMethod())) {
                decompiler.addToken(Token.OBJECTLIT);
            }

            Node right = transform(prop.getRight());
            if (prop.isGetterMethod()) {
                right = createUnary(Token.GET, right);
            } else if (prop.isSetterMethod()) {
                right = createUnary(Token.SET, right);
            } else if (prop.isNormalMethod()) {
                right = createUnary(Token.METHOD, right);
            }
            object.addChildToBack(right);

            if (i < size) {
                decompiler.addToken(Token.COMMA);
            }
        }
    }
    decompiler.addToken(Token.RC);
    object.putProp(Node.OBJECT_IDS_PROP, properties);
    return object;
}
 
Example #10
Source File: ConstraintGenUtil.java    From SJS with Apache License 2.0 4 votes vote down vote up
public static boolean isMethodAssignedInObjectLiteral(FunctionNode n){
	return (n.getParent() instanceof ObjectProperty && n.getParent().getParent() instanceof ObjectLiteral);
}