use of org.mozilla.javascript.ast.FunctionNode in project HL4A by HL4A.
the class IRFactory method transformGenExpr.
private Node transformGenExpr(GeneratorExpression node) {
Node pn;
FunctionNode fn = new FunctionNode();
fn.setSourceName(currentScriptOrFn.getNextTempName());
fn.setIsGenerator();
fn.setFunctionType(FunctionNode.FUNCTION_EXPRESSION);
fn.setRequiresActivation();
int functionType = fn.getFunctionType();
int start = decompiler.markFunctionStart(functionType);
Node mexpr = decompileFunctionHeader(fn);
int index = currentScriptOrFn.addFunction(fn);
PerFunctionVariables savedVars = new PerFunctionVariables(fn);
try {
// If we start needing to record much more codegen metadata during
// function parsing, we should lump it all into a helper class.
Node destructuring = (Node) fn.getProp(Node.DESTRUCTURING_PARAMS);
fn.removeProp(Node.DESTRUCTURING_PARAMS);
int lineno = node.lineno;
// only for body, not params
++nestingOfFunction;
Node body = genExprTransformHelper(node);
if (!fn.isExpressionClosure()) {
decompiler.addToken(Token.RC);
}
fn.setEncodedSourceBounds(start, decompiler.markFunctionEnd(start));
if (functionType != FunctionNode.FUNCTION_EXPRESSION && !fn.isExpressionClosure()) {
// Add EOL only if function is not part of expression
// since it gets SEMI + EOL from Statement in that case
decompiler.addToken(Token.EOL);
}
if (destructuring != null) {
body.addChildToFront(new Node(Token.EXPR_VOID, destructuring, lineno));
}
int syntheticType = fn.getFunctionType();
pn = initFunction(fn, index, body, syntheticType);
if (mexpr != null) {
pn = createAssignment(Token.ASSIGN, mexpr, pn);
if (syntheticType != FunctionNode.FUNCTION_EXPRESSION) {
pn = createExprStatementNoReturn(pn, fn.getLineno());
}
}
} finally {
--nestingOfFunction;
savedVars.restore();
}
Node call = createCallOrNew(Token.CALL, pn);
call.setLineno(node.getLineno());
decompiler.addToken(Token.LP);
decompiler.addToken(Token.RP);
return call;
}
use of org.mozilla.javascript.ast.FunctionNode in project HL4A by HL4A.
the class IRFactory method decompileFunctionHeader.
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;
}
use of org.mozilla.javascript.ast.FunctionNode 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;
}
use of org.mozilla.javascript.ast.FunctionNode in project HL4A by HL4A.
the class Parser method parseFunctionParams.
private void parseFunctionParams(FunctionNode fnNode) throws IOException {
if (matchToken(Token.RP)) {
fnNode.setRp(ts.tokenBeg - fnNode.getPosition());
return;
}
// Would prefer not to call createDestructuringAssignment until codegen,
// but the symbol definitions have to happen now, before body is parsed.
Map<String, Node> destructuring = null;
Set<String> paramNames = new HashSet<String>();
do {
int tt = peekToken();
if (tt == Token.LB || tt == Token.LC) {
AstNode expr = destructuringPrimaryExpr();
markDestructuring(expr);
fnNode.addParam(expr);
// variables from the destructuring assignment
if (destructuring == null) {
destructuring = new HashMap<String, Node>();
}
String pname = currentScriptOrFn.getNextTempName();
defineSymbol(Token.LP, pname, false);
destructuring.put(pname, expr);
} else {
if (mustMatchToken(Token.NAME, "msg.no.parm")) {
Name paramNameNode = createNameNode();
Comment jsdocNodeForName = getAndResetJsDoc();
if (jsdocNodeForName != null) {
paramNameNode.setJsDocNode(jsdocNodeForName);
}
fnNode.addParam(paramNameNode);
String paramName = ts.getString();
defineSymbol(Token.LP, paramName);
if (this.inUseStrictDirective) {
if ("eval".equals(paramName) || "arguments".equals(paramName)) {
reportError("msg.bad.id.strict", paramName);
}
if (paramNames.contains(paramName))
addError("msg.dup.param.strict", paramName);
paramNames.add(paramName);
}
} else {
fnNode.addParam(makeErrorNode());
}
}
} while (matchToken(Token.COMMA));
if (destructuring != null) {
Node destructuringNode = new Node(Token.COMMA);
// Add assignment helper for each destructuring parameter
for (Map.Entry<String, Node> param : destructuring.entrySet()) {
Node assign = createDestructuringAssignment(Token.VAR, param.getValue(), createName(param.getKey()));
destructuringNode.addChildToBack(assign);
}
fnNode.putProp(Node.DESTRUCTURING_PARAMS, destructuringNode);
}
if (mustMatchToken(Token.RP, "msg.no.paren.after.parms")) {
fnNode.setRp(ts.tokenBeg - fnNode.getPosition());
}
}
use of org.mozilla.javascript.ast.FunctionNode in project HL4A by HL4A.
the class Node method toStringTreeHelper.
private static void toStringTreeHelper(ScriptNode treeTop, Node n, ObjToIntMap printIds, int level, StringBuilder sb) {
if (Token.printTrees) {
if (printIds == null) {
printIds = new ObjToIntMap();
generatePrintIds(treeTop, printIds);
}
for (int i = 0; i != level; ++i) {
sb.append(" ");
}
n.toString(printIds, sb);
sb.append('\n');
for (Node cursor = n.getFirstChild(); cursor != null; cursor = cursor.getNext()) {
if (cursor.getType() == Token.FUNCTION) {
int fnIndex = cursor.getExistingIntProp(Node.FUNCTION_PROP);
FunctionNode fn = treeTop.getFunctionNode(fnIndex);
toStringTreeHelper(fn, fn, null, level + 1, sb);
} else {
toStringTreeHelper(treeTop, cursor, printIds, level + 1, sb);
}
}
}
}
Aggregations