Compare commits

..

4 Commits
0.7.1 ... 0.8.1

Author SHA1 Message Date
Juraj Kirchheim
810c2f9630 Release 0.8.1 2016-05-02 17:58:30 +02:00
Juraj Kirchheim
331f186dcd Minor API improvement. 2016-05-02 17:58:09 +02:00
Juraj Kirchheim
c406e0b844 Release 0.8.0 2016-05-02 17:54:13 +02:00
Juraj Kirchheim
43fb9eb16a Add build cache utility. 2016-05-02 17:45:45 +02:00
2 changed files with 70 additions and 2 deletions

View File

@@ -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": ""

View 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);
}
}