@specs-feup/lara
Version:
A js port of the popular framework for building source-to-source compilers
176 lines • 5.17 kB
JavaScript
//////////////////////////////////////////////////////
// This file is generated by build-LaraJoinPoint.js //
//////////////////////////////////////////////////////
import JavaTypes from "./lara/util/JavaTypes.js";
export class LaraJoinPoint {
/**
* @internal
*/
static _defaultAttributeInfo = {
name: null,
};
/**
* @internal
*/
_javaObject;
constructor(obj) {
this._javaObject = obj;
}
get attributes() {
return wrapJoinPoint(this._javaObject.getAttributes());
}
get selects() {
return wrapJoinPoint(this._javaObject.getSelects());
}
get actions() {
return wrapJoinPoint(this._javaObject.getActions());
}
get dump() {
return wrapJoinPoint(this._javaObject.getDump());
}
get joinPointType() {
return wrapJoinPoint(this._javaObject.getJoinPointType());
}
get node() {
return this._javaObject.getNode();
}
get self() {
return wrapJoinPoint(this._javaObject.getSelf());
}
get super() {
return wrapJoinPoint(this._javaObject.getSuper());
}
get children() {
return wrapJoinPoint(this._javaObject.getChildren());
}
get descendants() {
return wrapJoinPoint(this._javaObject.getDescendants());
}
get scopeNodes() {
return wrapJoinPoint(this._javaObject.getScopeNodes());
}
insert(p1, p2) {
return wrapJoinPoint(this._javaObject.insert(unwrapJoinPoint(p1), unwrapJoinPoint(p2)));
}
def(attribute, value) {
return wrapJoinPoint(this._javaObject.def(unwrapJoinPoint(attribute), unwrapJoinPoint(value)));
}
toString() {
return wrapJoinPoint(this._javaObject.toString());
}
equals(jp) {
return wrapJoinPoint(this._javaObject.equals(unwrapJoinPoint(jp)));
}
instanceOf(p1) {
return wrapJoinPoint(this._javaObject.instanceOf(unwrapJoinPoint(p1)));
}
}
const JoinpointMappers = [];
/**
* This function is for internal use only. DO NOT USE IT!
*/
export function clearJoinpointMappers() {
JoinpointMappers.length = 0;
}
/**
* This function is for internal use only. DO NOT USE IT!
*/
export function getJoinpointMappers() {
return JoinpointMappers;
}
export function registerJoinpointMapper(mapper) {
// Create mapper from object
const jpMapper = {
toJpClass(jpTypename) {
return mapper[jpTypename];
},
toJpInstance(jpTypename, javaJp) {
const jpClass = this.toJpClass(jpTypename);
if (jpClass) {
return new jpClass(javaJp);
}
return undefined;
},
fromJpClass(jpType) {
return Object.keys(mapper).find((key) => mapper[key] === jpType);
},
};
JoinpointMappers.push(jpMapper);
}
export function registerJoinpointMapperFunction(mapper) {
// Create mapper from function
const jpMapper = {
toJpClass(jpTypename) {
return mapper(jpTypename);
},
toJpInstance(jpTypename, javaJp) {
const jpClass = this.toJpClass(jpTypename);
if (jpClass) {
return new jpClass(javaJp);
}
return undefined;
},
// Not possible to implement with just a function
fromJpClass(jpType) {
return undefined;
},
};
JoinpointMappers.push(jpMapper);
}
export function wrapJoinPoint(obj) {
if (JoinpointMappers.length === 0) {
return obj;
}
if (obj === undefined) {
return obj;
}
if (obj instanceof LaraJoinPoint) {
return obj;
}
if (ArrayBuffer.isView(obj)) {
return Array.from(obj).map(wrapJoinPoint);
}
if (typeof obj !== "object") {
return obj;
}
if (Array.isArray(obj)) {
return obj.map(wrapJoinPoint);
}
if (!JavaTypes.isJavaObject(obj)) {
return obj;
}
if (JavaTypes.instanceOf(obj, "pt.up.fe.specs.jsengine.node.UndefinedValue")) {
return undefined;
}
if (JavaTypes.instanceOf(obj, "org.suikasoft.jOptions.DataStore.DataClass") &&
!JavaTypes.instanceOf(obj, "pt.up.fe.specs.clava.ClavaNode")) {
return obj;
}
if (obj.getClass().isEnum()) {
return obj.toString();
}
const isJavaJoinPoint = JavaTypes.JoinPoint.isJoinPoint(obj);
if (!isJavaJoinPoint) {
throw new Error(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`Given Java join point is a Java class but is not a JoinPoint: ${obj.getClass()}`);
}
const jpType = obj.getJoinPointType();
for (const mapper of JoinpointMappers) {
const laraJp = mapper.toJpInstance(jpType, obj);
if (laraJp) {
return laraJp;
}
}
throw new Error("No mapper found for join point type: " + jpType);
}
export function unwrapJoinPoint(obj) {
if (obj instanceof LaraJoinPoint) {
return obj._javaObject;
}
if (Array.isArray(obj)) {
return obj.map(unwrapJoinPoint);
}
return obj;
}
//# sourceMappingURL=LaraJoinPoint.js.map