Java Code Examples for org.mozilla.javascript.ast.ArrayLiteral#getElements()

The following examples show how to use org.mozilla.javascript.ast.ArrayLiteral#getElements() . 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: ConstraintVisitor.java    From SJS with Apache License 2.0 6 votes vote down vote up
/**
 * create constraint variable for the array literal. create subtype constraints between expressions
 * in the literal and the array's element type
 */
private ITypeTerm processArrayLiteral(ArrayLiteral lit) {
	ITypeTerm arrayTerm = findOrCreateArrayLiteralTerm(lit);

	ITypeTerm elemTerm = findOrCreateIndexedTerm(arrayTerm, lit.getLineno());
	List<AstNode> elements = lit.getElements();
	for (AstNode litElem : elements){
		ITypeTerm litElemTerm = processExpression(litElem);
		processCopy(litElem, litElemTerm, elemTerm,
				lit.getLineno(), (solution) -> subtypeError("array cannot contain " + shortSrc(litElem),
						solution.typeOfTerm(litElemTerm), solution.typeOfTerm(elemTerm), locationOf(litElem)));
	}

	createArrayConstraints(arrayTerm, lit);

	return arrayTerm;
}
 
Example 2
Source File: IRFactory.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
private Node transformArrayLiteral(ArrayLiteral node) {
    if (node.isDestructuring()) {
        return node;
    }
    decompiler.addToken(Token.LB);
    List<AstNode> elems = node.getElements();
    Node array = new Node(Token.ARRAYLIT);
    List<Integer> skipIndexes = null;
    for (int i = 0; i < elems.size(); ++i) {
        AstNode elem = elems.get(i);
        if (elem.getType() != Token.EMPTY) {
            array.addChildToBack(transform(elem));
        } else {
            if (skipIndexes == null) {
                skipIndexes = new ArrayList<Integer>();
            }
            skipIndexes.add(i);
        }
        if (i < elems.size() - 1)
            decompiler.addToken(Token.COMMA);
    }
    decompiler.addToken(Token.RB);
    array.putIntProp(Node.DESTRUCTURING_ARRAY_LENGTH,
                     node.getDestructuringLength());
    if (skipIndexes != null) {
        int[] skips = new int[skipIndexes.size()];
        for (int i = 0; i < skipIndexes.size(); i++)
            skips[i] = skipIndexes.get(i);
        array.putProp(Node.SKIP_INDEXES_PROP, skips);
    }
    return array;
}
 
Example 3
Source File: IRFactory.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
void decompileArrayLiteral(ArrayLiteral node) {
    decompiler.addToken(Token.LB);
    List<AstNode> elems = node.getElements();
    int size = elems.size();
    for (int i = 0; i < size; i++) {
        AstNode elem = elems.get(i);
        decompile(elem);
        if (i < size - 1) {
            decompiler.addToken(Token.COMMA);
        }
    }
    decompiler.addToken(Token.RB);
}
 
Example 4
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
boolean destructuringArray(ArrayLiteral array,
                           int variableType,
                           String tempName,
                           Node parent,
                           List<String> destructuringNames)
{
    boolean empty = true;
    int setOp = variableType == Token.CONST
        ? Token.SETCONST : Token.SETNAME;
    int index = 0;
    for (AstNode n : array.getElements()) {
        if (n.getType() == Token.EMPTY) {
            index++;
            continue;
        }
        Node rightElem = new Node(Token.GETELEM,
                                  createName(tempName),
                                  createNumber(index));
        if (n.getType() == Token.NAME) {
            String name = n.getString();
            parent.addChildToBack(new Node(setOp,
                                          createName(Token.BINDNAME,
                                                     name, null),
                                          rightElem));
            if (variableType != -1) {
                defineSymbol(variableType, name, true);
                destructuringNames.add(name);
            }
        } else {
            parent.addChildToBack
                (destructuringAssignmentHelper
                 (variableType, n,
                  rightElem,
                  currentScriptOrFn.getNextTempName()));
        }
        index++;
        empty = false;
    }
    return empty;
}
 
Example 5
Source File: ClassDefScanner.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns string array literal values.
 */
private List<String> arrayStringLiteral(final AstNode node) {
  checkState(node instanceof ArrayLiteral, node, "Expected array literal only");
  List<String> result = new ArrayList<>();
  //noinspection ConstantConditions
  ArrayLiteral array = (ArrayLiteral) node;
  for (AstNode element : array.getElements()) {
    result.add(stringLiteral(element));
  }
  return result;
}