@abaplint/transpiler
Version:
82 lines • 3.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NewObjectTranspiler = void 0;
const core_1 = require("@abaplint/core");
const abaplint = require("@abaplint/core");
const chunk_1 = require("../chunk");
const type_name_or_infer_1 = require("./type_name_or_infer");
const transpile_types_1 = require("../transpile_types");
const types_1 = require("../types");
class NewObjectTranspiler {
transpile(node, traversal) {
const ret = new chunk_1.Chunk();
const typeNameOrInfer = node.findDirectExpression(core_1.Expressions.TypeNameOrInfer);
if (typeNameOrInfer === undefined) {
throw new Error("NewObjectTranspiler: TypeNameOrInfer not found");
}
let para = "";
const parameters = node.findFirstExpression(abaplint.Expressions.ParameterListS);
if (parameters) {
para = traversal.traverse(parameters).getCode();
}
let type = new type_name_or_infer_1.TypeNameOrInfer().findTypeOrUndefined(typeNameOrInfer, traversal);
if (type === undefined) {
const scope = traversal.findCurrentScopeByToken(node.getFirstToken());
try {
type = traversal.lookupType(typeNameOrInfer, scope);
}
catch {
// ignore
}
}
if (type === undefined) {
if (traversal.options?.unknownTypes === types_1.UnknownTypesEnum.runtimeError) {
ret.appendString(`(function() { throw new Error("Void type: ${typeNameOrInfer.concatTokens().toUpperCase()}") })()`);
return ret;
}
else {
throw new Error("NewObjectTranspiler, type not found: " + node.concatTokens() + ", " + traversal.getCurrentObject().getName() + " line " + node.getFirstToken().getStart().getRow());
}
}
else if (type instanceof abaplint.BasicTypes.ObjectReferenceType) {
if (node.getChildren()[3].get() instanceof abaplint.Expressions.Source) {
// single default parameter
const scope = traversal.findCurrentScopeByToken(typeNameOrInfer.getFirstToken());
const cdef = traversal.findClassDefinition(type.getIdentifierName(), scope);
const constr = this.findConstructor(cdef, scope);
const pname = constr?.getParameters().getDefaultImporting()?.toLowerCase();
para = `{${pname}: ${traversal.traverse(node.getChildren()[3]).getCode()}}`;
}
const clas = traversal.lookupClassOrInterface(type.getIdentifierName(), node.getFirstToken());
ret.appendString(transpile_types_1.TranspileTypes.toType(type) + ".set(await (new " + clas + "()).constructor_(" + para + "))");
}
else {
const source = node.findFirstExpression(abaplint.Expressions.Source);
if (source === undefined) {
throw new Error("NewObjectTranspiler: DataReference source not found");
}
const typeCode = transpile_types_1.TranspileTypes.toType(type);
const sourceCode = traversal.traverse(source).getCode();
ret.appendString("((() => {const r = new abap.types.DataReference(" + typeCode + "); r.assign(" + sourceCode + "); return r; })())");
}
return ret;
}
findConstructor(cdef, spag) {
let def = cdef;
while (def !== undefined) {
const method = def?.getMethodDefinitions().getByName("CONSTRUCTOR");
if (method) {
return method;
}
const name = def.getSuperClass();
if (name) {
def = spag?.findClassDefinition(name);
}
else {
return undefined;
}
}
}
}
exports.NewObjectTranspiler = NewObjectTranspiler;
//# sourceMappingURL=new_object.js.map