Skip to content

Commit

Permalink
[Lava-JS] Simplifies unwrapJoinPoint (closes Clava issue 172)
Browse files Browse the repository at this point in the history
- Simplification takes into account that Java methods called from JS with array parameters should always be Object[];
  • Loading branch information
joaobispo committed Oct 23, 2024
1 parent 8f04c79 commit 3ff151e
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 69 deletions.
125 changes: 92 additions & 33 deletions Lara-JS/src-api/LaraJoinPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
/* eslint-disable @typescript-eslint/no-duplicate-type-constituents */

import java from "java";
import JavaTypes, { Engine, engine, NodeJavaPrefix } from "./lara/util/JavaTypes.js";
import JavaTypes, {
Engine,
engine,
NodeJavaPrefix,
} from "./lara/util/JavaTypes.js";

/**
* Type for type equality assertion. If T is equal to U, return Y, otherwise return N.
Expand All @@ -21,8 +25,7 @@ import JavaTypes, { Engine, engine, NodeJavaPrefix } from "./lara/util/JavaTypes
* type B = Equals<string, number, "Y", "N">; // "N"
*/
type Equals<T, U, Y = unknown, N = never> =
(<G>() => G extends T ? 1 : 2) extends
(<G>() => G extends U ? 1 : 2) ? Y : N;
(<G>() => G extends T ? 1 : 2) extends <G>() => G extends U ? 1 : 2 ? Y : N;

type DefaultAttributeHelper<
T extends typeof LaraJoinPoint,
Expand All @@ -37,26 +40,33 @@ type DefaultAttributeHelper<
// Extract the type A from A | undefined
type ExtractedType<T> = T extends undefined ? never : T;

export type DefaultAttribute<T extends typeof LaraJoinPoint> = DefaultAttributeHelper<
T,
ExtractedType<T["_defaultAttributeInfo"]["map"]>,
ExtractedType<T["_defaultAttributeInfo"]["type"]>
>;
export type DefaultAttribute<T extends typeof LaraJoinPoint> =
DefaultAttributeHelper<
T,
ExtractedType<T["_defaultAttributeInfo"]["map"]>,
ExtractedType<T["_defaultAttributeInfo"]["type"]>
>;

type NameFromWrapperClassHelper<T extends typeof LaraJoinPoint, U> = {
[K in keyof U]: Equals<T, U[K], K, never>;
}[keyof U];

export type NameFromWrapperClass<T extends typeof LaraJoinPoint> = NameFromWrapperClassHelper<
T,
ExtractedType<T["_defaultAttributeInfo"]["jpMapper"]>
>;
export type NameFromWrapperClass<T extends typeof LaraJoinPoint> =
NameFromWrapperClassHelper<
T,
ExtractedType<T["_defaultAttributeInfo"]["jpMapper"]>
>;

export class LaraJoinPoint {
/**
* @internal
*/
static readonly _defaultAttributeInfo: {readonly map?: any, readonly name: string | null, readonly type?: any, readonly jpMapper?: any} = {
static readonly _defaultAttributeInfo: {
readonly map?: any;
readonly name: string | null;
readonly type?: any;
readonly jpMapper?: any;
} = {
name: null,
};
/**
Expand All @@ -66,29 +76,70 @@ export class LaraJoinPoint {
constructor(obj: any) {
this._javaObject = obj;
}
get attributes(): string[] { return wrapJoinPoint(this._javaObject.getAttributes()) }
get selects(): string[] { return wrapJoinPoint(this._javaObject.getSelects()) }
get actions(): string[] { return wrapJoinPoint(this._javaObject.getActions()) }
get dump(): string { return wrapJoinPoint(this._javaObject.getDump()) }
get joinPointType(): string { return wrapJoinPoint(this._javaObject.getJoinPointType()) }
get node(): object { return (this._javaObject.getNode()) }
get self(): LaraJoinPoint { return wrapJoinPoint(this._javaObject.getSelf()) }
get super(): LaraJoinPoint { return wrapJoinPoint(this._javaObject.getSuper()) }
get children(): LaraJoinPoint[] { return wrapJoinPoint(this._javaObject.getChildren()) }
get descendants(): LaraJoinPoint[] { return wrapJoinPoint(this._javaObject.getDescendants()) }
get scopeNodes(): LaraJoinPoint[] { return wrapJoinPoint(this._javaObject.getScopeNodes()) }
get attributes(): string[] {
return wrapJoinPoint(this._javaObject.getAttributes());
}
get selects(): string[] {
return wrapJoinPoint(this._javaObject.getSelects());
}
get actions(): string[] {
return wrapJoinPoint(this._javaObject.getActions());
}
get dump(): string {
return wrapJoinPoint(this._javaObject.getDump());
}
get joinPointType(): string {
return wrapJoinPoint(this._javaObject.getJoinPointType());
}
get node(): object {
return this._javaObject.getNode();
}
get self(): LaraJoinPoint {
return wrapJoinPoint(this._javaObject.getSelf());
}
get super(): LaraJoinPoint {
return wrapJoinPoint(this._javaObject.getSuper());
}
get children(): LaraJoinPoint[] {
return wrapJoinPoint(this._javaObject.getChildren());
}
get descendants(): LaraJoinPoint[] {
return wrapJoinPoint(this._javaObject.getDescendants());
}
get scopeNodes(): LaraJoinPoint[] {
return wrapJoinPoint(this._javaObject.getScopeNodes());
}
insert(position: "before" | "after" | "replace", code: string): LaraJoinPoint;
insert(position: "before" | "after" | "replace", joinpoint: LaraJoinPoint): LaraJoinPoint;
insert(p1: "before" | "after" | "replace", p2: string | LaraJoinPoint): LaraJoinPoint { return wrapJoinPoint(this._javaObject.insert(unwrapJoinPoint(p1), unwrapJoinPoint(p2))); }
def(attribute: string, value: object): void { return wrapJoinPoint(this._javaObject.def(unwrapJoinPoint(attribute), unwrapJoinPoint(value))); }
toString(): string { return wrapJoinPoint(this._javaObject.toString()); }
equals(jp: LaraJoinPoint): boolean { return wrapJoinPoint(this._javaObject.equals(unwrapJoinPoint(jp))); }
insert(
position: "before" | "after" | "replace",
joinpoint: LaraJoinPoint
): LaraJoinPoint;
insert(
p1: "before" | "after" | "replace",
p2: string | LaraJoinPoint
): LaraJoinPoint {
return wrapJoinPoint(
this._javaObject.insert(unwrapJoinPoint(p1), unwrapJoinPoint(p2))
);
}
def(attribute: string, value: object): void {
return wrapJoinPoint(
this._javaObject.def(unwrapJoinPoint(attribute), unwrapJoinPoint(value))
);
}
toString(): string {
return wrapJoinPoint(this._javaObject.toString());
}
equals(jp: LaraJoinPoint): boolean {
return wrapJoinPoint(this._javaObject.equals(unwrapJoinPoint(jp)));
}
instanceOf(name: string): boolean;
instanceOf(names: string[]): boolean;
instanceOf(p1: string | string[]): boolean { return wrapJoinPoint(this._javaObject.instanceOf(unwrapJoinPoint(p1))); }
instanceOf(p1: string | string[]): boolean {
return wrapJoinPoint(this._javaObject.instanceOf(unwrapJoinPoint(p1)));
}
}


export type JoinpointMapperType = { [key: string]: typeof LaraJoinPoint };

const JoinpointMappers: JoinpointMapperType[] = [];
Expand Down Expand Up @@ -183,11 +234,18 @@ export function unwrapJoinPoint(obj: any): any {
}

if (Array.isArray(obj)) {
// Always convert to Object[]
// Java methods that interface with JavaScript and
// have an array parameter, it must always be Object[]

/*
if (engine == Engine.NodeJS) {
const isJpArray = obj.reduce((prev, curr) => {
return prev && curr instanceof LaraJoinPoint;
}, true);

const getClassName = (jp: LaraJoinPoint) =>
Object.getPrototypeOf(jp._javaObject).constructor.name;
Expand All @@ -205,8 +263,9 @@ export function unwrapJoinPoint(obj: any): any {
return java.newArray(clazz, obj.map(unwrapJoinPoint));
}
}

*/
return obj.map(unwrapJoinPoint);
}

Expand Down
110 changes: 74 additions & 36 deletions LaraApi/src-lara/LaraJoinPoint.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3ff151e

Please sign in to comment.