@abaplint/transpiler
Version:
60 lines • 3.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CallFunctionTranspiler = void 0;
const abaplint = require("@abaplint/core");
const chunk_1 = require("../chunk");
const expressions_1 = require("../expressions");
const call_1 = require("./call");
class CallFunctionTranspiler {
transpile(node, traversal) {
const fmchild = node.findDirectExpression(abaplint.Expressions.FunctionName)?.getFirstChild();
if (fmchild === undefined) {
throw new Error("CallFunctionTranspilerNameNotFound");
}
let fmname = "";
if (fmchild instanceof abaplint.Nodes.ExpressionNode
&& fmchild.get() instanceof abaplint.Expressions.FieldChain) {
fmname = new expressions_1.FieldChainTranspiler(true).transpile(fmchild, traversal).getCode();
fmname = fmname + ".trimEnd()";
}
else {
fmname = fmchild.concatTokens().toUpperCase();
}
let param = "";
const fmp = node.findDirectExpression(abaplint.Expressions.FunctionParameters);
if (fmp) {
param = traversal.traverse(fmp).getCode();
}
const ret = new chunk_1.Chunk();
const exceptions = node.findFirstExpression(abaplint.Expressions.ParameterListExceptions);
if (exceptions) {
ret.appendString("try {\n");
}
const calling = node.findExpressionAfterToken("CALLING");
const dest = node.findDirectExpression(abaplint.Expressions.Destination)?.findDirectExpression(abaplint.Expressions.Source);
if (dest) {
const s = new expressions_1.SourceTranspiler(true).transpile(dest, traversal);
param = param.replace("{", ",").replace(/}$/, "");
ret.appendString(`await abap.statements.callFunction({name:${fmname},destination:${s.getCode()}${param}});`);
}
else if (calling) {
param = param.replace("{", ",").replace(/}$/, "");
// typically used in combination with STARTING NEW TASK so dont await,
ret.appendString(`abap.statements.callFunction({name:${fmname},calling:this.${calling.concatTokens().toLowerCase()}${param}});`);
}
else {
const illegalFunc = traversal.lookupClassOrInterface("'CX_SY_DYN_CALL_ILLEGAL_FUNC'", node.getFirstToken(), true);
const call = `abap.FunctionModules[${fmname}]`;
// eslint-disable-next-line max-len
ret.appendString(`if (${call} === undefined) { if (${illegalFunc} === undefined) { throw "CX_SY_DYN_CALL_ILLEGAL_FUNC not found"; } else { throw await new ${illegalFunc}().constructor_({function: new abap.types.String().set(${fmname})});} }\n`);
ret.appendString(`await ${call}(${param});`);
}
if (exceptions) {
const build = call_1.CallTranspiler.buildExceptions(exceptions);
ret.appendString(build.post);
}
return ret;
}
}
exports.CallFunctionTranspiler = CallFunctionTranspiler;
//# sourceMappingURL=call_function.js.map