JsName gwtOnLoadName = topScope.declareName("gwtOnLoad");
gwtOnLoadName.setObfuscatable(false);
JsFunction gwtOnLoad = new JsFunction(sourceInfo, topScope, gwtOnLoadName, true);
gwtOnLoad.setArtificiallyRescued(true);
globalStmts.add(gwtOnLoad.makeStmt());
JsBlock body = new JsBlock(sourceInfo);
gwtOnLoad.setBody(body);
JsScope fnScope = gwtOnLoad.getScope();
List<JsParameter> params = gwtOnLoad.getParameters();
JsName errFn = fnScope.declareName("errFn");
JsName modName = fnScope.declareName("modName");
JsName modBase = fnScope.declareName("modBase");
JsName softPermutationId = fnScope.declareName("softPermutationId");
params.add(new JsParameter(sourceInfo, errFn));
params.add(new JsParameter(sourceInfo, modName));
params.add(new JsParameter(sourceInfo, modBase));
params.add(new JsParameter(sourceInfo, softPermutationId));
JsExpression asg =
createAssignment(topScope.findExistingUnobfuscatableName("$moduleName").makeRef(
sourceInfo), modName.makeRef(sourceInfo));
body.getStatements().add(asg.makeStmt());
asg =
createAssignment(topScope.findExistingUnobfuscatableName("$moduleBase").makeRef(
sourceInfo), modBase.makeRef(sourceInfo));
body.getStatements().add(asg.makeStmt());
// Assignment to CollapsedPropertyHolder.permutationId only if it's used
JsName permutationIdFieldName =
names.get(program.getIndexedField("CollapsedPropertyHolder.permutationId"));
if (permutationIdFieldName != null) {
asg =
createAssignment(permutationIdFieldName.makeRef(sourceInfo), softPermutationId
.makeRef(sourceInfo));
body.getStatements().add(asg.makeStmt());
}
JsIf jsIf = new JsIf(sourceInfo);
body.getStatements().add(jsIf);
jsIf.setIfExpr(errFn.makeRef(sourceInfo));
JsTry jsTry = new JsTry(sourceInfo);
jsIf.setThenStmt(jsTry);
JsBlock callBlock = new JsBlock(sourceInfo);
jsIf.setElseStmt(callBlock);
jsTry.setTryBlock(callBlock);
for (JsFunction func : entryFuncs) {
if (func == registerEntryFunction) {
continue;
} else if (func != null) {
JsInvocation call = new JsInvocation(sourceInfo);
call.setQualifier(entryName.makeRef(sourceInfo));
call.getArguments().add(func.getName().makeRef(sourceInfo));
JsInvocation entryCall = new JsInvocation(sourceInfo);
entryCall.setQualifier(call);
callBlock.getStatements().add(entryCall.makeStmt());
}
}
JsCatch jsCatch = new JsCatch(sourceInfo, fnScope, "e");
jsTry.getCatches().add(jsCatch);
JsBlock catchBlock = new JsBlock(sourceInfo);
jsCatch.setBody(catchBlock);
JsInvocation errCall = new JsInvocation(sourceInfo);
catchBlock.getStatements().add(errCall.makeStmt());
errCall.setQualifier(errFn.makeRef(sourceInfo));
errCall.getArguments().add(modName.makeRef(sourceInfo));
}