* @return The JS expression for the object to pass in the call.
*/
public JsExpr genObjToPass(CallNode callNode, Deque<Map<String, JsExpr>> localVarTranslations) {
// ------ Generate the expression for the original data to pass ------
JsExpr dataToPass;
if (callNode.isPassingAllData()) {
dataToPass = new JsExpr("opt_data", Integer.MAX_VALUE);
} else if (callNode.isPassingData()) {
dataToPass = jsExprTranslator.translateToJsExpr(
callNode.getDataExpr(), null, localVarTranslations);
} else {
dataToPass = new JsExpr("null", Integer.MAX_VALUE);
}
// ------ Case 1: No additional params ------
if (callNode.numChildren() == 0) {
return dataToPass;
}
// ------ Build an object literal containing the additional params ------
StringBuilder paramsObjSb = new StringBuilder();
paramsObjSb.append('{');
boolean isFirst = true;
for (CallParamNode child : callNode.getChildren()) {
if (isFirst) {
isFirst = false;
} else {
paramsObjSb.append(", ");
}
String key = child.getKey();
paramsObjSb.append(key).append(": ");
if (child instanceof CallParamValueNode) {
CallParamValueNode cpvn = (CallParamValueNode) child;
JsExpr valueJsExpr = jsExprTranslator.translateToJsExpr(
cpvn.getValueExprUnion().getExpr(), cpvn.getValueExprText(), localVarTranslations);
paramsObjSb.append(valueJsExpr.getText());
} else {
CallParamContentNode cpcn = (CallParamContentNode) child;
JsExpr valueJsExpr;
if (isComputableAsJsExprsVisitor.exec(cpcn)) {
valueJsExpr = JsExprUtils.concatJsExprs(
genJsExprsVisitorFactory.create(localVarTranslations).exec(cpcn));
} else {
// This is a param with content that cannot be represented as JS expressions, so we assume
// that code has been generated to define the temporary variable 'param<n>'.
String paramExpr = "param" + cpcn.getId();
if (jsSrcOptions.getCodeStyle() == SoyJsSrcOptions.CodeStyle.STRINGBUILDER) {
paramExpr += ".toString()";
}
valueJsExpr = new JsExpr(paramExpr, Integer.MAX_VALUE);
}
// If the param node had a content kind specified, it was autoescaped in the
// corresponding context. Hence the result of evaluating the param block is wrapped
// in a SanitizedContent instance of the appropriate kind.
// The expression for the constructor of SanitizedContent of the appropriate kind (e.g.,
// "new SanitizedHtml"), or null if the node has no 'kind' attribute.
valueJsExpr = JsExprUtils.maybeWrapAsSanitizedContent(cpcn.getContentKind(), valueJsExpr);
paramsObjSb.append(valueJsExpr.getText());
}
}
paramsObjSb.append('}');
// ------ Cases 2 and 3: Additional params with and without original data to pass ------
if (callNode.isPassingData()) {
return new JsExpr(
"soy.$$augmentMap(" + dataToPass.getText() + ", " + paramsObjSb.toString() + ")",
Integer.MAX_VALUE);
} else {
return new JsExpr(paramsObjSb.toString(), Integer.MAX_VALUE);
}
}