@as-pect/transform 8.0.1 → 9.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -7
- package/lib/ClassReflectionTransform.js +134 -0
- package/lib/appendGeneratedClassReflectionMembers.js +30 -0
- package/lib/astHelpers.js +21 -0
- package/lib/createAddReflectedValueKeyValuePairsMember.js +148 -176
- package/lib/createGenericTypeParameter.js +10 -10
- package/lib/createInheritedIgnoreListExpression.js +11 -0
- package/lib/createInterfaceReflectionMembers.js +25 -0
- package/lib/createStrictEqualsMember.js +143 -187
- package/lib/emptyTransformer.js +8 -8
- package/lib/hash.js +15 -15
- package/lib/index.js +36 -37
- package/lib/src/createAddReflectedValueKeyValuePairsMember.js +176 -0
- package/lib/src/createGenericTypeParameter.js +10 -0
- package/lib/src/createStrictEqualsMember.js +187 -0
- package/lib/src/emptyTransformer.js +8 -0
- package/lib/src/hash.js +15 -0
- package/lib/src/index.js +37 -0
- package/package.json +9 -5
package/README.md
CHANGED
|
@@ -1,11 +1,62 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@as-pect/transform`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
AssemblyScript transform for `as-pect` reflection support.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
This package implements the Class reflection transform. It injects the generated class reflection shape that the runtime helpers in `@as-pect/assembly` depend on when tests compare class instances or print reflected object values.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
const transform = require('transform');
|
|
7
|
+
## Generated shape
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
For each class declaration found in source files and namespaces, the transform appends two public instance methods:
|
|
10
|
+
|
|
11
|
+
- `__aspectStrictEquals(rawRef: Object, stack: usize[], cache: usize[], ignore: StaticArray<i64>): bool`
|
|
12
|
+
- `__aspectAddReflectedValueKeyValuePairs(reflectedValue: i32, seen: Map<usize, i32>, ignore: StaticArray<i64>): void`
|
|
13
|
+
|
|
14
|
+
These method names are compatibility-sensitive. `packages/assembly/assembly/internal/Reflect.ts` calls them directly when it cannot use a built-in comparison or reflected-value path.
|
|
15
|
+
|
|
16
|
+
## ClassReflectionTransform
|
|
17
|
+
|
|
18
|
+
`ClassReflectionTransform` is the module that owns the shared Class-member plan for generated class reflection behavior. It inspects each class once and records the instance members that should participate in both strict equality and reflected key/value generation.
|
|
19
|
+
|
|
20
|
+
The member plan includes, in source order:
|
|
21
|
+
|
|
22
|
+
- instance fields
|
|
23
|
+
- instance getters
|
|
24
|
+
|
|
25
|
+
The member plan excludes:
|
|
26
|
+
|
|
27
|
+
- static members
|
|
28
|
+
- regular instance methods
|
|
29
|
+
- inherited members, which are handled by generated `super` calls
|
|
30
|
+
|
|
31
|
+
Each planned member carries its name, source range, getter/field kind, and `djb2` name hash. Generated methods pass these hashes through `ignore: StaticArray<i64>` when calling `super` so inherited generated methods do not duplicate members overridden by a child class.
|
|
32
|
+
|
|
33
|
+
Hash values intentionally remain the compatibility seam with existing generated code. If two inherited member names ever collide under `djb2`, the generated ignore list treats them as the same inherited member and suppresses the parent entry deterministically; the transform does not rename members or attempt a runtime fallback.
|
|
34
|
+
|
|
35
|
+
## Generation responsibilities
|
|
36
|
+
|
|
37
|
+
Class reflection generation is split across a few narrow responsibilities:
|
|
38
|
+
|
|
39
|
+
- `index.ts` walks parsed sources and namespaces, then delegates class and interface declarations to the generation module.
|
|
40
|
+
- `appendGeneratedClassReflectionMembers.ts` orchestrates which generated members or interface contracts should be appended, including idempotence checks and user-authored collision errors.
|
|
41
|
+
- `ClassReflectionTransform.ts` owns compatibility-sensitive generated member names, generated-member marking, class/interface collision checks, local equality-operator detection, and the Class-member plan.
|
|
42
|
+
- `createStrictEqualsMember.ts`, `createAddReflectedValueKeyValuePairsMember.ts`, and `createInterfaceReflectionMembers.ts` build the generated AssemblyScript AST for each runtime method shape.
|
|
43
|
+
|
|
44
|
+
This keeps traversal, planning, collision policy, and AST construction separate enough that a change to one generated method does not require re-learning every transform rule.
|
|
45
|
+
|
|
46
|
+
## Runtime relationship
|
|
47
|
+
|
|
48
|
+
`@as-pect/assembly` uses the generated methods in two places:
|
|
49
|
+
|
|
50
|
+
1. `Reflect.equals()` calls `__aspectStrictEquals` to compare class instances structurally.
|
|
51
|
+
2. `Reflect.toReflectedValue()` calls `__aspectAddReflectedValueKeyValuePairs` to add reflected object keys and values.
|
|
52
|
+
|
|
53
|
+
The transform and runtime therefore share a seam: the transform owns the generated method shape, and the runtime assumes that shape exists after compilation with `@as-pect/transform`.
|
|
54
|
+
|
|
55
|
+
## Compatibility expectations
|
|
56
|
+
|
|
57
|
+
- Keep both generated method names unchanged unless a coordinated runtime migration is planned.
|
|
58
|
+
- Keep the `ignore` hash behavior stable so inherited and overridden members are not reported twice.
|
|
59
|
+
- Keep generated behavior dependency-free; this package should remain a small local transform.
|
|
60
|
+
- Preserve source order when adding reflected/equality-relevant members so reporter output remains stable.
|
|
61
|
+
- Keep transform generation idempotent for the same parsed source; repeated passes should not append duplicate generated members.
|
|
62
|
+
- Reject user-authored `__aspectStrictEquals` or `__aspectAddReflectedValueKeyValuePairs` members with a clear transform error instead of silently overwriting them.
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { djb2Hash } from "./hash.js";
|
|
2
|
+
/** The generated method runtime reflection calls for structural equality. */
|
|
3
|
+
export const STRICT_EQUALS_MEMBER_NAME = "__aspectStrictEquals";
|
|
4
|
+
/** The generated method runtime reflection calls to enumerate reflected object members. */
|
|
5
|
+
export const ADD_REFLECTED_VALUE_KEY_VALUE_PAIRS_MEMBER_NAME = "__aspectAddReflectedValueKeyValuePairs";
|
|
6
|
+
/** The generated marker inherited by classes that should defer strict equality to @operator("=="). */
|
|
7
|
+
export const HAS_EQ_OPERATOR_MEMBER_NAME = "__aspectHasEqOperator";
|
|
8
|
+
const generatedClassReflectionMembers = new WeakSet();
|
|
9
|
+
/** Mark a method declaration as generated by this transform pass. */
|
|
10
|
+
export function markGeneratedClassReflectionMember(methodDeclaration) {
|
|
11
|
+
generatedClassReflectionMembers.add(methodDeclaration);
|
|
12
|
+
return methodDeclaration;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Decide whether a generated class reflection member can be appended.
|
|
16
|
+
*
|
|
17
|
+
* The same parsed source can be passed through the transform more than once by
|
|
18
|
+
* tests, tooling, or recovery flows. Members that were generated by this module
|
|
19
|
+
* are skipped on later passes; user-authored collisions are rejected so runtime
|
|
20
|
+
* reflection does not silently call a method with an incompatible signature.
|
|
21
|
+
*/
|
|
22
|
+
export function shouldGenerateClassReflectionMember(classDeclaration, memberName) {
|
|
23
|
+
return shouldGenerateReflectionMember(classDeclaration, "class", memberName);
|
|
24
|
+
}
|
|
25
|
+
/** Decide whether a generated interface reflection contract can be appended. */
|
|
26
|
+
export function shouldGenerateInterfaceReflectionMember(interfaceDeclaration, memberName) {
|
|
27
|
+
return shouldGenerateReflectionMember(interfaceDeclaration, "interface", memberName);
|
|
28
|
+
}
|
|
29
|
+
function shouldGenerateReflectionMember(declaration, declarationKind, memberName) {
|
|
30
|
+
const existingMember = findMethod(declaration, memberName);
|
|
31
|
+
if (!existingMember)
|
|
32
|
+
return true;
|
|
33
|
+
if (generatedClassReflectionMembers.has(existingMember))
|
|
34
|
+
return false;
|
|
35
|
+
throw new Error(`Cannot generate ${memberName} for ${declarationKind} ${declaration.name.text} because that member already exists.`);
|
|
36
|
+
}
|
|
37
|
+
function findMethod(declaration, memberName) {
|
|
38
|
+
for (const member of declaration.members) {
|
|
39
|
+
if (member.kind !== 59 /* NodeKind.MethodDeclaration */)
|
|
40
|
+
continue;
|
|
41
|
+
const methodDeclaration = member;
|
|
42
|
+
if (methodDeclaration.name.text === memberName)
|
|
43
|
+
return methodDeclaration;
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
/** The kind of class member included in the class reflection member plan. */
|
|
48
|
+
export var ClassReflectionMemberKind;
|
|
49
|
+
(function (ClassReflectionMemberKind) {
|
|
50
|
+
ClassReflectionMemberKind["Field"] = "field";
|
|
51
|
+
ClassReflectionMemberKind["Getter"] = "getter";
|
|
52
|
+
})(ClassReflectionMemberKind || (ClassReflectionMemberKind = {}));
|
|
53
|
+
/** Return true when the class declares a local AssemblyScript equality operator overload. */
|
|
54
|
+
export function hasLocalEqualsOperator(classDeclaration) {
|
|
55
|
+
for (const member of classDeclaration.members) {
|
|
56
|
+
if (member.kind !== 59 /* NodeKind.MethodDeclaration */)
|
|
57
|
+
continue;
|
|
58
|
+
const methodDeclaration = member;
|
|
59
|
+
if (!methodDeclaration.decorators)
|
|
60
|
+
continue;
|
|
61
|
+
for (const decorator of methodDeclaration.decorators) {
|
|
62
|
+
if (!decorator.args || decorator.args.length === 0)
|
|
63
|
+
continue;
|
|
64
|
+
if (!isOperatorDecoratorName(decorator.name))
|
|
65
|
+
continue;
|
|
66
|
+
const operatorName = decorator.args[0];
|
|
67
|
+
if (!operatorName)
|
|
68
|
+
continue;
|
|
69
|
+
if (operatorName.kind !== 17 /* NodeKind.Literal */)
|
|
70
|
+
continue;
|
|
71
|
+
if (operatorName.literalKind !== 2 /* LiteralKind.String */)
|
|
72
|
+
continue;
|
|
73
|
+
if (operatorName.value === "==")
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
function isOperatorDecoratorName(name) {
|
|
80
|
+
if (name.kind === 7 /* NodeKind.Identifier */)
|
|
81
|
+
return name.text === "operator";
|
|
82
|
+
if (name.kind === 22 /* NodeKind.PropertyAccess */) {
|
|
83
|
+
const propertyAccess = name;
|
|
84
|
+
return propertyAccess.expression.kind === 7 /* NodeKind.Identifier */ && propertyAccess.expression.text === "operator";
|
|
85
|
+
}
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Create the shared member plan for class reflection generation.
|
|
90
|
+
*
|
|
91
|
+
* Instance fields and instance getters are included. Other methods, static
|
|
92
|
+
* members, and inherited members are intentionally left out; generated super
|
|
93
|
+
* calls handle inherited members while passing these hashes as the ignore list.
|
|
94
|
+
*
|
|
95
|
+
* @param classDeclaration - The class declaration to inspect.
|
|
96
|
+
*/
|
|
97
|
+
export function createClassReflectionMemberPlan(classDeclaration) {
|
|
98
|
+
const members = new Array();
|
|
99
|
+
for (const member of classDeclaration.members) {
|
|
100
|
+
if (!member.is(262144 /* CommonFlags.Instance */))
|
|
101
|
+
continue;
|
|
102
|
+
switch (member.kind) {
|
|
103
|
+
case 55 /* NodeKind.FieldDeclaration */: {
|
|
104
|
+
const fieldDeclaration = member;
|
|
105
|
+
const name = fieldDeclaration.name.text;
|
|
106
|
+
members.push({
|
|
107
|
+
name,
|
|
108
|
+
hash: djb2Hash(name),
|
|
109
|
+
range: fieldDeclaration.range,
|
|
110
|
+
kind: ClassReflectionMemberKind.Field,
|
|
111
|
+
});
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
case 59 /* NodeKind.MethodDeclaration */: {
|
|
115
|
+
if (!member.is(2048 /* CommonFlags.Get */))
|
|
116
|
+
break;
|
|
117
|
+
const methodDeclaration = member;
|
|
118
|
+
const name = methodDeclaration.name.text;
|
|
119
|
+
members.push({
|
|
120
|
+
name,
|
|
121
|
+
hash: djb2Hash(name),
|
|
122
|
+
range: methodDeclaration.name.range,
|
|
123
|
+
kind: ClassReflectionMemberKind.Getter,
|
|
124
|
+
});
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
classDeclaration,
|
|
131
|
+
members,
|
|
132
|
+
hashes: members.map((member) => member.hash),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ADD_REFLECTED_VALUE_KEY_VALUE_PAIRS_MEMBER_NAME, HAS_EQ_OPERATOR_MEMBER_NAME, STRICT_EQUALS_MEMBER_NAME, hasLocalEqualsOperator, markGeneratedClassReflectionMember, shouldGenerateClassReflectionMember, shouldGenerateInterfaceReflectionMember, } from "./ClassReflectionTransform.js";
|
|
2
|
+
import { createAddReflectedValueKeyValuePairsMember } from "./createAddReflectedValueKeyValuePairsMember.js";
|
|
3
|
+
import { createInterfaceAddReflectedValueKeyValuePairsMember, createInterfaceStrictEqualsMember, } from "./createInterfaceReflectionMembers.js";
|
|
4
|
+
import { createHasEqualsOperatorMember, createStrictEqualsMember } from "./createStrictEqualsMember.js";
|
|
5
|
+
/** Append all generated reflection members for a class declaration. */
|
|
6
|
+
export function appendGeneratedClassReflectionMembers(classDeclaration) {
|
|
7
|
+
const shouldGenerateStrictEquals = shouldGenerateClassReflectionMember(classDeclaration, STRICT_EQUALS_MEMBER_NAME);
|
|
8
|
+
const shouldGenerateReflectedPairs = shouldGenerateClassReflectionMember(classDeclaration, ADD_REFLECTED_VALUE_KEY_VALUE_PAIRS_MEMBER_NAME);
|
|
9
|
+
const shouldGenerateEqualsMarker = shouldGenerateClassReflectionMember(classDeclaration, HAS_EQ_OPERATOR_MEMBER_NAME);
|
|
10
|
+
if (hasLocalEqualsOperator(classDeclaration) && shouldGenerateEqualsMarker) {
|
|
11
|
+
classDeclaration.members.push(markGeneratedClassReflectionMember(createHasEqualsOperatorMember(classDeclaration)));
|
|
12
|
+
}
|
|
13
|
+
if (shouldGenerateStrictEquals) {
|
|
14
|
+
classDeclaration.members.push(markGeneratedClassReflectionMember(createStrictEqualsMember(classDeclaration)));
|
|
15
|
+
}
|
|
16
|
+
if (shouldGenerateReflectedPairs) {
|
|
17
|
+
classDeclaration.members.push(markGeneratedClassReflectionMember(createAddReflectedValueKeyValuePairsMember(classDeclaration)));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** Append all generated reflection contracts for an interface declaration. */
|
|
21
|
+
export function appendGeneratedInterfaceReflectionMembers(interfaceDeclaration) {
|
|
22
|
+
const shouldGenerateStrictEquals = shouldGenerateInterfaceReflectionMember(interfaceDeclaration, STRICT_EQUALS_MEMBER_NAME);
|
|
23
|
+
const shouldGenerateReflectedPairs = shouldGenerateInterfaceReflectionMember(interfaceDeclaration, ADD_REFLECTED_VALUE_KEY_VALUE_PAIRS_MEMBER_NAME);
|
|
24
|
+
if (shouldGenerateStrictEquals) {
|
|
25
|
+
interfaceDeclaration.members.push(markGeneratedClassReflectionMember(createInterfaceStrictEqualsMember(interfaceDeclaration)));
|
|
26
|
+
}
|
|
27
|
+
if (shouldGenerateReflectedPairs) {
|
|
28
|
+
interfaceDeclaration.members.push(markGeneratedClassReflectionMember(createInterfaceAddReflectedValueKeyValuePairsMember(interfaceDeclaration)));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { TypeNode } from "assemblyscript/dist/assemblyscript.js";
|
|
2
|
+
/** Create a named type without type arguments. */
|
|
3
|
+
export function createSimpleNamedType(name, range) {
|
|
4
|
+
return TypeNode.createNamedType(TypeNode.createSimpleTypeName(name, range), null, false, range);
|
|
5
|
+
}
|
|
6
|
+
/** Create an Array<T> type for generated reflection members. */
|
|
7
|
+
export function createArrayType(elementName, range) {
|
|
8
|
+
return TypeNode.createNamedType(TypeNode.createSimpleTypeName("Array", range), [createSimpleNamedType(elementName, range)], false, range);
|
|
9
|
+
}
|
|
10
|
+
/** Create a StaticArray<T> type for generated reflection members. */
|
|
11
|
+
export function createStaticArrayType(elementName, range) {
|
|
12
|
+
return TypeNode.createNamedType(TypeNode.createSimpleTypeName("StaticArray", range), [createSimpleNamedType(elementName, range)], false, range);
|
|
13
|
+
}
|
|
14
|
+
/** Create a Map<K, V> type for generated reflection members. */
|
|
15
|
+
export function createMapType(keyName, valueName, range) {
|
|
16
|
+
return TypeNode.createNamedType(TypeNode.createSimpleTypeName("Map", range), [createSimpleNamedType(keyName, range), createSimpleNamedType(valueName, range)], false, range);
|
|
17
|
+
}
|
|
18
|
+
/** Create a default parameter with the generated member range. */
|
|
19
|
+
export function createDefaultParameter(name, type, range) {
|
|
20
|
+
return TypeNode.createParameter(0 /* ParameterKind.Default */, TypeNode.createIdentifierExpression(name, range), type, null, range);
|
|
21
|
+
}
|
|
@@ -1,176 +1,148 @@
|
|
|
1
|
-
|
|
2
|
-
import { TypeNode, } from "assemblyscript/dist/assemblyscript.js";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
// const TypeNode = TypeNode;
|
|
6
|
-
// const {
|
|
7
|
-
// BlockStatement,
|
|
8
|
-
// ClassDeclaration,
|
|
9
|
-
// FieldDeclaration,
|
|
10
|
-
// MethodDeclaration,
|
|
11
|
-
// Range,
|
|
12
|
-
// Statement
|
|
13
|
-
// } =
|
|
14
|
-
// type BlockStatement = InstanceType<typeof BlockStatement>;
|
|
15
|
-
// type ClassDeclaration = InstanceType<typeof ClassDeclaration>;
|
|
16
|
-
// type FieldDeclaration = InstanceType<typeof FieldDeclaration>;
|
|
17
|
-
// type MethodDeclaration = InstanceType<typeof MethodDeclaration>;
|
|
18
|
-
// type Range = InstanceType<typeof Range>;
|
|
19
|
-
// type Statement = InstanceType<typeof Statement>;
|
|
20
|
-
/**
|
|
21
|
-
* Create a prototype method called __aspectAddReflectedValueKeyValuePairs on a given
|
|
22
|
-
* ClassDeclaration dynamically.
|
|
23
|
-
*
|
|
24
|
-
* @param {ClassDeclaration} classDeclaration - The target classDeclaration
|
|
25
|
-
*/
|
|
26
|
-
export function createAddReflectedValueKeyValuePairsMember(classDeclaration) {
|
|
27
|
-
const range = classDeclaration.name.range;
|
|
28
|
-
// __aspectAddReflectedValueKeyValuePairs(reflectedValue: i32, seen: Map<usize, i32>, ignore: StaticArray<i64>): void
|
|
29
|
-
return TypeNode.createMethodDeclaration(TypeNode.createIdentifierExpression(
|
|
30
|
-
// reflectedValue: i32
|
|
31
|
-
|
|
32
|
-
// seen: Map<usize, i32>
|
|
33
|
-
|
|
34
|
-
// ignore: i64[]
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
*
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Create a function call to __aspectPushReflectedObjectValue to add a key to a given
|
|
152
|
-
* reflected value.
|
|
153
|
-
*
|
|
154
|
-
* @param {string} name - The name of the property
|
|
155
|
-
* @param {Range} range - The reange for this function call
|
|
156
|
-
*/
|
|
157
|
-
function createPushReflectedObjectValueStatement(name, range) {
|
|
158
|
-
// __aspectPushReflectedObjectValue(reflectedValue, Reflect.toReflectedValue(this.propertyName, seen, ignore.concat([...])));
|
|
159
|
-
return TypeNode.createExpressionStatement(
|
|
160
|
-
// __aspectPushReflectedObjectValue(reflectedValue, Reflect.toReflectedValue(this.propertyName, seen, ignore.concat([...])))
|
|
161
|
-
TypeNode.createCallExpression(
|
|
162
|
-
// __aspectPushReflectedObjectValue
|
|
163
|
-
TypeNode.createIdentifierExpression("__aspectPushReflectedObjectValue", range), null, [
|
|
164
|
-
// reflectedValue
|
|
165
|
-
TypeNode.createIdentifierExpression("reflectedValue", range),
|
|
166
|
-
// Reflect.toReflectedValue(this.propertyName, seen))
|
|
167
|
-
TypeNode.createCallExpression(
|
|
168
|
-
// Reflect.toReflectedValue
|
|
169
|
-
TypeNode.createPropertyAccessExpression(TypeNode.createIdentifierExpression("Reflect", range), TypeNode.createIdentifierExpression("toReflectedValue", range), range), null, [
|
|
170
|
-
//this.propertyName
|
|
171
|
-
TypeNode.createPropertyAccessExpression(TypeNode.createThisExpression(range), TypeNode.createIdentifierExpression(name, range), range),
|
|
172
|
-
// seen
|
|
173
|
-
TypeNode.createIdentifierExpression("seen", range),
|
|
174
|
-
], range),
|
|
175
|
-
], range));
|
|
176
|
-
}
|
|
1
|
+
import { ADD_REFLECTED_VALUE_KEY_VALUE_PAIRS_MEMBER_NAME, createClassReflectionMemberPlan, } from "./ClassReflectionTransform.js";
|
|
2
|
+
import { TypeNode, } from "assemblyscript/dist/assemblyscript.js";
|
|
3
|
+
import { createDefaultParameter, createMapType, createSimpleNamedType, createStaticArrayType } from "./astHelpers.js";
|
|
4
|
+
import { createInheritedIgnoreListExpression } from "./createInheritedIgnoreListExpression.js";
|
|
5
|
+
// const TypeNode = TypeNode;
|
|
6
|
+
// const {
|
|
7
|
+
// BlockStatement,
|
|
8
|
+
// ClassDeclaration,
|
|
9
|
+
// FieldDeclaration,
|
|
10
|
+
// MethodDeclaration,
|
|
11
|
+
// Range,
|
|
12
|
+
// Statement
|
|
13
|
+
// } =
|
|
14
|
+
// type BlockStatement = InstanceType<typeof BlockStatement>;
|
|
15
|
+
// type ClassDeclaration = InstanceType<typeof ClassDeclaration>;
|
|
16
|
+
// type FieldDeclaration = InstanceType<typeof FieldDeclaration>;
|
|
17
|
+
// type MethodDeclaration = InstanceType<typeof MethodDeclaration>;
|
|
18
|
+
// type Range = InstanceType<typeof Range>;
|
|
19
|
+
// type Statement = InstanceType<typeof Statement>;
|
|
20
|
+
/**
|
|
21
|
+
* Create a prototype method called __aspectAddReflectedValueKeyValuePairs on a given
|
|
22
|
+
* ClassDeclaration dynamically.
|
|
23
|
+
*
|
|
24
|
+
* @param {ClassDeclaration} classDeclaration - The target classDeclaration
|
|
25
|
+
*/
|
|
26
|
+
export function createAddReflectedValueKeyValuePairsMember(classDeclaration) {
|
|
27
|
+
const range = classDeclaration.name.range;
|
|
28
|
+
// __aspectAddReflectedValueKeyValuePairs(reflectedValue: i32, seen: Map<usize, i32>, ignore: StaticArray<i64>): void
|
|
29
|
+
return TypeNode.createMethodDeclaration(TypeNode.createIdentifierExpression(ADD_REFLECTED_VALUE_KEY_VALUE_PAIRS_MEMBER_NAME, range), null, 256 /* CommonFlags.Public */ | 262144 /* CommonFlags.Instance */ | (classDeclaration.isGeneric ? 131072 /* CommonFlags.GenericContext */ : 0), null, TypeNode.createFunctionType([
|
|
30
|
+
// reflectedValue: i32
|
|
31
|
+
createDefaultParameter("reflectedValue", createSimpleNamedType("i32", range), range),
|
|
32
|
+
// seen: Map<usize, i32>
|
|
33
|
+
createDefaultParameter("seen", createMapType("usize", "i32", range), range),
|
|
34
|
+
// ignore: i64[]
|
|
35
|
+
createDefaultParameter("ignore", createStaticArrayType("i64", range), range),
|
|
36
|
+
],
|
|
37
|
+
// : void
|
|
38
|
+
createSimpleNamedType("void", range), null, false, range), createAddReflectedValueKeyValuePairsFunctionBody(classDeclaration), range);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Iterate over a given ClassDeclaration and return a block statement that contains the
|
|
42
|
+
* body of a supposed function that reports the key value pairs of a given class.
|
|
43
|
+
*
|
|
44
|
+
* @param {ClassDeclaration} classDeclaration - The class declaration to be reported
|
|
45
|
+
*/
|
|
46
|
+
function createAddReflectedValueKeyValuePairsFunctionBody(classDeclaration) {
|
|
47
|
+
const body = new Array();
|
|
48
|
+
const range = classDeclaration.name.range;
|
|
49
|
+
const memberPlan = createClassReflectionMemberPlan(classDeclaration);
|
|
50
|
+
// Add key/value reflection for each reflected/equality-relevant class member.
|
|
51
|
+
for (const member of memberPlan.members) {
|
|
52
|
+
pushKeyValueIfStatement(body, member.name, member.hash, member.range);
|
|
53
|
+
}
|
|
54
|
+
// call into super first after all the property checks have been added
|
|
55
|
+
body.unshift(createIsDefinedIfStatement(memberPlan.hashes, range));
|
|
56
|
+
return TypeNode.createBlockStatement(body, range);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Create an isDefined() function call with an if statement to prevent calls to
|
|
60
|
+
* super where they should not be made.
|
|
61
|
+
*
|
|
62
|
+
* @param {number[]} nameHashes - The array of property names to ignore in the children
|
|
63
|
+
* @param {Range} range - The reporting range of this statement
|
|
64
|
+
*/
|
|
65
|
+
function createIsDefinedIfStatement(nameHashes, range) {
|
|
66
|
+
// if (isDefined(super.__aspectAddReflectedValueKeyValuePairs))
|
|
67
|
+
// super.__aspectAddReflectedValueKeyValuePairs(reflectedValue, seen, StaticArray.concat(ignore, [...] as StaticArray<i64>))
|
|
68
|
+
return TypeNode.createIfStatement(
|
|
69
|
+
// isDefined(super.__aspectAddReflectedValueKeyValuePairs)
|
|
70
|
+
TypeNode.createCallExpression(TypeNode.createIdentifierExpression("isDefined", range), null, [
|
|
71
|
+
// super.__aspectAddReflectedValueKeyValuePairs
|
|
72
|
+
TypeNode.createPropertyAccessExpression(TypeNode.createSuperExpression(range), TypeNode.createIdentifierExpression(ADD_REFLECTED_VALUE_KEY_VALUE_PAIRS_MEMBER_NAME, range), range),
|
|
73
|
+
], range), TypeNode.createBlockStatement([
|
|
74
|
+
TypeNode.createExpressionStatement(
|
|
75
|
+
// super.__aspectAddReflectedValueKeyValuePairs(reflectedValue, seen, StaticArray.concat(ignore, [...] as StaticArray<i64>))
|
|
76
|
+
TypeNode.createCallExpression(TypeNode.createPropertyAccessExpression(TypeNode.createSuperExpression(range), TypeNode.createIdentifierExpression(ADD_REFLECTED_VALUE_KEY_VALUE_PAIRS_MEMBER_NAME, range), range), null, [
|
|
77
|
+
// reflectedValue,
|
|
78
|
+
TypeNode.createIdentifierExpression("reflectedValue", range),
|
|
79
|
+
// seen,
|
|
80
|
+
TypeNode.createIdentifierExpression("seen", range),
|
|
81
|
+
// StaticArray.concat(ignore, [...])
|
|
82
|
+
createInheritedIgnoreListExpression(nameHashes, range),
|
|
83
|
+
], range)),
|
|
84
|
+
], range), null, range);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* For each key-value pair, we need to perform a runtime check to make sure that this property
|
|
88
|
+
* was not overridden in the parent of a given class.
|
|
89
|
+
*
|
|
90
|
+
* @param {Statement[]} body - The collection of statements for the function body
|
|
91
|
+
* @param {string} name - The name of the property
|
|
92
|
+
* @param {Range} range - The range for these statements
|
|
93
|
+
*/
|
|
94
|
+
function pushKeyValueIfStatement(body, name, hashValue, range) {
|
|
95
|
+
body.push(
|
|
96
|
+
// if (!ignore.includes("propName")) { ... }
|
|
97
|
+
TypeNode.createIfStatement(TypeNode.createUnaryPrefixExpression(95 /* Token.Exclamation */,
|
|
98
|
+
// ignore.includes("propName")
|
|
99
|
+
TypeNode.createCallExpression(TypeNode.createPropertyAccessExpression(TypeNode.createIdentifierExpression("ignore", range), TypeNode.createIdentifierExpression("includes", range), range), null, [
|
|
100
|
+
// hashValue
|
|
101
|
+
TypeNode.createIntegerLiteralExpression(f64_as_i64(hashValue), range),
|
|
102
|
+
], range), range), TypeNode.createBlockStatement([createPushReflectedObjectKeyStatement(name, range), createPushReflectedObjectValueStatement(name, range)], range), null, range));
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Create a function call to __aspectPushReflectedObjectKey to add a key to a given
|
|
106
|
+
* reflected value.
|
|
107
|
+
*
|
|
108
|
+
* @param {string} name - The name of the property
|
|
109
|
+
* @param {Range} range - The reange for this function call
|
|
110
|
+
*/
|
|
111
|
+
function createPushReflectedObjectKeyStatement(name, range) {
|
|
112
|
+
// __aspectPushReflectedObjectKey(reflectedValue, Reflect.toReflectedValue("propertyName", seen));
|
|
113
|
+
return TypeNode.createExpressionStatement(TypeNode.createCallExpression(TypeNode.createIdentifierExpression("__aspectPushReflectedObjectKey", range), null, [
|
|
114
|
+
// reflectedValue
|
|
115
|
+
TypeNode.createIdentifierExpression("reflectedValue", range),
|
|
116
|
+
// Reflect.toReflectedValue("propertyName", seen)
|
|
117
|
+
TypeNode.createCallExpression(
|
|
118
|
+
// Reflect.toReflectedValue
|
|
119
|
+
TypeNode.createPropertyAccessExpression(TypeNode.createIdentifierExpression("Reflect", range), TypeNode.createIdentifierExpression("toReflectedValue", range), range), null, [TypeNode.createStringLiteralExpression(name, range), TypeNode.createIdentifierExpression("seen", range)], range),
|
|
120
|
+
], range));
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Create a function call to __aspectPushReflectedObjectValue to add a key to a given
|
|
124
|
+
* reflected value.
|
|
125
|
+
*
|
|
126
|
+
* @param {string} name - The name of the property
|
|
127
|
+
* @param {Range} range - The reange for this function call
|
|
128
|
+
*/
|
|
129
|
+
function createPushReflectedObjectValueStatement(name, range) {
|
|
130
|
+
// __aspectPushReflectedObjectValue(reflectedValue, Reflect.toReflectedValue(this.propertyName, seen, ignore.concat([...])));
|
|
131
|
+
return TypeNode.createExpressionStatement(
|
|
132
|
+
// __aspectPushReflectedObjectValue(reflectedValue, Reflect.toReflectedValue(this.propertyName, seen, ignore.concat([...])))
|
|
133
|
+
TypeNode.createCallExpression(
|
|
134
|
+
// __aspectPushReflectedObjectValue
|
|
135
|
+
TypeNode.createIdentifierExpression("__aspectPushReflectedObjectValue", range), null, [
|
|
136
|
+
// reflectedValue
|
|
137
|
+
TypeNode.createIdentifierExpression("reflectedValue", range),
|
|
138
|
+
// Reflect.toReflectedValue(this.propertyName, seen))
|
|
139
|
+
TypeNode.createCallExpression(
|
|
140
|
+
// Reflect.toReflectedValue
|
|
141
|
+
TypeNode.createPropertyAccessExpression(TypeNode.createIdentifierExpression("Reflect", range), TypeNode.createIdentifierExpression("toReflectedValue", range), range), null, [
|
|
142
|
+
//this.propertyName
|
|
143
|
+
TypeNode.createPropertyAccessExpression(TypeNode.createThisExpression(range), TypeNode.createIdentifierExpression(name, range), range),
|
|
144
|
+
// seen
|
|
145
|
+
TypeNode.createIdentifierExpression("seen", range),
|
|
146
|
+
], range),
|
|
147
|
+
], range));
|
|
148
|
+
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { TypeNode } from "assemblyscript/dist/assemblyscript.js";
|
|
2
|
-
/**
|
|
3
|
-
* This method makes a generic named parameter.
|
|
4
|
-
*
|
|
5
|
-
* @param {string} name - The name of the type.
|
|
6
|
-
* @param {Range} range - The range given for the type parameter.
|
|
7
|
-
*/
|
|
8
|
-
export function createGenericTypeParameter(name, range) {
|
|
9
|
-
return TypeNode.createNamedType(TypeNode.createSimpleTypeName(name, range), null, false, range);
|
|
10
|
-
}
|
|
1
|
+
import { TypeNode } from "assemblyscript/dist/assemblyscript.js";
|
|
2
|
+
/**
|
|
3
|
+
* This method makes a generic named parameter.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} name - The name of the type.
|
|
6
|
+
* @param {Range} range - The range given for the type parameter.
|
|
7
|
+
*/
|
|
8
|
+
export function createGenericTypeParameter(name, range) {
|
|
9
|
+
return TypeNode.createNamedType(TypeNode.createSimpleTypeName(name, range), null, false, range);
|
|
10
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { TypeNode } from "assemblyscript/dist/assemblyscript.js";
|
|
2
|
+
import { createStaticArrayType } from "./astHelpers.js";
|
|
3
|
+
/**
|
|
4
|
+
* Create `StaticArray.concat(ignore, [...hashes] as StaticArray<i64>)` for inherited member suppression.
|
|
5
|
+
*/
|
|
6
|
+
export function createInheritedIgnoreListExpression(nameHashes, range) {
|
|
7
|
+
return TypeNode.createCallExpression(TypeNode.createPropertyAccessExpression(TypeNode.createIdentifierExpression("StaticArray", range), TypeNode.createIdentifierExpression("concat", range), range), null, [
|
|
8
|
+
TypeNode.createIdentifierExpression("ignore", range),
|
|
9
|
+
TypeNode.createAssertionExpression(1 /* AssertionKind.As */, TypeNode.createArrayLiteralExpression(nameHashes.map((hash) => TypeNode.createIntegerLiteralExpression(f64_as_i64(hash), range)), range), createStaticArrayType("i64", range), range),
|
|
10
|
+
], range);
|
|
11
|
+
}
|