public void endVisit(JMethodBody x, Context ctx) {
JsBlock body = (JsBlock) pop();
List<JsNameRef> locals = popList(x.getLocals().size()); // locals
JsFunction jsFunc = methodBodyMap.get(x);
jsFunc.setBody(body); // body
/*
* Emit a statement to declare the method's complete set of local
* variables. JavaScript doesn't have the same concept of lexical scoping
* as Java, so it's okay to just predeclare all local vars at the top of
* the function, which saves us having to use the "var" keyword over and
* over.
*
* Note: it's fine to use the same JS ident to represent two different
* Java locals of the same name since they could never conflict with each
* other in Java. We use the alreadySeen set to make sure we don't declare
* the same-named local var twice.
*/
JsVars vars = new JsVars(x.getSourceInfo());
Set<String> alreadySeen = new HashSet<String>();
for (int i = 0; i < locals.size(); ++i) {
JsName name = names.get(x.getLocals().get(i));
String ident = name.getIdent();
if (!alreadySeen.contains(ident)) {
alreadySeen.add(ident);
vars.add(new JsVar(x.getSourceInfo(), name));
}
}
if (!vars.isEmpty()) {
jsFunc.getBody().getStatements().add(0, vars);
}
push(jsFunc);
}