Search in sources :

Example 1 with ObjectProperty

use of org.mozilla.javascript.ast.ObjectProperty in project st-js by st-js.

the class RhinoJavaScriptBuilder method objectProperty.

private ObjectProperty objectProperty(CharSequence name, AstNode value) {
    ObjectProperty prop = new ObjectProperty();
    prop.setLeft(name(name));
    prop.setRight(value);
    return prop;
}
Also used : ObjectProperty(org.mozilla.javascript.ast.ObjectProperty)

Example 2 with ObjectProperty

use of org.mozilla.javascript.ast.ObjectProperty in project HL4A by HL4A.

the class IRFactory method transformObjectLiteral.

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());
            // 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;
}
Also used : ObjectProperty(org.mozilla.javascript.ast.ObjectProperty) ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode)

Example 3 with ObjectProperty

use of org.mozilla.javascript.ast.ObjectProperty in project HL4A by HL4A.

the class Parser method objectLiteral.

private ObjectLiteral objectLiteral() throws IOException {
    int pos = ts.tokenBeg, lineno = ts.lineno;
    int afterComma = -1;
    List<ObjectProperty> elems = new ArrayList<ObjectProperty>();
    Set<String> getterNames = null;
    Set<String> setterNames = null;
    if (this.inUseStrictDirective) {
        getterNames = new HashSet<String>();
        setterNames = new HashSet<String>();
    }
    Comment objJsdocNode = getAndResetJsDoc();
    commaLoop: for (; ; ) {
        String propertyName = null;
        int entryKind = PROP_ENTRY;
        int tt = peekToken();
        Comment jsdocNode = getAndResetJsDoc();
        if (tt == Token.RC) {
            if (afterComma != -1)
                warnTrailingComma(pos, elems, afterComma);
            break commaLoop;
        } else {
            AstNode pname = objliteralProperty();
            if (pname == null) {
                propertyName = null;
                reportError("msg.bad.prop");
            } else {
                propertyName = ts.getString();
                int ppos = ts.tokenBeg;
                consumeToken();
                // This code path needs to handle both destructuring object
                // literals like:
                // var {get, b} = {get: 1, b: 2};
                // and getters like:
                // var x = {get 1() { return 2; };
                // So we check a whitelist of tokens to check if we're at the
                // first case. (Because of keywords, the second case may be
                // many tokens.)
                int peeked = peekToken();
                if (peeked != Token.COMMA && peeked != Token.COLON && peeked != Token.RC) {
                    if (peeked == Token.LP) {
                        entryKind = METHOD_ENTRY;
                    } else if (pname.getType() == Token.NAME) {
                        if ("get".equals(propertyName)) {
                            entryKind = GET_ENTRY;
                        } else if ("set".equals(propertyName)) {
                            entryKind = SET_ENTRY;
                        }
                    }
                    if (entryKind == GET_ENTRY || entryKind == SET_ENTRY) {
                        pname = objliteralProperty();
                        if (pname == null) {
                            reportError("msg.bad.prop");
                        }
                        consumeToken();
                    }
                    if (pname == null) {
                        propertyName = null;
                    } else {
                        propertyName = ts.getString();
                        ObjectProperty objectProp = methodDefinition(ppos, pname, entryKind);
                        pname.setJsDocNode(jsdocNode);
                        elems.add(objectProp);
                    }
                } else {
                    pname.setJsDocNode(jsdocNode);
                    elems.add(plainProperty(pname, tt));
                }
            }
        }
        if (this.inUseStrictDirective && propertyName != null) {
            switch(entryKind) {
                case PROP_ENTRY:
                case METHOD_ENTRY:
                    if (getterNames.contains(propertyName) || setterNames.contains(propertyName)) {
                        addError("msg.dup.obj.lit.prop.strict", propertyName);
                    }
                    getterNames.add(propertyName);
                    setterNames.add(propertyName);
                    break;
                case GET_ENTRY:
                    if (getterNames.contains(propertyName)) {
                        addError("msg.dup.obj.lit.prop.strict", propertyName);
                    }
                    getterNames.add(propertyName);
                    break;
                case SET_ENTRY:
                    if (setterNames.contains(propertyName)) {
                        addError("msg.dup.obj.lit.prop.strict", propertyName);
                    }
                    setterNames.add(propertyName);
                    break;
            }
        }
        // Eat any dangling jsdoc in the property.
        getAndResetJsDoc();
        if (matchToken(Token.COMMA)) {
            afterComma = ts.tokenEnd;
        } else {
            break commaLoop;
        }
    }
    mustMatchToken(Token.RC, "msg.no.brace.prop");
    ObjectLiteral pn = new ObjectLiteral(pos, ts.tokenEnd - pos);
    if (objJsdocNode != null) {
        pn.setJsDocNode(objJsdocNode);
    }
    pn.setElements(elems);
    pn.setLineno(lineno);
    return pn;
}
Also used : ObjectProperty(org.mozilla.javascript.ast.ObjectProperty) Comment(org.mozilla.javascript.ast.Comment) ObjectLiteral(org.mozilla.javascript.ast.ObjectLiteral) ArrayList(java.util.ArrayList) XmlString(org.mozilla.javascript.ast.XmlString) AstNode(org.mozilla.javascript.ast.AstNode)

Example 4 with ObjectProperty

use of org.mozilla.javascript.ast.ObjectProperty in project HL4A by HL4A.

the class Parser method methodDefinition.

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;
}
Also used : ObjectProperty(org.mozilla.javascript.ast.ObjectProperty) FunctionNode(org.mozilla.javascript.ast.FunctionNode) Name(org.mozilla.javascript.ast.Name)

Example 5 with ObjectProperty

use of org.mozilla.javascript.ast.ObjectProperty in project HL4A by HL4A.

the class IRFactory method decompileObjectLiteral.

// only used for destructuring forms
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);
}
Also used : ObjectProperty(org.mozilla.javascript.ast.ObjectProperty)

Aggregations

ObjectProperty (org.mozilla.javascript.ast.ObjectProperty)7 AstNode (org.mozilla.javascript.ast.AstNode)4 FunctionNode (org.mozilla.javascript.ast.FunctionNode)3 Name (org.mozilla.javascript.ast.Name)3 LetNode (org.mozilla.javascript.ast.LetNode)2 ScriptNode (org.mozilla.javascript.ast.ScriptNode)2 XmlString (org.mozilla.javascript.ast.XmlString)2 ArrayList (java.util.ArrayList)1 Comment (org.mozilla.javascript.ast.Comment)1 ErrorNode (org.mozilla.javascript.ast.ErrorNode)1 NumberLiteral (org.mozilla.javascript.ast.NumberLiteral)1 ObjectLiteral (org.mozilla.javascript.ast.ObjectLiteral)1 StringLiteral (org.mozilla.javascript.ast.StringLiteral)1