forked from CodenameCrew/hscript-improved
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUsingMacro.hx
More file actions
173 lines (149 loc) · 4.92 KB
/
UsingMacro.hx
File metadata and controls
173 lines (149 loc) · 4.92 KB
1
2
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
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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package hscript.macros;
#if macro
import Type.ValueType;
import haxe.macro.Compiler;
import haxe.macro.Context;
import haxe.macro.Expr;
using Lambda;
using StringTools;
using haxe.macro.Tools;
/**
* Macro used for the `using Class;` keyword
*
* you can make classes be able to be used specifing the classes/packages on Config.hx!
* or implementing the interface `hscript.utils.UsingClass`
* ```haxe
* public static final ALLOWED_USING = ["my.pack.VeryNiceTools"];
* ```
*
* Usage:
*
* ```haxe
* package my.pack;
* // @:usableEntry() // optional
* // @:usableEntry(forceAny) // optional // forces the class to be called with any type
* // @:usableEntry(onlyBasic) // optional // only basic types will be allowed
* // @:usableEntry(onlyBasic, forceAny) // optional // only basic types will be allowed, and the class will be called with any type
* class VeryNiceTools implements hscript.utils.UsingClass {}
* ```
*
* @author NeeEoo
* @see https://github.com/pisayesiwsi/hscript-iris/blob/master/crowplexus/iris/macro/UsingMacro.macro.hx
**/
class UsingMacro {
public static inline final USING_PREFIX = "_HX_USING__";
public static var unallowedMetas:Array<String> = [":noUsing", ":noUse"];
public static function init() {
#if !display
if(Context.defined("display")) return;
for(apply in Config.ALLOWED_USING) {
Compiler.addGlobalMetadata(apply, "@:build(hscript.macros.UsingMacro.build())");
}
#end
}
public static function build() {
var cls: haxe.macro.Type.ClassType = Context.getLocalClass().get();
var fields = Context.getBuildFields();
var key = cls.module;
var fkey = cls.module + "." + cls.name;
var packName = (cls.pack.length > 0 ? cls.pack.join(".") + "." : "") + cls.name;
if(cls.meta.get().find(function(m) return m.name == ':usingProcessed') != null) return fields;
if (Config.DISALLOW_USING.contains(key) || Config.DISALLOW_USING.contains(fkey) || Config.DISALLOW_USING.contains(packName))
return fields;
var entryField = cls.meta.get().find(function(m) return m.name == ':usableEntry');
var hasParams = entryField != null && entryField.params != null;
var forceAny = false;
var onlyBasic = false;
if (hasParams) {
for (i in 0...entryField.params.length) {
if (entryField.params[i].expr.match(EConst(CIdent("onlyBasic"))))
onlyBasic = true;
if (entryField.params[i].expr.match(EConst(CIdent("forceAny"))))
forceAny = true;
}
}
var data: Array<Array<String>> = [];
for (field in fields) {
// functions marked with @:noUsing won't be able to be used by variables
// also if you want it to be usable in source, but not in the script, use @:noUse
for(m in field.meta)
if(unallowedMetas.contains(m.name))
continue;
// It's called "static extensions" for some reason
if(!field.access.contains(AStatic))
continue;
switch (field.kind) {
default:
case FFun(f):
if (f.args.length == 0)
continue;
var arg = f.args[0];
if (arg.type == null)
continue;
var type = arg.type;
var valueType: String = switch (type) {
case TPath({name: "Int", pack: []}):
"TInt";
case TPath({name: "Float", pack: []}):
"TFloat";
case TPath({name: "Single", pack: []}):
"TFloat";
case TPath({name: "String", pack: []}):
"TClass(String)";
case TPath({name: "Bool", pack: []}):
"TBool";
case TPath({name: "Array", pack: []}):
"TClass(Array)";
case TPath({name: "Map", pack: []}):
"TClass(haxe.Constraints.IMap)";
case TPath({name: "Dynamic", pack: []}):
null;
case TPath({name: "Class", pack: []}):
"TClass(null)"; // this feels wrong
case TPath({name: "Enum", pack: []}):
"TEnum(null)";
// case TPath({name: "Void", pack: []}): "ValueType.TVoid";
default:
null; // null acts as a wildcard
}
// MIGHT CRASH COMPILATION? YEAH, IT CRASH ON TYPE PARAMETERS
if (!onlyBasic && valueType == null) {
var rtype:haxe.macro.Type = null;
try {rtype = type.toType();} catch(e) {}
if(rtype != null) {
switch (rtype) {
case TInst(t, []):
valueType = "TClass(" + t.toString() + ")";
default:
}
}
}
if (forceAny) {
valueType = null;
}
data.push([field.name, valueType]);
}
}
if(data.length == 0)
return fields;
fields.push({
name: '$USING_PREFIX${packName.replace(".", "_")}',
access: [APrivate, AStatic],
kind: FVar(macro : Map<String, Type.ValueType>, {
var arr: Array<Expr> = [];
for (i in data)
if (i[1] != null)
arr.push(macro $v{i[0]} => ${Context.parse("Type.ValueType." + i[1], Context.currentPos())});
else
arr.push(macro $v{i[0]} => null);
macro $a{arr};
}),
pos: cls.pos,
});
//var printer = new haxe.macro.Printer();
//trace(printer.printField(fields[fields.length - 1]));
cls.meta.add(':usingProcessed', [], cls.pos);
return fields;
}
}
#end