@@ -3,12 +3,17 @@ package polymod.hscript._internal;
3
3
import haxe .macro .Context ;
4
4
import haxe .macro .Expr ;
5
5
import haxe .macro .Type ;
6
+ import haxe .rtti .Meta ;
6
7
7
8
class PolymodFinalMacro
8
9
{
10
+ private static var _allFinals : Map <String , Array <String >> = null ;
11
+
9
12
public static function getAllFinals (): Map <String , Array <String >>
10
13
{
11
- return Reflect .callMethod (null , Reflect .field (Type .resolveClass (" polymod.hscript._internal.PolymodFinals" ), " getAllFinals" ), []);
14
+ if (_allFinals == null )
15
+ _allFinals = PolymodFinalMacro .fetchAllFinals ();
16
+ return _allFinals ;
12
17
}
13
18
14
19
public static macro function locateAllFinals (): Void
@@ -18,53 +23,45 @@ class PolymodFinalMacro
18
23
if (calledBefore )
19
24
return ;
20
25
21
- var allFinals : Map < String , Array <String > > = [];
26
+ var allFinals : Array <Expr > = [];
22
27
23
28
for (type in types )
24
29
{
25
30
switch (type )
26
31
{
27
32
case TClassDecl (t ):
28
33
var classType : ClassType = t .get ();
29
- var className : String = t .toString ();
34
+ var classPath : String = t .toString ();
30
35
if (classType .isInterface ) continue ;
31
36
32
- allFinals . set ( className , []) ;
37
+ var finals : Array < String > = [] ;
33
38
for (field in classType .statics .get ())
34
39
{
35
40
if (! field .isFinal ) continue ;
36
- allFinals [ className ] .push (field .name );
41
+ finals .push (field .name );
37
42
}
38
43
44
+ var entryData = [
45
+ macro $v {classPath },
46
+ macro $v {finals }
47
+ ];
48
+
49
+ allFinals .push (macro $a {entryData });
39
50
default :
40
51
continue ;
41
52
}
42
53
}
43
54
44
- Context .defineModule (' polymod.hscript._internal.PolymodFinalMacro' , [
45
- {
46
- pack : [' polymod' , ' hscript' , ' _internal' ],
47
- name : ' PolymodFinals' ,
48
- kind : TypeDefKind . TDClass (null , [], false , false , false ),
49
- fields : [
50
- {
51
- name : ' getAllFinals' ,
52
- access : [Access . APublic , Access . AStatic ],
53
- kind : FieldType . FFun (
54
- {
55
- args : [],
56
- ret : (macro : Map <String , Array <String >>),
57
- expr : macro
58
- {
59
- return $v {allFinals };
60
- }
61
- }),
62
- pos : Context .currentPos ()
63
- }
64
- ],
65
- pos : Context .currentPos ()
66
- }
67
- ]);
55
+ var finalMacroType : Type = Context .getType (' polymod.hscript._internal.PolymodFinalMacro' );
56
+
57
+ switch (finalMacroType )
58
+ {
59
+ case TInst (t , _ ):
60
+ var finalMacroClassType : ClassType = t .get ();
61
+ finalMacroClassType .meta .add (' finals' , allFinals , Context .currentPos ());
62
+ default :
63
+ throw ' Could not find PolymodFinalMacro type' ;
64
+ }
68
65
69
66
calledBefore = true ;
70
67
});
@@ -73,4 +70,31 @@ class PolymodFinalMacro
73
70
#if macro
74
71
static var calledBefore : Bool = false ;
75
72
#end
73
+
74
+ public static function fetchAllFinals (): Map <String , Array <String >>
75
+ {
76
+ var metaData = Meta .getType (PolymodFinalMacro );
77
+
78
+ if (metaData .finals != null )
79
+ {
80
+ var result : Map <String , Array <String >> = [];
81
+
82
+ for (element in metaData .finals )
83
+ {
84
+ if (element .length != 2 )
85
+ throw ' Malformed element in finals: ' + element ;
86
+
87
+ var classPath : String = element [0 ];
88
+ var finals : Array <String > = element [1 ];
89
+
90
+ result .set (classPath , finals );
91
+ }
92
+
93
+ return result ;
94
+ }
95
+ else
96
+ {
97
+ throw ' No finals found in PolymodFinalMacro' ;
98
+ }
99
+ }
76
100
}
0 commit comments