Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
810c2f9630 | ||
![]() |
331f186dcd | ||
![]() |
c406e0b844 | ||
![]() |
43fb9eb16a |
@@ -11,8 +11,8 @@
|
||||
"contributors": [
|
||||
"back2dos"
|
||||
],
|
||||
"releasenote": "Avoid stale fields cache.",
|
||||
"version": "0.7.1",
|
||||
"releasenote": "Minor API improvement.",
|
||||
"version": "0.8.1",
|
||||
"url": "http://haxetink.org/tink_macro",
|
||||
"dependencies": {
|
||||
"tink_core": ""
|
||||
|
68
src/tink/macro/BuildCache.hx
Normal file
68
src/tink/macro/BuildCache.hx
Normal file
@@ -0,0 +1,68 @@
|
||||
package tink.macro;
|
||||
|
||||
import haxe.macro.Context;
|
||||
import haxe.macro.Expr;
|
||||
import haxe.macro.Type;
|
||||
import tink.macro.TypeMap;
|
||||
|
||||
typedef BuildContext = {
|
||||
pos:Position,
|
||||
type:Type,
|
||||
usings:Array<TypePath>,
|
||||
name:String,
|
||||
}
|
||||
|
||||
class BuildCache {
|
||||
|
||||
static var cache = init();
|
||||
|
||||
static function init() {
|
||||
|
||||
function refresh() {
|
||||
cache = new Map();
|
||||
return true;
|
||||
}
|
||||
|
||||
Context.onMacroContextReused(refresh);
|
||||
refresh();
|
||||
|
||||
return cache;
|
||||
}
|
||||
|
||||
static public function getType(name, ?type, ?pos:Position, build:BuildContext->TypeDefinition) {
|
||||
|
||||
if (pos == null)
|
||||
pos = Context.currentPos();
|
||||
|
||||
if (type == null)
|
||||
switch Context.getLocalType() {
|
||||
case TInst(_.toString() == name => true, [v]):
|
||||
type = v;
|
||||
default:
|
||||
throw 'assert';
|
||||
}
|
||||
|
||||
var forName =
|
||||
switch cache[name] {
|
||||
case null: cache[name] = new TypeMap();
|
||||
case v: v;
|
||||
}
|
||||
|
||||
if (!forName.exists(type)) {
|
||||
var path = '$name${Lambda.count(forName)}',
|
||||
usings = [];
|
||||
|
||||
var def = build({
|
||||
pos: pos,
|
||||
type: type,
|
||||
usings: usings,
|
||||
name: path.split('.').pop()
|
||||
});
|
||||
|
||||
Context.defineModule(path, [def], usings);
|
||||
forName.set(type, Context.getType(path));
|
||||
}
|
||||
|
||||
return forName.get(type);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user