use of org.mozilla.javascript.ast.FunctionNode in project st-js by st-js.
the class RhinoJavaScriptBuilder method function.
/**
* {@inheritDoc}
*/
@Override
public AstNode function(String name, Iterable<AstNode> params, AstNode body) {
FunctionNode func = new FunctionNode();
if (name != null) {
func.setFunctionName((Name) name(name));
}
func.setParams(list(params));
if (body == null) {
func.setBody(new Block());
} else if (body instanceof Block) {
func.setBody(body);
} else {
func.setBody(addStatement(null, body));
}
return func;
}
use of org.mozilla.javascript.ast.FunctionNode in project HL4A by HL4A.
the class IRFactory method initFunction.
private Node initFunction(FunctionNode fnNode, int functionIndex, Node statements, int functionType) {
fnNode.setFunctionType(functionType);
fnNode.addChildToBack(statements);
int functionCount = fnNode.getFunctionCount();
if (functionCount != 0) {
// Functions containing other functions require activation objects
fnNode.setRequiresActivation();
}
if (functionType == FunctionNode.FUNCTION_EXPRESSION) {
Name name = fnNode.getFunctionName();
if (name != null && name.length() != 0 && fnNode.getSymbol(name.getIdentifier()) == null) {
// A function expression needs to have its name as a
// variable (if it isn't already allocated as a variable).
// See ECMA Ch. 13. We add code to the beginning of the
// function to initialize a local variable of the
// function's name to the function value, but only if the
// function doesn't already define a formal parameter, var,
// or nested function with the same name.
fnNode.putSymbol(new Symbol(Token.FUNCTION, name.getIdentifier()));
Node setFn = new Node(Token.EXPR_VOID, new Node(Token.SETNAME, Node.newString(Token.BINDNAME, name.getIdentifier()), new Node(Token.THISFN)));
statements.addChildrenToFront(setFn);
}
}
// Add return to end if needed.
Node lastStmt = statements.getLastChild();
if (lastStmt == null || lastStmt.getType() != Token.RETURN) {
statements.addChildToBack(new Node(Token.RETURN));
}
Node result = Node.newString(Token.FUNCTION, fnNode.getName());
result.putIntProp(Node.FUNCTION_PROP, functionIndex);
return result;
}
use of org.mozilla.javascript.ast.FunctionNode in project HL4A by HL4A.
the class IRFactory method transformFunction.
private Node transformFunction(FunctionNode fn) {
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 = fn.getBody().getLineno();
// only for body, not params
++nestingOfFunction;
Node body = transform(fn.getBody());
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();
Node 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());
}
}
return pn;
} finally {
--nestingOfFunction;
savedVars.restore();
}
}
use of org.mozilla.javascript.ast.FunctionNode in project HL4A by HL4A.
the class CodeGenerator method generateNestedFunctions.
private void generateNestedFunctions() {
int functionCount = scriptOrFn.getFunctionCount();
if (functionCount == 0)
return;
InterpreterData[] array = new InterpreterData[functionCount];
for (int i = 0; i != functionCount; i++) {
FunctionNode fn = scriptOrFn.getFunctionNode(i);
CodeGenerator gen = new CodeGenerator();
gen.compilerEnv = compilerEnv;
gen.scriptOrFn = fn;
gen.itsData = new InterpreterData(itsData);
gen.generateFunctionICode();
array[i] = gen.itsData;
}
itsData.itsNestedFunctions = array;
}
use of org.mozilla.javascript.ast.FunctionNode in project HL4A by HL4A.
the class CodeGenerator method generateFunctionICode.
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());
}
Aggregations