Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
cc009b2026 | ||
![]() |
0a36ee8e2b | ||
![]() |
5879773566 | ||
![]() |
58262a1810 | ||
![]() |
9ceadeb88f | ||
![]() |
15d75965ad | ||
![]() |
c7634e0a29 | ||
![]() |
49e48bc801 | ||
![]() |
c150b84f18 | ||
![]() |
04f86ce341 | ||
![]() |
45380371d8 | ||
![]() |
d94d946301 |
@@ -1,5 +1,7 @@
|
|||||||
# Tinkerbell Macro Library
|
# Tinkerbell Macro Library
|
||||||
[](https://gitter.im/haxetink/public)
|
|
||||||
|
[](https://travis-ci.org/haxetink/tink_macro)
|
||||||
|
[](https://gitter.im/haxetink/public)
|
||||||
|
|
||||||
Explained in current marketing speak, `tink_macro` is *the* macro toolkit ;)
|
Explained in current marketing speak, `tink_macro` is *the* macro toolkit ;)
|
||||||
|
|
||||||
|
@@ -11,8 +11,8 @@
|
|||||||
"contributors": [
|
"contributors": [
|
||||||
"back2dos"
|
"back2dos"
|
||||||
],
|
],
|
||||||
"releasenote": "Fix 3.2.1.",
|
"releasenote": "Haxe 4 compatibility.",
|
||||||
"version": "0.13.5",
|
"version": "0.14.1",
|
||||||
"url": "http://haxetink.org/tink_macro",
|
"url": "http://haxetink.org/tink_macro",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"tink_core": ""
|
"tink_core": ""
|
||||||
|
@@ -9,51 +9,51 @@ using tink.MacroApi;
|
|||||||
using Lambda;
|
using Lambda;
|
||||||
|
|
||||||
class ClassBuilder {
|
class ClassBuilder {
|
||||||
|
|
||||||
var memberList:Array<Member>;
|
var memberList:Array<Member>;
|
||||||
var macros:Map<String,Field>;
|
var macros:Map<String,Field>;
|
||||||
var constructor:Null<Constructor>;
|
var constructor:Null<Constructor>;
|
||||||
public var target(default, null):ClassType;
|
public var target(default, null):ClassType;
|
||||||
var superFields:Map<String,Bool>;
|
var superFields:Map<String,Bool>;
|
||||||
|
|
||||||
var initializeFrom:Array<Field>;
|
var initializeFrom:Array<Field>;
|
||||||
|
|
||||||
public function new(?target, ?fields) {
|
public function new(?target, ?fields) {
|
||||||
if (target == null)
|
if (target == null)
|
||||||
target = Context.getLocalClass().get();
|
target = Context.getLocalClass().get();
|
||||||
|
|
||||||
if (fields == null)
|
if (fields == null)
|
||||||
fields = Context.getBuildFields();
|
fields = Context.getBuildFields();
|
||||||
|
|
||||||
this.initializeFrom = fields;
|
this.initializeFrom = fields;
|
||||||
this.target = target;
|
this.target = target;
|
||||||
}
|
}
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
if (initializeFrom == null) return;
|
if (initializeFrom == null) return;
|
||||||
|
|
||||||
var fields = initializeFrom;
|
var fields = initializeFrom;
|
||||||
initializeFrom = null;
|
initializeFrom = null;
|
||||||
|
|
||||||
this.memberList = [];
|
this.memberList = [];
|
||||||
this.macros = new Map();
|
this.macros = new Map();
|
||||||
|
|
||||||
for (field in fields)
|
for (field in fields)
|
||||||
if (field.access.has(AMacro))
|
if (field.access.has(AMacro))
|
||||||
macros.set(field.name, field)
|
macros.set(field.name, field)
|
||||||
else if (field.name == 'new') {
|
else if (field.name == 'new') {
|
||||||
var m:Member = field;
|
var m:Member = field;
|
||||||
this.constructor = new Constructor(this, m.getFunction().sure(), m.isPublic, m.pos, field.meta);
|
this.constructor = new Constructor(this, m.getFunction().sure(), m.isPublic, m.pos, field.meta);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
doAddMember(field);
|
doAddMember(field);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getConstructor(?fallback:Function):Constructor {
|
public function getConstructor(?fallback:Function):Constructor {
|
||||||
init();
|
init();
|
||||||
if (constructor == null)
|
if (constructor == null)
|
||||||
if (fallback != null)
|
if (fallback != null)
|
||||||
constructor = new Constructor(this, fallback);
|
constructor = new Constructor(this, fallback);
|
||||||
else {
|
else {
|
||||||
@@ -64,18 +64,18 @@ class ClassBuilder {
|
|||||||
try {
|
try {
|
||||||
var ctor = cl.constructor.get();
|
var ctor = cl.constructor.get();
|
||||||
var func = Context.getTypedExpr(ctor.expr()).getFunction().sure();
|
var func = Context.getTypedExpr(ctor.expr()).getFunction().sure();
|
||||||
|
|
||||||
for (arg in func.args) //this is to deal with type parameter substitutions
|
for (arg in func.args) //this is to deal with type parameter substitutions
|
||||||
arg.type = null;
|
arg.type = null;
|
||||||
|
|
||||||
func.expr = "super".resolve().call(func.getArgIdents());
|
func.expr = "super".resolve().call(func.getArgIdents());
|
||||||
constructor = new Constructor(this, func);
|
constructor = new Constructor(this, func);
|
||||||
if (ctor.isPublic)
|
if (ctor.isPublic)
|
||||||
constructor.publish();
|
constructor.publish();
|
||||||
}
|
}
|
||||||
catch (e:Dynamic) {//fails for unknown reason
|
catch (e:Dynamic) {//fails for unknown reason
|
||||||
if (e == 'assert')
|
if (e == 'assert')
|
||||||
neko.Lib.rethrow(e);
|
tink.core.Error.rethrow(e);
|
||||||
constructor = new Constructor(this, null);
|
constructor = new Constructor(this, null);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -85,15 +85,15 @@ class ClassBuilder {
|
|||||||
if (constructor == null)
|
if (constructor == null)
|
||||||
constructor = new Constructor(this, null);
|
constructor = new Constructor(this, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
return constructor;
|
return constructor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasConstructor():Bool {
|
public function hasConstructor():Bool {
|
||||||
init();
|
init();
|
||||||
return this.constructor != null;
|
return this.constructor != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function export(?verbose):Array<Field> {
|
public function export(?verbose):Array<Field> {
|
||||||
if (initializeFrom != null) return null;
|
if (initializeFrom != null) return null;
|
||||||
var ret = (constructor == null || target.isInterface) ? [] : [constructor.toHaxe()];
|
var ret = (constructor == null || target.isInterface) ? [] : [constructor.toHaxe()];
|
||||||
@@ -108,24 +108,24 @@ class ClassBuilder {
|
|||||||
}
|
}
|
||||||
for (m in macros)
|
for (m in macros)
|
||||||
ret.push(m);
|
ret.push(m);
|
||||||
|
|
||||||
if (verbose)
|
if (verbose)
|
||||||
for (field in ret)
|
for (field in ret)
|
||||||
Context.warning(new Printer().printField(field), field.pos);
|
Context.warning(new Printer().printField(field), field.pos);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
public function iterator():Iterator<Member> {
|
public function iterator():Iterator<Member> {
|
||||||
init();
|
init();
|
||||||
return this.memberList.copy().iterator();
|
return this.memberList.copy().iterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasOwnMember(name:String):Bool {
|
public function hasOwnMember(name:String):Bool {
|
||||||
init();
|
init();
|
||||||
return
|
return
|
||||||
macros.exists(name) || memberByName(name).isSuccess();
|
macros.exists(name) || memberByName(name).isSuccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasSuperField(name:String):Bool {
|
public function hasSuperField(name:String):Bool {
|
||||||
if (superFields == null) {
|
if (superFields == null) {
|
||||||
superFields = new Map();
|
superFields = new Map();
|
||||||
@@ -139,51 +139,51 @@ class ClassBuilder {
|
|||||||
}
|
}
|
||||||
return superFields.get(name);
|
return superFields.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function memberByName(name:String, ?pos:Position) {
|
public function memberByName(name:String, ?pos:Position) {
|
||||||
init();
|
init();
|
||||||
for (m in memberList)
|
for (m in memberList)
|
||||||
if (m.name == name)
|
if (m.name == name)
|
||||||
return Success(m);
|
return Success(m);
|
||||||
|
|
||||||
return pos.makeFailure('unknown member $name');
|
return pos.makeFailure('unknown member $name');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function removeMember(member:Member):Bool {
|
public function removeMember(member:Member):Bool {
|
||||||
init();
|
init();
|
||||||
return
|
return
|
||||||
memberList.remove(member);
|
memberList.remove(member);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasMember(name:String):Bool
|
public function hasMember(name:String):Bool
|
||||||
return hasOwnMember(name) || hasSuperField(name);
|
return hasOwnMember(name) || hasSuperField(name);
|
||||||
|
|
||||||
function doAddMember(m:Member, ?front:Bool = false):Member {
|
function doAddMember(m:Member, ?front:Bool = false):Member {
|
||||||
init();
|
init();
|
||||||
|
|
||||||
if (m.name == 'new')
|
if (m.name == 'new')
|
||||||
throw 'Constructor must not be registered as ordinary member';
|
throw 'Constructor must not be registered as ordinary member';
|
||||||
|
|
||||||
//if (hasOwnMember(m.name))
|
//if (hasOwnMember(m.name))
|
||||||
//m.pos.error('duplicate member declaration ' + m.name);
|
//m.pos.error('duplicate member declaration ' + m.name);
|
||||||
|
|
||||||
if (front)
|
if (front)
|
||||||
memberList.unshift(m);
|
memberList.unshift(m);
|
||||||
else
|
else
|
||||||
memberList.push(m);
|
memberList.push(m);
|
||||||
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addMember(m:Member, ?front:Bool = false):Member {
|
|
||||||
doAddMember(m, front);
|
|
||||||
|
|
||||||
if (!m.isStatic && hasSuperField(m.name))
|
|
||||||
m.overrides = true;
|
|
||||||
|
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function addMember(m:Member, ?front:Bool = false):Member {
|
||||||
|
doAddMember(m, front);
|
||||||
|
|
||||||
|
if (!m.isStatic && hasSuperField(m.name))
|
||||||
|
m.overrides = true;
|
||||||
|
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
static public function run(plugins:Array<ClassBuilder->Void>, ?verbose) {
|
static public function run(plugins:Array<ClassBuilder->Void>, ?verbose) {
|
||||||
var builder = new ClassBuilder();
|
var builder = new ClassBuilder();
|
||||||
for (p in plugins)
|
for (p in plugins)
|
||||||
|
@@ -241,7 +241,7 @@ class Exprs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static public inline function iterate(target:Expr, body:Expr, ?loopVar:String = 'i', ?pos:Position)
|
static public inline function iterate(target:Expr, body:Expr, ?loopVar:String = 'i', ?pos:Position)
|
||||||
return EFor(EIn(loopVar.resolve(pos), target).at(pos), body).at(pos);
|
return macro @:pos(pos.sanitize()) for ($i{loopVar} in $target) $body;
|
||||||
|
|
||||||
static public function toFields(object:Dynamic<Expr>, ?pos:Position)
|
static public function toFields(object:Dynamic<Expr>, ?pos:Position)
|
||||||
return EObjectDecl([for (field in Reflect.fields(object))
|
return EObjectDecl([for (field in Reflect.fields(object))
|
||||||
|
@@ -19,10 +19,10 @@ class Types {
|
|||||||
try {
|
try {
|
||||||
Some(Context.getType(typeName));
|
Some(Context.getType(typeName));
|
||||||
}
|
}
|
||||||
catch (e:Dynamic)
|
catch (e:Dynamic)
|
||||||
if (Std.string(e) == 'Type not found \'$typeName\'') None;
|
if (Std.string(e) == 'Type not found \'$typeName\'') None;
|
||||||
else neko.Lib.rethrow(e);
|
else tink.core.Error.rethrow(e);
|
||||||
|
|
||||||
static var types = new Map<Int,Void->Type>();
|
static var types = new Map<Int,Void->Type>();
|
||||||
static var idCounter = 0;
|
static var idCounter = 0;
|
||||||
static public function getID(t:Type, ?reduced = true)
|
static public function getID(t:Type, ?reduced = true)
|
||||||
@@ -49,7 +49,7 @@ class Types {
|
|||||||
throw 'not implemented';
|
throw 'not implemented';
|
||||||
}
|
}
|
||||||
|
|
||||||
static public function getMeta(type:Type)
|
static public function getMeta(type:Type)
|
||||||
return switch type {
|
return switch type {
|
||||||
case TInst(_.get().meta => m, _): [m];
|
case TInst(_.get().meta => m, _): [m];
|
||||||
case TEnum(_.get().meta => m, _): [m];
|
case TEnum(_.get().meta => m, _): [m];
|
||||||
@@ -183,14 +183,14 @@ class Types {
|
|||||||
params : [TPExpr(register(f).toExpr())],
|
params : [TPExpr(register(f).toExpr())],
|
||||||
sub : null,
|
sub : null,
|
||||||
});
|
});
|
||||||
|
|
||||||
static function resolveDirectType()
|
static function resolveDirectType()
|
||||||
return
|
return
|
||||||
switch reduce(Context.getLocalType()) {
|
switch reduce(Context.getLocalType()) {
|
||||||
case TInst(_, [TInst(_.get() => { kind: KExpr(e) }, _)]):
|
case TInst(_, [TInst(_.get() => { kind: KExpr(e) }, _)]):
|
||||||
types[e.getInt().sure()]();//When using compiler server, this call throws on occasion, in which case modifying this file (to update mtime and invalidate the cache) will solve the problem
|
types[e.getInt().sure()]();//When using compiler server, this call throws on occasion, in which case modifying this file (to update mtime and invalidate the cache) will solve the problem
|
||||||
default:
|
default:
|
||||||
throw 'assert';
|
throw 'assert';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user