@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
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { TypeNode } from "assemblyscript/dist/assemblyscript.js";
|
|
2
|
+
import { ADD_REFLECTED_VALUE_KEY_VALUE_PAIRS_MEMBER_NAME, STRICT_EQUALS_MEMBER_NAME, } from "./ClassReflectionTransform.js";
|
|
3
|
+
import { createArrayType, createDefaultParameter, createMapType, createSimpleNamedType, createStaticArrayType, } from "./astHelpers.js";
|
|
4
|
+
/** Create the interface contract consumed by Reflect.equals for structural equality. */
|
|
5
|
+
export function createInterfaceStrictEqualsMember(interfaceDeclaration) {
|
|
6
|
+
const range = interfaceDeclaration.name.range;
|
|
7
|
+
return TypeNode.createMethodDeclaration(TypeNode.createIdentifierExpression(STRICT_EQUALS_MEMBER_NAME, range), null, createInterfaceMemberFlags(interfaceDeclaration), null, TypeNode.createFunctionType([
|
|
8
|
+
createDefaultParameter("rawRef", createSimpleNamedType("Object", range), range),
|
|
9
|
+
createDefaultParameter("stack", createArrayType("usize", range), range),
|
|
10
|
+
createDefaultParameter("cache", createArrayType("usize", range), range),
|
|
11
|
+
createDefaultParameter("ignore", createStaticArrayType("i64", range), range),
|
|
12
|
+
], createSimpleNamedType("bool", range), null, false, range), null, range);
|
|
13
|
+
}
|
|
14
|
+
/** Create the interface contract consumed by Reflect.toReflectedValue for member reporting. */
|
|
15
|
+
export function createInterfaceAddReflectedValueKeyValuePairsMember(interfaceDeclaration) {
|
|
16
|
+
const range = interfaceDeclaration.name.range;
|
|
17
|
+
return TypeNode.createMethodDeclaration(TypeNode.createIdentifierExpression(ADD_REFLECTED_VALUE_KEY_VALUE_PAIRS_MEMBER_NAME, range), null, createInterfaceMemberFlags(interfaceDeclaration), null, TypeNode.createFunctionType([
|
|
18
|
+
createDefaultParameter("reflectedValue", createSimpleNamedType("i32", range), range),
|
|
19
|
+
createDefaultParameter("seen", createMapType("usize", "i32", range), range),
|
|
20
|
+
createDefaultParameter("ignore", createStaticArrayType("i64", range), range),
|
|
21
|
+
], createSimpleNamedType("void", range), null, false, range), null, range);
|
|
22
|
+
}
|
|
23
|
+
function createInterfaceMemberFlags(interfaceDeclaration) {
|
|
24
|
+
return 256 /* CommonFlags.Public */ | 262144 /* CommonFlags.Instance */ | (interfaceDeclaration.isGeneric ? 131072 /* CommonFlags.GenericContext */ : 0);
|
|
25
|
+
}
|
|
@@ -1,187 +1,143 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { TypeNode, } from "assemblyscript/dist/assemblyscript.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
//
|
|
16
|
-
createDefaultParameter("
|
|
17
|
-
//
|
|
18
|
-
createDefaultParameter("
|
|
19
|
-
//
|
|
20
|
-
createDefaultParameter("
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
-
TypeNode.
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
*
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
*
|
|
145
|
-
* @param {string} root - The name of the identifier representing the root.
|
|
146
|
-
* @param {string} property - The name of the identifier representing the property.
|
|
147
|
-
* @param {Range} range - The range of the property access.
|
|
148
|
-
*/
|
|
149
|
-
function createPropertyAccess(root, property, range) {
|
|
150
|
-
// root.property
|
|
151
|
-
return TypeNode.createPropertyAccessExpression(TypeNode.createIdentifierExpression(root, range), TypeNode.createIdentifierExpression(property, range), range);
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* This method creates the function call into super.__aspectStrictEquals,
|
|
155
|
-
* wrapping it in a check to make sure the super function is defined first.
|
|
156
|
-
*
|
|
157
|
-
* @param {ClassDeclaration} classDeclaration - The given class declaration.
|
|
158
|
-
* @param {number[]} nameHashes - A collection of hash values of the comparing class properties.
|
|
159
|
-
*/
|
|
160
|
-
function createSuperCallStatement(classDeclaration, nameHashes) {
|
|
161
|
-
const range = classDeclaration.name.range;
|
|
162
|
-
const ifStatement = TypeNode.createIfStatement(TypeNode.createCallExpression(TypeNode.createIdentifierExpression("isDefined", range), null, [
|
|
163
|
-
TypeNode.createPropertyAccessExpression(TypeNode.createSuperExpression(range), TypeNode.createIdentifierExpression("__aspectStrictEquals", range), range),
|
|
164
|
-
], range), TypeNode.createBlockStatement([
|
|
165
|
-
TypeNode.createIfStatement(TypeNode.createUnaryPrefixExpression(95 /* Token.Exclamation */, createSuperCallExpression(nameHashes, range), range), TypeNode.createReturnStatement(TypeNode.createFalseExpression(range), range), null, range),
|
|
166
|
-
], range), null, range);
|
|
167
|
-
return ifStatement;
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* This method actually creates the super.__aspectStrictEquals function call.
|
|
171
|
-
*
|
|
172
|
-
* @param {number[]} hashValues - The collection of hashed property name values
|
|
173
|
-
* @param {Range} range - The super call expression range
|
|
174
|
-
*/
|
|
175
|
-
function createSuperCallExpression(hashValues, range) {
|
|
176
|
-
return TypeNode.createCallExpression(TypeNode.createPropertyAccessExpression(TypeNode.createSuperExpression(range), TypeNode.createIdentifierExpression("__aspectStrictEquals", range), range), null, [
|
|
177
|
-
TypeNode.createIdentifierExpression("ref", range),
|
|
178
|
-
TypeNode.createIdentifierExpression("stack", range),
|
|
179
|
-
TypeNode.createIdentifierExpression("cache", range),
|
|
180
|
-
// StaticArray.concat(ignore, [... props] as StaticArray<i64>)
|
|
181
|
-
TypeNode.createCallExpression(TypeNode.createPropertyAccessExpression(TypeNode.createIdentifierExpression("StaticArray", range), TypeNode.createIdentifierExpression("concat", range), range), null, [
|
|
182
|
-
TypeNode.createIdentifierExpression("ignore", range),
|
|
183
|
-
// [...] as StaticArray<i64>
|
|
184
|
-
TypeNode.createAssertionExpression(1 /* AssertionKind.As */, TypeNode.createArrayLiteralExpression(hashValues.map((e) => TypeNode.createIntegerLiteralExpression(f64_as_i64(e), range)), range), TypeNode.createNamedType(TypeNode.createSimpleTypeName("StaticArray", range), [TypeNode.createNamedType(TypeNode.createSimpleTypeName("i64", range), null, false, range)], false, range), range),
|
|
185
|
-
], range),
|
|
186
|
-
], range);
|
|
187
|
-
}
|
|
1
|
+
import { HAS_EQ_OPERATOR_MEMBER_NAME, STRICT_EQUALS_MEMBER_NAME, createClassReflectionMemberPlan, } from "./ClassReflectionTransform.js";
|
|
2
|
+
import { TypeNode, } from "assemblyscript/dist/assemblyscript.js";
|
|
3
|
+
import { createArrayType, createDefaultParameter, createSimpleNamedType, createStaticArrayType } from "./astHelpers.js";
|
|
4
|
+
import { createInheritedIgnoreListExpression } from "./createInheritedIgnoreListExpression.js";
|
|
5
|
+
/**
|
|
6
|
+
* This method creates a single FunctionDeclaration that allows Reflect.equals
|
|
7
|
+
* to validate normal class member values.
|
|
8
|
+
*
|
|
9
|
+
* @param {ClassDeclaration} classDeclaration - The class that requires a new function.
|
|
10
|
+
*/
|
|
11
|
+
export function createStrictEqualsMember(classDeclaration) {
|
|
12
|
+
const range = classDeclaration.name.range;
|
|
13
|
+
// __aspectStrictEquals(rawRef: Object, stackA: usize[], stackB: usize[], ignore: StaticArray<i64>): bool
|
|
14
|
+
return TypeNode.createMethodDeclaration(TypeNode.createIdentifierExpression(STRICT_EQUALS_MEMBER_NAME, range), null, 256 /* CommonFlags.Public */ | 262144 /* CommonFlags.Instance */ | (classDeclaration.isGeneric ? 131072 /* CommonFlags.GenericContext */ : 0), null, TypeNode.createFunctionType([
|
|
15
|
+
// rawRef: Object,
|
|
16
|
+
createDefaultParameter("rawRef", createSimpleNamedType("Object", range), range),
|
|
17
|
+
// stack: usize[]
|
|
18
|
+
createDefaultParameter("stack", createArrayType("usize", range), range),
|
|
19
|
+
// cache: usize[]
|
|
20
|
+
createDefaultParameter("cache", createArrayType("usize", range), range),
|
|
21
|
+
// ignore: StaticArray<i64>
|
|
22
|
+
createDefaultParameter("ignore", createStaticArrayType("i64", range), range),
|
|
23
|
+
],
|
|
24
|
+
// : bool
|
|
25
|
+
createSimpleNamedType("bool", range), null, false, range), createStrictEqualsFunctionBody(classDeclaration), range);
|
|
26
|
+
}
|
|
27
|
+
/** Create the inherited marker used to tell strict equality to defer to @operator("=="). */
|
|
28
|
+
export function createHasEqualsOperatorMember(classDeclaration) {
|
|
29
|
+
const range = classDeclaration.name.range;
|
|
30
|
+
// protected __aspectHasEqOperator(): bool { return true; }
|
|
31
|
+
return TypeNode.createMethodDeclaration(TypeNode.createIdentifierExpression(HAS_EQ_OPERATOR_MEMBER_NAME, range), null, 1024 /* CommonFlags.Protected */ | 262144 /* CommonFlags.Instance */ | (classDeclaration.isGeneric ? 131072 /* CommonFlags.GenericContext */ : 0), null, TypeNode.createFunctionType([], createSimpleNamedType("bool", range), null, false, range), TypeNode.createBlockStatement([TypeNode.createReturnStatement(TypeNode.createTrueExpression(range), range)], range), range);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* This method creates the entire function body for __aspectStrictEquals.
|
|
35
|
+
*
|
|
36
|
+
* @param {ClassDeclaration} classDeclaration - The class declaration.
|
|
37
|
+
*/
|
|
38
|
+
function createStrictEqualsFunctionBody(classDeclaration) {
|
|
39
|
+
const body = new Array();
|
|
40
|
+
const range = classDeclaration.name.range;
|
|
41
|
+
const memberPlan = createClassReflectionMemberPlan(classDeclaration);
|
|
42
|
+
const rawRef = TypeNode.createIdentifierExpression("rawRef", range);
|
|
43
|
+
const classType = TypeNode.createNamedType(TypeNode.createSimpleTypeName(classDeclaration.name.text, range), classDeclaration.isGeneric
|
|
44
|
+
? classDeclaration.typeParameters.map((node) => TypeNode.createNamedType(TypeNode.createSimpleTypeName(node.name.text, range), null, false, range))
|
|
45
|
+
: null, false, range);
|
|
46
|
+
// Check if the parameter is an instance of the class; return otherwise
|
|
47
|
+
body.push(TypeNode.createIfStatement(TypeNode.createUnaryPrefixExpression(95 /* Token.Exclamation */, TypeNode.createInstanceOfExpression(rawRef, classType, range), range), TypeNode.createReturnStatement(TypeNode.createFalseExpression(range), range), null, range));
|
|
48
|
+
// Cast rawRef into an instance of the class
|
|
49
|
+
body.push(TypeNode.createVariableStatement(null, [
|
|
50
|
+
TypeNode.createVariableDeclaration(TypeNode.createIdentifierExpression("ref", range), null, 8 /* CommonFlags.Const */, classType, TypeNode.createAssertionExpression(1 /* AssertionKind.As */, rawRef, classType, range), range),
|
|
51
|
+
], range));
|
|
52
|
+
// If this class, or a parent class, defines @operator("=="), strict equality uses that operator as authoritative.
|
|
53
|
+
body.push(createEqualsOperatorDelegationStatement(range));
|
|
54
|
+
// Generate a check for each reflected/equality-relevant class member.
|
|
55
|
+
for (const member of memberPlan.members) {
|
|
56
|
+
body.push(createStrictEqualsIfCheck(member.name, member.hash, member.range));
|
|
57
|
+
}
|
|
58
|
+
// if (isDefined(...)) super.__aspectStrictEquals(ref, stack, cache, ignore.concat([...props]));
|
|
59
|
+
body.push(createSuperCallStatement(classDeclaration, memberPlan.hashes));
|
|
60
|
+
// return true;
|
|
61
|
+
body.push(TypeNode.createReturnStatement(TypeNode.createTrueExpression(range), range));
|
|
62
|
+
return TypeNode.createBlockStatement(body, range);
|
|
63
|
+
}
|
|
64
|
+
/** Create the optional fast path that lets user-defined equality decide strict equality. */
|
|
65
|
+
function createEqualsOperatorDelegationStatement(range) {
|
|
66
|
+
return TypeNode.createIfStatement(TypeNode.createCallExpression(TypeNode.createIdentifierExpression("isDefined", range), null, [
|
|
67
|
+
TypeNode.createPropertyAccessExpression(TypeNode.createThisExpression(range), TypeNode.createIdentifierExpression(HAS_EQ_OPERATOR_MEMBER_NAME, range), range),
|
|
68
|
+
], range), TypeNode.createReturnStatement(TypeNode.createBinaryExpression(76 /* Token.Equals_Equals */, TypeNode.createThisExpression(range), TypeNode.createIdentifierExpression("ref", range), range), range), null, range);
|
|
69
|
+
}
|
|
70
|
+
function createStrictEqualsIfCheck(name, hashValue, range) {
|
|
71
|
+
const equalsCheck = TypeNode.createBinaryExpression(76 /* Token.Equals_Equals */,
|
|
72
|
+
// Reflect.equals(this.prop, ref.prop, stack, cache)
|
|
73
|
+
TypeNode.createCallExpression(
|
|
74
|
+
// Reflect.equals
|
|
75
|
+
createPropertyAccess("Reflect", "equals", range), null, // types can be inferred by the compiler!
|
|
76
|
+
// arguments
|
|
77
|
+
[
|
|
78
|
+
// this.prop
|
|
79
|
+
TypeNode.createPropertyAccessExpression(TypeNode.createThisExpression(range), TypeNode.createIdentifierExpression(name, range), range),
|
|
80
|
+
// ref.prop
|
|
81
|
+
createPropertyAccess("ref", name, range),
|
|
82
|
+
// stack
|
|
83
|
+
TypeNode.createIdentifierExpression("stack", range),
|
|
84
|
+
// cache
|
|
85
|
+
TypeNode.createIdentifierExpression("cache", range),
|
|
86
|
+
], range), createPropertyAccess("Reflect", "FAILED_MATCH", range), range);
|
|
87
|
+
// !ignore.includes("prop")
|
|
88
|
+
const includesCheck = TypeNode.createUnaryPrefixExpression(95 /* Token.Exclamation */,
|
|
89
|
+
// ignore.includes("prop")
|
|
90
|
+
TypeNode.createCallExpression(
|
|
91
|
+
// ignore.includes
|
|
92
|
+
TypeNode.createPropertyAccessExpression(TypeNode.createIdentifierExpression("ignore", range), TypeNode.createIdentifierExpression("includes", range), range), null,
|
|
93
|
+
// (nameHash)
|
|
94
|
+
[TypeNode.createIntegerLiteralExpression(f64_as_i64(hashValue), range)], range), range);
|
|
95
|
+
// if (Reflect.equals(this.prop, ref.prop, stack, cache) === Reflect.FAILED_MATCH) return false;
|
|
96
|
+
return TypeNode.createIfStatement(
|
|
97
|
+
// Reflect.equals(this.prop, ref.prop, stack, cache) === Reflect.FAILED_MATCH
|
|
98
|
+
TypeNode.createBinaryExpression(97 /* Token.Ampersand_Ampersand */, includesCheck, equalsCheck, range),
|
|
99
|
+
// return false;
|
|
100
|
+
TypeNode.createReturnStatement(TypeNode.createFalseExpression(range), range), null, range);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* This method creates a single property access and passes the given range to the AST.
|
|
104
|
+
*
|
|
105
|
+
* @param {string} root - The name of the identifier representing the root.
|
|
106
|
+
* @param {string} property - The name of the identifier representing the property.
|
|
107
|
+
* @param {Range} range - The range of the property access.
|
|
108
|
+
*/
|
|
109
|
+
function createPropertyAccess(root, property, range) {
|
|
110
|
+
// root.property
|
|
111
|
+
return TypeNode.createPropertyAccessExpression(TypeNode.createIdentifierExpression(root, range), TypeNode.createIdentifierExpression(property, range), range);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* This method creates the function call into super.__aspectStrictEquals,
|
|
115
|
+
* wrapping it in a check to make sure the super function is defined first.
|
|
116
|
+
*
|
|
117
|
+
* @param {ClassDeclaration} classDeclaration - The given class declaration.
|
|
118
|
+
* @param {number[]} nameHashes - A collection of hash values of the comparing class properties.
|
|
119
|
+
*/
|
|
120
|
+
function createSuperCallStatement(classDeclaration, nameHashes) {
|
|
121
|
+
const range = classDeclaration.name.range;
|
|
122
|
+
const ifStatement = TypeNode.createIfStatement(TypeNode.createCallExpression(TypeNode.createIdentifierExpression("isDefined", range), null, [
|
|
123
|
+
TypeNode.createPropertyAccessExpression(TypeNode.createSuperExpression(range), TypeNode.createIdentifierExpression(STRICT_EQUALS_MEMBER_NAME, range), range),
|
|
124
|
+
], range), TypeNode.createBlockStatement([
|
|
125
|
+
TypeNode.createIfStatement(TypeNode.createUnaryPrefixExpression(95 /* Token.Exclamation */, createSuperCallExpression(nameHashes, range), range), TypeNode.createReturnStatement(TypeNode.createFalseExpression(range), range), null, range),
|
|
126
|
+
], range), null, range);
|
|
127
|
+
return ifStatement;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* This method actually creates the super.__aspectStrictEquals function call.
|
|
131
|
+
*
|
|
132
|
+
* @param {number[]} hashValues - The collection of hashed property name values
|
|
133
|
+
* @param {Range} range - The super call expression range
|
|
134
|
+
*/
|
|
135
|
+
function createSuperCallExpression(hashValues, range) {
|
|
136
|
+
return TypeNode.createCallExpression(TypeNode.createPropertyAccessExpression(TypeNode.createSuperExpression(range), TypeNode.createIdentifierExpression(STRICT_EQUALS_MEMBER_NAME, range), range), null, [
|
|
137
|
+
TypeNode.createIdentifierExpression("ref", range),
|
|
138
|
+
TypeNode.createIdentifierExpression("stack", range),
|
|
139
|
+
TypeNode.createIdentifierExpression("cache", range),
|
|
140
|
+
// StaticArray.concat(ignore, [... props] as StaticArray<i64>)
|
|
141
|
+
createInheritedIgnoreListExpression(hashValues, range),
|
|
142
|
+
], range);
|
|
143
|
+
}
|
package/lib/emptyTransformer.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Transform } from "assemblyscript/dist/transform.js";
|
|
2
|
-
/**
|
|
3
|
-
* Just an empty transformer.
|
|
4
|
-
*/
|
|
5
|
-
export default class AspectTransform extends Transform {
|
|
6
|
-
// @ts-ignore
|
|
7
|
-
afterParse(_parser) { }
|
|
8
|
-
}
|
|
1
|
+
import { Transform } from "assemblyscript/dist/transform.js";
|
|
2
|
+
/**
|
|
3
|
+
* Just an empty transformer.
|
|
4
|
+
*/
|
|
5
|
+
export default class AspectTransform extends Transform {
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
afterParse(_parser) { }
|
|
8
|
+
}
|
package/lib/hash.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A simple djb2hash that returns a hash of a given string. See http://www.cse.yorku.ca/~oz/hash.html
|
|
3
|
-
* for implementation details.
|
|
4
|
-
*
|
|
5
|
-
* @param {string} str - The string to be hashed
|
|
6
|
-
* @returns {number} The hash of the string
|
|
7
|
-
*/
|
|
8
|
-
export function djb2Hash(str) {
|
|
9
|
-
const points = Array.from(str);
|
|
10
|
-
let h = 5381;
|
|
11
|
-
for (let p = 0; p < points.length; p++)
|
|
12
|
-
// h = h * 33 + c;
|
|
13
|
-
h = (h << 5) + h + points[p].codePointAt(0);
|
|
14
|
-
return h;
|
|
15
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* A simple djb2hash that returns a hash of a given string. See http://www.cse.yorku.ca/~oz/hash.html
|
|
3
|
+
* for implementation details.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} str - The string to be hashed
|
|
6
|
+
* @returns {number} The hash of the string
|
|
7
|
+
*/
|
|
8
|
+
export function djb2Hash(str) {
|
|
9
|
+
const points = Array.from(str);
|
|
10
|
+
let h = 5381;
|
|
11
|
+
for (let p = 0; p < points.length; p++)
|
|
12
|
+
// h = h * 33 + c;
|
|
13
|
+
h = (h << 5) + h + points[p].codePointAt(0);
|
|
14
|
+
return h;
|
|
15
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -1,37 +1,36 @@
|
|
|
1
|
-
import { Transform } from "assemblyscript/dist/transform.js";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
1
|
+
import { Transform } from "assemblyscript/dist/transform.js";
|
|
2
|
+
import { appendGeneratedClassReflectionMembers, appendGeneratedInterfaceReflectionMembers, } from "./appendGeneratedClassReflectionMembers.js";
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
export default class AspectTransform extends Transform {
|
|
5
|
+
/**
|
|
6
|
+
* This method results in a pure AST transform that inserts a strictEquals member
|
|
7
|
+
* into each ClassDeclaration.
|
|
8
|
+
*
|
|
9
|
+
* @param {Parser} parser - The AssemblyScript parser.
|
|
10
|
+
*/
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
afterParse(parser) {
|
|
13
|
+
// For backwards compatibility
|
|
14
|
+
let sources = parser.program ? parser.program.sources : parser.sources;
|
|
15
|
+
// for each program source
|
|
16
|
+
for (const source of sources) {
|
|
17
|
+
traverseStatements(source.statements);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function traverseStatements(statements) {
|
|
22
|
+
// for each statement in the source
|
|
23
|
+
for (const statement of statements) {
|
|
24
|
+
// find each class declaration
|
|
25
|
+
if (statement.kind === 52 /* NodeKind.ClassDeclaration */) {
|
|
26
|
+
appendGeneratedClassReflectionMembers(statement);
|
|
27
|
+
}
|
|
28
|
+
else if (statement.kind === 58 /* NodeKind.InterfaceDeclaration */) {
|
|
29
|
+
appendGeneratedInterfaceReflectionMembers(statement);
|
|
30
|
+
}
|
|
31
|
+
else if (statement.kind === 60 /* NodeKind.NamespaceDeclaration */) {
|
|
32
|
+
const namespaceDeclaration = statement;
|
|
33
|
+
traverseStatements(namespaceDeclaration.members);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|